From d2787d481efcd5f883d52eed5029be380196f0ef Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Wed, 7 Mar 2012 12:53:49 -0800 Subject: [PATCH] Extract the capability parsing logic in {Upload,Receive}Pack Change-Id: I7ac4e0ae98872a74b01162b5ca936fb15e2f8cff --- .../eclipse/jgit/transport/ReceivePack.java | 43 +++++++++++++--- .../eclipse/jgit/transport/UploadPack.java | 49 ++++++++++++++++--- 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java index f74e6661b..0b4356091 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ReceivePack.java @@ -98,6 +98,39 @@ import org.eclipse.jgit.util.io.TimeoutOutputStream; * Implements the server side of a push connection, receiving objects. */ public class ReceivePack { + /** Data in the first line of a request, the line itself plus capabilities. */ + public static class FirstLine { + private final String line; + private final Set capabilities; + + /** + * Parse the first line of a receive-pack request. + * + * @param line + * line from the client. + */ + public FirstLine(String line) { + final HashSet caps = new HashSet(); + final int nul = line.indexOf('\0'); + if (nul >= 0) { + for (String c : line.substring(nul + 1).split(" ")) + caps.add(c); + } + this.line = line.substring(0, nul); + this.capabilities = Collections.unmodifiableSet(caps); + } + + /** @return non-capabilities part of the line. */ + public String getLine() { + return line; + } + + /** @return capabilities parsed from the line. */ + public Set getCapabilities() { + return capabilities; + } + } + /** Database we write the stored objects into. */ private final Repository db; @@ -713,7 +746,6 @@ public class ReceivePack { pckOut = new PacketLineOut(rawOut); pckOut.setFlushOnEnd(false); - enabledCapabilities = new HashSet(); commands = new ArrayList(); service(); @@ -891,12 +923,9 @@ public class ReceivePack { break; if (commands.isEmpty()) { - final int nul = line.indexOf('\0'); - if (nul >= 0) { - for (String c : line.substring(nul + 1).split(" ")) - enabledCapabilities.add(c); - line = line.substring(0, nul); - } + final FirstLine firstLine = new FirstLine(line); + enabledCapabilities = firstLine.getCapabilities(); + line = firstLine.getLine(); } if (line.length() < 83) { 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 c61a23cab..fd3df85db 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -118,6 +118,44 @@ public class UploadPack { ANY; } + /** Data in the first line of a request, the line itself plus options. */ + public static class FirstLine { + private final String line; + private final Set options; + + /** + * Parse the first line of a receive-pack request. + * + * @param line + * line from the client. + */ + public FirstLine(String line) { + if (line.length() > 45) { + final HashSet opts = new HashSet(); + String opt = line.substring(45); + if (opt.startsWith(" ")) + opt = opt.substring(1); + for (String c : opt.split(" ")) + opts.add(c); + this.line = line.substring(0, 45); + this.options = Collections.unmodifiableSet(opts); + } else { + this.line = line; + this.options = Collections.emptySet(); + } + } + + /** @return non-capabilities part of the line. */ + public String getLine() { + return line; + } + + /** @return options parsed from the line. */ + public Set getOptions() { + return options; + } + } + /** Database we read the objects from. */ private final Repository db; @@ -167,7 +205,7 @@ public class UploadPack { private PreUploadHook preUploadHook = PreUploadHook.NULL; /** Capabilities requested by the client. */ - private final Set options = new HashSet(); + private Set options; /** Raw ObjectIds the client has asked for, before validating them. */ private final Set wantIds = new HashSet(); @@ -664,12 +702,9 @@ public class UploadPack { throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedGot, "want", line)); if (isFirst && line.length() > 45) { - String opt = line.substring(45); - if (opt.startsWith(" ")) - opt = opt.substring(1); - for (String c : opt.split(" ")) - options.add(c); - line = line.substring(0, 45); + final FirstLine firstLine = new FirstLine(line); + options = firstLine.getOptions(); + line = firstLine.getLine(); } wantIds.add(ObjectId.fromString(line.substring(5)));