blob: ce192a2d64c846e9d7ccffe10c9476f9c5abb8b8 [file] [log] [blame]
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05001#include "cache.h"
2#include "exec_cmd.h"
Matthias Lederhofer575ba9d2006-06-25 15:56:18 +02003#include "quote.h"
Jeff King20574f52016-02-22 17:44:39 -05004#include "argv-array.h"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05005#define MAX_ARGS 32
6
Scott R Parish384df832007-10-27 01:36:51 -07007static const char *argv_exec_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05008
Jeff King39b2f6a2017-09-06 08:30:28 -04009#ifdef RUNTIME_PREFIX
Jeff Kingc1bb33c2017-09-06 08:32:10 -040010static const char *argv0_path;
Jeff King39b2f6a2017-09-06 08:30:28 -040011
12static const char *system_prefix(void)
Steffen Prohaska2de9de52008-07-13 22:31:18 +020013{
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010014 static const char *prefix;
Steffen Prohaska026fa0d2009-01-18 13:00:09 +010015
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010016 assert(argv0_path);
17 assert(is_absolute_path(argv0_path));
18
Johannes Schindelin024aa7d2009-02-19 20:10:53 +010019 if (!prefix &&
20 !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
21 !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
22 !(prefix = strip_path_suffix(argv0_path, "git"))) {
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010023 prefix = PREFIX;
Johannes Sixtaa094572010-02-23 12:42:56 +010024 trace_printf("RUNTIME_PREFIX requested, "
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010025 "but prefix computation failed. "
26 "Using static fallback '%s'.\n", prefix);
27 }
Jeff King39b2f6a2017-09-06 08:30:28 -040028 return prefix;
29}
Jeff Kingc1bb33c2017-09-06 08:32:10 -040030
31void git_extract_argv0_path(const char *argv0)
32{
33 const char *slash;
34
35 if (!argv0 || !*argv0)
36 return;
37
38 slash = find_last_dir_sep(argv0);
39
40 if (slash)
41 argv0_path = xstrndup(argv0, slash - argv0);
42}
43
Jeff King39b2f6a2017-09-06 08:30:28 -040044#else
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010045
Jeff King39b2f6a2017-09-06 08:30:28 -040046static const char *system_prefix(void)
47{
48 return PREFIX;
49}
50
Jeff Kingc1bb33c2017-09-06 08:32:10 -040051void git_extract_argv0_path(const char *argv0)
52{
53}
54
Jeff King39b2f6a2017-09-06 08:30:28 -040055#endif /* RUNTIME_PREFIX */
56
57char *system_path(const char *path)
58{
59 struct strbuf d = STRBUF_INIT;
60
61 if (is_absolute_path(path))
62 return xstrdup(path);
63
64 strbuf_addf(&d, "%s/%s", system_prefix(), path);
Junio C Hamano59362e52014-11-24 11:33:54 -080065 return strbuf_detach(&d, NULL);
Steffen Prohaska2de9de52008-07-13 22:31:18 +020066}
67
Scott R Parish384df832007-10-27 01:36:51 -070068void git_set_argv_exec_path(const char *exec_path)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050069{
Scott R Parish384df832007-10-27 01:36:51 -070070 argv_exec_path = exec_path;
Johannes Sixtc90d5652009-03-21 23:21:18 +010071 /*
72 * Propagate this setting to external programs.
73 */
74 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050075}
76
77
78/* Returns the highest-priority, location to look for git programs. */
Timo Hirvonen962554c2006-02-26 17:13:46 +020079const char *git_exec_path(void)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050080{
Jeff King007ac542017-01-09 01:00:12 -050081 static char *cached_exec_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050082
Scott R Parish384df832007-10-27 01:36:51 -070083 if (argv_exec_path)
84 return argv_exec_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050085
Jeff King007ac542017-01-09 01:00:12 -050086 if (!cached_exec_path) {
87 const char *env = getenv(EXEC_PATH_ENVIRONMENT);
88 if (env && *env)
89 cached_exec_path = xstrdup(env);
90 else
91 cached_exec_path = system_path(GIT_EXEC_PATH);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050092 }
Jeff King007ac542017-01-09 01:00:12 -050093 return cached_exec_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050094}
95
Scott R Parish511707d2007-10-28 04:17:20 -070096static void add_path(struct strbuf *out, const char *path)
97{
98 if (path && *path) {
René Scharfe9610dec2014-07-28 20:34:42 +020099 strbuf_add_absolute_path(out, path);
Johannes Sixt80ba0742007-12-03 21:55:57 +0100100 strbuf_addch(out, PATH_SEP);
Scott R Parish511707d2007-10-28 04:17:20 -0700101 }
102}
103
Johannes Sixte1464ca2008-07-21 21:19:52 +0200104void setup_path(void)
Scott R Parish511707d2007-10-28 04:17:20 -0700105{
106 const char *old_path = getenv("PATH");
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500107 struct strbuf new_path = STRBUF_INIT;
Scott R Parish511707d2007-10-28 04:17:20 -0700108
Steffen Prohaska8e346282009-01-18 13:00:13 +0100109 add_path(&new_path, git_exec_path());
Scott R Parish511707d2007-10-28 04:17:20 -0700110
111 if (old_path)
112 strbuf_addstr(&new_path, old_path);
113 else
Chris Webbcb6a22c2010-04-13 10:07:13 +0100114 strbuf_addstr(&new_path, _PATH_DEFPATH);
Scott R Parish511707d2007-10-28 04:17:20 -0700115
116 setenv("PATH", new_path.buf, 1);
117
118 strbuf_release(&new_path);
119}
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500120
Jeff King20574f52016-02-22 17:44:39 -0500121const char **prepare_git_cmd(struct argv_array *out, const char **argv)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500122{
Jeff King20574f52016-02-22 17:44:39 -0500123 argv_array_push(out, "git");
124 argv_array_pushv(out, argv);
125 return out->argv;
Steffen Prohaska4933e5e2008-07-28 07:50:27 +0200126}
127
128int execv_git_cmd(const char **argv) {
Jeff King20574f52016-02-22 17:44:39 -0500129 struct argv_array nargv = ARGV_ARRAY_INIT;
130
131 prepare_git_cmd(&nargv, argv);
132 trace_argv_printf(nargv.argv, "trace: exec:");
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500133
Scott R Parish511707d2007-10-28 04:17:20 -0700134 /* execvp() can only ever return if it fails */
Jeff King20574f52016-02-22 17:44:39 -0500135 sane_execvp("git", (char **)nargv.argv);
Dmitry V. Levind6859902006-05-30 18:58:52 +0400136
Scott R Parish511707d2007-10-28 04:17:20 -0700137 trace_printf("trace: exec failed: %s\n", strerror(errno));
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500138
Jeff King20574f52016-02-22 17:44:39 -0500139 argv_array_clear(&nargv);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500140 return -1;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500141}
142
143
Junio C Hamano9201c702006-03-05 02:47:29 -0800144int execl_git_cmd(const char *cmd,...)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500145{
146 int argc;
Junio C Hamano9201c702006-03-05 02:47:29 -0800147 const char *argv[MAX_ARGS + 1];
148 const char *arg;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500149 va_list param;
150
151 va_start(param, cmd);
152 argv[0] = cmd;
153 argc = 1;
154 while (argc < MAX_ARGS) {
155 arg = argv[argc++] = va_arg(param, char *);
156 if (!arg)
157 break;
158 }
159 va_end(param);
160 if (MAX_ARGS <= argc)
161 return error("too many args to run %s", cmd);
162
163 argv[argc] = NULL;
164 return execv_git_cmd(argv);
165}