1 #include "git-compat-util.h"
3 #include "environment.h"
8 #include "run-command.h"
13 #if defined(RUNTIME_PREFIX)
15 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
16 #include <mach-o/dyld.h>
19 #if defined(HAVE_BSD_KERN_PROC_SYSCTL)
20 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
25 #endif /* RUNTIME_PREFIX */
29 static const char *system_prefix(void);
34 * When using a runtime prefix, Git dynamically resolves paths relative to its
37 * The method for determining the path of the executable is highly
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
));
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
);
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
)
75 if (!argv0
|| !*argv0
)
78 slash
= find_last_dir_sep(argv0
);
80 trace_printf("trace: resolved executable path from argv0: %s\n",
82 strbuf_add_absolute_path(buf
, argv0
);
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)) {
98 "trace: resolved executable path from procfs: %s\n",
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
)
115 char path
[MAXPATHLEN
];
116 size_t cb
= sizeof(path
);
120 mib
[2] = KERN_PROC_PATHNAME
;
122 if (!sysctl(mib
, 4, path
, &cb
, NULL
, 0)) {
124 "trace: resolved executable path from sysctl: %s\n",
126 strbuf_addstr(buf
, path
);
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
)
142 uint32_t size
= sizeof(path
);
143 if (!_NSGetExecutablePath(path
, &size
)) {
145 "trace: resolved executable path from Darwin stack: %s\n",
147 strbuf_addstr(buf
, path
);
152 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
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
);
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
190 * Each of these functions returns 0 on success, so evaluation will stop
191 * after the first successful method.
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 */
207 git_get_exec_path_wpgmptr(buf
) &&
208 #endif /* HAVE_WPGMPTR */
210 git_get_exec_path_from_argv0(buf
, argv0
)) {
214 if (strbuf_normalize_path(buf
)) {
215 trace_printf("trace: could not normalize path: %s\n", buf
->buf
);
219 trace2_cmd_path(buf
->buf
);
224 void git_resolve_executable_dir(const char *argv0
)
226 struct strbuf buf
= STRBUF_INIT
;
230 if (git_get_exec_path(&buf
, argv0
)) {
232 "trace: could not determine executable path from: %s\n",
234 strbuf_release(&buf
);
238 resolved
= strbuf_detach(&buf
, NULL
);
239 slash
= find_last_dir_sep(resolved
);
241 resolved
[slash
- resolved
] = '\0';
243 executable_dirname
= resolved
;
244 trace_printf("trace: resolved executable dir: %s\n",
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
);
296 exec_path_value
= xstrdup(env
);
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
)
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
);
321 strbuf_addstr(&new_path
, old_path
);
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
);
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
);
353 int execl_git_cmd(const char *cmd
, ...)
356 const char *argv
[MAX_ARGS
+ 1];
360 va_start(param
, cmd
);
363 while (argc
< MAX_ARGS
) {
364 arg
= argv
[argc
++] = va_arg(param
, char *);
369 if (MAX_ARGS
<= argc
)
370 return error(_("too many args to run %s"), cmd
);
373 return execv_git_cmd(argv
);