6 static const char *argv_exec_path;
7 static const char *argv0_path;
9 const char *system_path(const char *path)
12 static const char *prefix;
14 static const char *prefix = PREFIX;
16 struct strbuf d = STRBUF_INIT;
18 if (is_absolute_path(path))
23 assert(is_absolute_path(argv0_path));
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"))) {
30 trace_printf("RUNTIME_PREFIX requested, "
31 "but prefix computation failed. "
32 "Using static fallback '%s'.\n", prefix);
36 strbuf_addf(&d, "%s/%s", prefix, path);
37 path = strbuf_detach(&d, NULL);
41 const char *git_extract_argv0_path(const char *argv0)
45 if (!argv0 || !*argv0)
47 slash = argv0 + strlen(argv0);
49 while (argv0 <= slash && !is_dir_sep(*slash))
53 argv0_path = xstrndup(argv0, slash - argv0);
60 void git_set_argv_exec_path(const char *exec_path)
62 argv_exec_path = exec_path;
64 * Propagate this setting to external programs.
66 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
70 /* Returns the highest-priority, location to look for git programs. */
71 const char *git_exec_path(void)
76 return argv_exec_path;
78 env = getenv(EXEC_PATH_ENVIRONMENT);
83 return system_path(GIT_EXEC_PATH);
86 static void add_path(struct strbuf *out, const char *path)
89 if (is_absolute_path(path))
90 strbuf_addstr(out, path);
92 strbuf_addstr(out, absolute_path(path));
94 strbuf_addch(out, PATH_SEP);
100 const char *old_path = getenv("PATH");
101 struct strbuf new_path = STRBUF_INIT;
103 add_path(&new_path, git_exec_path());
104 add_path(&new_path, argv0_path);
107 strbuf_addstr(&new_path, old_path);
109 strbuf_addstr(&new_path, _PATH_DEFPATH);
111 setenv("PATH", new_path.buf, 1);
113 strbuf_release(&new_path);
116 const char **prepare_git_cmd(const char **argv)
121 for (argc = 0; argv[argc]; argc++)
122 ; /* just counting */
123 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
126 for (argc = 0; argv[argc]; argc++)
127 nargv[argc + 1] = argv[argc];
128 nargv[argc + 1] = NULL;
132 int execv_git_cmd(const char **argv) {
133 const char **nargv = prepare_git_cmd(argv);
134 trace_argv_printf(nargv, "trace: exec:");
136 /* execvp() can only ever return if it fails */
137 sane_execvp("git", (char **)nargv);
139 trace_printf("trace: exec failed: %s\n", strerror(errno));
146 int execl_git_cmd(const char *cmd,...)
149 const char *argv[MAX_ARGS + 1];
153 va_start(param, cmd);
156 while (argc < MAX_ARGS) {
157 arg = argv[argc++] = va_arg(param, char *);
162 if (MAX_ARGS <= argc)
163 return error("too many args to run %s", cmd);
166 return execv_git_cmd(argv);