rs =
+ db.getRefDatabase().getRefsByPrefix(prefixes).stream()
+ .collect(toMap(Ref::getName, identity(), (a, b) -> b));
+ if (refFilter != RefFilter.DEFAULT) {
+ return refFilter.filter(rs);
+ }
+ return transferConfig.getRefFilter().filter(rs);
+ }
+
+ // Slow path: filter the refs provided by the advertised refs hook.
+ // refFilter has already been applied to refs.
+ return refs.values().stream()
+ .filter(ref -> refPrefixes.stream()
+ .anyMatch(ref.getName()::startsWith))
+ .collect(toMap(Ref::getName, identity()));
+ }
+
+ /**
+ * Read a ref on behalf of the client.
+ *
+ * This checks that the ref is present in the ref advertisement since
+ * otherwise the client might not be supposed to be able to read it.
+ *
+ * @param name
+ * the unabbreviated name of the reference.
+ * @return the requested Ref, or {@code null} if it is not visible or
+ * does not exist.
+ * @throws java.io.IOException
+ * on failure to read the ref or check it for visibility.
+ */
+ @Nullable
+ private Ref getRef(String name) throws IOException {
+ if (refs != null) {
+ return refs.get(name);
+ }
+ if (!advertiseRefsHookCalled) {
+ advertiseRefsHook.advertiseRefs(this);
+ advertiseRefsHookCalled = true;
+ }
+ if (refs == null &&
+ refFilter == RefFilter.DEFAULT &&
+ transferConfig.hasDefaultRefFilter()) {
+ // Fast path: no ref filtering is needed.
+ return db.getRefDatabase().exactRef(name);
+ }
+ return getAdvertisedOrDefaultRefs().get(name);
+ }
+
private void service() throws IOException {
boolean sendPack = false;
// If it's a non-bidi request, we need to read the entire request before
@@ -921,16 +1000,7 @@ private void lsRefsV2() throws IOException {
if (req.getPeel()) {
adv.setDerefTags(true);
}
- Map refsToSend;
- if (req.getRefPrefixes().isEmpty()) {
- refsToSend = getAdvertisedOrDefaultRefs();
- } else {
- refsToSend = new HashMap<>();
- String[] prefixes = req.getRefPrefixes().toArray(new String[0]);
- for (Ref ref : db.getRefDatabase().getRefsByPrefix(prefixes)) {
- refsToSend.put(ref.getName(), ref);
- }
- }
+ Map refsToSend = getFilteredRefs(req.getRefPrefixes());
if (req.getSymrefs()) {
findSymrefs(adv, refsToSend);
}
@@ -953,8 +1023,7 @@ private void fetchV2() throws IOException {
}
ProtocolV2Parser parser = new ProtocolV2Parser(transferConfig);
- FetchV2Request req = parser.parseFetchRequest(pckIn,
- db.getRefDatabase());
+ FetchV2Request req = parser.parseFetchRequest(pckIn);
currentRequest = req;
rawOut.stopBuffering();
@@ -962,8 +1031,6 @@ private void fetchV2() throws IOException {
// TODO(ifrade): Refactor to pass around the Request object, instead of
// copying data back to class fields
- wantIds = req.getWantIds();
-
List deepenNots = new ArrayList<>();
for (String s : req.getDeepenNotRefs()) {
Ref ref = db.getRefDatabase().getRef(s);
@@ -974,6 +1041,24 @@ private void fetchV2() throws IOException {
deepenNots.add(ref.getObjectId());
}
+ Map wantedRefs = new TreeMap<>();
+ for (String refName : req.getWantedRefs()) {
+ Ref ref = getRef(refName);
+ if (ref == null) {
+ throw new PackProtocolException(MessageFormat
+ .format(JGitText.get().invalidRefName, refName));
+ }
+ ObjectId oid = ref.getObjectId();
+ if (oid == null) {
+ throw new PackProtocolException(MessageFormat
+ .format(JGitText.get().invalidRefName, refName));
+ }
+ // TODO(ifrade): Avoid mutating the parsed request.
+ req.getWantIds().add(oid);
+ wantedRefs.put(refName, oid);
+ }
+ wantIds = req.getWantIds();
+
boolean sectionSent = false;
boolean mayHaveShallow = req.getDepth() != 0
|| req.getDeepenSince() != 0
@@ -1027,13 +1112,13 @@ private void fetchV2() throws IOException {
sectionSent = true;
}
- if (!req.getWantedRefs().isEmpty()) {
+ if (!wantedRefs.isEmpty()) {
if (sectionSent) {
pckOut.writeDelim();
}
pckOut.writeString("wanted-refs\n"); //$NON-NLS-1$
- for (Map.Entry entry : req.getWantedRefs()
- .entrySet()) {
+ for (Map.Entry entry :
+ wantedRefs.entrySet()) {
pckOut.writeString(entry.getValue().getName() + ' ' +
entry.getKey() + '\n');
}
@@ -1286,15 +1371,7 @@ public void sendAdvertisedRefs(RefAdvertiser adv,
return;
}
- try {
- advertiseRefsHook.advertiseRefs(this);
- } catch (ServiceMayNotContinueException fail) {
- if (fail.getMessage() != null) {
- adv.writeOne("ERR " + fail.getMessage()); //$NON-NLS-1$
- fail.setOutput();
- }
- throw fail;
- }
+ Map advertisedOrDefaultRefs = getAdvertisedOrDefaultRefs();
if (serviceName != null) {
adv.writeOne("# service=" + serviceName + '\n'); //$NON-NLS-1$
@@ -1326,7 +1403,6 @@ public void sendAdvertisedRefs(RefAdvertiser adv,
adv.advertiseCapability(OPTION_FILTER);
}
adv.setDerefTags(true);
- Map advertisedOrDefaultRefs = getAdvertisedOrDefaultRefs();
findSymrefs(adv, advertisedOrDefaultRefs);
advertised = adv.send(advertisedOrDefaultRefs);
if (adv.isEmpty())