Skip to content

Commit e3d7bcc

Browse files
committed
ignore: Do not match on prefix of negated patterns
Matching on the prefix of a negated pattern was triggering false negatives on siblings of that pattern. e.g. Given the .gitignore: dir/* !dir/sub1/sub2/** The path `dir/a.text` would not be ignored.
1 parent bc74c53 commit e3d7bcc

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/attr_file.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,6 @@ bool git_attr_fnmatch__match(
429429
return (p_fnmatch(match->pattern, relpath, flags) != FNM_NOMATCH);
430430
}
431431

432-
/* if path is a directory prefix of a negated pattern, then match */
433-
if ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) && path->is_dir) {
434-
size_t pathlen = strlen(relpath);
435-
bool prefixed = (pathlen <= match->length) &&
436-
((match->flags & GIT_ATTR_FNMATCH_ICASE) ?
437-
!strncasecmp(match->pattern, relpath, pathlen) :
438-
!strncmp(match->pattern, relpath, pathlen));
439-
440-
if (prefixed && git_path_at_end_of_segment(&match->pattern[pathlen]))
441-
return true;
442-
}
443-
444432
return (p_fnmatch(match->pattern, filename, flags) != FNM_NOMATCH);
445433
}
446434

tests/status/ignore.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,16 +1218,36 @@ void test_status_ignore__ignored_subdirfiles_with_subdir_rule(void)
12181218
{
12191219
static const char *test_files[] = {
12201220
"empty_standard_repo/dir/a.test",
1221-
"empty_standard_repo/dir/sub1/b.test",
1221+
"empty_standard_repo/dir/sub1/sub2/b.test",
12221222
NULL
12231223
};
12241224

12251225
make_test_data("empty_standard_repo", test_files);
12261226
cl_git_mkfile(
12271227
"empty_standard_repo/.gitignore",
1228-
"/dir/*\n"
1229-
"!/dir/sub1/sub2/**/*\n");
1228+
"dir/*\n"
1229+
"!dir/sub1/sub2/**\n");
12301230

12311231
assert_is_ignored("dir/a.test");
12321232
assert_is_ignored("dir/sub1/a.test");
12331233
}
1234+
1235+
void test_status_ignore__ignored_subdirfiles_with_negations(void)
1236+
{
1237+
static const char *test_files[] = {
1238+
"empty_standard_repo/dir/a.test",
1239+
"empty_standard_repo/dir/b.test",
1240+
"empty_standard_repo/dir/sub1/c.test",
1241+
NULL
1242+
};
1243+
1244+
make_test_data("empty_standard_repo", test_files);
1245+
cl_git_mkfile(
1246+
"empty_standard_repo/.gitignore",
1247+
"dir/*\n"
1248+
"!dir/a.test\n");
1249+
1250+
refute_is_ignored("dir/a.test");
1251+
assert_is_ignored("dir/b.test");
1252+
assert_is_ignored("dir/sub1/c.test");
1253+
}

0 commit comments

Comments
 (0)