Skip to content

Commit aab0eea

Browse files
committed
Merge branch 'js/expand-runtime-prefix'
Pathname expansion (like "~username/") learned a way to specify a location relative to Git installation (e.g. its $sharedir which is $(prefix)/share), with "%(prefix)". * js/expand-runtime-prefix: expand_user_path: allow in-flight topics to keep using the old name interpolate_path(): allow specifying paths relative to the runtime prefix Use a better name for the function interpolating paths expand_user_path(): clarify the role of the `real_home` parameter expand_user_path(): remove stale part of the comment tests: exercise the RUNTIME_PREFIX feature
2 parents f19b275 + 7ed37eb commit aab0eea

File tree

10 files changed

+64
-15
lines changed

10 files changed

+64
-15
lines changed

Documentation/config.txt

Lines changed: 9 additions & 0 deletions
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

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,11 @@ ifdef GIT_TEST_INDEX_VERSION
28482848
endif
28492849
ifdef GIT_TEST_PERL_FATAL_WARNINGS
28502850
@echo GIT_TEST_PERL_FATAL_WARNINGS=\''$(subst ','\'',$(subst ','\'',$(GIT_TEST_PERL_FATAL_WARNINGS)))'\' >>$@+
2851+
endif
2852+
ifdef RUNTIME_PREFIX
2853+
@echo RUNTIME_PREFIX=\'true\' >>$@+
2854+
else
2855+
@echo RUNTIME_PREFIX=\'false\' >>$@+
28512856
endif
28522857
@if cmp $@+ $@ >/dev/null 2>&1; then $(RM) $@+; else mv $@+ $@; fi
28532858

builtin/credential-cache.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int cmd_credential_store(int argc, const char **argv, const char *prefix)
173173
if (file) {
174174
string_list_append(&fns, file);
175175
} else {
176-
if ((file = expand_user_path("~/.git-credentials", 0)))
176+
if ((file = interpolate_path("~/.git-credentials", 0)))
177177
string_list_append_nodup(&fns, file);
178178
file = xdg_config_home("credentials");
179179
if (file)

builtin/gc.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,9 @@ typedef int create_file_fn(const char *path, void *cb);
12551255
int raceproof_create_file(const char *path, create_file_fn fn, void *cb);
12561256

12571257
int mkdir_in_gitdir(const char *path);
1258-
char *expand_user_path(const char *path, int real_home);
1258+
char *interpolate_path(const char *path, int real_home);
1259+
/* NEEDSWORK: remove this synonym once in-flight topics have migrated */
1260+
#define expand_user_path interpolate_path
12591261
const char *enter_repo(const char *path, int strict);
12601262
static inline int is_absolute_path(const char *path)
12611263
{

config.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
137137
if (!path)
138138
return config_error_nonbool("include.path");
139139

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

188-
expanded = expand_user_path(pat->buf, 1);
188+
expanded = interpolate_path(pat->buf, 1);
189189
if (expanded) {
190190
strbuf_reset(pat);
191191
strbuf_addstr(pat, expanded);
@@ -1270,7 +1270,7 @@ int git_config_pathname(const char **dest, const char *var, const char *value)
12701270
{
12711271
if (!value)
12721272
return config_error_nonbool(var);
1273-
*dest = expand_user_path(value, 0);
1273+
*dest = interpolate_path(value, 0);
12741274
if (!*dest)
12751275
die(_("failed to expand user dir in: '%s'"), value);
12761276
return 0;
@@ -1845,7 +1845,7 @@ void git_global_config(char **user_out, char **xdg_out)
18451845
char *xdg_config = NULL;
18461846

18471847
if (!user_config) {
1848-
user_config = expand_user_path("~/.gitconfig", 0);
1848+
user_config = interpolate_path("~/.gitconfig", 0);
18491849
xdg_config = xdg_config_home("config");
18501850
}
18511851

path.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "packfile.h"
1313
#include "object-store.h"
1414
#include "lockfile.h"
15+
#include "exec-cmd.h"
1516

1617
static int get_st_mode_bits(const char *path, int *mode)
1718
{
@@ -719,19 +720,25 @@ static struct passwd *getpw_str(const char *username, size_t len)
719720
}
720721

721722
/*
722-
* Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
723-
* then it is a newly allocated string. Returns NULL on getpw failure or
724-
* 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.
725725
*
726-
* 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.
727730
*/
728-
char *expand_user_path(const char *path, int real_home)
731+
char *interpolate_path(const char *path, int real_home)
729732
{
730733
struct strbuf user_path = STRBUF_INIT;
731734
const char *to_copy = path;
732735

733736
if (path == NULL)
734737
goto return_null;
738+
739+
if (skip_prefix(path, "%(prefix)/", &path))
740+
return system_path(path);
741+
735742
if (path[0] == '~') {
736743
const char *first_slash = strchrnul(path, '/');
737744
const char *username = path + 1;
@@ -812,7 +819,7 @@ const char *enter_repo(const char *path, int strict)
812819
strbuf_add(&validated_path, path, len);
813820

814821
if (used_path.buf[0] == '~') {
815-
char *newpath = expand_user_path(used_path.buf, 0);
822+
char *newpath = interpolate_path(used_path.buf, 0);
816823
if (!newpath)
817824
return NULL;
818825
strbuf_attach(&used_path, newpath, strlen(newpath),

sequencer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ N_("Your name and email address were configured automatically based\n"
12411241

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

t/t0060-path-utils.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,30 @@ 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_done

0 commit comments

Comments
 (0)