blob: 7519edecdc297a0c0fdbbb2f3c68cb55abf2e8b2 [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, ":")) {
Ramkumar Ramachandra09c5ae52013-07-28 22:29:42 +053040 const char *args[] = { editor, real_path(path), NULL };
René Scharfed3180272014-08-19 21:09:35 +020041 struct child_process p = CHILD_PROCESS_INIT;
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 p.argv = args;
45 p.env = env;
46 p.use_shell = 1;
47 if (start_command(&p) < 0)
48 return error("unable to start editor '%s'", editor);
49
Paul Fox913ef362012-11-30 17:41:26 -050050 sigchain_push(SIGINT, SIG_IGN);
51 sigchain_push(SIGQUIT, SIG_IGN);
52 ret = finish_command(&p);
Jeff King709ca732013-01-05 09:49:49 -050053 sig = ret - 128;
Paul Fox913ef362012-11-30 17:41:26 -050054 sigchain_pop(SIGINT);
55 sigchain_pop(SIGQUIT);
Jeff King12508572012-11-30 17:41:50 -050056 if (sig == SIGINT || sig == SIGQUIT)
57 raise(sig);
Paul Fox913ef362012-11-30 17:41:26 -050058 if (ret)
Stephan Beyer71982032008-07-25 18:28:42 +020059 return error("There was a problem with the editor '%s'.",
60 editor);
Stephan Beyerd82f33e2008-07-25 18:28:41 +020061 }
62
63 if (!buffer)
Stephan Beyer71982032008-07-25 18:28:42 +020064 return 0;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020065 if (strbuf_read_file(buffer, path, 0) < 0)
Nguyễn Thái Ngọc Duy9f9a5222016-05-08 16:47:43 +070066 return error_errno("could not read file '%s'", path);
Stephan Beyer71982032008-07-25 18:28:42 +020067 return 0;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020068}