package org.eclipse.jgit.internal.transport.parser;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
r.getLine());
Set<String> capabilities = r.getCapabilities();
Set<String> expectedCapabilities = new HashSet<>(
- Arrays.asList("no-progress", "include-tag", "ofs-delta",
- "agent=JGit/unknown"));
+ Arrays.asList("no-progress", "include-tag", "ofs-delta"));
assertEquals(expectedCapabilities, capabilities);
+ assertEquals("JGit/unknown", r.getAgent());
}
@Test
assertEquals("want b9d4d1eb2f93058814480eae9e1b67550f46ee38",
r.getLine());
assertTrue(r.getCapabilities().isEmpty());
+ assertNull(r.getAgent());
}
private String makeFirstWantLine(String capability) {
List<String> validNames = Arrays.asList(
"c", "cap", "C", "CAP", "1", "1cap", "cap-64k_test",
"-", "-cap",
- "_", "_cap", "agent=pack.age/Version");
+ "_", "_cap");
for (String capability: validNames) {
FirstWant r = FirstWant.fromLine(makeFirstWantLine(capability));
assertTrue(r.getCapabilities().contains(capability));
}
}
+
+ @Test
+ public void testFirstWantValidAgentName() throws PackProtocolException {
+ FirstWant r = FirstWant.fromLine(makeFirstWantLine("agent=pack.age/Version"));
+ assertEquals(r.getCapabilities().size(), 0);
+ assertEquals("pack.age/Version", r.getAgent());
+ }
}
*/
package org.eclipse.jgit.internal.transport.parser;
-import java.util.Arrays;
+import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
+
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
+import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.errors.PackProtocolException;
import org.eclipse.jgit.internal.JGitText;
private final Set<String> capabilities;
+ @Nullable
+ private final String agent;
+
+ private static final String AGENT_PREFIX = OPTION_AGENT + '=';
+
/**
* Parse the first want line in the protocol v0/v1 pack negotiation.
*
public static FirstWant fromLine(String line) throws PackProtocolException {
String wantLine;
Set<String> capabilities;
+ String agent = null;
if (line.length() > 45) {
String opt = line.substring(45);
throw new PackProtocolException(JGitText.get().wantNoSpaceWithCapabilities);
}
opt = opt.substring(1);
- HashSet<String> opts = new HashSet<>(
- Arrays.asList(opt.split(" "))); //$NON-NLS-1$
+
+ HashSet<String> opts = new HashSet<>();
+ for (String clientCapability : opt.split(" ")) { //$NON-NLS-1$
+ if (clientCapability.startsWith(AGENT_PREFIX)) {
+ agent = clientCapability.substring(AGENT_PREFIX.length());
+ } else {
+ opts.add(clientCapability);
+ }
+ }
wantLine = line.substring(0, 45);
capabilities = Collections.unmodifiableSet(opts);
} else {
capabilities = Collections.emptySet();
}
- return new FirstWant(wantLine, capabilities);
+ return new FirstWant(wantLine, capabilities, agent);
}
- private FirstWant(String line, Set<String> capabilities) {
+ private FirstWant(String line, Set<String> capabilities,
+ @Nullable String agent) {
this.line = line;
this.capabilities = capabilities;
+ this.agent = agent;
}
/** @return non-capabilities part of the line. */
return line;
}
- /** @return capabilities parsed from the line as an immutable set. */
+ /**
+ * @return capabilities parsed from the line as an immutable set (excluding
+ * agent).
+ */
public Set<String> getCapabilities() {
return capabilities;
}
+
+ /** @return client user agent parsed from the line. */
+ @Nullable
+ public String getAgent() {
+ return agent;
+ }
}
* @return this builder
*/
Builder addClientCapabilities(Collection<String> 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);
- }
- }
+ clientCaps.addAll(clientCapabilities);
return this;
}
if (line.length() > 45) {
FirstWant firstLine = FirstWant.fromLine(line);
reqBuilder.addClientCapabilities(firstLine.getCapabilities());
+ reqBuilder.setAgent(firstLine.getAgent());
line = firstLine.getLine();
}
}
/** @return capabilities parsed from the line. */
public Set<String> getOptions() {
+ if (firstWant.getAgent() != null) {
+ Set<String> caps = new HashSet<>(firstWant.getCapabilities());
+ caps.add(OPTION_AGENT + '=' + firstWant.getAgent());
+ return caps;
+ }
return firstWant.getCapabilities();
}
}