]> source.dussan.org Git - jgit.git/commitdiff
upload-pack: send symbolic refs as capabilities 24/36824/5
authorYuxuan 'fishy' Wang <fishywang@google.com>
Thu, 20 Nov 2014 23:50:24 +0000 (15:50 -0800)
committerYuxuan 'fishy' Wang <fishywang@google.com>
Fri, 21 Nov 2014 17:47:41 +0000 (09:47 -0800)
cgit has this feature for some time. This will teach JGit to send symbolic refs,
too.

Change-Id: I7cb2ab4e6d31a838a0af92eac64535fdb66ed74a
Signed-off-by: Yuxuan 'fishy' Wang <fishywang@google.com>
org.eclipse.jgit/src/org/eclipse/jgit/transport/GitProtocolConstants.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/RefAdvertiser.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

index 43fd07944cd0dae40e092e342a47d9780d098380..9ec14aade76adff3882339656d006e9c4173c517 100644 (file)
@@ -129,6 +129,13 @@ public class GitProtocolConstants {
         */
        public static final String OPTION_ALLOW_TIP_SHA1_IN_WANT = "allow-tip-sha1-in-want"; //$NON-NLS-1$
 
+       /**
+        * Symbolic reference support for better negotiation.
+        *
+        * @since 3.6
+        */
+       public static final String OPTION_SYMREF = "symref"; //$NON-NLS-1$
+
        /**
         * The client supports atomic pushes. If this option is used, the server
         * will update all refs within one atomic transaction.
index 581a44b1dc8db93985a669fe7f67bc6f560512ec..76547a628b344154f90bdf336d1b5cec0728ccd9 100644 (file)
@@ -43,6 +43,8 @@
 
 package org.eclipse.jgit.transport;
 
+import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_SYMREF;
+
 import java.io.IOException;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
@@ -145,6 +147,27 @@ public abstract class RefAdvertiser {
                capablities.add(name);
        }
 
+       /**
+        * Add a symbolic ref to capabilities.
+        * <p>
+        * This method must be invoked prior to any of the following:
+        * <ul>
+        * <li>{@link #send(Map)}
+        * <li>{@link #advertiseHave(AnyObjectId)}
+        * </ul>
+        *
+        * @param from
+        *            The symbolic ref, e.g. "HEAD"
+        * @param to
+        *            The real ref it points to, e.g. "refs/heads/master"
+        *
+        * @since 3.6
+        */
+       public void addSymref(String from, String to) {
+               String symref = String.format("%s=%s:%s", OPTION_SYMREF, from, to); //$NON-NLS-1$
+               advertiseCapability(symref);
+       }
+
        /**
         * Format an advertisement for the supplied refs.
         *
index 83806f129a36830e6a7b26fc82c7e82c61b78a57..1a653bd2be534466b5b0e522f2d1ea5b12a16ce9 100644 (file)
@@ -807,7 +807,9 @@ public class UploadPack {
                                || policy == null)
                        adv.advertiseCapability(OPTION_ALLOW_TIP_SHA1_IN_WANT);
                adv.setDerefTags(true);
-               advertised = adv.send(getAdvertisedOrDefaultRefs());
+               Map<String, Ref> refs = getAdvertisedOrDefaultRefs();
+               findSymrefs(adv, refs);
+               advertised = adv.send(refs);
                if (adv.isEmpty())
                        adv.advertiseId(ObjectId.zeroId(), "capabilities^{}"); //$NON-NLS-1$
                adv.end();
@@ -1430,4 +1432,12 @@ public class UploadPack {
                if (sideband)
                        pckOut.end();
        }
+
+       private void findSymrefs(
+                       final RefAdvertiser adv, final Map<String, Ref> refs) {
+               Ref head = refs.get(Constants.HEAD);
+               if (head != null && head.isSymbolic()) {
+                       adv.addSymref(Constants.HEAD, head.getLeaf().getName());
+               }
+       }
 }