]> git.ipfire.org Git - thirdparty/git.git/blame - editor.c
Merge branch 'jc/maint-imap-config-parse' into maint
[thirdparty/git.git] / editor.c
CommitLineData
d82f33e2
SB
1#include "cache.h"
2#include "strbuf.h"
3#include "run-command.h"
4
8f4b576a
JN
5#ifndef DEFAULT_EDITOR
6#define DEFAULT_EDITOR "vi"
7#endif
8
44fcb497 9const char *git_editor(void)
d82f33e2 10{
d33738d7
JN
11 const char *editor = getenv("GIT_EDITOR");
12 const char *terminal = getenv("TERM");
13 int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
d82f33e2 14
d82f33e2
SB
15 if (!editor && editor_program)
16 editor = editor_program;
d33738d7 17 if (!editor && !terminal_is_dumb)
d82f33e2
SB
18 editor = getenv("VISUAL");
19 if (!editor)
20 editor = getenv("EDITOR");
21
d33738d7 22 if (!editor && terminal_is_dumb)
44fcb497 23 return NULL;
d82f33e2
SB
24
25 if (!editor)
8f4b576a 26 editor = DEFAULT_EDITOR;
d82f33e2 27
44fcb497
JN
28 return editor;
29}
30
31int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
32{
33 const char *editor = git_editor();
34
35 if (!editor)
36 return error("Terminal is dumb, but EDITOR unset");
37
d82f33e2
SB
38 if (strcmp(editor, ":")) {
39 size_t len = strlen(editor);
40 int i = 0;
7198203a 41 int failed;
d82f33e2 42 const char *args[6];
f285a2d7 43 struct strbuf arg0 = STRBUF_INIT;
d82f33e2 44
eab58f1e 45 if (strcspn(editor, "|&;<>()$`\\\"' \t\n*?[#~=%") != len) {
d82f33e2
SB
46 /* there are specials */
47 strbuf_addf(&arg0, "%s \"$@\"", editor);
48 args[i++] = "sh";
49 args[i++] = "-c";
50 args[i++] = arg0.buf;
51 }
52 args[i++] = editor;
53 args[i++] = path;
54 args[i] = NULL;
55
7198203a 56 failed = run_command_v_opt_cd_env(args, 0, NULL, env);
d82f33e2 57 strbuf_release(&arg0);
7198203a
SB
58 if (failed)
59 return error("There was a problem with the editor '%s'.",
60 editor);
d82f33e2
SB
61 }
62
63 if (!buffer)
7198203a 64 return 0;
d82f33e2 65 if (strbuf_read_file(buffer, path, 0) < 0)
7198203a
SB
66 return error("could not read file '%s': %s",
67 path, strerror(errno));
68 return 0;
d82f33e2 69}