Update draft release notes for 1.6.0.2
[git/git-svn.git] / exec_cmd.c
blobce6741eb682b59ad638c7bee6ca31e2fcd53f281
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
4 #define MAX_ARGS 32
6 extern char **environ;
7 static const char *argv_exec_path;
8 static const char *argv0_path;
10 const char *system_path(const char *path)
12 if (!is_absolute_path(path) && argv0_path) {
13 struct strbuf d = STRBUF_INIT;
14 strbuf_addf(&d, "%s/%s", argv0_path, path);
15 path = strbuf_detach(&d, NULL);
17 return path;
20 void git_set_argv0_path(const char *path)
22 argv0_path = path;
25 void git_set_argv_exec_path(const char *exec_path)
27 argv_exec_path = exec_path;
31 /* Returns the highest-priority, location to look for git programs. */
32 const char *git_exec_path(void)
34 const char *env;
36 if (argv_exec_path)
37 return argv_exec_path;
39 env = getenv(EXEC_PATH_ENVIRONMENT);
40 if (env && *env) {
41 return env;
44 return system_path(GIT_EXEC_PATH);
47 static void add_path(struct strbuf *out, const char *path)
49 if (path && *path) {
50 if (is_absolute_path(path))
51 strbuf_addstr(out, path);
52 else
53 strbuf_addstr(out, make_nonrelative_path(path));
55 strbuf_addch(out, PATH_SEP);
59 void setup_path(void)
61 const char *old_path = getenv("PATH");
62 struct strbuf new_path;
64 strbuf_init(&new_path, 0);
66 add_path(&new_path, argv_exec_path);
67 add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT));
68 add_path(&new_path, system_path(GIT_EXEC_PATH));
69 add_path(&new_path, argv0_path);
71 if (old_path)
72 strbuf_addstr(&new_path, old_path);
73 else
74 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
76 setenv("PATH", new_path.buf, 1);
78 strbuf_release(&new_path);
81 const char **prepare_git_cmd(const char **argv)
83 int argc;
84 const char **nargv;
86 for (argc = 0; argv[argc]; argc++)
87 ; /* just counting */
88 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
90 nargv[0] = "git";
91 for (argc = 0; argv[argc]; argc++)
92 nargv[argc + 1] = argv[argc];
93 nargv[argc + 1] = NULL;
94 return nargv;
97 int execv_git_cmd(const char **argv) {
98 const char **nargv = prepare_git_cmd(argv);
99 trace_argv_printf(nargv, "trace: exec:");
101 /* execvp() can only ever return if it fails */
102 execvp("git", (char **)nargv);
104 trace_printf("trace: exec failed: %s\n", strerror(errno));
106 free(nargv);
107 return -1;
111 int execl_git_cmd(const char *cmd,...)
113 int argc;
114 const char *argv[MAX_ARGS + 1];
115 const char *arg;
116 va_list param;
118 va_start(param, cmd);
119 argv[0] = cmd;
120 argc = 1;
121 while (argc < MAX_ARGS) {
122 arg = argv[argc++] = va_arg(param, char *);
123 if (!arg)
124 break;
126 va_end(param);
127 if (MAX_ARGS <= argc)
128 return error("too many args to run %s", cmd);
130 argv[argc] = NULL;
131 return execv_git_cmd(argv);