]> source.dussan.org Git - jgit.git/commitdiff
Remove rebase temporary files on checkout failure 99/3499/2
authorBernard Leach <leachbj@bouncycastle.org>
Sun, 22 May 2011 14:20:32 +0000 (00:20 +1000)
committerChris Aniszczyk <caniszczyk@gmail.com>
Mon, 23 May 2011 13:24:30 +0000 (08:24 -0500)
A checkout conflict during rebase setup should leave the repository
in SAFE state which means ensuring that the rebase temporary files
need to be removed.

Bug: 346813
Change-Id: If8b758fde73ed5a452a99a195a844825a03bae1a
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RebaseCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/RebaseCommand.java

index 9197ac9983cdc3abd1a1ef9fa662042d64e9cacf..672f4d86aa440869fab8c652b7f8a1ced5255ad8 100644 (file)
@@ -164,7 +164,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
                 * Create the following commits and then attempt to rebase topic onto
                 * master. This will fail as the cherry-pick list C, D, E an F contains
                 * a merge commit (F).
-                * 
+                *
                 * <pre>
                 * A - B (master)
                 *   \
@@ -1053,7 +1053,7 @@ public class RebaseCommandTest extends RepositoryTestCase {
 
                // checkout topic branch / modify file2 and add
                checkoutBranch("refs/heads/topic");
-               writeTrashFile("file2", "uncommitted file2");
+               File uncommittedFile = writeTrashFile("file2", "uncommitted file2");
                git.add().addFilepattern("file2").call();
                // do not commit
 
@@ -1067,6 +1067,9 @@ public class RebaseCommandTest extends RepositoryTestCase {
                assertNotNull(exception);
                assertEquals("Checkout conflict with files: \nfile2",
                                exception.getMessage());
+
+               checkFile(uncommittedFile, "uncommitted file2");
+               assertEquals(RepositoryState.SAFE, git.getRepository().getRepositoryState());
        }
 
        @Test
@@ -1375,4 +1378,38 @@ public class RebaseCommandTest extends RepositoryTestCase {
                checkFile(new File(db.getWorkTree(), "file2"), "more change");
                assertEquals(Status.FAST_FORWARD, res.getStatus());
        }
+
+       @Test
+       public void testRebaseShouldLeaveWorkspaceUntouchedWithUnstagedChangesConflict()
+                       throws Exception {
+               writeTrashFile(FILE1, "initial file");
+               git.add().addFilepattern(FILE1).call();
+               RevCommit initial = git.commit().setMessage("initial commit").call();
+               createBranch(initial, "refs/heads/side");
+
+               writeTrashFile(FILE1, "updated file");
+               git.add().addFilepattern(FILE1).call();
+               git.commit().setMessage("updated FILE1 on master").call();
+
+               // switch to side, modify the file
+               checkoutBranch("refs/heads/side");
+               writeTrashFile(FILE1, "side update");
+               git.add().addFilepattern(FILE1).call();
+               git.commit().setMessage("updated FILE1 on side").call();
+
+               File theFile = writeTrashFile(FILE1, "dirty the file");
+
+               // and attempt to rebase
+               try {
+                       RebaseResult rebaseResult = git.rebase()
+                                       .setUpstream("refs/heads/master").call();
+                       fail("Checkout with conflict should have occured, not "
+                                       + rebaseResult.getStatus());
+               } catch (JGitInternalException e) {
+                       checkFile(theFile, "dirty the file");
+               }
+
+               assertEquals(RepositoryState.SAFE, git.getRepository()
+                               .getRepositoryState());
+       }
 }
index 114ef03a3b09b95a4b40a89910963b4ea4064ae7..1503857ca363bb0c57b8a0d81cedbf33b20adfb7 100644 (file)
@@ -595,7 +595,13 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                // we rewind to the upstream commit
                monitor.beginTask(MessageFormat.format(JGitText.get().rewinding,
                                upstreamCommit.getShortMessage()), ProgressMonitor.UNKNOWN);
-               checkoutCommit(upstreamCommit);
+               boolean checkoutOk = false;
+               try {
+                       checkoutOk = checkoutCommit(upstreamCommit);
+               } finally {
+                       if (!checkoutOk)
+                               FileUtils.delete(rebaseDir, FileUtils.RECURSIVE);
+               }
                monitor.endTask();
 
                return null;
@@ -771,7 +777,7 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                return RawParseUtils.decode(content, 0, end);
        }
 
-       private void checkoutCommit(RevCommit commit) throws IOException {
+       private boolean checkoutCommit(RevCommit commit) throws IOException {
                try {
                        RevCommit head = walk.parseCommit(repo.resolve(Constants.HEAD));
                        DirCacheCheckout dco = new DirCacheCheckout(repo, head.getTree(),
@@ -795,6 +801,7 @@ public class RebaseCommand extends GitCommand<RebaseResult> {
                        walk.release();
                        monitor.endTask();
                }
+               return true;
        }
 
        private List<Step> loadSteps() throws IOException {