pack-objects: move in_pack_pos out of struct object_entry
[alt-git.git] / exec_cmd.c
blobce192a2d64c846e9d7ccffe10c9476f9c5abb8b8
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;
9 #ifdef RUNTIME_PREFIX
10 static const char *argv0_path;
12 static const char *system_prefix(void)
14 static const char *prefix;
16 assert(argv0_path);
17 assert(is_absolute_path(argv0_path));
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"))) {
23 prefix = PREFIX;
24 trace_printf("RUNTIME_PREFIX requested, "
25 "but prefix computation failed. "
26 "Using static fallback '%s'.\n", prefix);
28 return prefix;
31 void git_extract_argv0_path(const char *argv0)
33 const char *slash;
35 if (!argv0 || !*argv0)
36 return;
38 slash = find_last_dir_sep(argv0);
40 if (slash)
41 argv0_path = xstrndup(argv0, slash - argv0);
44 #else
46 static const char *system_prefix(void)
48 return PREFIX;
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))
62 return xstrdup(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;
83 if (argv_exec_path)
84 return argv_exec_path;
86 if (!cached_exec_path) {
87 const char *env = getenv(EXEC_PATH_ENVIRONMENT);
88 if (env && *env)
89 cached_exec_path = xstrdup(env);
90 else
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)
98 if (path && *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());
111 if (old_path)
112 strbuf_addstr(&new_path, old_path);
113 else
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);
125 return 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);
140 return -1;
144 int execl_git_cmd(const char *cmd,...)
146 int argc;
147 const char *argv[MAX_ARGS + 1];
148 const char *arg;
149 va_list param;
151 va_start(param, cmd);
152 argv[0] = cmd;
153 argc = 1;
154 while (argc < MAX_ARGS) {
155 arg = argv[argc++] = va_arg(param, char *);
156 if (!arg)
157 break;
159 va_end(param);
160 if (MAX_ARGS <= argc)
161 return error("too many args to run %s", cmd);
163 argv[argc] = NULL;
164 return execv_git_cmd(argv);