1 #include "git-compat-util.h"
3 #include "environment.h"
7 #include "run-command.h"
12 #if defined(RUNTIME_PREFIX)
14 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
15 #include <mach-o/dyld.h>
18 #if defined(HAVE_BSD_KERN_PROC_SYSCTL)
19 #include <sys/param.h>
20 #include <sys/types.h>
21 #include <sys/sysctl.h>
24 #endif /* RUNTIME_PREFIX */
28 static const char *system_prefix(void);
33 * When using a runtime prefix, Git dynamically resolves paths relative to its
36 * The method for determining the path of the executable is highly
41 * Path to the current Git executable. Resolved on startup by
42 * 'git_resolve_executable_dir'.
44 static const char *executable_dirname
;
46 static const char *system_prefix(void)
48 static const char *prefix
;
50 assert(executable_dirname
);
51 assert(is_absolute_path(executable_dirname
));
54 !(prefix
= strip_path_suffix(executable_dirname
, GIT_EXEC_PATH
)) &&
55 !(prefix
= strip_path_suffix(executable_dirname
, BINDIR
)) &&
56 !(prefix
= strip_path_suffix(executable_dirname
, "git"))) {
57 prefix
= FALLBACK_RUNTIME_PREFIX
;
58 trace_printf("RUNTIME_PREFIX requested, "
59 "but prefix computation failed. "
60 "Using static fallback '%s'.\n", prefix
);
66 * Resolves the executable path from argv[0], only if it is absolute.
68 * Returns 0 on success, -1 on failure.
70 static int git_get_exec_path_from_argv0(struct strbuf
*buf
, const char *argv0
)
74 if (!argv0
|| !*argv0
)
77 slash
= find_last_dir_sep(argv0
);
79 trace_printf("trace: resolved executable path from argv0: %s\n",
81 strbuf_add_absolute_path(buf
, argv0
);
87 #ifdef PROCFS_EXECUTABLE_PATH
89 * Resolves the executable path by examining a procfs symlink.
91 * Returns 0 on success, -1 on failure.
93 static int git_get_exec_path_procfs(struct strbuf
*buf
)
95 if (strbuf_realpath(buf
, PROCFS_EXECUTABLE_PATH
, 0)) {
97 "trace: resolved executable path from procfs: %s\n",
103 #endif /* PROCFS_EXECUTABLE_PATH */
105 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
107 * Resolves the executable path using KERN_PROC_PATHNAME BSD sysctl.
109 * Returns 0 on success, -1 on failure.
111 static int git_get_exec_path_bsd_sysctl(struct strbuf
*buf
)
114 char path
[MAXPATHLEN
];
115 size_t cb
= sizeof(path
);
119 mib
[2] = KERN_PROC_PATHNAME
;
121 if (!sysctl(mib
, 4, path
, &cb
, NULL
, 0)) {
123 "trace: resolved executable path from sysctl: %s\n",
125 strbuf_addstr(buf
, path
);
130 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
132 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
134 * Resolves the executable path by querying Darwin application stack.
136 * Returns 0 on success, -1 on failure.
138 static int git_get_exec_path_darwin(struct strbuf
*buf
)
141 uint32_t size
= sizeof(path
);
142 if (!_NSGetExecutablePath(path
, &size
)) {
144 "trace: resolved executable path from Darwin stack: %s\n",
146 strbuf_addstr(buf
, path
);
151 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
155 * Resolves the executable path by using the global variable _wpgmptr.
157 * Returns 0 on success, -1 on failure.
159 static int git_get_exec_path_wpgmptr(struct strbuf
*buf
)
161 int len
= wcslen(_wpgmptr
) * 3 + 1;
162 strbuf_grow(buf
, len
);
163 len
= xwcstoutf(buf
->buf
, _wpgmptr
, len
);
169 #endif /* HAVE_WPGMPTR */
172 * Resolves the absolute path of the current executable.
174 * Returns 0 on success, -1 on failure.
176 static int git_get_exec_path(struct strbuf
*buf
, const char *argv0
)
179 * Identifying the executable path is operating system specific.
180 * Selectively employ all available methods in order of preference,
181 * preferring highly-available authoritative methods over
182 * selectively-available or non-authoritative methods.
184 * All cases fall back on resolving against argv[0] if there isn't a
185 * better functional method. However, note that argv[0] can be
186 * used-supplied on many operating systems, and is not authoritative
189 * Each of these functions returns 0 on success, so evaluation will stop
190 * after the first successful method.
193 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
194 git_get_exec_path_bsd_sysctl(buf
) &&
195 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
197 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
198 git_get_exec_path_darwin(buf
) &&
199 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
201 #ifdef PROCFS_EXECUTABLE_PATH
202 git_get_exec_path_procfs(buf
) &&
203 #endif /* PROCFS_EXECUTABLE_PATH */
206 git_get_exec_path_wpgmptr(buf
) &&
207 #endif /* HAVE_WPGMPTR */
209 git_get_exec_path_from_argv0(buf
, argv0
)) {
213 if (strbuf_normalize_path(buf
)) {
214 trace_printf("trace: could not normalize path: %s\n", buf
->buf
);
218 trace2_cmd_path(buf
->buf
);
223 void git_resolve_executable_dir(const char *argv0
)
225 struct strbuf buf
= STRBUF_INIT
;
229 if (git_get_exec_path(&buf
, argv0
)) {
231 "trace: could not determine executable path from: %s\n",
233 strbuf_release(&buf
);
237 resolved
= strbuf_detach(&buf
, NULL
);
238 slash
= find_last_dir_sep(resolved
);
240 resolved
[slash
- resolved
] = '\0';
242 executable_dirname
= resolved
;
243 trace_printf("trace: resolved executable dir: %s\n",
250 * When not using a runtime prefix, Git uses a hard-coded path.
252 static const char *system_prefix(void)
254 return FALLBACK_RUNTIME_PREFIX
;
258 * This is called during initialization, but No work needs to be done here when
259 * runtime prefix is not being used.
261 void git_resolve_executable_dir(const char *argv0 UNUSED
)
265 #endif /* RUNTIME_PREFIX */
267 char *system_path(const char *path
)
269 struct strbuf d
= STRBUF_INIT
;
271 if (is_absolute_path(path
))
272 return xstrdup(path
);
274 strbuf_addf(&d
, "%s/%s", system_prefix(), path
);
275 return strbuf_detach(&d
, NULL
);
278 static const char *exec_path_value
;
280 void git_set_exec_path(const char *exec_path
)
282 exec_path_value
= exec_path
;
284 * Propagate this setting to external programs.
286 setenv(EXEC_PATH_ENVIRONMENT
, exec_path
, 1);
289 /* Returns the highest-priority location to look for git programs. */
290 const char *git_exec_path(void)
292 if (!exec_path_value
) {
293 const char *env
= getenv(EXEC_PATH_ENVIRONMENT
);
295 exec_path_value
= xstrdup(env
);
297 exec_path_value
= system_path(GIT_EXEC_PATH
);
299 return exec_path_value
;
302 static void add_path(struct strbuf
*out
, const char *path
)
305 strbuf_add_absolute_path(out
, path
);
306 strbuf_addch(out
, PATH_SEP
);
310 void setup_path(void)
312 const char *exec_path
= git_exec_path();
313 const char *old_path
= getenv("PATH");
314 struct strbuf new_path
= STRBUF_INIT
;
316 git_set_exec_path(exec_path
);
317 add_path(&new_path
, exec_path
);
320 strbuf_addstr(&new_path
, old_path
);
322 strbuf_addstr(&new_path
, _PATH_DEFPATH
);
324 setenv("PATH", new_path
.buf
, 1);
326 strbuf_release(&new_path
);
329 const char **prepare_git_cmd(struct strvec
*out
, const char **argv
)
331 strvec_push(out
, "git");
332 strvec_pushv(out
, argv
);
336 int execv_git_cmd(const char **argv
)
338 struct strvec nargv
= STRVEC_INIT
;
340 prepare_git_cmd(&nargv
, argv
);
341 trace_argv_printf(nargv
.v
, "trace: exec:");
343 /* execvp() can only ever return if it fails */
344 sane_execvp("git", (char **)nargv
.v
);
346 trace_printf("trace: exec failed: %s\n", strerror(errno
));
348 strvec_clear(&nargv
);
352 int execl_git_cmd(const char *cmd
, ...)
355 const char *argv
[MAX_ARGS
+ 1];
359 va_start(param
, cmd
);
362 while (argc
< MAX_ARGS
) {
363 arg
= argv
[argc
++] = va_arg(param
, char *);
368 if (MAX_ARGS
<= argc
)
369 return error(_("too many args to run %s"), cmd
);
372 return execv_git_cmd(argv
);