{"thread":{"id":"15883","subject":"[JGIT PATCH 4/8] Allow a DirCache to be created with no backing store file","startedAt":"2008-10-13T21:10:09Z","lastAt":"2009-01-19T17:51:54Z","messageCount":16,"participants":["Shawn O. Pearce","Robin Rosenberg","Tomi Pakarinen"],"isPatch":true,"patchVersion":1,"patchTotal":8},"messages":[{"id":"92967","messageId":"1223932217-4771-1-git-send-email-spearce@spearce.org","threadId":"15883","inReplyTo":null,"subject":"[JGIT PATCH 0/8] Crude merge support","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-10-13T21:10:09Z","receivedAt":"2008-10-13T21:10:09Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"This series add some basic merge support to JGit.  Its more of a\n\"merge toolkit\" than a merge implementation.  I have about 200\nlines of code in another codebase that calls this stuff in JGit to\nactually make a merge.  Eventually I hope to migrate a lot of that\ncode back into JGit, but for now its not here as I'm not entirely\nsure what API merge applications need, and how much of that code\nis application specific and how much of it should be core to JGit.\n\nFWIW, that code will be open-sourced soon-ish too.  But I really\nthink a lot of it belongs in JGit, so it will probably migrate over.\n\nNo unit tests yet.  Yea, I know.  I've had this done and in use\nfor about 3 or 4 weeks now.  I just couldn't find the time to get\nthe Javadoc cleaned up to post to the list for comments, let alone\nwrite unit tests.\n\nBut it does add the missing \"write-tree\" support to DirCache. :)\n\nShawn O. Pearce (8):\n  Expose the raw path for the current entry of a TreeWalk\n  Expose DirCacheEntry.getFileMode as a utility function\n  Add writeTree support to DirCache\n  Allow a DirCache to be created with no backing store file\n  Allow CanonicalTreeParsers to be created with a UTF-8 path prefix\n  Recursively load an entire tree into a DirCacheBuilder\n  Allow DirCacheEntry instances to be created with stage > 0\n  Define a basic merge API, and a two-way tree merge strategy\n\n .../src/org/spearce/jgit/dircache/DirCache.java    |   37 ++++\n .../org/spearce/jgit/dircache/DirCacheBuilder.java |   58 ++++++\n .../org/spearce/jgit/dircache/DirCacheEntry.java   |   61 +++++-\n .../org/spearce/jgit/dircache/DirCacheTree.java    |  115 +++++++++++\n .../spearce/jgit/errors/UnmergedPathException.java |   67 +++++++\n .../src/org/spearce/jgit/lib/FileMode.java         |    7 +\n .../src/org/spearce/jgit/lib/ObjectWriter.java     |   12 +-\n .../src/org/spearce/jgit/merge/MergeStrategy.java  |  134 +++++++++++++\n .../src/org/spearce/jgit/merge/Merger.java         |  207 ++++++++++++++++++++\n .../org/spearce/jgit/merge/StrategyOneSided.java   |   98 +++++++++\n .../jgit/merge/StrategySimpleTwoWayInCore.java     |  179 +++++++++++++++++\n .../jgit/treewalk/AbstractTreeIterator.java        |   31 +++\n .../spearce/jgit/treewalk/CanonicalTreeParser.java |   28 +++\n .../src/org/spearce/jgit/treewalk/TreeWalk.java    |   17 ++-\n 14 files changed, 1041 insertions(+), 10 deletions(-)\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java\n"},{"id":"92970","messageId":"1223932217-4771-2-git-send-email-spearce@spearce.org","threadId":"15883","inReplyTo":"1223932217-4771-1-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 1/8] Expose the raw path for the current entry of a TreeWalk","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-10-13T21:10:10Z","receivedAt":"2008-10-13T21:10:10Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Copying the path byte array (keeping it encoded in UTF-8) is quicker\nthan converting to String and then back again to UTF-8 when creating\na DirCacheEntry for the current position in a TreeWalk.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../src/org/spearce/jgit/treewalk/TreeWalk.java    |   15 +++++++++++++++\n 1 files changed, 15 insertions(+), 0 deletions(-)\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 26544b5..3af3d09 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@@ -582,6 +582,21 @@ public String getPathString() {\n \t}\n \n \t/**\n+\t * Get the current entry's complete path as a UTF-8 byte array.\n+\t * \n+\t * @return complete path of the current entry, from the root of the\n+\t *         repository. If the current entry is in a subtree there will be at\n+\t *         least one '/' in the returned string.\n+\t */\n+\tpublic byte[] getRawPath() {\n+\t\tfinal AbstractTreeIterator t = currentHead;\n+\t\tfinal int n = t.pathLen;\n+\t\tfinal byte[] r = new byte[n];\n+\t\tSystem.arraycopy(t.path, 0, r, 0, n);\n+\t\treturn r;\n+\t}\n+\n+\t/**\n \t * Test if the supplied path matches the current entry's path.\n \t * <p>\n \t * This method tests that the supplied path is exactly equal to the current\n-- \n1.6.0.2.706.g340fc\n"},{"id":"92969","messageId":"1223932217-4771-3-git-send-email-spearce@spearce.org","threadId":"15883","inReplyTo":"1223932217-4771-2-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 2/8] Expose DirCacheEntry.getFileMode as a utility function","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-10-13T21:10:11Z","receivedAt":"2008-10-13T21:10:11Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Its easier to get the FileMode object in some applications than to\nget the raw mode and convert it to the FileMode in the application\ncode.  Its slower, but sometimes you just have to have the proper\nFileMode singleton.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../org/spearce/jgit/dircache/DirCacheEntry.java   |    9 +++++++++\n 1 files changed, 9 insertions(+), 0 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java\nindex cc683d7..355cd3e 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java\n@@ -295,6 +295,15 @@ public int getRawMode() {\n \t}\n \n \t/**\n+\t * Obtain the {@link FileMode} for this entry.\n+\t * \n+\t * @return the file mode singleton for this entry.\n+\t */\n+\tpublic FileMode getFileMode() {\n+\t\treturn FileMode.fromBits(getRawMode());\n+\t}\n+\n+\t/**\n \t * Set the file mode for this entry.\n \t *\n \t * @param mode\n-- \n1.6.0.2.706.g340fc\n"},{"id":"92974","messageId":"1223932217-4771-4-git-send-email-spearce@spearce.org","threadId":"15883","inReplyTo":"1223932217-4771-3-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 3/8] Add writeTree support to DirCache","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-10-13T21:10:12Z","receivedAt":"2008-10-13T21:10:12Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"This way we can write a full tree from the DirCache, including reusing\nany valid tree entries stored within the 'TREE' cache extension.  By\nreusing those entries we can avoid generating the tree objects that\nare already stored in the Git repository.\n\nThe algorithm may cause up to 3 passes over the DirCache entries:\n\n * Pass 1: Compute the tree structure\n * Pass 2: Compute the sizes of each tree\n * Pass 3: Write the tree object to the object store\n\nThese extra passes cause more CPU time to be expended in exchange\nfor a lower memory requirement during the tree writing.  The code\nis only formatting the lowest level leaf tree which has not yet\nbeen written to the object store, so higher level trees do not\noccupy memory while they are waiting for the leaves to write out.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../src/org/spearce/jgit/dircache/DirCache.java    |   20 ++++\n .../org/spearce/jgit/dircache/DirCacheTree.java    |  115 ++++++++++++++++++++\n .../spearce/jgit/errors/UnmergedPathException.java |   67 ++++++++++++\n .../src/org/spearce/jgit/lib/FileMode.java         |    7 ++\n .../src/org/spearce/jgit/lib/ObjectWriter.java     |   12 ++-\n 5 files changed, 219 insertions(+), 2 deletions(-)\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java\nindex 76657c4..b3c57ad 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java\n@@ -51,8 +51,11 @@\n import java.util.Comparator;\n \n import org.spearce.jgit.errors.CorruptObjectException;\n+import org.spearce.jgit.errors.UnmergedPathException;\n import org.spearce.jgit.lib.Constants;\n import org.spearce.jgit.lib.LockFile;\n+import org.spearce.jgit.lib.ObjectId;\n+import org.spearce.jgit.lib.ObjectWriter;\n import org.spearce.jgit.lib.Repository;\n import org.spearce.jgit.util.MutableInteger;\n import org.spearce.jgit.util.NB;\n@@ -692,4 +695,21 @@ public DirCacheTree getCacheTree(final boolean build) {\n \t\t}\n \t\treturn tree;\n \t}\n+\n+\t/**\n+\t * Write all index trees to the object store, returning the root tree.\n+\t * \n+\t * @param ow\n+\t *            the writer to use when serializing to the store.\n+\t * @return identity for the root tree.\n+\t * @throws UnmergedPathException\n+\t *             one or more paths contain higher-order stages (stage > 0),\n+\t *             which cannot be stored in a tree object.\n+\t * @throws IOException\n+\t *             an unexpected error occurred writing to the object store.\n+\t */\n+\tpublic ObjectId writeTree(final ObjectWriter ow)\n+\t\t\tthrows UnmergedPathException, IOException {\n+\t\treturn getCacheTree(true).writeTree(sortedEntries, 0, 0, ow);\n+\t}\n }\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheTree.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheTree.java\nindex 26b6348..cf96ded 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheTree.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheTree.java\n@@ -1,5 +1,6 @@\n /*\n  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>\n+ * Copyright (C) 2008, Google Inc.\n  *\n  * All rights reserved.\n  *\n@@ -37,14 +38,20 @@\n \n package org.spearce.jgit.dircache;\n \n+import static org.spearce.jgit.lib.Constants.OBJECT_ID_LENGTH;\n+\n+import java.io.ByteArrayOutputStream;\n import java.io.IOException;\n import java.io.OutputStream;\n import java.nio.ByteBuffer;\n import java.util.Arrays;\n import java.util.Comparator;\n \n+import org.spearce.jgit.errors.UnmergedPathException;\n import org.spearce.jgit.lib.Constants;\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.util.MutableInteger;\n import org.spearce.jgit.util.RawParseUtils;\n \n@@ -273,6 +280,114 @@ public String getPathString() {\n \t\treturn r.toString();\n \t}\n \n+\t/**\n+\t * Write (if necessary) this tree to the object store.\n+\t * \n+\t * @param cache\n+\t *            the complete cache from DirCache.\n+\t * @param cIdx\n+\t *            first position of <code>cache</code> that is a member of this\n+\t *            tree. The path of <code>cache[cacheIdx].path</code> for the\n+\t *            range <code>[0,pathOff-1)</code> matches the complete path of\n+\t *            this tree, from the root of the repository.\n+\t * @param pathOffset\n+\t *            number of bytes of <code>cache[cacheIdx].path</code> that\n+\t *            matches this tree's path. The value at array position\n+\t *            <code>cache[cacheIdx].path[pathOff-1]</code> is always '/' if\n+\t *            <code>pathOff</code> is > 0.\n+\t * @param ow\n+\t *            the writer to use when serializing to the store.\n+\t * @return identity of this tree.\n+\t * @throws UnmergedPathException\n+\t *             one or more paths contain higher-order stages (stage > 0),\n+\t *             which cannot be stored in a tree object.\n+\t * @throws IOException\n+\t *             an unexpected error occurred writing to the object store.\n+\t */\n+\tObjectId writeTree(final DirCacheEntry[] cache, int cIdx,\n+\t\t\tfinal int pathOffset, final ObjectWriter ow)\n+\t\t\tthrows UnmergedPathException, IOException {\n+\t\tif (id == null) {\n+\t\t\tfinal int endIdx = cIdx + entrySpan;\n+\t\t\tfinal int size = computeSize(cache, cIdx, pathOffset, ow);\n+\t\t\tfinal ByteArrayOutputStream out = new ByteArrayOutputStream(size);\n+\t\t\tint childIdx = 0;\n+\t\t\tint entryIdx = cIdx;\n+\n+\t\t\twhile (entryIdx < endIdx) {\n+\t\t\t\tfinal DirCacheEntry e = cache[entryIdx];\n+\t\t\t\tfinal byte[] ep = e.path;\n+\t\t\t\tif (childIdx < childCnt) {\n+\t\t\t\t\tfinal DirCacheTree st = children[childIdx];\n+\t\t\t\t\tif (st.contains(ep, pathOffset, ep.length)) {\n+\t\t\t\t\t\tFileMode.TREE.copyTo(out);\n+\t\t\t\t\t\tout.write(' ');\n+\t\t\t\t\t\tout.write(st.encodedName);\n+\t\t\t\t\t\tout.write(0);\n+\t\t\t\t\t\tst.id.copyRawTo(out);\n+\n+\t\t\t\t\t\tentryIdx += st.entrySpan;\n+\t\t\t\t\t\tchildIdx++;\n+\t\t\t\t\t\tcontinue;\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\n+\t\t\t\te.getFileMode().copyTo(out);\n+\t\t\t\tout.write(' ');\n+\t\t\t\tout.write(ep, pathOffset, ep.length - pathOffset);\n+\t\t\t\tout.write(0);\n+\t\t\t\tout.write(e.idBuffer(), e.idOffset(), OBJECT_ID_LENGTH);\n+\t\t\t\tentryIdx++;\n+\t\t\t}\n+\n+\t\t\tid = ow.writeCanonicalTree(out.toByteArray());\n+\t\t}\n+\t\treturn id;\n+\t}\n+\n+\tprivate int computeSize(final DirCacheEntry[] cache, int cIdx,\n+\t\t\tfinal int pathOffset, final ObjectWriter ow)\n+\t\t\tthrows UnmergedPathException, IOException {\n+\t\tfinal int endIdx = cIdx + entrySpan;\n+\t\tint childIdx = 0;\n+\t\tint entryIdx = cIdx;\n+\t\tint size = 0;\n+\n+\t\twhile (entryIdx < endIdx) {\n+\t\t\tfinal DirCacheEntry e = cache[entryIdx];\n+\t\t\tif (e.getStage() != 0)\n+\t\t\t\tthrow new UnmergedPathException(e);\n+\n+\t\t\tfinal byte[] ep = e.path;\n+\t\t\tif (childIdx < childCnt) {\n+\t\t\t\tfinal DirCacheTree st = children[childIdx];\n+\t\t\t\tif (st.contains(ep, pathOffset, ep.length)) {\n+\t\t\t\t\tfinal int stOffset = pathOffset + st.nameLength() + 1;\n+\t\t\t\t\tst.writeTree(cache, entryIdx, stOffset, ow);\n+\n+\t\t\t\t\tsize += FileMode.TREE.copyToLength();\n+\t\t\t\t\tsize += st.nameLength();\n+\t\t\t\t\tsize += OBJECT_ID_LENGTH + 2;\n+\n+\t\t\t\t\tentryIdx += st.entrySpan;\n+\t\t\t\t\tchildIdx++;\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\t\t\t}\n+\n+\t\t\tfinal FileMode mode = e.getFileMode();\n+\t\t\tif (mode.getObjectType() == Constants.OBJ_BAD)\n+\t\t\t\tthrow new UnmergedPathException(e);\n+\n+\t\t\tsize += mode.copyToLength();\n+\t\t\tsize += ep.length - pathOffset;\n+\t\t\tsize += OBJECT_ID_LENGTH + 2;\n+\t\t\tentryIdx++;\n+\t\t}\n+\n+\t\treturn size;\n+\t}\n+\n \tprivate void appendName(final StringBuilder r) {\n \t\tif (parent != null) {\n \t\t\tparent.appendName(r);\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java b/org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java\nnew file mode 100644\nindex 0000000..17a3965\n--- /dev/null\n+++ b/org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java\n@@ -0,0 +1,67 @@\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.errors;\n+\n+import java.io.IOException;\n+\n+import org.spearce.jgit.dircache.DirCacheEntry;\n+\n+/**\n+ * Indicates one or more paths in a DirCache have non-zero stages present.\n+ */\n+public class UnmergedPathException extends IOException {\n+\tprivate static final long serialVersionUID = 1L;\n+\n+\tprivate final DirCacheEntry entry;\n+\n+\t/**\n+\t * Create a new unmerged path exception.\n+\t * \n+\t * @param dce\n+\t *            the first non-zero stage of the unmerged path.\n+\t */\n+\tpublic UnmergedPathException(final DirCacheEntry dce) {\n+\t\tsuper(\"Unmerged path: \" + dce.getPathString());\n+\t\tentry = dce;\n+\t}\n+\n+\t/** @return the first non-zero stage of the unmerged path */\n+\tpublic DirCacheEntry getDirCacheEntry() {\n+\t\treturn entry;\n+\t}\n+}\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java b/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java\nindex fe5f2f6..cf42f37 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java\n@@ -191,6 +191,13 @@ public void copyTo(final OutputStream os) throws IOException {\n \t}\n \n \t/**\n+\t * @return the number of bytes written by {@link #copyTo(OutputStream)}.\n+\t */\n+\tpublic int copyToLength() {\n+\t\treturn octalBytes.length;\n+\t}\n+\n+\t/**\n \t * Get the object type that should appear for this type of mode.\n \t * <p>\n \t * See the object type constants in {@link Constants}.\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectWriter.java b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectWriter.java\nindex 6c2cd4f..ffa7837 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectWriter.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectWriter.java\n@@ -155,10 +155,18 @@ public ObjectId writeTree(final Tree t) throws IOException {\n \t\t\to.write(0);\n \t\t\tid.copyRawTo(o);\n \t\t}\n-\t\treturn writeTree(o.toByteArray());\n+\t\treturn writeCanonicalTree(o.toByteArray());\n \t}\n \n-\tprivate ObjectId writeTree(final byte[] b) throws IOException {\n+\t/**\n+\t * Write a canonical tree to the object database.\n+\t *\n+\t * @param b\n+\t *            the canonical encoding of the tree object.\n+\t * @return SHA-1 of the tree\n+\t * @throws IOException\n+\t */\n+\tpublic ObjectId writeCanonicalTree(final byte[] b) throws IOException {\n \t\treturn writeTree(b.length, new ByteArrayInputStream(b));\n \t}\n \n-- \n1.6.0.2.706.g340fc\n"},{"id":"92966","messageId":"1223932217-4771-5-git-send-email-spearce@spearce.org","threadId":"15883","inReplyTo":"1223932217-4771-4-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 4/8] Allow a DirCache to be created with no backing store file","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-10-13T21:10:13Z","receivedAt":"2008-10-13T21:10:13Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"This permits using a DirCache as a temporary storage area in memory\nonly, with no chance of it being written out to disk.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../src/org/spearce/jgit/dircache/DirCache.java    |   17 +++++++++++++++++\n 1 files changed, 17 insertions(+), 0 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java\nindex b3c57ad..c5a4f91 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java\n@@ -111,6 +111,17 @@ static int cmp(final byte[] aPath, final int aLen, final byte[] bPath,\n \t}\n \n \t/**\n+\t * Create a new empty index which is never stored on disk.\n+\t * \n+\t * @return an empty cache which has no backing store file. The cache may not\n+\t *         be read or written, but it may be queried and updated (in\n+\t *         memory).\n+\t */\n+\tpublic static DirCache newInCore() {\n+\t\treturn new DirCache(null);\n+\t}\n+\n+\t/**\n \t * Create a new in-core index representation and read an index from disk.\n \t * <p>\n \t * The new index will be read before it is returned to the caller. Read\n@@ -297,6 +308,8 @@ void replace(final DirCacheEntry[] e, final int cnt) {\n \t *             library does not support.\n \t */\n \tpublic void read() throws IOException, CorruptObjectException {\n+\t\tif (liveFile == null)\n+\t\t\tthrow new IOException(\"DirCache does not have a backing file\");\n \t\tif (!liveFile.exists())\n \t\t\tclear();\n \t\telse if (liveFile.lastModified() != lastModified) {\n@@ -407,6 +420,8 @@ private static boolean is_DIRC(final byte[] hdr) {\n \t *             hold the lock.\n \t */\n \tpublic boolean lock() throws IOException {\n+\t\tif (liveFile == null)\n+\t\t\tthrow new IOException(\"DirCache does not have a backing file\");\n \t\tfinal LockFile tmp = new LockFile(liveFile);\n \t\tif (tmp.lock()) {\n \t\t\ttmp.setNeedStatInformation(true);\n@@ -515,6 +530,8 @@ public boolean commit() {\n \t}\n \n \tprivate void requireLocked(final LockFile tmp) {\n+\t\tif (liveFile == null)\n+\t\t\tthrow new IllegalStateException(\"DirCache is not locked\");\n \t\tif (tmp == null)\n \t\t\tthrow new IllegalStateException(\"DirCache \"\n \t\t\t\t\t+ liveFile.getAbsolutePath() + \" not locked.\");\n-- \n1.6.0.2.706.g340fc\n"},{"id":"92972","messageId":"1223932217-4771-6-git-send-email-spearce@spearce.org","threadId":"15883","inReplyTo":"1223932217-4771-5-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 5/8] Allow CanonicalTreeParsers to be created with a UTF-8 path prefix","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-10-13T21:10:14Z","receivedAt":"2008-10-13T21:10:14Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Creating an iterator with a path prefix permits a tree to be\n\"mounted\" at a different part of a repository, permitting more\nsophisticated merge strategies beyond just 1:1 path matching.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../jgit/treewalk/AbstractTreeIterator.java        |   31 ++++++++++++++++++++\n .../spearce/jgit/treewalk/CanonicalTreeParser.java |   28 ++++++++++++++++++\n .../src/org/spearce/jgit/treewalk/TreeWalk.java    |    2 +-\n 3 files changed, 60 insertions(+), 1 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java\nindex 5226ab6..adfbb11 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java\n@@ -170,6 +170,37 @@ protected AbstractTreeIterator(final String prefix) {\n \t}\n \n \t/**\n+\t * Create a new iterator with no parent and a prefix.\n+\t * <p>\n+\t * The prefix path supplied is inserted in front of all paths generated by\n+\t * this iterator. It is intended to be used when an iterator is being\n+\t * created for a subsection of an overall repository and needs to be\n+\t * combined with other iterators that are created to run over the entire\n+\t * repository namespace.\n+\t *\n+\t * @param prefix\n+\t *            position of this iterator in the repository tree. The value\n+\t *            may be null or the empty array to indicate the prefix is the\n+\t *            root of the repository. A trailing slash ('/') is\n+\t *            automatically appended if the prefix does not end in '/'.\n+\t */\n+\tprotected AbstractTreeIterator(final byte[] prefix) {\n+\t\tparent = null;\n+\n+\t\tif (prefix != null && prefix.length > 0) {\n+\t\t\tpathLen = prefix.length;\n+\t\t\tpath = new byte[Math.max(DEFAULT_PATH_SIZE, pathLen + 1)];\n+\t\t\tSystem.arraycopy(prefix, 0, path, 0, pathLen);\n+\t\t\tif (path[pathLen - 1] != '/')\n+\t\t\t\tpath[pathLen++] = '/';\n+\t\t\tpathOffset = pathLen;\n+\t\t} else {\n+\t\t\tpath = new byte[DEFAULT_PATH_SIZE];\n+\t\t\tpathOffset = 0;\n+\t\t}\n+\t}\n+\t\n+\t/**\n \t * Create an iterator for a subtree of an existing iterator.\n \t * \n \t * @param p\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java\nindex dcc53cd..3dac6dd 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java\n@@ -62,6 +62,34 @@ public CanonicalTreeParser() {\n \t\t// Nothing necessary.\n \t}\n \n+\t/**\n+\t * Create a new parser for a tree appearing in a subset of a repository.\n+\t * \n+\t * @param prefix\n+\t *            position of this iterator in the repository tree. The value\n+\t *            may be null or the empty array to indicate the prefix is the\n+\t *            root of the repository. A trailing slash ('/') is\n+\t *            automatically appended if the prefix does not end in '/'.\n+\t * @param repo\n+\t *            repository to load the tree data from.\n+\t * @param treeId\n+\t *            identity of the tree being parsed; used only in exception\n+\t *            messages if data corruption is found.\n+\t * @throws MissingObjectException\n+\t *             the object supplied is not available from the repository.\n+\t * @throws IncorrectObjectTypeException\n+\t *             the object supplied as an argument is not actually a tree and\n+\t *             cannot be parsed as though it were a tree.\n+\t * @throws IOException\n+\t *             a loose object or pack file could not be read.\n+\t */\n+\tpublic CanonicalTreeParser(final byte[] prefix, final Repository repo,\n+\t\t\tfinal ObjectId treeId) throws IncorrectObjectTypeException,\n+\t\t\tIOException {\n+\t\tsuper(prefix);\n+\t\treset(repo, treeId);\n+\t}\n+\n \tprivate CanonicalTreeParser(final CanonicalTreeParser p) {\n \t\tsuper(p);\n \t}\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 3af3d09..b1cbd2d 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@@ -307,7 +307,7 @@ public void reset(final ObjectId[] ids) throws MissingObjectException,\n \t\t\t\to = trees[i];\n \t\t\t\twhile (o.parent != null)\n \t\t\t\t\to = o.parent;\n-\t\t\t\tif (o instanceof CanonicalTreeParser) {\n+\t\t\t\tif (o instanceof CanonicalTreeParser && o.pathOffset == 0) {\n \t\t\t\t\to.matches = null;\n \t\t\t\t\to.matchShift = 0;\n \t\t\t\t\t((CanonicalTreeParser) o).reset(db, ids[i]);\n-- \n1.6.0.2.706.g340fc\n"},{"id":"92968","messageId":"1223932217-4771-7-git-send-email-spearce@spearce.org","threadId":"15883","inReplyTo":"1223932217-4771-6-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 6/8] Recursively load an entire tree into a DirCacheBuilder","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-10-13T21:10:15Z","receivedAt":"2008-10-13T21:10:15Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"This implements the DirCache portion of \"git read-tree\", where a\ntree can be recursively read into a DirCache instance without an\nimpact on the working directory.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../org/spearce/jgit/dircache/DirCacheBuilder.java |   58 ++++++++++++++++++++\n 1 files changed, 58 insertions(+), 0 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java\nindex 3a37054..88bda4d 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java\n@@ -37,8 +37,15 @@\n \n package org.spearce.jgit.dircache;\n \n+import java.io.IOException;\n import java.util.Arrays;\n \n+import org.spearce.jgit.lib.AnyObjectId;\n+import org.spearce.jgit.lib.Repository;\n+import org.spearce.jgit.treewalk.AbstractTreeIterator;\n+import org.spearce.jgit.treewalk.CanonicalTreeParser;\n+import org.spearce.jgit.treewalk.TreeWalk;\n+\n /**\n  * Updates a {@link DirCache} by adding individual {@link DirCacheEntry}s.\n  * <p>\n@@ -112,6 +119,57 @@ public void keep(final int pos, int cnt) {\n \t\tfastKeep(pos, cnt);\n \t}\n \n+\t/**\n+\t * Recursively add an entire tree into this builder.\n+\t * <p>\n+\t * If pathPrefix is \"a/b\" and the tree contains file \"c\" then the resulting\n+\t * DirCacheEntry will have the path \"a/b/c\".\n+\t * <p>\n+\t * All entries are inserted at stage 0, therefore assuming that the\n+\t * application will not insert any other paths with the same pathPrefix.\n+\t * \n+\t * @param pathPrefix\n+\t *            UTF-8 encoded prefix to mount the tree's entries at. If the\n+\t *            path does not end with '/' one will be automatically inserted\n+\t *            as necessary.\n+\t * @param db\n+\t *            repository the tree(s) will be read from during recursive\n+\t *            traversal. This must be the same repository that the resulting\n+\t *            DirCache would be written out to (or used in) otherwise the\n+\t *            caller is simply asking for deferred MissingObjectExceptions.\n+\t * @param tree\n+\t *            the tree to recursively add. This tree's contents will appear\n+\t *            under <code>pathPrefix</code>. The ObjectId must be that of a\n+\t *            tree; the caller is responsible for dereferencing a tag or\n+\t *            commit (if necessary).\n+\t * @throws IOException\n+\t *             a tree cannot be read to iterate through its entries.\n+\t */\n+\tpublic void addTree(final byte[] pathPrefix, final Repository db,\n+\t\t\tfinal AnyObjectId tree) throws IOException {\n+\t\tfinal TreeWalk tw = new TreeWalk(db);\n+\t\ttw.reset();\n+\t\ttw.addTree(new CanonicalTreeParser(pathPrefix, db, tree.toObjectId()));\n+\t\ttw.setRecursive(true);\n+\t\tif (tw.next()) {\n+\t\t\tfinal DirCacheEntry newEntry = toEntry(tw);\n+\t\t\tbeforeAdd(newEntry);\n+\t\t\tfastAdd(newEntry);\n+\t\t\twhile (tw.next())\n+\t\t\t\tfastAdd(toEntry(tw));\n+\t\t}\n+\t}\n+\n+\tprivate DirCacheEntry toEntry(final TreeWalk tw) {\n+\t\tfinal DirCacheEntry e = new DirCacheEntry(tw.getRawPath());\n+\t\tfinal AbstractTreeIterator i;\n+\n+\t\ti = tw.getTree(0, AbstractTreeIterator.class);\n+\t\te.setFileMode(tw.getFileMode(0));\n+\t\te.setObjectIdFromRaw(i.idBuffer(), i.idOffset());\n+\t\treturn e;\n+\t}\n+\n \tpublic void finish() {\n \t\tif (!sorted)\n \t\t\tresort();\n-- \n1.6.0.2.706.g340fc\n"},{"id":"92971","messageId":"1223932217-4771-8-git-send-email-spearce@spearce.org","threadId":"15883","inReplyTo":"1223932217-4771-7-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 7/8] Allow DirCacheEntry instances to be created with stage > 0","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-10-13T21:10:16Z","receivedAt":"2008-10-13T21:10:16Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"As the stage is part of the sorting criteria for DirCacheEntry\nobjects we don't allow the stage to be modified on the fly in\nan existing instance.  Instead the stage must be set by reading\nit from the on-disk format or by creating a new entry with the\nproper path and stage components.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../org/spearce/jgit/dircache/DirCacheEntry.java   |   52 +++++++++++++++++---\n 1 files changed, 45 insertions(+), 7 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java\nindex 355cd3e..9304501 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java\n@@ -60,6 +60,18 @@\n public class DirCacheEntry {\n \tprivate static final byte[] nullpad = new byte[8];\n \n+\t/** The standard (fully merged) stage for an entry. */\n+\tpublic static final int STAGE_0 = 0;\n+\n+\t/** The base tree revision for an entry. */\n+\tpublic static final int STAGE_1 = 1;\n+\n+\t/** The first tree revision (usually called \"ours\"). */\n+\tpublic static final int STAGE_2 = 2;\n+\n+\t/** The second tree revision (usually called \"theirs\"). */\n+\tpublic static final int STAGE_3 = 3;\n+\n \t// private static final int P_CTIME = 0;\n \n \t// private static final int P_CTIME_NSEC = 4;\n@@ -141,8 +153,8 @@ DirCacheEntry(final byte[] sharedInfo, final int infoAt,\n \t}\n \n \t/**\n-\t * Create an empty entry.\n-\t *\n+\t * Create an empty entry at stage 0.\n+\t * \n \t * @param newPath\n \t *            name of the cache entry.\n \t */\n@@ -151,20 +163,46 @@ public DirCacheEntry(final String newPath) {\n \t}\n \n \t/**\n-\t * Create an empty entry.\n-\t *\n+\t * Create an empty entry at the specified stage.\n+\t * \n+\t * @param newPath\n+\t *            name of the cache entry.\n+\t * @param stage\n+\t *            the stage index of the new entry.\n+\t */\n+\tpublic DirCacheEntry(final String newPath, final int stage) {\n+\t\tthis(Constants.encode(newPath), stage);\n+\t}\n+\n+\t/**\n+\t * Create an empty entry at stage 0.\n+\t * \n \t * @param newPath\n \t *            name of the cache entry, in the standard encoding.\n \t */\n \tpublic DirCacheEntry(final byte[] newPath) {\n+\t\tthis(newPath, STAGE_0);\n+\t}\n+\n+\t/**\n+\t * Create an empty entry at the specified stage.\n+\t * \n+\t * @param newPath\n+\t *            name of the cache entry, in the standard encoding.\n+\t * @param stage\n+\t *            the stage index of the new entry.\n+\t */\n+\tpublic DirCacheEntry(final byte[] newPath, final int stage) {\n \t\tinfo = new byte[INFO_LEN];\n \t\tinfoOffset = 0;\n-\n \t\tpath = newPath;\n+\n+\t\tint flags = ((stage & 0x3) << 12);\n \t\tif (path.length < NAME_MASK)\n-\t\t\tNB.encodeInt16(info, infoOffset + P_FLAGS, path.length);\n+\t\t\tflags |= path.length;\n \t\telse\n-\t\t\tNB.encodeInt16(info, infoOffset + P_FLAGS, NAME_MASK);\n+\t\t\tflags |= NAME_MASK;\n+\t\tNB.encodeInt16(info, infoOffset + P_FLAGS, flags);\n \t}\n \n \tvoid write(final OutputStream os) throws IOException {\n-- \n1.6.0.2.706.g340fc\n"},{"id":"92973","messageId":"1223932217-4771-9-git-send-email-spearce@spearce.org","threadId":"15883","inReplyTo":"1223932217-4771-8-git-send-email-spearce@spearce.org","subject":"[JGIT PATCH 8/8] Define a basic merge API, and a two-way tree merge strategy","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-10-13T21:10:17Z","receivedAt":"2008-10-13T21:10:17Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"This basic merge implementation is sufficient to merge two commits in\nmemory and write the result out as a new commit, without having a work\ntree on the local filesystem.  It is therefore suitable for use within\na batch server process where human intervention is not available to\nresolve conflicts.\n\nThis API should permit extending it with the working tree and a copy\nof the work tree's DirCache, so edits in the tree can be merged in\nparallel with edits from commits.  But the functionality is not yet\nimplemented, so it is still a pie-in-the-sky concept.\n\nThe main strategy \"simple-two-way-in-core\" provides a basic 3 way\nmerge on the path level only.  File contents are never patched by\nthis strategy, making it somewhat safe for automatic merges.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../src/org/spearce/jgit/merge/MergeStrategy.java  |  134 +++++++++++++\n .../src/org/spearce/jgit/merge/Merger.java         |  207 ++++++++++++++++++++\n .../org/spearce/jgit/merge/StrategyOneSided.java   |   98 +++++++++\n .../jgit/merge/StrategySimpleTwoWayInCore.java     |  179 +++++++++++++++++\n 4 files changed, 618 insertions(+), 0 deletions(-)\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java\n create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java b/org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java\nnew file mode 100644\nindex 0000000..d28dcc1\n--- /dev/null\n+++ b/org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java\n@@ -0,0 +1,134 @@\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.merge;\n+\n+import java.util.HashMap;\n+\n+import org.spearce.jgit.lib.Repository;\n+\n+/**\n+ * A method of combining two or more trees together to form an output tree.\n+ * <p>\n+ * Different strategies may employ different techniques for deciding which paths\n+ * (and ObjectIds) to carry from the input trees into the final output tree.\n+ */\n+public abstract class MergeStrategy {\n+\t/** Simple strategy that sets the output tree to the first input tree. */\n+\tpublic static final MergeStrategy OURS = new StrategyOneSided(\"ours\", 0);\n+\n+\t/** Simple strategy that sets the output tree to the second input tree. */\n+\tpublic static final MergeStrategy THEIRS = new StrategyOneSided(\"theirs\", 1);\n+\n+\t/** Simple strategy to merge paths, without simultaneous edits. */\n+\tpublic static final MergeStrategy SIMPLE_TWO_WAY_IN_CORE = StrategySimpleTwoWayInCore.INSTANCE;\n+\n+\tprivate static final HashMap<String, MergeStrategy> STRATEGIES = new HashMap<String, MergeStrategy>();\n+\n+\tstatic {\n+\t\tregister(OURS);\n+\t\tregister(THEIRS);\n+\t\tregister(SIMPLE_TWO_WAY_IN_CORE);\n+\t}\n+\n+\t/**\n+\t * Register a merge strategy so it can later be obtained by name.\n+\t * \n+\t * @param imp\n+\t *            the strategy to register.\n+\t * @throws IllegalArgumentException\n+\t *             a strategy by the same name has already been registered.\n+\t */\n+\tpublic static void register(final MergeStrategy imp) {\n+\t\tregister(imp.getName(), imp);\n+\t}\n+\n+\t/**\n+\t * Register a merge strategy so it can later be obtained by name.\n+\t * \n+\t * @param name\n+\t *            name the strategy can be looked up under.\n+\t * @param imp\n+\t *            the strategy to register.\n+\t * @throws IllegalArgumentException\n+\t *             a strategy by the same name has already been registered.\n+\t */\n+\tpublic static synchronized void register(final String name,\n+\t\t\tfinal MergeStrategy imp) {\n+\t\tif (STRATEGIES.containsKey(name))\n+\t\t\tthrow new IllegalArgumentException(\"Merge strategy \\\"\" + name\n+\t\t\t\t\t+ \"\\\" already exists as a default strategy\");\n+\t\tSTRATEGIES.put(name, imp);\n+\t}\n+\n+\t/**\n+\t * Locate a strategy by name.\n+\t * \n+\t * @param name\n+\t *            name of the strategy to locate.\n+\t * @return the strategy instance; null if no strategy matches the name.\n+\t */\n+\tpublic static synchronized MergeStrategy get(final String name) {\n+\t\treturn STRATEGIES.get(name);\n+\t}\n+\n+\t/**\n+\t * Get all registered strategies.\n+\t * \n+\t * @return the registered strategy instances. No inherit order is returned;\n+\t *         the caller may modify (and/or sort) the returned array if\n+\t *         necessary to obtain a reasonable ordering.\n+\t */\n+\tpublic static synchronized MergeStrategy[] get() {\n+\t\tfinal MergeStrategy[] r = new MergeStrategy[STRATEGIES.size()];\n+\t\tSTRATEGIES.values().toArray(r);\n+\t\treturn r;\n+\t}\n+\n+\t/** @return default name of this strategy implementation. */\n+\tpublic abstract String getName();\n+\n+\t/**\n+\t * Create a new merge instance.\n+\t * \n+\t * @param db\n+\t *            repository database the merger will read from, and eventually\n+\t *            write results back to.\n+\t * @return the new merge instance which implements this strategy.\n+\t */\n+\tpublic abstract Merger newMerger(Repository db);\n+}\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java b/org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java\nnew file mode 100644\nindex 0000000..fb8d7b2\n--- /dev/null\n+++ b/org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java\n@@ -0,0 +1,207 @@\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.merge;\n+\n+import java.io.IOException;\n+\n+import org.spearce.jgit.errors.IncorrectObjectTypeException;\n+import org.spearce.jgit.lib.AnyObjectId;\n+import org.spearce.jgit.lib.Constants;\n+import org.spearce.jgit.lib.ObjectId;\n+import org.spearce.jgit.lib.ObjectWriter;\n+import org.spearce.jgit.lib.Repository;\n+import org.spearce.jgit.revwalk.RevCommit;\n+import org.spearce.jgit.revwalk.RevObject;\n+import org.spearce.jgit.revwalk.RevTree;\n+import org.spearce.jgit.revwalk.RevWalk;\n+import org.spearce.jgit.revwalk.filter.RevFilter;\n+import org.spearce.jgit.treewalk.AbstractTreeIterator;\n+import org.spearce.jgit.treewalk.CanonicalTreeParser;\n+import org.spearce.jgit.treewalk.EmptyTreeIterator;\n+\n+/**\n+ * Instance of a specific {@link MergeStrategy} for a single {@link Repository}.\n+ */\n+public abstract class Merger {\n+\t/** The repository this merger operates on. */\n+\tprotected final Repository db;\n+\n+\t/** A RevWalk for computing merge bases, or listing incoming commits. */\n+\tprotected final RevWalk walk;\n+\n+\tprivate ObjectWriter writer;\n+\n+\t/** The original objects supplied in the merge; this can be any tree-ish. */\n+\tprotected RevObject[] sourceObjects;\n+\n+\t/** If {@link #sourceObjects}[i] is a commit, this is the commit. */\n+\tprotected RevCommit[] sourceCommits;\n+\n+\t/** The trees matching every entry in {@link #sourceObjects}. */\n+\tprotected RevTree[] sourceTrees;\n+\n+\t/**\n+\t * Create a new merge instance for a repository.\n+\t * \n+\t * @param local\n+\t *            the repository this merger will read and write data on.\n+\t */\n+\tprotected Merger(final Repository local) {\n+\t\tdb = local;\n+\t\twalk = new RevWalk(db);\n+\t}\n+\n+\t/**\n+\t * @return the repository this merger operates on.\n+\t */\n+\tpublic Repository getRepository() {\n+\t\treturn db;\n+\t}\n+\n+\t/**\n+\t * @return an object writer to create objects in {@link #getRepository()}.\n+\t */\n+\tpublic ObjectWriter getObjectWriter() {\n+\t\tif (writer == null)\n+\t\t\twriter = new ObjectWriter(getRepository());\n+\t\treturn writer;\n+\t}\n+\n+\t/**\n+\t * Merge together two or more tree-ish objects.\n+\t * <p>\n+\t * Any tree-ish may be supplied as inputs. Commits and/or tags pointing at\n+\t * trees or commits may be passed as input objects.\n+\t * \n+\t * @param tips\n+\t *            source trees to be combined together. The merge base is not\n+\t *            included in this set.\n+\t * @return true if the merge was completed without conflicts; false if the\n+\t *         merge strategy cannot handle this merge or there were conflicts\n+\t *         preventing it from automatically resolving all paths.\n+\t * @throws IncorrectObjectTypeException\n+\t *             one of the input objects is not a commit, but the strategy\n+\t *             requires it to be a commit.\n+\t * @throws IOException\n+\t *             one or more sources could not be read, or outputs could not\n+\t *             be written to the Repository.\n+\t */\n+\tpublic final boolean merge(final AnyObjectId[] tips) throws IOException {\n+\t\tsourceObjects = new RevObject[tips.length];\n+\t\tfor (int i = 0; i < tips.length; i++)\n+\t\t\tsourceObjects[i] = walk.parseAny(tips[i]);\n+\n+\t\tsourceCommits = new RevCommit[sourceObjects.length];\n+\t\tfor (int i = 0; i < sourceObjects.length; i++) {\n+\t\t\ttry {\n+\t\t\t\tsourceCommits[i] = walk.parseCommit(sourceObjects[i]);\n+\t\t\t} catch (IncorrectObjectTypeException err) {\n+\t\t\t\tsourceCommits[i] = null;\n+\t\t\t}\n+\t\t}\n+\n+\t\tsourceTrees = new RevTree[sourceObjects.length];\n+\t\tfor (int i = 0; i < sourceObjects.length; i++)\n+\t\t\tsourceTrees[i] = walk.parseTree(sourceObjects[i]);\n+\n+\t\treturn mergeImpl();\n+\t}\n+\n+\t/**\n+\t * Create an iterator to walk the merge base of two commits.\n+\t * \n+\t * @param aIdx\n+\t *            index of the first commit in {@link #sourceObjects}.\n+\t * @param bIdx\n+\t *            index of the second commit in {@link #sourceObjects}.\n+\t * @return the new iterator\n+\t * @throws IncorrectObjectTypeException\n+\t *             one of the input objects is not a commit.\n+\t * @throws IOException\n+\t *             objects are missing or multiple merge bases were found.\n+\t */\n+\tprotected AbstractTreeIterator mergeBase(final int aIdx, final int bIdx)\n+\t\t\tthrows IOException {\n+\t\tif (sourceCommits[aIdx] == null)\n+\t\t\tthrow new IncorrectObjectTypeException(sourceObjects[aIdx],\n+\t\t\t\t\tConstants.TYPE_COMMIT);\n+\t\tif (sourceCommits[bIdx] == null)\n+\t\t\tthrow new IncorrectObjectTypeException(sourceObjects[bIdx],\n+\t\t\t\t\tConstants.TYPE_COMMIT);\n+\n+\t\twalk.reset();\n+\t\twalk.setRevFilter(RevFilter.MERGE_BASE);\n+\t\twalk.markStart(sourceCommits[aIdx]);\n+\t\twalk.markStart(sourceCommits[bIdx]);\n+\t\tfinal RevCommit base = walk.next();\n+\t\tif (base == null)\n+\t\t\treturn new EmptyTreeIterator();\n+\t\tfinal RevCommit base2 = walk.next();\n+\t\tif (base2 != null) {\n+\t\t\tthrow new IOException(\"Multiple merge bases for:\" + \"\\n  \"\n+\t\t\t\t\t+ sourceCommits[aIdx].name() + \"\\n  \"\n+\t\t\t\t\t+ sourceCommits[bIdx].name() + \"found:\" + \"\\n  \"\n+\t\t\t\t\t+ base.name() + \"\\n  \" + base2.name());\n+\t\t}\n+\t\treturn new CanonicalTreeParser(null, db, base.getTree());\n+\t}\n+\n+\t/**\n+\t * Execute the merge.\n+\t * <p>\n+\t * This method is called from {@link #merge(AnyObjectId[])} after the\n+\t * {@link #sourceObjects}, {@link #sourceCommits} and {@link #sourceTrees}\n+\t * have been populated.\n+\t * \n+\t * @return true if the merge was completed without conflicts; false if the\n+\t *         merge strategy cannot handle this merge or there were conflicts\n+\t *         preventing it from automatically resolving all paths.\n+\t * @throws IncorrectObjectTypeException\n+\t *             one of the input objects is not a commit, but the strategy\n+\t *             requires it to be a commit.\n+\t * @throws IOException\n+\t *             one or more sources could not be read, or outputs could not\n+\t *             be written to the Repository.\n+\t */\n+\tprotected abstract boolean mergeImpl() throws IOException;\n+\n+\t/**\n+\t * @return resulting tree, if {@link #merge(AnyObjectId[])} returned true.\n+\t */\n+\tpublic abstract ObjectId getResultTreeId();\n+}\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java b/org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java\nnew file mode 100644\nindex 0000000..0c3dcc2\n--- /dev/null\n+++ b/org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java\n@@ -0,0 +1,98 @@\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.merge;\n+\n+import java.io.IOException;\n+\n+import org.spearce.jgit.lib.ObjectId;\n+import org.spearce.jgit.lib.Repository;\n+\n+/**\n+ * Trivial merge strategy to make the resulting tree exactly match an input.\n+ * <p>\n+ * This strategy can be used to cauterize an entire side branch of history, by\n+ * setting the output tree to one of the inputs, and ignoring any of the paths\n+ * of the other inputs.\n+ */\n+public class StrategyOneSided extends MergeStrategy {\n+\tprivate final String strategyName;\n+\n+\tprivate final int treeIndex;\n+\n+\t/**\n+\t * Create a new merge strategy to select a specific input tree.\n+\t * \n+\t * @param name\n+\t *            name of this strategy.\n+\t * @param index\n+\t *            the position of the input tree to accept as the result.\n+\t */\n+\tprotected StrategyOneSided(final String name, final int index) {\n+\t\tstrategyName = name;\n+\t\ttreeIndex = index;\n+\t}\n+\n+\t@Override\n+\tpublic String getName() {\n+\t\treturn strategyName;\n+\t}\n+\n+\t@Override\n+\tpublic Merger newMerger(final Repository db) {\n+\t\treturn new OneSide(db, treeIndex);\n+\t}\n+\n+\tprotected static class OneSide extends Merger {\n+\t\tprivate final int treeIndex;\n+\n+\t\tprotected OneSide(final Repository local, final int index) {\n+\t\t\tsuper(local);\n+\t\t\ttreeIndex = index;\n+\t\t}\n+\n+\t\t@Override\n+\t\tprotected boolean mergeImpl() throws IOException {\n+\t\t\treturn treeIndex < sourceTrees.length;\n+\t\t}\n+\n+\t\t@Override\n+\t\tpublic ObjectId getResultTreeId() {\n+\t\t\treturn sourceTrees[treeIndex];\n+\t\t}\n+\t}\n+}\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java b/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java\nnew file mode 100644\nindex 0000000..893add9\n--- /dev/null\n+++ b/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java\n@@ -0,0 +1,179 @@\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.merge;\n+\n+import java.io.IOException;\n+\n+import org.spearce.jgit.dircache.DirCache;\n+import org.spearce.jgit.dircache.DirCacheBuilder;\n+import org.spearce.jgit.dircache.DirCacheEntry;\n+import org.spearce.jgit.errors.UnmergedPathException;\n+import org.spearce.jgit.lib.ObjectId;\n+import org.spearce.jgit.lib.Repository;\n+import org.spearce.jgit.treewalk.AbstractTreeIterator;\n+import org.spearce.jgit.treewalk.NameConflictTreeWalk;\n+\n+/**\n+ * Merges two commits together in-memory, ignoring any working directory.\n+ * <p>\n+ * The strategy chooses a path from one of the two input trees if the path is\n+ * unchanged in the other relative to their common merge base tree. This is a\n+ * trivial 3-way merge (at the file path level only).\n+ * <p>\n+ * Modifications of the same file path (content and/or file mode) by both input\n+ * trees will cause a merge conflict, as this strategy does not attempt to merge\n+ * file contents.\n+ */\n+public class StrategySimpleTwoWayInCore extends MergeStrategy {\n+\tstatic final MergeStrategy INSTANCE = new StrategySimpleTwoWayInCore();\n+\n+\t/** Create a new instance of the strategy. */\n+\tprotected StrategySimpleTwoWayInCore() {\n+\t\t//\n+\t}\n+\n+\t@Override\n+\tpublic String getName() {\n+\t\treturn \"simple-two-way-in-core\";\n+\t}\n+\n+\t@Override\n+\tpublic Merger newMerger(final Repository db) {\n+\t\treturn new InCoreMerger(db);\n+\t}\n+\n+\tprivate static class InCoreMerger extends Merger {\n+\t\tprivate static final int T_BASE = 0;\n+\n+\t\tprivate static final int T_OURS = 1;\n+\n+\t\tprivate static final int T_THEIRS = 2;\n+\n+\t\tprivate final NameConflictTreeWalk tw;\n+\n+\t\tprivate final DirCache cache;\n+\n+\t\tprivate DirCacheBuilder builder;\n+\n+\t\tprivate ObjectId resultTree;\n+\n+\t\tInCoreMerger(final Repository local) {\n+\t\t\tsuper(local);\n+\t\t\ttw = new NameConflictTreeWalk(db);\n+\t\t\tcache = DirCache.newInCore();\n+\t\t}\n+\n+\t\t@Override\n+\t\tprotected boolean mergeImpl() throws IOException {\n+\t\t\tif (sourceTrees.length != 2)\n+\t\t\t\treturn false;\n+\n+\t\t\ttw.reset();\n+\t\t\ttw.addTree(mergeBase(0, 1));\n+\t\t\ttw.addTree(sourceTrees[0]);\n+\t\t\ttw.addTree(sourceTrees[1]);\n+\n+\t\t\tboolean hasConflict = false;\n+\t\t\tbuilder = cache.builder();\n+\t\t\twhile (tw.next()) {\n+\t\t\t\tfinal int modeO = tw.getRawMode(T_OURS);\n+\t\t\t\tfinal int modeT = tw.getRawMode(T_THEIRS);\n+\t\t\t\tif (modeO == modeT && tw.idEqual(T_OURS, T_THEIRS)) {\n+\t\t\t\t\tsame();\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\n+\t\t\t\tfinal int modeB = tw.getRawMode(T_BASE);\n+\t\t\t\tif (modeB == modeO && tw.idEqual(T_BASE, T_OURS))\n+\t\t\t\t\tadd(T_THEIRS, DirCacheEntry.STAGE_0);\n+\t\t\t\telse if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS))\n+\t\t\t\t\tadd(T_OURS, DirCacheEntry.STAGE_0);\n+\t\t\t\telse {\n+\t\t\t\t\tconflict();\n+\t\t\t\t\thasConflict = true;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tbuilder.finish();\n+\t\t\tbuilder = null;\n+\n+\t\t\tif (hasConflict)\n+\t\t\t\treturn false;\n+\t\t\ttry {\n+\t\t\t\tresultTree = cache.writeTree(getObjectWriter());\n+\t\t\t\treturn true;\n+\t\t\t} catch (UnmergedPathException upe) {\n+\t\t\t\tresultTree = null;\n+\t\t\t\treturn false;\n+\t\t\t}\n+\t\t}\n+\n+\t\tprivate void same() throws IOException {\n+\t\t\tif (tw.isSubtree())\n+\t\t\t\tbuilder.addTree(tw.getRawPath(), db, tw.getObjectId(1));\n+\t\t\telse\n+\t\t\t\tadd(T_OURS, DirCacheEntry.STAGE_0);\n+\t\t}\n+\n+\t\tprivate void conflict() {\n+\t\t\tadd(T_BASE, DirCacheEntry.STAGE_1);\n+\t\t\tadd(T_OURS, DirCacheEntry.STAGE_2);\n+\t\t\tadd(T_THEIRS, DirCacheEntry.STAGE_3);\n+\t\t}\n+\n+\t\tprivate void add(final int tree, final int stage) {\n+\t\t\tfinal AbstractTreeIterator i = getTree(tree);\n+\t\t\tif (i != null) {\n+\t\t\t\tfinal DirCacheEntry e;\n+\n+\t\t\t\te = new DirCacheEntry(tw.getRawPath(), stage);\n+\t\t\t\te.setObjectIdFromRaw(i.idBuffer(), i.idOffset());\n+\t\t\t\te.setFileMode(tw.getFileMode(tree));\n+\t\t\t\tbuilder.add(e);\n+\t\t\t}\n+\t\t}\n+\n+\t\tprivate AbstractTreeIterator getTree(final int tree) {\n+\t\t\treturn tw.getTree(tree, AbstractTreeIterator.class);\n+\t\t}\n+\n+\t\t@Override\n+\t\tpublic ObjectId getResultTreeId() {\n+\t\t\treturn resultTree;\n+\t\t}\n+\t}\n+}\n-- \n1.6.0.2.706.g340fc\n"},{"id":"93804","messageId":"200810232314.29867.robin.rosenberg@dewire.com","threadId":"15883","inReplyTo":"1223932217-4771-9-git-send-email-spearce@spearce.org","subject":"Re: [JGIT PATCH 8/8] Define a basic merge API, and a two-way tree merge strategy","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2008-10-23T21:14:29Z","receivedAt":"2008-10-23T21:14:29Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"Hi, Shawn\n\nShouldn't testTrivialTwoWay_disjointhistories()  work?\n\nThe two trees have nothing in common and so should be trivially mergeable.\n\n-- robin\n\n>From cef2695431e368da616a1e9c8de3e5e419854a4c Mon Sep 17 00:00:00 2001\nFrom: Robin Rosenberg <robin.rosenberg@dewire.com>\nDate: Thu, 23 Oct 2008 23:09:10 +0200\nSubject: [EGIT PATCH] Simple merge test\n\n---\n .../org/spearce/jgit/merge/SimpleMergeTest.java    |   28 ++++++++++++++++++++\n 1 files changed, 28 insertions(+), 0 deletions(-)\n create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java\n\ndiff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java\nnew file mode 100644\nindex 0000000..8ec1c7f\n--- /dev/null\n+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java\n@@ -0,0 +1,28 @@\n+package org.spearce.jgit.merge;\n+\n+import java.io.IOException;\n+\n+import org.spearce.jgit.lib.ObjectId;\n+import org.spearce.jgit.lib.RepositoryTestCase;\n+\n+public class SimpleMergeTest extends RepositoryTestCase {\n+\n+\tpublic void testTrivialTwoWay_noway() throws IOException {\n+\t\tMerger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db);\n+\t\tboolean merge = ourMerger.merge(new ObjectId[] { db.resolve(\"a\"), db.resolve(\"c\") });\n+\t\tassertFalse(merge);\n+\t}\n+\n+\tpublic void testTrivialTwoWay_disjointhistories() throws IOException {\n+\t\tMerger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db);\n+\t\tboolean merge = ourMerger.merge(new ObjectId[] { db.resolve(\"a\"), db.resolve(\"c~4\") });\n+\t\tassertTrue(merge); \n+\t}\n+\n+\tpublic void testTrivialTwoWay_ok() throws IOException {\n+\t\tMerger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db);\n+\t\tboolean merge = ourMerger.merge(new ObjectId[] { db.resolve(\"a^0^0^0\"), db.resolve(\"a^0^0^1\") });\n+\t\tassertTrue(merge);\n+\t\tassertEquals(db.mapTree(\"a^0^0\").getId(), ourMerger.getResultTreeId());\n+\t}\n+}\n-- \n1.6.0.2.308.gef4a\n"},{"id":"100657","messageId":"200901152205.00600.robin.rosenberg@dewire.com","threadId":"15883","inReplyTo":"200810232314.29867.robin.rosenberg@dewire.com","subject":"Re: [JGIT PATCH 8/8] Define a basic merge API, and a two-way tree merge strategy","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2009-01-15T21:05:00Z","receivedAt":"2009-01-15T21:05:00Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"I never got a  received reply... on this.\n\n-- robin\n\ntorsdag 23 oktober 2008 23:14:29 skrev Robin Rosenberg:\n> Hi, Shawn\n> \n> Shouldn't testTrivialTwoWay_disjointhistories()  work?\n> \n> The two trees have nothing in common and so should be trivially mergeable.\n> \n> -- robin\n> \n> From cef2695431e368da616a1e9c8de3e5e419854a4c Mon Sep 17 00:00:00 2001\n> From: Robin Rosenberg <robin.rosenberg@dewire.com>\n> Date: Thu, 23 Oct 2008 23:09:10 +0200\n> Subject: [EGIT PATCH] Simple merge test\n> \n> ---\n>  .../org/spearce/jgit/merge/SimpleMergeTest.java    |   28 ++++++++++++++++++++\n>  1 files changed, 28 insertions(+), 0 deletions(-)\n>  create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java\n> \n> diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java\n> new file mode 100644\n> index 0000000..8ec1c7f\n> --- /dev/null\n> +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java\n> @@ -0,0 +1,28 @@\n> +package org.spearce.jgit.merge;\n> +\n> +import java.io.IOException;\n> +\n> +import org.spearce.jgit.lib.ObjectId;\n> +import org.spearce.jgit.lib.RepositoryTestCase;\n> +\n> +public class SimpleMergeTest extends RepositoryTestCase {\n> +\n> +\tpublic void testTrivialTwoWay_noway() throws IOException {\n> +\t\tMerger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db);\n> +\t\tboolean merge = ourMerger.merge(new ObjectId[] { db.resolve(\"a\"), db.resolve(\"c\") });\n> +\t\tassertFalse(merge);\n> +\t}\n> +\n> +\tpublic void testTrivialTwoWay_disjointhistories() throws IOException {\n> +\t\tMerger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db);\n> +\t\tboolean merge = ourMerger.merge(new ObjectId[] { db.resolve(\"a\"), db.resolve(\"c~4\") });\n> +\t\tassertTrue(merge); \n> +\t}\n> +\n> +\tpublic void testTrivialTwoWay_ok() throws IOException {\n> +\t\tMerger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db);\n> +\t\tboolean merge = ourMerger.merge(new ObjectId[] { db.resolve(\"a^0^0^0\"), db.resolve(\"a^0^0^1\") });\n> +\t\tassertTrue(merge);\n> +\t\tassertEquals(db.mapTree(\"a^0^0\").getId(), ourMerger.getResultTreeId());\n> +\t}\n> +}\n"},{"id":"100659","messageId":"20090115210936.GI10179@spearce.org","threadId":"15883","inReplyTo":"200901152205.00600.robin.rosenberg@dewire.com","subject":"Re: [JGIT PATCH 8/8] Define a basic merge API, and a two-way tree merge strategy","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-01-15T21:09:36Z","receivedAt":"2009-01-15T21:09:36Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Robin Rosenberg <robin.rosenberg@dewire.com> wrote:\n> I never got a  received reply... on this.\n\nSorry.  Its in my \"pending\" queue.  I'm still using the code\nin Gerrit but I've been so swamped that I haven't been able to\nlook at your test case, or what's wrong with the code and why it\ndoesn't pass.\n\nI honestly hoped to have something by this point, but I got behind\nand I haven't quite had a chance to look at it.\n \n> torsdag 23 oktober 2008 23:14:29 skrev Robin Rosenberg:\n> > Hi, Shawn\n> > \n> > Shouldn't testTrivialTwoWay_disjointhistories()  work?\n> > \n> > The two trees have nothing in common and so should be trivially mergeable.\n> > \n> > -- robin\n> > \n> > From cef2695431e368da616a1e9c8de3e5e419854a4c Mon Sep 17 00:00:00 2001\n> > From: Robin Rosenberg <robin.rosenberg@dewire.com>\n> > Date: Thu, 23 Oct 2008 23:09:10 +0200\n> > Subject: [EGIT PATCH] Simple merge test\n> > \n> > ---\n> >  .../org/spearce/jgit/merge/SimpleMergeTest.java    |   28 ++++++++++++++++++++\n> >  1 files changed, 28 insertions(+), 0 deletions(-)\n> >  create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java\n\n-- \nShawn.\n"},{"id":"100871","messageId":"f299b4f30901171116y216835c9jc11df2d424ee0377@mail.gmail.com","threadId":"15883","inReplyTo":"20090115210936.GI10179@spearce.org","subject":"Re: [JGIT PATCH 8/8] Define a basic merge API, and a two-way tree merge strategy","fromName":"Tomi Pakarinen","fromEmail":"tomi.pakarinen@gmail.com","sentAt":"2009-01-17T19:16:21Z","receivedAt":"2009-01-17T19:16:21Z","isPatch":true,"sender":{"key":"tomi.pakarinen@gmail.com","avatar":null},"body":"testTrivialTwoWay_disjointhistories() failed because merge strategy\ndidn't handle missing base\nversion. Am'i right?\n\n  Tomi.\n\n>From 1ed694b55d307c640d29eeebfcd108e08681297b Mon Sep 17 00:00:00 2001\nFrom: Tomi Pakarinen <tomi.pakarinen@iki.fi>\nDate: Sat, 17 Jan 2009 20:56:04 +0200\nSubject: [PATCH] If base version missing, we can merge version from\none of other trees.\n\nSigned-off-by: Tomi Pakarinen <tomi.pakarinen@iki.fi>\n---\n .../jgit/merge/StrategySimpleTwoWayInCore.java     |   28 +++++++++++++++-----\n 1 files changed, 21 insertions(+), 7 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java\nb/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java\nindex 893add9..eb718ab 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java\n@@ -43,6 +43,7 @@\n import org.spearce.jgit.dircache.DirCacheBuilder;\n import org.spearce.jgit.dircache.DirCacheEntry;\n import org.spearce.jgit.errors.UnmergedPathException;\n+import org.spearce.jgit.lib.FileMode;\n import org.spearce.jgit.lib.ObjectId;\n import org.spearce.jgit.lib.Repository;\n import org.spearce.jgit.treewalk.AbstractTreeIterator;\n@@ -119,13 +120,26 @@ protected boolean mergeImpl() throws IOException {\n \t\t\t\t}\n\n \t\t\t\tfinal int modeB = tw.getRawMode(T_BASE);\n-\t\t\t\tif (modeB == modeO && tw.idEqual(T_BASE, T_OURS))\n-\t\t\t\t\tadd(T_THEIRS, DirCacheEntry.STAGE_0);\n-\t\t\t\telse if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS))\n-\t\t\t\t\tadd(T_OURS, DirCacheEntry.STAGE_0);\n-\t\t\t\telse {\n-\t\t\t\t\tconflict();\n-\t\t\t\t\thasConflict = true;\n+\t\t\t\tif (!FileMode.MISSING.equals(modeB)) {\n+\t\t\t\t\tif (modeB == modeO && tw.idEqual(T_BASE, T_OURS))\n+\t\t\t\t\t\tadd(T_THEIRS, DirCacheEntry.STAGE_0);\n+\t\t\t\t\telse if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS))\n+\t\t\t\t\t\tadd(T_OURS, DirCacheEntry.STAGE_0);\n+\t\t\t\t\telse {\n+\t\t\t\t\t\tconflict();\n+\t\t\t\t\t\thasConflict = true;\n+\t\t\t\t\t}\n+\t\t\t\t} else {\n+\t\t\t\t\tif (!FileMode.MISSING.equals(modeO)\n+\t\t\t\t\t\t\t&& FileMode.MISSING.equals(modeT))\n+\t\t\t\t\t\tadd(T_OURS, DirCacheEntry.STAGE_0);\n+\t\t\t\t\telse if (FileMode.MISSING.equals(modeO)\n+\t\t\t\t\t\t\t&& !FileMode.MISSING.equals(modeT))\n+\t\t\t\t\t\tadd(T_THEIRS, DirCacheEntry.STAGE_0);\n+\t\t\t\t\telse {\n+\t\t\t\t\t\tconflict();\n+\t\t\t\t\t\thasConflict = true;\n+\t\t\t\t\t}\n \t\t\t\t}\n \t\t\t}\n \t\t\tbuilder.finish();\n-- \n1.6.0.4\n"},{"id":"101052","messageId":"200901182121.01469.robin.rosenberg.lists@dewire.com","threadId":"15883","inReplyTo":"f299b4f30901171116y216835c9jc11df2d424ee0377@mail.gmail.com","subject":"Re: [JGIT PATCH 8/8] Define a basic merge API, and a two-way tree merge strategy","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg.lists@dewire.com","sentAt":"2009-01-18T20:21:01Z","receivedAt":"2009-01-18T20:21:01Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"lördag 17 januari 2009 20:16:21 skrev Tomi Pakarinen:\n> testTrivialTwoWay_disjointhistories() failed because merge strategy\n> didn't handle missing base\n> version. Am'i right?\nKyllä\n\nOr so it seems. My test cases pass. I'll see if I can come up with somehing nasty.\nIf not I'll apply \n\n-- robin\n"},{"id":"101150","messageId":"20090119174254.GC14053@spearce.org","threadId":"15883","inReplyTo":"f299b4f30901171116y216835c9jc11df2d424ee0377@mail.gmail.com","subject":"Re: [JGIT PATCH 8/8] Define a basic merge API, and a two-way tree merge strategy","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-01-19T17:42:54Z","receivedAt":"2009-01-19T17:42:54Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Tomi Pakarinen <tomi.pakarinen@gmail.com> wrote:\n> testTrivialTwoWay_disjointhistories() failed because merge strategy\n> didn't handle missing base\n> version. Am'i right?\n\nI don't think so.\n \n> @@ -119,13 +120,26 @@ protected boolean mergeImpl() throws IOException {\n>  \t\t\t\t}\n> \n>  \t\t\t\tfinal int modeB = tw.getRawMode(T_BASE);\n\nUnder a missing base condition modeB == 0.  So,\n\n> -\t\t\t\tif (modeB == modeO && tw.idEqual(T_BASE, T_OURS))\n> -\t\t\t\t\tadd(T_THEIRS, DirCacheEntry.STAGE_0);\n\nIf modeB == 0 and modeO == 0 its not in the base and its not in ours.\nBoth SHA-1s will be 0{40} and are thus idEqual, so we should enter\nthis add(T_THEIRS) block.  Which is what you tried to write below in\nyour else block, isn't it?.\n\n> -\t\t\t\telse if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS))\n> -\t\t\t\t\tadd(T_OURS, DirCacheEntry.STAGE_0);\n\nAgain, if modeB == 0 and modeT == 0 both SHA-1s will be 0{40} and\nare idEqual, so we should enter this add(T_OURS) block if both base\nand theirs are missing.  Which again is what you tried to write in\nyour else block.\n\nIf that isn't coming out right then perhaps tw.idEqual() is busted\nfor when FileMode is FileMode.MISSING (aka 0).  Granted, doing\nidEqual on FileMode.MISSING is pointless and just wastes clock\ncycles, but it shouldn't harm the algorithm's correctness.\n\n> -\t\t\t\telse {\n> -\t\t\t\t\tconflict();\n> -\t\t\t\t\thasConflict = true;\n> +\t\t\t\tif (!FileMode.MISSING.equals(modeB)) {\n> +\t\t\t\t\tif (modeB == modeO && tw.idEqual(T_BASE, T_OURS))\n> +\t\t\t\t\t\tadd(T_THEIRS, DirCacheEntry.STAGE_0);\n> +\t\t\t\t\telse if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS))\n> +\t\t\t\t\t\tadd(T_OURS, DirCacheEntry.STAGE_0);\n> +\t\t\t\t\telse {\n> +\t\t\t\t\t\tconflict();\n> +\t\t\t\t\t\thasConflict = true;\n> +\t\t\t\t\t}\n> +\t\t\t\t} else {\n> +\t\t\t\t\tif (!FileMode.MISSING.equals(modeO)\n> +\t\t\t\t\t\t\t&& FileMode.MISSING.equals(modeT))\n> +\t\t\t\t\t\tadd(T_OURS, DirCacheEntry.STAGE_0);\n> +\t\t\t\t\telse if (FileMode.MISSING.equals(modeO)\n> +\t\t\t\t\t\t\t&& !FileMode.MISSING.equals(modeT))\n> +\t\t\t\t\t\tadd(T_THEIRS, DirCacheEntry.STAGE_0);\n> +\t\t\t\t\telse {\n> +\t\t\t\t\t\tconflict();\n> +\t\t\t\t\t\thasConflict = true;\n> +\t\t\t\t\t}\n>  \t\t\t\t}\n>  \t\t\t}\n>  \t\t\tbuilder.finish();\n> -- \n> 1.6.0.4\n\n-- \nShawn.\n"},{"id":"101152","messageId":"20090119175154.GD14053@spearce.org","threadId":"15883","inReplyTo":"20090119174254.GC14053@spearce.org","subject":"Re: [JGIT PATCH 8/8] Define a basic merge API, and a two-way tree merge strategy","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-01-19T17:51:54Z","receivedAt":"2009-01-19T17:51:54Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"\"Shawn O. Pearce\" <spearce@spearce.org> wrote:\n> Tomi Pakarinen <tomi.pakarinen@gmail.com> wrote:\n> > testTrivialTwoWay_disjointhistories() failed because merge strategy\n> > didn't handle missing base\n> > version. Am'i right?\n> \n> If that isn't coming out right then perhaps tw.idEqual() is busted\n\nYup, that's what it is, idEqual is busted.\n\nThe definition of TreeWalk.idEqual is:\n\n\tpublic boolean idEqual(final int nthA, final int nthB) {\n\t\tfinal AbstractTreeIterator ch = currentHead;\n\t\tfinal AbstractTreeIterator a = trees[nthA];\n\t\tfinal AbstractTreeIterator b = trees[nthB];\n\t\treturn a.matches == ch && b.matches == ch && a.idEqual(b);\n\t}\n\nThe problem is this method always returns false if the name isn't\ndefined in either path.  I think this is the definition we want\ninstead:\n\n\t\tif (a.matches == ch && b.matches == ch)\n\t\t\treturn a.idEqual(b);\n\t\tif (a.matches != ch && b.matches != ch) {\n\t\t\t// If neither tree matches the current path node then neither\n\t\t\t// tree has this entry. In such case the ObjectId is zero(),\n\t\t\t// and zero() is always equal to zero().\n\t\t\t//\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\nPatch to follow.\n\n-- \nShawn.\n"}]}