Browse Source

Merge "Fix bugs in TreeWalk#isPathSuffix used by PathSuffixFilter"

stable-3.1
Christian Halstrick 11 years ago committed by Gerrit Code Review @ Eclipse.org
parent
commit
de00ec94d8
  1. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java
  2. 10
      org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java

10
org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/filter/PathSuffixFilterTest.java

@ -82,6 +82,16 @@ public class PathSuffixFilterTest extends RepositoryTestCase {
assertEquals(expected, paths); 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 { private ObjectId createTree(String... paths) throws IOException {
final ObjectInserter odi = db.newObjectInserter(); final ObjectInserter odi = db.newObjectInserter();
final DirCache dc = db.readDirCache(); final DirCache dc = db.readDirCache();

10
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java

@ -835,13 +835,17 @@ public class TreeWalk {
final AbstractTreeIterator t = currentHead; final AbstractTreeIterator t = currentHead;
final byte[] c = t.path; final byte[] c = t.path;
final int cLen = t.pathLen; final int cLen = t.pathLen;
int ci;
for (ci = 1; ci < cLen && ci < pLen; ci++) { for (int i = 1; i <= pLen; i++) {
if (c[cLen-ci] != p[pLen-ci]) // 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; return false;
} }
// Whole pattern tested -> matches
return true; return true;
} }

Loading…
Cancel
Save