diff options
author | René Scheibe <rene.scheibe@gmail.com> | 2018-08-04 19:26:11 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2019-06-19 11:51:24 +0200 |
commit | b4edf9ec142cd8d728800625926e12a7cecfa52e (patch) | |
tree | 11d446e7b5b26275ac14005fbd2256b913c1e4a8 | |
parent | e5f86d46308db2ecda9e4c5fb0041595cf499a7c (diff) | |
download | jgit-b4edf9ec142cd8d728800625926e12a7cecfa52e.tar.gz jgit-b4edf9ec142cd8d728800625926e12a7cecfa52e.zip |
Fix "reset -hard" bug that folders could not be created
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>
3 files changed, 42 insertions, 0 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 9b12011aab..1a52e971ab 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 @@ -190,6 +190,22 @@ public class ResetCommandTest extends RepositoryTestCase { } @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 { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java index 483051ceeb..8092c3134b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java @@ -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 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 a3dbc50a78..47c9150529 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java @@ -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); |