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