Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix toplevel wildcard matching #39

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading