]> source.dussan.org Git - jgit.git/commitdiff
Adds a callAsMap() function to LsRemoteCommand. 43/27143/4
authorYuxuan 'fishy' Wang <fishywang@google.com>
Thu, 22 May 2014 21:04:16 +0000 (14:04 -0700)
committerYuxuan 'fishy' Wang <fishywang@google.com>
Sat, 24 May 2014 19:19:23 +0000 (12:19 -0700)
The call() function of LsRemoteCommand returns Collection<Ref>, while its
internal is using Map<String, Ref> all the time. Sometimes the map is much more
useful to the caller so add a callAsMap() function to keep the API
compatibility.

Change-Id: Icb96b71277d5e2de59872aa777352dedc048c4e3
Signed-off-by: Yuxuan 'fishy' Wang <fishywang@google.com>
org.eclipse.jgit/src/org/eclipse/jgit/api/LsRemoteCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java

index 55ca58f9cb2fa055e9fe93134023862a36caeb78..e099a43502496b47b604ef74e0739549d73a0aa6 100644 (file)
@@ -46,6 +46,7 @@ import java.net.URISyntaxException;
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -153,6 +154,28 @@ public class LsRemoteCommand extends
        public Collection<Ref> call() throws GitAPIException,
                        InvalidRemoteException,
                        org.eclipse.jgit.api.errors.TransportException {
+               return execute().values();
+       }
+
+       /**
+        * Same as {@link #call()}, but return Map instead of Collection.
+        *
+        * @return a map from names to references in the remote repository
+        * @throws InvalidRemoteException
+        *             when called with an invalid remote uri
+        * @throws org.eclipse.jgit.api.errors.TransportException
+        *             for errors that occurs during transport
+        * @since 3.5
+        */
+       public Map<String, Ref> callAsMap() throws GitAPIException,
+                       InvalidRemoteException,
+                       org.eclipse.jgit.api.errors.TransportException {
+               return Collections.unmodifiableMap(execute());
+       }
+
+       protected Map<String, Ref> execute() throws GitAPIException,
+                       InvalidRemoteException,
+                       org.eclipse.jgit.api.errors.TransportException {
                checkCallable();
 
                Transport transport = null;
@@ -184,7 +207,7 @@ public class LsRemoteCommand extends
                                                        refmap.put(r.getName(), r);
                                                        break;
                                                }
-                       return refmap.values();
+                       return refmap;
                } catch (URISyntaxException e) {
                        throw new InvalidRemoteException(MessageFormat.format(
                                        JGitText.get().invalidRemote, remote));
index e90abb5fbb6d60c9917d27ae785482e782080b67..c6a6d8e66c3bbf033bc2bf656c46140016715063 100644 (file)
@@ -160,16 +160,10 @@ public class RepoCommand extends GitCommand<RevCommit> {
        /** A default implementation of {@link RemoteReader} callback. */
        public static class DefaultRemoteReader implements RemoteReader {
                public ObjectId sha1(String uri, String ref) throws GitAPIException {
-                       Collection<Ref> refs = Git
+                       Map<String, Ref> map = Git
                                        .lsRemoteRepository()
                                        .setRemote(uri)
-                                       .call();
-                       // Since LsRemoteCommand.call() only returned Map.values() to us, we
-                       // have to rebuild the map here.
-                       Map<String, Ref> map = new HashMap<String, Ref>(refs.size());
-                       for (Ref r : refs)
-                               map.put(r.getName(), r);
-
+                                       .callAsMap();
                        Ref r = RefDatabase.findRef(map, ref);
                        return r != null ? r.getObjectId() : null;
                }