blob: 27bdecdaf3d99d3179737e07be971d2458408660 [file] [log] [blame]
Stephan Beyerd82f33e2008-07-25 18:28:41 +02001#include "cache.h"
2#include "strbuf.h"
3#include "run-command.h"
Paul Fox913ef362012-11-30 17:41:26 -05004#include "sigchain.h"
Stephan Beyerd82f33e2008-07-25 18:28:41 +02005
Jonathan Nieder8f4b5762009-10-30 20:44:41 -05006#ifndef DEFAULT_EDITOR
7#define DEFAULT_EDITOR "vi"
8#endif
9
Jonathan Nieder44fcb492009-11-11 18:01:27 -060010const char *git_editor(void)
Stephan Beyerd82f33e2008-07-25 18:28:41 +020011{
Jonathan Niederd33738d2009-11-11 17:56:07 -060012 const char *editor = getenv("GIT_EDITOR");
13 const char *terminal = getenv("TERM");
14 int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
Stephan Beyerd82f33e2008-07-25 18:28:41 +020015
Stephan Beyerd82f33e2008-07-25 18:28:41 +020016 if (!editor && editor_program)
17 editor = editor_program;
Jonathan Niederd33738d2009-11-11 17:56:07 -060018 if (!editor && !terminal_is_dumb)
Stephan Beyerd82f33e2008-07-25 18:28:41 +020019 editor = getenv("VISUAL");
20 if (!editor)
21 editor = getenv("EDITOR");
22
Jonathan Niederd33738d2009-11-11 17:56:07 -060023 if (!editor && terminal_is_dumb)
Jonathan Nieder44fcb492009-11-11 18:01:27 -060024 return NULL;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020025
26 if (!editor)
Jonathan Nieder8f4b5762009-10-30 20:44:41 -050027 editor = DEFAULT_EDITOR;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020028
Jonathan Nieder44fcb492009-11-11 18:01:27 -060029 return editor;
30}
31
32int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
33{
34 const char *editor = git_editor();
35
36 if (!editor)
37 return error("Terminal is dumb, but EDITOR unset");
38
Stephan Beyerd82f33e2008-07-25 18:28:41 +020039 if (strcmp(editor, ":")) {
Jeff Kingbac80372009-12-30 05:56:16 -050040 const char *args[] = { editor, path, NULL };
Jeff Kingf42ca312012-11-30 17:41:04 -050041 struct child_process p;
Jeff King12508572012-11-30 17:41:50 -050042 int ret, sig;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020043
Jeff Kingf42ca312012-11-30 17:41:04 -050044 memset(&p, 0, sizeof(p));
45 p.argv = args;
46 p.env = env;
47 p.use_shell = 1;
48 if (start_command(&p) < 0)
49 return error("unable to start editor '%s'", editor);
50
Paul Fox913ef362012-11-30 17:41:26 -050051 sigchain_push(SIGINT, SIG_IGN);
52 sigchain_push(SIGQUIT, SIG_IGN);
53 ret = finish_command(&p);
Jeff King709ca732013-01-05 09:49:49 -050054 sig = ret - 128;
Paul Fox913ef362012-11-30 17:41:26 -050055 sigchain_pop(SIGINT);
56 sigchain_pop(SIGQUIT);
Jeff King12508572012-11-30 17:41:50 -050057 if (sig == SIGINT || sig == SIGQUIT)
58 raise(sig);
Paul Fox913ef362012-11-30 17:41:26 -050059 if (ret)
Stephan Beyer71982032008-07-25 18:28:42 +020060 return error("There was a problem with the editor '%s'.",
61 editor);
Stephan Beyerd82f33e2008-07-25 18:28:41 +020062 }
63
64 if (!buffer)
Stephan Beyer71982032008-07-25 18:28:42 +020065 return 0;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020066 if (strbuf_read_file(buffer, path, 0) < 0)
Stephan Beyer71982032008-07-25 18:28:42 +020067 return error("could not read file '%s': %s",
68 path, strerror(errno));
69 return 0;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020070}