exec_cmd.con commit Update draft release notes to 2.2 (19b5d50)
   1#include "cache.h"
   2#include "exec_cmd.h"
   3#include "quote.h"
   4#define MAX_ARGS        32
   5
   6static const char *argv_exec_path;
   7static const char *argv0_path;
   8
   9const char *system_path(const char *path)
  10{
  11#ifdef RUNTIME_PREFIX
  12        static const char *prefix;
  13#else
  14        static const char *prefix = PREFIX;
  15#endif
  16        struct strbuf d = STRBUF_INIT;
  17
  18        if (is_absolute_path(path))
  19                return path;
  20
  21#ifdef RUNTIME_PREFIX
  22        assert(argv0_path);
  23        assert(is_absolute_path(argv0_path));
  24
  25        if (!prefix &&
  26            !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
  27            !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
  28            !(prefix = strip_path_suffix(argv0_path, "git"))) {
  29                prefix = PREFIX;
  30                trace_printf("RUNTIME_PREFIX requested, "
  31                                "but prefix computation failed.  "
  32                                "Using static fallback '%s'.\n", prefix);
  33        }
  34#endif
  35
  36        strbuf_addf(&d, "%s/%s", prefix, path);
  37        path = strbuf_detach(&d, NULL);
  38        return path;
  39}
  40
  41const char *git_extract_argv0_path(const char *argv0)
  42{
  43        const char *slash;
  44
  45        if (!argv0 || !*argv0)
  46                return NULL;
  47        slash = argv0 + strlen(argv0);
  48
  49        while (argv0 <= slash && !is_dir_sep(*slash))
  50                slash--;
  51
  52        if (slash >= argv0) {
  53                argv0_path = xstrndup(argv0, slash - argv0);
  54                return slash + 1;
  55        }
  56
  57        return argv0;
  58}
  59
  60void git_set_argv_exec_path(const char *exec_path)
  61{
  62        argv_exec_path = exec_path;
  63        /*
  64         * Propagate this setting to external programs.
  65         */
  66        setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
  67}
  68
  69
  70/* Returns the highest-priority, location to look for git programs. */
  71const char *git_exec_path(void)
  72{
  73        const char *env;
  74
  75        if (argv_exec_path)
  76                return argv_exec_path;
  77
  78        env = getenv(EXEC_PATH_ENVIRONMENT);
  79        if (env && *env) {
  80                return env;
  81        }
  82
  83        return system_path(GIT_EXEC_PATH);
  84}
  85
  86static void add_path(struct strbuf *out, const char *path)
  87{
  88        if (path && *path) {
  89                strbuf_add_absolute_path(out, path);
  90                strbuf_addch(out, PATH_SEP);
  91        }
  92}
  93
  94void setup_path(void)
  95{
  96        const char *old_path = getenv("PATH");
  97        struct strbuf new_path = STRBUF_INIT;
  98
  99        add_path(&new_path, git_exec_path());
 100        add_path(&new_path, argv0_path);
 101
 102        if (old_path)
 103                strbuf_addstr(&new_path, old_path);
 104        else
 105                strbuf_addstr(&new_path, _PATH_DEFPATH);
 106
 107        setenv("PATH", new_path.buf, 1);
 108
 109        strbuf_release(&new_path);
 110}
 111
 112const char **prepare_git_cmd(const char **argv)
 113{
 114        int argc;
 115        const char **nargv;
 116
 117        for (argc = 0; argv[argc]; argc++)
 118                ; /* just counting */
 119        nargv = xmalloc(sizeof(*nargv) * (argc + 2));
 120
 121        nargv[0] = "git";
 122        for (argc = 0; argv[argc]; argc++)
 123                nargv[argc + 1] = argv[argc];
 124        nargv[argc + 1] = NULL;
 125        return nargv;
 126}
 127
 128int execv_git_cmd(const char **argv) {
 129        const char **nargv = prepare_git_cmd(argv);
 130        trace_argv_printf(nargv, "trace: exec:");
 131
 132        /* execvp() can only ever return if it fails */
 133        sane_execvp("git", (char **)nargv);
 134
 135        trace_printf("trace: exec failed: %s\n", strerror(errno));
 136
 137        free(nargv);
 138        return -1;
 139}
 140
 141
 142int execl_git_cmd(const char *cmd,...)
 143{
 144        int argc;
 145        const char *argv[MAX_ARGS + 1];
 146        const char *arg;
 147        va_list param;
 148
 149        va_start(param, cmd);
 150        argv[0] = cmd;
 151        argc = 1;
 152        while (argc < MAX_ARGS) {
 153                arg = argv[argc++] = va_arg(param, char *);
 154                if (!arg)
 155                        break;
 156        }
 157        va_end(param);
 158        if (MAX_ARGS <= argc)
 159                return error("too many args to run %s", cmd);
 160
 161        argv[argc] = NULL;
 162        return execv_git_cmd(argv);
 163}