* Test revert command
*/
public class RevertCommandTest extends RepositoryTestCase {
+
@Test
public void testRevert() throws IOException, JGitInternalException,
GitAPIException {
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
private String ourCommitName = null;
+ private boolean insertChangeId;
+
private List<Ref> revertedRefs = new LinkedList<>();
private MergeResult failingResult;
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);
dco.checkout();
try (Git git = new Git(getRepository())) {
newHead = git.commit().setMessage(newMessage)
+ .setInsertChangeId(insertChangeId)
.setReflogComment("revert: " + shortMessage) //$NON-NLS-1$
.call();
}
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;
+ }
+
}