]> source.dussan.org Git - jgit.git/commitdiff
fetch: Accept any SHA-1 on lhs of refspec 03/98603/1
authorShawn Pearce <spearce@spearce.org>
Sun, 4 Jun 2017 20:58:16 +0000 (13:58 -0700)
committerShawn Pearce <spearce@spearce.org>
Sun, 4 Jun 2017 20:58:16 +0000 (13:58 -0700)
Allow fetch to accept a SHA-1 on the left hand side of a RefSpec,
enabling callers to pass a specific SHA-1 they want that may not have
been advertised by the remote repository. This can be passed along to
the network protocol to be sent in a "want" line.

Rest of the plumbing only cares about the ObjectId of the Ref in
the askFor map, so make up a fake name using ObjectId.name() to
pass the desired ObjectId into the network code.

Change-Id: I620a189f3de005c403aa68b7d0442d6aa94e6056

org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchProcess.java

index 280e6d4df7db15e6d7161f8645bc57c2c392cfc0..dd26fe59ada80c5b39b6760d69eab9e015ea919d 100644 (file)
@@ -74,6 +74,7 @@ import org.eclipse.jgit.lib.BatchRefUpdate;
 import org.eclipse.jgit.lib.BatchingProgressMonitor;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.ObjectIdRef;
 import org.eclipse.jgit.lib.ProgressMonitor;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.RefDatabase;
@@ -360,12 +361,19 @@ class FetchProcess {
 
        private void expandSingle(final RefSpec spec, final Set<Ref> matched)
                        throws TransportException {
-               final Ref src = conn.getRef(spec.getSource());
+               String want = spec.getSource();
+               if (ObjectId.isId(want)) {
+                       want(ObjectId.fromString(want));
+                       return;
+               }
+
+               Ref src = conn.getRef(want);
                if (src == null) {
-                       throw new TransportException(MessageFormat.format(JGitText.get().remoteDoesNotHaveSpec, spec.getSource()));
+                       throw new TransportException(MessageFormat.format(JGitText.get().remoteDoesNotHaveSpec, want));
                }
-               if (matched.add(src))
+               if (matched.add(src)) {
                        want(src, spec);
+               }
        }
 
        private Collection<Ref> expandAutoFollowTags() throws TransportException {
@@ -440,6 +448,11 @@ class FetchProcess {
                fetchHeadUpdates.add(fhr);
        }
 
+       private void want(ObjectId id) {
+               askFor.put(id,
+                               new ObjectIdRef.Unpeeled(Ref.Storage.NETWORK, id.name(), id));
+       }
+
        private TrackingRefUpdate createUpdate(RefSpec spec, ObjectId newId)
                        throws TransportException {
                Ref ref = localRefs().get(spec.getDestination());