RefFilter refFilter, ProtocolV2Hook hook, String... inputLines)
throws Exception {
- ByteArrayOutputStream send = new ByteArrayOutputStream();
- PacketLineOut pckOut = new PacketLineOut(send);
- for (String line : inputLines) {
- if (line == PacketLineIn.END) {
- pckOut.end();
- } else if (line == PacketLineIn.DELIM) {
- pckOut.writeDelim();
- } else {
- pckOut.writeString(line);
- }
- }
+ ByteArrayInputStream send = linesAsInputStream(inputLines);
server.getConfig().setString("protocol", null, "version", "2");
UploadPack up = new UploadPack(server);
}
ByteArrayOutputStream recv = new ByteArrayOutputStream();
- up.upload(new ByteArrayInputStream(send.toByteArray()), recv, null);
+ up.upload(send, recv, null);
return new ByteArrayInputStream(recv.toByteArray());
}
+ private static ByteArrayInputStream linesAsInputStream(String... inputLines)
+ throws IOException {
+ try (ByteArrayOutputStream send = new ByteArrayOutputStream()) {
+ PacketLineOut pckOut = new PacketLineOut(send);
+ for (String line : inputLines) {
+ if (line == PacketLineIn.END) {
+ pckOut.end();
+ } else if (line == PacketLineIn.DELIM) {
+ pckOut.writeDelim();
+ } else {
+ pckOut.writeString(line);
+ }
+ }
+ return new ByteArrayInputStream(send.toByteArray());
+ }
+ }
+
/*
* Invokes UploadPack with protocol v2 and sends it the given lines.
* Returns UploadPack's output stream, not including the capability
assertTrue(client.hasObject(three.toObjectId()));
}
+ @Test
+ public void testGetPeerAgentProtocolV0() throws Exception {
+ RevCommit one = remote.commit().message("1").create();
+ remote.update("one", one);
+
+ UploadPack up = new UploadPack(server);
+ ByteArrayInputStream send = linesAsInputStream(
+ "want " + one.getName() + " agent=JGit-test/1.2.3\n",
+ PacketLineIn.END,
+ "have 11cedf1b796d44207da702f7d420684022fc0f09\n", "done\n");
+
+ ByteArrayOutputStream recv = new ByteArrayOutputStream();
+ up.upload(send, recv, null);
+
+ assertEquals(up.getPeerUserAgent(), "JGit-test/1.2.3");
+ }
+
+ @Test
+ public void testGetPeerAgentProtocolV2() throws Exception {
+ server.getConfig().setString("protocol", null, "version", "2");
+
+ RevCommit one = remote.commit().message("1").create();
+ remote.update("one", one);
+
+ UploadPack up = new UploadPack(server);
+ up.setExtraParameters(Sets.of("version=2"));
+
+ ByteArrayInputStream send = linesAsInputStream(
+ "command=fetch\n", "agent=JGit-test/1.2.4\n",
+ PacketLineIn.DELIM, "want " + one.getName() + "\n",
+ "have 11cedf1b796d44207da702f7d420684022fc0f09\n", "done\n",
+ PacketLineIn.END);
+
+ ByteArrayOutputStream recv = new ByteArrayOutputStream();
+ up.upload(send, recv, null);
+
+ assertEquals(up.getPeerUserAgent(), "JGit-test/1.2.4");
+ }
+
private static class RejectAllRefFilter implements RefFilter {
@Override
public Map<String, Ref> filter(Map<String, Ref> refs) {
import java.util.Set;
import org.eclipse.jgit.annotations.NonNull;
+import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.lib.ObjectId;
/**
final List<String> deepenNotRefs;
+ @Nullable
+ final String agent;
+
/**
* Initialize the common fields of a fetch request.
*
* @param deepenSince
* Requests that the shallow clone/fetch should be cut at a
* specific time, instead of depth
+ * @param agent
+ * agent as reported by the client in the request body
*/
FetchRequest(@NonNull Set<ObjectId> wantIds, int depth,
@NonNull Set<ObjectId> clientShallowCommits, long filterBlobLimit,
@NonNull Set<String> clientCapabilities, int deepenSince,
- @NonNull List<String> deepenNotRefs) {
+ @NonNull List<String> deepenNotRefs, @Nullable String agent) {
this.wantIds = requireNonNull(wantIds);
this.depth = depth;
this.clientShallowCommits = requireNonNull(clientShallowCommits);
this.clientCapabilities = requireNonNull(clientCapabilities);
this.deepenSince = deepenSince;
this.deepenNotRefs = requireNonNull(deepenNotRefs);
+ this.agent = agent;
}
/**
* These options are listed and well-defined in the git protocol
* specification.
*
- * @return capabilities sent by the client
+ * The agent capability is not included in this set. It can be retrieved via
+ * {@link #getAgent()}.
+ *
+ * @return capabilities sent by the client (excluding the "agent"
+ * capability)
*/
@NonNull
Set<String> getClientCapabilities() {
List<String> getDeepenNotRefs() {
return deepenNotRefs;
}
+
+ /**
+ * @return string identifying the agent (as sent in the request body by the
+ * client)
+ */
+ @Nullable
+ String getAgent() {
+ return agent;
+ }
}
\ No newline at end of file
import java.util.Set;
import org.eclipse.jgit.annotations.NonNull;
+import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.lib.ObjectId;
/**
FetchV0Request(@NonNull Set<ObjectId> wantIds, int depth,
@NonNull Set<ObjectId> clientShallowCommits, long filterBlobLimit,
- @NonNull Set<String> clientCapabilities) {
+ @NonNull Set<String> clientCapabilities, @Nullable String agent) {
super(wantIds, depth, clientShallowCommits, filterBlobLimit,
- clientCapabilities, 0, Collections.emptyList());
+ clientCapabilities, 0, Collections.emptyList(), agent);
}
static final class Builder {
final Set<String> clientCaps = new HashSet<>();
+ String agent;
+
/**
* @param objectId
* object id received in a "want" line
* @return this builder
*/
Builder addClientCapabilities(Collection<String> clientCapabilities) {
- clientCaps.addAll(clientCapabilities);
+ for (String cap: clientCapabilities) {
+ // TODO(ifrade): Do this is done on parse time
+ if (cap.startsWith("agent=")) { //$NON-NLS-1$
+ agent = cap.substring("agent=".length()); //$NON-NLS-1$
+ } else {
+ clientCaps.add(cap);
+ }
+ }
+ return this;
+ }
+
+ /**
+ * @param clientAgent
+ * agent line sent by the client in the request body
+ * @return this builder
+ */
+ Builder setAgent(String clientAgent) {
+ agent = clientAgent;
return this;
}
FetchV0Request build() {
return new FetchV0Request(wantIds, depth, clientShallowCommits,
- filterBlobLimit, clientCaps);
+ filterBlobLimit, clientCaps, agent);
}
+
}
}
private final boolean doneReceived;
- @Nullable
- private final String agent;
-
@NonNull
private final List<String> serverOptions;
boolean doneReceived, @NonNull Set<String> clientCapabilities,
@Nullable String agent, @NonNull List<String> serverOptions) {
super(wantIds, depth, clientShallowCommits, filterBlobLimit,
- clientCapabilities, deepenSince, deepenNotRefs);
+ clientCapabilities, deepenSince, deepenNotRefs, agent);
this.peerHas = requireNonNull(peerHas);
this.wantedRefs = requireNonNull(wantedRefs);
this.doneReceived = doneReceived;
- this.agent = agent;
this.serverOptions = requireNonNull(serverOptions);
}
return doneReceived;
}
- /**
- * @return string identifying the agent (as sent in the request body by the
- * client)
- */
- @Nullable
- String getAgent() {
- return agent;
- }
-
/**
* Options received in server-option lines. The caller can choose to act on
* these in an application-specific way
* @since 4.0
*/
public String getPeerUserAgent() {
- if (currentRequest == null) {
- return userAgent;
+ if (currentRequest != null && currentRequest.getAgent() != null) {
+ return currentRequest.getAgent();
}
- return UserAgent.getAgent(currentRequest.getClientCapabilities(),
- userAgent);
+ return userAgent;
}
private boolean negotiate(FetchRequest req,