Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Stocker2013-07-08 04:51:52 -0400
committerRobin Stocker2013-07-08 04:53:50 -0400
commit56ee8117802a672e80011ee07ea6b253a5b7d0a9 (patch)
tree936beac19060d5089f9c277bdcfbea66dfe528f0
parent12326ddead2948f20fadcd495096997a941e648b (diff)
downloadjgit-56ee8117802a672e80011ee07ea6b253a5b7d0a9.tar.gz
jgit-56ee8117802a672e80011ee07ea6b253a5b7d0a9.tar.xz
jgit-56ee8117802a672e80011ee07ea6b253a5b7d0a9.zip
Fix bugs in TreeWalk#isPathSuffix used by PathSuffixFilter
* It didn't check the first character in the pattern due to a off-by-one error. Spotted by James Roper. * It returned true even when pattern was longer than current path, e.g. it returned that ".txt" is suffix of "txt". Bug: 411999 Change-Id: I9fbcd68a11fb57cc49956b70c387a47271a0424f Signed-off-by: Robin Stocker <robin@nibor.org>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java10
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java10
2 files changed, 17 insertions, 3 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java
index 3ec159198a..d871c5ec10 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java
@@ -82,6 +82,16 @@ public class PathSuffixFilterTest extends RepositoryTestCase {
assertEquals(expected, paths);
}
+ @Test
+ public void testEdgeCases() throws IOException {
+ ObjectId treeId = createTree("abc", "abcd", "bcd", "c");
+ assertEquals(new ArrayList<String>(), getMatchingPaths("xbcd", treeId));
+ assertEquals(new ArrayList<String>(), getMatchingPaths("abcx", treeId));
+ assertEquals(Arrays.asList("abcd"), getMatchingPaths("abcd", treeId));
+ assertEquals(Arrays.asList("abcd", "bcd"), getMatchingPaths("bcd", treeId));
+ assertEquals(Arrays.asList("abc", "c"), getMatchingPaths("c", treeId));
+ }
+
private ObjectId createTree(String... paths) throws IOException {
final ObjectInserter odi = db.newObjectInserter();
final DirCache dc = db.readDirCache();
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java
index 627df927b4..90850eafda 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java
@@ -835,13 +835,17 @@ public class TreeWalk {
final AbstractTreeIterator t = currentHead;
final byte[] c = t.path;
final int cLen = t.pathLen;
- int ci;
- for (ci = 1; ci < cLen && ci < pLen; ci++) {
- if (c[cLen-ci] != p[pLen-ci])
+ for (int i = 1; i <= pLen; i++) {
+ // Pattern longer than current path
+ if (i > cLen)
+ return false;
+ // Current path doesn't match pattern
+ if (c[cLen - i] != p[pLen - i])
return false;
}
+ // Whole pattern tested -> matches
return true;
}

Back to the top