{"thread":{"id":"14993","subject":"[JGIT PATCH] Fix TreeWalk to correctly handle missing subtrees","startedAt":"2008-08-13T21:42:22Z","lastAt":"2008-08-13T21:42:22Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"87112","messageId":"1218663742-1276-1-git-send-email-spearce@spearce.org","threadId":"14993","inReplyTo":null,"subject":"[JGIT PATCH] Fix TreeWalk to correctly handle missing subtrees","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-08-13T21:42:22Z","receivedAt":"2008-08-13T21:42:22Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"When we are walking through a pair of trees and one of the two trees has\nadded a new subtree in the middle we need to make sure we use the special\nEmptyTreeIterator as a placeholder for the other tree which is missing\nthat path entry.\n\nThis fixes the issue Robin noticed where pulling up the difference for\n\"55e9a699df...0f0ae68377 org.spearce.jgit.test\" showed files deleted\nby 0f0a when they were not even touched.  The addition of a new subtree\n(\"revwalk\") by 0f0a is what threw the iteration off, causing the other\ntree (55e9's) to enter into the wrong subtree at the wrong time.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n\n  This is on top of my misc-cleanup series.\n\n .../jgit/treewalk/TreeWalkBasicDiffTest.java       |  116 ++++++++++++++++++++\n .../src/org/spearce/jgit/treewalk/TreeWalk.java    |    3 +-\n 2 files changed, 118 insertions(+), 1 deletions(-)\n create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/TreeWalkBasicDiffTest.java\n\ndiff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/TreeWalkBasicDiffTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/TreeWalkBasicDiffTest.java\nnew file mode 100644\nindex 0000000..a25c273\n--- /dev/null\n+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/TreeWalkBasicDiffTest.java\n@@ -0,0 +1,116 @@\n+/*\n+ * Copyright (C) 2008, Google Inc.\n+ *\n+ * All rights reserved.\n+ *\n+ * Redistribution and use in source and binary forms, with or\n+ * without modification, are permitted provided that the following\n+ * conditions are met:\n+ *\n+ * - Redistributions of source code must retain the above copyright\n+ *   notice, this list of conditions and the following disclaimer.\n+ *\n+ * - Redistributions in binary form must reproduce the above\n+ *   copyright notice, this list of conditions and the following\n+ *   disclaimer in the documentation and/or other materials provided\n+ *   with the distribution.\n+ *\n+ * - Neither the name of the Git Development Community nor the\n+ *   names of its contributors may be used to endorse or promote\n+ *   products derived from this software without specific prior\n+ *   written permission.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\n+ * CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,\n+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+\n+package org.spearce.jgit.treewalk;\n+\n+import org.spearce.jgit.lib.FileMode;\n+import org.spearce.jgit.lib.ObjectId;\n+import org.spearce.jgit.lib.ObjectWriter;\n+import org.spearce.jgit.lib.RepositoryTestCase;\n+import org.spearce.jgit.lib.Tree;\n+import org.spearce.jgit.treewalk.filter.TreeFilter;\n+\n+public class TreeWalkBasicDiffTest extends RepositoryTestCase {\n+\tpublic void testMissingSubtree_DetectFileAdded_FileModified()\n+\t\t\tthrows Exception {\n+\t\tfinal ObjectWriter ow = new ObjectWriter(db);\n+\t\tfinal ObjectId aFileId = ow.writeBlob(\"a\".getBytes());\n+\t\tfinal ObjectId bFileId = ow.writeBlob(\"b\".getBytes());\n+\t\tfinal ObjectId cFileId1 = ow.writeBlob(\"c-1\".getBytes());\n+\t\tfinal ObjectId cFileId2 = ow.writeBlob(\"c-2\".getBytes());\n+\n+\t\t// Create sub-a/empty, sub-c/empty = hello.\n+\t\tfinal ObjectId oldTree;\n+\t\t{\n+\t\t\tfinal Tree root = new Tree(db);\n+\t\t\t{\n+\t\t\t\tfinal Tree subA = root.addTree(\"sub-a\");\n+\t\t\t\tsubA.addFile(\"empty\").setId(aFileId);\n+\t\t\t\tsubA.setId(ow.writeTree(subA));\n+\t\t\t}\n+\t\t\t{\n+\t\t\t\tfinal Tree subC = root.addTree(\"sub-c\");\n+\t\t\t\tsubC.addFile(\"empty\").setId(cFileId1);\n+\t\t\t\tsubC.setId(ow.writeTree(subC));\n+\t\t\t}\n+\t\t\toldTree = ow.writeTree(root);\n+\t\t}\n+\n+\t\t// Create sub-a/empty, sub-b/empty, sub-c/empty.\n+\t\tfinal ObjectId newTree;\n+\t\t{\n+\t\t\tfinal Tree root = new Tree(db);\n+\t\t\t{\n+\t\t\t\tfinal Tree subA = root.addTree(\"sub-a\");\n+\t\t\t\tsubA.addFile(\"empty\").setId(aFileId);\n+\t\t\t\tsubA.setId(ow.writeTree(subA));\n+\t\t\t}\n+\t\t\t{\n+\t\t\t\tfinal Tree subB = root.addTree(\"sub-b\");\n+\t\t\t\tsubB.addFile(\"empty\").setId(bFileId);\n+\t\t\t\tsubB.setId(ow.writeTree(subB));\n+\t\t\t}\n+\t\t\t{\n+\t\t\t\tfinal Tree subC = root.addTree(\"sub-c\");\n+\t\t\t\tsubC.addFile(\"empty\").setId(cFileId2);\n+\t\t\t\tsubC.setId(ow.writeTree(subC));\n+\t\t\t}\n+\t\t\tnewTree = ow.writeTree(root);\n+\t\t}\n+\n+\t\tfinal TreeWalk tw = new TreeWalk(db);\n+\t\ttw.reset(new ObjectId[] { oldTree, newTree });\n+\t\ttw.setRecursive(true);\n+\t\ttw.setFilter(TreeFilter.ANY_DIFF);\n+\n+\t\tassertTrue(tw.next());\n+\t\tassertEquals(\"sub-b/empty\", tw.getPathString());\n+\t\tassertEquals(FileMode.MISSING, tw.getFileMode(0));\n+\t\tassertEquals(FileMode.REGULAR_FILE, tw.getFileMode(1));\n+\t\tassertEquals(ObjectId.zeroId(), tw.getObjectId(0));\n+\t\tassertEquals(bFileId, tw.getObjectId(1));\n+\n+\t\tassertTrue(tw.next());\n+\t\tassertEquals(\"sub-c/empty\", tw.getPathString());\n+\t\tassertEquals(FileMode.REGULAR_FILE, tw.getFileMode(0));\n+\t\tassertEquals(FileMode.REGULAR_FILE, tw.getFileMode(1));\n+\t\tassertEquals(cFileId1, tw.getObjectId(0));\n+\t\tassertEquals(cFileId2, tw.getObjectId(1));\n+\n+\t\tassertFalse(tw.next());\n+\t}\n+}\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java\nindex 9f373e6..aa05736 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java\n@@ -606,11 +606,12 @@ public class TreeWalk {\n \t */\n \tpublic void enterSubtree() throws MissingObjectException,\n \t\t\tIncorrectObjectTypeException, CorruptObjectException, IOException {\n+\t\tfinal AbstractTreeIterator ch = currentHead;\n \t\tfinal AbstractTreeIterator[] tmp = new AbstractTreeIterator[trees.length];\n \t\tfor (int i = 0; i < trees.length; i++) {\n \t\t\tfinal AbstractTreeIterator t = trees[i];\n \t\t\tfinal AbstractTreeIterator n;\n-\t\t\tif (!t.eof() && FileMode.TREE.equals(t.mode))\n+\t\t\tif (t.matches == ch && !t.eof() && FileMode.TREE.equals(t.mode))\n \t\t\t\tn = t.createSubtreeIterator(db);\n \t\t\telse\n \t\t\t\tn = new EmptyTreeIterator(t);\n-- \n1.6.0.rc2.22.g71b99\n"}]}