From 18069ffe8cbede40cf2524922c262b67656e7021 Mon Sep 17 00:00:00 2001 From: Stefan Lay Date: Wed, 6 Nov 2013 09:43:31 +0100 Subject: [PATCH] Rebase interactive should finish if last step is edit When the last step was an edit step, rebase interactive did not finish after continuing the rebase. Instead, it returned with the status FAST_FORWARD. Change-Id: Ib19857474ac089dfeaae665ad5e95c66c21099b0 --- .../eclipse/jgit/api/RebaseCommandTest.java | 33 +++++++++++++++++++ .../org/eclipse/jgit/api/RebaseCommand.java | 23 ++++++++----- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java index eb6c5f0a6b..531e1b0e2c 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java @@ -2294,6 +2294,39 @@ public class RebaseCommandTest extends RepositoryTestCase { }).call(); } + @Test + public void testRebaseEndsIfLastStepIsEdit() throws Exception { + // create file1 on master + writeTrashFile(FILE1, FILE1); + git.add().addFilepattern(FILE1).call(); + git.commit().setMessage("Add file1\nnew line").call(); + assertTrue(new File(db.getWorkTree(), FILE1).exists()); + + // create file2 on master + writeTrashFile("file2", "file2"); + git.add().addFilepattern("file2").call(); + git.commit().setMessage("Add file2\nnew line").call(); + assertTrue(new File(db.getWorkTree(), "file2").exists()); + + git.rebase().setUpstream("HEAD~1") + .runInteractively(new InteractiveHandler() { + + public void prepareSteps(List steps) { + steps.get(0).setAction(Action.EDIT); + } + + public String modifyCommitMessage(String commit) { + return commit; + } + }).call(); + git.commit().setAmend(true) + .setMessage("Add file2\nnew line\nanother line").call(); + RebaseResult result = git.rebase().setOperation(Operation.CONTINUE) + .call(); + assertEquals(Status.OK, result.getStatus()); + + } + private File getTodoFile() { File todoFile = new File(db.getDirectory(), GIT_REBASE_TODO); return todoFile; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java index ef739bb050..ea48688ff9 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java @@ -284,6 +284,9 @@ public class RebaseCommand extends GitCommand { List steps = repo.readRebaseTodo( rebaseState.getPath(GIT_REBASE_TODO), false); + if (steps.size() == 0) { + return finishRebase(walk.parseCommit(repo.resolve(Constants.HEAD)), false); + } if (isInteractive()) { interactiveHandler.prepareSteps(steps); repo.writeRebaseTodoFile(rebaseState.getPath(GIT_REBASE_TODO), @@ -370,15 +373,7 @@ public class RebaseCommand extends GitCommand { monitor.endTask(); } } - if (newHead != null) { - String headName = rebaseState.readFile(HEAD_NAME); - updateHead(headName, newHead, upstreamCommit); - FileUtils.delete(rebaseState.getDir(), FileUtils.RECURSIVE); - if (lastStepWasForward) - return RebaseResult.FAST_FORWARD_RESULT; - return RebaseResult.OK_RESULT; - } - return RebaseResult.FAST_FORWARD_RESULT; + return finishRebase(newHead, lastStepWasForward); } catch (CheckoutConflictException cce) { return new RebaseResult(cce.getConflictingPaths()); } catch (IOException ioe) { @@ -386,6 +381,16 @@ public class RebaseCommand extends GitCommand { } } + private RebaseResult finishRebase(RevCommit newHead, + boolean lastStepWasForward) throws IOException { + String headName = rebaseState.readFile(HEAD_NAME); + updateHead(headName, newHead, upstreamCommit); + FileUtils.delete(rebaseState.getDir(), FileUtils.RECURSIVE); + if (lastStepWasForward || newHead == null) + return RebaseResult.FAST_FORWARD_RESULT; + return RebaseResult.OK_RESULT; + } + private void checkSteps(List steps) throws InvalidRebaseStepException, IOException { if (steps.isEmpty()) -- 2.39.5