3 #include "environment.h"
11 #if defined(RUNTIME_PREFIX)
13 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
14 #include <mach-o/dyld.h>
17 #if defined(HAVE_BSD_KERN_PROC_SYSCTL)
18 #include <sys/param.h>
19 #include <sys/types.h>
20 #include <sys/sysctl.h>
23 #endif /* RUNTIME_PREFIX */
27 static const char *system_prefix(void);
32 * When using a runtime prefix, Git dynamically resolves paths relative to its
35 * The method for determining the path of the executable is highly
40 * Path to the current Git executable. Resolved on startup by
41 * 'git_resolve_executable_dir'.
43 static const char *executable_dirname
;
45 static const char *system_prefix(void)
47 static const char *prefix
;
49 assert(executable_dirname
);
50 assert(is_absolute_path(executable_dirname
));
53 !(prefix
= strip_path_suffix(executable_dirname
, GIT_EXEC_PATH
)) &&
54 !(prefix
= strip_path_suffix(executable_dirname
, BINDIR
)) &&
55 !(prefix
= strip_path_suffix(executable_dirname
, "git"))) {
56 prefix
= FALLBACK_RUNTIME_PREFIX
;
57 trace_printf("RUNTIME_PREFIX requested, "
58 "but prefix computation failed. "
59 "Using static fallback '%s'.\n", prefix
);
65 * Resolves the executable path from argv[0], only if it is absolute.
67 * Returns 0 on success, -1 on failure.
69 static int git_get_exec_path_from_argv0(struct strbuf
*buf
, const char *argv0
)
73 if (!argv0
|| !*argv0
)
76 slash
= find_last_dir_sep(argv0
);
78 trace_printf("trace: resolved executable path from argv0: %s\n",
80 strbuf_add_absolute_path(buf
, argv0
);
86 #ifdef PROCFS_EXECUTABLE_PATH
88 * Resolves the executable path by examining a procfs symlink.
90 * Returns 0 on success, -1 on failure.
92 static int git_get_exec_path_procfs(struct strbuf
*buf
)
94 if (strbuf_realpath(buf
, PROCFS_EXECUTABLE_PATH
, 0)) {
96 "trace: resolved executable path from procfs: %s\n",
102 #endif /* PROCFS_EXECUTABLE_PATH */
104 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
106 * Resolves the executable path using KERN_PROC_PATHNAME BSD sysctl.
108 * Returns 0 on success, -1 on failure.
110 static int git_get_exec_path_bsd_sysctl(struct strbuf
*buf
)
113 char path
[MAXPATHLEN
];
114 size_t cb
= sizeof(path
);
118 mib
[2] = KERN_PROC_PATHNAME
;
120 if (!sysctl(mib
, 4, path
, &cb
, NULL
, 0)) {
122 "trace: resolved executable path from sysctl: %s\n",
124 strbuf_addstr(buf
, path
);
129 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
131 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
133 * Resolves the executable path by querying Darwin application stack.
135 * Returns 0 on success, -1 on failure.
137 static int git_get_exec_path_darwin(struct strbuf
*buf
)
140 uint32_t size
= sizeof(path
);
141 if (!_NSGetExecutablePath(path
, &size
)) {
143 "trace: resolved executable path from Darwin stack: %s\n",
145 strbuf_addstr(buf
, path
);
150 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
154 * Resolves the executable path by using the global variable _wpgmptr.
156 * Returns 0 on success, -1 on failure.
158 static int git_get_exec_path_wpgmptr(struct strbuf
*buf
)
160 int len
= wcslen(_wpgmptr
) * 3 + 1;
161 strbuf_grow(buf
, len
);
162 len
= xwcstoutf(buf
->buf
, _wpgmptr
, len
);
168 #endif /* HAVE_WPGMPTR */
171 * Resolves the absolute path of the current executable.
173 * Returns 0 on success, -1 on failure.
175 static int git_get_exec_path(struct strbuf
*buf
, const char *argv0
)
178 * Identifying the executable path is operating system specific.
179 * Selectively employ all available methods in order of preference,
180 * preferring highly-available authoritative methods over
181 * selectively-available or non-authoritative methods.
183 * All cases fall back on resolving against argv[0] if there isn't a
184 * better functional method. However, note that argv[0] can be
185 * used-supplied on many operating systems, and is not authoritative
188 * Each of these functions returns 0 on success, so evaluation will stop
189 * after the first successful method.
192 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
193 git_get_exec_path_bsd_sysctl(buf
) &&
194 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
196 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
197 git_get_exec_path_darwin(buf
) &&
198 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
200 #ifdef PROCFS_EXECUTABLE_PATH
201 git_get_exec_path_procfs(buf
) &&
202 #endif /* PROCFS_EXECUTABLE_PATH */
205 git_get_exec_path_wpgmptr(buf
) &&
206 #endif /* HAVE_WPGMPTR */
208 git_get_exec_path_from_argv0(buf
, argv0
)) {
212 if (strbuf_normalize_path(buf
)) {
213 trace_printf("trace: could not normalize path: %s\n", buf
->buf
);
217 trace2_cmd_path(buf
->buf
);
222 void git_resolve_executable_dir(const char *argv0
)
224 struct strbuf buf
= STRBUF_INIT
;
228 if (git_get_exec_path(&buf
, argv0
)) {
230 "trace: could not determine executable path from: %s\n",
232 strbuf_release(&buf
);
236 resolved
= strbuf_detach(&buf
, NULL
);
237 slash
= find_last_dir_sep(resolved
);
239 resolved
[slash
- resolved
] = '\0';
241 executable_dirname
= resolved
;
242 trace_printf("trace: resolved executable dir: %s\n",
249 * When not using a runtime prefix, Git uses a hard-coded path.
251 static const char *system_prefix(void)
253 return FALLBACK_RUNTIME_PREFIX
;
257 * This is called during initialization, but No work needs to be done here when
258 * runtime prefix is not being used.
260 void git_resolve_executable_dir(const char *argv0 UNUSED
)
264 #endif /* RUNTIME_PREFIX */
266 char *system_path(const char *path
)
268 struct strbuf d
= STRBUF_INIT
;
270 if (is_absolute_path(path
))
271 return xstrdup(path
);
273 strbuf_addf(&d
, "%s/%s", system_prefix(), path
);
274 return strbuf_detach(&d
, NULL
);
277 static const char *exec_path_value
;
279 void git_set_exec_path(const char *exec_path
)
281 exec_path_value
= exec_path
;
283 * Propagate this setting to external programs.
285 setenv(EXEC_PATH_ENVIRONMENT
, exec_path
, 1);
288 /* Returns the highest-priority location to look for git programs. */
289 const char *git_exec_path(void)
291 if (!exec_path_value
) {
292 const char *env
= getenv(EXEC_PATH_ENVIRONMENT
);
294 exec_path_value
= xstrdup(env
);
296 exec_path_value
= system_path(GIT_EXEC_PATH
);
298 return exec_path_value
;
301 static void add_path(struct strbuf
*out
, const char *path
)
304 strbuf_add_absolute_path(out
, path
);
305 strbuf_addch(out
, PATH_SEP
);
309 void setup_path(void)
311 const char *exec_path
= git_exec_path();
312 const char *old_path
= getenv("PATH");
313 struct strbuf new_path
= STRBUF_INIT
;
315 git_set_exec_path(exec_path
);
316 add_path(&new_path
, exec_path
);
319 strbuf_addstr(&new_path
, old_path
);
321 strbuf_addstr(&new_path
, _PATH_DEFPATH
);
323 setenv("PATH", new_path
.buf
, 1);
325 strbuf_release(&new_path
);
328 const char **prepare_git_cmd(struct strvec
*out
, const char **argv
)
330 strvec_push(out
, "git");
331 strvec_pushv(out
, argv
);
335 int execv_git_cmd(const char **argv
)
337 struct strvec nargv
= STRVEC_INIT
;
339 prepare_git_cmd(&nargv
, argv
);
340 trace_argv_printf(nargv
.v
, "trace: exec:");
342 /* execvp() can only ever return if it fails */
343 sane_execvp("git", (char **)nargv
.v
);
345 trace_printf("trace: exec failed: %s\n", strerror(errno
));
347 strvec_clear(&nargv
);
351 int execl_git_cmd(const char *cmd
, ...)
354 const char *argv
[MAX_ARGS
+ 1];
358 va_start(param
, cmd
);
361 while (argc
< MAX_ARGS
) {
362 arg
= argv
[argc
++] = va_arg(param
, char *);
367 if (MAX_ARGS
<= argc
)
368 return error(_("too many args to run %s"), cmd
);
371 return execv_git_cmd(argv
);