Merge branch 'ph/mergetool'
[git/git-p4.git] / exec_cmd.c
blob0f8f4b5b7d9dea4458b9944dd81cf98c2e9a1322
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 *builtin_exec_path = GIT_EXEC_PATH;
8 static const char *argv_exec_path;
10 void git_set_argv_exec_path(const char *exec_path)
12 argv_exec_path = exec_path;
16 /* Returns the highest-priority, location to look for git programs. */
17 const char *git_exec_path(void)
19 const char *env;
21 if (argv_exec_path)
22 return argv_exec_path;
24 env = getenv(EXEC_PATH_ENVIRONMENT);
25 if (env && *env) {
26 return env;
29 return builtin_exec_path;
32 static void add_path(struct strbuf *out, const char *path)
34 if (path && *path) {
35 if (is_absolute_path(path))
36 strbuf_addstr(out, path);
37 else
38 strbuf_addstr(out, make_absolute_path(path));
40 strbuf_addch(out, ':');
44 void setup_path(const char *cmd_path)
46 const char *old_path = getenv("PATH");
47 struct strbuf new_path;
49 strbuf_init(&new_path, 0);
51 add_path(&new_path, argv_exec_path);
52 add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT));
53 add_path(&new_path, builtin_exec_path);
54 add_path(&new_path, cmd_path);
56 if (old_path)
57 strbuf_addstr(&new_path, old_path);
58 else
59 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
61 setenv("PATH", new_path.buf, 1);
63 strbuf_release(&new_path);
66 int execv_git_cmd(const char **argv)
68 int argc;
69 const char **nargv;
71 for (argc = 0; argv[argc]; argc++)
72 ; /* just counting */
73 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
75 nargv[0] = "git";
76 for (argc = 0; argv[argc]; argc++)
77 nargv[argc + 1] = argv[argc];
78 nargv[argc + 1] = NULL;
79 trace_argv_printf(nargv, "trace: exec:");
81 /* execvp() can only ever return if it fails */
82 execvp("git", (char **)nargv);
84 trace_printf("trace: exec failed: %s\n", strerror(errno));
86 free(nargv);
87 return -1;
91 int execl_git_cmd(const char *cmd,...)
93 int argc;
94 const char *argv[MAX_ARGS + 1];
95 const char *arg;
96 va_list param;
98 va_start(param, cmd);
99 argv[0] = cmd;
100 argc = 1;
101 while (argc < MAX_ARGS) {
102 arg = argv[argc++] = va_arg(param, char *);
103 if (!arg)
104 break;
106 va_end(param);
107 if (MAX_ARGS <= argc)
108 return error("too many args to run %s", cmd);
110 argv[argc] = NULL;
111 return execv_git_cmd(argv);