From 8f9697b4c182be6f4ec3eeda130bd57237f154c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scheibe?= Date: Sat, 4 Aug 2018 13:19:17 +0200 Subject: [PATCH] Fix "reset -hard" bug that folders could not be deleted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The deleted code is not required as removed files are deleted correctly in doCheckout() anyway. The deleted code failed in case a non-empty directory had to be deleted. file.delete() returned false, triggering an exception. Bug: 479266 Change-Id: I011bb3882ff0c35b238aa3eccad7889041210277 Signed-off-by: René Scheibe --- .../eclipse/jgit/api/ResetCommandTest.java | 20 +++++++++++++++++++ .../jgit/dircache/DirCacheCheckout.java | 8 -------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java index 1a52e971a..333ebd338 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java @@ -205,6 +205,26 @@ public class ResetCommandTest extends RepositoryTestCase { assertTrue(new File(db.getWorkTree(), "dir-or-file/c.txt").exists()); } + @Test + public void testHardResetWithConflicts_DeleteFolder_UnstagedChanges() throws Exception { + setupRepository(); + ObjectId prevHead = db.resolve(Constants.HEAD); + + writeTrashFile("dir-or-file/c.txt", "content"); + git.add().addFilepattern("dir-or-file/c.txt").call(); + git.commit().setMessage("adding dir-or-file/c.txt").call(); + + writeTrashFile("dir-or-file-2/d.txt", "content"); + git.add().addFilepattern("dir-or-file-2/d.txt").call(); + FileUtils.delete(new File(db.getWorkTree(), "dir-or-file-2"), FileUtils.RECURSIVE); + writeTrashFile("dir-or-file-2", "content"); + + // bug 479266: cannot delete folder "dir-or-file" + git.reset().setMode(ResetType.HARD).setRef(prevHead.getName()).call(); + assertFalse(new File(db.getWorkTree(), "dir-or-file").exists()); + assertFalse(new File(db.getWorkTree(), "dir-or-file-2").exists()); + } + @Test public void testResetToNonexistingHEAD() throws JGitInternalException, AmbiguousObjectException, IOException, GitAPIException { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java index 47c915052..3b2e74e9e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java @@ -1298,14 +1298,6 @@ public class DirCacheCheckout { JGitText.get().cannotDeleteFile, c)); removeEmptyParents(conflict); } - for (String r : removed) { - File file = new File(repo.getWorkTree(), r); - if (!file.delete()) - throw new CheckoutConflictException( - MessageFormat.format(JGitText.get().cannotDeleteFile, - file.getAbsolutePath())); - removeEmptyParents(file); - } } /**