]> source.dussan.org Git - jgit.git/commitdiff
Added check for null on DirCacheEntry in checkoutEntry method 24/170224/10
authorTudor Matrescu <mit.tudor@gmail.com>
Wed, 21 Oct 2020 09:41:40 +0000 (12:41 +0300)
committerTudor Matrescu <mit.tudor@gmail.com>
Thu, 3 Dec 2020 07:59:10 +0000 (02:59 -0500)
Observed the error when trying to force checkout from a branch
that had no changes on it. When the 'keep()' method from 'DirCacheCheckout'
method was called the 'DirCacheEntry e' was null and was passed like
this to the 'checkoutEntry()' method where the 'getObjectId()' is
being called on the 'e' object

Change-Id: If3a9b9e60064459d187c7db04eb4471a72c6cece

org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/DirCacheCheckoutTest.java
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java

index b943486b1b668c09817b5e7901d54a9884eb91cd..9dfceae3458078614bd32b8dca14442d6a93805e 100644 (file)
@@ -13,6 +13,7 @@
 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;
@@ -47,6 +48,7 @@ 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;
@@ -2146,4 +2148,11 @@ 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));
+       }
+
+
 }
index 8c51a7ac2f3ae4cd7941a9374ed0a125bd4ce9e7..344626de3786be1a49e1251aea6eb21f20e024fe 100644 (file)
@@ -1214,9 +1214,10 @@ public class DirCacheCheckout {
 
        private void keep(String path, DirCacheEntry e, WorkingTreeIterator f)
                        throws IOException {
-               if (e != null && !FileMode.TREE.equals(e.getFileMode()))
+               if (e != null && !FileMode.TREE.equals(e.getFileMode())) {
                        builder.add(e);
-               if (force) {
+               }
+               if (e != null && force) {
                        if (f == null || f.isModified(e, true, walk.getObjectReader())) {
                                kept.add(path);
                                checkoutEntry(repo, e, walk.getObjectReader(), false,
@@ -1447,6 +1448,9 @@ public class DirCacheCheckout {
        public static void checkoutEntry(Repository repo, DirCacheEntry entry,
                        ObjectReader or, boolean deleteRecursive,
                        CheckoutMetadata checkoutMetadata) throws IOException {
+               if (entry == null) {
+                       return;
+               }
                if (checkoutMetadata == null)
                        checkoutMetadata = CheckoutMetadata.EMPTY;
                ObjectLoader ol = or.open(entry.getObjectId());