blob: bf225706ee377b89035eb21f76f9957cfaf6363b [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"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05004#define MAX_ARGS 32
5
6extern char **environ;
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
Steffen Prohaska2de9de52008-07-13 22:31:18 +020010const char *system_path(const char *path)
11{
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))
20 return path;
21
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);
38 path = strbuf_detach(&d, NULL);
Steffen Prohaska2de9de52008-07-13 22:31:18 +020039 return path;
40}
41
Steve Haslam4dd47c32009-01-18 13:00:10 +010042const char *git_extract_argv0_path(const char *argv0)
Johannes Sixte1464ca2008-07-21 21:19:52 +020043{
Steffen Prohaska2cd72b02009-01-18 13:00:11 +010044 const char *slash;
45
46 if (!argv0 || !*argv0)
47 return NULL;
48 slash = argv0 + strlen(argv0);
Steve Haslam4dd47c32009-01-18 13:00:10 +010049
50 while (argv0 <= slash && !is_dir_sep(*slash))
51 slash--;
52
53 if (slash >= argv0) {
54 argv0_path = xstrndup(argv0, slash - argv0);
55 return slash + 1;
56 }
57
58 return argv0;
Johannes Sixte1464ca2008-07-21 21:19:52 +020059}
60
Scott R Parish384df832007-10-27 01:36:51 -070061void git_set_argv_exec_path(const char *exec_path)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050062{
Scott R Parish384df832007-10-27 01:36:51 -070063 argv_exec_path = exec_path;
Johannes Sixtc90d5652009-03-21 23:21:18 +010064 /*
65 * Propagate this setting to external programs.
66 */
67 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050068}
69
70
71/* Returns the highest-priority, location to look for git programs. */
Timo Hirvonen962554c2006-02-26 17:13:46 +020072const char *git_exec_path(void)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050073{
74 const char *env;
75
Scott R Parish384df832007-10-27 01:36:51 -070076 if (argv_exec_path)
77 return argv_exec_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050078
Junio C Hamanod4ebc362006-12-19 01:28:15 -080079 env = getenv(EXEC_PATH_ENVIRONMENT);
Dmitry V. Levin2b601622006-05-29 04:34:34 +040080 if (env && *env) {
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050081 return env;
82 }
83
Johannes Sixt49fa65a2008-07-23 21:12:18 +020084 return system_path(GIT_EXEC_PATH);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050085}
86
Scott R Parish511707d2007-10-28 04:17:20 -070087static void add_path(struct strbuf *out, const char *path)
88{
89 if (path && *path) {
90 if (is_absolute_path(path))
91 strbuf_addstr(out, path);
92 else
Johannes Sixt10c4c882008-07-21 21:19:55 +020093 strbuf_addstr(out, make_nonrelative_path(path));
Scott R Parish511707d2007-10-28 04:17:20 -070094
Johannes Sixt80ba0742007-12-03 21:55:57 +010095 strbuf_addch(out, PATH_SEP);
Scott R Parish511707d2007-10-28 04:17:20 -070096 }
97}
98
Johannes Sixte1464ca2008-07-21 21:19:52 +020099void setup_path(void)
Scott R Parish511707d2007-10-28 04:17:20 -0700100{
101 const char *old_path = getenv("PATH");
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500102 struct strbuf new_path = STRBUF_INIT;
Scott R Parish511707d2007-10-28 04:17:20 -0700103
Steffen Prohaska8e346282009-01-18 13:00:13 +0100104 add_path(&new_path, git_exec_path());
Johannes Sixte1464ca2008-07-21 21:19:52 +0200105 add_path(&new_path, argv0_path);
Scott R Parish511707d2007-10-28 04:17:20 -0700106
107 if (old_path)
108 strbuf_addstr(&new_path, old_path);
109 else
Chris Webbcb6a22c2010-04-13 10:07:13 +0100110 strbuf_addstr(&new_path, _PATH_DEFPATH);
Scott R Parish511707d2007-10-28 04:17:20 -0700111
112 setenv("PATH", new_path.buf, 1);
113
114 strbuf_release(&new_path);
115}
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500116
Steffen Prohaska4933e5e2008-07-28 07:50:27 +0200117const char **prepare_git_cmd(const char **argv)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500118{
Junio C Hamano7550be02007-12-01 22:09:22 -0800119 int argc;
120 const char **nargv;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500121
Junio C Hamano7550be02007-12-01 22:09:22 -0800122 for (argc = 0; argv[argc]; argc++)
123 ; /* just counting */
124 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
Junio C Hamano9201c702006-03-05 02:47:29 -0800125
Junio C Hamano7550be02007-12-01 22:09:22 -0800126 nargv[0] = "git";
127 for (argc = 0; argv[argc]; argc++)
128 nargv[argc + 1] = argv[argc];
129 nargv[argc + 1] = NULL;
Steffen Prohaska4933e5e2008-07-28 07:50:27 +0200130 return nargv;
131}
132
133int execv_git_cmd(const char **argv) {
134 const char **nargv = prepare_git_cmd(argv);
Junio C Hamano7550be02007-12-01 22:09:22 -0800135 trace_argv_printf(nargv, "trace: exec:");
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500136
Scott R Parish511707d2007-10-28 04:17:20 -0700137 /* execvp() can only ever return if it fails */
Junio C Hamano7550be02007-12-01 22:09:22 -0800138 execvp("git", (char **)nargv);
Dmitry V. Levind6859902006-05-30 18:58:52 +0400139
Scott R Parish511707d2007-10-28 04:17:20 -0700140 trace_printf("trace: exec failed: %s\n", strerror(errno));
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500141
Junio C Hamano7550be02007-12-01 22:09:22 -0800142 free(nargv);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500143 return -1;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500144}
145
146
Junio C Hamano9201c702006-03-05 02:47:29 -0800147int execl_git_cmd(const char *cmd,...)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500148{
149 int argc;
Junio C Hamano9201c702006-03-05 02:47:29 -0800150 const char *argv[MAX_ARGS + 1];
151 const char *arg;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500152 va_list param;
153
154 va_start(param, cmd);
155 argv[0] = cmd;
156 argc = 1;
157 while (argc < MAX_ARGS) {
158 arg = argv[argc++] = va_arg(param, char *);
159 if (!arg)
160 break;
161 }
162 va_end(param);
163 if (MAX_ARGS <= argc)
164 return error("too many args to run %s", cmd);
165
166 argv[argc] = NULL;
167 return execv_git_cmd(argv);
168}