Skip to content

Commit 3f28c5f

Browse files
authored
Merge pull request git-for-windows#3472 from dscho/expand-runtime-prefix
Re-do the path interpolation support regarding RUNTIME_PREFIX
2 parents c7d0a86 + 6319bce commit 3f28c5f

9 files changed

+61
-34
lines changed

Documentation/config.txt

+9
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,15 @@ pathname::
298298
tilde expansion happens to such a string: `~/`
299299
is expanded to the value of `$HOME`, and `~user/` to the
300300
specified user's home directory.
301+
+
302+
If a path starts with `%(prefix)/`, the remainder is interpreted as a
303+
path relative to Git's "runtime prefix", i.e. relative to the location
304+
where Git itself was installed. For example, `%(prefix)/bin/` refers to
305+
the directory in which the Git executable itself lives. If Git was
306+
compiled without runtime prefix support, the compiled-in prefix will be
307+
subsituted instead. In the unlikely event that a literal path needs to
308+
be specified that should _not_ be expanded, it needs to be prefixed by
309+
`./`, like so: `./%(prefix)/bin`.
301310

302311

303312
Variables

builtin/credential-cache.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static char *get_socket_path(void)
9090
{
9191
struct stat sb;
9292
char *old_dir, *socket;
93-
old_dir = expand_user_path("~/.git-credential-cache", 0);
93+
old_dir = interpolate_path("~/.git-credential-cache", 0);
9494
if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode))
9595
socket = xstrfmt("%s/socket", old_dir);
9696
else

builtin/credential-store.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ int cmd_credential_store(int argc, const char **argv, const char *prefix)
176176
if (file) {
177177
string_list_append(&fns, file);
178178
} else {
179-
if ((file = expand_user_path("~/.git-credentials", 0)))
179+
if ((file = interpolate_path("~/.git-credentials", 0)))
180180
string_list_append_nodup(&fns, file);
181181
file = xdg_config_home("credentials");
182182
if (file)

builtin/gc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ static char *launchctl_service_filename(const char *name)
15421542
struct strbuf filename = STRBUF_INIT;
15431543
strbuf_addf(&filename, "~/Library/LaunchAgents/%s.plist", name);
15441544

1545-
expanded = expand_user_path(filename.buf, 1);
1545+
expanded = interpolate_path(filename.buf, 1);
15461546
if (!expanded)
15471547
die(_("failed to expand path '%s'"), filename.buf);
15481548

cache.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,9 @@ typedef int create_file_fn(const char *path, void *cb);
12531253
int raceproof_create_file(const char *path, create_file_fn fn, void *cb);
12541254

12551255
int mkdir_in_gitdir(const char *path);
1256-
char *expand_user_path(const char *path, int real_home);
1256+
char *interpolate_path(const char *path, int real_home);
1257+
/* NEEDSWORK: remove this synonym once in-flight topics have migrated */
1258+
#define expand_user_path interpolate_path
12571259
const char *enter_repo(const char *path, int strict);
12581260
static inline int is_absolute_path(const char *path)
12591261
{

config.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
136136
if (!path)
137137
return config_error_nonbool("include.path");
138138

139-
expanded = expand_user_path(path, 0);
139+
expanded = interpolate_path(path, 0);
140140
if (!expanded)
141141
return error(_("could not expand include path '%s'"), path);
142142
path = expanded;
@@ -184,7 +184,7 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
184184
char *expanded;
185185
int prefix = 0;
186186

187-
expanded = expand_user_path(pat->buf, 1);
187+
expanded = interpolate_path(pat->buf, 1);
188188
if (expanded) {
189189
strbuf_reset(pat);
190190
strbuf_addstr(pat, expanded);
@@ -1269,7 +1269,7 @@ int git_config_pathname(const char **dest, const char *var, const char *value)
12691269
{
12701270
if (!value)
12711271
return config_error_nonbool(var);
1272-
*dest = expand_user_path(value, 0);
1272+
*dest = interpolate_path(value, 0);
12731273
if (!*dest)
12741274
die(_("failed to expand user dir in: '%s'"), value);
12751275
return 0;
@@ -1842,7 +1842,7 @@ void git_global_config(char **user_out, char **xdg_out)
18421842
char *xdg_config = NULL;
18431843

18441844
if (!user_config) {
1845-
user_config = expand_user_path("~/.gitconfig", 0);
1845+
user_config = interpolate_path("~/.gitconfig", 0);
18461846
xdg_config = xdg_config_home("config");
18471847
}
18481848

path.c

+15-7
Original file line numberDiff line numberDiff line change
@@ -720,22 +720,30 @@ static struct passwd *getpw_str(const char *username, size_t len)
720720
}
721721

722722
/*
723-
* Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
724-
* then it is a newly allocated string. Returns NULL on getpw failure or
725-
* if path is NULL.
723+
* Return a string with ~ and ~user expanded via getpw*. Returns NULL on getpw
724+
* failure or if path is NULL.
726725
*
727-
* If real_home is true, strbuf_realpath($HOME) is used in the expansion.
726+
* If real_home is true, strbuf_realpath($HOME) is used in the `~/` expansion.
727+
*
728+
* If the path starts with `%(prefix)/`, the remainder is interpreted as
729+
* relative to where Git is installed, and expanded to the absolute path.
728730
*/
729-
char *expand_user_path(const char *path, int real_home)
731+
char *interpolate_path(const char *path, int real_home)
730732
{
731733
struct strbuf user_path = STRBUF_INIT;
732734
const char *to_copy = path;
733735

734736
if (path == NULL)
735737
goto return_null;
738+
739+
if (skip_prefix(path, "%(prefix)/", &path))
740+
return system_path(path);
741+
736742
#ifdef __MINGW32__
737-
if (path[0] == '/')
743+
if (path[0] == '/') {
744+
warning(_("encountered old-style '%s' that should be '%%(prefix)%s'"), path, path);
738745
return system_path(path + 1);
746+
}
739747
#endif
740748
if (path[0] == '~') {
741749
const char *first_slash = strchrnul(path, '/');
@@ -817,7 +825,7 @@ const char *enter_repo(const char *path, int strict)
817825
strbuf_add(&validated_path, path, len);
818826

819827
if (used_path.buf[0] == '~') {
820-
char *newpath = expand_user_path(used_path.buf, 0);
828+
char *newpath = interpolate_path(used_path.buf, 0);
821829
if (!newpath)
822830
return NULL;
823831
strbuf_attach(&used_path, newpath, strlen(newpath),

sequencer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ N_("Your name and email address were configured automatically based\n"
12421242

12431243
static const char *implicit_ident_advice(void)
12441244
{
1245-
char *user_config = expand_user_path("~/.gitconfig", 0);
1245+
char *user_config = interpolate_path("~/.gitconfig", 0);
12461246
char *xdg_config = xdg_config_home("config");
12471247
int config_exists = file_exists(user_config) || file_exists(xdg_config);
12481248

t/t0060-path-utils.sh

+26-18
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,32 @@ test_expect_success MINGW 'is_valid_path() on Windows' '
525525
"PRN./abc"
526526
'
527527

528+
test_lazy_prereq RUNTIME_PREFIX '
529+
test true = "$RUNTIME_PREFIX"
530+
'
531+
532+
test_lazy_prereq CAN_EXEC_IN_PWD '
533+
cp "$GIT_EXEC_PATH"/git$X ./ &&
534+
./git rev-parse
535+
'
536+
537+
test_expect_success RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX works' '
538+
mkdir -p pretend/bin pretend/libexec/git-core &&
539+
echo "echo HERE" | write_script pretend/libexec/git-core/git-here &&
540+
cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
541+
GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
542+
echo HERE >expect &&
543+
test_cmp expect actual'
544+
545+
test_expect_success RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works' '
546+
mkdir -p pretend/bin &&
547+
cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
548+
git config yes.path "%(prefix)/yes" &&
549+
GIT_EXEC_PATH= ./pretend/bin/git config --path yes.path >actual &&
550+
echo "$(pwd)/pretend/yes" >expect &&
551+
test_cmp expect actual
552+
'
553+
528554
test_expect_success MINGW 'MSYSTEM/PATH is adjusted if necessary' '
529555
mkdir -p "$HOME"/bin pretend/mingw64/bin \
530556
pretend/mingw64/libexec/git-core pretend/usr/bin &&
@@ -545,22 +571,4 @@ test_expect_success MINGW 'MSYSTEM/PATH is adjusted if necessary' '
545571
test_cmp expect actual
546572
'
547573

548-
test_lazy_prereq RUNTIME_PREFIX '
549-
test true = "$RUNTIME_PREFIX"
550-
'
551-
552-
test_lazy_prereq CAN_EXEC_IN_PWD '
553-
cp "$GIT_EXEC_PATH"/git$X ./ &&
554-
./git rev-parse
555-
'
556-
557-
test_expect_success RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX works' '
558-
mkdir -p pretend/git pretend/libexec/git-core &&
559-
echo "echo HERE" | write_script pretend/libexec/git-core/git-here &&
560-
cp "$GIT_EXEC_PATH"/git$X pretend/git/ &&
561-
GIT_EXEC_PATH= ./pretend/git/git here >actual &&
562-
echo HERE >expect &&
563-
test_cmp expect actual
564-
'
565-
566574
test_done

0 commit comments

Comments
 (0)