summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test
diff options
context:
space:
mode:
authorThomas Wolf <thomas.wolf@paranor.ch>2020-12-30 10:12:34 +0100
committerThomas Wolf <thomas.wolf@paranor.ch>2020-12-30 10:51:14 +0100
commit5b1a6e0e382da62e8678afdb2524109efb38eeb4 (patch)
tree838914dd3a6355044bc6b02382bbe8534755d9ac /org.eclipse.jgit.test
parent086f474054440579dece78aa3a12a3157c80b344 (diff)
downloadjgit-5b1a6e0e382da62e8678afdb2524109efb38eeb4.tar.gz
jgit-5b1a6e0e382da62e8678afdb2524109efb38eeb4.zip
Fix NPE in DirCacheCheckout
If a file exists in head, merge, and the working tree, but not in the index, and we're doing a force checkout, the checkout must be an "update", not a "keep". This is a follow-up on If3a9b9e60064459d187c7db04eb4471a72c6cece. Bug: 569962 Change-Id: I59a7ac41898ddc1dd90e86b09b621a41fdf45667 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java49
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java9
2 files changed, 49 insertions, 9 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
index 0a0a88c838..e520732513 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
@@ -147,6 +147,55 @@ public class CheckoutCommandTest extends RepositoryTestCase {
}
@Test
+ public void testCheckoutForcedNoChangeNotInIndex() throws Exception {
+ git.checkout().setCreateBranch(true).setName("test2").call();
+ File f = writeTrashFile("NewFile.txt", "New file");
+ git.add().addFilepattern("NewFile.txt").call();
+ git.commit().setMessage("New file created").call();
+ git.checkout().setName("test").call();
+ assertFalse("NewFile.txt should not exist", f.exists());
+ writeTrashFile("NewFile.txt", "New file");
+ git.add().addFilepattern("NewFile.txt").call();
+ git.commit().setMessage("New file created again with same content")
+ .call();
+ // Now remove the file from the index only. So it exists in both
+ // commits, and in the working tree, but not in the index.
+ git.rm().addFilepattern("NewFile.txt").setCached(true).call();
+ assertTrue("NewFile.txt should exist", f.isFile());
+ git.checkout().setForced(true).setName("test2").call();
+ assertTrue("NewFile.txt should exist", f.isFile());
+ assertEquals(Constants.R_HEADS + "test2", git.getRepository()
+ .exactRef(Constants.HEAD).getTarget().getName());
+ assertTrue("Force checkout should have undone git rm --cached",
+ git.status().call().isClean());
+ }
+
+ @Test
+ public void testCheckoutNoChangeNotInIndex() throws Exception {
+ git.checkout().setCreateBranch(true).setName("test2").call();
+ File f = writeTrashFile("NewFile.txt", "New file");
+ git.add().addFilepattern("NewFile.txt").call();
+ git.commit().setMessage("New file created").call();
+ git.checkout().setName("test").call();
+ assertFalse("NewFile.txt should not exist", f.exists());
+ writeTrashFile("NewFile.txt", "New file");
+ git.add().addFilepattern("NewFile.txt").call();
+ git.commit().setMessage("New file created again with same content")
+ .call();
+ // Now remove the file from the index only. So it exists in both
+ // commits, and in the working tree, but not in the index.
+ git.rm().addFilepattern("NewFile.txt").setCached(true).call();
+ assertTrue("NewFile.txt should exist", f.isFile());
+ git.checkout().setName("test2").call();
+ assertTrue("NewFile.txt should exist", f.isFile());
+ assertEquals(Constants.R_HEADS + "test2", git.getRepository()
+ .exactRef(Constants.HEAD).getTarget().getName());
+ org.eclipse.jgit.api.Status status = git.status().call();
+ assertEquals("[NewFile.txt]", status.getRemoved().toString());
+ assertEquals("[NewFile.txt]", status.getUntracked().toString());
+ }
+
+ @Test
public void testCreateBranchOnCheckout() throws Exception {
git.checkout().setCreateBranch(true).setName("test2").call();
assertNotNull(db.exactRef("refs/heads/test2"));
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 9dfceae345..b943486b1b 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
@@ -13,7 +13,6 @@
package org.eclipse.jgit.lib;
import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.eclipse.jgit.dircache.DirCacheCheckout.checkoutEntry;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -48,7 +47,6 @@ import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.events.ChangeRecorder;
import org.eclipse.jgit.events.ListenerHandle;
-import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
@@ -2148,11 +2146,4 @@ public class DirCacheCheckoutTest extends RepositoryTestCase {
assertEquals("WorkDir has not the right size.", i.size(), nrFiles);
}
}
-
- @Test
- public void shouldReturnAndNotThrowNPEWhenCheckoutEntryIsCalledWithNullEntry() throws Exception{
- checkoutEntry(new InMemoryRepository(null), null, null, true, new CheckoutMetadata(null, null));
- }
-
-
}