Skip to content

Commit cc89331

Browse files
vdyegitster
authored andcommitted
read-tree: explicitly disallow prefixes with a leading '/'
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 <[email protected]> Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c521b0 commit cc89331

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

builtin/read-tree.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
166166
if (1 < opts.merge + opts.reset + prefix_set)
167167
die("Which one? -m, --reset, or --prefix?");
168168

169+
/* Prefix should not start with a directory separator */
170+
if (opts.prefix && opts.prefix[0] == '/')
171+
die("Invalid prefix, prefix cannot start with '/'");
172+
169173
if (opts.reset)
170174
opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
171175

t/t1003-read-tree-prefix.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,14 @@ test_expect_success 'read-tree --prefix' '
2525
cmp expect actual
2626
'
2727

28+
test_expect_success 'read-tree --prefix with leading slash exits with error' '
29+
git rm -rf . &&
30+
test_must_fail git read-tree --prefix=/two/ $tree &&
31+
git read-tree --prefix=two/ $tree &&
32+
33+
git rm -rf . &&
34+
test_must_fail git read-tree --prefix=/ $tree &&
35+
git read-tree --prefix= $tree
36+
'
37+
2838
test_done

0 commit comments

Comments
 (0)