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;
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());
}
}
private String message;
+ private boolean insertChangeId;
+
private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
/**
try (Git git = new Git(getRepository())) {
newHeadId = git.commit()
.setReflogComment(refLogMessage.toString())
+ .setInsertChangeId(insertChangeId)
.call().getId();
}
mergeStatus = MergeStatus.MERGED;
return this;
}
+ /**
+ * 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>