format-patch: thread as reply to cover letter even with in-reply-to
[git/gitweb.git] / exec_cmd.c
blobf234066defd637dc3c048ba3d9eb43f1ecbad446
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
4 #define MAX_ARGS 32
6 extern char **environ;
7 static const char *argv_exec_path;
8 static const char *argv0_path;
10 const char *system_path(const char *path)
12 #ifdef RUNTIME_PREFIX
13 static const char *prefix;
14 #else
15 static const char *prefix = PREFIX;
16 #endif
17 struct strbuf d = STRBUF_INIT;
19 if (is_absolute_path(path))
20 return path;
22 #ifdef RUNTIME_PREFIX
23 assert(argv0_path);
24 assert(is_absolute_path(argv0_path));
26 if (!prefix) {
27 const char *strip[] = {
28 GIT_EXEC_PATH,
29 BINDIR,
32 const char **s;
34 for (s = strip; *s; s++) {
35 const char *sargv = argv0_path + strlen(argv0_path);
36 const char *ss = *s + strlen(*s);
37 while (argv0_path < sargv && *s < ss
38 && (*sargv == *ss ||
39 (is_dir_sep(*sargv) && is_dir_sep(*ss)))) {
40 sargv--;
41 ss--;
43 if (*s == ss) {
44 struct strbuf d = STRBUF_INIT;
45 /* We also skip the trailing directory separator. */
46 assert(sargv - argv0_path - 1 >= 0);
47 strbuf_add(&d, argv0_path, sargv - argv0_path - 1);
48 prefix = strbuf_detach(&d, NULL);
49 break;
54 if (!prefix) {
55 prefix = PREFIX;
56 fprintf(stderr, "RUNTIME_PREFIX requested, "
57 "but prefix computation failed. "
58 "Using static fallback '%s'.\n", prefix);
60 #endif
62 strbuf_addf(&d, "%s/%s", prefix, path);
63 path = strbuf_detach(&d, NULL);
64 return path;
67 const char *git_extract_argv0_path(const char *argv0)
69 const char *slash;
71 if (!argv0 || !*argv0)
72 return NULL;
73 slash = argv0 + strlen(argv0);
75 while (argv0 <= slash && !is_dir_sep(*slash))
76 slash--;
78 if (slash >= argv0) {
79 argv0_path = xstrndup(argv0, slash - argv0);
80 return slash + 1;
83 return argv0;
86 void git_set_argv_exec_path(const char *exec_path)
88 argv_exec_path = exec_path;
92 /* Returns the highest-priority, location to look for git programs. */
93 const char *git_exec_path(void)
95 const char *env;
97 if (argv_exec_path)
98 return argv_exec_path;
100 env = getenv(EXEC_PATH_ENVIRONMENT);
101 if (env && *env) {
102 return env;
105 return system_path(GIT_EXEC_PATH);
108 static void add_path(struct strbuf *out, const char *path)
110 if (path && *path) {
111 if (is_absolute_path(path))
112 strbuf_addstr(out, path);
113 else
114 strbuf_addstr(out, make_nonrelative_path(path));
116 strbuf_addch(out, PATH_SEP);
120 void setup_path(void)
122 const char *old_path = getenv("PATH");
123 struct strbuf new_path = STRBUF_INIT;
125 add_path(&new_path, git_exec_path());
126 add_path(&new_path, argv0_path);
128 if (old_path)
129 strbuf_addstr(&new_path, old_path);
130 else
131 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
133 setenv("PATH", new_path.buf, 1);
135 strbuf_release(&new_path);
138 const char **prepare_git_cmd(const char **argv)
140 int argc;
141 const char **nargv;
143 for (argc = 0; argv[argc]; argc++)
144 ; /* just counting */
145 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
147 nargv[0] = "git";
148 for (argc = 0; argv[argc]; argc++)
149 nargv[argc + 1] = argv[argc];
150 nargv[argc + 1] = NULL;
151 return nargv;
154 int execv_git_cmd(const char **argv) {
155 const char **nargv = prepare_git_cmd(argv);
156 trace_argv_printf(nargv, "trace: exec:");
158 /* execvp() can only ever return if it fails */
159 execvp("git", (char **)nargv);
161 trace_printf("trace: exec failed: %s\n", strerror(errno));
163 free(nargv);
164 return -1;
168 int execl_git_cmd(const char *cmd,...)
170 int argc;
171 const char *argv[MAX_ARGS + 1];
172 const char *arg;
173 va_list param;
175 va_start(param, cmd);
176 argv[0] = cmd;
177 argc = 1;
178 while (argc < MAX_ARGS) {
179 arg = argv[argc++] = va_arg(param, char *);
180 if (!arg)
181 break;
183 va_end(param);
184 if (MAX_ARGS <= argc)
185 return error("too many args to run %s", cmd);
187 argv[argc] = NULL;
188 return execv_git_cmd(argv);