]> source.dussan.org Git - jgit.git/commitdiff
Fix NPE in openFetch on Transport without local repository 20/14720/3
authorRobin Stocker <robin@nibor.org>
Sat, 20 Jul 2013 14:26:05 +0000 (16:26 +0200)
committerChris Aniszczyk <caniszczyk@gmail.com>
Mon, 22 Jul 2013 03:15:31 +0000 (22:15 -0500)
Setting the walk and other fields to null will result in NPEs when the
user e.g. calls fetch on the connection, but at least the advertised
refs can be read like that without having a local repository.

Bug: 413389
Change-Id: I39c8363e81a1c7e6cb3412ba88542ead669e69ed
Signed-off-by: Robin Stocker <robin@nibor.org>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackFetchConnection.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/Transport.java

index b686d160b188b2f81e3bfa378ea1447b0802628e..6fb130231a154f59911e2d1698165e7c33ac7a53 100644 (file)
@@ -376,4 +376,20 @@ public class HttpClientTests extends HttpTestCase {
                        t.close();
                }
        }
+
+       @Test
+       public void testListRemoteWithoutLocalRepository() throws Exception {
+               Transport t = Transport.open(smartAuthNoneURI);
+               try {
+                       FetchConnection c = t.openFetch();
+                       try {
+                               Ref head = c.getRef(Constants.HEAD);
+                               assertNotNull(head);
+                       } finally {
+                               c.close();
+                       }
+               } finally {
+                       t.close();
+               }
+       }
 }
index 1b13d0ba2a7a9a405e5b6a03d05789cf044854f0..7a30d251adcebf6e8149b29fe65a26e6a0862c0f 100644 (file)
@@ -239,21 +239,33 @@ public abstract class BasePackFetchConnection extends BasePackConnection
        public BasePackFetchConnection(final PackTransport packTransport) {
                super(packTransport);
 
-               final FetchConfig cfg = local.getConfig().get(FetchConfig.KEY);
+               if (local != null) {
+                       final FetchConfig cfg = local.getConfig().get(FetchConfig.KEY);
+                       allowOfsDelta = cfg.allowOfsDelta;
+               } else {
+                       allowOfsDelta = true;
+               }
                includeTags = transport.getTagOpt() != TagOpt.NO_TAGS;
                thinPack = transport.isFetchThin();
-               allowOfsDelta = cfg.allowOfsDelta;
-
-               walk = new RevWalk(local);
-               reachableCommits = new RevCommitList<RevCommit>();
-               REACHABLE = walk.newFlag("REACHABLE"); //$NON-NLS-1$
-               COMMON = walk.newFlag("COMMON"); //$NON-NLS-1$
-               STATE = walk.newFlag("STATE"); //$NON-NLS-1$
-               ADVERTISED = walk.newFlag("ADVERTISED"); //$NON-NLS-1$
-
-               walk.carry(COMMON);
-               walk.carry(REACHABLE);
-               walk.carry(ADVERTISED);
+
+               if (local != null) {
+                       walk = new RevWalk(local);
+                       reachableCommits = new RevCommitList<RevCommit>();
+                       REACHABLE = walk.newFlag("REACHABLE"); //$NON-NLS-1$
+                       COMMON = walk.newFlag("COMMON"); //$NON-NLS-1$
+                       STATE = walk.newFlag("STATE"); //$NON-NLS-1$
+                       ADVERTISED = walk.newFlag("ADVERTISED"); //$NON-NLS-1$
+
+                       walk.carry(COMMON);
+                       walk.carry(REACHABLE);
+                       walk.carry(ADVERTISED);
+               } else {
+                       walk = null;
+                       REACHABLE = null;
+                       COMMON = null;
+                       STATE = null;
+                       ADVERTISED = null;
+               }
        }
 
        private static class FetchConfig {
@@ -357,7 +369,8 @@ public abstract class BasePackFetchConnection extends BasePackConnection
 
        @Override
        public void close() {
-               walk.release();
+               if (walk != null)
+                       walk.release();
                super.close();
        }
 
index 3c196109d747a6bd8d3923922c26361c467ed34b..dd04ce50c2b033eabfdc93d970051bf572d12cd2 100644 (file)
@@ -559,6 +559,9 @@ public abstract class Transport {
 
        /**
         * Open a new transport with no local repository.
+        * <p>
+        * Note that the resulting transport instance can not be used for fetching
+        * or pushing, but only for reading remote refs.
         *
         * @param uri
         * @return new Transport instance
@@ -1238,6 +1241,9 @@ public abstract class Transport {
 
        /**
         * Begins a new connection for fetching from the remote repository.
+        * <p>
+        * If the transport has no local repository, the fetch connection can only
+        * be used for reading remote refs.
         *
         * @return a fresh connection to fetch from the remote repository.
         * @throws NotSupportedException