]> git.ipfire.org Git - thirdparty/git.git/blob - editor.c
cmake: handle also unit tests
[thirdparty/git.git] / editor.c
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"
14 #include "wrapper.h"
15
16 #ifndef DEFAULT_EDITOR
17 #define DEFAULT_EDITOR "vi"
18 #endif
19
20 int is_terminal_dumb(void)
21 {
22 const char *terminal = getenv("TERM");
23 return !terminal || !strcmp(terminal, "dumb");
24 }
25
26 const char *git_editor(void)
27 {
28 const char *editor = getenv("GIT_EDITOR");
29 int terminal_is_dumb = is_terminal_dumb();
30
31 if (!editor && editor_program)
32 editor = editor_program;
33 if (!editor && !terminal_is_dumb)
34 editor = getenv("VISUAL");
35 if (!editor)
36 editor = getenv("EDITOR");
37
38 if (!editor && terminal_is_dumb)
39 return NULL;
40
41 if (!editor)
42 editor = DEFAULT_EDITOR;
43
44 return editor;
45 }
46
47 const char *git_sequence_editor(void)
48 {
49 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
50
51 if (!editor)
52 git_config_get_string_tmp("sequence.editor", &editor);
53 if (!editor)
54 editor = git_editor();
55
56 return editor;
57 }
58
59 static int launch_specified_editor(const char *editor, const char *path,
60 struct strbuf *buffer, const char *const *env)
61 {
62 if (!editor)
63 return error("Terminal is dumb, but EDITOR unset");
64
65 if (strcmp(editor, ":")) {
66 struct strbuf realpath = STRBUF_INIT;
67 struct child_process p = CHILD_PROCESS_INIT;
68 int ret, sig;
69 int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
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 }
86
87 strbuf_realpath(&realpath, path, 1);
88
89 strvec_pushl(&p.args, editor, realpath.buf, NULL);
90 if (env)
91 strvec_pushv(&p.env, (const char **)env);
92 p.use_shell = 1;
93 p.trace2_child_class = "editor";
94 if (start_command(&p) < 0) {
95 strbuf_release(&realpath);
96 return error("unable to start editor '%s'", editor);
97 }
98
99 sigchain_push(SIGINT, SIG_IGN);
100 sigchain_push(SIGQUIT, SIG_IGN);
101 ret = finish_command(&p);
102 strbuf_release(&realpath);
103 sig = ret - 128;
104 sigchain_pop(SIGINT);
105 sigchain_pop(SIGQUIT);
106 if (sig == SIGINT || sig == SIGQUIT)
107 raise(sig);
108 if (ret)
109 return error("There was a problem with the editor '%s'.",
110 editor);
111
112 if (print_waiting_for_editor && !is_terminal_dumb())
113 /*
114 * Erase the entire line to avoid wasting the
115 * vertical space.
116 */
117 term_clear_line();
118 }
119
120 if (!buffer)
121 return 0;
122 if (strbuf_read_file(buffer, path, 0) < 0)
123 return error_errno("could not read file '%s'", path);
124 return 0;
125 }
126
127 int 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
132 int 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 }
137
138 int 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 }