8 #if defined(RUNTIME_PREFIX)
10 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
11 #include <mach-o/dyld.h>
14 #if defined(HAVE_BSD_KERN_PROC_SYSCTL)
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <sys/sysctl.h>
20 #endif /* RUNTIME_PREFIX */
24 static const char *system_prefix(void);
29 * When using a runtime prefix, Git dynamically resolves paths relative to its
32 * The method for determining the path of the executable is highly
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
));
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
);
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
)
70 if (!argv0
|| !*argv0
)
73 slash
= find_last_dir_sep(argv0
);
75 trace_printf("trace: resolved executable path from argv0: %s\n",
77 strbuf_add_absolute_path(buf
, argv0
);
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)) {
93 "trace: resolved executable path from procfs: %s\n",
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
)
110 char path
[MAXPATHLEN
];
111 size_t cb
= sizeof(path
);
115 mib
[2] = KERN_PROC_PATHNAME
;
117 if (!sysctl(mib
, 4, path
, &cb
, NULL
, 0)) {
119 "trace: resolved executable path from sysctl: %s\n",
121 strbuf_addstr(buf
, path
);
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
)
137 uint32_t size
= sizeof(path
);
138 if (!_NSGetExecutablePath(path
, &size
)) {
140 "trace: resolved executable path from Darwin stack: %s\n",
142 strbuf_addstr(buf
, path
);
147 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
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
);
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
185 * Each of these functions returns 0 on success, so evaluation will stop
186 * after the first successful method.
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 */
202 git_get_exec_path_wpgmptr(buf
) &&
203 #endif /* HAVE_WPGMPTR */
205 git_get_exec_path_from_argv0(buf
, argv0
)) {
209 if (strbuf_normalize_path(buf
)) {
210 trace_printf("trace: could not normalize path: %s\n", buf
->buf
);
214 trace2_cmd_path(buf
->buf
);
219 void git_resolve_executable_dir(const char *argv0
)
221 struct strbuf buf
= STRBUF_INIT
;
225 if (git_get_exec_path(&buf
, argv0
)) {
227 "trace: could not determine executable path from: %s\n",
229 strbuf_release(&buf
);
233 resolved
= strbuf_detach(&buf
, NULL
);
234 slash
= find_last_dir_sep(resolved
);
236 resolved
[slash
- resolved
] = '\0';
238 executable_dirname
= resolved
;
239 trace_printf("trace: resolved executable dir: %s\n",
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
);
291 exec_path_value
= xstrdup(env
);
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
)
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
);
316 strbuf_addstr(&new_path
, old_path
);
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
);
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
);
348 int execl_git_cmd(const char *cmd
, ...)
351 const char *argv
[MAX_ARGS
+ 1];
355 va_start(param
, cmd
);
358 while (argc
< MAX_ARGS
) {
359 arg
= argv
[argc
++] = va_arg(param
, char *);
364 if (MAX_ARGS
<= argc
)
365 return error(_("too many args to run %s"), cmd
);
368 return execv_git_cmd(argv
);