Git 2.12.4
[git/git.git] / exec_cmd.c
CommitLineData
77cb17e9
MO
1#include "cache.h"
2#include "exec_cmd.h"
575ba9d6 3#include "quote.h"
20574f55 4#include "argv-array.h"
77cb17e9
MO
5#define MAX_ARGS 32
6
384df833 7static const char *argv_exec_path;
e1464ca7 8static const char *argv0_path;
77cb17e9 9
59362e56 10char *system_path(const char *path)
2de9de5e 11{
35fb0e86
SP
12#ifdef RUNTIME_PREFIX
13 static const char *prefix;
14#else
026fa0d5 15 static const char *prefix = PREFIX;
35fb0e86 16#endif
026fa0d5
SP
17 struct strbuf d = STRBUF_INIT;
18
19 if (is_absolute_path(path))
59362e56 20 return xstrdup(path);
026fa0d5 21
35fb0e86
SP
22#ifdef RUNTIME_PREFIX
23 assert(argv0_path);
24 assert(is_absolute_path(argv0_path));
25
024aa7d8
JS
26 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"))) {
35fb0e86 30 prefix = PREFIX;
aa094570 31 trace_printf("RUNTIME_PREFIX requested, "
35fb0e86
SP
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
34 }
35#endif
36
026fa0d5 37 strbuf_addf(&d, "%s/%s", prefix, path);
59362e56 38 return strbuf_detach(&d, NULL);
2de9de5e
SP
39}
40
6854a8f5 41void git_extract_argv0_path(const char *argv0)
e1464ca7 42{
2cd72b0b
SP
43 const char *slash;
44
45 if (!argv0 || !*argv0)
6854a8f5 46 return;
4dd47c3b 47
f4598233 48 slash = find_last_dir_sep(argv0);
4dd47c3b 49
6854a8f5 50 if (slash)
4dd47c3b 51 argv0_path = xstrndup(argv0, slash - argv0);
e1464ca7
JS
52}
53
384df833 54void git_set_argv_exec_path(const char *exec_path)
77cb17e9 55{
384df833 56 argv_exec_path = exec_path;
c90d565a
JS
57 /*
58 * Propagate this setting to external programs.
59 */
60 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
77cb17e9
MO
61}
62
63
64/* Returns the highest-priority, location to look for git programs. */
962554c6 65const char *git_exec_path(void)
77cb17e9 66{
007ac544 67 static char *cached_exec_path;
77cb17e9 68
384df833
SP
69 if (argv_exec_path)
70 return argv_exec_path;
77cb17e9 71
007ac544
JK
72 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);
77cb17e9 78 }
007ac544 79 return cached_exec_path;
77cb17e9
MO
80}
81
511707d4
SP
82static void add_path(struct strbuf *out, const char *path)
83{
84 if (path && *path) {
9610decf 85 strbuf_add_absolute_path(out, path);
80ba074f 86 strbuf_addch(out, PATH_SEP);
511707d4
SP
87 }
88}
89
e1464ca7 90void setup_path(void)
511707d4
SP
91{
92 const char *old_path = getenv("PATH");
f285a2d7 93 struct strbuf new_path = STRBUF_INIT;
511707d4 94
8e346283 95 add_path(&new_path, git_exec_path());
511707d4
SP
96
97 if (old_path)
98 strbuf_addstr(&new_path, old_path);
99 else
cb6a22c0 100 strbuf_addstr(&new_path, _PATH_DEFPATH);
511707d4
SP
101
102 setenv("PATH", new_path.buf, 1);
103
104 strbuf_release(&new_path);
105}
77cb17e9 106
20574f55 107const char **prepare_git_cmd(struct argv_array *out, const char **argv)
77cb17e9 108{
20574f55
JK
109 argv_array_push(out, "git");
110 argv_array_pushv(out, argv);
111 return out->argv;
4933e5eb
SP
112}
113
114int execv_git_cmd(const char **argv) {
20574f55
JK
115 struct argv_array nargv = ARGV_ARRAY_INIT;
116
117 prepare_git_cmd(&nargv, argv);
118 trace_argv_printf(nargv.argv, "trace: exec:");
575ba9d6 119
511707d4 120 /* execvp() can only ever return if it fails */
20574f55 121 sane_execvp("git", (char **)nargv.argv);
77cb17e9 122
511707d4 123 trace_printf("trace: exec failed: %s\n", strerror(errno));
575ba9d6 124
20574f55 125 argv_array_clear(&nargv);
511707d4 126 return -1;
77cb17e9
MO
127}
128
129
9201c707 130int execl_git_cmd(const char *cmd,...)
77cb17e9
MO
131{
132 int argc;
9201c707
JH
133 const char *argv[MAX_ARGS + 1];
134 const char *arg;
77cb17e9
MO
135 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}