]> git.ipfire.org Git - thirdparty/git.git/blame - editor.c
Move launch_editor() from builtin-tag.c to editor.c
[thirdparty/git.git] / editor.c
CommitLineData
d82f33e2
SB
1#include "cache.h"
2#include "strbuf.h"
3#include "run-command.h"
4
5void launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
6{
7 const char *editor, *terminal;
8
9 editor = getenv("GIT_EDITOR");
10 if (!editor && editor_program)
11 editor = editor_program;
12 if (!editor)
13 editor = getenv("VISUAL");
14 if (!editor)
15 editor = getenv("EDITOR");
16
17 terminal = getenv("TERM");
18 if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
19 fprintf(stderr,
20 "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
21 "Please supply the message using either -m or -F option.\n");
22 exit(1);
23 }
24
25 if (!editor)
26 editor = "vi";
27
28 if (strcmp(editor, ":")) {
29 size_t len = strlen(editor);
30 int i = 0;
31 const char *args[6];
32 struct strbuf arg0;
33
34 strbuf_init(&arg0, 0);
35 if (strcspn(editor, "$ \t'") != len) {
36 /* there are specials */
37 strbuf_addf(&arg0, "%s \"$@\"", editor);
38 args[i++] = "sh";
39 args[i++] = "-c";
40 args[i++] = arg0.buf;
41 }
42 args[i++] = editor;
43 args[i++] = path;
44 args[i] = NULL;
45
46 if (run_command_v_opt_cd_env(args, 0, NULL, env))
47 die("There was a problem with the editor %s.", editor);
48 strbuf_release(&arg0);
49 }
50
51 if (!buffer)
52 return;
53 if (strbuf_read_file(buffer, path, 0) < 0)
54 die("could not read message file '%s': %s",
55 path, strerror(errno));
56}