3 #include "environment.h"
9 #if defined(RUNTIME_PREFIX)
11 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
12 #include <mach-o/dyld.h>
15 #if defined(HAVE_BSD_KERN_PROC_SYSCTL)
16 #include <sys/param.h>
17 #include <sys/types.h>
18 #include <sys/sysctl.h>
21 #endif /* RUNTIME_PREFIX */
25 static const char *system_prefix(void);
30 * When using a runtime prefix, Git dynamically resolves paths relative to its
33 * The method for determining the path of the executable is highly
38 * Path to the current Git executable. Resolved on startup by
39 * 'git_resolve_executable_dir'.
41 static const char *executable_dirname
;
43 static const char *system_prefix(void)
45 static const char *prefix
;
47 assert(executable_dirname
);
48 assert(is_absolute_path(executable_dirname
));
51 !(prefix
= strip_path_suffix(executable_dirname
, GIT_EXEC_PATH
)) &&
52 !(prefix
= strip_path_suffix(executable_dirname
, BINDIR
)) &&
53 !(prefix
= strip_path_suffix(executable_dirname
, "git"))) {
54 prefix
= FALLBACK_RUNTIME_PREFIX
;
55 trace_printf("RUNTIME_PREFIX requested, "
56 "but prefix computation failed. "
57 "Using static fallback '%s'.\n", prefix
);
63 * Resolves the executable path from argv[0], only if it is absolute.
65 * Returns 0 on success, -1 on failure.
67 static int git_get_exec_path_from_argv0(struct strbuf
*buf
, const char *argv0
)
71 if (!argv0
|| !*argv0
)
74 slash
= find_last_dir_sep(argv0
);
76 trace_printf("trace: resolved executable path from argv0: %s\n",
78 strbuf_add_absolute_path(buf
, argv0
);
84 #ifdef PROCFS_EXECUTABLE_PATH
86 * Resolves the executable path by examining a procfs symlink.
88 * Returns 0 on success, -1 on failure.
90 static int git_get_exec_path_procfs(struct strbuf
*buf
)
92 if (strbuf_realpath(buf
, PROCFS_EXECUTABLE_PATH
, 0)) {
94 "trace: resolved executable path from procfs: %s\n",
100 #endif /* PROCFS_EXECUTABLE_PATH */
102 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
104 * Resolves the executable path using KERN_PROC_PATHNAME BSD sysctl.
106 * Returns 0 on success, -1 on failure.
108 static int git_get_exec_path_bsd_sysctl(struct strbuf
*buf
)
111 char path
[MAXPATHLEN
];
112 size_t cb
= sizeof(path
);
116 mib
[2] = KERN_PROC_PATHNAME
;
118 if (!sysctl(mib
, 4, path
, &cb
, NULL
, 0)) {
120 "trace: resolved executable path from sysctl: %s\n",
122 strbuf_addstr(buf
, path
);
127 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
129 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
131 * Resolves the executable path by querying Darwin application stack.
133 * Returns 0 on success, -1 on failure.
135 static int git_get_exec_path_darwin(struct strbuf
*buf
)
138 uint32_t size
= sizeof(path
);
139 if (!_NSGetExecutablePath(path
, &size
)) {
141 "trace: resolved executable path from Darwin stack: %s\n",
143 strbuf_addstr(buf
, path
);
148 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
152 * Resolves the executable path by using the global variable _wpgmptr.
154 * Returns 0 on success, -1 on failure.
156 static int git_get_exec_path_wpgmptr(struct strbuf
*buf
)
158 int len
= wcslen(_wpgmptr
) * 3 + 1;
159 strbuf_grow(buf
, len
);
160 len
= xwcstoutf(buf
->buf
, _wpgmptr
, len
);
166 #endif /* HAVE_WPGMPTR */
169 * Resolves the absolute path of the current executable.
171 * Returns 0 on success, -1 on failure.
173 static int git_get_exec_path(struct strbuf
*buf
, const char *argv0
)
176 * Identifying the executable path is operating system specific.
177 * Selectively employ all available methods in order of preference,
178 * preferring highly-available authoritative methods over
179 * selectively-available or non-authoritative methods.
181 * All cases fall back on resolving against argv[0] if there isn't a
182 * better functional method. However, note that argv[0] can be
183 * used-supplied on many operating systems, and is not authoritative
186 * Each of these functions returns 0 on success, so evaluation will stop
187 * after the first successful method.
190 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
191 git_get_exec_path_bsd_sysctl(buf
) &&
192 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
194 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
195 git_get_exec_path_darwin(buf
) &&
196 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
198 #ifdef PROCFS_EXECUTABLE_PATH
199 git_get_exec_path_procfs(buf
) &&
200 #endif /* PROCFS_EXECUTABLE_PATH */
203 git_get_exec_path_wpgmptr(buf
) &&
204 #endif /* HAVE_WPGMPTR */
206 git_get_exec_path_from_argv0(buf
, argv0
)) {
210 if (strbuf_normalize_path(buf
)) {
211 trace_printf("trace: could not normalize path: %s\n", buf
->buf
);
215 trace2_cmd_path(buf
->buf
);
220 void git_resolve_executable_dir(const char *argv0
)
222 struct strbuf buf
= STRBUF_INIT
;
226 if (git_get_exec_path(&buf
, argv0
)) {
228 "trace: could not determine executable path from: %s\n",
230 strbuf_release(&buf
);
234 resolved
= strbuf_detach(&buf
, NULL
);
235 slash
= find_last_dir_sep(resolved
);
237 resolved
[slash
- resolved
] = '\0';
239 executable_dirname
= resolved
;
240 trace_printf("trace: resolved executable dir: %s\n",
247 * When not using a runtime prefix, Git uses a hard-coded path.
249 static const char *system_prefix(void)
251 return FALLBACK_RUNTIME_PREFIX
;
255 * This is called during initialization, but No work needs to be done here when
256 * runtime prefix is not being used.
258 void git_resolve_executable_dir(const char *argv0 UNUSED
)
262 #endif /* RUNTIME_PREFIX */
264 char *system_path(const char *path
)
266 struct strbuf d
= STRBUF_INIT
;
268 if (is_absolute_path(path
))
269 return xstrdup(path
);
271 strbuf_addf(&d
, "%s/%s", system_prefix(), path
);
272 return strbuf_detach(&d
, NULL
);
275 static const char *exec_path_value
;
277 void git_set_exec_path(const char *exec_path
)
279 exec_path_value
= exec_path
;
281 * Propagate this setting to external programs.
283 setenv(EXEC_PATH_ENVIRONMENT
, exec_path
, 1);
286 /* Returns the highest-priority location to look for git programs. */
287 const char *git_exec_path(void)
289 if (!exec_path_value
) {
290 const char *env
= getenv(EXEC_PATH_ENVIRONMENT
);
292 exec_path_value
= xstrdup(env
);
294 exec_path_value
= system_path(GIT_EXEC_PATH
);
296 return exec_path_value
;
299 static void add_path(struct strbuf
*out
, const char *path
)
302 strbuf_add_absolute_path(out
, path
);
303 strbuf_addch(out
, PATH_SEP
);
307 void setup_path(void)
309 const char *exec_path
= git_exec_path();
310 const char *old_path
= getenv("PATH");
311 struct strbuf new_path
= STRBUF_INIT
;
313 git_set_exec_path(exec_path
);
314 add_path(&new_path
, exec_path
);
317 strbuf_addstr(&new_path
, old_path
);
319 strbuf_addstr(&new_path
, _PATH_DEFPATH
);
321 setenv("PATH", new_path
.buf
, 1);
323 strbuf_release(&new_path
);
326 const char **prepare_git_cmd(struct strvec
*out
, const char **argv
)
328 strvec_push(out
, "git");
329 strvec_pushv(out
, argv
);
333 int execv_git_cmd(const char **argv
)
335 struct strvec nargv
= STRVEC_INIT
;
337 prepare_git_cmd(&nargv
, argv
);
338 trace_argv_printf(nargv
.v
, "trace: exec:");
340 /* execvp() can only ever return if it fails */
341 sane_execvp("git", (char **)nargv
.v
);
343 trace_printf("trace: exec failed: %s\n", strerror(errno
));
345 strvec_clear(&nargv
);
349 int execl_git_cmd(const char *cmd
, ...)
352 const char *argv
[MAX_ARGS
+ 1];
356 va_start(param
, cmd
);
359 while (argc
< MAX_ARGS
) {
360 arg
= argv
[argc
++] = va_arg(param
, char *);
365 if (MAX_ARGS
<= argc
)
366 return error(_("too many args to run %s"), cmd
);
369 return execv_git_cmd(argv
);