Skip to content

Commit 2de9de5

Browse files
sprohaskagitster
authored andcommitted
Move code interpreting path relative to exec-dir to new function system_path()
Expanding system paths relative to git_exec_path can be used for creating an installation that can be moved to a different directory without re-compiling. We use this approach for template_dir and the system wide gitconfig. The Windows installer (msysgit) is an example for such a setup. This commit moves common code to a new function system_path(). System paths that are to be interpreted relative to git_exec_path are passed to system_path() and the return value is used instead of the original path. system_path() prefixes a relative path with git_exec_path and leaves absolute paths unmodified. For example, we now write template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); [j6t: moved from path.c to exec_cmd.c] Signed-off-by: Steffen Prohaska <prohaska@zib.de> Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a9a3e82 commit 2de9de5

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

builtin-init-db.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,8 @@ static void copy_templates(const char *template_dir)
115115

116116
if (!template_dir)
117117
template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
118-
if (!template_dir) {
119-
/*
120-
* if the hard-coded template is relative, it is
121-
* interpreted relative to the exec_dir
122-
*/
123-
template_dir = DEFAULT_GIT_TEMPLATE_DIR;
124-
if (!is_absolute_path(template_dir)) {
125-
struct strbuf d = STRBUF_INIT;
126-
strbuf_addf(&d, "%s/%s", git_exec_path(), template_dir);
127-
template_dir = strbuf_detach(&d, NULL);
128-
}
129-
}
118+
if (!template_dir)
119+
template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
130120
strcpy(template_path, template_dir);
131121
template_len = strlen(template_path);
132122
if (template_path[template_len-1] != '/') {

config.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -581,15 +581,8 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
581581
const char *git_etc_gitconfig(void)
582582
{
583583
static const char *system_wide;
584-
if (!system_wide) {
585-
system_wide = ETC_GITCONFIG;
586-
if (!is_absolute_path(system_wide)) {
587-
/* interpret path relative to exec-dir */
588-
struct strbuf d = STRBUF_INIT;
589-
strbuf_addf(&d, "%s/%s", git_exec_path(), system_wide);
590-
system_wide = strbuf_detach(&d, NULL);
591-
}
592-
}
584+
if (!system_wide)
585+
system_wide = system_path(ETC_GITCONFIG);
593586
return system_wide;
594587
}
595588

exec_cmd.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ static const char *builtin_exec_path(void)
4040
#endif
4141
}
4242

43+
const char *system_path(const char *path)
44+
{
45+
if (!is_absolute_path(path)) {
46+
struct strbuf d = STRBUF_INIT;
47+
strbuf_addf(&d, "%s/%s", git_exec_path(), path);
48+
path = strbuf_detach(&d, NULL);
49+
}
50+
return path;
51+
}
52+
4353
void git_set_argv_exec_path(const char *exec_path)
4454
{
4555
argv_exec_path = exec_path;

exec_cmd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ extern const char* git_exec_path(void);
66
extern void setup_path(const char *);
77
extern int execv_git_cmd(const char **argv); /* NULL terminated */
88
extern int execl_git_cmd(const char *cmd, ...);
9-
9+
extern const char *system_path(const char *path);
1010

1111
#endif /* GIT_EXEC_CMD_H */

0 commit comments

Comments
 (0)