Skip to content

Commit ed096c4

Browse files
committed
Merge branch 'sp/runtime-prefix'
* sp/runtime-prefix: Windows: Revert to default paths and convert them by RUNTIME_PREFIX Compute prefix at runtime if RUNTIME_PREFIX is set Modify setup_path() to only add git_exec_path() to PATH Add calls to git_extract_argv0_path() in programs that call git_config_* git_extract_argv0_path(): Move check for valid argv0 from caller to callee Refactor git_set_argv0_path() to git_extract_argv0_path() Move computation of absolute paths from Makefile to runtime (in preparation for RUNTIME_PREFIX)
2 parents fa5bc8a + 2565522 commit ed096c4

21 files changed

+151
-46
lines changed

Makefile

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,28 +182,32 @@ STRIP ?= strip
182182
# Among the variables below, these:
183183
# gitexecdir
184184
# template_dir
185+
# mandir
186+
# infodir
185187
# htmldir
186188
# ETC_GITCONFIG (but not sysconfdir)
187-
# can be specified as a relative path ../some/where/else (which must begin
188-
# with ../); this is interpreted as relative to $(bindir) and "git" at
189+
# can be specified as a relative path some/where/else;
190+
# this is interpreted as relative to $(prefix) and "git" at
189191
# runtime figures out where they are based on the path to the executable.
190192
# This can help installing the suite in a relocatable way.
191193

192194
prefix = $(HOME)
193-
bindir = $(prefix)/bin
194-
mandir = $(prefix)/share/man
195-
infodir = $(prefix)/share/info
196-
gitexecdir = $(prefix)/libexec/git-core
195+
bindir_relative = bin
196+
bindir = $(prefix)/$(bindir_relative)
197+
mandir = share/man
198+
infodir = share/info
199+
gitexecdir = libexec/git-core
197200
sharedir = $(prefix)/share
198-
template_dir = $(sharedir)/git-core/templates
199-
htmldir=$(sharedir)/doc/git-doc
201+
template_dir = share/git-core/templates
202+
htmldir = share/doc/git-doc
200203
ifeq ($(prefix),/usr)
201204
sysconfdir = /etc
205+
ETC_GITCONFIG = $(sysconfdir)/gitconfig
202206
else
203207
sysconfdir = $(prefix)/etc
208+
ETC_GITCONFIG = etc/gitconfig
204209
endif
205210
lib = lib
206-
ETC_GITCONFIG = $(sysconfdir)/gitconfig
207211
# DESTDIR=
208212

209213
# default configuration for gitweb
@@ -790,6 +794,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
790794
SNPRINTF_RETURNS_BOGUS = YesPlease
791795
NO_SVN_TESTS = YesPlease
792796
NO_PERL_MAKEMAKER = YesPlease
797+
RUNTIME_PREFIX = YesPlease
793798
NO_POSIX_ONLY_PROGRAMS = YesPlease
794799
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease
795800
COMPAT_CFLAGS += -D__USE_MINGW_ACCESS -DNOGDI -Icompat -Icompat/regex -Icompat/fnmatch
@@ -798,9 +803,6 @@ ifneq (,$(findstring MINGW,$(uname_S)))
798803
COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/regex/regex.o compat/winansi.o
799804
EXTLIBS += -lws2_32
800805
X = .exe
801-
gitexecdir = ../libexec/git-core
802-
template_dir = ../share/git-core/templates/
803-
ETC_GITCONFIG = ../etc/gitconfig
804806
endif
805807
ifneq (,$(findstring arm,$(uname_M)))
806808
ARM_SHA1 = YesPlease
@@ -1038,6 +1040,9 @@ ifdef INTERNAL_QSORT
10381040
COMPAT_CFLAGS += -DINTERNAL_QSORT
10391041
COMPAT_OBJS += compat/qsort.o
10401042
endif
1043+
ifdef RUNTIME_PREFIX
1044+
COMPAT_CFLAGS += -DRUNTIME_PREFIX
1045+
endif
10411046

10421047
ifdef NO_PTHREADS
10431048
THREADED_DELTA_SEARCH =
@@ -1097,6 +1102,7 @@ ETC_GITCONFIG_SQ = $(subst ','\'',$(ETC_GITCONFIG))
10971102

10981103
DESTDIR_SQ = $(subst ','\'',$(DESTDIR))
10991104
bindir_SQ = $(subst ','\'',$(bindir))
1105+
bindir_relative_SQ = $(subst ','\'',$(bindir_relative))
11001106
mandir_SQ = $(subst ','\'',$(mandir))
11011107
infodir_SQ = $(subst ','\'',$(infodir))
11021108
gitexecdir_SQ = $(subst ','\'',$(gitexecdir))
@@ -1262,7 +1268,12 @@ git.o git.spec \
12621268
$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) $<
12631269

12641270
exec_cmd.o: exec_cmd.c GIT-CFLAGS
1265-
$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' $<
1271+
$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) \
1272+
'-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' \
1273+
'-DBINDIR="$(bindir_relative_SQ)"' \
1274+
'-DPREFIX="$(prefix_SQ)"' \
1275+
$<
1276+
12661277
builtin-init-db.o: builtin-init-db.c GIT-CFLAGS
12671278
$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir_SQ)"' $<
12681279

@@ -1422,17 +1433,17 @@ remove-dashes:
14221433

14231434
### Installation rules
14241435

1425-
ifeq ($(firstword $(subst /, ,$(template_dir))),..)
1426-
template_instdir = $(bindir)/$(template_dir)
1427-
else
1436+
ifeq ($(abspath $(template_dir)),$(template_dir))
14281437
template_instdir = $(template_dir)
1438+
else
1439+
template_instdir = $(prefix)/$(template_dir)
14291440
endif
14301441
export template_instdir
14311442

1432-
ifeq ($(firstword $(subst /, ,$(gitexecdir))),..)
1433-
gitexec_instdir = $(bindir)/$(gitexecdir)
1434-
else
1443+
ifeq ($(abspath $(gitexecdir)),$(gitexecdir))
14351444
gitexec_instdir = $(gitexecdir)
1445+
else
1446+
gitexec_instdir = $(prefix)/$(gitexecdir)
14361447
endif
14371448
gitexec_instdir_SQ = $(subst ','\'',$(gitexec_instdir))
14381449
export gitexec_instdir

builtin-help.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ static void setup_man_path(void)
329329
* old_path, the ':' at the end will let 'man' to try
330330
* system-wide paths after ours to find the manual page. If
331331
* there is old_path, we need ':' as delimiter. */
332-
strbuf_addstr(&new_path, GIT_MAN_PATH);
332+
strbuf_addstr(&new_path, system_path(GIT_MAN_PATH));
333333
strbuf_addch(&new_path, ':');
334334
if (old_path)
335335
strbuf_addstr(&new_path, old_path);
@@ -375,7 +375,7 @@ static void show_man_page(const char *git_cmd)
375375
static void show_info_page(const char *git_cmd)
376376
{
377377
const char *page = cmd_to_page(git_cmd);
378-
setenv("INFOPATH", GIT_INFO_PATH, 1);
378+
setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
379379
execlp("info", "info", "gitman", page, NULL);
380380
}
381381

daemon.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,8 @@ int main(int argc, char **argv)
937937
gid_t gid = 0;
938938
int i;
939939

940+
git_extract_argv0_path(argv[0]);
941+
940942
for (i = 1; i < argc; i++) {
941943
char *arg = argv[i];
942944

exec_cmd.c

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,78 @@ 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+
#else
15+
static const char *prefix = PREFIX;
16+
#endif
17+
struct strbuf d = STRBUF_INIT;
18+
19+
if (is_absolute_path(path))
20+
return path;
21+
22+
#ifdef RUNTIME_PREFIX
23+
assert(argv0_path);
24+
assert(is_absolute_path(argv0_path));
25+
26+
if (!prefix) {
27+
const char *strip[] = {
28+
GIT_EXEC_PATH,
29+
BINDIR,
30+
0
31+
};
32+
const char **s;
33+
34+
for (s = strip; *s; s++) {
35+
const char *sargv = argv0_path + strlen(argv0_path);
36+
const char *ss = *s + strlen(*s);
37+
while (argv0_path < sargv && *s < ss
38+
&& (*sargv == *ss ||
39+
(is_dir_sep(*sargv) && is_dir_sep(*ss)))) {
40+
sargv--;
41+
ss--;
42+
}
43+
if (*s == ss) {
44+
struct strbuf d = STRBUF_INIT;
45+
/* We also skip the trailing directory separator. */
46+
assert(sargv - argv0_path - 1 >= 0);
47+
strbuf_add(&d, argv0_path, sargv - argv0_path - 1);
48+
prefix = strbuf_detach(&d, NULL);
49+
break;
50+
}
51+
}
1652
}
53+
54+
if (!prefix) {
55+
prefix = PREFIX;
56+
fprintf(stderr, "RUNTIME_PREFIX requested, "
57+
"but prefix computation failed. "
58+
"Using static fallback '%s'.\n", prefix);
59+
}
60+
#endif
61+
62+
strbuf_addf(&d, "%s/%s", prefix, path);
63+
path = strbuf_detach(&d, NULL);
1764
return path;
1865
}
1966

20-
void git_set_argv0_path(const char *path)
67+
const char *git_extract_argv0_path(const char *argv0)
2168
{
22-
argv0_path = path;
69+
const char *slash;
70+
71+
if (!argv0 || !*argv0)
72+
return NULL;
73+
slash = argv0 + strlen(argv0);
74+
75+
while (argv0 <= slash && !is_dir_sep(*slash))
76+
slash--;
77+
78+
if (slash >= argv0) {
79+
argv0_path = xstrndup(argv0, slash - argv0);
80+
return slash + 1;
81+
}
82+
83+
return argv0;
2384
}
2485

2586
void git_set_argv_exec_path(const char *exec_path)
@@ -61,9 +122,7 @@ void setup_path(void)
61122
const char *old_path = getenv("PATH");
62123
struct strbuf new_path = STRBUF_INIT;
63124

64-
add_path(&new_path, argv_exec_path);
65-
add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT));
66-
add_path(&new_path, system_path(GIT_EXEC_PATH));
125+
add_path(&new_path, git_exec_path());
67126
add_path(&new_path, argv0_path);
68127

69128
if (old_path)

exec_cmd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#define GIT_EXEC_CMD_H
33

44
extern void git_set_argv_exec_path(const char *exec_path);
5-
extern void git_set_argv0_path(const char *path);
6-
extern const char* git_exec_path(void);
5+
extern const char *git_extract_argv0_path(const char *path);
6+
extern const char *git_exec_path(void);
77
extern void setup_path(void);
88
extern const char **prepare_git_cmd(const char **argv);
99
extern int execv_git_cmd(const char **argv); /* NULL terminated */

fast-import.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Format of STDIN stream:
150150
#include "refs.h"
151151
#include "csum-file.h"
152152
#include "quote.h"
153+
#include "exec_cmd.h"
153154

154155
#define PACK_ID_BITS 16
155156
#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
@@ -2406,6 +2407,8 @@ int main(int argc, const char **argv)
24062407
{
24072408
unsigned int i, show_stats = 1;
24082409

2410+
git_extract_argv0_path(argv[0]);
2411+
24092412
setup_git_directory();
24102413
git_config(git_pack_config, NULL);
24112414
if (!pack_compression_seen && core_compression_seen)

git.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -442,21 +442,11 @@ static int run_argv(int *argcp, const char ***argv)
442442

443443
int main(int argc, const char **argv)
444444
{
445-
const char *cmd = argv[0] && *argv[0] ? argv[0] : "git-help";
446-
char *slash = (char *)cmd + strlen(cmd);
445+
const char *cmd;
447446

448-
/*
449-
* Take the basename of argv[0] as the command
450-
* name, and the dirname as the default exec_path
451-
* if we don't have anything better.
452-
*/
453-
while (cmd <= slash && !is_dir_sep(*slash))
454-
slash--;
455-
if (cmd <= slash) {
456-
*slash++ = 0;
457-
git_set_argv0_path(cmd);
458-
cmd = slash;
459-
}
447+
cmd = git_extract_argv0_path(argv[0]);
448+
if (!cmd)
449+
cmd = "git-help";
460450

461451
/*
462452
* "git-xxxx" is the same as "git xxxx", but we obviously:

hash-object.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "blob.h"
99
#include "quote.h"
1010
#include "parse-options.h"
11+
#include "exec_cmd.h"
1112

1213
static void hash_fd(int fd, const char *type, int write_object, const char *path)
1314
{
@@ -81,6 +82,8 @@ int main(int argc, const char **argv)
8182

8283
type = blob_type;
8384

85+
git_extract_argv0_path(argv[0]);
86+
8487
git_config(git_default_config, NULL);
8588

8689
argc = parse_options(argc, argv, hash_object_options, hash_object_usage, 0);

http-push.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,8 @@ int main(int argc, char **argv)
21962196
struct ref *ref;
21972197
char *rewritten_url = NULL;
21982198

2199+
git_extract_argv0_path(argv[0]);
2200+
21992201
setup_git_directory();
22002202

22012203
remote = xcalloc(sizeof(*remote), 1);

imap-send.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
#include "cache.h"
26+
#include "exec_cmd.h"
2627
#ifdef NO_OPENSSL
2728
typedef void *SSL;
2829
#endif
@@ -1389,6 +1390,8 @@ int main(int argc, char **argv)
13891390
int total, n = 0;
13901391
int nongit_ok;
13911392

1393+
git_extract_argv0_path(argv[0]);
1394+
13921395
/* init the random number generator */
13931396
arc4_init();
13941397

0 commit comments

Comments
 (0)