From e74751769e7be0901d1190e789bf360512ff3213 Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Tue, 18 Jun 2013 16:41:58 -0700 Subject: [PATCH] UploadPack: set RefFilter from TransportConfig Teach TransportConfig to respect uploadpack.hiderefs, which is new in C git 1.8.2. Change-Id: Id12e7f00b9a60258e996410f67fa10616459f53f --- .../jgit/transport/TransferConfig.java | 40 ++++++++++++++++++- .../eclipse/jgit/transport/UploadPack.java | 8 +++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java index 22888afc0..1286718de 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java @@ -43,8 +43,12 @@ 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 filter(Map refs) { + Map result = new HashMap(); + for (Map.Entry 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); + } + }; + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java index 8478dffe4..f1028a0f8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -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 { *

* 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.