]> source.dussan.org Git - jgit.git/commitdiff
Fix ClassCastException in MergeCommand 60/3860/5
authorDenys Digtiar <duemir@gmail.com>
Mon, 18 Jul 2011 05:33:30 +0000 (08:33 +0300)
committerChris Aniszczyk <zx@twitter.com>
Sun, 21 Aug 2011 20:53:02 +0000 (13:53 -0700)
Test was added which reproduce the ClassCastException when ours or
theirs merge strategy is set to MergeCommand. Merger and MergeCommand
were updated in order to avoid exception.

Change-Id: I4c1284b4e80d82638d0677a05e5d38182526d196
Signed-off-by: Denys Digtiar <duemir@gmail.com>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
org.eclipse.jgit.test/META-INF/MANIFEST.MF
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/merge/Merger.java
org.eclipse.jgit/src/org/eclipse/jgit/merge/ThreeWayMerger.java
org.eclipse.jgit/src/org/eclipse/jgit/notes/NoteMapMerger.java

index d7f7ef1b6e49838f03ba960c0a543f65fff24817..06db49d87ac720e823dc4fd035863ebb89d996f5 100644 (file)
@@ -38,9 +38,11 @@ Import-Package: org.eclipse.jgit;version="[1.1.0,1.2.0)",
  org.eclipse.jgit.treewalk.filter;version="[1.1.0,1.2.0)",
  org.eclipse.jgit.util;version="[1.1.0,1.2.0)",
  org.eclipse.jgit.util.io;version="[1.1.0,1.2.0)",
- org.junit;version="[4.0.0,5.0.0)",
  org.hamcrest.core;version="[1.1.0,2.0.0)",
- org.hamcrest;version="[1.1.0,2.0.0)"
+ org.hamcrest;version="[1.1.0,2.0.0)",
+ org.junit;version="[4.4.0,5.0.0)",
+ org.junit.runner;version="[4.4.0,5.0.0)",
+ org.junit.experimental.theories;version="[4.4.0,5.0.0)"
 Require-Bundle: com.jcraft.jsch;bundle-version="[0.1.37,0.2.0)"
 Export-Package: org.eclipse.jgit.lib
  
index 7811706fc1a44867f88fb47f1d3727ec23e36936..1c682cae10612969e086f7f3ef521275a0ebb794 100644 (file)
@@ -62,8 +62,17 @@ import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.util.FileUtils;
 import org.junit.Test;
+import org.junit.experimental.theories.DataPoints;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
 
+@RunWith(Theories.class)
 public class MergeCommandTest extends RepositoryTestCase {
+
+       public static @DataPoints
+       MergeStrategy[] mergeStrategies = MergeStrategy.get();
+
        @Test
        public void testMergeInItself() throws Exception {
                Git git = new Git(db);
@@ -161,6 +170,28 @@ public class MergeCommandTest extends RepositoryTestCase {
                }
        }
 
+       @Theory
+       public void testMergeSuccessAllStrategies(MergeStrategy mergeStrategy)
+                       throws Exception {
+               Git git = new Git(db);
+
+               RevCommit first = git.commit().setMessage("first").call();
+               createBranch(first, "refs/heads/side");
+
+               writeTrashFile("a", "a");
+               git.add().addFilepattern("a").call();
+               git.commit().setMessage("second").call();
+
+               checkoutBranch("refs/heads/side");
+               writeTrashFile("b", "b");
+               git.add().addFilepattern("b").call();
+               git.commit().setMessage("third").call();
+
+               MergeResult result = git.merge().setStrategy(mergeStrategy)
+                               .include(db.getRef(Constants.MASTER)).call();
+               assertEquals(MergeStatus.MERGED, result.getMergeStatus());
+       }
+
        @Test
        public void testContentMerge() throws Exception {
                Git git = new Git(db);
index 5e77565562275f4054801ed5e27350dd9c86186a..eca7e0b8c06a68c59fefe66d0015e93e720777af 100644 (file)
@@ -71,9 +71,9 @@ import org.eclipse.jgit.lib.RefUpdate.Result;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.merge.MergeMessageFormatter;
 import org.eclipse.jgit.merge.MergeStrategy;
+import org.eclipse.jgit.merge.Merger;
 import org.eclipse.jgit.merge.ResolveMerger;
 import org.eclipse.jgit.merge.ResolveMerger.MergeFailureReason;
-import org.eclipse.jgit.merge.ThreeWayMerger;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.treewalk.FileTreeIterator;
@@ -193,8 +193,7 @@ public class MergeCommand extends GitCommand<MergeResult> {
                                                commits, head);
                                repo.writeMergeCommitMsg(mergeMessage);
                                repo.writeMergeHeads(Arrays.asList(ref.getObjectId()));
-                               ThreeWayMerger merger = (ThreeWayMerger) mergeStrategy
-                                               .newMerger(repo);
+                               Merger merger = mergeStrategy.newMerger(repo);
                                boolean noProblems;
                                Map<String, org.eclipse.jgit.merge.MergeResult<?>> lowLevelResults = null;
                                Map<String, MergeFailureReason> failingPaths = null;
index 1bfa269f2541fd505c84d555cc369332db7cb498..55ac999cd487926192058acf10204c99f28cef55 100644 (file)
@@ -134,7 +134,7 @@ public abstract class Merger {
         *             one or more sources could not be read, or outputs could not
         *             be written to the Repository.
         */
-       public boolean merge(final AnyObjectId[] tips) throws IOException {
+       public 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]);
index 9350adf6e3a8f1336f217b5d99ec6bf7ecfa3b60..521f8963a4d516637bb489cfe65ea7235cdaa013 100644 (file)
@@ -84,7 +84,7 @@ public abstract class ThreeWayMerger extends Merger {
         * @param id
         *            common base treeish; null to automatically compute the common
         *            base from the input commits during
-        *            {@link #merge(AnyObjectId, AnyObjectId)}.
+        *            {@link #merge(AnyObjectId...)}.
         * @throws IncorrectObjectTypeException
         *             the object is not a treeish.
         * @throws MissingObjectException
@@ -101,33 +101,8 @@ public abstract class ThreeWayMerger extends Merger {
                }
        }
 
-       /**
-        * Merge together two 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 a
-        *            source tree to be combined together.
-        * @param b
-        *            source tree to be combined together.
-        * @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 boolean merge(final AnyObjectId a, final AnyObjectId b)
-                       throws IOException {
-               return merge(new AnyObjectId[] { a, b });
-       }
-
        @Override
-       public boolean merge(final AnyObjectId[] tips) throws IOException {
+       public boolean merge(final AnyObjectId... tips) throws IOException {
                if (tips.length != 2)
                        return false;
                return super.merge(tips);
index b0965d2c07891ab9f2bd8cbfb3c5d7e2a2767491..b335fbe87b164d5f6412563eae681731c4dbc41b 100644 (file)
@@ -55,7 +55,6 @@ import org.eclipse.jgit.lib.ObjectReader;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.merge.MergeStrategy;
 import org.eclipse.jgit.merge.Merger;
-import org.eclipse.jgit.merge.ThreeWayMergeStrategy;
 import org.eclipse.jgit.merge.ThreeWayMerger;
 import org.eclipse.jgit.treewalk.AbstractTreeIterator;
 import org.eclipse.jgit.treewalk.TreeWalk;
@@ -323,23 +322,13 @@ public class NoteMapMerger {
                ObjectId theirsId = write(theirsList);
                inserter.flush();
 
-               ObjectId resultTreeId;
-               if (nonNotesMergeStrategy instanceof ThreeWayMergeStrategy) {
-                       ThreeWayMerger m = ((ThreeWayMergeStrategy) nonNotesMergeStrategy)
-                                       .newMerger(db, true);
-                       m.setBase(baseId);
-                       if (!m.merge(oursId, theirsId))
-                               throw new NotesMergeConflictException(baseList, oursList,
-                                               theirsList);
-
-                       resultTreeId = m.getResultTreeId();
-               } else {
-                       Merger m = nonNotesMergeStrategy.newMerger(db, true);
-                       if (!m.merge(new AnyObjectId[] { oursId, theirsId }))
-                               throw new NotesMergeConflictException(baseList, oursList,
-                                               theirsList);
-                       resultTreeId = m.getResultTreeId();
-               }
+               Merger m = nonNotesMergeStrategy.newMerger(db, true);
+               if (m instanceof ThreeWayMerger)
+                       ((ThreeWayMerger) m).setBase(baseId);
+               if (!m.merge(oursId, theirsId))
+                       throw new NotesMergeConflictException(baseList, oursList,
+                                       theirsList);
+               ObjectId resultTreeId = m.getResultTreeId();
                AbbreviatedObjectId none = AbbreviatedObjectId.fromString("");
                return NoteParser.parse(none, resultTreeId, reader).nonNotes;
        }