4 #include "argv-array.h"
7 static const char *argv_exec_path;
8 static const char *argv0_path;
10 char *system_path(const char *path)
13 static const char *prefix;
15 static const char *prefix = PREFIX;
17 struct strbuf d = STRBUF_INIT;
19 if (is_absolute_path(path))
24 assert(is_absolute_path(argv0_path));
27 !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
28 !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
29 !(prefix = strip_path_suffix(argv0_path, "git"))) {
31 trace_printf("RUNTIME_PREFIX requested, "
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
37 strbuf_addf(&d, "%s/%s", prefix, path);
38 return strbuf_detach(&d, NULL);
41 const char *git_extract_argv0_path(const char *argv0)
45 if (!argv0 || !*argv0)
48 slash = find_last_dir_sep(argv0);
51 argv0_path = xstrndup(argv0, slash - argv0);
58 void git_set_argv_exec_path(const char *exec_path)
60 argv_exec_path = exec_path;
62 * Propagate this setting to external programs.
64 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
68 /* Returns the highest-priority, location to look for git programs. */
69 const char *git_exec_path(void)
74 return argv_exec_path;
76 env = getenv(EXEC_PATH_ENVIRONMENT);
81 return system_path(GIT_EXEC_PATH);
84 static void add_path(struct strbuf *out, const char *path)
87 strbuf_add_absolute_path(out, path);
88 strbuf_addch(out, PATH_SEP);
94 const char *old_path = getenv("PATH");
95 struct strbuf new_path = STRBUF_INIT;
97 add_path(&new_path, git_exec_path());
100 strbuf_addstr(&new_path, old_path);
102 strbuf_addstr(&new_path, _PATH_DEFPATH);
104 setenv("PATH", new_path.buf, 1);
106 strbuf_release(&new_path);
109 const char **prepare_git_cmd(struct argv_array *out, const char **argv)
111 argv_array_push(out, "git");
112 argv_array_pushv(out, argv);
116 int execv_git_cmd(const char **argv) {
117 struct argv_array nargv = ARGV_ARRAY_INIT;
119 prepare_git_cmd(&nargv, argv);
120 trace_argv_printf(nargv.argv, "trace: exec:");
122 /* execvp() can only ever return if it fails */
123 sane_execvp("git", (char **)nargv.argv);
125 trace_printf("trace: exec failed: %s\n", strerror(errno));
127 argv_array_clear(&nargv);
132 int execl_git_cmd(const char *cmd,...)
135 const char *argv[MAX_ARGS + 1];
139 va_start(param, cmd);
142 while (argc < MAX_ARGS) {
143 arg = argv[argc++] = va_arg(param, char *);
148 if (MAX_ARGS <= argc)
149 return error("too many args to run %s", cmd);
152 return execv_git_cmd(argv);