Skip to content

Commit 66df56f

Browse files
committed
expand_user_path(): support specifying paths relative to the runtime prefix
Ever since Git learned to detect its install location at runtime, there was the slightly awkward problem that it was impossible to specify paths relative to said location. For example, if a version of Git was shipped with custom SSL certificates to use, there was no portable way to specify `http.sslCAInfo`. In Git for Windows, the problem was "solved" for years by interpreting paths starting with a slash as relative to the runtime prefix. However, this is not correct: such paths _are_ legal on Windows, and they are interpreted as absolute paths in the same drive as the current directory. After a lengthy discussion, and a way lengthier time to mull over the problem and its best solution, we decided to introduce support for the magic sequence `<RUNTIME-PREFIX>/`. If a path starts with this, the remainder is interpreted as relative to the detected runtime prefix. This solves the problem, but what new problems does it stir up? Here are the two most obvious ones: - What if Git was not compiled with support for a runtime prefix? In that case, we will simply use the compiled-in hard-coded prefix. - What if a user _wants_ to specify a path starting with the magic sequence? In that case, the user will simply need to prefix the magic sequence with `./` and voilà, the path won't be expanded. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent cc8f09b commit 66df56f

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Documentation/config.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,16 @@ 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 `<RUNTIME-PREFIX>/`, the remainder is
303+
interpreted as a path relative to Git's "runtime prefix", i.e. relative
304+
to the location where Git itself was installed. For example,
305+
`<RUNTIME-PREFIX>/bin/` refers to the directory in which the Git
306+
executable itself lives. If Git was compiled without runtime prefix
307+
support, the compiled-in prefix will be subsituted instead. In the
308+
unlikely event that a literal path needs to be specified that should
309+
_not_ be expanded, it needs to be prefixed by `./`, like so:
310+
`./<RUNTIME-PREFIX>/bin`.
301311

302312

303313
Variables

path.c

Lines changed: 5 additions & 0 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
{
@@ -732,6 +733,10 @@ char *expand_user_path(const char *path, int real_home)
732733

733734
if (path == NULL)
734735
goto return_null;
736+
737+
if (skip_prefix(path, "<RUNTIME-PREFIX>/", &path))
738+
return system_path(path);
739+
735740
if (path[0] == '~') {
736741
const char *first_slash = strchrnul(path, '/');
737742
const char *username = path + 1;

t/t0060-path-utils.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,14 @@ test_expect_success RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX works' '
540540
cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
541541
GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
542542
echo HERE >expect &&
543+
test_cmp expect actual'
544+
545+
test_expect_success RUNTIME_PREFIX,CAN_EXEC_IN_PWD '<RUNTIME-PREFIX>/ works' '
546+
mkdir -p pretend/bin &&
547+
cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
548+
git config yes.path "<RUNTIME-PREFIX>/yes" &&
549+
GIT_EXEC_PATH= ./pretend/bin/git config --path yes.path >actual &&
550+
echo "$(pwd)/pretend/yes" >expect &&
543551
test_cmp expect actual
544552
'
545553

0 commit comments

Comments
 (0)