Sync with 2.43.4
[git/gitster.git] / editor.c
blobb67b802ddf8493ea25ccbb1fc5c046240bfe917d
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "advice.h"
4 #include "config.h"
5 #include "editor.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "pager.h"
9 #include "path.h"
10 #include "strbuf.h"
11 #include "strvec.h"
12 #include "run-command.h"
13 #include "sigchain.h"
15 #ifndef DEFAULT_EDITOR
16 #define DEFAULT_EDITOR "vi"
17 #endif
19 int is_terminal_dumb(void)
21 const char *terminal = getenv("TERM");
22 return !terminal || !strcmp(terminal, "dumb");
25 const char *git_editor(void)
27 const char *editor = getenv("GIT_EDITOR");
28 int terminal_is_dumb = is_terminal_dumb();
30 if (!editor && editor_program)
31 editor = editor_program;
32 if (!editor && !terminal_is_dumb)
33 editor = getenv("VISUAL");
34 if (!editor)
35 editor = getenv("EDITOR");
37 if (!editor && terminal_is_dumb)
38 return NULL;
40 if (!editor)
41 editor = DEFAULT_EDITOR;
43 return editor;
46 const char *git_sequence_editor(void)
48 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
50 if (!editor)
51 git_config_get_string_tmp("sequence.editor", &editor);
52 if (!editor)
53 editor = git_editor();
55 return editor;
58 static int launch_specified_editor(const char *editor, const char *path,
59 struct strbuf *buffer, const char *const *env)
61 if (!editor)
62 return error("Terminal is dumb, but EDITOR unset");
64 if (strcmp(editor, ":")) {
65 struct strbuf realpath = STRBUF_INIT;
66 struct child_process p = CHILD_PROCESS_INIT;
67 int ret, sig;
68 int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
70 if (print_waiting_for_editor) {
72 * A dumb terminal cannot erase the line later on. Add a
73 * newline to separate the hint from subsequent output.
75 * Make sure that our message is separated with a whitespace
76 * from further cruft that may be written by the editor.
78 const char term = is_terminal_dumb() ? '\n' : ' ';
80 fprintf(stderr,
81 _("hint: Waiting for your editor to close the file...%c"),
82 term);
83 fflush(stderr);
86 strbuf_realpath(&realpath, path, 1);
88 strvec_pushl(&p.args, editor, realpath.buf, NULL);
89 if (env)
90 strvec_pushv(&p.env, (const char **)env);
91 p.use_shell = 1;
92 p.trace2_child_class = "editor";
93 if (start_command(&p) < 0) {
94 strbuf_release(&realpath);
95 return error("unable to start editor '%s'", editor);
98 sigchain_push(SIGINT, SIG_IGN);
99 sigchain_push(SIGQUIT, SIG_IGN);
100 ret = finish_command(&p);
101 strbuf_release(&realpath);
102 sig = ret - 128;
103 sigchain_pop(SIGINT);
104 sigchain_pop(SIGQUIT);
105 if (sig == SIGINT || sig == SIGQUIT)
106 raise(sig);
107 if (ret)
108 return error("There was a problem with the editor '%s'.",
109 editor);
111 if (print_waiting_for_editor && !is_terminal_dumb())
113 * Erase the entire line to avoid wasting the
114 * vertical space.
116 term_clear_line();
119 if (!buffer)
120 return 0;
121 if (strbuf_read_file(buffer, path, 0) < 0)
122 return error_errno("could not read file '%s'", path);
123 return 0;
126 int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
128 return launch_specified_editor(git_editor(), path, buffer, env);
131 int launch_sequence_editor(const char *path, struct strbuf *buffer,
132 const char *const *env)
134 return launch_specified_editor(git_sequence_editor(), path, buffer, env);
137 int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
138 const char *const *env)
140 char *path2 = NULL;
141 int fd, res = 0;
143 if (!is_absolute_path(path))
144 path = path2 = xstrdup(git_path("%s", path));
146 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
147 if (fd < 0)
148 res = error_errno(_("could not open '%s' for writing"), path);
149 else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
150 res = error_errno(_("could not write to '%s'"), path);
151 close(fd);
152 } else if (close(fd) < 0)
153 res = error_errno(_("could not close '%s'"), path);
154 else {
155 strbuf_reset(buffer);
156 if (launch_editor(path, buffer, env) < 0)
157 res = error_errno(_("could not edit '%s'"), path);
158 unlink(path);
161 free(path2);
162 return res;