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