Skip to content

Commit 7b6c649

Browse files
committed
system_path(): Add prefix computation at runtime if RUNTIME_PREFIX set
This commit modifies system_path() to compute the prefix at runtime if configured to do so. If RUNTIME_PREFIX is defined, 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 is computed as "/msysgit". We report an error if the runtime prefix computation fails, which can happen if the executable is not installed at a known location. The user should know that the global configuration is not picked up, because this can cause unexpected behavior. If we explicitly want to ignore system wide paths, we can set the environment variable GIT_CONFIG_NOSYSTEM, as our tests do. The implementation requires that argv0_path is set up properly, which is currently the case only on Windows. argv0_path must point to the absolute path of the directory of the executable, which is verified by two calls to assert(). On Windows, the wrapper for main() (see compat/mingw.h) guarantees that this is the case. On Unix, further work is required before RUNTIME_PREFIX can be enabled.
1 parent fa7fbeb commit 7b6c649

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,9 @@ ifdef INTERNAL_QSORT
989989
COMPAT_CFLAGS += -DINTERNAL_QSORT
990990
COMPAT_OBJS += compat/qsort.o
991991
endif
992+
ifdef RUNTIME_PREFIX
993+
COMPAT_CFLAGS += -DRUNTIME_PREFIX
994+
endif
992995

993996
ifdef THREADED_DELTA_SEARCH
994997
BASIC_CFLAGS += -DTHREADED_DELTA_SEARCH

exec_cmd.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,48 @@ static const char *argv0_path;
99

1010
const char *system_path(const char *path)
1111
{
12-
if (!is_absolute_path(path) && argv0_path) {
13-
struct strbuf d = STRBUF_INIT;
14-
strbuf_addf(&d, "%s/%s", argv0_path, path);
15-
path = strbuf_detach(&d, NULL);
12+
#ifdef RUNTIME_PREFIX
13+
static const char *prefix;
14+
15+
assert(argv0_path);
16+
assert(is_absolute_path(argv0_path));
17+
18+
if (!prefix) {
19+
const char *strip[] = {
20+
GIT_EXEC_PATH,
21+
BINDIR,
22+
0
23+
};
24+
const char **s;
25+
26+
for (s = strip; *s; s++) {
27+
const char *sargv = argv0_path + strlen(argv0_path);
28+
const char *ss = *s + strlen(*s);
29+
while (argv0_path < sargv && *s < ss
30+
&& (*sargv == *ss ||
31+
(is_dir_sep(*sargv) && is_dir_sep(*ss)))) {
32+
sargv--;
33+
ss--;
34+
}
35+
if (*s == ss) {
36+
struct strbuf d = STRBUF_INIT;
37+
strbuf_add(&d, argv0_path, sargv - argv0_path);
38+
prefix = strbuf_detach(&d, NULL);
39+
break;
40+
}
41+
}
1642
}
43+
44+
if (!prefix) {
45+
fprintf(stderr, "RUNTIME_PREFIX requested for path '%s', "
46+
"but prefix computation failed.\n", path);
47+
return path;
48+
}
49+
50+
struct strbuf d = STRBUF_INIT;
51+
strbuf_addf(&d, "%s/%s", prefix, path);
52+
path = strbuf_detach(&d, NULL);
53+
#endif
1754
return path;
1855
}
1956

0 commit comments

Comments
 (0)