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