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