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