3 #include "run-command.h"
6 #define DEFAULT_EDITOR "vi"
9 const char *git_editor(void)
11 const char *editor
= getenv("GIT_EDITOR");
12 const char *terminal
= getenv("TERM");
13 int terminal_is_dumb
= !terminal
|| !strcmp(terminal
, "dumb");
15 if (!editor
&& editor_program
)
16 editor
= editor_program
;
17 if (!editor
&& !terminal_is_dumb
)
18 editor
= getenv("VISUAL");
20 editor
= getenv("EDITOR");
22 if (!editor
&& terminal_is_dumb
)
26 editor
= DEFAULT_EDITOR
;
31 int launch_editor(const char *path
, struct strbuf
*buffer
, const char *const *env
)
33 const char *editor
= git_editor();
36 return error("Terminal is dumb, but EDITOR unset");
38 if (strcmp(editor
, ":")) {
39 size_t len
= strlen(editor
);
43 struct strbuf arg0
= STRBUF_INIT
;
45 if (strcspn(editor
, "|&;<>()$`\\\"' \t\n*?[#~=%") != len
) {
46 /* there are specials */
47 strbuf_addf(&arg0
, "%s \"$@\"", editor
);
56 failed
= run_command_v_opt_cd_env(args
, 0, NULL
, env
);
57 strbuf_release(&arg0
);
59 return error("There was a problem with the editor '%s'.",
65 if (strbuf_read_file(buffer
, path
, 0) < 0)
66 return error("could not read file '%s': %s",
67 path
, strerror(errno
));