70618f106699ececf18357b63f1cf394d1f2f3bb
   1#include "cache.h"
   2#include "strbuf.h"
   3#include "run-command.h"
   4
   5const char *git_editor(void)
   6{
   7        const char *editor = getenv("GIT_EDITOR");
   8        const char *terminal = getenv("TERM");
   9        int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
  10
  11        if (!editor && editor_program)
  12                editor = editor_program;
  13        if (!editor && !terminal_is_dumb)
  14                editor = getenv("VISUAL");
  15        if (!editor)
  16                editor = getenv("EDITOR");
  17
  18        if (!editor && terminal_is_dumb)
  19                return NULL;
  20
  21        if (!editor)
  22                editor = "vi";
  23
  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
  34        if (strcmp(editor, ":")) {
  35                size_t len = strlen(editor);
  36                int i = 0;
  37                int failed;
  38                const char *args[6];
  39                struct strbuf arg0 = STRBUF_INIT;
  40
  41                if (strcspn(editor, "|&;<>()$`\\\"' \t\n*?[#~=%") != len) {
  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
  52                failed = run_command_v_opt_cd_env(args, 0, NULL, env);
  53                strbuf_release(&arg0);
  54                if (failed)
  55                        return error("There was a problem with the editor '%s'.",
  56                                        editor);
  57        }
  58
  59        if (!buffer)
  60                return 0;
  61        if (strbuf_read_file(buffer, path, 0) < 0)
  62                return error("could not read file '%s': %s",
  63                                path, strerror(errno));
  64        return 0;
  65}