@matheusbsbs / matheus-bagliano / commits / e53ffe2704
Fork 0

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 <peff@peff.net> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jan 31, 2020 at 20:16 UTC e53ffe2704d7e10690f4382e46c1411a482531f1
2 files changed +9 -6
builtin/sparse-checkout.c
+1 -1
@@ -149,7 +149,7 @@ static char *escaped_pattern(char *pattern)
149 struct strbuf final = STRBUF_INIT;
150
151 while (*p) {
152 - if (*p == '*' || *p == '\\')
152 + if (is_glob_special(*p))
153 strbuf_addch(&final, '\\');
154
155 strbuf_addch(&final, *p);
t/t1091-sparse-checkout-builtin.sh
+8 -5
@@ -381,20 +381,21 @@ test_expect_success 'pattern-checks: contained glob characters' '
381 done
382 '
383
384 -test_expect_success BSLASHPSPEC 'pattern-checks: escaped "*"' '
384 +test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
385 git clone repo escaped &&
386 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
387 NEWTREE=$(git -C escaped mktree <<-EOF
388 $(git -C escaped ls-tree HEAD)
389 040000 tree $TREEOID zbad\\dir
390 040000 tree $TREEOID zdoes*exist
391 + 040000 tree $TREEOID zglob[!a]?
392 EOF
393 ) &&
394 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
395 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]? &&
397 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]?" &&
399 cat >expect <<-\EOF &&
400 /*
401 !/*/
@@ -403,9 +404,10 @@ test_expect_success BSLASHPSPEC 'pattern-checks: escaped "*"' '
404 /zbad\\dir/bogus/
405 /zdoes\*exist/
406 /zdoes\*not\*exist/
407 + /zglob\[!a]\?/
408 EOF
409 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]?" &&
411 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
412 git -C escaped sparse-checkout set --stdin <list-expect &&
413 cat >expect <<-\EOF &&
@@ -416,9 +418,10 @@ test_expect_success BSLASHPSPEC 'pattern-checks: escaped "*"' '
418 /folder2/
419 /zbad\\dir/
420 /zdoes\*exist/
421 + /zglob\[!a]\?/
422 EOF
423 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]? &&
425 git -C escaped sparse-checkout list >list-actual &&
426 test_cmp list-expect list-actual
427 '