Browse Source

Fix error occurring during checkout

Fix NullPointerException occurring when calling
CheckoutCommand with forced == true option when
the branch isn't changed and there is deleted
uncommitted file.

Change-Id: I99bf1fc25e6889f07092320d7bc2772ec5d341b5
Signed-off-by: Nail Samatov <sanail@yandex.ru>
master
Nail Samatov 5 years ago
parent
commit
f30e48914e
  1. 22
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
  2. 2
      org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java

22
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java

@ -124,6 +124,28 @@ public class CheckoutCommandTest extends RepositoryTestCase {
.setForced(true).call().getObjectId());
}
@Test
public void testCheckoutForced_deleteFileAndRestore() throws Exception {
File testFile = new File(db.getWorkTree(), "Test.txt");
assertTrue(testFile.exists());
assertEquals("test", git.getRepository().getBranch());
FileUtils.delete(testFile);
assertFalse(testFile.exists());
// Switch from "test" to "master".
assertEquals(initialCommit.getId(), git.checkout().setName("master")
.setForced(true).call().getObjectId());
assertTrue(testFile.exists());
assertEquals("master", git.getRepository().getBranch());
FileUtils.delete(testFile);
assertFalse(testFile.exists());
// Stay in current branch.
assertEquals(initialCommit.getId(), git.checkout().setName("master")
.setForced(true).call().getObjectId());
assertTrue(testFile.exists());
}
@Test
public void testCreateBranchOnCheckout() throws Exception {
git.checkout().setCreateBranch(true).setName("test2").call();

2
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java vendored

@ -1217,7 +1217,7 @@ public class DirCacheCheckout {
if (e != null && !FileMode.TREE.equals(e.getFileMode()))
builder.add(e);
if (force) {
if (f.isModified(e, true, walk.getObjectReader())) {
if (f == null || f.isModified(e, true, walk.getObjectReader())) {
kept.add(path);
checkoutEntry(repo, e, walk.getObjectReader(), false,
new CheckoutMetadata(walk.getEolStreamType(CHECKOUT_OP),

Loading…
Cancel
Save