blob: fb94aeba9cef2893ea7173e5634640e596659474 [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;
Johannes Sixte1464ca2008-07-21 21:19:52 +02008static const char *argv0_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05009
Junio C Hamano59362e52014-11-24 11:33:54 -080010char *system_path(const char *path)
Steffen Prohaska2de9de52008-07-13 22:31:18 +020011{
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010012#ifdef RUNTIME_PREFIX
13 static const char *prefix;
14#else
Steffen Prohaska026fa0d2009-01-18 13:00:09 +010015 static const char *prefix = PREFIX;
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010016#endif
Steffen Prohaska026fa0d2009-01-18 13:00:09 +010017 struct strbuf d = STRBUF_INIT;
18
19 if (is_absolute_path(path))
Junio C Hamano59362e52014-11-24 11:33:54 -080020 return xstrdup(path);
Steffen Prohaska026fa0d2009-01-18 13:00:09 +010021
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010022#ifdef RUNTIME_PREFIX
23 assert(argv0_path);
24 assert(is_absolute_path(argv0_path));
25
Johannes Schindelin024aa7d2009-02-19 20:10:53 +010026 if (!prefix &&
27 !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
28 !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
29 !(prefix = strip_path_suffix(argv0_path, "git"))) {
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010030 prefix = PREFIX;
Johannes Sixtaa094572010-02-23 12:42:56 +010031 trace_printf("RUNTIME_PREFIX requested, "
Steffen Prohaska35fb0e862009-01-18 13:00:14 +010032 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
34 }
35#endif
36
Steffen Prohaska026fa0d2009-01-18 13:00:09 +010037 strbuf_addf(&d, "%s/%s", prefix, path);
Junio C Hamano59362e52014-11-24 11:33:54 -080038 return strbuf_detach(&d, NULL);
Steffen Prohaska2de9de52008-07-13 22:31:18 +020039}
40
Jeff King6854a8f2016-11-26 23:31:13 -050041void git_extract_argv0_path(const char *argv0)
Johannes Sixte1464ca2008-07-21 21:19:52 +020042{
Steffen Prohaska2cd72b02009-01-18 13:00:11 +010043 const char *slash;
44
45 if (!argv0 || !*argv0)
Jeff King6854a8f2016-11-26 23:31:13 -050046 return;
Steve Haslam4dd47c32009-01-18 13:00:10 +010047
Alexander Kuleshovf4598232016-02-19 14:44:48 +060048 slash = find_last_dir_sep(argv0);
Steve Haslam4dd47c32009-01-18 13:00:10 +010049
Jeff King6854a8f2016-11-26 23:31:13 -050050 if (slash)
Steve Haslam4dd47c32009-01-18 13:00:10 +010051 argv0_path = xstrndup(argv0, slash - argv0);
Johannes Sixte1464ca2008-07-21 21:19:52 +020052}
53
Scott R Parish384df832007-10-27 01:36:51 -070054void git_set_argv_exec_path(const char *exec_path)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050055{
Scott R Parish384df832007-10-27 01:36:51 -070056 argv_exec_path = exec_path;
Johannes Sixtc90d5652009-03-21 23:21:18 +010057 /*
58 * Propagate this setting to external programs.
59 */
60 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050061}
62
63
64/* Returns the highest-priority, location to look for git programs. */
Timo Hirvonen962554c2006-02-26 17:13:46 +020065const char *git_exec_path(void)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050066{
Jeff King007ac542017-01-09 01:00:12 -050067 static char *cached_exec_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050068
Scott R Parish384df832007-10-27 01:36:51 -070069 if (argv_exec_path)
70 return argv_exec_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050071
Jeff King007ac542017-01-09 01:00:12 -050072 if (!cached_exec_path) {
73 const char *env = getenv(EXEC_PATH_ENVIRONMENT);
74 if (env && *env)
75 cached_exec_path = xstrdup(env);
76 else
77 cached_exec_path = system_path(GIT_EXEC_PATH);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050078 }
Jeff King007ac542017-01-09 01:00:12 -050079 return cached_exec_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050080}
81
Scott R Parish511707d2007-10-28 04:17:20 -070082static void add_path(struct strbuf *out, const char *path)
83{
84 if (path && *path) {
René Scharfe9610dec2014-07-28 20:34:42 +020085 strbuf_add_absolute_path(out, path);
Johannes Sixt80ba0742007-12-03 21:55:57 +010086 strbuf_addch(out, PATH_SEP);
Scott R Parish511707d2007-10-28 04:17:20 -070087 }
88}
89
Johannes Sixte1464ca2008-07-21 21:19:52 +020090void setup_path(void)
Scott R Parish511707d2007-10-28 04:17:20 -070091{
92 const char *old_path = getenv("PATH");
Brandon Caseyf285a2d2008-10-09 14:12:12 -050093 struct strbuf new_path = STRBUF_INIT;
Scott R Parish511707d2007-10-28 04:17:20 -070094
Steffen Prohaska8e346282009-01-18 13:00:13 +010095 add_path(&new_path, git_exec_path());
Scott R Parish511707d2007-10-28 04:17:20 -070096
97 if (old_path)
98 strbuf_addstr(&new_path, old_path);
99 else
Chris Webbcb6a22c2010-04-13 10:07:13 +0100100 strbuf_addstr(&new_path, _PATH_DEFPATH);
Scott R Parish511707d2007-10-28 04:17:20 -0700101
102 setenv("PATH", new_path.buf, 1);
103
104 strbuf_release(&new_path);
105}
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500106
Jeff King20574f52016-02-22 17:44:39 -0500107const char **prepare_git_cmd(struct argv_array *out, const char **argv)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500108{
Jeff King20574f52016-02-22 17:44:39 -0500109 argv_array_push(out, "git");
110 argv_array_pushv(out, argv);
111 return out->argv;
Steffen Prohaska4933e5e2008-07-28 07:50:27 +0200112}
113
114int execv_git_cmd(const char **argv) {
Jeff King20574f52016-02-22 17:44:39 -0500115 struct argv_array nargv = ARGV_ARRAY_INIT;
116
117 prepare_git_cmd(&nargv, argv);
118 trace_argv_printf(nargv.argv, "trace: exec:");
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500119
Scott R Parish511707d2007-10-28 04:17:20 -0700120 /* execvp() can only ever return if it fails */
Jeff King20574f52016-02-22 17:44:39 -0500121 sane_execvp("git", (char **)nargv.argv);
Dmitry V. Levind6859902006-05-30 18:58:52 +0400122
Scott R Parish511707d2007-10-28 04:17:20 -0700123 trace_printf("trace: exec failed: %s\n", strerror(errno));
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500124
Jeff King20574f52016-02-22 17:44:39 -0500125 argv_array_clear(&nargv);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500126 return -1;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500127}
128
129
Junio C Hamano9201c702006-03-05 02:47:29 -0800130int execl_git_cmd(const char *cmd,...)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500131{
132 int argc;
Junio C Hamano9201c702006-03-05 02:47:29 -0800133 const char *argv[MAX_ARGS + 1];
134 const char *arg;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500135 va_list param;
136
137 va_start(param, cmd);
138 argv[0] = cmd;
139 argc = 1;
140 while (argc < MAX_ARGS) {
141 arg = argv[argc++] = va_arg(param, char *);
142 if (!arg)
143 break;
144 }
145 va_end(param);
146 if (MAX_ARGS <= argc)
147 return error("too many args to run %s", cmd);
148
149 argv[argc] = NULL;
150 return execv_git_cmd(argv);
151}