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