]> source.dussan.org Git - jgit.git/commitdiff
Fix "reset -hard" bug that folders could not be created 68/127968/11
authorRené Scheibe <rene.scheibe@gmail.com>
Sat, 4 Aug 2018 17:26:11 +0000 (19:26 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 19 Jun 2019 09:51:24 +0000 (11:51 +0200)
Creating a folder failed in case a file with the same name already
existed.

Bug: 479266
Change-Id: Ia987660ec0968ad4081dbd5a60e80660539497e3
Signed-off-by: René Scheibe <rene.scheibe@gmail.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java

index 9b12011aabe86be0b682b6f18fdbbb9477af8185..1a52e971abb1cac264741bd68db94a61151c0277 100644 (file)
@@ -189,6 +189,22 @@ public class ResetCommandTest extends RepositoryTestCase {
                assertFalse(new File(db.getWorkTree(), "dir-or-file").exists());
        }
 
+       @Test
+       public void testHardResetWithConflicts_CreateFolder_UnstagedChanges() throws Exception {
+               setupRepository();
+
+               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();
+
+               FileUtils.delete(new File(db.getWorkTree(), "dir-or-file"), FileUtils.RECURSIVE);
+               writeTrashFile("dir-or-file", "content");
+
+               // bug 479266: cannot create folder "dir-or-file"
+               git.reset().setMode(ResetType.HARD).setRef(Constants.HEAD).call();
+               assertTrue(new File(db.getWorkTree(), "dir-or-file/c.txt").exists());
+       }
+
        @Test
        public void testResetToNonexistingHEAD() throws JGitInternalException,
                        AmbiguousObjectException, IOException, GitAPIException {
index 483051ceebbb37d312c001b19d04017f970d6ccd..8092c3134b6a1db2aabe96d3ef5e991edb3ba0eb 100644 (file)
@@ -1977,6 +1977,29 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
                }
        }
 
+       @Test
+       public void testCheckoutWithEmptyIndexDoesntOverwrite() throws Exception {
+               try (Git git = new Git(db);
+                               TestRepository<Repository> db_t = new TestRepository<>(db)) {
+                       // prepare the commits
+                       BranchBuilder master = db_t.branch("master");
+                       RevCommit mergeCommit = master.commit()
+                                       .add("p/x", "headContent")
+                                       .message("m0").create();
+                       master.commit().add("p/x", "headContent").message("m1").create();
+                       git.checkout().setName("master").call();
+
+                       // empty index and write unsaved data in 'p'
+                       git.rm().addFilepattern("p").call();
+                       writeTrashFile("p", "important data");
+
+                       git.checkout().setName(mergeCommit.getName()).call();
+
+                       assertEquals("", indexState(CONTENT));
+                       assertEquals("important data", read("p"));
+               }
+       }
+
        private static class TestFileTreeIterator extends FileTreeIterator {
 
                // For assertions only
index a3dbc50a78c70b9615b4957c42a21f4649605dc4..47c9150529c3d4a228aea10ec59ccdcba2e2827b 100644 (file)
@@ -1470,6 +1470,9 @@ public class DirCacheCheckout {
                ObjectLoader ol = or.open(entry.getObjectId());
                File f = new File(repo.getWorkTree(), entry.getPathString());
                File parentDir = f.getParentFile();
+               if (parentDir.isFile()) {
+                       FileUtils.delete(parentDir);
+               }
                FileUtils.mkdirs(parentDir, true);
                FS fs = repo.getFS();
                WorkingTreeOptions opt = repo.getConfig().get(WorkingTreeOptions.KEY);