From e1464ca7bb0d705210ba7198f004b2fb2b807e12 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 21 Jul 2008 21:19:52 +0200 Subject: Record the command invocation path early We will need the command invocation path in system_path(). This path was passed to setup_path(), but system_path() can be called earlier, for example via: main commit_pager_choice setup_pager git_config git_etc_gitconfig system_path Therefore, we introduce git_set_argv0_path() and call it as soon as possible. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index 8899e31b3b..dedb01da6f 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -5,6 +5,7 @@ extern char **environ; static const char *argv_exec_path; +static const char *argv0_path; static const char *builtin_exec_path(void) { @@ -50,6 +51,11 @@ const char *system_path(const char *path) return path; } +void git_set_argv0_path(const char *path) +{ + argv0_path = path; +} + void git_set_argv_exec_path(const char *exec_path) { argv_exec_path = exec_path; @@ -84,7 +90,7 @@ static void add_path(struct strbuf *out, const char *path) } } -void setup_path(const char *cmd_path) +void setup_path(void) { const char *old_path = getenv("PATH"); struct strbuf new_path; @@ -94,7 +100,7 @@ void setup_path(const char *cmd_path) add_path(&new_path, argv_exec_path); add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT)); add_path(&new_path, builtin_exec_path()); - add_path(&new_path, cmd_path); + add_path(&new_path, argv0_path); if (old_path) strbuf_addstr(&new_path, old_path); -- cgit v1.2.1 From 966c6edd318f2e44dd150103ec2b6b7a53be58f0 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 21 Jul 2008 21:19:53 +0200 Subject: Fix relative built-in paths to be relative to the command invocation $(gitexecdir) (as defined in the Makefile) has gained another path component, but the relative paths in the MINGW section of the Makefile, which are interpreted relative to it, do not account for it. Instead of adding another ../ in front of the path, we change the code that constructs the absolute paths to do it relative to the command's directory, which is essentially $(bindir). We do it this way because we will also allow a relative $(gitexecdir) later. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index dedb01da6f..45f92eb164 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -43,9 +43,9 @@ static const char *builtin_exec_path(void) const char *system_path(const char *path) { - if (!is_absolute_path(path)) { + if (!is_absolute_path(path) && argv0_path) { struct strbuf d = STRBUF_INIT; - strbuf_addf(&d, "%s/%s", git_exec_path(), path); + strbuf_addf(&d, "%s/%s", argv0_path, path); path = strbuf_detach(&d, NULL); } return path; -- cgit v1.2.1 From 49fa65a7a8185e81c1098815df607042103b0493 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Wed, 23 Jul 2008 21:12:18 +0200 Subject: Allow the built-in exec path to be relative to the command invocation path If GIT_EXEC_PATH (the macro that is defined in the Makefile) is relative, it is interpreted relative to the command's invocation path, which usually is $(bindir). The Makefile rules were written with the assumption that $(gitexecdir) is an absolute path. We introduce a separate variable that names the (absolute) installation directory. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 38 ++------------------------------------ 1 file changed, 2 insertions(+), 36 deletions(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index 45f92eb164..c23603452e 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -7,40 +7,6 @@ extern char **environ; static const char *argv_exec_path; static const char *argv0_path; -static const char *builtin_exec_path(void) -{ -#ifndef __MINGW32__ - return GIT_EXEC_PATH; -#else - int len; - char *p, *q, *sl; - static char *ep; - if (ep) - return ep; - - len = strlen(_pgmptr); - if (len < 2) - return ep = "."; - - p = ep = xmalloc(len+1); - q = _pgmptr; - sl = NULL; - /* copy program name, turn '\\' into '/', skip last part */ - while ((*p = *q)) { - if (*q == '\\' || *q == '/') { - *p = '/'; - sl = p; - } - p++, q++; - } - if (sl) - *sl = '\0'; - else - ep[0] = '.', ep[1] = '\0'; - return ep; -#endif -} - const char *system_path(const char *path) { if (!is_absolute_path(path) && argv0_path) { @@ -75,7 +41,7 @@ const char *git_exec_path(void) return env; } - return builtin_exec_path(); + return system_path(GIT_EXEC_PATH); } static void add_path(struct strbuf *out, const char *path) @@ -99,7 +65,7 @@ void setup_path(void) add_path(&new_path, argv_exec_path); add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT)); - add_path(&new_path, builtin_exec_path()); + add_path(&new_path, system_path(GIT_EXEC_PATH)); add_path(&new_path, argv0_path); if (old_path) -- cgit v1.2.1 From 10c4c881c4d2cb0ece0508e7142e189e68445257 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Mon, 21 Jul 2008 21:19:55 +0200 Subject: Allow add_path() to add non-existent directories to the path This function had used make_absolute_path(); but this function dies if the directory that contains the entry whose relative path was supplied in the argument does not exist. This is a problem if the argument is, for example, "../libexec/git-core", and that "../libexec" does not exist. Since the resolution of symbolic links is not required for elements in PATH, we can fall back to using make_nonrelative_path(), which simply prepends $PWD to the path. We have to move make_nonrelative_path() alongside make_absolute_path() in abspath.c so that git-shell can be linked. See 5b8e6f85f. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index c23603452e..0ed768ddc0 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -50,7 +50,7 @@ static void add_path(struct strbuf *out, const char *path) if (is_absolute_path(path)) strbuf_addstr(out, path); else - strbuf_addstr(out, make_absolute_path(path)); + strbuf_addstr(out, make_nonrelative_path(path)); strbuf_addch(out, PATH_SEP); } -- cgit v1.2.1 From 4933e5ebdefdb88841275132ef358e6649963751 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Mon, 28 Jul 2008 07:50:27 +0200 Subject: Refactor, adding prepare_git_cmd(const char **argv) prepare_git_cmd(const char **argv) adds a first entry "git" to the array argv. The new array is allocated on the heap. It's the caller's responsibility to release it with free(). The code was already present in execv_git_cmd() but could not be used from outside. Now it can also be called for preparing the command list in the MinGW codepath in run-command.c. Signed-off-by: Steffen Prohaska Signed-off-by: Junio C Hamano --- exec_cmd.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index 0ed768ddc0..ce6741eb68 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -78,7 +78,7 @@ void setup_path(void) strbuf_release(&new_path); } -int execv_git_cmd(const char **argv) +const char **prepare_git_cmd(const char **argv) { int argc; const char **nargv; @@ -91,6 +91,11 @@ int execv_git_cmd(const char **argv) for (argc = 0; argv[argc]; argc++) nargv[argc + 1] = argv[argc]; nargv[argc + 1] = NULL; + return nargv; +} + +int execv_git_cmd(const char **argv) { + const char **nargv = prepare_git_cmd(argv); trace_argv_printf(nargv, "trace: exec:"); /* execvp() can only ever return if it fails */ -- cgit v1.2.1 From f285a2d7ed6548666989406de8f0e7233eb84368 Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Thu, 9 Oct 2008 14:12:12 -0500 Subject: Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer Many call sites use strbuf_init(&foo, 0) to initialize local strbuf variable "foo" which has not been accessed since its declaration. These can be replaced with a static initialization using the STRBUF_INIT macro which is just as readable, saves a function call, and takes up fewer lines. Signed-off-by: Brandon Casey Signed-off-by: Shawn O. Pearce --- exec_cmd.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index ce6741eb68..cdd35f9195 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -59,9 +59,7 @@ static void add_path(struct strbuf *out, const char *path) void setup_path(void) { const char *old_path = getenv("PATH"); - struct strbuf new_path; - - strbuf_init(&new_path, 0); + struct strbuf new_path = STRBUF_INIT; add_path(&new_path, argv_exec_path); add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT)); -- cgit v1.2.1 From 026fa0d5ad9538ca76838070861531c037d7b9ba Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 18 Jan 2009 13:00:09 +0100 Subject: Move computation of absolute paths from Makefile to runtime (in preparation for RUNTIME_PREFIX) This commit prepares the Makefile for relocatable binaries (called RUNTIME_PREFIX). Such binaries will be able to be moved together with the system configuration files to a different directory, requiring to compute the prefix at runtime. In a first step, we make all paths relative in the Makefile and teach system_path() to add the prefix instead. We used to compute absolute paths in the Makefile and passed them to C as defines. We now pass relative paths to C and call system_path() to add the prefix at runtime. Signed-off-by: Steffen Prohaska Acked-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index cdd35f9195..114d6389e4 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -9,11 +9,14 @@ static const char *argv0_path; const char *system_path(const char *path) { - if (!is_absolute_path(path) && argv0_path) { - struct strbuf d = STRBUF_INIT; - strbuf_addf(&d, "%s/%s", argv0_path, path); - path = strbuf_detach(&d, NULL); - } + static const char *prefix = PREFIX; + struct strbuf d = STRBUF_INIT; + + if (is_absolute_path(path)) + return path; + + strbuf_addf(&d, "%s/%s", prefix, path); + path = strbuf_detach(&d, NULL); return path; } -- cgit v1.2.1 From 4dd47c3b867f51211d0dc4474dab0fee5ca614da Mon Sep 17 00:00:00 2001 From: Steve Haslam Date: Sun, 18 Jan 2009 13:00:10 +0100 Subject: Refactor git_set_argv0_path() to git_extract_argv0_path() This commit moves the code that computes the dirname of argv[0] from git.c's main() to git_set_argv0_path() and renames the function to git_extract_argv0_path(). This makes the code in git.c's main less cluttered, and we can use the dirname computation from other main() functions too. [ spr: - split Steve's original commit and wrote new commit message. - Integrated Johannes Schindelin's cca1704897e7fdb182f68d4c48a437c5d7bc5203 while rebasing onto master. ] Signed-off-by: Steve Haslam Signed-off-by: Steffen Prohaska Acked-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index 114d6389e4..e508f1e66d 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -20,9 +20,19 @@ const char *system_path(const char *path) return path; } -void git_set_argv0_path(const char *path) +const char *git_extract_argv0_path(const char *argv0) { - argv0_path = path; + const char *slash = argv0 + strlen(argv0); + + while (argv0 <= slash && !is_dir_sep(*slash)) + slash--; + + if (slash >= argv0) { + argv0_path = xstrndup(argv0, slash - argv0); + return slash + 1; + } + + return argv0; } void git_set_argv_exec_path(const char *exec_path) -- cgit v1.2.1 From 2cd72b0b290e40fb4d6a925ce26603503f01aa09 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 18 Jan 2009 13:00:11 +0100 Subject: git_extract_argv0_path(): Move check for valid argv0 from caller to callee This simplifies the calling code. Signed-off-by: Steffen Prohaska Acked-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index e508f1e66d..f8f416b646 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -22,7 +22,11 @@ const char *system_path(const char *path) const char *git_extract_argv0_path(const char *argv0) { - const char *slash = argv0 + strlen(argv0); + const char *slash; + + if (!argv0 || !*argv0) + return NULL; + slash = argv0 + strlen(argv0); while (argv0 <= slash && !is_dir_sep(*slash)) slash--; -- cgit v1.2.1 From 8e3462837b0ace04357503a3f58802cc2231df29 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 18 Jan 2009 13:00:13 +0100 Subject: Modify setup_path() to only add git_exec_path() to PATH Searching git programs only in the highest priority location is sufficient. It does not make sense that some of the required programs are located at the highest priority location but other programs are picked up from a lower priority exec-path. If exec-path is overridden a complete set of commands should be provided, otherwise several different versions could get mixed, which is likely to cause confusion. If a user explicitly overrides the default location (by --exec-path or GIT_EXEC_PATH), we now expect that all the required programs are found there. Instead of adding the directories "argv_exec_path", "getenv(EXEC_PATH_ENVIRONMENT)", and "system_path(GIT_EXEC_PATH)" to PATH, we now rely on git_exec_path(), which implements the same order, but only returns the highest priority location to search for executables. Accessing only the location with highest priority is also required for testing executables built with RUNTIME_PREFIX. The call to system_path() should be avoided if RUNTIME_PREFIX is set and the executable is not installed at its final destination. Because we test before installing, we want to avoid calling system_path() during tests. The modifications in this commit avoid calling system_path(GIT_EXEC_PATH) if a higher-priority location is provided, which is the case when running the tests. Signed-off-by: Steffen Prohaska Acked-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index f8f416b646..23f4873b2b 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -78,9 +78,7 @@ void setup_path(void) const char *old_path = getenv("PATH"); struct strbuf new_path = STRBUF_INIT; - add_path(&new_path, argv_exec_path); - add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT)); - add_path(&new_path, system_path(GIT_EXEC_PATH)); + add_path(&new_path, git_exec_path()); add_path(&new_path, argv0_path); if (old_path) -- cgit v1.2.1 From 35fb0e8633217f602360a9987af51c4b960e7850 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 18 Jan 2009 13:00:14 +0100 Subject: Compute prefix at runtime if RUNTIME_PREFIX is set This commit adds support for relocatable binaries (called RUNTIME_PREFIX). Such binaries can be moved together with the system configuration files to a different directory, as long as the relative paths from the binary to the configuration files is preserved. This functionality is essential on Windows where we deliver git binaries with an installer that allows to freely choose the installation location. If RUNTIME_PREFIX is unset we use the static prefix. This will be the default on Unix. Thus, the behavior on Unix will remain identical to the old implementation, which used to add the prefix in the Makefile. If RUNTIME_PREFIX is set the prefix is computed from the location of the executable. In this case, system_path() tries to strip known directories that executables can be located in from the path of the executable. If the path is successfully stripped it is used as the prefix. For example, if the executable is "/msysgit/bin/git" and BINDIR is "bin", then the prefix computed is "/msysgit". If the runtime prefix computation fails, we fall back to the static prefix specified in the makefile. This can be the case if the executable is not installed at a known location. Note that our test system sets GIT_CONFIG_NOSYSTEM to tell git to ignore global configuration files during testing. Hence testing does not trigger the fall back. Note that RUNTIME_PREFIX only works on Windows, though adding support on Unix should not be too hard. The implementation requires argv0_path to be set to an absolute path. argv0_path must point to the directory of the executable. We use assert() to verify this in debug builds. On Windows, the wrapper for main() (see compat/mingw.h) guarantees that argv0_path is correctly initialized. On Unix, further work is required before RUNTIME_PREFIX can be enabled. Signed-off-by: Steffen Prohaska Acked-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index 23f4873b2b..f234066def 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -9,12 +9,56 @@ static const char *argv0_path; const char *system_path(const char *path) { +#ifdef RUNTIME_PREFIX + static const char *prefix; +#else static const char *prefix = PREFIX; +#endif struct strbuf d = STRBUF_INIT; if (is_absolute_path(path)) return path; +#ifdef RUNTIME_PREFIX + assert(argv0_path); + assert(is_absolute_path(argv0_path)); + + if (!prefix) { + const char *strip[] = { + GIT_EXEC_PATH, + BINDIR, + 0 + }; + const char **s; + + for (s = strip; *s; s++) { + const char *sargv = argv0_path + strlen(argv0_path); + const char *ss = *s + strlen(*s); + while (argv0_path < sargv && *s < ss + && (*sargv == *ss || + (is_dir_sep(*sargv) && is_dir_sep(*ss)))) { + sargv--; + ss--; + } + if (*s == ss) { + struct strbuf d = STRBUF_INIT; + /* We also skip the trailing directory separator. */ + assert(sargv - argv0_path - 1 >= 0); + strbuf_add(&d, argv0_path, sargv - argv0_path - 1); + prefix = strbuf_detach(&d, NULL); + break; + } + } + } + + if (!prefix) { + prefix = PREFIX; + fprintf(stderr, "RUNTIME_PREFIX requested, " + "but prefix computation failed. " + "Using static fallback '%s'.\n", prefix); + } +#endif + strbuf_addf(&d, "%s/%s", prefix, path); path = strbuf_detach(&d, NULL); return path; -- cgit v1.2.1 From 024aa7d8d5114f76e616a0ed8ddcf3b463ff2c14 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 19 Feb 2009 20:10:53 +0100 Subject: system_path(): simplify using strip_path_suffix(), and add suffix "git" At least for the author of this patch, the logic in system_path() was too hard to understand. Using the function strip_path_suffix() documents the idea of the code better. The real change is to add the suffix "git", so that a runtime prefix will be computed correctly even when the executable was called in /git/ as is the case in msysGit (Windows insists to search the current directory before the PATH when looking for an executable). Signed-off-by: Johannes Schindelin Acked-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index f234066def..217c12577f 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -23,35 +23,10 @@ const char *system_path(const char *path) assert(argv0_path); assert(is_absolute_path(argv0_path)); - if (!prefix) { - const char *strip[] = { - GIT_EXEC_PATH, - BINDIR, - 0 - }; - const char **s; - - for (s = strip; *s; s++) { - const char *sargv = argv0_path + strlen(argv0_path); - const char *ss = *s + strlen(*s); - while (argv0_path < sargv && *s < ss - && (*sargv == *ss || - (is_dir_sep(*sargv) && is_dir_sep(*ss)))) { - sargv--; - ss--; - } - if (*s == ss) { - struct strbuf d = STRBUF_INIT; - /* We also skip the trailing directory separator. */ - assert(sargv - argv0_path - 1 >= 0); - strbuf_add(&d, argv0_path, sargv - argv0_path - 1); - prefix = strbuf_detach(&d, NULL); - break; - } - } - } - - if (!prefix) { + if (!prefix && + !(prefix = strip_path_suffix(argv0_path, GIT_EXEC_PATH)) && + !(prefix = strip_path_suffix(argv0_path, BINDIR)) && + !(prefix = strip_path_suffix(argv0_path, "git"))) { prefix = PREFIX; fprintf(stderr, "RUNTIME_PREFIX requested, " "but prefix computation failed. " -- cgit v1.2.1 From c90d565a46a664663467557d381417a3153fe26e Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 21 Mar 2009 23:21:18 +0100 Subject: Propagate --exec-path setting to external commands via GIT_EXEC_PATH Let PATH0=$PATH that was set before the invocation. Let /foo be a build directory. Let /pfx be the installation prefix. Let pfxexecpath=/pfx/libexec/git-core. The following is going on when 'git --exec-path=/foo gc' is invoked: 1. git sets PATH=/foo:$PATH0 using the path from --exec-path 2. gc execs 'git repack' (note: no dash). 3. Since there is a git in /foo (it's a build directory), /foo/git is taken. 4. No explicit exec-path is set this time, hence, this secondary git sets PATH=$pfxexecpath:/foo:$PATH 5. Since 'repack' is not a built-in, execv_dashed_external execs 'git-repack' (note: dash). 6. There is a $pfxexecpath/git-repack, and it is taken. 7. This git-repack runs 'git pack-objects' (note: no dash). 8. There is no git in $pfxexecpath, but there is one in /foo. Hence, /foo/git is run. 9. pack-objects is a builtin, hence, in effect /foo/git-pack-objects is run. As you can see, the way in which we previously set the PATH allowed to mix gits of different vintage. By setting GIT_EXEC_PATH when --exec-path was given on the command line, we reduce the confusion. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- exec_cmd.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'exec_cmd.c') diff --git a/exec_cmd.c b/exec_cmd.c index ce6741eb68..589059f026 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -25,6 +25,10 @@ void git_set_argv0_path(const char *path) void git_set_argv_exec_path(const char *exec_path) { argv_exec_path = exec_path; + /* + * Propagate this setting to external programs. + */ + setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1); } -- cgit v1.2.1