From 7ff1e0d8f56dda85ba2a2224126a1075bda1bcf2 Mon Sep 17 00:00:00 2001 From: Maik Schreiber Date: Thu, 19 Jun 2014 12:54:14 +0200 Subject: [PATCH] Fix RevertCommand to correctly revert multiple commits at once. The fix is to move the new head commit to the newly-created revert commit, so that additional revert commits will use the correct head. Change-Id: I5de3a9a2a4c276e60af732e9c507cbbdfd1a4652 Signed-off-by: Maik Schreiber Signed-off-by: Matthias Sohn --- .../eclipse/jgit/api/RevertCommandTest.java | 100 ++++++++++++++++++ .../org/eclipse/jgit/api/RevertCommand.java | 1 + 2 files changed, 101 insertions(+) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java index bb3e7a92e..060168c67 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java @@ -99,6 +99,8 @@ public class RevertCommandTest extends RepositoryTestCase { git.revert().include(fixingA).call(); + assertEquals(RepositoryState.SAFE, db.getRepositoryState()); + assertTrue(new File(db.getWorkTree(), "b").exists()); checkFile(new File(db.getWorkTree(), "a"), "first line\nsec. line\nthird line\nfourth line\n"); @@ -123,6 +125,104 @@ public class RevertCommandTest extends RepositoryTestCase { } + @Test + public void testRevertMultiple() throws IOException, JGitInternalException, + GitAPIException { + Git git = new Git(db); + + writeTrashFile("a", "first\n"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add first").call(); + + writeTrashFile("a", "first\nsecond\n"); + git.add().addFilepattern("a").call(); + RevCommit secondCommit = git.commit().setMessage("add second").call(); + + writeTrashFile("a", "first\nsecond\nthird\n"); + git.add().addFilepattern("a").call(); + RevCommit thirdCommit = git.commit().setMessage("add third").call(); + + git.revert().include(thirdCommit).include(secondCommit).call(); + + assertEquals(RepositoryState.SAFE, db.getRepositoryState()); + + checkFile(new File(db.getWorkTree(), "a"), "first\n"); + Iterator history = git.log().call().iterator(); + RevCommit revertCommit = history.next(); + String expectedMessage = "Revert \"add second\"\n\n" + + "This reverts commit " + + secondCommit.getId().getName() + ".\n"; + assertEquals(expectedMessage, revertCommit.getFullMessage()); + revertCommit = history.next(); + expectedMessage = "Revert \"add third\"\n\n" + + "This reverts commit " + thirdCommit.getId().getName() + + ".\n"; + assertEquals(expectedMessage, revertCommit.getFullMessage()); + assertEquals("add third", history.next().getFullMessage()); + assertEquals("add second", history.next().getFullMessage()); + assertEquals("add first", history.next().getFullMessage()); + assertFalse(history.hasNext()); + + ReflogReader reader = db.getReflogReader(Constants.HEAD); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + reader = db.getReflogReader(db.getBranch()); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + + } + + @Test + public void testRevertMultipleWithFail() throws IOException, + JGitInternalException, GitAPIException { + Git git = new Git(db); + + writeTrashFile("a", "first\n"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add first").call(); + + writeTrashFile("a", "first\nsecond\n"); + git.add().addFilepattern("a").call(); + RevCommit secondCommit = git.commit().setMessage("add second").call(); + + writeTrashFile("a", "first\nsecond\nthird\n"); + git.add().addFilepattern("a").call(); + git.commit().setMessage("add third").call(); + + writeTrashFile("a", "first\nsecond\nthird\nfourth\n"); + git.add().addFilepattern("a").call(); + RevCommit fourthCommit = git.commit().setMessage("add fourth").call(); + + git.revert().include(fourthCommit).include(secondCommit).call(); + + // not SAFE because it failed + assertEquals(RepositoryState.REVERTING, db.getRepositoryState()); + + checkFile(new File(db.getWorkTree(), "a"), "first\n" + + "<<<<<<< master\n" + "second\n" + "third\n" + "=======\n" + + ">>>>>>> " + secondCommit.getId().abbreviate(7).name() + + " add second\n"); + Iterator history = git.log().call().iterator(); + RevCommit revertCommit = history.next(); + String expectedMessage = "Revert \"add fourth\"\n\n" + + "This reverts commit " + fourthCommit.getId().getName() + + ".\n"; + assertEquals(expectedMessage, revertCommit.getFullMessage()); + assertEquals("add fourth", history.next().getFullMessage()); + assertEquals("add third", history.next().getFullMessage()); + assertEquals("add second", history.next().getFullMessage()); + assertEquals("add first", history.next().getFullMessage()); + assertFalse(history.hasNext()); + + ReflogReader reader = db.getReflogReader(Constants.HEAD); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + reader = db.getReflogReader(db.getBranch()); + assertTrue(reader.getLastEntry().getComment() + .startsWith("revert: Revert \"")); + + } + @Test public void testRevertDirtyIndex() throws Exception { Git git = new Git(db); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java index 52327d76c..470d823ac 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java @@ -186,6 +186,7 @@ public class RevertCommand extends GitCommand { .setMessage(newMessage) .setReflogComment("revert: " + shortMessage).call(); //$NON-NLS-1$ revertedRefs.add(src); + headCommit = newHead; } else { unmergedPaths = merger.getUnmergedPaths(); Map failingPaths = merger