assertThat(pckIn.readString(), is(tag.toObjectId().getName() + " refs/tags/tag"));
assertTrue(pckIn.readString() == PacketLineIn.END);
}
+
+ @Test
+ public void testV2LsRefsRefPrefix() throws Exception {
+ RevCommit tip = remote.commit().message("message").create();
+ remote.update("master", tip);
+ remote.update("other", tip);
+ remote.update("yetAnother", tip);
+
+ ByteArrayInputStream recvStream = uploadPackV2(
+ "command=ls-refs\n",
+ PacketLineIn.DELIM,
+ "ref-prefix refs/heads/maste",
+ "ref-prefix refs/heads/other",
+ PacketLineIn.END);
+ PacketLineIn pckIn = new PacketLineIn(recvStream);
+
+ assertThat(pckIn.readString(), is(tip.toObjectId().getName() + " refs/heads/master"));
+ assertThat(pckIn.readString(), is(tip.toObjectId().getName() + " refs/heads/other"));
+ assertTrue(pckIn.readString() == PacketLineIn.END);
+ }
+
+ @Test
+ public void testV2LsRefsRefPrefixNoSlash() throws Exception {
+ RevCommit tip = remote.commit().message("message").create();
+ remote.update("master", tip);
+ remote.update("other", tip);
+
+ ByteArrayInputStream recvStream = uploadPackV2(
+ "command=ls-refs\n",
+ PacketLineIn.DELIM,
+ "ref-prefix refs/heads/maste",
+ "ref-prefix r",
+ PacketLineIn.END);
+ PacketLineIn pckIn = new PacketLineIn(recvStream);
+
+ assertThat(pckIn.readString(), is(tip.toObjectId().getName() + " refs/heads/master"));
+ assertThat(pckIn.readString(), is(tip.toObjectId().getName() + " refs/heads/other"));
+ assertTrue(pckIn.readString() == PacketLineIn.END);
+ }
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
private void lsRefsV2() throws IOException {
PacketLineOutRefAdvertiser adv = new PacketLineOutRefAdvertiser(pckOut);
- Map<String, Ref> refs = getAdvertisedOrDefaultRefs();
String line;
+ ArrayList<String> refPrefixes = new ArrayList<>();
+ boolean needToFindSymrefs = false;
adv.setUseProtocolV2(true);
if (line.equals("peel")) {
adv.setDerefTags(true);
} else if (line.equals("symrefs")) {
- findSymrefs(adv, refs);
+ needToFindSymrefs = true;
+ } else if (line.startsWith("ref-prefix ")) {
+ refPrefixes.add(line.substring("ref-prefix ".length()));
} else {
throw new PackProtocolException("unexpected " + line);
}
}
rawOut.stopBuffering();
- adv.send(refs);
+ Map<String, Ref> refsToSend;
+ if (refPrefixes.isEmpty()) {
+ refsToSend = getAdvertisedOrDefaultRefs();
+ } else {
+ refsToSend = new HashMap<>();
+ for (String refPrefix : refPrefixes) {
+ for (Ref ref : db.getRefDatabase().getRefsByPrefix(refPrefix)) {
+ refsToSend.put(ref.getName(), ref);
+ }
+ }
+ }
+
+ if (needToFindSymrefs) {
+ findSymrefs(adv, refsToSend);
+ }
+
+ adv.send(refsToSend);
adv.end();
}