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