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>
This commit is contained in:
Derrick Stolee 2020-01-31 20:16:09 +00:00 committed by Junio C Hamano
parent 9abc60f801
commit 4f52c2ce6c
2 changed files with 51 additions and 7 deletions

35
dir.c
View File

@ -630,6 +630,36 @@ int pl_hashmap_cmp(const void *unused_cmp_data,
return strncmp(ee1->pattern, ee2->pattern, min_len); return strncmp(ee1->pattern, ee2->pattern, min_len);
} }
static char *dup_and_filter_pattern(const char *pattern)
{
char *set, *read;
size_t count = 0;
char *result = xstrdup(pattern);
set = result;
read = result;
while (*read) {
/* skip escape characters (once) */
if (*read == '\\')
read++;
*set = *read;
set++;
read++;
count++;
}
*set = 0;
if (count > 2 &&
*(set - 1) == '*' &&
*(set - 2) == '/')
*(set - 2) = 0;
return result;
}
static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern *given) static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern *given)
{ {
struct pattern_entry *translated; struct pattern_entry *translated;
@ -702,8 +732,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
goto clear_hashmaps; goto clear_hashmaps;
} }
truncated = xstrdup(given->pattern); truncated = dup_and_filter_pattern(given->pattern);
truncated[given->patternlen - 2] = 0;
translated = xmalloc(sizeof(struct pattern_entry)); translated = xmalloc(sizeof(struct pattern_entry));
translated->pattern = truncated; translated->pattern = truncated;
@ -737,7 +766,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
translated = xmalloc(sizeof(struct pattern_entry)); translated = xmalloc(sizeof(struct pattern_entry));
translated->pattern = xstrdup(given->pattern); translated->pattern = dup_and_filter_pattern(given->pattern);
translated->patternlen = given->patternlen; translated->patternlen = given->patternlen;
hashmap_entry_init(&translated->ent, hashmap_entry_init(&translated->ent,
ignore_case ? ignore_case ?

View File

@ -378,13 +378,28 @@ test_expect_success 'pattern-checks: contained glob characters' '
done done
' '
test_expect_success 'pattern-checks: escaped "*"' ' test_expect_success BSLASHPSPEC 'pattern-checks: escaped "*"' '
cat >repo/.git/info/sparse-checkout <<-\EOF && git clone repo escaped &&
TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
NEWTREE=$(git -C escaped mktree <<-EOF
$(git -C escaped ls-tree HEAD)
040000 tree $TREEOID zbad\\dir
040000 tree $TREEOID zdoes*exist
EOF
) &&
COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
git -C escaped reset --hard $COMMIT &&
check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" &&
git -C escaped sparse-checkout init --cone &&
cat >escaped/.git/info/sparse-checkout <<-\EOF &&
/* /*
!/*/ !/*/
/does\*not\*exist/ /zbad\\dir/
!/zbad\\dir/*/
/zdoes\*not\*exist/
/zdoes\*exist/
EOF EOF
check_read_tree_errors repo "a" "" check_read_tree_errors escaped "a zbad\\dir zdoes*exist"
' '
test_done test_done