Commit | Line | Data |
---|---|---|
77cb17e9 MO |
1 | #include "cache.h" |
2 | #include "exec_cmd.h" | |
575ba9d6 | 3 | #include "quote.h" |
20574f55 | 4 | #include "argv-array.h" |
77cb17e9 MO |
5 | #define MAX_ARGS 32 |
6 | ||
384df833 | 7 | static const char *argv_exec_path; |
e1464ca7 | 8 | static const char *argv0_path; |
77cb17e9 | 9 | |
35fb0e86 | 10 | #ifdef RUNTIME_PREFIX |
026fa0d5 | 11 | |
39b2f6af JK |
12 | static const char *system_prefix(void) |
13 | { | |
14 | static const char *prefix; | |
026fa0d5 | 15 | |
35fb0e86 SP |
16 | assert(argv0_path); |
17 | assert(is_absolute_path(argv0_path)); | |
18 | ||
024aa7d8 JS |
19 | if (!prefix && |
20 | !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) && | |
21 | !(prefix = strip_path_suffix(argv0_path, BINDIR)) && | |
22 | !(prefix = strip_path_suffix(argv0_path, "git"))) { | |
35fb0e86 | 23 | prefix = PREFIX; |
aa094570 | 24 | trace_printf("RUNTIME_PREFIX requested, " |
35fb0e86 SP |
25 | "but prefix computation failed. " |
26 | "Using static fallback '%s'.\n", prefix); | |
27 | } | |
39b2f6af JK |
28 | return prefix; |
29 | } | |
30 | #else | |
31 | ||
32 | static const char *system_prefix(void) | |
33 | { | |
34 | return PREFIX; | |
35 | } | |
36 | ||
37 | #endif /* RUNTIME_PREFIX */ | |
38 | ||
39 | char *system_path(const char *path) | |
40 | { | |
41 | struct strbuf d = STRBUF_INIT; | |
42 | ||
43 | if (is_absolute_path(path)) | |
44 | return xstrdup(path); | |
35fb0e86 | 45 | |
39b2f6af | 46 | strbuf_addf(&d, "%s/%s", system_prefix(), path); |
59362e56 | 47 | return strbuf_detach(&d, NULL); |
2de9de5e SP |
48 | } |
49 | ||
6854a8f5 | 50 | void git_extract_argv0_path(const char *argv0) |
e1464ca7 | 51 | { |
2cd72b0b SP |
52 | const char *slash; |
53 | ||
54 | if (!argv0 || !*argv0) | |
6854a8f5 | 55 | return; |
4dd47c3b | 56 | |
f4598233 | 57 | slash = find_last_dir_sep(argv0); |
4dd47c3b | 58 | |
6854a8f5 | 59 | if (slash) |
4dd47c3b | 60 | argv0_path = xstrndup(argv0, slash - argv0); |
e1464ca7 JS |
61 | } |
62 | ||
384df833 | 63 | void git_set_argv_exec_path(const char *exec_path) |
77cb17e9 | 64 | { |
384df833 | 65 | argv_exec_path = exec_path; |
c90d565a JS |
66 | /* |
67 | * Propagate this setting to external programs. | |
68 | */ | |
69 | setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1); | |
77cb17e9 MO |
70 | } |
71 | ||
72 | ||
73 | /* Returns the highest-priority, location to look for git programs. */ | |
962554c6 | 74 | const char *git_exec_path(void) |
77cb17e9 | 75 | { |
007ac544 | 76 | static char *cached_exec_path; |
77cb17e9 | 77 | |
384df833 SP |
78 | if (argv_exec_path) |
79 | return argv_exec_path; | |
77cb17e9 | 80 | |
007ac544 JK |
81 | if (!cached_exec_path) { |
82 | const char *env = getenv(EXEC_PATH_ENVIRONMENT); | |
83 | if (env && *env) | |
84 | cached_exec_path = xstrdup(env); | |
85 | else | |
86 | cached_exec_path = system_path(GIT_EXEC_PATH); | |
77cb17e9 | 87 | } |
007ac544 | 88 | return cached_exec_path; |
77cb17e9 MO |
89 | } |
90 | ||
511707d4 SP |
91 | static void add_path(struct strbuf *out, const char *path) |
92 | { | |
93 | if (path && *path) { | |
9610decf | 94 | strbuf_add_absolute_path(out, path); |
80ba074f | 95 | strbuf_addch(out, PATH_SEP); |
511707d4 SP |
96 | } |
97 | } | |
98 | ||
e1464ca7 | 99 | void 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()); |
511707d4 SP |
105 | |
106 | if (old_path) | |
107 | strbuf_addstr(&new_path, old_path); | |
108 | else | |
cb6a22c0 | 109 | strbuf_addstr(&new_path, _PATH_DEFPATH); |
511707d4 SP |
110 | |
111 | setenv("PATH", new_path.buf, 1); | |
112 | ||
113 | strbuf_release(&new_path); | |
114 | } | |
77cb17e9 | 115 | |
20574f55 | 116 | const char **prepare_git_cmd(struct argv_array *out, const char **argv) |
77cb17e9 | 117 | { |
20574f55 JK |
118 | argv_array_push(out, "git"); |
119 | argv_array_pushv(out, argv); | |
120 | return out->argv; | |
4933e5eb SP |
121 | } |
122 | ||
123 | int execv_git_cmd(const char **argv) { | |
20574f55 JK |
124 | struct argv_array nargv = ARGV_ARRAY_INIT; |
125 | ||
126 | prepare_git_cmd(&nargv, argv); | |
127 | trace_argv_printf(nargv.argv, "trace: exec:"); | |
575ba9d6 | 128 | |
511707d4 | 129 | /* execvp() can only ever return if it fails */ |
20574f55 | 130 | sane_execvp("git", (char **)nargv.argv); |
77cb17e9 | 131 | |
511707d4 | 132 | trace_printf("trace: exec failed: %s\n", strerror(errno)); |
575ba9d6 | 133 | |
20574f55 | 134 | argv_array_clear(&nargv); |
511707d4 | 135 | return -1; |
77cb17e9 MO |
136 | } |
137 | ||
138 | ||
9201c707 | 139 | int execl_git_cmd(const char *cmd,...) |
77cb17e9 MO |
140 | { |
141 | int argc; | |
9201c707 JH |
142 | const char *argv[MAX_ARGS + 1]; |
143 | const char *arg; | |
77cb17e9 MO |
144 | va_list param; |
145 | ||
146 | va_start(param, cmd); | |
147 | argv[0] = cmd; | |
148 | argc = 1; | |
149 | while (argc < MAX_ARGS) { | |
150 | arg = argv[argc++] = va_arg(param, char *); | |
151 | if (!arg) | |
152 | break; | |
153 | } | |
154 | va_end(param); | |
155 | if (MAX_ARGS <= argc) | |
156 | return error("too many args to run %s", cmd); | |
157 | ||
158 | argv[argc] = NULL; | |
159 | return execv_git_cmd(argv); | |
160 | } |