]> git.ipfire.org Git - thirdparty/git.git/commitdiff
read-tree: explicitly disallow prefixes with a leading '/'
authorVictoria Dye <vdye@github.com>
Tue, 1 Mar 2022 20:24:26 +0000 (20:24 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 1 Mar 2022 20:36:00 +0000 (12:36 -0800)
Exit with an error if a prefix provided to `git read-tree --prefix` begins
with '/'. In most cases, prefixes like this result in an "invalid path"
error; however, the repository root would be interpreted as valid when
specified as '--prefix=/'. This is due to leniency around trailing directory
separators on prefixes (e.g., allowing both '--prefix=my-dir' and
'--prefix=my-dir/') - the '/' in the prefix is actually the *trailing*
slash, although it could be misinterpreted as a *leading* slash.

To remove the confusing repo root-as-'/' case and make it clear that
prefixes should not begin with '/', exit with an error if the first
character of the provided prefix is '/'.

Helped-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Victoria Dye <vdye@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/read-tree.c
t/t1003-read-tree-prefix.sh

index 2109c4c9e5c1c747ea08c8e2152b610c5159ddbe..c1a401971c2d4b170a69e381207c43ec38dfc85d 100644 (file)
@@ -166,6 +166,10 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
        if (1 < opts.merge + opts.reset + prefix_set)
                die("Which one? -m, --reset, or --prefix?");
 
+       /* Prefix should not start with a directory separator */
+       if (opts.prefix && opts.prefix[0] == '/')
+               die("Invalid prefix, prefix cannot start with '/'");
+
        if (opts.reset)
                opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
 
index e0db2066f3194b7084f25ea249156c30711f3e2b..c860c08ecb46a37a6f20bf403963afd7576fed9f 100755 (executable)
@@ -25,4 +25,14 @@ test_expect_success 'read-tree --prefix' '
        cmp expect actual
 '
 
+test_expect_success 'read-tree --prefix with leading slash exits with error' '
+       git rm -rf . &&
+       test_must_fail git read-tree --prefix=/two/ $tree &&
+       git read-tree --prefix=two/ $tree &&
+
+       git rm -rf . &&
+       test_must_fail git read-tree --prefix=/ $tree &&
+       git read-tree --prefix= $tree
+'
+
 test_done