blob: 9a9b4e12d1db7f8ec2f4f99d9a5f348b680cecc3 [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
Lars Schneidera64f2132017-11-29 15:37:51 +010010int is_terminal_dumb(void)
11{
12 const char *terminal = getenv("TERM");
13 return !terminal || !strcmp(terminal, "dumb");
14}
15
Jonathan Nieder44fcb492009-11-11 18:01:27 -060016const char *git_editor(void)
Stephan Beyerd82f33e2008-07-25 18:28:41 +020017{
Jonathan Niederd33738d2009-11-11 17:56:07 -060018 const char *editor = getenv("GIT_EDITOR");
Lars Schneidera64f2132017-11-29 15:37:51 +010019 int terminal_is_dumb = is_terminal_dumb();
Stephan Beyerd82f33e2008-07-25 18:28:41 +020020
Stephan Beyerd82f33e2008-07-25 18:28:41 +020021 if (!editor && editor_program)
22 editor = editor_program;
Jonathan Niederd33738d2009-11-11 17:56:07 -060023 if (!editor && !terminal_is_dumb)
Stephan Beyerd82f33e2008-07-25 18:28:41 +020024 editor = getenv("VISUAL");
25 if (!editor)
26 editor = getenv("EDITOR");
27
Jonathan Niederd33738d2009-11-11 17:56:07 -060028 if (!editor && terminal_is_dumb)
Jonathan Nieder44fcb492009-11-11 18:01:27 -060029 return NULL;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020030
31 if (!editor)
Jonathan Nieder8f4b5762009-10-30 20:44:41 -050032 editor = DEFAULT_EDITOR;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020033
Jonathan Nieder44fcb492009-11-11 18:01:27 -060034 return editor;
35}
36
37int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
38{
39 const char *editor = git_editor();
40
41 if (!editor)
42 return error("Terminal is dumb, but EDITOR unset");
43
Stephan Beyerd82f33e2008-07-25 18:28:41 +020044 if (strcmp(editor, ":")) {
Ramkumar Ramachandra09c5ae52013-07-28 22:29:42 +053045 const char *args[] = { editor, real_path(path), NULL };
René Scharfed3180272014-08-19 21:09:35 +020046 struct child_process p = CHILD_PROCESS_INIT;
Jeff King12508572012-11-30 17:41:50 -050047 int ret, sig;
Lars Schneiderabfb04d2017-12-07 16:16:41 +010048 int print_waiting_for_editor = advice_waiting_for_editor && isatty(2);
49
50 if (print_waiting_for_editor) {
51 /*
52 * A dumb terminal cannot erase the line later on. Add a
53 * newline to separate the hint from subsequent output.
54 *
55 * Make sure that our message is separated with a whitespace
56 * from further cruft that may be written by the editor.
57 */
58 const char term = is_terminal_dumb() ? '\n' : ' ';
59
60 fprintf(stderr,
61 _("hint: Waiting for your editor to close the file...%c"),
62 term);
63 fflush(stderr);
64 }
Stephan Beyerd82f33e2008-07-25 18:28:41 +020065
Jeff Kingf42ca312012-11-30 17:41:04 -050066 p.argv = args;
67 p.env = env;
68 p.use_shell = 1;
69 if (start_command(&p) < 0)
70 return error("unable to start editor '%s'", editor);
71
Paul Fox913ef362012-11-30 17:41:26 -050072 sigchain_push(SIGINT, SIG_IGN);
73 sigchain_push(SIGQUIT, SIG_IGN);
74 ret = finish_command(&p);
Jeff King709ca732013-01-05 09:49:49 -050075 sig = ret - 128;
Paul Fox913ef362012-11-30 17:41:26 -050076 sigchain_pop(SIGINT);
77 sigchain_pop(SIGQUIT);
Jeff King12508572012-11-30 17:41:50 -050078 if (sig == SIGINT || sig == SIGQUIT)
79 raise(sig);
Paul Fox913ef362012-11-30 17:41:26 -050080 if (ret)
Stephan Beyer71982032008-07-25 18:28:42 +020081 return error("There was a problem with the editor '%s'.",
82 editor);
Lars Schneiderabfb04d2017-12-07 16:16:41 +010083
84 if (print_waiting_for_editor && !is_terminal_dumb())
85 /*
86 * Go back to the beginning and erase the entire line to
87 * avoid wasting the vertical space.
88 */
89 fputs("\r\033[K", stderr);
Stephan Beyerd82f33e2008-07-25 18:28:41 +020090 }
91
92 if (!buffer)
Stephan Beyer71982032008-07-25 18:28:42 +020093 return 0;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020094 if (strbuf_read_file(buffer, path, 0) < 0)
Nguyễn Thái Ngọc Duy9f9a5222016-05-08 16:47:43 +070095 return error_errno("could not read file '%s'", path);
Stephan Beyer71982032008-07-25 18:28:42 +020096 return 0;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020097}