sparse-checkout: properly match escaped characters
In cone mode, the sparse-checkout feature uses hashset containment queries to match paths. Make this algorithm respect escaped asterisk (*) and backslash (\) characters. Create dup_and_filter_pattern() method to convert a pattern by removing escape characters and dropping an optional "/*" at the end. This method is available in dir.h as we will use it in builtin/sparse-checkout.c in a later change. 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
4f52c2ce6c578896964e960f6017510f0efd3f46
2 files changed
+51
-7
dir.c
+32
-3
@@ -630,6 +630,36 @@ int pl_hashmap_cmp(const void *unused_cmp_data,
630
return strncmp(ee1->pattern, ee2->pattern, min_len);
631
}
632
633
+static char *dup_and_filter_pattern(const char *pattern)
634
+{
635
+ char *set, *read;
636
+ size_t count = 0;
637
+ char *result = xstrdup(pattern);
638
+
639
+ set = result;
640
+ read = result;
641
+
642
+ while (*read) {
643
+ /* skip escape characters (once) */
644
+ if (*read == '\\')
645
+ read++;
646
+
647
+ *set = *read;
648
+
649
+ set++;
650
+ read++;
651
+ count++;
652
+ }
653
+ *set = 0;
654
+
655
+ if (count > 2 &&
656
+ *(set - 1) == '*' &&
657
+ *(set - 2) == '/')
658
+ *(set - 2) = 0;
659
+
660
+ return result;
661
+}
662
+
663
static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern *given)
664
{
665
struct pattern_entry *translated;
@@ -702,8 +732,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
732
goto clear_hashmaps;
733
}
734
705
- truncated = xstrdup(given->pattern);
706
- truncated[given->patternlen - 2] = 0;
735
+ truncated = dup_and_filter_pattern(given->pattern);
736
737
translated = xmalloc(sizeof(struct pattern_entry));
738
translated->pattern = truncated;
@@ -737,7 +766,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
766
767
translated = xmalloc(sizeof(struct pattern_entry));
768
740
- translated->pattern = xstrdup(given->pattern);
769
+ translated->pattern = dup_and_filter_pattern(given->pattern);
770
translated->patternlen = given->patternlen;
771
hashmap_entry_init(&translated->ent,
772
ignore_case ?
t/t1091-sparse-checkout-builtin.sh
+19
-4
@@ -378,13 +378,28 @@ test_expect_success 'pattern-checks: contained glob characters' '
378
done
379
'
380
381
-test_expect_success 'pattern-checks: escaped "*"' '
382
- cat >repo/.git/info/sparse-checkout <<-\EOF &&
381
+test_expect_success BSLASHPSPEC 'pattern-checks: escaped "*"' '
382
+ git clone repo escaped &&
383
+ TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
384
+ NEWTREE=$(git -C escaped mktree <<-EOF
385
+ $(git -C escaped ls-tree HEAD)
386
+ 040000 tree $TREEOID zbad\\dir
387
+ 040000 tree $TREEOID zdoes*exist
388
+ EOF
389
+ ) &&
390
+ COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
391
+ git -C escaped reset --hard $COMMIT &&
392
+ check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" &&
393
+ git -C escaped sparse-checkout init --cone &&
394
+ cat >escaped/.git/info/sparse-checkout <<-\EOF &&
395
/*
396
!/*/
385
- /does\*not\*exist/
397
+ /zbad\\dir/
398
+ !/zbad\\dir/*/
399
+ /zdoes\*not\*exist/
400
+ /zdoes\*exist/
401
EOF
387
- check_read_tree_errors repo "a" ""
402
+ check_read_tree_errors escaped "a zbad\\dir zdoes*exist"
403
'
404
405
test_done