Skip to content

Commit 4f52c2c

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9abc60f commit 4f52c2c

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

dir.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,36 @@ int pl_hashmap_cmp(const void *unused_cmp_data,
630630
return strncmp(ee1->pattern, ee2->pattern, min_len);
631631
}
632632

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+
633663
static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern *given)
634664
{
635665
struct pattern_entry *translated;
@@ -702,8 +732,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
702732
goto clear_hashmaps;
703733
}
704734

705-
truncated = xstrdup(given->pattern);
706-
truncated[given->patternlen - 2] = 0;
735+
truncated = dup_and_filter_pattern(given->pattern);
707736

708737
translated = xmalloc(sizeof(struct pattern_entry));
709738
translated->pattern = truncated;
@@ -737,7 +766,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
737766

738767
translated = xmalloc(sizeof(struct pattern_entry));
739768

740-
translated->pattern = xstrdup(given->pattern);
769+
translated->pattern = dup_and_filter_pattern(given->pattern);
741770
translated->patternlen = given->patternlen;
742771
hashmap_entry_init(&translated->ent,
743772
ignore_case ?

t/t1091-sparse-checkout-builtin.sh

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,28 @@ test_expect_success 'pattern-checks: contained glob characters' '
378378
done
379379
'
380380

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 &&
383395
/*
384396
!/*/
385-
/does\*not\*exist/
397+
/zbad\\dir/
398+
!/zbad\\dir/*/
399+
/zdoes\*not\*exist/
400+
/zdoes\*exist/
386401
EOF
387-
check_read_tree_errors repo "a" ""
402+
check_read_tree_errors escaped "a zbad\\dir zdoes*exist"
388403
'
389404

390405
test_done

0 commit comments

Comments
 (0)