builtin/merge-tree: convert remaining caller of get_sha1 to object_id
[alt-git.git] / exec_cmd.c
blobfb94aeba9cef2893ea7173e5634640e596659474
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
4 #include "argv-array.h"
5 #define MAX_ARGS 32
7 static const char *argv_exec_path;
8 static const char *argv0_path;
10 char *system_path(const char *path)
12 #ifdef RUNTIME_PREFIX
13 static const char *prefix;
14 #else
15 static const char *prefix = PREFIX;
16 #endif
17 struct strbuf d = STRBUF_INIT;
19 if (is_absolute_path(path))
20 return xstrdup(path);
22 #ifdef RUNTIME_PREFIX
23 assert(argv0_path);
24 assert(is_absolute_path(argv0_path));
26 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"))) {
30 prefix = PREFIX;
31 trace_printf("RUNTIME_PREFIX requested, "
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix);
35 #endif
37 strbuf_addf(&d, "%s/%s", prefix, path);
38 return strbuf_detach(&d, NULL);
41 void git_extract_argv0_path(const char *argv0)
43 const char *slash;
45 if (!argv0 || !*argv0)
46 return;
48 slash = find_last_dir_sep(argv0);
50 if (slash)
51 argv0_path = xstrndup(argv0, slash - argv0);
54 void git_set_argv_exec_path(const char *exec_path)
56 argv_exec_path = exec_path;
58 * Propagate this setting to external programs.
60 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
64 /* Returns the highest-priority, location to look for git programs. */
65 const char *git_exec_path(void)
67 static char *cached_exec_path;
69 if (argv_exec_path)
70 return argv_exec_path;
72 if (!cached_exec_path) {
73 const char *env = getenv(EXEC_PATH_ENVIRONMENT);
74 if (env && *env)
75 cached_exec_path = xstrdup(env);
76 else
77 cached_exec_path = system_path(GIT_EXEC_PATH);
79 return cached_exec_path;
82 static void add_path(struct strbuf *out, const char *path)
84 if (path && *path) {
85 strbuf_add_absolute_path(out, path);
86 strbuf_addch(out, PATH_SEP);
90 void setup_path(void)
92 const char *old_path = getenv("PATH");
93 struct strbuf new_path = STRBUF_INIT;
95 add_path(&new_path, git_exec_path());
97 if (old_path)
98 strbuf_addstr(&new_path, old_path);
99 else
100 strbuf_addstr(&new_path, _PATH_DEFPATH);
102 setenv("PATH", new_path.buf, 1);
104 strbuf_release(&new_path);
107 const char **prepare_git_cmd(struct argv_array *out, const char **argv)
109 argv_array_push(out, "git");
110 argv_array_pushv(out, argv);
111 return out->argv;
114 int execv_git_cmd(const char **argv) {
115 struct argv_array nargv = ARGV_ARRAY_INIT;
117 prepare_git_cmd(&nargv, argv);
118 trace_argv_printf(nargv.argv, "trace: exec:");
120 /* execvp() can only ever return if it fails */
121 sane_execvp("git", (char **)nargv.argv);
123 trace_printf("trace: exec failed: %s\n", strerror(errno));
125 argv_array_clear(&nargv);
126 return -1;
130 int execl_git_cmd(const char *cmd,...)
132 int argc;
133 const char *argv[MAX_ARGS + 1];
134 const char *arg;
135 va_list param;
137 va_start(param, cmd);
138 argv[0] = cmd;
139 argc = 1;
140 while (argc < MAX_ARGS) {
141 arg = argv[argc++] = va_arg(param, char *);
142 if (!arg)
143 break;
145 va_end(param);
146 if (MAX_ARGS <= argc)
147 return error("too many args to run %s", cmd);
149 argv[argc] = NULL;
150 return execv_git_cmd(argv);