Skip to content

Commit d286583

Browse files
committed
interpolate_path(): allow 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 an even lengthier time to mull over the problem and its best solution, and then more discussions, we eventually decided to introduce support for the magic sequence `%(prefix)/`. If a path starts with this, the remainder is interpreted as relative to the detected (runtime) prefix. If built without runtime prefix support, Git will simply interpolate the compiled-in prefix. If a user _wants_ to specify a path starting with the magic sequence, they can prefix the magic sequence with `./` and voilà, the path won't be expanded. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 19fd9c3 commit d286583

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
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

path.c

Lines changed: 8 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
{
@@ -723,6 +724,9 @@ static struct passwd *getpw_str(const char *username, size_t len)
723724
* failure or if path is NULL.
724725
*
725726
* 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.
726730
*/
727731
char *interpolate_path(const char *path, int real_home)
728732
{
@@ -731,6 +735,10 @@ char *interpolate_path(const char *path, int real_home)
731735

732736
if (path == NULL)
733737
goto return_null;
738+
739+
if (skip_prefix(path, "%(prefix)/", &path))
740+
return system_path(path);
741+
734742
if (path[0] == '~') {
735743
const char *first_slash = strchrnul(path, '/');
736744
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 '%(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 &&
543551
test_cmp expect actual
544552
'
545553

0 commit comments

Comments
 (0)