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; |
Scott R Parish | 384df83 | 2007-10-27 01:36:51 -0700 | [diff] [blame] | 7 | static const char *argv_exec_path; |
Johannes Sixt | e1464ca | 2008-07-21 21:19:52 +0200 | [diff] [blame] | 8 | static const char *argv0_path; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 9 | |
Steffen Prohaska | 2de9de5 | 2008-07-13 22:31:18 +0200 | [diff] [blame] | 10 | const char *system_path(const char *path) |
| 11 | { |
Steffen Prohaska | 35fb0e86 | 2009-01-18 13:00:14 +0100 | [diff] [blame] | 12 | #ifdef RUNTIME_PREFIX |
| 13 | static const char *prefix; |
| 14 | #else |
Steffen Prohaska | 026fa0d | 2009-01-18 13:00:09 +0100 | [diff] [blame] | 15 | static const char *prefix = PREFIX; |
Steffen Prohaska | 35fb0e86 | 2009-01-18 13:00:14 +0100 | [diff] [blame] | 16 | #endif |
Steffen Prohaska | 026fa0d | 2009-01-18 13:00:09 +0100 | [diff] [blame] | 17 | struct strbuf d = STRBUF_INIT; |
| 18 | |
| 19 | if (is_absolute_path(path)) |
| 20 | return path; |
| 21 | |
Steffen Prohaska | 35fb0e86 | 2009-01-18 13:00:14 +0100 | [diff] [blame] | 22 | #ifdef RUNTIME_PREFIX |
| 23 | assert(argv0_path); |
| 24 | assert(is_absolute_path(argv0_path)); |
| 25 | |
Johannes Schindelin | 024aa7d | 2009-02-19 20:10:53 +0100 | [diff] [blame] | 26 | if (!prefix && |
| 27 | !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) && |
| 28 | !(prefix = strip_path_suffix(argv0_path, BINDIR)) && |
| 29 | !(prefix = strip_path_suffix(argv0_path, "git"))) { |
Steffen Prohaska | 35fb0e86 | 2009-01-18 13:00:14 +0100 | [diff] [blame] | 30 | prefix = PREFIX; |
Johannes Sixt | aa09457 | 2010-02-23 12:42:56 +0100 | [diff] [blame] | 31 | trace_printf("RUNTIME_PREFIX requested, " |
Steffen Prohaska | 35fb0e86 | 2009-01-18 13:00:14 +0100 | [diff] [blame] | 32 | "but prefix computation failed. " |
| 33 | "Using static fallback '%s'.\n", prefix); |
| 34 | } |
| 35 | #endif |
| 36 | |
Steffen Prohaska | 026fa0d | 2009-01-18 13:00:09 +0100 | [diff] [blame] | 37 | strbuf_addf(&d, "%s/%s", prefix, path); |
| 38 | path = strbuf_detach(&d, NULL); |
Steffen Prohaska | 2de9de5 | 2008-07-13 22:31:18 +0200 | [diff] [blame] | 39 | return path; |
| 40 | } |
| 41 | |
Steve Haslam | 4dd47c3 | 2009-01-18 13:00:10 +0100 | [diff] [blame] | 42 | const char *git_extract_argv0_path(const char *argv0) |
Johannes Sixt | e1464ca | 2008-07-21 21:19:52 +0200 | [diff] [blame] | 43 | { |
Steffen Prohaska | 2cd72b0 | 2009-01-18 13:00:11 +0100 | [diff] [blame] | 44 | const char *slash; |
| 45 | |
| 46 | if (!argv0 || !*argv0) |
| 47 | return NULL; |
| 48 | slash = argv0 + strlen(argv0); |
Steve Haslam | 4dd47c3 | 2009-01-18 13:00:10 +0100 | [diff] [blame] | 49 | |
| 50 | while (argv0 <= slash && !is_dir_sep(*slash)) |
| 51 | slash--; |
| 52 | |
| 53 | if (slash >= argv0) { |
| 54 | argv0_path = xstrndup(argv0, slash - argv0); |
| 55 | return slash + 1; |
| 56 | } |
| 57 | |
| 58 | return argv0; |
Johannes Sixt | e1464ca | 2008-07-21 21:19:52 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Scott R Parish | 384df83 | 2007-10-27 01:36:51 -0700 | [diff] [blame] | 61 | void git_set_argv_exec_path(const char *exec_path) |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 62 | { |
Scott R Parish | 384df83 | 2007-10-27 01:36:51 -0700 | [diff] [blame] | 63 | argv_exec_path = exec_path; |
Johannes Sixt | c90d565 | 2009-03-21 23:21:18 +0100 | [diff] [blame] | 64 | /* |
| 65 | * Propagate this setting to external programs. |
| 66 | */ |
| 67 | setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1); |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | |
| 71 | /* Returns the highest-priority, location to look for git programs. */ |
Timo Hirvonen | 962554c | 2006-02-26 17:13:46 +0200 | [diff] [blame] | 72 | const char *git_exec_path(void) |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 73 | { |
| 74 | const char *env; |
| 75 | |
Scott R Parish | 384df83 | 2007-10-27 01:36:51 -0700 | [diff] [blame] | 76 | if (argv_exec_path) |
| 77 | return argv_exec_path; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 78 | |
Junio C Hamano | d4ebc36 | 2006-12-19 01:28:15 -0800 | [diff] [blame] | 79 | env = getenv(EXEC_PATH_ENVIRONMENT); |
Dmitry V. Levin | 2b60162 | 2006-05-29 04:34:34 +0400 | [diff] [blame] | 80 | if (env && *env) { |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 81 | return env; |
| 82 | } |
| 83 | |
Johannes Sixt | 49fa65a | 2008-07-23 21:12:18 +0200 | [diff] [blame] | 84 | return system_path(GIT_EXEC_PATH); |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Scott R Parish | 511707d | 2007-10-28 04:17:20 -0700 | [diff] [blame] | 87 | static void add_path(struct strbuf *out, const char *path) |
| 88 | { |
| 89 | if (path && *path) { |
| 90 | if (is_absolute_path(path)) |
| 91 | strbuf_addstr(out, path); |
| 92 | else |
Johannes Sixt | 10c4c88 | 2008-07-21 21:19:55 +0200 | [diff] [blame] | 93 | strbuf_addstr(out, make_nonrelative_path(path)); |
Scott R Parish | 511707d | 2007-10-28 04:17:20 -0700 | [diff] [blame] | 94 | |
Johannes Sixt | 80ba074 | 2007-12-03 21:55:57 +0100 | [diff] [blame] | 95 | strbuf_addch(out, PATH_SEP); |
Scott R Parish | 511707d | 2007-10-28 04:17:20 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Johannes Sixt | e1464ca | 2008-07-21 21:19:52 +0200 | [diff] [blame] | 99 | void setup_path(void) |
Scott R Parish | 511707d | 2007-10-28 04:17:20 -0700 | [diff] [blame] | 100 | { |
| 101 | const char *old_path = getenv("PATH"); |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 102 | struct strbuf new_path = STRBUF_INIT; |
Scott R Parish | 511707d | 2007-10-28 04:17:20 -0700 | [diff] [blame] | 103 | |
Steffen Prohaska | 8e34628 | 2009-01-18 13:00:13 +0100 | [diff] [blame] | 104 | add_path(&new_path, git_exec_path()); |
Johannes Sixt | e1464ca | 2008-07-21 21:19:52 +0200 | [diff] [blame] | 105 | add_path(&new_path, argv0_path); |
Scott R Parish | 511707d | 2007-10-28 04:17:20 -0700 | [diff] [blame] | 106 | |
| 107 | if (old_path) |
| 108 | strbuf_addstr(&new_path, old_path); |
| 109 | else |
Chris Webb | cb6a22c | 2010-04-13 10:07:13 +0100 | [diff] [blame] | 110 | strbuf_addstr(&new_path, _PATH_DEFPATH); |
Scott R Parish | 511707d | 2007-10-28 04:17:20 -0700 | [diff] [blame] | 111 | |
| 112 | setenv("PATH", new_path.buf, 1); |
| 113 | |
| 114 | strbuf_release(&new_path); |
| 115 | } |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 116 | |
Steffen Prohaska | 4933e5e | 2008-07-28 07:50:27 +0200 | [diff] [blame] | 117 | const char **prepare_git_cmd(const char **argv) |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 118 | { |
Junio C Hamano | 7550be0 | 2007-12-01 22:09:22 -0800 | [diff] [blame] | 119 | int argc; |
| 120 | const char **nargv; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 121 | |
Junio C Hamano | 7550be0 | 2007-12-01 22:09:22 -0800 | [diff] [blame] | 122 | for (argc = 0; argv[argc]; argc++) |
| 123 | ; /* just counting */ |
| 124 | nargv = xmalloc(sizeof(*nargv) * (argc + 2)); |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 125 | |
Junio C Hamano | 7550be0 | 2007-12-01 22:09:22 -0800 | [diff] [blame] | 126 | nargv[0] = "git"; |
| 127 | for (argc = 0; argv[argc]; argc++) |
| 128 | nargv[argc + 1] = argv[argc]; |
| 129 | nargv[argc + 1] = NULL; |
Steffen Prohaska | 4933e5e | 2008-07-28 07:50:27 +0200 | [diff] [blame] | 130 | return nargv; |
| 131 | } |
| 132 | |
| 133 | int execv_git_cmd(const char **argv) { |
| 134 | const char **nargv = prepare_git_cmd(argv); |
Junio C Hamano | 7550be0 | 2007-12-01 22:09:22 -0800 | [diff] [blame] | 135 | trace_argv_printf(nargv, "trace: exec:"); |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 136 | |
Scott R Parish | 511707d | 2007-10-28 04:17:20 -0700 | [diff] [blame] | 137 | /* execvp() can only ever return if it fails */ |
Junio C Hamano | 7550be0 | 2007-12-01 22:09:22 -0800 | [diff] [blame] | 138 | execvp("git", (char **)nargv); |
Dmitry V. Levin | d685990 | 2006-05-30 18:58:52 +0400 | [diff] [blame] | 139 | |
Scott R Parish | 511707d | 2007-10-28 04:17:20 -0700 | [diff] [blame] | 140 | trace_printf("trace: exec failed: %s\n", strerror(errno)); |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 141 | |
Junio C Hamano | 7550be0 | 2007-12-01 22:09:22 -0800 | [diff] [blame] | 142 | free(nargv); |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 143 | return -1; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 147 | int execl_git_cmd(const char *cmd,...) |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 148 | { |
| 149 | int argc; |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 150 | const char *argv[MAX_ARGS + 1]; |
| 151 | const char *arg; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 152 | va_list param; |
| 153 | |
| 154 | va_start(param, cmd); |
| 155 | argv[0] = cmd; |
| 156 | argc = 1; |
| 157 | while (argc < MAX_ARGS) { |
| 158 | arg = argv[argc++] = va_arg(param, char *); |
| 159 | if (!arg) |
| 160 | break; |
| 161 | } |
| 162 | va_end(param); |
| 163 | if (MAX_ARGS <= argc) |
| 164 | return error("too many args to run %s", cmd); |
| 165 | |
| 166 | argv[argc] = NULL; |
| 167 | return execv_git_cmd(argv); |
| 168 | } |