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