]> source.dussan.org Git - jgit.git/commitdiff
RevertCommand: support for inserting a Gerrit change ID 42/204242/1
authorThomas Wolf <twolf@apache.org>
Fri, 8 Sep 2023 20:27:19 +0000 (22:27 +0200)
committerThomas Wolf <twolf@apache.org>
Fri, 8 Sep 2023 20:58:02 +0000 (22:58 +0200)
Add a setter for the flag to be passed through to the CommitCommand.

Bug: 342790
Change-Id: I87548d7c2742af8af5ef6105115e3ab9c58d1d9f
Signed-off-by: Thomas Wolf <twolf@apache.org>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java

index 1c7b8d13a85e17c845139877117b8c57dab32fd6..4ebe994ef78b425a2831e7e9096a494e6166afc3 100644 (file)
@@ -39,6 +39,7 @@ import org.junit.Test;
  * Test revert command
  */
 public class RevertCommandTest extends RepositoryTestCase {
+
        @Test
        public void testRevert() throws IOException, JGitInternalException,
                        GitAPIException {
@@ -90,7 +91,43 @@ public class RevertCommandTest extends RepositoryTestCase {
                        assertTrue(reader.getLastEntry().getComment()
                                        .startsWith("revert: Revert \""));
                }
+       }
+
+       @Test
+       public void testRevertWithChangeId()
+                       throws IOException, JGitInternalException, GitAPIException {
+               try (Git git = new Git(db)) {
+                       writeTrashFile("a", "first line\nthird line\n");
+                       git.add().addFilepattern("a").call();
+                       git.commit().setMessage("create a").call();
+
+                       writeTrashFile("a", "first line\nsecond line\nthird line\n");
+                       git.add().addFilepattern("a").call();
+                       RevCommit second = git.commit().setMessage("changed a").call();
+
+                       writeTrashFile("a",
+                                       "first line\nsecond line\nthird line\nfourth line\n");
+                       git.add().addFilepattern("a").call();
+                       git.commit().setMessage("changed a again").call();
+
+                       git.revert().include(second).setInsertChangeId(true).call();
 
+                       assertEquals(RepositoryState.SAFE, db.getRepositoryState());
+
+                       checkFile(new File(db.getWorkTree(), "a"),
+                                       "first line\nthird line\nfourth line\n");
+                       Iterator<RevCommit> history = git.log().call().iterator();
+                       RevCommit revertCommit = history.next();
+                       String expectedMessage = "Revert \"changed a\"\n\n"
+                                       + "This reverts commit " + second.getId().getName() + ".\n";
+                       String commitMessage = revertCommit.getFullMessage();
+                       assertTrue(commitMessage.matches("^\\Q" + expectedMessage
+                                       + "\\E\nChange-Id: I[a-fA-F0-9]{40}\n$"));
+                       assertEquals("changed a again", history.next().getFullMessage());
+                       assertEquals("changed a", history.next().getFullMessage());
+                       assertEquals("create a", history.next().getFullMessage());
+                       assertFalse(history.hasNext());
+               }
        }
 
        @Test
index 513f579b675538f29aa33230a9c00a1da71c2582..5231d4a327b8eee2de08a6e26f47876dd11bfba4 100644 (file)
@@ -62,6 +62,8 @@ public class RevertCommand extends GitCommand<RevCommit> {
 
        private String ourCommitName = null;
 
+       private boolean insertChangeId;
+
        private List<Ref> revertedRefs = new LinkedList<>();
 
        private MergeResult failingResult;
@@ -132,7 +134,7 @@ public class RevertCommand extends GitCommand<RevCommit> {
 
                                String ourName = calculateOurName(headRef);
                                String revertName = srcCommit.getId()
-                                               .abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name() + " " //$NON-NLS-1$
+                                               .abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name() + ' '
                                                + srcCommit.getShortMessage();
 
                                ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo);
@@ -162,6 +164,7 @@ public class RevertCommand extends GitCommand<RevCommit> {
                                        dco.checkout();
                                        try (Git git = new Git(getRepository())) {
                                                newHead = git.commit().setMessage(newMessage)
+                                                               .setInsertChangeId(insertChangeId)
                                                                .setReflogComment("revert: " + shortMessage) //$NON-NLS-1$
                                                                .call();
                                        }
@@ -327,4 +330,18 @@ public class RevertCommand extends GitCommand<RevCommit> {
                this.monitor = monitor;
                return this;
        }
+
+       /**
+        * Defines whether to add a Gerrit change ID to each revert commit message.
+        *
+        * @param insertChangeId
+        *            whether to insert a change ID
+        * @return {@code this}
+        * @since 6.8
+        */
+       public RevertCommand setInsertChangeId(boolean insertChangeId) {
+               this.insertChangeId = insertChangeId;
+               return this;
+       }
+
 }