Browse Source

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 <blizzy@blizzy.de>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-3.5
Maik Schreiber 11 years ago committed by Matthias Sohn
parent
commit
7ff1e0d8f5
  1. 100
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java
  2. 1
      org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java

100
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<RevCommit> 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<RevCommit> 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);

1
org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java

@ -186,6 +186,7 @@ public class RevertCommand extends GitCommand<RevCommit> {
.setMessage(newMessage)
.setReflogComment("revert: " + shortMessage).call(); //$NON-NLS-1$
revertedRefs.add(src);
headCommit = newHead;
} else {
unmergedPaths = merger.getUnmergedPaths();
Map<String, MergeFailureReason> failingPaths = merger

Loading…
Cancel
Save