diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java index 2cdaea28d..209b2b837 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java @@ -197,6 +197,31 @@ public class FileTreeIteratorTest extends RepositoryTestCase { assertEquals(expect, top.getEntryObjectId()); } + @Test + public void testDirCacheMatchingId() throws Exception { + File f = writeTrashFile("file", "content"); + Git git = new Git(db); + writeTrashFile("file", "content"); + fsTick(f); + git.add().addFilepattern("file").call(); + DirCacheEntry dce = db.readDirCache().getEntry("file"); + TreeWalk tw = new TreeWalk(db); + FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db + .getConfig().get(WorkingTreeOptions.KEY)); + tw.addTree(fti); + DirCacheIterator dci = new DirCacheIterator(db.readDirCache()); + tw.addTree(dci); + fti.setDirCacheIterator(tw, 1); + while (tw.next() && !tw.getPathString().equals("file")) { + // + } + assertEquals(MetadataDiff.EQUAL, fti.compareMetadata(dce)); + ObjectId fromRaw = ObjectId.fromRaw(fti.idBuffer(), fti.idOffset()); + assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea", + fromRaw.getName()); + assertFalse(fti.isModified(dce, false)); + } + @Test public void testIsModifiedSymlink() throws Exception { File f = writeTrashFile("symlink", "content"); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java index ebe9f73fb..8d0555bf9 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/WorkingTreeIterator.java @@ -131,6 +131,9 @@ public abstract class WorkingTreeIterator extends AbstractTreeIterator { /** Cached canonical length, initialized from {@link #idBuffer()} */ private long canonLen = -1; + /** The offset of the content id in {@link #idBuffer()} */ + private int contentIdOffset; + /** * Create a new iterator with no parent. * @@ -234,11 +237,14 @@ public abstract class WorkingTreeIterator extends AbstractTreeIterator { DirCacheIterator.class); if (i != null) { DirCacheEntry ent = i.getDirCacheEntry(); - if (ent != null && compareMetadata(ent) == MetadataDiff.EQUAL) - return i.idBuffer(); + if (ent != null && compareMetadata(ent) == MetadataDiff.EQUAL) { + contentIdOffset = i.idOffset(); + contentIdFromPtr = ptr; + return contentId = i.idBuffer(); + } + contentIdOffset = 0; } } - switch (mode & FileMode.TYPE_MASK) { case FileMode.TYPE_FILE: contentIdFromPtr = ptr; @@ -427,7 +433,7 @@ public abstract class WorkingTreeIterator extends AbstractTreeIterator { @Override public int idOffset() { - return 0; + return contentIdOffset; } @Override