]> source.dussan.org Git - jgit.git/commitdiff
UploadPack: set RefFilter from TransportConfig 00/13900/7
authorDave Borowitz <dborowitz@google.com>
Tue, 18 Jun 2013 23:41:58 +0000 (16:41 -0700)
committerDave Borowitz <dborowitz@google.com>
Wed, 3 Jul 2013 22:10:39 +0000 (15:10 -0700)
Teach TransportConfig to respect uploadpack.hiderefs, which is new in
C git 1.8.2.

Change-Id: Id12e7f00b9a60258e996410f67fa10616459f53f

org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

index 22888afc0e7f3f5ff7de3b550a5a691a0525b0cc..1286718de28d67481a2454752cd3077a9bd39e61 100644 (file)
 
 package org.eclipse.jgit.transport;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.lib.Config.SectionParser;
+import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
 
 /**
@@ -61,6 +65,7 @@ public class TransferConfig {
 
        private final boolean fsckObjects;
        private final boolean allowTipSha1InWant;
+       private final String[] hideRefs;
 
        TransferConfig(final Repository db) {
                this(db.getConfig());
@@ -68,8 +73,9 @@ public class TransferConfig {
 
        private TransferConfig(final Config rc) {
                fsckObjects = rc.getBoolean("receive", "fsckobjects", false); //$NON-NLS-1$ //$NON-NLS-2$
-               allowTipSha1InWant =
-                               rc.getBoolean("uploadpack", "allowtipsha1inwant", false); //$NON-NLS-1$ //$NON-NLS-2$
+               allowTipSha1InWant = rc.getBoolean(
+                               "uploadpack", "allowtipsha1inwant", false); //$NON-NLS-1$ //$NON-NLS-2$
+               hideRefs = rc.getStringList("uploadpack", null, "hiderefs"); //$NON-NLS-1$ //$NON-NLS-2$
        }
 
        /**
@@ -85,4 +91,34 @@ public class TransferConfig {
        public boolean isAllowTipSha1InWant() {
                return allowTipSha1InWant;
        }
+
+       /**
+        * @return {@link RefFilter} respecting configured hidden refs.
+        */
+       public RefFilter getRefFilter() {
+               if (hideRefs.length == 0)
+                       return RefFilter.DEFAULT;
+
+               return new RefFilter() {
+                       public Map<String, Ref> filter(Map<String, Ref> refs) {
+                               Map<String, Ref> result = new HashMap<String, Ref>();
+                               for (Map.Entry<String, Ref> e : refs.entrySet()) {
+                                       boolean add = true;
+                                       for (String hide : hideRefs) {
+                                               if (e.getKey().equals(hide) || prefixMatch(hide, e.getKey())) {
+                                                       add = false;
+                                                       break;
+                                               }
+                                       }
+                                       if (add)
+                                               result.put(e.getKey(), e.getValue());
+                               }
+                               return result;
+                       }
+
+                       private boolean prefixMatch(String p, String s) {
+                               return p.charAt(p.length() - 1) == '/' && s.startsWith(p);
+                       }
+               };
+       }
 }
index 8478dffe45dcd5d02c641fe01a954c0efcc8319a..f1028a0f8fb6431a0ea24d5ffb728f3accc95060 100644 (file)
@@ -351,7 +351,10 @@ public class UploadPack {
                        refs = allRefs;
                else
                        refs = db.getAllRefs();
-               refs = refFilter.filter(refs);
+               if (refFilter == RefFilter.DEFAULT)
+                       refs = transferConfig.getRefFilter().filter(refs);
+               else
+                       refs = refFilter.filter(refs);
        }
 
        /** @return timeout (in seconds) before aborting an IO operation. */
@@ -444,7 +447,8 @@ public class UploadPack {
         * <p>
         * Only refs allowed by this filter will be sent to the client.
         * The filter is run against the refs specified by the
-        * {@link AdvertiseRefsHook} (if applicable).
+        * {@link AdvertiseRefsHook} (if applicable). If null or not set, uses the
+        * filter implied by the {@link TransferConfig}.
         *
         * @param refFilter
         *            the filter; may be null to show all refs.