]> git.ipfire.org Git - thirdparty/git.git/blame - editor.c
Merge branch 'rj/add-i-leak-fix'
[thirdparty/git.git] / editor.c
CommitLineData
77f091ed 1#include "git-compat-util.h"
0b027f6c 2#include "abspath.h"
6c6ddf92 3#include "advice.h"
2aed0181 4#include "config.h"
4e120823 5#include "editor.h"
32a8f510 6#include "environment.h"
f394e093 7#include "gettext.h"
ca4eed70 8#include "pager.h"
d1cbe1e6 9#include "path.h"
d82f33e2 10#include "strbuf.h"
c7c4bdec 11#include "strvec.h"
d82f33e2 12#include "run-command.h"
913ef360 13#include "sigchain.h"
d82f33e2 14
8f4b576a
JN
15#ifndef DEFAULT_EDITOR
16#define DEFAULT_EDITOR "vi"
17#endif
18
a64f213d
LS
19int is_terminal_dumb(void)
20{
21 const char *terminal = getenv("TERM");
22 return !terminal || !strcmp(terminal, "dumb");
23}
24
44fcb497 25const char *git_editor(void)
d82f33e2 26{
d33738d7 27 const char *editor = getenv("GIT_EDITOR");
a64f213d 28 int terminal_is_dumb = is_terminal_dumb();
d82f33e2 29
d82f33e2
SB
30 if (!editor && editor_program)
31 editor = editor_program;
d33738d7 32 if (!editor && !terminal_is_dumb)
d82f33e2
SB
33 editor = getenv("VISUAL");
34 if (!editor)
35 editor = getenv("EDITOR");
36
d33738d7 37 if (!editor && terminal_is_dumb)
44fcb497 38 return NULL;
d82f33e2
SB
39
40 if (!editor)
8f4b576a 41 editor = DEFAULT_EDITOR;
d82f33e2 42
44fcb497
JN
43 return editor;
44}
45
2aed0181 46const char *git_sequence_editor(void)
44fcb497 47{
2aed0181
AG
48 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
49
50 if (!editor)
f1de981e 51 git_config_get_string_tmp("sequence.editor", &editor);
2aed0181
AG
52 if (!editor)
53 editor = git_editor();
44fcb497 54
2aed0181
AG
55 return editor;
56}
57
58static int launch_specified_editor(const char *editor, const char *path,
59 struct strbuf *buffer, const char *const *env)
60{
44fcb497
JN
61 if (!editor)
62 return error("Terminal is dumb, but EDITOR unset");
63
d82f33e2 64 if (strcmp(editor, ":")) {
3d7747e3 65 struct strbuf realpath = STRBUF_INIT;
d3180279 66 struct child_process p = CHILD_PROCESS_INIT;
1250857c 67 int ret, sig;
ed9bff08 68 int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
abfb04d0
LS
69
70 if (print_waiting_for_editor) {
71 /*
72 * A dumb terminal cannot erase the line later on. Add a
73 * newline to separate the hint from subsequent output.
74 *
75 * Make sure that our message is separated with a whitespace
76 * from further cruft that may be written by the editor.
77 */
78 const char term = is_terminal_dumb() ? '\n' : ' ';
79
80 fprintf(stderr,
81 _("hint: Waiting for your editor to close the file...%c"),
82 term);
83 fflush(stderr);
84 }
d82f33e2 85
3d7747e3 86 strbuf_realpath(&realpath, path, 1);
3d7747e3 87
2b709893 88 strvec_pushl(&p.args, editor, realpath.buf, NULL);
c7c4bdec 89 if (env)
29fda24d 90 strvec_pushv(&p.env, (const char **)env);
f42ca31d 91 p.use_shell = 1;
eee73d1d 92 p.trace2_child_class = "editor";
3d7747e3
AM
93 if (start_command(&p) < 0) {
94 strbuf_release(&realpath);
f42ca31d 95 return error("unable to start editor '%s'", editor);
3d7747e3 96 }
f42ca31d 97
913ef360
PF
98 sigchain_push(SIGINT, SIG_IGN);
99 sigchain_push(SIGQUIT, SIG_IGN);
100 ret = finish_command(&p);
3d7747e3 101 strbuf_release(&realpath);
709ca730 102 sig = ret - 128;
913ef360
PF
103 sigchain_pop(SIGINT);
104 sigchain_pop(SIGQUIT);
1250857c
JK
105 if (sig == SIGINT || sig == SIGQUIT)
106 raise(sig);
abfb04d0
LS
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();
48e1ca27
RJ
113 if (ret)
114 return error("there was a problem with the editor '%s'",
115 editor);
d82f33e2
SB
116 }
117
118 if (!buffer)
7198203a 119 return 0;
d82f33e2 120 if (strbuf_read_file(buffer, path, 0) < 0)
9f9a522c 121 return error_errno("could not read file '%s'", path);
7198203a 122 return 0;
d82f33e2 123}
2aed0181
AG
124
125int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
126{
127 return launch_specified_editor(git_editor(), path, buffer, env);
128}
129
130int launch_sequence_editor(const char *path, struct strbuf *buffer,
131 const char *const *env)
132{
133 return launch_specified_editor(git_sequence_editor(), path, buffer, env);
134}
4e120823
EN
135
136int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
137 const char *const *env)
138{
139 char *path2 = NULL;
140 int fd, res = 0;
141
142 if (!is_absolute_path(path))
143 path = path2 = xstrdup(git_path("%s", path));
144
145 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
146 if (fd < 0)
147 res = error_errno(_("could not open '%s' for writing"), path);
148 else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
149 res = error_errno(_("could not write to '%s'"), path);
150 close(fd);
151 } else if (close(fd) < 0)
152 res = error_errno(_("could not close '%s'"), path);
153 else {
154 strbuf_reset(buffer);
155 if (launch_editor(path, buffer, env) < 0)
156 res = error_errno(_("could not edit '%s'"), path);
157 unlink(path);
158 }
159
160 free(path2);
161 return res;
162}