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