4 #include "argv-array.h"
7 static const char *argv_exec_path
;
10 static const char *argv0_path
;
12 static const char *system_prefix(void)
14 static const char *prefix
;
17 assert(is_absolute_path(argv0_path
));
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"))) {
24 trace_printf("RUNTIME_PREFIX requested, "
25 "but prefix computation failed. "
26 "Using static fallback '%s'.\n", prefix
);
31 void git_extract_argv0_path(const char *argv0
)
35 if (!argv0
|| !*argv0
)
38 slash
= find_last_dir_sep(argv0
);
41 argv0_path
= xstrndup(argv0
, slash
- argv0
);
46 static const char *system_prefix(void)
51 void git_extract_argv0_path(const char *argv0
)
55 #endif /* RUNTIME_PREFIX */
57 char *system_path(const char *path
)
59 struct strbuf d
= STRBUF_INIT
;
61 if (is_absolute_path(path
))
64 strbuf_addf(&d
, "%s/%s", system_prefix(), path
);
65 return strbuf_detach(&d
, NULL
);
68 void git_set_argv_exec_path(const char *exec_path
)
70 argv_exec_path
= exec_path
;
72 * Propagate this setting to external programs.
74 setenv(EXEC_PATH_ENVIRONMENT
, exec_path
, 1);
78 /* Returns the highest-priority, location to look for git programs. */
79 const char *git_exec_path(void)
81 static char *cached_exec_path
;
84 return argv_exec_path
;
86 if (!cached_exec_path
) {
87 const char *env
= getenv(EXEC_PATH_ENVIRONMENT
);
89 cached_exec_path
= xstrdup(env
);
91 cached_exec_path
= system_path(GIT_EXEC_PATH
);
93 return cached_exec_path
;
96 static void add_path(struct strbuf
*out
, const char *path
)
99 strbuf_add_absolute_path(out
, path
);
100 strbuf_addch(out
, PATH_SEP
);
104 void setup_path(void)
106 const char *old_path
= getenv("PATH");
107 struct strbuf new_path
= STRBUF_INIT
;
109 add_path(&new_path
, git_exec_path());
112 strbuf_addstr(&new_path
, old_path
);
114 strbuf_addstr(&new_path
, _PATH_DEFPATH
);
116 setenv("PATH", new_path
.buf
, 1);
118 strbuf_release(&new_path
);
121 const char **prepare_git_cmd(struct argv_array
*out
, const char **argv
)
123 argv_array_push(out
, "git");
124 argv_array_pushv(out
, argv
);
128 int execv_git_cmd(const char **argv
) {
129 struct argv_array nargv
= ARGV_ARRAY_INIT
;
131 prepare_git_cmd(&nargv
, argv
);
132 trace_argv_printf(nargv
.argv
, "trace: exec:");
134 /* execvp() can only ever return if it fails */
135 sane_execvp("git", (char **)nargv
.argv
);
137 trace_printf("trace: exec failed: %s\n", strerror(errno
));
139 argv_array_clear(&nargv
);
144 int execl_git_cmd(const char *cmd
,...)
147 const char *argv
[MAX_ARGS
+ 1];
151 va_start(param
, cmd
);
154 while (argc
< MAX_ARGS
) {
155 arg
= argv
[argc
++] = va_arg(param
, char *);
160 if (MAX_ARGS
<= argc
)
161 return error("too many args to run %s", cmd
);
164 return execv_git_cmd(argv
);