]> source.dussan.org Git - jgit.git/commitdiff
Add FetchCommand#setRefSpecs(String...) variant 44/104644/1
authorDave Borowitz <dborowitz@google.com>
Thu, 7 Sep 2017 11:46:25 +0000 (07:46 -0400)
committerDave Borowitz <dborowitz@google.com>
Thu, 7 Sep 2017 11:46:25 +0000 (07:46 -0400)
Much of the time the caller can specify a RefSpec succinctly using a
string, and doesn't care about calling setters. Add a convenience method
for this case, and use it where applicable in JGit core.

Change-Id: Ic3fac7fc568eee4759236a5264d2e7e5f9b9716d

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/FetchCommand.java

index 3c196724a96e7762b9f83c405b5c5d29a451bbb2..1201d9f391dca58e515ec6ec790f16b6f6ea662b 100644 (file)
@@ -85,7 +85,6 @@ import org.eclipse.jgit.lib.Sets;
 import org.eclipse.jgit.lib.StoredConfig;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.storage.file.FileBasedConfig;
-import org.eclipse.jgit.transport.RefSpec;
 import org.eclipse.jgit.transport.RemoteConfig;
 import org.eclipse.jgit.transport.URIish;
 import org.eclipse.jgit.util.FileUtils;
@@ -431,8 +430,8 @@ public class CheckoutCommandTest extends RepositoryTestCase {
                        config.save();
 
                        // fetch from first repository
-                       RefSpec spec = new RefSpec("+refs/heads/*:refs/remotes/origin/*");
-                       git2.fetch().setRemote("origin").setRefSpecs(spec).call();
+                       git2.fetch().setRemote("origin")
+                                       .setRefSpecs("+refs/heads/*:refs/remotes/origin/*").call();
                        return db2;
                }
        }
index a36f6c551a804c53c39da8ec1cb087af48aca7e5..83a0564c774370274d92de89eff64890ea58095a 100644 (file)
@@ -56,7 +56,6 @@ import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.lib.StoredConfig;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.transport.FetchResult;
-import org.eclipse.jgit.transport.RefSpec;
 import org.eclipse.jgit.transport.RemoteConfig;
 import org.eclipse.jgit.transport.TagOpt;
 import org.eclipse.jgit.transport.TrackingRefUpdate;
@@ -93,9 +92,8 @@ public class FetchCommandTest extends RepositoryTestCase {
                RevCommit commit = remoteGit.commit().setMessage("initial commit").call();
                Ref tagRef = remoteGit.tag().setName("tag").call();
 
-               RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x");
-               git.fetch().setRemote("test").setRefSpecs(spec)
-                               .call();
+               git.fetch().setRemote("test")
+                               .setRefSpecs("refs/heads/master:refs/heads/x").call();
 
                assertEquals(commit.getId(),
                                db.resolve(commit.getId().getName() + "^{commit}"));
@@ -108,8 +106,8 @@ public class FetchCommandTest extends RepositoryTestCase {
                remoteGit.commit().setMessage("commit").call();
                Ref tagRef = remoteGit.tag().setName("foo").call();
 
-               RefSpec spec = new RefSpec("refs/heads/*:refs/remotes/origin/*");
-               git.fetch().setRemote("test").setRefSpecs(spec)
+               git.fetch().setRemote("test")
+                               .setRefSpecs("refs/heads/*:refs/remotes/origin/*")
                                .setTagOpt(TagOpt.AUTO_FOLLOW).call();
 
                assertEquals(tagRef.getObjectId(), db.resolve("foo"));
@@ -120,8 +118,8 @@ public class FetchCommandTest extends RepositoryTestCase {
                remoteGit.commit().setMessage("commit").call();
                Ref tagRef = remoteGit.tag().setName("foo").call();
                remoteGit.commit().setMessage("commit2").call();
-               RefSpec spec = new RefSpec("refs/heads/*:refs/remotes/origin/*");
-               git.fetch().setRemote("test").setRefSpecs(spec)
+               git.fetch().setRemote("test")
+                               .setRefSpecs("refs/heads/*:refs/remotes/origin/*")
                                .setTagOpt(TagOpt.AUTO_FOLLOW).call();
                assertEquals(tagRef.getObjectId(), db.resolve("foo"));
        }
@@ -132,9 +130,8 @@ public class FetchCommandTest extends RepositoryTestCase {
                remoteGit.checkout().setName("other").setCreateBranch(true).call();
                remoteGit.commit().setMessage("commit2").call();
                remoteGit.tag().setName("foo").call();
-               RefSpec spec = new RefSpec(
-                               "refs/heads/master:refs/remotes/origin/master");
-               git.fetch().setRemote("test").setRefSpecs(spec)
+               git.fetch().setRemote("test")
+                               .setRefSpecs("refs/heads/master:refs/remotes/origin/master")
                                .setTagOpt(TagOpt.AUTO_FOLLOW).call();
                assertNull(db.resolve("foo"));
        }
@@ -146,7 +143,7 @@ public class FetchCommandTest extends RepositoryTestCase {
                Ref tagRef = remoteGit.tag().setName(tagName).call();
                ObjectId originalId = tagRef.getObjectId();
 
-               RefSpec spec = new RefSpec("refs/heads/*:refs/remotes/origin/*");
+               String spec = "refs/heads/*:refs/remotes/origin/*";
                git.fetch().setRemote("test").setRefSpecs(spec)
                                .setTagOpt(TagOpt.AUTO_FOLLOW).call();
                assertEquals(originalId, db.resolve(tagName));
@@ -172,7 +169,7 @@ public class FetchCommandTest extends RepositoryTestCase {
                remoteGit.commit().setMessage("commit").call();
                Ref tagRef1 = remoteGit.tag().setName(tagName).call();
 
-               RefSpec spec = new RefSpec("refs/heads/*:refs/remotes/origin/*");
+               String spec = "refs/heads/*:refs/remotes/origin/*";
                git.fetch().setRemote("test").setRefSpecs(spec)
                                .setTagOpt(TagOpt.AUTO_FOLLOW).call();
                assertEquals(tagRef1.getObjectId(), db.resolve(tagName));
index 8f83de79ac500aa7a2b9230f4862c9da5364efd9..5270283edd53c66c1ec69907d5a43ff4d1e3818d 100644 (file)
  */
 package org.eclipse.jgit.api;
 
+import static java.util.stream.Collectors.toList;
+
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.text.MessageFormat;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import org.eclipse.jgit.annotations.Nullable;
@@ -387,6 +390,18 @@ public class FetchCommand extends TransportCommand<FetchCommand, FetchResult> {
                return refSpecs;
        }
 
+       /**
+        * The ref specs to be used in the fetch operation
+        *
+        * @param specs
+        * @return {@code this}
+        * @since 4.9
+        */
+       public FetchCommand setRefSpecs(String... specs) {
+               return setRefSpecs(
+                               Arrays.stream(specs).map(RefSpec::new).collect(toList()));
+       }
+
        /**
         * The ref specs to be used in the fetch operation
         *
@@ -394,11 +409,7 @@ public class FetchCommand extends TransportCommand<FetchCommand, FetchResult> {
         * @return {@code this}
         */
        public FetchCommand setRefSpecs(RefSpec... specs) {
-               checkCallable();
-               this.refSpecs.clear();
-               for (RefSpec spec : specs)
-                       refSpecs.add(spec);
-               return this;
+               return setRefSpecs(Arrays.asList(specs));
        }
 
        /**