From 2e43dcd6454d49d733d114a6c40ced05a3e93b8d Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Mon, 17 Oct 2011 08:28:19 +0200 Subject: [PATCH] Fix bad checkout behaviour when a file is removed We deleted the entry if there was a file and an index entry, but not when there was just an index entry. Now delete the file in both cases since the missing file just means our worktree is dirty. This affected the implementation of reset --hard. Bug: 347574 Change-Id: Ie66fa61303472422830f5e33614e93ad65094e5d --- .../jgit/lib/DirCacheCheckoutTest.java | 30 +++++++++++++++++++ .../jgit/dircache/DirCacheCheckout.java | 10 +++---- 2 files changed, 34 insertions(+), 6 deletions(-) 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 9fb4b3e17..8c3f6f37c 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 @@ -47,6 +47,7 @@ import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.MergeResult.MergeStatus; +import org.eclipse.jgit.api.ResetCommand.ResetType; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.NoFilepatternException; import org.eclipse.jgit.dircache.DirCache; @@ -147,6 +148,35 @@ public void testResetHard() throws IOException, NoFilepatternException, "h()", "untracked", "untracked")); } + /** + * Reset hard from unclean condition. + *

+ * WorkDir: Empty
+ * Index: f/g
+ * Merge: x + * + * @throws Exception + */ + @Test + public void testResetHardFromIndexEntryWithoutFileToTreeWithoutFile() + throws Exception { + Git git = new Git(db); + writeTrashFile("x", "x"); + git.add().addFilepattern("x").call(); + RevCommit id1 = git.commit().setMessage("c1").call(); + + writeTrashFile("f/g", "f/g"); + git.rm().addFilepattern("x").call(); + git.add().addFilepattern("f/g").call(); + git.commit().setMessage("c2").call(); + deleteTrashFile("f/g"); + deleteTrashFile("f"); + + // The actual test + git.reset().setMode(ResetType.HARD).setRef(id1.getName()).call(); + assertIndex(mkmap("x", "x")); + } + private DirCacheCheckout resetHard(RevCommit commit) throws NoWorkTreeException, CorruptObjectException, IOException { 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 68a53dfa8..465f01ada 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java @@ -352,12 +352,10 @@ void processEntry(CanonicalTreeParser m, DirCacheBuildIterator i, } } } else { - // There is no file/folder for that path in the working tree. - // The only entry we have is the index entry. If that entry is a - // conflict simply remove it. Otherwise keep that entry in the - // index - if (i.getDirCacheEntry().getStage() == 0) - keep(i.getDirCacheEntry()); + // There is no file/folder for that path in the working tree, + // nor in the merge head. + // The only entry we have is the index entry. Like the case + // where there is a file with the same name, remove it, } } }