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