Modify setup_path() to only add git_exec_path() to PATH
[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{
026fa0d5
SP
12 static const char *prefix = PREFIX;
13 struct strbuf d = STRBUF_INIT;
14
15 if (is_absolute_path(path))
16 return path;
17
18 strbuf_addf(&d, "%s/%s", prefix, path);
19 path = strbuf_detach(&d, NULL);
2de9de5e
SP
20 return path;
21}
22
4dd47c3b 23const char *git_extract_argv0_path(const char *argv0)
e1464ca7 24{
2cd72b0b
SP
25 const char *slash;
26
27 if (!argv0 || !*argv0)
28 return NULL;
29 slash = argv0 + strlen(argv0);
4dd47c3b
SH
30
31 while (argv0 <= slash && !is_dir_sep(*slash))
32 slash--;
33
34 if (slash >= argv0) {
35 argv0_path = xstrndup(argv0, slash - argv0);
36 return slash + 1;
37 }
38
39 return argv0;
e1464ca7
JS
40}
41
384df833 42void git_set_argv_exec_path(const char *exec_path)
77cb17e9 43{
384df833 44 argv_exec_path = exec_path;
77cb17e9
MO
45}
46
47
48/* Returns the highest-priority, location to look for git programs. */
962554c6 49const char *git_exec_path(void)
77cb17e9
MO
50{
51 const char *env;
52
384df833
SP
53 if (argv_exec_path)
54 return argv_exec_path;
77cb17e9 55
d4ebc36c 56 env = getenv(EXEC_PATH_ENVIRONMENT);
2b601626 57 if (env && *env) {
77cb17e9
MO
58 return env;
59 }
60
49fa65a7 61 return system_path(GIT_EXEC_PATH);
77cb17e9
MO
62}
63
511707d4
SP
64static void add_path(struct strbuf *out, const char *path)
65{
66 if (path && *path) {
67 if (is_absolute_path(path))
68 strbuf_addstr(out, path);
69 else
10c4c881 70 strbuf_addstr(out, make_nonrelative_path(path));
511707d4 71
80ba074f 72 strbuf_addch(out, PATH_SEP);
511707d4
SP
73 }
74}
75
e1464ca7 76void setup_path(void)
511707d4
SP
77{
78 const char *old_path = getenv("PATH");
f285a2d7 79 struct strbuf new_path = STRBUF_INIT;
511707d4 80
8e346283 81 add_path(&new_path, git_exec_path());
e1464ca7 82 add_path(&new_path, argv0_path);
511707d4
SP
83
84 if (old_path)
85 strbuf_addstr(&new_path, old_path);
86 else
87 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
88
89 setenv("PATH", new_path.buf, 1);
90
91 strbuf_release(&new_path);
92}
77cb17e9 93
4933e5eb 94const char **prepare_git_cmd(const char **argv)
77cb17e9 95{
7550be0a
JH
96 int argc;
97 const char **nargv;
77cb17e9 98
7550be0a
JH
99 for (argc = 0; argv[argc]; argc++)
100 ; /* just counting */
101 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
77cb17e9 102
7550be0a
JH
103 nargv[0] = "git";
104 for (argc = 0; argv[argc]; argc++)
105 nargv[argc + 1] = argv[argc];
106 nargv[argc + 1] = NULL;
4933e5eb
SP
107 return nargv;
108}
109
110int execv_git_cmd(const char **argv) {
111 const char **nargv = prepare_git_cmd(argv);
7550be0a 112 trace_argv_printf(nargv, "trace: exec:");
575ba9d6 113
511707d4 114 /* execvp() can only ever return if it fails */
7550be0a 115 execvp("git", (char **)nargv);
77cb17e9 116
511707d4 117 trace_printf("trace: exec failed: %s\n", strerror(errno));
575ba9d6 118
7550be0a 119 free(nargv);
511707d4 120 return -1;
77cb17e9
MO
121}
122
123
9201c707 124int execl_git_cmd(const char *cmd,...)
77cb17e9
MO
125{
126 int argc;
9201c707
JH
127 const char *argv[MAX_ARGS + 1];
128 const char *arg;
77cb17e9
MO
129 va_list param;
130
131 va_start(param, cmd);
132 argv[0] = cmd;
133 argc = 1;
134 while (argc < MAX_ARGS) {
135 arg = argv[argc++] = va_arg(param, char *);
136 if (!arg)
137 break;
138 }
139 va_end(param);
140 if (MAX_ARGS <= argc)
141 return error("too many args to run %s", cmd);
142
143 argv[argc] = NULL;
144 return execv_git_cmd(argv);
145}