Browse Source

Handle Gerrit Change-Ids for merge commits

Otherwise successful, non-conflicting merges will never get a
Gerrit Change-Id.

Bug: 358206
Change-Id: I9b599ad01d9f7332200c1d81a1ba6ce5ef990ab5
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
stable-5.0
Thomas Wolf 7 years ago committed by Matthias Sohn
parent
commit
42e69409d7
  1. 72
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java
  2. 20
      org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java

72
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());
}
}

20
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;
@ -618,6 +621,23 @@ public class MergeCommand extends GitCommand<MergeResult> {
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>

Loading…
Cancel
Save