From 3ad454497cdb6a917c00b01c735f5a469b8fb2ff Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Thu, 21 Feb 2013 00:07:33 +0100 Subject: [PATCH] Do not cherry-pick merge commits during rebase Rebase computes the list of commits that are included in the merges, just like Git does, so do not try to include the merge commits. Re-recreating merges during rebase is a bit more complicated and might be a useful future extension, but for now just linearize during rebase. Change-Id: I61239d265f395e5ead580df2528e46393dc6bdbd Signed-off-by: Robin Stocker --- .../eclipse/jgit/api/RebaseCommandTest.java | 83 ++++++++++++------- .../org/eclipse/jgit/api/RebaseCommand.java | 6 +- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java index e08fa54d8..27f41b50f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java @@ -42,6 +42,9 @@ */ package org.eclipse.jgit.api; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -62,7 +65,6 @@ import org.eclipse.jgit.api.RebaseCommand.Operation; import org.eclipse.jgit.api.RebaseCommand.Step; import org.eclipse.jgit.api.RebaseResult.Status; -import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.RefNotFoundException; import org.eclipse.jgit.api.errors.UnmergedPathsException; import org.eclipse.jgit.api.errors.WrongRepositoryStateException; @@ -161,72 +163,89 @@ public void testFastForwardWithMultipleCommits() throws Exception { assertEquals(Status.FAST_FORWARD, res.getStatus()); } + /** + * Create the following commits and then attempt to rebase topic onto + * master. This will serialize the branches. + * + *
+	 * A - B (master)
+	 *   \
+	 *    C - D - F (topic)
+	 *     \      /
+	 *      E  -  (side)
+	 * 
+ * + * into + * + *
+	 * A - B - (master)  C' - D' - E' (topic')
+	 *   \
+	 *    C - D - F (topic)
+	 *     \      /
+	 *      E  -  (side)
+	 * 
+ * + * @throws Exception + */ @Test - public void testRebaseFailsCantCherryPickMergeCommits() + public void testRebaseShouldIgnoreMergeCommits() throws Exception { - /** - * Create the following commits and then attempt to rebase topic onto - * master. This will fail as the cherry-pick list C, D, E an F contains - * a merge commit (F). - * - *
-		 * A - B (master)
-		 *   \
-		 *    C - D - F (topic)
-		 *     \      /
-		 *      E  -  (side)
-		 * 
- */ // create file1 on master writeTrashFile(FILE1, FILE1); git.add().addFilepattern(FILE1).call(); - RevCommit first = git.commit().setMessage("Add file1").call(); + RevCommit a = git.commit().setMessage("Add file1").call(); assertTrue(new File(db.getWorkTree(), FILE1).exists()); // create a topic branch - createBranch(first, "refs/heads/topic"); + createBranch(a, "refs/heads/topic"); // update FILE1 on master writeTrashFile(FILE1, "blah"); git.add().addFilepattern(FILE1).call(); - git.commit().setMessage("updated file1 on master").call(); + RevCommit b = git.commit().setMessage("updated file1 on master").call(); checkoutBranch("refs/heads/topic"); writeTrashFile("file3", "more changess"); git.add().addFilepattern("file3").call(); - RevCommit topicCommit = git.commit() + RevCommit c = git.commit() .setMessage("update file3 on topic").call(); // create a branch from the topic commit - createBranch(topicCommit, "refs/heads/side"); + createBranch(c, "refs/heads/side"); // second commit on topic writeTrashFile("file2", "file2"); git.add().addFilepattern("file2").call(); - git.commit().setMessage("Add file2").call(); + RevCommit d = git.commit().setMessage("Add file2").call(); assertTrue(new File(db.getWorkTree(), "file2").exists()); // switch to side branch and update file2 checkoutBranch("refs/heads/side"); writeTrashFile("file3", "more change"); git.add().addFilepattern("file3").call(); - RevCommit sideCommit = git.commit().setMessage("update file2 on side") + RevCommit e = git.commit().setMessage("update file2 on side") .call(); - // switch back to topic and merge in side + // switch back to topic and merge in side, creating f checkoutBranch("refs/heads/topic"); - MergeResult result = git.merge().include(sideCommit.getId()) + MergeResult result = git.merge().include(e.getId()) .setStrategy(MergeStrategy.RESOLVE).call(); assertEquals(MergeStatus.MERGED, result.getMergeStatus()); + RebaseResult res = git.rebase().setUpstream("refs/heads/master").call(); + assertEquals(Status.OK, res.getStatus()); - try { - RebaseResult rebase = git.rebase().setUpstream("refs/heads/master") - .call(); - fail("MultipleParentsNotAllowedException expected: " - + rebase.getStatus()); - } catch (JGitInternalException e) { - // expected - } + RevWalk rw = new RevWalk(db); + rw.markStart(rw.parseCommit(db.resolve("refs/heads/topic"))); + assertDerivedFrom(rw.next(), e); + assertDerivedFrom(rw.next(), d); + assertDerivedFrom(rw.next(), c); + assertEquals(b, rw.next()); + assertEquals(a, rw.next()); + } + + static void assertDerivedFrom(RevCommit derived, RevCommit original) { + assertThat(derived, not(equalTo(original))); + assertEquals(original.getFullMessage(), derived.getFullMessage()); } @Test diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java index 5158c8533..a100c9d87 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java @@ -640,11 +640,7 @@ else if (!isInteractive() && walk.isMergedInto(headCommit, upstream)) { List cherryPickList = new ArrayList(); for (RevCommit commit : commitsToUse) { if (commit.getParentCount() != 1) - throw new JGitInternalException( - MessageFormat.format( - JGitText.get().canOnlyCherryPickCommitsWithOneParent, - commit.name(), - Integer.valueOf(commit.getParentCount()))); + continue; cherryPickList.add(commit); }