Merge branch 'pw/diff-no-index-from-named-pipes'
[alt-git.git] / exec-cmd.c
blob1d597e84ea7f4c3c8296cbf527bf0c5514037e99
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "environment.h"
4 #include "exec-cmd.h"
5 #include "gettext.h"
6 #include "path.h"
7 #include "quote.h"
8 #include "run-command.h"
9 #include "strvec.h"
10 #include "trace.h"
11 #include "trace2.h"
13 #if defined(RUNTIME_PREFIX)
15 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
16 #include <mach-o/dyld.h>
17 #endif
19 #if defined(HAVE_BSD_KERN_PROC_SYSCTL)
20 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
23 #endif
25 #endif /* RUNTIME_PREFIX */
27 #define MAX_ARGS 32
29 static const char *system_prefix(void);
31 #ifdef RUNTIME_PREFIX
33 /**
34 * When using a runtime prefix, Git dynamically resolves paths relative to its
35 * executable.
37 * The method for determining the path of the executable is highly
38 * platform-specific.
41 /**
42 * Path to the current Git executable. Resolved on startup by
43 * 'git_resolve_executable_dir'.
45 static const char *executable_dirname;
47 static const char *system_prefix(void)
49 static const char *prefix;
51 assert(executable_dirname);
52 assert(is_absolute_path(executable_dirname));
54 if (!prefix &&
55 !(prefix = strip_path_suffix(executable_dirname, GIT_EXEC_PATH)) &&
56 !(prefix = strip_path_suffix(executable_dirname, BINDIR)) &&
57 !(prefix = strip_path_suffix(executable_dirname, "git"))) {
58 prefix = FALLBACK_RUNTIME_PREFIX;
59 trace_printf("RUNTIME_PREFIX requested, "
60 "but prefix computation failed. "
61 "Using static fallback '%s'.\n", prefix);
63 return prefix;
67 * Resolves the executable path from argv[0], only if it is absolute.
69 * Returns 0 on success, -1 on failure.
71 static int git_get_exec_path_from_argv0(struct strbuf *buf, const char *argv0)
73 const char *slash;
75 if (!argv0 || !*argv0)
76 return -1;
78 slash = find_last_dir_sep(argv0);
79 if (slash) {
80 trace_printf("trace: resolved executable path from argv0: %s\n",
81 argv0);
82 strbuf_add_absolute_path(buf, argv0);
83 return 0;
85 return -1;
88 #ifdef PROCFS_EXECUTABLE_PATH
90 * Resolves the executable path by examining a procfs symlink.
92 * Returns 0 on success, -1 on failure.
94 static int git_get_exec_path_procfs(struct strbuf *buf)
96 if (strbuf_realpath(buf, PROCFS_EXECUTABLE_PATH, 0)) {
97 trace_printf(
98 "trace: resolved executable path from procfs: %s\n",
99 buf->buf);
100 return 0;
102 return -1;
104 #endif /* PROCFS_EXECUTABLE_PATH */
106 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
108 * Resolves the executable path using KERN_PROC_PATHNAME BSD sysctl.
110 * Returns 0 on success, -1 on failure.
112 static int git_get_exec_path_bsd_sysctl(struct strbuf *buf)
114 int mib[4];
115 char path[MAXPATHLEN];
116 size_t cb = sizeof(path);
118 mib[0] = CTL_KERN;
119 mib[1] = KERN_PROC;
120 mib[2] = KERN_PROC_PATHNAME;
121 mib[3] = -1;
122 if (!sysctl(mib, 4, path, &cb, NULL, 0)) {
123 trace_printf(
124 "trace: resolved executable path from sysctl: %s\n",
125 path);
126 strbuf_addstr(buf, path);
127 return 0;
129 return -1;
131 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
133 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
135 * Resolves the executable path by querying Darwin application stack.
137 * Returns 0 on success, -1 on failure.
139 static int git_get_exec_path_darwin(struct strbuf *buf)
141 char path[PATH_MAX];
142 uint32_t size = sizeof(path);
143 if (!_NSGetExecutablePath(path, &size)) {
144 trace_printf(
145 "trace: resolved executable path from Darwin stack: %s\n",
146 path);
147 strbuf_addstr(buf, path);
148 return 0;
150 return -1;
152 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
154 #ifdef HAVE_WPGMPTR
156 * Resolves the executable path by using the global variable _wpgmptr.
158 * Returns 0 on success, -1 on failure.
160 static int git_get_exec_path_wpgmptr(struct strbuf *buf)
162 int len = wcslen(_wpgmptr) * 3 + 1;
163 strbuf_grow(buf, len);
164 len = xwcstoutf(buf->buf, _wpgmptr, len);
165 if (len < 0)
166 return -1;
167 buf->len += len;
168 return 0;
170 #endif /* HAVE_WPGMPTR */
173 * Resolves the absolute path of the current executable.
175 * Returns 0 on success, -1 on failure.
177 static int git_get_exec_path(struct strbuf *buf, const char *argv0)
180 * Identifying the executable path is operating system specific.
181 * Selectively employ all available methods in order of preference,
182 * preferring highly-available authoritative methods over
183 * selectively-available or non-authoritative methods.
185 * All cases fall back on resolving against argv[0] if there isn't a
186 * better functional method. However, note that argv[0] can be
187 * used-supplied on many operating systems, and is not authoritative
188 * in those cases.
190 * Each of these functions returns 0 on success, so evaluation will stop
191 * after the first successful method.
193 if (
194 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
195 git_get_exec_path_bsd_sysctl(buf) &&
196 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
198 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
199 git_get_exec_path_darwin(buf) &&
200 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
202 #ifdef PROCFS_EXECUTABLE_PATH
203 git_get_exec_path_procfs(buf) &&
204 #endif /* PROCFS_EXECUTABLE_PATH */
206 #ifdef HAVE_WPGMPTR
207 git_get_exec_path_wpgmptr(buf) &&
208 #endif /* HAVE_WPGMPTR */
210 git_get_exec_path_from_argv0(buf, argv0)) {
211 return -1;
214 if (strbuf_normalize_path(buf)) {
215 trace_printf("trace: could not normalize path: %s\n", buf->buf);
216 return -1;
219 trace2_cmd_path(buf->buf);
221 return 0;
224 void git_resolve_executable_dir(const char *argv0)
226 struct strbuf buf = STRBUF_INIT;
227 char *resolved;
228 const char *slash;
230 if (git_get_exec_path(&buf, argv0)) {
231 trace_printf(
232 "trace: could not determine executable path from: %s\n",
233 argv0);
234 strbuf_release(&buf);
235 return;
238 resolved = strbuf_detach(&buf, NULL);
239 slash = find_last_dir_sep(resolved);
240 if (slash)
241 resolved[slash - resolved] = '\0';
243 executable_dirname = resolved;
244 trace_printf("trace: resolved executable dir: %s\n",
245 executable_dirname);
248 #else
251 * When not using a runtime prefix, Git uses a hard-coded path.
253 static const char *system_prefix(void)
255 return FALLBACK_RUNTIME_PREFIX;
259 * This is called during initialization, but No work needs to be done here when
260 * runtime prefix is not being used.
262 void git_resolve_executable_dir(const char *argv0 UNUSED)
266 #endif /* RUNTIME_PREFIX */
268 char *system_path(const char *path)
270 struct strbuf d = STRBUF_INIT;
272 if (is_absolute_path(path))
273 return xstrdup(path);
275 strbuf_addf(&d, "%s/%s", system_prefix(), path);
276 return strbuf_detach(&d, NULL);
279 static const char *exec_path_value;
281 void git_set_exec_path(const char *exec_path)
283 exec_path_value = exec_path;
285 * Propagate this setting to external programs.
287 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
290 /* Returns the highest-priority location to look for git programs. */
291 const char *git_exec_path(void)
293 if (!exec_path_value) {
294 const char *env = getenv(EXEC_PATH_ENVIRONMENT);
295 if (env && *env)
296 exec_path_value = xstrdup(env);
297 else
298 exec_path_value = system_path(GIT_EXEC_PATH);
300 return exec_path_value;
303 static void add_path(struct strbuf *out, const char *path)
305 if (path && *path) {
306 strbuf_add_absolute_path(out, path);
307 strbuf_addch(out, PATH_SEP);
311 void setup_path(void)
313 const char *exec_path = git_exec_path();
314 const char *old_path = getenv("PATH");
315 struct strbuf new_path = STRBUF_INIT;
317 git_set_exec_path(exec_path);
318 add_path(&new_path, exec_path);
320 if (old_path)
321 strbuf_addstr(&new_path, old_path);
322 else
323 strbuf_addstr(&new_path, _PATH_DEFPATH);
325 setenv("PATH", new_path.buf, 1);
327 strbuf_release(&new_path);
330 const char **prepare_git_cmd(struct strvec *out, const char **argv)
332 strvec_push(out, "git");
333 strvec_pushv(out, argv);
334 return out->v;
337 int execv_git_cmd(const char **argv)
339 struct strvec nargv = STRVEC_INIT;
341 prepare_git_cmd(&nargv, argv);
342 trace_argv_printf(nargv.v, "trace: exec:");
344 /* execvp() can only ever return if it fails */
345 sane_execvp("git", (char **)nargv.v);
347 trace_printf("trace: exec failed: %s\n", strerror(errno));
349 strvec_clear(&nargv);
350 return -1;
353 int execl_git_cmd(const char *cmd, ...)
355 int argc;
356 const char *argv[MAX_ARGS + 1];
357 const char *arg;
358 va_list param;
360 va_start(param, cmd);
361 argv[0] = cmd;
362 argc = 1;
363 while (argc < MAX_ARGS) {
364 arg = argv[argc++] = va_arg(param, char *);
365 if (!arg)
366 break;
368 va_end(param);
369 if (MAX_ARGS <= argc)
370 return error(_("too many args to run %s"), cmd);
372 argv[argc] = NULL;
373 return execv_git_cmd(argv);