diff options
author | Jonathan Nieder <jrn@google.com> | 2018-10-22 13:49:38 -0400 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org <gerrit@eclipse.org> | 2018-10-22 13:49:38 -0400 |
commit | a579a56e3ae2e5ab6b3287c4450fe6c59ebc85e7 (patch) | |
tree | afc76bb3aafb162d5f06321b83510f3436b23e98 /org.eclipse.jgit | |
parent | 8420c729a0a8b55b5a9c9eae1064a2333de7d188 (diff) | |
parent | 94a3d8bae9ff9a04e32ef5b82539ba70af0649bf (diff) | |
download | jgit-a579a56e3ae2e5ab6b3287c4450fe6c59ebc85e7.tar.gz jgit-a579a56e3ae2e5ab6b3287c4450fe6c59ebc85e7.zip |
Merge changes from topic 'moving-to-request-2'
* changes:
UploadPack v0: Extract "agent" client capability at parse time
UploadPack: Return correct peer user agent on v2 requests
Diffstat (limited to 'org.eclipse.jgit')
6 files changed, 81 insertions, 29 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/parser/FirstWant.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/parser/FirstWant.java index 1ac9b18874..2dae021702 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/parser/FirstWant.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/parser/FirstWant.java @@ -42,11 +42,13 @@ */ 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; @@ -72,6 +74,11 @@ public class FirstWant { 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. * @@ -84,6 +91,7 @@ public class FirstWant { 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); @@ -91,8 +99,15 @@ public class FirstWant { 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 { @@ -100,12 +115,14 @@ public class FirstWant { 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. */ @@ -113,8 +130,17 @@ public class FirstWant { 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; + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchRequest.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchRequest.java index 5d28a4d9f2..40ba3a3ad2 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchRequest.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchRequest.java @@ -48,6 +48,7 @@ import java.util.List; import java.util.Set; import org.eclipse.jgit.annotations.NonNull; +import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.lib.ObjectId; /** @@ -69,6 +70,9 @@ abstract class FetchRequest { final List<String> deepenNotRefs; + @Nullable + final String agent; + /** * Initialize the common fields of a fetch request. * @@ -88,11 +92,13 @@ abstract class FetchRequest { * @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); @@ -100,6 +106,7 @@ abstract class FetchRequest { this.clientCapabilities = requireNonNull(clientCapabilities); this.deepenSince = deepenSince; this.deepenNotRefs = requireNonNull(deepenNotRefs); + this.agent = agent; } /** @@ -146,7 +153,11 @@ abstract class FetchRequest { * 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() { @@ -171,4 +182,13 @@ abstract class FetchRequest { 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 diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV0Request.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV0Request.java index bb358c886f..05f4a8155f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV0Request.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV0Request.java @@ -48,6 +48,7 @@ import java.util.HashSet; import java.util.Set; import org.eclipse.jgit.annotations.NonNull; +import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.lib.ObjectId; /** @@ -57,9 +58,9 @@ final class FetchV0Request extends FetchRequest { 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 { @@ -74,6 +75,8 @@ final class FetchV0Request extends FetchRequest { final Set<String> clientCaps = new HashSet<>(); + String agent; + /** * @param objectId * object id received in a "want" line @@ -116,6 +119,16 @@ final class FetchV0Request extends FetchRequest { } /** + * @param clientAgent + * agent line sent by the client in the request body + * @return this builder + */ + Builder setAgent(String clientAgent) { + agent = clientAgent; + return this; + } + + /** * @param filterBlobLim * blob limit set in a "filter" line * @return this builder @@ -127,7 +140,8 @@ final class FetchV0Request extends FetchRequest { FetchV0Request build() { return new FetchV0Request(wantIds, depth, clientShallowCommits, - filterBlobLimit, clientCaps); + filterBlobLimit, clientCaps, agent); } + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV2Request.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV2Request.java index 951d2d79ea..8e36a109e9 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV2Request.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV2Request.java @@ -71,9 +71,6 @@ public final class FetchV2Request extends FetchRequest { private final boolean doneReceived; - @Nullable - private final String agent; - @NonNull private final List<String> serverOptions; @@ -86,11 +83,10 @@ public final class FetchV2Request extends FetchRequest { 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); } @@ -118,15 +114,6 @@ public final class FetchV2Request extends FetchRequest { } /** - * @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 * diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV0Parser.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV0Parser.java index 60d5fff6dd..21498d6f5c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV0Parser.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV0Parser.java @@ -143,6 +143,7 @@ final class ProtocolV0Parser { if (line.length() > 45) { FirstWant firstLine = FirstWant.fromLine(line); reqBuilder.addClientCapabilities(firstLine.getCapabilities()); + reqBuilder.setAgent(firstLine.getAgent()); line = firstLine.getLine(); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java index 535c914fc3..b69f2cd92f 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -209,6 +209,11 @@ public class UploadPack { /** @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(); } } @@ -1369,12 +1374,11 @@ public class UploadPack { * @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, |