6 #if defined(RUNTIME_PREFIX)
8 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
9 #include <mach-o/dyld.h>
12 #if defined(HAVE_BSD_KERN_PROC_SYSCTL)
13 #include <sys/param.h>
14 #include <sys/types.h>
15 #include <sys/sysctl.h>
18 #endif /* RUNTIME_PREFIX */
22 static const char *system_prefix(void);
27 * When using a runtime prefix, Git dynamically resolves paths relative to its
30 * The method for determining the path of the executable is highly
35 * Path to the current Git executable. Resolved on startup by
36 * 'git_resolve_executable_dir'.
38 static const char *executable_dirname
;
40 static const char *system_prefix(void)
42 static const char *prefix
;
44 assert(executable_dirname
);
45 assert(is_absolute_path(executable_dirname
));
48 !(prefix
= strip_path_suffix(executable_dirname
, GIT_EXEC_PATH
)) &&
49 !(prefix
= strip_path_suffix(executable_dirname
, BINDIR
)) &&
50 !(prefix
= strip_path_suffix(executable_dirname
, "git"))) {
51 prefix
= FALLBACK_RUNTIME_PREFIX
;
52 trace_printf("RUNTIME_PREFIX requested, "
53 "but prefix computation failed. "
54 "Using static fallback '%s'.\n", prefix
);
60 * Resolves the executable path from argv[0], only if it is absolute.
62 * Returns 0 on success, -1 on failure.
64 static int git_get_exec_path_from_argv0(struct strbuf
*buf
, const char *argv0
)
68 if (!argv0
|| !*argv0
)
71 slash
= find_last_dir_sep(argv0
);
73 trace_printf("trace: resolved executable path from argv0: %s\n",
75 strbuf_add_absolute_path(buf
, argv0
);
81 #ifdef PROCFS_EXECUTABLE_PATH
83 * Resolves the executable path by examining a procfs symlink.
85 * Returns 0 on success, -1 on failure.
87 static int git_get_exec_path_procfs(struct strbuf
*buf
)
89 if (strbuf_realpath(buf
, PROCFS_EXECUTABLE_PATH
, 0)) {
91 "trace: resolved executable path from procfs: %s\n",
97 #endif /* PROCFS_EXECUTABLE_PATH */
99 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
101 * Resolves the executable path using KERN_PROC_PATHNAME BSD sysctl.
103 * Returns 0 on success, -1 on failure.
105 static int git_get_exec_path_bsd_sysctl(struct strbuf
*buf
)
108 char path
[MAXPATHLEN
];
109 size_t cb
= sizeof(path
);
113 mib
[2] = KERN_PROC_PATHNAME
;
115 if (!sysctl(mib
, 4, path
, &cb
, NULL
, 0)) {
117 "trace: resolved executable path from sysctl: %s\n",
119 strbuf_addstr(buf
, path
);
124 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
126 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
128 * Resolves the executable path by querying Darwin application stack.
130 * Returns 0 on success, -1 on failure.
132 static int git_get_exec_path_darwin(struct strbuf
*buf
)
135 uint32_t size
= sizeof(path
);
136 if (!_NSGetExecutablePath(path
, &size
)) {
138 "trace: resolved executable path from Darwin stack: %s\n",
140 strbuf_addstr(buf
, path
);
145 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
149 * Resolves the executable path by using the global variable _wpgmptr.
151 * Returns 0 on success, -1 on failure.
153 static int git_get_exec_path_wpgmptr(struct strbuf
*buf
)
155 int len
= wcslen(_wpgmptr
) * 3 + 1;
156 strbuf_grow(buf
, len
);
157 len
= xwcstoutf(buf
->buf
, _wpgmptr
, len
);
163 #endif /* HAVE_WPGMPTR */
166 * Resolves the absolute path of the current executable.
168 * Returns 0 on success, -1 on failure.
170 static int git_get_exec_path(struct strbuf
*buf
, const char *argv0
)
173 * Identifying the executable path is operating system specific.
174 * Selectively employ all available methods in order of preference,
175 * preferring highly-available authoritative methods over
176 * selectively-available or non-authoritative methods.
178 * All cases fall back on resolving against argv[0] if there isn't a
179 * better functional method. However, note that argv[0] can be
180 * used-supplied on many operating systems, and is not authoritative
183 * Each of these functions returns 0 on success, so evaluation will stop
184 * after the first successful method.
187 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
188 git_get_exec_path_bsd_sysctl(buf
) &&
189 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
191 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
192 git_get_exec_path_darwin(buf
) &&
193 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
195 #ifdef PROCFS_EXECUTABLE_PATH
196 git_get_exec_path_procfs(buf
) &&
197 #endif /* PROCFS_EXECUTABLE_PATH */
200 git_get_exec_path_wpgmptr(buf
) &&
201 #endif /* HAVE_WPGMPTR */
203 git_get_exec_path_from_argv0(buf
, argv0
)) {
207 if (strbuf_normalize_path(buf
)) {
208 trace_printf("trace: could not normalize path: %s\n", buf
->buf
);
212 trace2_cmd_path(buf
->buf
);
217 void git_resolve_executable_dir(const char *argv0
)
219 struct strbuf buf
= STRBUF_INIT
;
223 if (git_get_exec_path(&buf
, argv0
)) {
225 "trace: could not determine executable path from: %s\n",
227 strbuf_release(&buf
);
231 resolved
= strbuf_detach(&buf
, NULL
);
232 slash
= find_last_dir_sep(resolved
);
234 resolved
[slash
- resolved
] = '\0';
236 executable_dirname
= resolved
;
237 trace_printf("trace: resolved executable dir: %s\n",
244 * When not using a runtime prefix, Git uses a hard-coded path.
246 static const char *system_prefix(void)
248 return FALLBACK_RUNTIME_PREFIX
;
252 * This is called during initialization, but No work needs to be done here when
253 * runtime prefix is not being used.
255 void git_resolve_executable_dir(const char *argv0 UNUSED
)
259 #endif /* RUNTIME_PREFIX */
261 char *system_path(const char *path
)
263 struct strbuf d
= STRBUF_INIT
;
265 if (is_absolute_path(path
))
266 return xstrdup(path
);
268 strbuf_addf(&d
, "%s/%s", system_prefix(), path
);
269 return strbuf_detach(&d
, NULL
);
272 static const char *exec_path_value
;
274 void git_set_exec_path(const char *exec_path
)
276 exec_path_value
= exec_path
;
278 * Propagate this setting to external programs.
280 setenv(EXEC_PATH_ENVIRONMENT
, exec_path
, 1);
283 /* Returns the highest-priority location to look for git programs. */
284 const char *git_exec_path(void)
286 if (!exec_path_value
) {
287 const char *env
= getenv(EXEC_PATH_ENVIRONMENT
);
289 exec_path_value
= xstrdup(env
);
291 exec_path_value
= system_path(GIT_EXEC_PATH
);
293 return exec_path_value
;
296 static void add_path(struct strbuf
*out
, const char *path
)
299 strbuf_add_absolute_path(out
, path
);
300 strbuf_addch(out
, PATH_SEP
);
304 void setup_path(void)
306 const char *exec_path
= git_exec_path();
307 const char *old_path
= getenv("PATH");
308 struct strbuf new_path
= STRBUF_INIT
;
310 git_set_exec_path(exec_path
);
311 add_path(&new_path
, exec_path
);
314 strbuf_addstr(&new_path
, old_path
);
316 strbuf_addstr(&new_path
, _PATH_DEFPATH
);
318 setenv("PATH", new_path
.buf
, 1);
320 strbuf_release(&new_path
);
323 const char **prepare_git_cmd(struct strvec
*out
, const char **argv
)
325 strvec_push(out
, "git");
326 strvec_pushv(out
, argv
);
330 int execv_git_cmd(const char **argv
)
332 struct strvec nargv
= STRVEC_INIT
;
334 prepare_git_cmd(&nargv
, argv
);
335 trace_argv_printf(nargv
.v
, "trace: exec:");
337 /* execvp() can only ever return if it fails */
338 sane_execvp("git", (char **)nargv
.v
);
340 trace_printf("trace: exec failed: %s\n", strerror(errno
));
342 strvec_clear(&nargv
);
346 int execl_git_cmd(const char *cmd
, ...)
349 const char *argv
[MAX_ARGS
+ 1];
353 va_start(param
, cmd
);
356 while (argc
< MAX_ARGS
) {
357 arg
= argv
[argc
++] = va_arg(param
, char *);
362 if (MAX_ARGS
<= argc
)
363 return error(_("too many args to run %s"), cmd
);
366 return execv_git_cmd(argv
);