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 40d8458ef..725ebc0d2 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 @@ -156,6 +156,34 @@ public class ResetCommandTest extends RepositoryTestCase { assertEquals(prevHead, db.readOrigHead()); } + @Test + public void testHardResetWithConflicts_DoOverWriteUntrackedFile() + throws JGitInternalException, + AmbiguousObjectException, IOException, GitAPIException { + setupRepository(); + git.rm().setCached(true).addFilepattern("a.txt").call(); + assertTrue(new File(db.getWorkTree(), "a.txt").exists()); + git.reset().setMode(ResetType.HARD).setRef(Constants.HEAD) + .call(); + assertTrue(new File(db.getWorkTree(), "a.txt").exists()); + assertEquals("content", read(new File(db.getWorkTree(), "a.txt"))); + } + + @Test + public void testHardResetWithConflicts_DoDeleteFileFolderConflicts() + throws JGitInternalException, + AmbiguousObjectException, IOException, GitAPIException { + setupRepository(); + writeTrashFile("d/c.txt", "x"); + git.add().addFilepattern("d/c.txt").call(); + FileUtils.delete(new File(db.getWorkTree(), "d"), FileUtils.RECURSIVE); + writeTrashFile("d", "y"); + + git.reset().setMode(ResetType.HARD).setRef(Constants.HEAD) + .call(); + assertFalse(new File(db.getWorkTree(), "d").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 fc4cc9093..12ceb74ab 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java @@ -354,8 +354,16 @@ public class DirCacheCheckout { // The index entry is missing if (f != null && !FileMode.TREE.equals(f.getEntryFileMode()) && !f.isEntryIgnored()) { - // don't overwrite an untracked and not ignored file - conflicts.add(walk.getPathString()); + if (failOnConflict) { + // don't overwrite an untracked and not ignored file + conflicts.add(walk.getPathString()); + } else { + // failOnConflict is false. Putting something to conflicts + // would mean we delete it. Instead we want the mergeCommit + // content to be checked out. + update(m.getEntryPathString(), m.getEntryObjectId(), + m.getEntryFileMode()); + } } else update(m.getEntryPathString(), m.getEntryObjectId(), m.getEntryFileMode()); @@ -390,6 +398,9 @@ public class DirCacheCheckout { if (f != null) { // There is a file/folder for that path in the working tree if (walk.isDirectoryFileConflict()) { + // We put it in conflicts. Even if failOnConflict is false + // this would cause the path to be deleted. Thats exactly what + // we want in this situation conflicts.add(walk.getPathString()); } else { // No file/folder conflict exists. All entries are files or