]> source.dussan.org Git - jgit.git/commitdiff
Treat CloneCommand.setRemote(null) as setRemote("origin") 77/49977/2
authorJonathan Nieder <jrn@google.com>
Wed, 10 Jun 2015 22:43:27 +0000 (15:43 -0700)
committerJonathan Nieder <jrn@google.com>
Thu, 11 Jun 2015 18:54:44 +0000 (11:54 -0700)
A non-bare clone command with null remote produces a
NullPointerException when trying to produce a refspec to fetch against.

In a bare repository, a null remote name is accepted by mistake,
producing a configuration with items like 'remote.url' instead of
'remote.<remote>.url'.  This was never meant to work.

Instead, let's make setRemote(null) undo any previous setRemote calls
and re-set the remote name to DEFAULT_REMOTE, imitating C git clone's
--no-origin option.

While we're here, add some tests for setRemote working normally.

Change-Id: I76f502da5e677df501d3ef387e7f61f42a7ca238
Signed-off-by: Jonathan Nieder <jrn@google.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

index 72cc51be98e3335cab7b76ace0887667ea5e7a4f..ce11e1b1bccdb528151bd749d6dc479aa5e422bf 100644 (file)
@@ -202,6 +202,66 @@ public class CloneCommandTest extends RepositoryTestCase {
                                fetchRefSpec(git2.getRepository()));
        }
 
+       @Test
+       public void testCloneRepositoryCustomRemote() throws Exception {
+               File directory = createTempDirectory("testCloneRemoteUpstream");
+               CloneCommand command = Git.cloneRepository();
+               command.setDirectory(directory);
+               command.setRemote("upstream");
+               command.setURI(fileUri());
+               Git git2 = command.call();
+               addRepoToClose(git2.getRepository());
+               assertEquals("+refs/heads/*:refs/remotes/upstream/*",
+                               git2.getRepository()
+                                       .getConfig()
+                                       .getStringList("remote", "upstream",
+                                                       "fetch")[0]);
+               assertEquals("upstream",
+                               git2.getRepository()
+                                       .getConfig()
+                                       .getString("branch", "test", "remote"));
+               assertEquals(db.resolve("test"),
+                               git2.getRepository().resolve("upstream/test"));
+       }
+
+       @Test
+       public void testBareCloneRepositoryCustomRemote() throws Exception {
+               File directory = createTempDirectory("testCloneRemoteUpstream_bare");
+               CloneCommand command = Git.cloneRepository();
+               command.setBare(true);
+               command.setDirectory(directory);
+               command.setRemote("upstream");
+               command.setURI(fileUri());
+               Git git2 = command.call();
+               addRepoToClose(git2.getRepository());
+               assertEquals("+refs/heads/*:refs/heads/*",
+                               git2.getRepository()
+                                       .getConfig()
+                                       .getStringList("remote", "upstream",
+                                                       "fetch")[0]);
+               assertEquals("upstream",
+                               git2.getRepository()
+                                       .getConfig()
+                                       .getString("branch", "test", "remote"));
+               assertNull(git2.getRepository().resolve("upstream/test"));
+       }
+
+       @Test
+       public void testBareCloneRepositoryNullRemote() throws Exception {
+               File directory = createTempDirectory("testCloneRemoteNull_bare");
+               CloneCommand command = Git.cloneRepository();
+               command.setBare(true);
+               command.setDirectory(directory);
+               command.setRemote(null);
+               command.setURI(fileUri());
+               Git git2 = command.call();
+               addRepoToClose(git2.getRepository());
+               assertEquals("+refs/heads/*:refs/heads/*", git2.getRepository()
+                               .getConfig().getStringList("remote", "origin", "fetch")[0]);
+               assertEquals("origin", git2.getRepository().getConfig()
+                               .getString("branch", "test", "remote"));
+       }
+
        public static RefSpec fetchRefSpec(Repository r) throws URISyntaxException {
                RemoteConfig remoteConfig =
                                new RemoteConfig(r.getConfig(), Constants.DEFAULT_REMOTE_NAME);
index 5ea8398741f119db80897632cac62519aa6ac35c..20d06331d26ee81790bd4616f91619d4c5beb559 100644 (file)
@@ -403,10 +403,14 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
         *
         * @see Constants#DEFAULT_REMOTE_NAME
         * @param remote
-        *            name that keeps track of the upstream repository
+        *            name that keeps track of the upstream repository.
+        *            {@code null} means to use DEFAULT_REMOTE_NAME.
         * @return this instance
         */
        public CloneCommand setRemote(String remote) {
+               if (remote == null) {
+                       remote = Constants.DEFAULT_REMOTE_NAME;
+               }
                this.remote = remote;
                return this;
        }