UploadPack: set RefFilter from TransportConfig

Teach TransportConfig to respect uploadpack.hiderefs, which is new in
C git 1.8.2.

Change-Id: Id12e7f00b9a60258e996410f67fa10616459f53f
This commit is contained in:
Dave Borowitz 2013-06-18 16:41:58 -07:00
parent e599af1900
commit e74751769e
2 changed files with 44 additions and 4 deletions

View File

@ -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 TransferConfig parse(final Config cfg) {
private final boolean fsckObjects;
private final boolean allowTipSha1InWant;
private final String[] hideRefs;
TransferConfig(final Repository db) {
this(db.getConfig());
@ -68,8 +73,9 @@ public TransferConfig parse(final Config cfg) {
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 boolean isFsckObjects() {
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);
}
};
}
}

View File

@ -351,7 +351,10 @@ public void setAdvertisedRefs(Map<String, Ref> allRefs) {
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 void setAdvertiseRefsHook(final AdvertiseRefsHook advertiseRefsHook) {
* <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.