From 49cb6ba5dd337dfdc9302bb33248e8448f530d3d Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Sat, 17 Feb 2018 13:20:57 +0100 Subject: PathMatcher: fix handling of **/ **/ should match only directories, but not files Change-Id: I885c83e5912cac5bff338ba657faf6bb9ec94064 Signed-off-by: Marc Strapetz --- .../jgit/attributes/CGitAttributesTest.java | 28 +++++++++++++ .../org/eclipse/jgit/ignore/CGitIgnoreTest.java | 28 +++++++++++++ .../eclipse/jgit/ignore/FastIgnoreRuleTest.java | 49 +++++++++++++++++++++- 3 files changed, 104 insertions(+), 1 deletion(-) (limited to 'org.eclipse.jgit.test/tst') diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/CGitAttributesTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/CGitAttributesTest.java index 34838138e3..344d1af6a0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/CGitAttributesTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/CGitAttributesTest.java @@ -365,6 +365,34 @@ public class CGitAttributesTest extends RepositoryTestCase { assertSameAsCGit(); } + @Test + public void testDirectoryWildmatchDoesNotMatchFiles1() throws Exception { + createFiles("a", "dir/b", "dir/sub/c"); + writeTrashFile(".gitattributes", "**/ bar\n"); + assertSameAsCGit(); + } + + @Test + public void testDirectoryWildmatchDoesNotMatchFiles2() throws Exception { + createFiles("a", "dir/b", "dir/sub/c"); + writeTrashFile(".gitattributes", "**/**/ bar\n"); + assertSameAsCGit(); + } + + @Test + public void testDirectoryWildmatchDoesNotMatchFiles3() throws Exception { + createFiles("a", "x/b", "sub/x/c", "sub/x/d/e"); + writeTrashFile(".gitattributes", "x/**/ bar\n"); + assertSameAsCGit(); + } + + @Test + public void testDirectoryWildmatchDoesNotMatchFiles4() throws Exception { + createFiles("a", "dir/x", "dir/sub1/x", "dir/sub2/x/y"); + writeTrashFile(".gitattributes", "x/**/ bar\n"); + assertSameAsCGit(); + } + @Test public void testDirectoryMatchSubComplex() throws Exception { createFiles("src/new/foo.txt", "foo/src/new/foo.txt", "sub/src/new"); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/CGitIgnoreTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/CGitIgnoreTest.java index aa42e4943b..155bd27303 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/CGitIgnoreTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/CGitIgnoreTest.java @@ -266,6 +266,34 @@ public class CGitIgnoreTest extends RepositoryTestCase { assertSameAsCGit(); } + @Test + public void testDirectoryWildmatchDoesNotMatchFiles1() throws Exception { + createFiles("a", "dir/b", "dir/sub/c"); + writeTrashFile(".gitignore", "**/\n"); + assertSameAsCGit(); + } + + @Test + public void testDirectoryWildmatchDoesNotMatchFiles2() throws Exception { + createFiles("a", "dir/b", "dir/sub/c"); + writeTrashFile(".gitignore", "**/**/\n"); + assertSameAsCGit(); + } + + @Test + public void testDirectoryWildmatchDoesNotMatchFiles3() throws Exception { + createFiles("a", "x/b", "sub/x/c", "sub/x/d/e"); + writeTrashFile(".gitignore", "x/**/\n"); + assertSameAsCGit(); + } + + @Test + public void testDirectoryWildmatchDoesNotMatchFiles4() throws Exception { + createFiles("a", "dir/x", "dir/sub1/x", "dir/sub2/x/y"); + writeTrashFile(".gitignore", "**/x/\n"); + assertSameAsCGit(); + } + @Test public void testUnescapedBracketsInGroup() throws Exception { createFiles("[", "]", "[]", "][", "[[]", "[]]", "[[]]"); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/FastIgnoreRuleTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/FastIgnoreRuleTest.java index 06164c8a91..2a1721e66c 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/FastIgnoreRuleTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/FastIgnoreRuleTest.java @@ -48,10 +48,18 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.junit.Before; import org.junit.Test; public class FastIgnoreRuleTest { + private boolean pathMatch; + + @Before + public void setup() { + pathMatch = false; + } + @Test public void testSimpleCharClass() { assertMatched("][a]", "]a"); @@ -410,6 +418,19 @@ public class FastIgnoreRuleTest { assertMatched("a/**/b/**/c", "a/c/b/d/c"); assertMatched("a/**/**/b/**/**/c", "a/c/b/d/c"); + + assertMatched("**/", "a/"); + assertMatched("**/", "a/b"); + assertMatched("**/", "a/b/c"); + assertMatched("**/**/", "a/"); + assertMatched("**/**/", "a/b"); + assertMatched("**/**/", "a/b/"); + assertMatched("**/**/", "a/b/c"); + assertMatched("x/**/", "x/a/"); + assertMatched("x/**/", "x/a/b"); + assertMatched("x/**/", "x/a/b/"); + assertMatched("**/x/", "a/x/"); + assertMatched("**/x/", "a/b/x/"); } @Test @@ -424,6 +445,10 @@ public class FastIgnoreRuleTest { assertNotMatched("!/**/*.zip", "c/a/b.zip"); assertNotMatched("!**/*.zip", "c/a/b.zip"); assertNotMatched("a/**/b", "a/c/bb"); + + assertNotMatched("**/", "a"); + assertNotMatched("**/**/", "a"); + assertNotMatched("**/x/", "a/b/x"); } @SuppressWarnings("unused") @@ -465,6 +490,28 @@ public class FastIgnoreRuleTest { split("/a/b/c/", '/').toArray()); } + @Test + public void testPathMatch() { + pathMatch = true; + assertMatched("a", "a"); + assertMatched("a/", "a/"); + assertNotMatched("a/", "a/b"); + + assertMatched("**", "a"); + assertMatched("**", "a/"); + assertMatched("**", "a/b"); + + assertNotMatched("**/", "a"); + assertNotMatched("**/", "a/b"); + assertMatched("**/", "a/"); + assertMatched("**/", "a/b/"); + + assertNotMatched("x/**/", "x/a"); + assertNotMatched("x/**/", "x/a/b"); + assertMatched("x/**/", "x/a/"); + assertMatched("x/**/", "x/y/a/"); + } + private void assertMatched(String pattern, String path) { boolean match = match(pattern, path); String result = path + " is " + (match ? "ignored" : "not ignored") @@ -520,7 +567,7 @@ public class FastIgnoreRuleTest { FastIgnoreRule r = new FastIgnoreRule(pattern); // If speed of this test is ever an issue, we can use a presetRule field // to avoid recompiling a pattern each time. - boolean match = r.isMatch(target, isDirectory); + boolean match = r.isMatch(target, isDirectory, pathMatch); if (r.getNegation()) match = !match; return match; -- cgit v1.2.3