diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/dircache.testRemovedSubtree b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/dircache.testRemovedSubtree new file mode 100644 index 000000000..01aa24d11 Binary files /dev/null and b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/test/resources/dircache.testRemovedSubtree differ diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java index 99acd528c..bc84753f9 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheIteratorTest.java @@ -43,12 +43,15 @@ package org.eclipse.jgit.dircache; +import java.io.File; import java.util.Collections; import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.PathFilterGroup; +import org.eclipse.jgit.util.FS; +import org.eclipse.jgit.util.JGitTestUtil; public class DirCacheIteratorTest extends RepositoryTestCase { public void testEmptyTree_NoTreeWalk() throws Exception { @@ -269,4 +272,26 @@ public class DirCacheIteratorTest extends RepositoryTestCase { assertFalse(tw.next()); } } + + public void testRemovedSubtree() throws Exception { + final File path = JGitTestUtil + .getTestResourceFile("dircache.testRemovedSubtree"); + + final DirCache dc = DirCache.read(path, FS.DETECTED); + assertEquals(2, dc.getEntryCount()); + + final TreeWalk tw = new TreeWalk(db); + tw.setRecursive(true); + tw.addTree(new DirCacheIterator(dc)); + + assertTrue(tw.next()); + assertEquals("a/a", tw.getPathString()); + assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0)); + + assertTrue(tw.next()); + assertEquals("q", tw.getPathString()); + assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0)); + + assertFalse(tw.next()); + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheTree.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheTree.java index 2995e17c9..c71c91332 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheTree.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheTree.java @@ -484,14 +484,11 @@ public class DirCacheTree { stIdx++; } - if (stIdx < childCnt) { - // None of our remaining children can be in this tree - // as the current cache entry is after our own name. - // - final DirCacheTree[] dct = new DirCacheTree[stIdx]; - System.arraycopy(children, 0, dct, 0, stIdx); - children = dct; - } + // None of our remaining children can be in this tree + // as the current cache entry is after our own name. + // + while (stIdx < childCnt) + removeChild(childCnt - 1); } private void insertChild(final int stIdx, final DirCacheTree st) {