Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "exec_cmd.h" |
Matthias Lederhofer | 575ba9d | 2006-06-25 15:56:18 +0200 | [diff] [blame] | 3 | #include "quote.h" |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 4 | #define MAX_ARGS 32 |
| 5 | |
| 6 | extern char **environ; |
| 7 | static const char *builtin_exec_path = GIT_EXEC_PATH; |
| 8 | static const char *current_exec_path = NULL; |
| 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. */ |
Timo Hirvonen | 962554c | 2006-02-26 17:13:46 +0200 | [diff] [blame] | 17 | const char *git_exec_path(void) |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 18 | { |
| 19 | const char *env; |
| 20 | |
| 21 | if (current_exec_path) |
| 22 | return current_exec_path; |
| 23 | |
| 24 | env = getenv("GIT_EXEC_PATH"); |
Dmitry V. Levin | 2b60162 | 2006-05-29 04:34:34 +0400 | [diff] [blame] | 25 | if (env && *env) { |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 26 | return env; |
| 27 | } |
| 28 | |
| 29 | return builtin_exec_path; |
| 30 | } |
| 31 | |
| 32 | |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 33 | int execv_git_cmd(const char **argv) |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 34 | { |
| 35 | char git_command[PATH_MAX + 1]; |
Dmitry V. Levin | d685990 | 2006-05-30 18:58:52 +0400 | [diff] [blame] | 36 | int i; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 37 | const char *paths[] = { current_exec_path, |
| 38 | getenv("GIT_EXEC_PATH"), |
| 39 | builtin_exec_path }; |
| 40 | |
Junio C Hamano | b4f2a6a | 2006-03-09 11:58:05 -0800 | [diff] [blame] | 41 | for (i = 0; i < ARRAY_SIZE(paths); ++i) { |
Dmitry V. Levin | d685990 | 2006-05-30 18:58:52 +0400 | [diff] [blame] | 42 | size_t len; |
| 43 | int rc; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 44 | const char *exec_dir = paths[i]; |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 45 | const char *tmp; |
| 46 | |
Dmitry V. Levin | 2b60162 | 2006-05-29 04:34:34 +0400 | [diff] [blame] | 47 | if (!exec_dir || !*exec_dir) continue; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 48 | |
| 49 | if (*exec_dir != '/') { |
| 50 | if (!getcwd(git_command, sizeof(git_command))) { |
| 51 | fprintf(stderr, "git: cannot determine " |
Dmitry V. Levin | d685990 | 2006-05-30 18:58:52 +0400 | [diff] [blame] | 52 | "current directory: %s\n", |
| 53 | strerror(errno)); |
| 54 | break; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 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 | } |
Dmitry V. Levin | d685990 | 2006-05-30 18:58:52 +0400 | [diff] [blame] | 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 | } |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 73 | } else { |
Dmitry V. Levin | d685990 | 2006-05-30 18:58:52 +0400 | [diff] [blame] | 74 | if (strlen(exec_dir) + 1 > sizeof(git_command)) { |
| 75 | fprintf(stderr, "git: command name given " |
| 76 | "is too long.\n"); |
| 77 | break; |
| 78 | } |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 79 | strcpy(git_command, exec_dir); |
| 80 | } |
| 81 | |
| 82 | len = strlen(git_command); |
Dmitry V. Levin | d685990 | 2006-05-30 18:58:52 +0400 | [diff] [blame] | 83 | rc = snprintf(git_command + len, sizeof(git_command) - len, |
| 84 | "/git-%s", argv[0]); |
| 85 | if (rc < 0 || rc >= sizeof(git_command) - len) { |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 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 | |
Matthias Lederhofer | 575ba9d | 2006-06-25 15:56:18 +0200 | [diff] [blame] | 100 | if (getenv("GIT_TRACE")) { |
Matthias Lederhofer | 575ba9d | 2006-06-25 15:56:18 +0200 | [diff] [blame] | 101 | const char **p = argv; |
Timo Hirvonen | e82e058 | 2006-06-28 12:15:00 +0300 | [diff] [blame] | 102 | fputs("trace: exec:", stderr); |
Matthias Lederhofer | 575ba9d | 2006-06-25 15:56:18 +0200 | [diff] [blame] | 103 | while (*p) { |
| 104 | fputc(' ', stderr); |
| 105 | sq_quote_print(stderr, *p); |
| 106 | ++p; |
| 107 | } |
| 108 | putc('\n', stderr); |
| 109 | fflush(stderr); |
| 110 | } |
| 111 | |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 112 | /* execve() can only ever return if it fails */ |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 113 | execve(git_command, (char **)argv, environ); |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 114 | |
Matthias Lederhofer | 575ba9d | 2006-06-25 15:56:18 +0200 | [diff] [blame] | 115 | if (getenv("GIT_TRACE")) { |
| 116 | fprintf(stderr, "trace: exec failed: %s\n", |
| 117 | strerror(errno)); |
| 118 | fflush(stderr); |
| 119 | } |
| 120 | |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 121 | argv[0] = tmp; |
| 122 | } |
| 123 | return -1; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 128 | int execl_git_cmd(const char *cmd,...) |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 129 | { |
| 130 | int argc; |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 131 | const char *argv[MAX_ARGS + 1]; |
| 132 | const char *arg; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 133 | va_list param; |
| 134 | |
| 135 | va_start(param, cmd); |
| 136 | argv[0] = cmd; |
| 137 | argc = 1; |
| 138 | while (argc < MAX_ARGS) { |
| 139 | arg = argv[argc++] = va_arg(param, char *); |
| 140 | if (!arg) |
| 141 | break; |
| 142 | } |
| 143 | va_end(param); |
| 144 | if (MAX_ARGS <= argc) |
| 145 | return error("too many args to run %s", cmd); |
| 146 | |
| 147 | argv[argc] = NULL; |
| 148 | return execv_git_cmd(argv); |
| 149 | } |