]> git.ipfire.org Git - thirdparty/git.git/blame - editor.c
Sync with 2.16.6
[thirdparty/git.git] / editor.c
CommitLineData
d82f33e2
SB
1#include "cache.h"
2#include "strbuf.h"
3#include "run-command.h"
913ef360 4#include "sigchain.h"
d82f33e2 5
8f4b576a
JN
6#ifndef DEFAULT_EDITOR
7#define DEFAULT_EDITOR "vi"
8#endif
9
a64f213d
LS
10int is_terminal_dumb(void)
11{
12 const char *terminal = getenv("TERM");
13 return !terminal || !strcmp(terminal, "dumb");
14}
15
44fcb497 16const char *git_editor(void)
d82f33e2 17{
d33738d7 18 const char *editor = getenv("GIT_EDITOR");
a64f213d 19 int terminal_is_dumb = is_terminal_dumb();
d82f33e2 20
d82f33e2
SB
21 if (!editor && editor_program)
22 editor = editor_program;
d33738d7 23 if (!editor && !terminal_is_dumb)
d82f33e2
SB
24 editor = getenv("VISUAL");
25 if (!editor)
26 editor = getenv("EDITOR");
27
d33738d7 28 if (!editor && terminal_is_dumb)
44fcb497 29 return NULL;
d82f33e2
SB
30
31 if (!editor)
8f4b576a 32 editor = DEFAULT_EDITOR;
d82f33e2 33
44fcb497
JN
34 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
d82f33e2 44 if (strcmp(editor, ":")) {
09c5ae5a 45 const char *args[] = { editor, real_path(path), NULL };
d3180279 46 struct child_process p = CHILD_PROCESS_INIT;
1250857c 47 int ret, sig;
abfb04d0
LS
48 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 }
d82f33e2 65
f42ca31d
JK
66 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
913ef360
PF
72 sigchain_push(SIGINT, SIG_IGN);
73 sigchain_push(SIGQUIT, SIG_IGN);
74 ret = finish_command(&p);
709ca730 75 sig = ret - 128;
913ef360
PF
76 sigchain_pop(SIGINT);
77 sigchain_pop(SIGQUIT);
1250857c
JK
78 if (sig == SIGINT || sig == SIGQUIT)
79 raise(sig);
913ef360 80 if (ret)
7198203a
SB
81 return error("There was a problem with the editor '%s'.",
82 editor);
abfb04d0
LS
83
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);
d82f33e2
SB
90 }
91
92 if (!buffer)
7198203a 93 return 0;
d82f33e2 94 if (strbuf_read_file(buffer, path, 0) < 0)
9f9a522c 95 return error_errno("could not read file '%s'", path);
7198203a 96 return 0;
d82f33e2 97}