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