Skip to content

Commit

Permalink
Fix toplevel wildcard matching
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbartschat authored Feb 6, 2024
1 parent db3b60a commit 2e07ab3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/libgit2/sparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ static bool pattern_matches_path(git_attr_fnmatch *match, git_attr_path *path, s

if (HAS_FLAG(match, GIT_ATTR_FNMATCH_HASWILD)) {
expected_extra_nesting = true;
if (match->length <= 1) {
// Top level wildcard always matches
return true;
}
exact_match_length = match->length - 2; // Cut off the trailing "/*"
}

Expand Down Expand Up @@ -183,7 +187,7 @@ static int parse_sparse_file(
bool matched = false;
size_t k;
git_attr_fnmatch *parent_match;
git_vector_foreach(&attrs->rules, k, parent_match) {
git_vector_rforeach(&attrs->rules, k, parent_match) {
if (pattern_matches_path(parent_match, &parent_path, parent_length)) {
matched = !HAS_FLAG(parent_match, GIT_ATTR_FNMATCH_NEGATIVE);
break;
Expand Down
28 changes: 27 additions & 1 deletion tests/libgit2/sparse/paths.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ void test_sparse_paths__check_toplevel(void)
}
}

void test_sparse_paths__check_toplevelwildcard(void)
{
git_sparse_checkout_init_options scopts = GIT_SPARSE_CHECKOUT_INIT_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("sparse");

cl_git_pass(git_sparse_checkout_init(g_repo, &scopts));
{
char *pattern_strings[] = {"/*"};
git_strarray patterns = { pattern_strings, ARRAY_SIZE(pattern_strings) };
cl_git_pass(git_sparse_checkout_add(g_repo, &patterns));
}

char *matches[] = {
"_", // Even with no include patterns, toplevel files are included.
"A/",
"A/_",
};

size_t j;
for ( j = 0; j < ARRAY_SIZE(matches); j++) {
assert_is_checkout(matches[j]);
}

}

void test_sparse_paths__validate_cone(void)
{
size_t i;
Expand Down Expand Up @@ -160,7 +185,8 @@ void test_sparse_paths__validate_cone(void)
char *missing_parent_patterns[] = {
"/A/B/",
"/A/B/C/",
"/*\n!/A/B/*/\n/A/B/C/D/"
"/*\n!/A/B/*/\n/A/B/C/D/",
"/A/\n!/A/B/*/\n/A/B/C/D/"
};

for (i = 0; i < ARRAY_SIZE(good_patterns); i++) {
Expand Down

0 comments on commit 2e07ab3

Please sign in to comment.