6 static const char *builtin_exec_path = GIT_EXEC_PATH;
7 static const char *current_exec_path = NULL;
9 void git_set_exec_path(const char *exec_path)
11 current_exec_path = exec_path;
15 /* Returns the highest-priority, location to look for git programs. */
16 const char *git_exec_path(void)
20 if (current_exec_path)
21 return current_exec_path;
23 env = getenv("GIT_EXEC_PATH");
28 return builtin_exec_path;
32 int execv_git_cmd(const char **argv)
34 char git_command[PATH_MAX + 1];
36 const char *paths[] = { current_exec_path,
37 getenv("GIT_EXEC_PATH"),
40 for (i = 0; i < ARRAY_SIZE(paths); ++i) {
43 const char *exec_dir = paths[i];
46 if (!exec_dir || !*exec_dir) continue;
48 if (*exec_dir != '/') {
49 if (!getcwd(git_command, sizeof(git_command))) {
50 fprintf(stderr, "git: cannot determine "
51 "current directory: %s\n",
55 len = strlen(git_command);
58 while (!strncmp(exec_dir, "./", 2)) {
60 while (*exec_dir == '/')
64 rc = snprintf(git_command + len,
65 sizeof(git_command) - len, "/%s",
67 if (rc < 0 || rc >= sizeof(git_command) - len) {
68 fprintf(stderr, "git: command name given "
73 if (strlen(exec_dir) + 1 > sizeof(git_command)) {
74 fprintf(stderr, "git: command name given "
78 strcpy(git_command, exec_dir);
81 len = strlen(git_command);
82 rc = snprintf(git_command + len, sizeof(git_command) - len,
84 if (rc < 0 || rc >= sizeof(git_command) - len) {
86 "git: command name given is too long.\n");
90 /* argv[0] must be the git command, but the argv array
91 * belongs to the caller, and my be reused in
92 * subsequent loop iterations. Save argv[0] and
93 * restore it on error.
97 argv[0] = git_command;
99 /* execve() can only ever return if it fails */
100 execve(git_command, (char **)argv, environ);
109 int execl_git_cmd(const char *cmd,...)
112 const char *argv[MAX_ARGS + 1];
116 va_start(param, cmd);
119 while (argc < MAX_ARGS) {
120 arg = argv[argc++] = va_arg(param, char *);
125 if (MAX_ARGS <= argc)
126 return error("too many args to run %s", cmd);
129 return execv_git_cmd(argv);