]> source.dussan.org Git - jgit.git/commitdiff
Handle Gerrit Change-Ids for merge commits 71/120871/4
authorThomas Wolf <thomas.wolf@paranor.ch>
Fri, 6 Apr 2018 21:58:34 +0000 (23:58 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 18 Apr 2018 21:33:37 +0000 (23:33 +0200)
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>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/MergeCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java

index 4b23349fa66ec061a1e6d25bc8935e62ea10ebcb..6f7df4bd8435445e676c007f423332b477ae6666 100644 (file)
@@ -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());
                }
        }
 
index cd50cae4fb5720aff078f07d5c62f76ea6d905ea..c8a9049e6c36a6fe65923781d0cfb0aabd5a82d5 100644 (file)
@@ -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>