cache.h: remove expand_user_path()
[git/debian.git] / exec-cmd.c
blob042d9247a5bdd4d45fb44c29a6a28e3284ec89f2
1 #include "cache.h"
2 #include "abspath.h"
3 #include "exec-cmd.h"
4 #include "gettext.h"
5 #include "quote.h"
6 #include "strvec.h"
8 #if defined(RUNTIME_PREFIX)
10 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
11 #include <mach-o/dyld.h>
12 #endif
14 #if defined(HAVE_BSD_KERN_PROC_SYSCTL)
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <sys/sysctl.h>
18 #endif
20 #endif /* RUNTIME_PREFIX */
22 #define MAX_ARGS 32
24 static const char *system_prefix(void);
26 #ifdef RUNTIME_PREFIX
28 /**
29 * When using a runtime prefix, Git dynamically resolves paths relative to its
30 * executable.
32 * The method for determining the path of the executable is highly
33 * platform-specific.
36 /**
37 * Path to the current Git executable. Resolved on startup by
38 * 'git_resolve_executable_dir'.
40 static const char *executable_dirname;
42 static const char *system_prefix(void)
44 static const char *prefix;
46 assert(executable_dirname);
47 assert(is_absolute_path(executable_dirname));
49 if (!prefix &&
50 !(prefix = strip_path_suffix(executable_dirname, GIT_EXEC_PATH)) &&
51 !(prefix = strip_path_suffix(executable_dirname, BINDIR)) &&
52 !(prefix = strip_path_suffix(executable_dirname, "git"))) {
53 prefix = FALLBACK_RUNTIME_PREFIX;
54 trace_printf("RUNTIME_PREFIX requested, "
55 "but prefix computation failed. "
56 "Using static fallback '%s'.\n", prefix);
58 return prefix;
62 * Resolves the executable path from argv[0], only if it is absolute.
64 * Returns 0 on success, -1 on failure.
66 static int git_get_exec_path_from_argv0(struct strbuf *buf, const char *argv0)
68 const char *slash;
70 if (!argv0 || !*argv0)
71 return -1;
73 slash = find_last_dir_sep(argv0);
74 if (slash) {
75 trace_printf("trace: resolved executable path from argv0: %s\n",
76 argv0);
77 strbuf_add_absolute_path(buf, argv0);
78 return 0;
80 return -1;
83 #ifdef PROCFS_EXECUTABLE_PATH
85 * Resolves the executable path by examining a procfs symlink.
87 * Returns 0 on success, -1 on failure.
89 static int git_get_exec_path_procfs(struct strbuf *buf)
91 if (strbuf_realpath(buf, PROCFS_EXECUTABLE_PATH, 0)) {
92 trace_printf(
93 "trace: resolved executable path from procfs: %s\n",
94 buf->buf);
95 return 0;
97 return -1;
99 #endif /* PROCFS_EXECUTABLE_PATH */
101 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
103 * Resolves the executable path using KERN_PROC_PATHNAME BSD sysctl.
105 * Returns 0 on success, -1 on failure.
107 static int git_get_exec_path_bsd_sysctl(struct strbuf *buf)
109 int mib[4];
110 char path[MAXPATHLEN];
111 size_t cb = sizeof(path);
113 mib[0] = CTL_KERN;
114 mib[1] = KERN_PROC;
115 mib[2] = KERN_PROC_PATHNAME;
116 mib[3] = -1;
117 if (!sysctl(mib, 4, path, &cb, NULL, 0)) {
118 trace_printf(
119 "trace: resolved executable path from sysctl: %s\n",
120 path);
121 strbuf_addstr(buf, path);
122 return 0;
124 return -1;
126 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
128 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
130 * Resolves the executable path by querying Darwin application stack.
132 * Returns 0 on success, -1 on failure.
134 static int git_get_exec_path_darwin(struct strbuf *buf)
136 char path[PATH_MAX];
137 uint32_t size = sizeof(path);
138 if (!_NSGetExecutablePath(path, &size)) {
139 trace_printf(
140 "trace: resolved executable path from Darwin stack: %s\n",
141 path);
142 strbuf_addstr(buf, path);
143 return 0;
145 return -1;
147 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
149 #ifdef HAVE_WPGMPTR
151 * Resolves the executable path by using the global variable _wpgmptr.
153 * Returns 0 on success, -1 on failure.
155 static int git_get_exec_path_wpgmptr(struct strbuf *buf)
157 int len = wcslen(_wpgmptr) * 3 + 1;
158 strbuf_grow(buf, len);
159 len = xwcstoutf(buf->buf, _wpgmptr, len);
160 if (len < 0)
161 return -1;
162 buf->len += len;
163 return 0;
165 #endif /* HAVE_WPGMPTR */
168 * Resolves the absolute path of the current executable.
170 * Returns 0 on success, -1 on failure.
172 static int git_get_exec_path(struct strbuf *buf, const char *argv0)
175 * Identifying the executable path is operating system specific.
176 * Selectively employ all available methods in order of preference,
177 * preferring highly-available authoritative methods over
178 * selectively-available or non-authoritative methods.
180 * All cases fall back on resolving against argv[0] if there isn't a
181 * better functional method. However, note that argv[0] can be
182 * used-supplied on many operating systems, and is not authoritative
183 * in those cases.
185 * Each of these functions returns 0 on success, so evaluation will stop
186 * after the first successful method.
188 if (
189 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
190 git_get_exec_path_bsd_sysctl(buf) &&
191 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
193 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
194 git_get_exec_path_darwin(buf) &&
195 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
197 #ifdef PROCFS_EXECUTABLE_PATH
198 git_get_exec_path_procfs(buf) &&
199 #endif /* PROCFS_EXECUTABLE_PATH */
201 #ifdef HAVE_WPGMPTR
202 git_get_exec_path_wpgmptr(buf) &&
203 #endif /* HAVE_WPGMPTR */
205 git_get_exec_path_from_argv0(buf, argv0)) {
206 return -1;
209 if (strbuf_normalize_path(buf)) {
210 trace_printf("trace: could not normalize path: %s\n", buf->buf);
211 return -1;
214 trace2_cmd_path(buf->buf);
216 return 0;
219 void git_resolve_executable_dir(const char *argv0)
221 struct strbuf buf = STRBUF_INIT;
222 char *resolved;
223 const char *slash;
225 if (git_get_exec_path(&buf, argv0)) {
226 trace_printf(
227 "trace: could not determine executable path from: %s\n",
228 argv0);
229 strbuf_release(&buf);
230 return;
233 resolved = strbuf_detach(&buf, NULL);
234 slash = find_last_dir_sep(resolved);
235 if (slash)
236 resolved[slash - resolved] = '\0';
238 executable_dirname = resolved;
239 trace_printf("trace: resolved executable dir: %s\n",
240 executable_dirname);
243 #else
246 * When not using a runtime prefix, Git uses a hard-coded path.
248 static const char *system_prefix(void)
250 return FALLBACK_RUNTIME_PREFIX;
254 * This is called during initialization, but No work needs to be done here when
255 * runtime prefix is not being used.
257 void git_resolve_executable_dir(const char *argv0 UNUSED)
261 #endif /* RUNTIME_PREFIX */
263 char *system_path(const char *path)
265 struct strbuf d = STRBUF_INIT;
267 if (is_absolute_path(path))
268 return xstrdup(path);
270 strbuf_addf(&d, "%s/%s", system_prefix(), path);
271 return strbuf_detach(&d, NULL);
274 static const char *exec_path_value;
276 void git_set_exec_path(const char *exec_path)
278 exec_path_value = exec_path;
280 * Propagate this setting to external programs.
282 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
285 /* Returns the highest-priority location to look for git programs. */
286 const char *git_exec_path(void)
288 if (!exec_path_value) {
289 const char *env = getenv(EXEC_PATH_ENVIRONMENT);
290 if (env && *env)
291 exec_path_value = xstrdup(env);
292 else
293 exec_path_value = system_path(GIT_EXEC_PATH);
295 return exec_path_value;
298 static void add_path(struct strbuf *out, const char *path)
300 if (path && *path) {
301 strbuf_add_absolute_path(out, path);
302 strbuf_addch(out, PATH_SEP);
306 void setup_path(void)
308 const char *exec_path = git_exec_path();
309 const char *old_path = getenv("PATH");
310 struct strbuf new_path = STRBUF_INIT;
312 git_set_exec_path(exec_path);
313 add_path(&new_path, exec_path);
315 if (old_path)
316 strbuf_addstr(&new_path, old_path);
317 else
318 strbuf_addstr(&new_path, _PATH_DEFPATH);
320 setenv("PATH", new_path.buf, 1);
322 strbuf_release(&new_path);
325 const char **prepare_git_cmd(struct strvec *out, const char **argv)
327 strvec_push(out, "git");
328 strvec_pushv(out, argv);
329 return out->v;
332 int execv_git_cmd(const char **argv)
334 struct strvec nargv = STRVEC_INIT;
336 prepare_git_cmd(&nargv, argv);
337 trace_argv_printf(nargv.v, "trace: exec:");
339 /* execvp() can only ever return if it fails */
340 sane_execvp("git", (char **)nargv.v);
342 trace_printf("trace: exec failed: %s\n", strerror(errno));
344 strvec_clear(&nargv);
345 return -1;
348 int execl_git_cmd(const char *cmd, ...)
350 int argc;
351 const char *argv[MAX_ARGS + 1];
352 const char *arg;
353 va_list param;
355 va_start(param, cmd);
356 argv[0] = cmd;
357 argc = 1;
358 while (argc < MAX_ARGS) {
359 arg = argv[argc++] = va_arg(param, char *);
360 if (!arg)
361 break;
363 va_end(param);
364 if (MAX_ARGS <= argc)
365 return error(_("too many args to run %s"), cmd);
367 argv[argc] = NULL;
368 return execv_git_cmd(argv);