]>
Commit | Line | Data |
---|---|---|
77cb17e9 MO |
1 | #include "cache.h" |
2 | #include "exec_cmd.h" | |
575ba9d6 | 3 | #include "quote.h" |
77cb17e9 MO |
4 | #define MAX_ARGS 32 |
5 | ||
6 | extern char **environ; | |
7 | static const char *builtin_exec_path = GIT_EXEC_PATH; | |
96f1e58f | 8 | static const char *current_exec_path; |
77cb17e9 MO |
9 | |
10 | void git_set_exec_path(const char *exec_path) | |
11 | { | |
12 | current_exec_path = exec_path; | |
13 | } | |
14 | ||
15 | ||
16 | /* Returns the highest-priority, location to look for git programs. */ | |
962554c6 | 17 | const char *git_exec_path(void) |
77cb17e9 MO |
18 | { |
19 | const char *env; | |
20 | ||
21 | if (current_exec_path) | |
22 | return current_exec_path; | |
23 | ||
d4ebc36c | 24 | env = getenv(EXEC_PATH_ENVIRONMENT); |
2b601626 | 25 | if (env && *env) { |
77cb17e9 MO |
26 | return env; |
27 | } | |
28 | ||
29 | return builtin_exec_path; | |
30 | } | |
31 | ||
32 | ||
9201c707 | 33 | int execv_git_cmd(const char **argv) |
77cb17e9 MO |
34 | { |
35 | char git_command[PATH_MAX + 1]; | |
d6859901 | 36 | int i; |
77cb17e9 | 37 | const char *paths[] = { current_exec_path, |
d4ebc36c | 38 | getenv(EXEC_PATH_ENVIRONMENT), |
77cb17e9 MO |
39 | builtin_exec_path }; |
40 | ||
b4f2a6ac | 41 | for (i = 0; i < ARRAY_SIZE(paths); ++i) { |
d6859901 DL |
42 | size_t len; |
43 | int rc; | |
77cb17e9 | 44 | const char *exec_dir = paths[i]; |
9201c707 JH |
45 | const char *tmp; |
46 | ||
2b601626 | 47 | if (!exec_dir || !*exec_dir) continue; |
77cb17e9 MO |
48 | |
49 | if (*exec_dir != '/') { | |
50 | if (!getcwd(git_command, sizeof(git_command))) { | |
51 | fprintf(stderr, "git: cannot determine " | |
d6859901 DL |
52 | "current directory: %s\n", |
53 | strerror(errno)); | |
54 | break; | |
77cb17e9 MO |
55 | } |
56 | len = strlen(git_command); | |
57 | ||
58 | /* Trivial cleanup */ | |
59 | while (!strncmp(exec_dir, "./", 2)) { | |
60 | exec_dir += 2; | |
61 | while (*exec_dir == '/') | |
62 | exec_dir++; | |
63 | } | |
d6859901 DL |
64 | |
65 | rc = snprintf(git_command + len, | |
66 | sizeof(git_command) - len, "/%s", | |
67 | exec_dir); | |
68 | if (rc < 0 || rc >= sizeof(git_command) - len) { | |
69 | fprintf(stderr, "git: command name given " | |
70 | "is too long.\n"); | |
71 | break; | |
72 | } | |
77cb17e9 | 73 | } else { |
d6859901 DL |
74 | if (strlen(exec_dir) + 1 > sizeof(git_command)) { |
75 | fprintf(stderr, "git: command name given " | |
76 | "is too long.\n"); | |
77 | break; | |
78 | } | |
77cb17e9 MO |
79 | strcpy(git_command, exec_dir); |
80 | } | |
81 | ||
82 | len = strlen(git_command); | |
d6859901 DL |
83 | rc = snprintf(git_command + len, sizeof(git_command) - len, |
84 | "/git-%s", argv[0]); | |
85 | if (rc < 0 || rc >= sizeof(git_command) - len) { | |
77cb17e9 MO |
86 | fprintf(stderr, |
87 | "git: command name given is too long.\n"); | |
88 | break; | |
89 | } | |
90 | ||
91 | /* argv[0] must be the git command, but the argv array | |
92 | * belongs to the caller, and my be reused in | |
93 | * subsequent loop iterations. Save argv[0] and | |
94 | * restore it on error. | |
95 | */ | |
96 | ||
97 | tmp = argv[0]; | |
98 | argv[0] = git_command; | |
99 | ||
7cf67205 | 100 | trace_argv_printf(argv, -1, "trace: exec:"); |
575ba9d6 | 101 | |
77cb17e9 | 102 | /* execve() can only ever return if it fails */ |
9201c707 | 103 | execve(git_command, (char **)argv, environ); |
77cb17e9 | 104 | |
7cf67205 | 105 | trace_printf("trace: exec failed: %s\n", strerror(errno)); |
575ba9d6 | 106 | |
77cb17e9 MO |
107 | argv[0] = tmp; |
108 | } | |
109 | return -1; | |
110 | ||
111 | } | |
112 | ||
113 | ||
9201c707 | 114 | int execl_git_cmd(const char *cmd,...) |
77cb17e9 MO |
115 | { |
116 | int argc; | |
9201c707 JH |
117 | const char *argv[MAX_ARGS + 1]; |
118 | const char *arg; | |
77cb17e9 MO |
119 | va_list param; |
120 | ||
121 | va_start(param, cmd); | |
122 | argv[0] = cmd; | |
123 | argc = 1; | |
124 | while (argc < MAX_ARGS) { | |
125 | arg = argv[argc++] = va_arg(param, char *); | |
126 | if (!arg) | |
127 | break; | |
128 | } | |
129 | va_end(param); | |
130 | if (MAX_ARGS <= argc) | |
131 | return error("too many args to run %s", cmd); | |
132 | ||
133 | argv[argc] = NULL; | |
134 | return execv_git_cmd(argv); | |
135 | } |