Commit 9aa049d4c1578cfd73ce66801fe2575632296060

Patrick Steinhardt 2019-03-29T13:28:59

Merge pull request #5020 from implausible/fix/gitignore-negation Negation of subdir ignore causes other subdirs to be unignored

diff --git a/src/attr_file.c b/src/attr_file.c
index 40c72ea..8619647 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -429,18 +429,6 @@ bool git_attr_fnmatch__match(
 		return (p_fnmatch(match->pattern, relpath, flags) != FNM_NOMATCH);
 	}
 
-	/* if path is a directory prefix of a negated pattern, then match */
-	if ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) && path->is_dir) {
-		size_t pathlen = strlen(relpath);
-		bool prefixed = (pathlen <= match->length) &&
-			((match->flags & GIT_ATTR_FNMATCH_ICASE) ?
-			!strncasecmp(match->pattern, relpath, pathlen) :
-			!strncmp(match->pattern, relpath, pathlen));
-
-		if (prefixed && git_path_at_end_of_segment(&match->pattern[pathlen]))
-			return true;
-	}
-
 	return (p_fnmatch(match->pattern, filename, flags) != FNM_NOMATCH);
 }
 
diff --git a/tests/attr/ignore.c b/tests/attr/ignore.c
index 165e2ba..110304a 100644
--- a/tests/attr/ignore.c
+++ b/tests/attr/ignore.c
@@ -372,3 +372,28 @@ void test_attr_ignore__case_sensitive_unignore_does_nothing(void)
 
 	assert_is_ignored(true, "case/file");
 }
+
+void test_attr_ignore__ignored_subdirfiles_with_subdir_rule(void)
+{
+	cl_git_rewritefile(
+		"attr/.gitignore",
+		"dir/*\n"
+		"!dir/sub1/sub2/**\n");
+
+	assert_is_ignored(true, "dir/a.test");
+	assert_is_ignored(true, "dir/sub1/a.test");
+	assert_is_ignored(true, "dir/sub1/sub2");
+}
+
+void test_attr_ignore__ignored_subdirfiles_with_negations(void)
+{
+	cl_git_rewritefile(
+		"attr/.gitignore",
+		"dir/*\n"
+		"!dir/a.test\n");
+
+	assert_is_ignored(false, "dir/a.test");
+	assert_is_ignored(true, "dir/b.test");
+	assert_is_ignored(true, "dir/sub1/c.test");
+}
+