Skip to content

Commit 54be8e8

Browse files
committed
sparse-checkout: escape all glob characters on write
The sparse-checkout patterns allow special globs according to fnmatch(3). When writing cone-mode patterns for paths containing these characters, they must be escaped. Use is_glob_special() to check which characters must be escaped this way, and add a path to the tests that contains all glob characters at once. Note that ']' is not special, since the initial bracket '[' is escaped. Reported-by: Jeff King <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
1 parent e2c6f85 commit 54be8e8

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

builtin/sparse-checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static char *escaped_pattern(char *pattern)
149149
struct strbuf final = STRBUF_INIT;
150150

151151
while (*p) {
152-
if (*p == '*' || *p == '\\')
152+
if (is_glob_special(*p))
153153
strbuf_addch(&final, '\\');
154154

155155
strbuf_addch(&final, *p);

t/t1091-sparse-checkout-builtin.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,21 @@ test_expect_success 'pattern-checks: contained glob characters' '
381381
done
382382
'
383383

384-
test_expect_success BSLASHPSPEC 'pattern-checks: escaped "*"' '
384+
test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
385385
git clone repo escaped &&
386386
TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
387387
NEWTREE=$(git -C escaped mktree <<-EOF
388388
$(git -C escaped ls-tree HEAD)
389389
040000 tree $TREEOID zbad\\dir
390390
040000 tree $TREEOID zdoes*exist
391+
040000 tree $TREEOID zglob[!a]?
391392
EOF
392393
) &&
393394
COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
394395
git -C escaped reset --hard $COMMIT &&
395-
check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" &&
396+
check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
396397
git -C escaped sparse-checkout init --cone &&
397-
git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" &&
398+
git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
398399
cat >expect <<-\EOF &&
399400
/*
400401
!/*/
@@ -403,9 +404,10 @@ test_expect_success BSLASHPSPEC 'pattern-checks: escaped "*"' '
403404
/zbad\\dir/bogus/
404405
/zdoes\*exist/
405406
/zdoes\*not\*exist/
407+
/zglob\[!a]\?/
406408
EOF
407409
test_cmp expect escaped/.git/info/sparse-checkout &&
408-
check_read_tree_errors escaped "a zbad\\dir zdoes*exist" &&
410+
check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
409411
git -C escaped ls-tree -d --name-only HEAD >list-expect &&
410412
git -C escaped sparse-checkout set --stdin <list-expect &&
411413
cat >expect <<-\EOF &&
@@ -416,9 +418,10 @@ test_expect_success BSLASHPSPEC 'pattern-checks: escaped "*"' '
416418
/folder2/
417419
/zbad\\dir/
418420
/zdoes\*exist/
421+
/zglob\[!a]\?/
419422
EOF
420423
test_cmp expect escaped/.git/info/sparse-checkout &&
421-
check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" &&
424+
check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
422425
git -C escaped sparse-checkout list >list-actual &&
423426
test_cmp list-expect list-actual
424427
'

0 commit comments

Comments
 (0)