1#include "cache.h"
2#include "exec_cmd.h"
3#include "quote.h"
4#include "argv-array.h"
5#define MAX_ARGS 32
6
7static const char *argv_exec_path;
8
9#ifdef RUNTIME_PREFIX
10static const char *argv0_path;
11
12static const char *system_prefix(void)
13{
14 static const char *prefix;
15
16 assert(argv0_path);
17 assert(is_absolute_path(argv0_path));
18
19 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"))) {
23 prefix = PREFIX;
24 trace_printf("RUNTIME_PREFIX requested, "
25 "but prefix computation failed. "
26 "Using static fallback '%s'.\n", prefix);
27 }
28 return prefix;
29}
30
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
44#else
45
46static const char *system_prefix(void)
47{
48 return PREFIX;
49}
50
51void git_extract_argv0_path(const char *argv0)
52{
53}
54
55#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);
65 return strbuf_detach(&d, NULL);
66}
67
68void git_set_argv_exec_path(const char *exec_path)
69{
70 argv_exec_path = exec_path;
71 /*
72 * Propagate this setting to external programs.
73 */
74 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
75}
76
77
78/* Returns the highest-priority, location to look for git programs. */
79const char *git_exec_path(void)
80{
81 static char *cached_exec_path;
82
83 if (argv_exec_path)
84 return argv_exec_path;
85
86 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);
92 }
93 return cached_exec_path;
94}
95
96static void add_path(struct strbuf *out, const char *path)
97{
98 if (path && *path) {
99 strbuf_add_absolute_path(out, path);
100 strbuf_addch(out, PATH_SEP);
101 }
102}
103
104void setup_path(void)
105{
106 const char *old_path = getenv("PATH");
107 struct strbuf new_path = STRBUF_INIT;
108
109 add_path(&new_path, git_exec_path());
110
111 if (old_path)
112 strbuf_addstr(&new_path, old_path);
113 else
114 strbuf_addstr(&new_path, _PATH_DEFPATH);
115
116 setenv("PATH", new_path.buf, 1);
117
118 strbuf_release(&new_path);
119}
120
121const char **prepare_git_cmd(struct argv_array *out, const char **argv)
122{
123 argv_array_push(out, "git");
124 argv_array_pushv(out, argv);
125 return out->argv;
126}
127
128int execv_git_cmd(const char **argv) {
129 struct argv_array nargv = ARGV_ARRAY_INIT;
130
131 prepare_git_cmd(&nargv, argv);
132 trace_argv_printf(nargv.argv, "trace: exec:");
133
134 /* execvp() can only ever return if it fails */
135 sane_execvp("git", (char **)nargv.argv);
136
137 trace_printf("trace: exec failed: %s\n", strerror(errno));
138
139 argv_array_clear(&nargv);
140 return -1;
141}
142
143
144int execl_git_cmd(const char *cmd,...)
145{
146 int argc;
147 const char *argv[MAX_ARGS + 1];
148 const char *arg;
149 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}