🌐 AI搜索 & 代理 主页
blob: 408e4e55e1c58931444c772d35d23b505bf3e2ea [file] [log] [blame]
Michal Ostrowski77cb17e2006-01-11 02:12:171#include "cache.h"
2#include "exec_cmd.h"
Matthias Lederhofer575ba9d2006-06-25 13:56:183#include "quote.h"
Michal Ostrowski77cb17e2006-01-11 02:12:174#define MAX_ARGS 32
5
6extern char **environ;
Scott R Parish384df832007-10-27 08:36:517static const char *argv_exec_path;
Johannes Sixte1464ca2008-07-21 19:19:528static const char *argv0_path;
Michal Ostrowski77cb17e2006-01-11 02:12:179
Steffen Prohaska2de9de52008-07-13 20:31:1810const char *system_path(const char *path)
11{
Steffen Prohaska35fb0e862009-01-18 12:00:1412#ifdef RUNTIME_PREFIX
13 static const char *prefix;
14#else
Steffen Prohaska026fa0d2009-01-18 12:00:0915 static const char *prefix = PREFIX;
Steffen Prohaska35fb0e862009-01-18 12:00:1416#endif
Steffen Prohaska026fa0d2009-01-18 12:00:0917 struct strbuf d = STRBUF_INIT;
18
19 if (is_absolute_path(path))
20 return path;
21
Steffen Prohaska35fb0e862009-01-18 12:00:1422#ifdef RUNTIME_PREFIX
23 assert(argv0_path);
24 assert(is_absolute_path(argv0_path));
25
Johannes Schindelin024aa7d2009-02-19 19:10:5326 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"))) {
Steffen Prohaska35fb0e862009-01-18 12:00:1430 prefix = PREFIX;
31 fprintf(stderr, "RUNTIME_PREFIX requested, "
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
34 }
35#endif
36
Steffen Prohaska026fa0d2009-01-18 12:00:0937 strbuf_addf(&d, "%s/%s", prefix, path);
38 path = strbuf_detach(&d, NULL);
Steffen Prohaska2de9de52008-07-13 20:31:1839 return path;
40}
41
Steve Haslam4dd47c32009-01-18 12:00:1042const char *git_extract_argv0_path(const char *argv0)
Johannes Sixte1464ca2008-07-21 19:19:5243{
Steffen Prohaska2cd72b02009-01-18 12:00:1144 const char *slash;
45
46 if (!argv0 || !*argv0)
47 return NULL;
48 slash = argv0 + strlen(argv0);
Steve Haslam4dd47c32009-01-18 12:00:1049
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;
Johannes Sixte1464ca2008-07-21 19:19:5259}
60
Scott R Parish384df832007-10-27 08:36:5161void git_set_argv_exec_path(const char *exec_path)
Michal Ostrowski77cb17e2006-01-11 02:12:1762{
Scott R Parish384df832007-10-27 08:36:5163 argv_exec_path = exec_path;
Johannes Sixtc90d5652009-03-21 22:21:1864 /*
65 * Propagate this setting to external programs.
66 */
67 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
Michal Ostrowski77cb17e2006-01-11 02:12:1768}
69
70
71/* Returns the highest-priority, location to look for git programs. */
Timo Hirvonen962554c2006-02-26 15:13:4672const char *git_exec_path(void)
Michal Ostrowski77cb17e2006-01-11 02:12:1773{
74 const char *env;
75
Scott R Parish384df832007-10-27 08:36:5176 if (argv_exec_path)
77 return argv_exec_path;
Michal Ostrowski77cb17e2006-01-11 02:12:1778
Junio C Hamanod4ebc362006-12-19 09:28:1579 env = getenv(EXEC_PATH_ENVIRONMENT);
Dmitry V. Levin2b601622006-05-29 00:34:3480 if (env && *env) {
Michal Ostrowski77cb17e2006-01-11 02:12:1781 return env;
82 }
83
Johannes Sixt49fa65a2008-07-23 19:12:1884 return system_path(GIT_EXEC_PATH);
Michal Ostrowski77cb17e2006-01-11 02:12:1785}
86
Scott R Parish511707d2007-10-28 11:17:2087static 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
Johannes Sixt10c4c882008-07-21 19:19:5593 strbuf_addstr(out, make_nonrelative_path(path));
Scott R Parish511707d2007-10-28 11:17:2094
Johannes Sixt80ba0742007-12-03 20:55:5795 strbuf_addch(out, PATH_SEP);
Scott R Parish511707d2007-10-28 11:17:2096 }
97}
98
Johannes Sixte1464ca2008-07-21 19:19:5299void setup_path(void)
Scott R Parish511707d2007-10-28 11:17:20100{
101 const char *old_path = getenv("PATH");
Brandon Caseyf285a2d2008-10-09 19:12:12102 struct strbuf new_path = STRBUF_INIT;
Scott R Parish511707d2007-10-28 11:17:20103
Steffen Prohaska8e346282009-01-18 12:00:13104 add_path(&new_path, git_exec_path());
Johannes Sixte1464ca2008-07-21 19:19:52105 add_path(&new_path, argv0_path);
Scott R Parish511707d2007-10-28 11:17:20106
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}
Michal Ostrowski77cb17e2006-01-11 02:12:17116
Steffen Prohaska4933e5e2008-07-28 05:50:27117const char **prepare_git_cmd(const char **argv)
Michal Ostrowski77cb17e2006-01-11 02:12:17118{
Junio C Hamano7550be02007-12-02 06:09:22119 int argc;
120 const char **nargv;
Michal Ostrowski77cb17e2006-01-11 02:12:17121
Junio C Hamano7550be02007-12-02 06:09:22122 for (argc = 0; argv[argc]; argc++)
123 ; /* just counting */
124 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
Junio C Hamano9201c702006-03-05 10:47:29125
Junio C Hamano7550be02007-12-02 06:09:22126 nargv[0] = "git";
127 for (argc = 0; argv[argc]; argc++)
128 nargv[argc + 1] = argv[argc];
129 nargv[argc + 1] = NULL;
Steffen Prohaska4933e5e2008-07-28 05:50:27130 return nargv;
131}
132
133int execv_git_cmd(const char **argv) {
134 const char **nargv = prepare_git_cmd(argv);
Junio C Hamano7550be02007-12-02 06:09:22135 trace_argv_printf(nargv, "trace: exec:");
Michal Ostrowski77cb17e2006-01-11 02:12:17136
Scott R Parish511707d2007-10-28 11:17:20137 /* execvp() can only ever return if it fails */
Junio C Hamano7550be02007-12-02 06:09:22138 execvp("git", (char **)nargv);
Dmitry V. Levind6859902006-05-30 14:58:52139
Scott R Parish511707d2007-10-28 11:17:20140 trace_printf("trace: exec failed: %s\n", strerror(errno));
Michal Ostrowski77cb17e2006-01-11 02:12:17141
Junio C Hamano7550be02007-12-02 06:09:22142 free(nargv);
Michal Ostrowski77cb17e2006-01-11 02:12:17143 return -1;
Michal Ostrowski77cb17e2006-01-11 02:12:17144}
145
146
Junio C Hamano9201c702006-03-05 10:47:29147int execl_git_cmd(const char *cmd,...)
Michal Ostrowski77cb17e2006-01-11 02:12:17148{
149 int argc;
Junio C Hamano9201c702006-03-05 10:47:29150 const char *argv[MAX_ARGS + 1];
151 const char *arg;
Michal Ostrowski77cb17e2006-01-11 02:12:17152 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}