1#include "cache.h"
2#include "exec_cmd.h"
3#include "quote.h"
4#define MAX_ARGS 32
5
6static const char *argv_exec_path;
7static const char *argv0_path;
8
9char *system_path(const char *path)
10{
11#ifdef RUNTIME_PREFIX
12 static const char *prefix;
13#else
14 static const char *prefix = PREFIX;
15#endif
16 struct strbuf d = STRBUF_INIT;
17
18 if (is_absolute_path(path))
19 return xstrdup(path);
20
21#ifdef RUNTIME_PREFIX
22 assert(argv0_path);
23 assert(is_absolute_path(argv0_path));
24
25 if (!prefix &&
26 !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) &&
27 !(prefix = strip_path_suffix(argv0_path, BINDIR)) &&
28 !(prefix = strip_path_suffix(argv0_path, "git"))) {
29 prefix = PREFIX;
30 trace_printf("RUNTIME_PREFIX requested, "
31 "but prefix computation failed. "
32 "Using static fallback '%s'.\n", prefix);
33 }
34#endif
35
36 strbuf_addf(&d, "%s/%s", prefix, path);
37 return strbuf_detach(&d, NULL);
38}
39
40const char *git_extract_argv0_path(const char *argv0)
41{
42 const char *slash;
43
44 if (!argv0 || !*argv0)
45 return NULL;
46 slash = argv0 + strlen(argv0);
47
48 while (argv0 <= slash && !is_dir_sep(*slash))
49 slash--;
50
51 if (slash >= argv0) {
52 argv0_path = xstrndup(argv0, slash - argv0);
53 return slash + 1;
54 }
55
56 return argv0;
57}
58
59void git_set_argv_exec_path(const char *exec_path)
60{
61 argv_exec_path = exec_path;
62 /*
63 * Propagate this setting to external programs.
64 */
65 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
66}
67
68
69/* Returns the highest-priority, location to look for git programs. */
70const char *git_exec_path(void)
71{
72 const char *env;
73
74 if (argv_exec_path)
75 return argv_exec_path;
76
77 env = getenv(EXEC_PATH_ENVIRONMENT);
78 if (env && *env) {
79 return env;
80 }
81
82 return system_path(GIT_EXEC_PATH);
83}
84
85static void add_path(struct strbuf *out, const char *path)
86{
87 if (path && *path) {
88 if (is_absolute_path(path))
89 strbuf_addstr(out, path);
90 else
91 strbuf_addstr(out, absolute_path(path));
92
93 strbuf_addch(out, PATH_SEP);
94 }
95}
96
97void setup_path(void)
98{
99 const char *old_path = getenv("PATH");
100 struct strbuf new_path = STRBUF_INIT;
101
102 add_path(&new_path, git_exec_path());
103 add_path(&new_path, argv0_path);
104
105 if (old_path)
106 strbuf_addstr(&new_path, old_path);
107 else
108 strbuf_addstr(&new_path, _PATH_DEFPATH);
109
110 setenv("PATH", new_path.buf, 1);
111
112 strbuf_release(&new_path);
113}
114
115const char **prepare_git_cmd(const char **argv)
116{
117 int argc;
118 const char **nargv;
119
120 for (argc = 0; argv[argc]; argc++)
121 ; /* just counting */
122 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
123
124 nargv[0] = "git";
125 for (argc = 0; argv[argc]; argc++)
126 nargv[argc + 1] = argv[argc];
127 nargv[argc + 1] = NULL;
128 return nargv;
129}
130
131int execv_git_cmd(const char **argv) {
132 const char **nargv = prepare_git_cmd(argv);
133 trace_argv_printf(nargv, "trace: exec:");
134
135 /* execvp() can only ever return if it fails */
136 sane_execvp("git", (char **)nargv);
137
138 trace_printf("trace: exec failed: %s\n", strerror(errno));
139
140 free(nargv);
141 return -1;
142}
143
144
145int execl_git_cmd(const char *cmd,...)
146{
147 int argc;
148 const char *argv[MAX_ARGS + 1];
149 const char *arg;
150 va_list param;
151
152 va_start(param, cmd);
153 argv[0] = cmd;
154 argc = 1;
155 while (argc < MAX_ARGS) {
156 arg = argv[argc++] = va_arg(param, char *);
157 if (!arg)
158 break;
159 }
160 va_end(param);
161 if (MAX_ARGS <= argc)
162 return error("too many args to run %s", cmd);
163
164 argv[argc] = NULL;
165 return execv_git_cmd(argv);
166}