]> git.ipfire.org Git - thirdparty/git.git/blame - editor.c
Merge branch 'tl/ls-tree-oid-only'
[thirdparty/git.git] / editor.c
CommitLineData
d82f33e2 1#include "cache.h"
2aed0181 2#include "config.h"
d82f33e2 3#include "strbuf.h"
c7c4bdec 4#include "strvec.h"
d82f33e2 5#include "run-command.h"
913ef360 6#include "sigchain.h"
d82f33e2 7
8f4b576a
JN
8#ifndef DEFAULT_EDITOR
9#define DEFAULT_EDITOR "vi"
10#endif
11
a64f213d
LS
12int is_terminal_dumb(void)
13{
14 const char *terminal = getenv("TERM");
15 return !terminal || !strcmp(terminal, "dumb");
16}
17
44fcb497 18const char *git_editor(void)
d82f33e2 19{
d33738d7 20 const char *editor = getenv("GIT_EDITOR");
a64f213d 21 int terminal_is_dumb = is_terminal_dumb();
d82f33e2 22
d82f33e2
SB
23 if (!editor && editor_program)
24 editor = editor_program;
d33738d7 25 if (!editor && !terminal_is_dumb)
d82f33e2
SB
26 editor = getenv("VISUAL");
27 if (!editor)
28 editor = getenv("EDITOR");
29
d33738d7 30 if (!editor && terminal_is_dumb)
44fcb497 31 return NULL;
d82f33e2
SB
32
33 if (!editor)
8f4b576a 34 editor = DEFAULT_EDITOR;
d82f33e2 35
44fcb497
JN
36 return editor;
37}
38
2aed0181 39const char *git_sequence_editor(void)
44fcb497 40{
2aed0181
AG
41 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
42
43 if (!editor)
f1de981e 44 git_config_get_string_tmp("sequence.editor", &editor);
2aed0181
AG
45 if (!editor)
46 editor = git_editor();
44fcb497 47
2aed0181
AG
48 return editor;
49}
50
51static int launch_specified_editor(const char *editor, const char *path,
52 struct strbuf *buffer, const char *const *env)
53{
44fcb497
JN
54 if (!editor)
55 return error("Terminal is dumb, but EDITOR unset");
56
d82f33e2 57 if (strcmp(editor, ":")) {
3d7747e3 58 struct strbuf realpath = STRBUF_INIT;
d3180279 59 struct child_process p = CHILD_PROCESS_INIT;
1250857c 60 int ret, sig;
ed9bff08 61 int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
abfb04d0
LS
62
63 if (print_waiting_for_editor) {
64 /*
65 * A dumb terminal cannot erase the line later on. Add a
66 * newline to separate the hint from subsequent output.
67 *
68 * Make sure that our message is separated with a whitespace
69 * from further cruft that may be written by the editor.
70 */
71 const char term = is_terminal_dumb() ? '\n' : ' ';
72
73 fprintf(stderr,
74 _("hint: Waiting for your editor to close the file...%c"),
75 term);
76 fflush(stderr);
77 }
d82f33e2 78
3d7747e3 79 strbuf_realpath(&realpath, path, 1);
3d7747e3 80
2b709893 81 strvec_pushl(&p.args, editor, realpath.buf, NULL);
c7c4bdec
ÆAB
82 if (env)
83 strvec_pushv(&p.env_array, (const char **)env);
f42ca31d 84 p.use_shell = 1;
eee73d1d 85 p.trace2_child_class = "editor";
3d7747e3
AM
86 if (start_command(&p) < 0) {
87 strbuf_release(&realpath);
f42ca31d 88 return error("unable to start editor '%s'", editor);
3d7747e3 89 }
f42ca31d 90
913ef360
PF
91 sigchain_push(SIGINT, SIG_IGN);
92 sigchain_push(SIGQUIT, SIG_IGN);
93 ret = finish_command(&p);
3d7747e3 94 strbuf_release(&realpath);
709ca730 95 sig = ret - 128;
913ef360
PF
96 sigchain_pop(SIGINT);
97 sigchain_pop(SIGQUIT);
1250857c
JK
98 if (sig == SIGINT || sig == SIGQUIT)
99 raise(sig);
913ef360 100 if (ret)
7198203a
SB
101 return error("There was a problem with the editor '%s'.",
102 editor);
abfb04d0
LS
103
104 if (print_waiting_for_editor && !is_terminal_dumb())
105 /*
cd1096b2
SG
106 * Erase the entire line to avoid wasting the
107 * vertical space.
abfb04d0 108 */
cd1096b2 109 term_clear_line();
d82f33e2
SB
110 }
111
112 if (!buffer)
7198203a 113 return 0;
d82f33e2 114 if (strbuf_read_file(buffer, path, 0) < 0)
9f9a522c 115 return error_errno("could not read file '%s'", path);
7198203a 116 return 0;
d82f33e2 117}
2aed0181
AG
118
119int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
120{
121 return launch_specified_editor(git_editor(), path, buffer, env);
122}
123
124int launch_sequence_editor(const char *path, struct strbuf *buffer,
125 const char *const *env)
126{
127 return launch_specified_editor(git_sequence_editor(), path, buffer, env);
128}