]> source.dussan.org Git - jgit.git/commitdiff
Fix matching ignores and attributes pattern of form a/b/**. 50/100550/8
authorDmitry Pavlenko <pavlenko@tmatesoft.com>
Mon, 3 Jul 2017 12:08:15 +0000 (14:08 +0200)
committerDavid Pursehouse <david.pursehouse@gmail.com>
Mon, 24 Jul 2017 08:16:33 +0000 (09:16 +0100)
Fix patch matching for patterns of form a/b/** : this should not match
paths like a/b but still match a/b/ and a/b/c.

Change-Id: Iacbf496a43f01312e7d9052f29c3f9c33807c85d
Signed-off-by: Dmitry Pavlenko <pavlenko@tmatesoft.com>
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/FastIgnoreRuleTest.java
org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java

index ec2370e67fa7301ee119a069a62122a29aff736a..f0d3c3690fe0be5a4eab016c8635022167745859 100644 (file)
@@ -166,6 +166,25 @@ public class AttributesNodeTest {
                assertAttribute("file.type3", node, asSet(A_UNSET_ATTR, B_SET_ATTR));
        }
 
+       @Test
+       public void testDoubleAsteriskAtEnd() throws IOException {
+               String attributeFileContent = "dir/** \tA -B\tC=value";
+
+               is = new ByteArrayInputStream(attributeFileContent.getBytes());
+               AttributesNode node = new AttributesNode();
+               node.parse(is);
+               assertAttribute("dir", node,
+                               asSet(new Attribute[]{}));
+               assertAttribute("dir/", node,
+                               asSet(new Attribute[]{}));
+               assertAttribute("dir/file.type1", node,
+                               asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
+               assertAttribute("dir/sub/", node,
+                               asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
+               assertAttribute("dir/sub/file.type1", node,
+                               asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
+       }
+
        private void assertAttribute(String path, AttributesNode node,
                        Attributes attrs) throws IOException {
                Attributes attributes = new Attributes();
index 1863b803216b385e0080ed20b6043ec9c2af30e1..bcc8f7e47f70068738088bb67673aa2549fb65cc 100644 (file)
@@ -391,7 +391,6 @@ public class FastIgnoreRuleTest {
                assertMatched("/**/a/b", "c/d/a/b");
                assertMatched("/**/**/a/b", "c/d/a/b");
 
-               assertMatched("a/b/**", "a/b");
                assertMatched("a/b/**", "a/b/c");
                assertMatched("a/b/**", "a/b/c/d/");
                assertMatched("a/b/**/**", "a/b/c/d");
@@ -415,6 +414,12 @@ public class FastIgnoreRuleTest {
 
        @Test
        public void testWildmatchDoNotMatch() {
+               assertNotMatched("a/**", "a/");
+               assertNotMatched("a/b/**", "a/b/");
+               assertNotMatched("a/**", "a");
+               assertNotMatched("a/b/**", "a/b");
+               assertNotMatched("a/b/**/", "a/b");
+               assertNotMatched("a/b/**/**", "a/b");
                assertNotMatched("**/a/b", "a/c/b");
                assertNotMatched("!/**/*.zip", "c/a/b.zip");
                assertNotMatched("!**/*.zip", "c/a/b.zip");
index 65224eab91c14cbb65d3fab1fbeaef2bc335b5c8..ce9ad80fb00763c904bd2300465e73901f48eadc 100644 (file)
@@ -227,29 +227,30 @@ public class PathMatcher extends AbstractMatcher {
                        int left = right;
                        right = path.indexOf(slash, right);
                        if (right == -1) {
-                               if (left < endExcl)
+                               if (left < endExcl) {
                                        match = matches(matcher, path, left, endExcl,
                                                        assumeDirectory);
+                               } else {
+                                       // a/** should not match a/ or a
+                                       match = match && matchers.get(matcher) != WILD;
+                               }
                                if (match) {
-                                       if (matcher == matchers.size() - 2
-                                                       && matchers.get(matcher + 1) == WILD)
-                                               // ** can match *nothing*: a/b/** match also a/b
-                                               return true;
                                        if (matcher < matchers.size() - 1
                                                        && matchers.get(matcher) == WILD) {
                                                // ** can match *nothing*: a/**/b match also a/b
                                                matcher++;
                                                match = matches(matcher, path, left, endExcl,
                                                                assumeDirectory);
-                                       } else if (dirOnly && !assumeDirectory)
+                                       } else if (dirOnly && !assumeDirectory) {
                                                // Directory expectations not met
                                                return false;
+                                       }
                                }
                                return match && matcher + 1 == matchers.size();
                        }
-                       if (right - left > 0)
+                       if (right - left > 0) {
                                match = matches(matcher, path, left, right, assumeDirectory);
-                       else {
+                       else {
                                // path starts with slash???
                                right++;
                                continue;
@@ -261,12 +262,14 @@ public class PathMatcher extends AbstractMatcher {
                                        right = left - 1;
                                }
                                matcher++;
-                               if (matcher == matchers.size())
+                               if (matcher == matchers.size()) {
                                        return true;
-                       } else if (lastWildmatch != -1)
+                               }
+                       } else if (lastWildmatch != -1) {
                                matcher = lastWildmatch + 1;
-                       else
+                       } else {
                                return false;
+                       }
                        right++;
                }
        }