* [JGIT PATCH 00/10] Merge API v2 @ 2009-01-22 23:28 Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 01/10] Fix TreeWalk.idEqual when both trees are missing the path Shawn O. Pearce 0 siblings, 1 reply; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git 2nd attempt at the merge API. Changes from my prior posting last fall: - Fixed the bug Robin identified (the first patch in the series) - Rebased against current master - Basic smoke tests provided by Robin Shawn O. Pearce (10): Fix TreeWalk.idEqual when both trees are missing the path Expose the raw path for the current entry of a TreeWalk Expose DirCacheEntry.getFileMode as a utility function Add writeTree support to DirCache Allow a DirCache to be created with no backing store file Allow CanonicalTreeParsers to be created with a UTF-8 path prefix Recursively load an entire tree into a DirCacheBuilder Allow DirCacheEntry instances to be created with stage > 0 Define a basic merge API, and a two-way tree merge strategy Add a few simple merge test cases .../spearce/jgit/test/resources/create-second-pack | 13 +- ...ck-3280af9c07ee18a87705ef50b0cc4cd20266cf12.idx | Bin 0 -> 1296 bytes ...k-3280af9c07ee18a87705ef50b0cc4cd20266cf12.pack | Bin 0 -> 562 bytes ...ck-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745.idx | Bin 1088 -> 1100 bytes ...ck-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.idx | Bin 2696 -> 2976 bytes ...k-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.pack | Bin 5956 -> 5901 bytes .../org/spearce/jgit/test/resources/packed-refs | 4 +- .../org/spearce/jgit/lib/RepositoryTestCase.java | 3 +- .../org/spearce/jgit/merge/SimpleMergeTest.java | 86 ++++++++ .../org/spearce/jgit/transport/TransportTest.java | 2 +- .../src/org/spearce/jgit/dircache/DirCache.java | 37 ++++ .../org/spearce/jgit/dircache/DirCacheBuilder.java | 65 ++++++ .../org/spearce/jgit/dircache/DirCacheEntry.java | 61 +++++- .../org/spearce/jgit/dircache/DirCacheTree.java | 115 +++++++++++ .../spearce/jgit/errors/UnmergedPathException.java | 67 ++++++ .../src/org/spearce/jgit/lib/FileMode.java | 7 + .../src/org/spearce/jgit/lib/ObjectWriter.java | 12 +- .../src/org/spearce/jgit/merge/MergeStrategy.java | 134 ++++++++++++ .../src/org/spearce/jgit/merge/Merger.java | 213 ++++++++++++++++++++ .../org/spearce/jgit/merge/StrategyOneSided.java | 98 +++++++++ .../jgit/merge/StrategySimpleTwoWayInCore.java | 179 ++++++++++++++++ .../jgit/treewalk/AbstractTreeIterator.java | 31 +++ .../spearce/jgit/treewalk/CanonicalTreeParser.java | 30 +++ .../src/org/spearce/jgit/treewalk/TreeWalk.java | 28 +++- 24 files changed, 1170 insertions(+), 15 deletions(-) create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.idx create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.pack create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java ^ permalink raw reply [flat|nested] 12+ messages in thread
* [JGIT PATCH 01/10] Fix TreeWalk.idEqual when both trees are missing the path 2009-01-22 23:28 [JGIT PATCH 00/10] Merge API v2 Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 02/10] Expose the raw path for the current entry of a TreeWalk Shawn O. Pearce 0 siblings, 1 reply; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git The Javadoc of idEqual() says its simply a faster form of getObjectId(nthA).equals(getObjectId(nthB)), but its code didn't match that definition when both trees didn't exist at the current path. If a tree doesn't exist for the current path getObjectId() returns ObjectId.zero(), indicating the "magic" 0{40} SHA-1 for the current path. If both tree entries don't exist for the current path, we should be doing a compare of ObjectId.zero() against ObjectId.zero(), which must be true as the values are the same. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../src/org/spearce/jgit/treewalk/TreeWalk.java | 11 ++++++++++- 1 files changed, 10 insertions(+), 1 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java index ecf8851..414587c 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java @@ -616,7 +616,16 @@ public boolean idEqual(final int nthA, final int nthB) { final AbstractTreeIterator ch = currentHead; final AbstractTreeIterator a = trees[nthA]; final AbstractTreeIterator b = trees[nthB]; - return a.matches == ch && b.matches == ch && a.idEqual(b); + if (a.matches == ch && b.matches == ch) + return a.idEqual(b); + if (a.matches != ch && b.matches != ch) { + // If neither tree matches the current path node then neither + // tree has this entry. In such case the ObjectId is zero(), + // and zero() is always equal to zero(). + // + return true; + } + return false; } /** -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* [JGIT PATCH 02/10] Expose the raw path for the current entry of a TreeWalk 2009-01-22 23:28 ` [JGIT PATCH 01/10] Fix TreeWalk.idEqual when both trees are missing the path Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 03/10] Expose DirCacheEntry.getFileMode as a utility function Shawn O. Pearce 2009-01-23 21:42 ` [JGIT PATCH 02/10] Expose the raw path for the current entry of a TreeWalk Robin Rosenberg 0 siblings, 2 replies; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git Copying the path byte array (keeping it encoded in UTF-8) is quicker than converting to String and then back again to UTF-8 when creating a DirCacheEntry for the current position in a TreeWalk. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../src/org/spearce/jgit/treewalk/TreeWalk.java | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java index 414587c..22040e3 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java @@ -663,6 +663,21 @@ public String getPathString() { } /** + * Get the current entry's complete path as a UTF-8 byte array. + * + * @return complete path of the current entry, from the root of the + * repository. If the current entry is in a subtree there will be at + * least one '/' in the returned string. + */ + public byte[] getRawPath() { + final AbstractTreeIterator t = currentHead; + final int n = t.pathLen; + final byte[] r = new byte[n]; + System.arraycopy(t.path, 0, r, 0, n); + return r; + } + + /** * Test if the supplied path matches the current entry's path. * <p> * This method tests that the supplied path is exactly equal to the current -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* [JGIT PATCH 03/10] Expose DirCacheEntry.getFileMode as a utility function 2009-01-22 23:28 ` [JGIT PATCH 02/10] Expose the raw path for the current entry of a TreeWalk Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 04/10] Add writeTree support to DirCache Shawn O. Pearce 2009-01-23 21:42 ` [JGIT PATCH 02/10] Expose the raw path for the current entry of a TreeWalk Robin Rosenberg 1 sibling, 1 reply; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git Its easier to get the FileMode object in some applications than to get the raw mode and convert it to the FileMode in the application code. Its slower, but sometimes you just have to have the proper FileMode singleton. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../org/spearce/jgit/dircache/DirCacheEntry.java | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java index cc683d7..355cd3e 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java +++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java @@ -295,6 +295,15 @@ public int getRawMode() { } /** + * Obtain the {@link FileMode} for this entry. + * + * @return the file mode singleton for this entry. + */ + public FileMode getFileMode() { + return FileMode.fromBits(getRawMode()); + } + + /** * Set the file mode for this entry. * * @param mode -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* [JGIT PATCH 04/10] Add writeTree support to DirCache 2009-01-22 23:28 ` [JGIT PATCH 03/10] Expose DirCacheEntry.getFileMode as a utility function Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 05/10] Allow a DirCache to be created with no backing store file Shawn O. Pearce 0 siblings, 1 reply; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git This way we can write a full tree from the DirCache, including reusing any valid tree entries stored within the 'TREE' cache extension. By reusing those entries we can avoid generating the tree objects that are already stored in the Git repository. The algorithm may cause up to 3 passes over the DirCache entries: * Pass 1: Compute the tree structure * Pass 2: Compute the sizes of each tree * Pass 3: Write the tree object to the object store These extra passes cause more CPU time to be expended in exchange for a lower memory requirement during the tree writing. The code is only formatting the lowest level leaf tree which has not yet been written to the object store, so higher level trees do not occupy memory while they are waiting for the leaves to write out. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../src/org/spearce/jgit/dircache/DirCache.java | 20 ++++ .../org/spearce/jgit/dircache/DirCacheTree.java | 115 ++++++++++++++++++++ .../spearce/jgit/errors/UnmergedPathException.java | 67 ++++++++++++ .../src/org/spearce/jgit/lib/FileMode.java | 7 ++ .../src/org/spearce/jgit/lib/ObjectWriter.java | 12 ++- 5 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java index 76657c4..b3c57ad 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java +++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java @@ -51,8 +51,11 @@ import java.util.Comparator; import org.spearce.jgit.errors.CorruptObjectException; +import org.spearce.jgit.errors.UnmergedPathException; import org.spearce.jgit.lib.Constants; import org.spearce.jgit.lib.LockFile; +import org.spearce.jgit.lib.ObjectId; +import org.spearce.jgit.lib.ObjectWriter; import org.spearce.jgit.lib.Repository; import org.spearce.jgit.util.MutableInteger; import org.spearce.jgit.util.NB; @@ -692,4 +695,21 @@ public DirCacheTree getCacheTree(final boolean build) { } return tree; } + + /** + * Write all index trees to the object store, returning the root tree. + * + * @param ow + * the writer to use when serializing to the store. + * @return identity for the root tree. + * @throws UnmergedPathException + * one or more paths contain higher-order stages (stage > 0), + * which cannot be stored in a tree object. + * @throws IOException + * an unexpected error occurred writing to the object store. + */ + public ObjectId writeTree(final ObjectWriter ow) + throws UnmergedPathException, IOException { + return getCacheTree(true).writeTree(sortedEntries, 0, 0, ow); + } } diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheTree.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheTree.java index 26b6348..cf96ded 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheTree.java +++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheTree.java @@ -1,5 +1,6 @@ /* * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> + * Copyright (C) 2008, Google Inc. * * All rights reserved. * @@ -37,14 +38,20 @@ package org.spearce.jgit.dircache; +import static org.spearce.jgit.lib.Constants.OBJECT_ID_LENGTH; + +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Comparator; +import org.spearce.jgit.errors.UnmergedPathException; import org.spearce.jgit.lib.Constants; +import org.spearce.jgit.lib.FileMode; import org.spearce.jgit.lib.ObjectId; +import org.spearce.jgit.lib.ObjectWriter; import org.spearce.jgit.util.MutableInteger; import org.spearce.jgit.util.RawParseUtils; @@ -273,6 +280,114 @@ public String getPathString() { return r.toString(); } + /** + * Write (if necessary) this tree to the object store. + * + * @param cache + * the complete cache from DirCache. + * @param cIdx + * first position of <code>cache</code> that is a member of this + * tree. The path of <code>cache[cacheIdx].path</code> for the + * range <code>[0,pathOff-1)</code> matches the complete path of + * this tree, from the root of the repository. + * @param pathOffset + * number of bytes of <code>cache[cacheIdx].path</code> that + * matches this tree's path. The value at array position + * <code>cache[cacheIdx].path[pathOff-1]</code> is always '/' if + * <code>pathOff</code> is > 0. + * @param ow + * the writer to use when serializing to the store. + * @return identity of this tree. + * @throws UnmergedPathException + * one or more paths contain higher-order stages (stage > 0), + * which cannot be stored in a tree object. + * @throws IOException + * an unexpected error occurred writing to the object store. + */ + ObjectId writeTree(final DirCacheEntry[] cache, int cIdx, + final int pathOffset, final ObjectWriter ow) + throws UnmergedPathException, IOException { + if (id == null) { + final int endIdx = cIdx + entrySpan; + final int size = computeSize(cache, cIdx, pathOffset, ow); + final ByteArrayOutputStream out = new ByteArrayOutputStream(size); + int childIdx = 0; + int entryIdx = cIdx; + + while (entryIdx < endIdx) { + final DirCacheEntry e = cache[entryIdx]; + final byte[] ep = e.path; + if (childIdx < childCnt) { + final DirCacheTree st = children[childIdx]; + if (st.contains(ep, pathOffset, ep.length)) { + FileMode.TREE.copyTo(out); + out.write(' '); + out.write(st.encodedName); + out.write(0); + st.id.copyRawTo(out); + + entryIdx += st.entrySpan; + childIdx++; + continue; + } + } + + e.getFileMode().copyTo(out); + out.write(' '); + out.write(ep, pathOffset, ep.length - pathOffset); + out.write(0); + out.write(e.idBuffer(), e.idOffset(), OBJECT_ID_LENGTH); + entryIdx++; + } + + id = ow.writeCanonicalTree(out.toByteArray()); + } + return id; + } + + private int computeSize(final DirCacheEntry[] cache, int cIdx, + final int pathOffset, final ObjectWriter ow) + throws UnmergedPathException, IOException { + final int endIdx = cIdx + entrySpan; + int childIdx = 0; + int entryIdx = cIdx; + int size = 0; + + while (entryIdx < endIdx) { + final DirCacheEntry e = cache[entryIdx]; + if (e.getStage() != 0) + throw new UnmergedPathException(e); + + final byte[] ep = e.path; + if (childIdx < childCnt) { + final DirCacheTree st = children[childIdx]; + if (st.contains(ep, pathOffset, ep.length)) { + final int stOffset = pathOffset + st.nameLength() + 1; + st.writeTree(cache, entryIdx, stOffset, ow); + + size += FileMode.TREE.copyToLength(); + size += st.nameLength(); + size += OBJECT_ID_LENGTH + 2; + + entryIdx += st.entrySpan; + childIdx++; + continue; + } + } + + final FileMode mode = e.getFileMode(); + if (mode.getObjectType() == Constants.OBJ_BAD) + throw new UnmergedPathException(e); + + size += mode.copyToLength(); + size += ep.length - pathOffset; + size += OBJECT_ID_LENGTH + 2; + entryIdx++; + } + + return size; + } + private void appendName(final StringBuilder r) { if (parent != null) { parent.appendName(r); diff --git a/org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java b/org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java new file mode 100644 index 0000000..17a3965 --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/errors/UnmergedPathException.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2008, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.errors; + +import java.io.IOException; + +import org.spearce.jgit.dircache.DirCacheEntry; + +/** + * Indicates one or more paths in a DirCache have non-zero stages present. + */ +public class UnmergedPathException extends IOException { + private static final long serialVersionUID = 1L; + + private final DirCacheEntry entry; + + /** + * Create a new unmerged path exception. + * + * @param dce + * the first non-zero stage of the unmerged path. + */ + public UnmergedPathException(final DirCacheEntry dce) { + super("Unmerged path: " + dce.getPathString()); + entry = dce; + } + + /** @return the first non-zero stage of the unmerged path */ + public DirCacheEntry getDirCacheEntry() { + return entry; + } +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java b/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java index fe5f2f6..cf42f37 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/FileMode.java @@ -191,6 +191,13 @@ public void copyTo(final OutputStream os) throws IOException { } /** + * @return the number of bytes written by {@link #copyTo(OutputStream)}. + */ + public int copyToLength() { + return octalBytes.length; + } + + /** * Get the object type that should appear for this type of mode. * <p> * See the object type constants in {@link Constants}. diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectWriter.java b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectWriter.java index e84798a..546cc68 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectWriter.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/ObjectWriter.java @@ -155,10 +155,18 @@ public ObjectId writeTree(final Tree t) throws IOException { o.write(0); id.copyRawTo(o); } - return writeTree(o.toByteArray()); + return writeCanonicalTree(o.toByteArray()); } - private ObjectId writeTree(final byte[] b) throws IOException { + /** + * Write a canonical tree to the object database. + * + * @param b + * the canonical encoding of the tree object. + * @return SHA-1 of the tree + * @throws IOException + */ + public ObjectId writeCanonicalTree(final byte[] b) throws IOException { return writeTree(b.length, new ByteArrayInputStream(b)); } -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* [JGIT PATCH 05/10] Allow a DirCache to be created with no backing store file 2009-01-22 23:28 ` [JGIT PATCH 04/10] Add writeTree support to DirCache Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 06/10] Allow CanonicalTreeParsers to be created with a UTF-8 path prefix Shawn O. Pearce 0 siblings, 1 reply; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git This permits using a DirCache as a temporary storage area in memory only, with no chance of it being written out to disk. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../src/org/spearce/jgit/dircache/DirCache.java | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java index b3c57ad..c5a4f91 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java +++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java @@ -111,6 +111,17 @@ static int cmp(final byte[] aPath, final int aLen, final byte[] bPath, } /** + * Create a new empty index which is never stored on disk. + * + * @return an empty cache which has no backing store file. The cache may not + * be read or written, but it may be queried and updated (in + * memory). + */ + public static DirCache newInCore() { + return new DirCache(null); + } + + /** * Create a new in-core index representation and read an index from disk. * <p> * The new index will be read before it is returned to the caller. Read @@ -297,6 +308,8 @@ void replace(final DirCacheEntry[] e, final int cnt) { * library does not support. */ public void read() throws IOException, CorruptObjectException { + if (liveFile == null) + throw new IOException("DirCache does not have a backing file"); if (!liveFile.exists()) clear(); else if (liveFile.lastModified() != lastModified) { @@ -407,6 +420,8 @@ private static boolean is_DIRC(final byte[] hdr) { * hold the lock. */ public boolean lock() throws IOException { + if (liveFile == null) + throw new IOException("DirCache does not have a backing file"); final LockFile tmp = new LockFile(liveFile); if (tmp.lock()) { tmp.setNeedStatInformation(true); @@ -515,6 +530,8 @@ public boolean commit() { } private void requireLocked(final LockFile tmp) { + if (liveFile == null) + throw new IllegalStateException("DirCache is not locked"); if (tmp == null) throw new IllegalStateException("DirCache " + liveFile.getAbsolutePath() + " not locked."); -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* [JGIT PATCH 06/10] Allow CanonicalTreeParsers to be created with a UTF-8 path prefix 2009-01-22 23:28 ` [JGIT PATCH 05/10] Allow a DirCache to be created with no backing store file Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 07/10] Recursively load an entire tree into a DirCacheBuilder Shawn O. Pearce 0 siblings, 1 reply; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git Creating an iterator with a path prefix permits a tree to be "mounted" at a different part of a repository, permitting more sophisticated merge strategies beyond just 1:1 path matching. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../jgit/treewalk/AbstractTreeIterator.java | 31 ++++++++++++++++++++ .../spearce/jgit/treewalk/CanonicalTreeParser.java | 30 +++++++++++++++++++ .../src/org/spearce/jgit/treewalk/TreeWalk.java | 2 +- 3 files changed, 62 insertions(+), 1 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java index 791056c..62ca69c 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/AbstractTreeIterator.java @@ -172,6 +172,37 @@ protected AbstractTreeIterator(final String prefix) { } /** + * Create a new iterator with no parent and a prefix. + * <p> + * The prefix path supplied is inserted in front of all paths generated by + * this iterator. It is intended to be used when an iterator is being + * created for a subsection of an overall repository and needs to be + * combined with other iterators that are created to run over the entire + * repository namespace. + * + * @param prefix + * position of this iterator in the repository tree. The value + * may be null or the empty array to indicate the prefix is the + * root of the repository. A trailing slash ('/') is + * automatically appended if the prefix does not end in '/'. + */ + protected AbstractTreeIterator(final byte[] prefix) { + parent = null; + + if (prefix != null && prefix.length > 0) { + pathLen = prefix.length; + path = new byte[Math.max(DEFAULT_PATH_SIZE, pathLen + 1)]; + System.arraycopy(prefix, 0, path, 0, pathLen); + if (path[pathLen - 1] != '/') + path[pathLen++] = '/'; + pathOffset = pathLen; + } else { + path = new byte[DEFAULT_PATH_SIZE]; + pathOffset = 0; + } + } + + /** * Create an iterator for a subtree of an existing iterator. * * @param p diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java index 2bcf792..ed883bd 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/CanonicalTreeParser.java @@ -67,6 +67,36 @@ public CanonicalTreeParser() { raw = EMPTY; } + /** + * Create a new parser for a tree appearing in a subset of a repository. + * + * @param prefix + * position of this iterator in the repository tree. The value + * may be null or the empty array to indicate the prefix is the + * root of the repository. A trailing slash ('/') is + * automatically appended if the prefix does not end in '/'. + * @param repo + * repository to load the tree data from. + * @param treeId + * identity of the tree being parsed; used only in exception + * messages if data corruption is found. + * @param curs + * a window cursor to use during data access from the repository. + * @throws MissingObjectException + * the object supplied is not available from the repository. + * @throws IncorrectObjectTypeException + * the object supplied as an argument is not actually a tree and + * cannot be parsed as though it were a tree. + * @throws IOException + * a loose object or pack file could not be read. + */ + public CanonicalTreeParser(final byte[] prefix, final Repository repo, + final ObjectId treeId, final WindowCursor curs) + throws IncorrectObjectTypeException, IOException { + super(prefix); + reset(repo, treeId, curs); + } + private CanonicalTreeParser(final CanonicalTreeParser p) { super(p); } diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java index 22040e3..416e027 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java +++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java @@ -354,7 +354,7 @@ public void reset(final AnyObjectId[] ids) throws MissingObjectException, o = trees[i]; while (o.parent != null) o = o.parent; - if (o instanceof CanonicalTreeParser) { + if (o instanceof CanonicalTreeParser && o.pathOffset == 0) { o.matches = null; o.matchShift = 0; ((CanonicalTreeParser) o).reset(db, ids[i], curs); -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* [JGIT PATCH 07/10] Recursively load an entire tree into a DirCacheBuilder 2009-01-22 23:28 ` [JGIT PATCH 06/10] Allow CanonicalTreeParsers to be created with a UTF-8 path prefix Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 08/10] Allow DirCacheEntry instances to be created with stage > 0 Shawn O. Pearce 0 siblings, 1 reply; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git This implements the DirCache portion of "git read-tree", where a tree can be recursively read into a DirCache instance without an impact on the working directory. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../org/spearce/jgit/dircache/DirCacheBuilder.java | 65 ++++++++++++++++++++ 1 files changed, 65 insertions(+), 0 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java index 3a37054..10161fa 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java +++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheBuilder.java @@ -37,8 +37,16 @@ package org.spearce.jgit.dircache; +import java.io.IOException; import java.util.Arrays; +import org.spearce.jgit.lib.AnyObjectId; +import org.spearce.jgit.lib.Repository; +import org.spearce.jgit.lib.WindowCursor; +import org.spearce.jgit.treewalk.AbstractTreeIterator; +import org.spearce.jgit.treewalk.CanonicalTreeParser; +import org.spearce.jgit.treewalk.TreeWalk; + /** * Updates a {@link DirCache} by adding individual {@link DirCacheEntry}s. * <p> @@ -112,6 +120,63 @@ public void keep(final int pos, int cnt) { fastKeep(pos, cnt); } + /** + * Recursively add an entire tree into this builder. + * <p> + * If pathPrefix is "a/b" and the tree contains file "c" then the resulting + * DirCacheEntry will have the path "a/b/c". + * <p> + * All entries are inserted at stage 0, therefore assuming that the + * application will not insert any other paths with the same pathPrefix. + * + * @param pathPrefix + * UTF-8 encoded prefix to mount the tree's entries at. If the + * path does not end with '/' one will be automatically inserted + * as necessary. + * @param db + * repository the tree(s) will be read from during recursive + * traversal. This must be the same repository that the resulting + * DirCache would be written out to (or used in) otherwise the + * caller is simply asking for deferred MissingObjectExceptions. + * @param tree + * the tree to recursively add. This tree's contents will appear + * under <code>pathPrefix</code>. The ObjectId must be that of a + * tree; the caller is responsible for dereferencing a tag or + * commit (if necessary). + * @throws IOException + * a tree cannot be read to iterate through its entries. + */ + public void addTree(final byte[] pathPrefix, final Repository db, + final AnyObjectId tree) throws IOException { + final TreeWalk tw = new TreeWalk(db); + tw.reset(); + final WindowCursor curs = new WindowCursor(); + try { + tw.addTree(new CanonicalTreeParser(pathPrefix, db, tree + .toObjectId(), curs)); + } finally { + curs.release(); + } + tw.setRecursive(true); + if (tw.next()) { + final DirCacheEntry newEntry = toEntry(tw); + beforeAdd(newEntry); + fastAdd(newEntry); + while (tw.next()) + fastAdd(toEntry(tw)); + } + } + + private DirCacheEntry toEntry(final TreeWalk tw) { + final DirCacheEntry e = new DirCacheEntry(tw.getRawPath()); + final AbstractTreeIterator i; + + i = tw.getTree(0, AbstractTreeIterator.class); + e.setFileMode(tw.getFileMode(0)); + e.setObjectIdFromRaw(i.idBuffer(), i.idOffset()); + return e; + } + public void finish() { if (!sorted) resort(); -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* [JGIT PATCH 08/10] Allow DirCacheEntry instances to be created with stage > 0 2009-01-22 23:28 ` [JGIT PATCH 07/10] Recursively load an entire tree into a DirCacheBuilder Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 09/10] Define a basic merge API, and a two-way tree merge strategy Shawn O. Pearce 0 siblings, 1 reply; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git As the stage is part of the sorting criteria for DirCacheEntry objects we don't allow the stage to be modified on the fly in an existing instance. Instead the stage must be set by reading it from the on-disk format or by creating a new entry with the proper path and stage components. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../org/spearce/jgit/dircache/DirCacheEntry.java | 52 +++++++++++++++++--- 1 files changed, 45 insertions(+), 7 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java index 355cd3e..9304501 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java +++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCacheEntry.java @@ -60,6 +60,18 @@ public class DirCacheEntry { private static final byte[] nullpad = new byte[8]; + /** The standard (fully merged) stage for an entry. */ + public static final int STAGE_0 = 0; + + /** The base tree revision for an entry. */ + public static final int STAGE_1 = 1; + + /** The first tree revision (usually called "ours"). */ + public static final int STAGE_2 = 2; + + /** The second tree revision (usually called "theirs"). */ + public static final int STAGE_3 = 3; + // private static final int P_CTIME = 0; // private static final int P_CTIME_NSEC = 4; @@ -141,8 +153,8 @@ DirCacheEntry(final byte[] sharedInfo, final int infoAt, } /** - * Create an empty entry. - * + * Create an empty entry at stage 0. + * * @param newPath * name of the cache entry. */ @@ -151,20 +163,46 @@ public DirCacheEntry(final String newPath) { } /** - * Create an empty entry. - * + * Create an empty entry at the specified stage. + * + * @param newPath + * name of the cache entry. + * @param stage + * the stage index of the new entry. + */ + public DirCacheEntry(final String newPath, final int stage) { + this(Constants.encode(newPath), stage); + } + + /** + * Create an empty entry at stage 0. + * * @param newPath * name of the cache entry, in the standard encoding. */ public DirCacheEntry(final byte[] newPath) { + this(newPath, STAGE_0); + } + + /** + * Create an empty entry at the specified stage. + * + * @param newPath + * name of the cache entry, in the standard encoding. + * @param stage + * the stage index of the new entry. + */ + public DirCacheEntry(final byte[] newPath, final int stage) { info = new byte[INFO_LEN]; infoOffset = 0; - path = newPath; + + int flags = ((stage & 0x3) << 12); if (path.length < NAME_MASK) - NB.encodeInt16(info, infoOffset + P_FLAGS, path.length); + flags |= path.length; else - NB.encodeInt16(info, infoOffset + P_FLAGS, NAME_MASK); + flags |= NAME_MASK; + NB.encodeInt16(info, infoOffset + P_FLAGS, flags); } void write(final OutputStream os) throws IOException { -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* [JGIT PATCH 09/10] Define a basic merge API, and a two-way tree merge strategy 2009-01-22 23:28 ` [JGIT PATCH 08/10] Allow DirCacheEntry instances to be created with stage > 0 Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 10/10] Add a few simple merge test cases Shawn O. Pearce 0 siblings, 1 reply; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git This basic merge implementation is sufficient to merge two commits in memory and write the result out as a new commit, without having a work tree on the local filesystem. It is therefore suitable for use within a batch server process where human intervention is not available to resolve conflicts. This API should permit extending it with the working tree and a copy of the work tree's DirCache, so edits in the tree can be merged in parallel with edits from commits. But the functionality is not yet implemented, so it is still a pie-in-the-sky concept. The main strategy "simple-two-way-in-core" provides a basic 3 way merge on the path level only. File contents are never patched by this strategy, making it somewhat safe for automatic merges. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../src/org/spearce/jgit/merge/MergeStrategy.java | 134 ++++++++++++ .../src/org/spearce/jgit/merge/Merger.java | 213 ++++++++++++++++++++ .../org/spearce/jgit/merge/StrategyOneSided.java | 98 +++++++++ .../jgit/merge/StrategySimpleTwoWayInCore.java | 179 ++++++++++++++++ 4 files changed, 624 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java diff --git a/org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java b/org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java new file mode 100644 index 0000000..d28dcc1 --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/merge/MergeStrategy.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2008, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.merge; + +import java.util.HashMap; + +import org.spearce.jgit.lib.Repository; + +/** + * A method of combining two or more trees together to form an output tree. + * <p> + * Different strategies may employ different techniques for deciding which paths + * (and ObjectIds) to carry from the input trees into the final output tree. + */ +public abstract class MergeStrategy { + /** Simple strategy that sets the output tree to the first input tree. */ + public static final MergeStrategy OURS = new StrategyOneSided("ours", 0); + + /** Simple strategy that sets the output tree to the second input tree. */ + public static final MergeStrategy THEIRS = new StrategyOneSided("theirs", 1); + + /** Simple strategy to merge paths, without simultaneous edits. */ + public static final MergeStrategy SIMPLE_TWO_WAY_IN_CORE = StrategySimpleTwoWayInCore.INSTANCE; + + private static final HashMap<String, MergeStrategy> STRATEGIES = new HashMap<String, MergeStrategy>(); + + static { + register(OURS); + register(THEIRS); + register(SIMPLE_TWO_WAY_IN_CORE); + } + + /** + * Register a merge strategy so it can later be obtained by name. + * + * @param imp + * the strategy to register. + * @throws IllegalArgumentException + * a strategy by the same name has already been registered. + */ + public static void register(final MergeStrategy imp) { + register(imp.getName(), imp); + } + + /** + * Register a merge strategy so it can later be obtained by name. + * + * @param name + * name the strategy can be looked up under. + * @param imp + * the strategy to register. + * @throws IllegalArgumentException + * a strategy by the same name has already been registered. + */ + public static synchronized void register(final String name, + final MergeStrategy imp) { + if (STRATEGIES.containsKey(name)) + throw new IllegalArgumentException("Merge strategy \"" + name + + "\" already exists as a default strategy"); + STRATEGIES.put(name, imp); + } + + /** + * Locate a strategy by name. + * + * @param name + * name of the strategy to locate. + * @return the strategy instance; null if no strategy matches the name. + */ + public static synchronized MergeStrategy get(final String name) { + return STRATEGIES.get(name); + } + + /** + * Get all registered strategies. + * + * @return the registered strategy instances. No inherit order is returned; + * the caller may modify (and/or sort) the returned array if + * necessary to obtain a reasonable ordering. + */ + public static synchronized MergeStrategy[] get() { + final MergeStrategy[] r = new MergeStrategy[STRATEGIES.size()]; + STRATEGIES.values().toArray(r); + return r; + } + + /** @return default name of this strategy implementation. */ + public abstract String getName(); + + /** + * Create a new merge instance. + * + * @param db + * repository database the merger will read from, and eventually + * write results back to. + * @return the new merge instance which implements this strategy. + */ + public abstract Merger newMerger(Repository db); +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java b/org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java new file mode 100644 index 0000000..04990a1 --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/merge/Merger.java @@ -0,0 +1,213 @@ +/* + * Copyright (C) 2008, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.merge; + +import java.io.IOException; + +import org.spearce.jgit.errors.IncorrectObjectTypeException; +import org.spearce.jgit.lib.AnyObjectId; +import org.spearce.jgit.lib.Constants; +import org.spearce.jgit.lib.ObjectId; +import org.spearce.jgit.lib.ObjectWriter; +import org.spearce.jgit.lib.Repository; +import org.spearce.jgit.lib.WindowCursor; +import org.spearce.jgit.revwalk.RevCommit; +import org.spearce.jgit.revwalk.RevObject; +import org.spearce.jgit.revwalk.RevTree; +import org.spearce.jgit.revwalk.RevWalk; +import org.spearce.jgit.revwalk.filter.RevFilter; +import org.spearce.jgit.treewalk.AbstractTreeIterator; +import org.spearce.jgit.treewalk.CanonicalTreeParser; +import org.spearce.jgit.treewalk.EmptyTreeIterator; + +/** + * Instance of a specific {@link MergeStrategy} for a single {@link Repository}. + */ +public abstract class Merger { + /** The repository this merger operates on. */ + protected final Repository db; + + /** A RevWalk for computing merge bases, or listing incoming commits. */ + protected final RevWalk walk; + + private ObjectWriter writer; + + /** The original objects supplied in the merge; this can be any tree-ish. */ + protected RevObject[] sourceObjects; + + /** If {@link #sourceObjects}[i] is a commit, this is the commit. */ + protected RevCommit[] sourceCommits; + + /** The trees matching every entry in {@link #sourceObjects}. */ + protected RevTree[] sourceTrees; + + /** + * Create a new merge instance for a repository. + * + * @param local + * the repository this merger will read and write data on. + */ + protected Merger(final Repository local) { + db = local; + walk = new RevWalk(db); + } + + /** + * @return the repository this merger operates on. + */ + public Repository getRepository() { + return db; + } + + /** + * @return an object writer to create objects in {@link #getRepository()}. + */ + public ObjectWriter getObjectWriter() { + if (writer == null) + writer = new ObjectWriter(getRepository()); + return writer; + } + + /** + * Merge together two or more tree-ish objects. + * <p> + * Any tree-ish may be supplied as inputs. Commits and/or tags pointing at + * trees or commits may be passed as input objects. + * + * @param tips + * source trees to be combined together. The merge base is not + * included in this set. + * @return true if the merge was completed without conflicts; false if the + * merge strategy cannot handle this merge or there were conflicts + * preventing it from automatically resolving all paths. + * @throws IncorrectObjectTypeException + * one of the input objects is not a commit, but the strategy + * requires it to be a commit. + * @throws IOException + * one or more sources could not be read, or outputs could not + * be written to the Repository. + */ + public final boolean merge(final AnyObjectId[] tips) throws IOException { + sourceObjects = new RevObject[tips.length]; + for (int i = 0; i < tips.length; i++) + sourceObjects[i] = walk.parseAny(tips[i]); + + sourceCommits = new RevCommit[sourceObjects.length]; + for (int i = 0; i < sourceObjects.length; i++) { + try { + sourceCommits[i] = walk.parseCommit(sourceObjects[i]); + } catch (IncorrectObjectTypeException err) { + sourceCommits[i] = null; + } + } + + sourceTrees = new RevTree[sourceObjects.length]; + for (int i = 0; i < sourceObjects.length; i++) + sourceTrees[i] = walk.parseTree(sourceObjects[i]); + + return mergeImpl(); + } + + /** + * Create an iterator to walk the merge base of two commits. + * + * @param aIdx + * index of the first commit in {@link #sourceObjects}. + * @param bIdx + * index of the second commit in {@link #sourceObjects}. + * @return the new iterator + * @throws IncorrectObjectTypeException + * one of the input objects is not a commit. + * @throws IOException + * objects are missing or multiple merge bases were found. + */ + protected AbstractTreeIterator mergeBase(final int aIdx, final int bIdx) + throws IOException { + if (sourceCommits[aIdx] == null) + throw new IncorrectObjectTypeException(sourceObjects[aIdx], + Constants.TYPE_COMMIT); + if (sourceCommits[bIdx] == null) + throw new IncorrectObjectTypeException(sourceObjects[bIdx], + Constants.TYPE_COMMIT); + + walk.reset(); + walk.setRevFilter(RevFilter.MERGE_BASE); + walk.markStart(sourceCommits[aIdx]); + walk.markStart(sourceCommits[bIdx]); + final RevCommit base = walk.next(); + if (base == null) + return new EmptyTreeIterator(); + final RevCommit base2 = walk.next(); + if (base2 != null) { + throw new IOException("Multiple merge bases for:" + "\n " + + sourceCommits[aIdx].name() + "\n " + + sourceCommits[bIdx].name() + "found:" + "\n " + + base.name() + "\n " + base2.name()); + } + final WindowCursor curs = new WindowCursor(); + try { + return new CanonicalTreeParser(null, db, base.getTree(), curs); + } finally { + curs.release(); + } + } + + /** + * Execute the merge. + * <p> + * This method is called from {@link #merge(AnyObjectId[])} after the + * {@link #sourceObjects}, {@link #sourceCommits} and {@link #sourceTrees} + * have been populated. + * + * @return true if the merge was completed without conflicts; false if the + * merge strategy cannot handle this merge or there were conflicts + * preventing it from automatically resolving all paths. + * @throws IncorrectObjectTypeException + * one of the input objects is not a commit, but the strategy + * requires it to be a commit. + * @throws IOException + * one or more sources could not be read, or outputs could not + * be written to the Repository. + */ + protected abstract boolean mergeImpl() throws IOException; + + /** + * @return resulting tree, if {@link #merge(AnyObjectId[])} returned true. + */ + public abstract ObjectId getResultTreeId(); +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java b/org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java new file mode 100644 index 0000000..0c3dcc2 --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/merge/StrategyOneSided.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2008, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.merge; + +import java.io.IOException; + +import org.spearce.jgit.lib.ObjectId; +import org.spearce.jgit.lib.Repository; + +/** + * Trivial merge strategy to make the resulting tree exactly match an input. + * <p> + * This strategy can be used to cauterize an entire side branch of history, by + * setting the output tree to one of the inputs, and ignoring any of the paths + * of the other inputs. + */ +public class StrategyOneSided extends MergeStrategy { + private final String strategyName; + + private final int treeIndex; + + /** + * Create a new merge strategy to select a specific input tree. + * + * @param name + * name of this strategy. + * @param index + * the position of the input tree to accept as the result. + */ + protected StrategyOneSided(final String name, final int index) { + strategyName = name; + treeIndex = index; + } + + @Override + public String getName() { + return strategyName; + } + + @Override + public Merger newMerger(final Repository db) { + return new OneSide(db, treeIndex); + } + + protected static class OneSide extends Merger { + private final int treeIndex; + + protected OneSide(final Repository local, final int index) { + super(local); + treeIndex = index; + } + + @Override + protected boolean mergeImpl() throws IOException { + return treeIndex < sourceTrees.length; + } + + @Override + public ObjectId getResultTreeId() { + return sourceTrees[treeIndex]; + } + } +} diff --git a/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java b/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java new file mode 100644 index 0000000..893add9 --- /dev/null +++ b/org.spearce.jgit/src/org/spearce/jgit/merge/StrategySimpleTwoWayInCore.java @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2008, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.merge; + +import java.io.IOException; + +import org.spearce.jgit.dircache.DirCache; +import org.spearce.jgit.dircache.DirCacheBuilder; +import org.spearce.jgit.dircache.DirCacheEntry; +import org.spearce.jgit.errors.UnmergedPathException; +import org.spearce.jgit.lib.ObjectId; +import org.spearce.jgit.lib.Repository; +import org.spearce.jgit.treewalk.AbstractTreeIterator; +import org.spearce.jgit.treewalk.NameConflictTreeWalk; + +/** + * Merges two commits together in-memory, ignoring any working directory. + * <p> + * The strategy chooses a path from one of the two input trees if the path is + * unchanged in the other relative to their common merge base tree. This is a + * trivial 3-way merge (at the file path level only). + * <p> + * Modifications of the same file path (content and/or file mode) by both input + * trees will cause a merge conflict, as this strategy does not attempt to merge + * file contents. + */ +public class StrategySimpleTwoWayInCore extends MergeStrategy { + static final MergeStrategy INSTANCE = new StrategySimpleTwoWayInCore(); + + /** Create a new instance of the strategy. */ + protected StrategySimpleTwoWayInCore() { + // + } + + @Override + public String getName() { + return "simple-two-way-in-core"; + } + + @Override + public Merger newMerger(final Repository db) { + return new InCoreMerger(db); + } + + private static class InCoreMerger extends Merger { + private static final int T_BASE = 0; + + private static final int T_OURS = 1; + + private static final int T_THEIRS = 2; + + private final NameConflictTreeWalk tw; + + private final DirCache cache; + + private DirCacheBuilder builder; + + private ObjectId resultTree; + + InCoreMerger(final Repository local) { + super(local); + tw = new NameConflictTreeWalk(db); + cache = DirCache.newInCore(); + } + + @Override + protected boolean mergeImpl() throws IOException { + if (sourceTrees.length != 2) + return false; + + tw.reset(); + tw.addTree(mergeBase(0, 1)); + tw.addTree(sourceTrees[0]); + tw.addTree(sourceTrees[1]); + + boolean hasConflict = false; + builder = cache.builder(); + while (tw.next()) { + final int modeO = tw.getRawMode(T_OURS); + final int modeT = tw.getRawMode(T_THEIRS); + if (modeO == modeT && tw.idEqual(T_OURS, T_THEIRS)) { + same(); + continue; + } + + final int modeB = tw.getRawMode(T_BASE); + if (modeB == modeO && tw.idEqual(T_BASE, T_OURS)) + add(T_THEIRS, DirCacheEntry.STAGE_0); + else if (modeB == modeT && tw.idEqual(T_BASE, T_THEIRS)) + add(T_OURS, DirCacheEntry.STAGE_0); + else { + conflict(); + hasConflict = true; + } + } + builder.finish(); + builder = null; + + if (hasConflict) + return false; + try { + resultTree = cache.writeTree(getObjectWriter()); + return true; + } catch (UnmergedPathException upe) { + resultTree = null; + return false; + } + } + + private void same() throws IOException { + if (tw.isSubtree()) + builder.addTree(tw.getRawPath(), db, tw.getObjectId(1)); + else + add(T_OURS, DirCacheEntry.STAGE_0); + } + + private void conflict() { + add(T_BASE, DirCacheEntry.STAGE_1); + add(T_OURS, DirCacheEntry.STAGE_2); + add(T_THEIRS, DirCacheEntry.STAGE_3); + } + + private void add(final int tree, final int stage) { + final AbstractTreeIterator i = getTree(tree); + if (i != null) { + final DirCacheEntry e; + + e = new DirCacheEntry(tw.getRawPath(), stage); + e.setObjectIdFromRaw(i.idBuffer(), i.idOffset()); + e.setFileMode(tw.getFileMode(tree)); + builder.add(e); + } + } + + private AbstractTreeIterator getTree(final int tree) { + return tw.getTree(tree, AbstractTreeIterator.class); + } + + @Override + public ObjectId getResultTreeId() { + return resultTree; + } + } +} -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* [JGIT PATCH 10/10] Add a few simple merge test cases 2009-01-22 23:28 ` [JGIT PATCH 09/10] Define a basic merge API, and a two-way tree merge strategy Shawn O. Pearce @ 2009-01-22 23:28 ` Shawn O. Pearce 0 siblings, 0 replies; 12+ messages in thread From: Shawn O. Pearce @ 2009-01-22 23:28 UTC (permalink / raw) To: Robin Rosenberg; +Cc: git, Robin Rosenberg Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org> --- .../spearce/jgit/test/resources/create-second-pack | 13 +++- ...ck-3280af9c07ee18a87705ef50b0cc4cd20266cf12.idx | Bin 0 -> 1296 bytes ...k-3280af9c07ee18a87705ef50b0cc4cd20266cf12.pack | Bin 0 -> 562 bytes ...ck-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745.idx | Bin 1088 -> 1100 bytes ...ck-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.idx | Bin 2696 -> 2976 bytes ...k-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.pack | Bin 5956 -> 5901 bytes .../org/spearce/jgit/test/resources/packed-refs | 4 +- .../org/spearce/jgit/lib/RepositoryTestCase.java | 3 +- .../org/spearce/jgit/merge/SimpleMergeTest.java | 86 ++++++++++++++++++++ .../org/spearce/jgit/transport/TransportTest.java | 2 +- 10 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.idx create mode 100644 org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.pack create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java diff --git a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/create-second-pack b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/create-second-pack index 052877d..5501a67 100755 --- a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/create-second-pack +++ b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/create-second-pack @@ -138,7 +138,18 @@ done git repack -d +git checkout -b f a +mkdir f +echo "an eff" >f/f +git add f/f +git commit -m "An eff" +git checkout -b g a +mkdir f +echo "an F" >f/f +git add f/f +git commit -m "An F" +git repack -d git pack-refs --all -qgit --all master +gitk --all master diff --git a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.idx b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.idx new file mode 100644 index 0000000000000000000000000000000000000000..300c0cea48c6c4eda2b870b3631355c2137cebb0 GIT binary patch literal 1296 zcmexg;-AdGz`z8=xBw$if)Wfen|Y98R-n6x!E8Wvw8HE_v#E_afM$;3(J&x74Y2LV z)Q$1iT(-xv{o%d?ezP~d30WlO{o~}bSuv7YXEk0%y}!Bi+NY5F;<3>Ud#7h~`1YT> zpf~%nNr$-eG+Xt8#O{s4dXa}k?k(2XVRih3k&*4s=J(gu+%LX=d`sY>$NY6y^vccL zFK5=6yT6d|<2_P&W-sI8NG8+rIE_yi-_5-HB{Hf?{<GXKDS;Zn=f$-%{*{*K-dyoz z&(nux&wt#UJNGfeC6_5O>cO{}3Ii^?mL8a&y=G5Sv+Zu7cmBCoC&;S-%NRy4AT9!? z`vpMy1+YjH0J3?2>?R=nM4az|fajV^sv(TevSnRfJoL#bbLi`ut*5^~(=3sT{r8l` LdIkBjci#j6ga?3q literal 0 HcmV?d00001 diff --git a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.pack b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.pack new file mode 100644 index 0000000000000000000000000000000000000000..fca3460ed2d263db153ef11a3e559aa47be9efa1 GIT binary patch literal 562 zcmWG=boORoU|<4bj+wj_bLMu(avd@dV7p)3wYNd_w#iMsjvLl57%x5H=K9t?sp<Ik zy-ohcJU(|Bgw!K{Gkv_Y)iV2yx4Bxy1{RN(o3pO*q+i>SxK8BLij_wrj#O`T2rS%| zUidH}#Dz~P_m$6`*V#8S7O30$?L3y2f3D=Xx!-Gc->7BMhQ+Jj+_2mgvGJNrY1Q>E z`!M4(EkS4a)5PtqUNAR^RLt3ZvSM!F_9Ysop$AQ_o=`fgan!){>Lnvp)w4&gTr#<6 zbJ0Ne><t4`-HQenFC4JBdBNan&7yZ)YUX+jzt0Q2UdLK7$Jmg8snw;eQ&#wAvg(}L zl3h94ac_R_Q2Cq@G><bvueUMReOX@5{%4zP=e_0sF8g8j-j}{7l$@EFjfI6<?;LqF z>59_cNnJv1%X-D5F2%7O*}gP)y~fA+sX;sDEEi;$xJmWcT1KEPCI$w^hJhZ*0-ygC ze!96v!gK9rDgDLw?#EQU4L(uJFlPtvbn}Wi-e-b6e7zqtF>ux~X%pkRgJF9WHdZg* z5cec;Vs*D^S%j>8VoFLzLPA>7q6xDjVkSi`2@eW9bYaU!C+}OQ8-8rt@>W#!-)0}K z>7PxQ7@WVWFrc_@!{3{K{@t4O`{?)ne9o9JGxophirXp4u;mKxJX5gi3V?27nZf8F Z&i6pTbIm2y5XNWOvMw(k`ec<k001O50^|Sy literal 0 HcmV?d00001 diff --git a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745.idx b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745.idx index cf58d5baa11824217bb0c5eab7444fb406219885..58b712f6c58e3b99abd536c17c306ef2c37c5238 100644 GIT binary patch delta 89 zcmV-f0H*)I2+Rlw|8!4d00002kpW(@UZMj;7j}SvqA`rvo4QPkhl4}`;t5v+%)Fm1 v0000CGYgnnQ2?6FEimT$X76fU(jBSR$3y!oeS1unZtru#oQ%-R@OV6B4p%0y literal 1088 zcmZQzpebMknm&q0!(cQG4Bj-rBVN|fu*k6I#_U}_-R;dTjE^|OnT@$;L<KU;I;(5& j?04Gxtk_GkE3duSzFw<E`CW}`!gtH2lI+N+ZF^b)CtDqp diff --git a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.idx b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.idx index 2579301b8447e4d55480f65ae6c27fe1eb99bf4d..3ff542377406158d8155129ae66fc0b51cba76b8 100644 GIT binary patch delta 1975 zcmWNSc{~(o9L8VczD?s85wmDmCU<REE7_%?a(yIa3Nhujj!cFI!x(prt4tc2lCznL z1~tkN*3sk&Z76pK<xDp1`^WqFywCG~f6x28f4y`imWssBXy*$600c{ff@n4!@-K4* zh&E+cMsP%Ib<EEyNg~_)<6Z=z@v&lR&4t^o{5zON$(63T8`MI*TJpp#7UthPAzfOc zlyz+6vCLqF@J2mU$LOeTUUpuRf{5UCEp}(q*rmd}hnHb5?x{MYFmbx@5(WhVT^1(X zog&=aI9Bs}ggMsT%6*iUi?KF(51%v80Z!80O5c~}T}7;?f9zY{pjX?po{6)4p}hSG zlzCk0IJh3wY`JgCbeOr=uK9Z8%1+SCZoz?POdQGFPH=ox19Pu#;j#r@gY}e1-X<*8 ztg+3zu`%e}u|U$ulM|81z|3FJ^;VmwapsJJkGG1&aa+H3thG=i6+bmj_TbvrmCoc$ zc-yFT6Q)bDggm<Y)Qu}Sk;Arc{@X<mxt;$l9oOn`m^eDS&^agm4)w8xB*YDNdr2e5 zzr&jva1$*1^6a;>iUlNBQ@xh8XZX%<u1CTWMR>|Q75Q-VjND>ulSg@_&y*95g(;{K znYx3VYzXiYJx$@;d$o8X3qJqlCvZA=6Z=cJTbNfw5c28!rt}-S^usHds;jwag@fn! zS8qfuunGdNq*dwi%%<rm`)D7`ZQjV}p*eitQyKU7m+ezpO!oV6y>Cu*yRWo>jXdr3 z6wX<(v4{SC3u6eoBJPRHuJ1n~-?w~;6YU70RtZ?l`BxOJe3O$y<L@@*EOt2|nJBV) zfUb#Gd^$1LmP{*L&3+x))b?@9<J$6el!VHmD5<jYKvqYXVhkqC6DdEi-lUDO3(+Wv zysbq^*0ZSCE%sTX^|tid<ah2SO>QO4`c1`0C!9^A-bc4mTX$Bn+nyvFuTzbuMG0ln zofVa_Xv*eWh7K>8D@Y+OxtymFACsb;Z<zmGDU+1EV@QgMDUI$>z_6s6KQMpGh*P2x z%27#^OUM6a+4}cayIYGz&CXG&j&VP#_wMmIm3ncr?ixZkebb*h_{C1Gzo+d7PKBqZ zXf?m=%uh@WYm=xCsjJ<8_qDSs*Wf{%&aMs(OYt`2mKrRmy(MQgoz`BBwidoe8+g%> zkobcw40dFRR|4*|w|g382N~&qdC(MYDm!Y_IGz_ih))>Lb!k=_X1FK){^C8N1mI*< zI91De^gMY2+U__m!<w|n|H=;R^b0F;_eB{RDW%8X8tu~MH^dlwwr3P&IE7SadfZK< zluYB#KP`)?6rB5BK_Be<V`0qbm|;XcIsEe@mc?7i+m@1Q@!3n6mimLMn)DB}wZhkF z?CCk#*}&Q`qbb8hw8X0W$m86h+N)Rpd`>^N<Dfs{NHR?zwsw44sTF9+i*3!martnz z;^>#D&%cs&@7FU#JcGj8ZA-P)4464)EJ}Fs_D)k8g}UC>EMq;kV-$Q|gjM}DwV=5% z_}I5Yp9^KKqfTxT?ZP>6k}Q7dUuITBA*)ICD8-dd`I_;E%+O~Mby*TAGD*r4w6g~S zyexy8`t_fa%a#N#nxE>vw>QeLQ@PFre#!G>iugOlHc^LvgCdJg(<9gojkul{_bC+8 z69>F(>mSLu)wQ-0PS<?<onE#j+{Fc_$SwfBx=-c&b+<bLq$97^G`Pli=B_8pB8Jpi zexE+1dB$g0Y|%t;idVb;@Q9Lv%z@S6Caz{T2wLNKuRiisi2WQ?nk7CKq+eIDvCy~q z6`k1HRCDBH2Hk%`K4*Myu93VLnPN4C#S*;PS>JH8KcypkB@5ncRZU=}KH96_q#HEM zxBm3rCok9UqU83-QUf&xGM<>MR`)V4C58qdSe8{266V7V(zoQctO_q`ni7>qgY|c; zruth3kkWtR1%_Q7n&w~us~__9)|NEra+0;p|EDD5x~h#dgPA>b>D4-#;)Gq>2)Om; zC2i5-Qs|itym7H^$-c$DtN9JvzN+O$Twg0&`nQv2$@&Auk>uq*xAD!f@qcim;RdFh zLfeAS+><K!IQJf5?d=N<HM@@Ka>ZdA*u*!ZgK}+DCT0ksb@vC2=f<9yy(gNkYH3K) zOziCCA8z3FEF;fH4z4*<GzffNE?M|!j(WY9r%R>R)XPK68OCKk!$nlyoMV??=#U*P zd1l{D@BH-~wiLU_;hu2WOsS4@L5+XRsR#3Vt1mefM%%CO+Doafhws1rdP-CkQ&T<| zc5i-n?*k?xbdKrk^XwQwPSfH68UVRT0Hn+SP`UzutS(#>4m8E#SF{O5J^%{-P@e-p zSrhKn0g${7z@AX3O8^k1z&}d{o-4xzJ!!~`F#%9K0_`dQkj?;z%){^@s0{&-2!y$Z zAR_`96!f3Lxp<iO9J0}n#R5PPAApBh@UjS4;2VT+0HAyUkS9Q04*gLG>%$>fQgIBn zhYS+>NDat!0U(?M**mb<M_95O&SRkO0WX5(glYf~KM1ufe25Utqd_JFW;MW>$FQ8# tIo)0Ce0{H4d`WoglAGiPUdBsD(Nnqf$%y01fxIk6k#j3%Ow8X>?tj#hbMOEF delta 1424 zcmXAodpOg39LK-zhS}CORBqiywTvYvt=owdD#N2mgzBt@5IKe;vS>-U{M=6D3@eXI zwN+-U$n|h4?YM<bxg?b9L8d%7UpxPPp6C1ezTfZ9=grgQ67$uSEdjtNDOOS2&*wP< z#X22l#6LK^By6P^01bB(lW#e;PSHzLL5MFsS6EYh-&h0yQGl2$6OI9ZKBloRJmlyt ze{7J*6yMO*Ws-V3^%|UaVwI)u%1Ddz=JW!~7IC7T8e?rc0NP}1uY1NtV?1EdM|xcy zgI!z%v|#|0)+4e111pF>z6?7n@9SSOnEb{qIZ)#f`EwHh*c_ej;~>xNV#BVv(5gKK zGHOTeXgvgSjNWxdEc$&~PT%n|m&~@bksg~d1VHB`B9#NNuB0%%<Zc$b^ZD@*9P8Ew zy+^cpcK}e;WJ_dMg)B>rEZf>JD?t&)MIT!LSi>jR&k;TD^m-mx-|W?&ouuH_{8}FZ z-y+5;tCm&xhlWNPKdTMk$0Te8p}))9gs9&J003I{)a}CPJ+D*kz9(f#+0Il`$?Qw0 zkY?Yw;CwJJR#~hg*2M(rebOEcuY~h7Bv=mAtZ=WkJ0)S@(kBj|l{)UaN7@kN5(t25 z?Wtft+>61=n|DpPyQhqcuO{EfXaj%{?!BetTj&TcofUDT*hIXupNqG<0{?GCUdn;E zgtJ1=d%exBQxfnkb<^B+!D-d*r%<`(`LiS4iZ+?T<G(r&j^hR;(~&9-(3(a7r&!9% z9sYY`n#GCso`HF7>tz81x*}NBRm<vkE>+fz$=p&VzlW;pT7_!l<+FJ3q2lR4LYQ$N z1BYpytK4L46JVGfQn(Q+-x?EVYF)6L<ujY`O7iMJTI2Gyk20v-0r8OoiLyBJIgWY1 zidUXyV?j~4-uwq-vlwQQA19PvXMUl3Con76SDfxzUM|vt=d@-aZqbY#8kTdV0c*_D zsQQ}wQ(`0$09+{|kOP$+IUI+`rIK}Pd``w+n17g}sC0AwJOFC5oVID3^>140mdLtd zQvz*x(y2NYDyfeTtW$p!@UU#lgLh7Ah1;J*ZeHFD&#Wj-ZPl^1K|*U&d35X&UkNOt znGwI-udioN;NhRU?MK-*{iHI8iteI_dQyYh?4x@Cz-K4ZtB4(l>$Sa2gNoUJjMfzt zPr_owA#^zq-IVF-i#MYXZ~pd2*PkZU<zeQG`b6jeotfwuP;$%dL2P!vz4wc}up;T1 zA8_tlz-@cIf25mo)GYW>RM5mTp7jS5OkzL6lLLtue*b6m5UVVZ(q}fRr#|D_S&<AI zrfE=i^{Q<x_srsvZJiEmYBaN^VSEY#yUU~-F{-km_w=u)3l2^9O>B)ac?{=n$g~`Y zU#`3n!<nnA(%RR(Nb&bp=9T;#pAM_}v`XZEbiryOL+f|^iFu|?kRTGpgVz44&N;l3 z##Eqv)DI<0HDXG8p5|VHJs3j{s;_3Kvr-$C_eDxK8h`ksUZlm37dm-WXTuB`wQ(_O z1B5!2Bmek?WV&;M`Q;)q%rL2?>2VDE`4M|`&Hgy@%xv9p#}r?<iwYz~4pb<W;vB?f zRR2=*PY)P8>o9g>{$nm&vPZj-hfjy<sD<>TxNOVrP9j!o#|#A8c2o+Dnt|V}z;ouA zFE)I_FDFS2R-c6Q%Yo?9Ud)}Ib;%TrY80)HMDp_FCoMoFI)y_knjx=H>F;I5y(En> zJN<jy?d351@!^^kPXmnh8#>YD*&aiDH%NSL0i&2i%u$4cun_t(TE+z<DpRM+f7>Fh z?~SAtr)g`O&$8&1U0h1vT~cU;GC=Re9<sOUyT0Rzlyn)1V7VEdU<8k4y$JXZTkul8 diff --git a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.pack b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.pack index bb47c90d93b0b2276fc36b96bc8734085bde6007..ef56d7e941828561e9f6e2cbc46e2d6c9c8d76bf 100644 GIT binary patch delta 55 zcmV-70LcHuE{!g*s{sV$1%N=4!2w$X;R0W?G6I+l180Cov)>O<2NYiZCg!fo8+@2= Nzv)s5^+^m_Both>71IC! delta 110 zcmV-!0FnQVF2pXds{t1D1;B8vj0l7bffm|^TP<Z-I%~PjDwD4PTNd#G&=E?wtTJn> zFyp3IpgDDT#i6f=vkd~64Hb6+x-!#bo>2e-!WL1JYjAg}b^oces1H#G6hxtEG1~WN Q{ggPWqc#a04LlG+NkNq^UjP6A diff --git a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/packed-refs b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/packed-refs index 746bd6b..38a70ec 100644 --- a/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/packed-refs +++ b/org.spearce.jgit.test/tst-rsrc/org/spearce/jgit/test/resources/packed-refs @@ -4,8 +4,10 @@ 6e1475206e57110fcef4b92320436c1e9872a322 refs/heads/c f73b95671f326616d66b2afb3bdfcdbbce110b44 refs/heads/d d0114ab8ac326bab30e3a657a0397578c5a1af88 refs/heads/e -d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864 refs/heads/pa +47d3697c3747e8184e0dc479ccbd01e359023577 refs/heads/f +175d5b80bd9768884d8fced02e9bd33488174396 refs/heads/g 49322bb17d3acc9146f98c97d078513228bbf3c0 refs/heads/master +d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864 refs/heads/pa 6db9c2ebf75590eef973081736730a9ea169a0c4 refs/tags/A 17768080a2318cd89bba4c8b87834401e2095703 refs/tags/B ^d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864 diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java index 9e48fde..20348f1 100644 --- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java @@ -236,7 +236,8 @@ public void run() { "pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371", "pack-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745", "pack-546ff360fe3488adb20860ce3436a2d6373d2796", - "pack-e6d07037cbcf13376308a0a995d1fa48f8f76aaa" + "pack-e6d07037cbcf13376308a0a995d1fa48f8f76aaa", + "pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12" }; final File packDir = new File(db.getObjectsDirectory(), "pack"); for (int k = 0; k < packs.length; k++) { 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 new file mode 100644 index 0000000..96064f5 --- /dev/null +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/merge/SimpleMergeTest.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2008, Robin Rosenberg + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.spearce.jgit.merge; + +import java.io.IOException; + +import org.spearce.jgit.lib.ObjectId; +import org.spearce.jgit.lib.RepositoryTestCase; + +public class SimpleMergeTest extends RepositoryTestCase { + + public void testOurs() throws IOException { + Merger ourMerger = MergeStrategy.OURS.newMerger(db); + boolean merge = ourMerger.merge(new ObjectId[] { db.resolve("a"), db.resolve("c") }); + assertTrue(merge); + assertEquals(db.mapTree("a").getId(), ourMerger.getResultTreeId()); + } + + public void testTheirs() throws IOException { + Merger ourMerger = MergeStrategy.THEIRS.newMerger(db); + boolean merge = ourMerger.merge(new ObjectId[] { db.resolve("a"), db.resolve("c") }); + assertTrue(merge); + assertEquals(db.mapTree("c").getId(), ourMerger.getResultTreeId()); + } + + public void testTrivialTwoWay() throws IOException { + Merger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db); + boolean merge = ourMerger.merge(new ObjectId[] { db.resolve("a"), db.resolve("c") }); + assertTrue(merge); + assertEquals("02ba32d3649e510002c21651936b7077aa75ffa9",ourMerger.getResultTreeId().name()); + } + + public void testTrivialTwoWay_disjointhistories() throws IOException { + Merger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db); + boolean merge = ourMerger.merge(new ObjectId[] { db.resolve("a"), db.resolve("c~4") }); + assertTrue(merge); + assertEquals("86265c33b19b2be71bdd7b8cb95823f2743d03a8",ourMerger.getResultTreeId().name()); + } + + public void testTrivialTwoWay_ok() throws IOException { + Merger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db); + boolean merge = ourMerger.merge(new ObjectId[] { db.resolve("a^0^0^0"), db.resolve("a^0^0^1") }); + assertTrue(merge); + assertEquals(db.mapTree("a^0^0").getId(), ourMerger.getResultTreeId()); + } + + public void testTrivialTwoWay_conflict() throws IOException { + Merger ourMerger = MergeStrategy.SIMPLE_TWO_WAY_IN_CORE.newMerger(db); + boolean merge = ourMerger.merge(new ObjectId[] { db.resolve("f"), db.resolve("g") }); + assertFalse(merge); + } +} diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/TransportTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/TransportTest.java index dc1cb21..c6e3335 100644 --- a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/TransportTest.java +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/TransportTest.java @@ -123,7 +123,7 @@ public void testFindRemoteRefUpdatesWildcardNoTracking() throws IOException { .findRemoteRefUpdatesFor(Collections.nCopies(1, new RefSpec( "+refs/heads/*:refs/heads/test/*"))); - assertEquals(7, result.size()); + assertEquals(9, result.size()); boolean foundA = false; boolean foundB = false; for (final RemoteRefUpdate rru : result) { -- 1.6.1.399.g0d272 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [JGIT PATCH 02/10] Expose the raw path for the current entry of a TreeWalk 2009-01-22 23:28 ` [JGIT PATCH 02/10] Expose the raw path for the current entry of a TreeWalk Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 03/10] Expose DirCacheEntry.getFileMode as a utility function Shawn O. Pearce @ 2009-01-23 21:42 ` Robin Rosenberg 1 sibling, 0 replies; 12+ messages in thread From: Robin Rosenberg @ 2009-01-23 21:42 UTC (permalink / raw) To: Shawn O. Pearce; +Cc: git fredag 23 januari 2009 00:28:02 skrev Shawn O. Pearce: > Copying the path byte array (keeping it encoded in UTF-8) is quicker > than converting to String and then back again to UTF-8 when creating > a DirCacheEntry for the current position in a TreeWalk. [...] > + * Get the current entry's complete path as a UTF-8 byte array. /Hopefully/ UTF-8. It is often a legacy encoding, like ISO-8859-X, CP12xx etc. I'll come up with something less specific that just UTF-8. -- robin ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2009-01-23 21:43 UTC | newest] Thread overview: 12+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2009-01-22 23:28 [JGIT PATCH 00/10] Merge API v2 Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 01/10] Fix TreeWalk.idEqual when both trees are missing the path Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 02/10] Expose the raw path for the current entry of a TreeWalk Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 03/10] Expose DirCacheEntry.getFileMode as a utility function Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 04/10] Add writeTree support to DirCache Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 05/10] Allow a DirCache to be created with no backing store file Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 06/10] Allow CanonicalTreeParsers to be created with a UTF-8 path prefix Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 07/10] Recursively load an entire tree into a DirCacheBuilder Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 08/10] Allow DirCacheEntry instances to be created with stage > 0 Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 09/10] Define a basic merge API, and a two-way tree merge strategy Shawn O. Pearce 2009-01-22 23:28 ` [JGIT PATCH 10/10] Add a few simple merge test cases Shawn O. Pearce 2009-01-23 21:42 ` [JGIT PATCH 02/10] Expose the raw path for the current entry of a TreeWalk Robin Rosenberg
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).