Git 1.7.0
[git/git.git] / exec_cmd.c
CommitLineData
77cb17e9
MO
1#include "cache.h"
2#include "exec_cmd.h"
575ba9d6 3#include "quote.h"
77cb17e9
MO
4#define MAX_ARGS 32
5
6extern char **environ;
384df833 7static const char *argv_exec_path;
e1464ca7 8static const char *argv0_path;
77cb17e9 9
2de9de5e
SP
10const char *system_path(const char *path)
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))
20 return path;
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
SP
30 prefix = PREFIX;
31 fprintf(stderr, "RUNTIME_PREFIX requested, "
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
34 }
35#endif
36
026fa0d5
SP
37 strbuf_addf(&d, "%s/%s", prefix, path);
38 path = strbuf_detach(&d, NULL);
2de9de5e
SP
39 return path;
40}
41
4dd47c3b 42const char *git_extract_argv0_path(const char *argv0)
e1464ca7 43{
2cd72b0b
SP
44 const char *slash;
45
46 if (!argv0 || !*argv0)
47 return NULL;
48 slash = argv0 + strlen(argv0);
4dd47c3b
SH
49
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;
e1464ca7
JS
59}
60
384df833 61void git_set_argv_exec_path(const char *exec_path)
77cb17e9 62{
384df833 63 argv_exec_path = exec_path;
c90d565a
JS
64 /*
65 * Propagate this setting to external programs.
66 */
67 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
77cb17e9
MO
68}
69
70
71/* Returns the highest-priority, location to look for git programs. */
962554c6 72const char *git_exec_path(void)
77cb17e9
MO
73{
74 const char *env;
75
384df833
SP
76 if (argv_exec_path)
77 return argv_exec_path;
77cb17e9 78
d4ebc36c 79 env = getenv(EXEC_PATH_ENVIRONMENT);
2b601626 80 if (env && *env) {
77cb17e9
MO
81 return env;
82 }
83
49fa65a7 84 return system_path(GIT_EXEC_PATH);
77cb17e9
MO
85}
86
511707d4
SP
87static 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
10c4c881 93 strbuf_addstr(out, make_nonrelative_path(path));
511707d4 94
80ba074f 95 strbuf_addch(out, PATH_SEP);
511707d4
SP
96 }
97}
98
e1464ca7 99void setup_path(void)
511707d4
SP
100{
101 const char *old_path = getenv("PATH");
f285a2d7 102 struct strbuf new_path = STRBUF_INIT;
511707d4 103
8e346283 104 add_path(&new_path, git_exec_path());
e1464ca7 105 add_path(&new_path, argv0_path);
511707d4
SP
106
107 if (old_path)
108 strbuf_addstr(&new_path, old_path);
109 else
110 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
111
112 setenv("PATH", new_path.buf, 1);
113
114 strbuf_release(&new_path);
115}
77cb17e9 116
4933e5eb 117const char **prepare_git_cmd(const char **argv)
77cb17e9 118{
7550be0a
JH
119 int argc;
120 const char **nargv;
77cb17e9 121
7550be0a
JH
122 for (argc = 0; argv[argc]; argc++)
123 ; /* just counting */
124 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
77cb17e9 125
7550be0a
JH
126 nargv[0] = "git";
127 for (argc = 0; argv[argc]; argc++)
128 nargv[argc + 1] = argv[argc];
129 nargv[argc + 1] = NULL;
4933e5eb
SP
130 return nargv;
131}
132
133int execv_git_cmd(const char **argv) {
134 const char **nargv = prepare_git_cmd(argv);
7550be0a 135 trace_argv_printf(nargv, "trace: exec:");
575ba9d6 136
511707d4 137 /* execvp() can only ever return if it fails */
7550be0a 138 execvp("git", (char **)nargv);
77cb17e9 139
511707d4 140 trace_printf("trace: exec failed: %s\n", strerror(errno));
575ba9d6 141
7550be0a 142 free(nargv);
511707d4 143 return -1;
77cb17e9
MO
144}
145
146
9201c707 147int execl_git_cmd(const char *cmd,...)
77cb17e9
MO
148{
149 int argc;
9201c707
JH
150 const char *argv[MAX_ARGS + 1];
151 const char *arg;
77cb17e9
MO
152 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}