diff options
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java | 72 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java | 20 |
2 files changed, 77 insertions, 15 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java index 4b23349fa6..6f7df4bd84 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java @@ -54,6 +54,7 @@ import static org.junit.Assume.assumeTrue; import java.io.File; import java.util.Iterator; +import java.util.regex.Pattern; import org.eclipse.jgit.api.MergeCommand.FastForwardMode; import org.eclipse.jgit.api.MergeResult.MergeStatus; @@ -1584,36 +1585,77 @@ public class MergeCommandTest extends RepositoryTestCase { assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); } + private Ref prepareSuccessfulMerge(Git git) throws Exception { + writeTrashFile("a", "1\na\n3\n"); + git.add().addFilepattern("a").call(); + RevCommit initialCommit = git.commit().setMessage("initial").call(); + + createBranch(initialCommit, "refs/heads/side"); + checkoutBranch("refs/heads/side"); + + writeTrashFile("b", "1\nb\n3\n"); + git.add().addFilepattern("b").call(); + git.commit().setMessage("side").call(); + + checkoutBranch("refs/heads/master"); + + writeTrashFile("c", "1\nc\n3\n"); + git.add().addFilepattern("c").call(); + git.commit().setMessage("main").call(); + + return db.exactRef("refs/heads/side"); + } + @Test public void testMergeWithMessageOption() throws Exception { try (Git git = new Git(db)) { - writeTrashFile("a", "1\na\n3\n"); - git.add().addFilepattern("a").call(); - RevCommit initialCommit = git.commit().setMessage("initial").call(); + Ref sideBranch = prepareSuccessfulMerge(git); - createBranch(initialCommit, "refs/heads/side"); - checkoutBranch("refs/heads/side"); + git.merge().include(sideBranch).setStrategy(MergeStrategy.RESOLVE) + .setMessage("user message").call(); - writeTrashFile("b", "1\nb\n3\n"); - git.add().addFilepattern("b").call(); - git.commit().setMessage("side").call(); + assertNull(db.readMergeCommitMsg()); - checkoutBranch("refs/heads/master"); + Iterator<RevCommit> it = git.log().call().iterator(); + RevCommit newHead = it.next(); + assertEquals("user message", newHead.getFullMessage()); + } + } - writeTrashFile("c", "1\nc\n3\n"); - git.add().addFilepattern("c").call(); - git.commit().setMessage("main").call(); + @Test + public void testMergeWithChangeId() throws Exception { + try (Git git = new Git(db)) { + Ref sideBranch = prepareSuccessfulMerge(git); - Ref sideBranch = db.exactRef("refs/heads/side"); + git.merge().include(sideBranch).setStrategy(MergeStrategy.RESOLVE) + .setInsertChangeId(true).call(); + + assertNull(db.readMergeCommitMsg()); + + Iterator<RevCommit> it = git.log().call().iterator(); + RevCommit newHead = it.next(); + String commitMessage = newHead.getFullMessage(); + assertTrue(Pattern.compile("\nChange-Id: I[0-9a-fA-F]{40}\n") + .matcher(commitMessage).find()); + } + } + + @Test + public void testMergeWithMessageAndChangeId() throws Exception { + try (Git git = new Git(db)) { + Ref sideBranch = prepareSuccessfulMerge(git); git.merge().include(sideBranch).setStrategy(MergeStrategy.RESOLVE) - .setMessage("user message").call(); + .setMessage("user message").setInsertChangeId(true).call(); assertNull(db.readMergeCommitMsg()); Iterator<RevCommit> it = git.log().call().iterator(); RevCommit newHead = it.next(); - assertEquals("user message", newHead.getFullMessage()); + String commitMessage = newHead.getFullMessage(); + assertTrue(commitMessage.startsWith("user message\n\n")); + assertTrue(Pattern.compile("\nChange-Id: I[0-9a-fA-F]{40}\n") + .matcher(commitMessage).find()); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java index cd50cae4fb..c8a9049e6c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java @@ -112,6 +112,8 @@ public class MergeCommand extends GitCommand<MergeResult> { private String message; + private boolean insertChangeId; + private ProgressMonitor monitor = NullProgressMonitor.INSTANCE; /** @@ -392,6 +394,7 @@ public class MergeCommand extends GitCommand<MergeResult> { try (Git git = new Git(getRepository())) { newHeadId = git.commit() .setReflogComment(refLogMessage.toString()) + .setInsertChangeId(insertChangeId) .call().getId(); } mergeStatus = MergeStatus.MERGED; @@ -619,6 +622,23 @@ public class MergeCommand extends GitCommand<MergeResult> { } /** + * If set to true a change id will be inserted into the commit message + * + * An existing change id is not replaced. An initial change id (I000...) + * will be replaced by the change id. + * + * @param insertChangeId + * whether to insert a change id + * @return {@code this} + * @since 5.0 + */ + public MergeCommand setInsertChangeId(boolean insertChangeId) { + checkCallable(); + this.insertChangeId = insertChangeId; + return this; + } + + /** * The progress monitor associated with the diff operation. By default, this * is set to <code>NullProgressMonitor</code> * |