]> source.dussan.org Git - jgit.git/commitdiff
Move protocol v0/v1 parsing to its own class and request objects 59/129459/18
authorIvan Frade <ifrade@google.com>
Tue, 28 Aug 2018 20:57:52 +0000 (13:57 -0700)
committerIvan Frade <ifrade@google.com>
Mon, 15 Oct 2018 23:27:55 +0000 (16:27 -0700)
Protocol v0/v1 parsing code doesn't have any real dependency on UploadPack.

Move it to its class and use a request object to read the data in
UploadPack.

This makes the code easier to test, keeps similar structure than protocol v2,
reduces the line count of UploadPack and paves the way to remove the
members as implicit parameters in it.

Change-Id: I8188da8bd77e90230a7e37c02d800ea18463694f
Signed-off-by: Ivan Frade <ifrade@google.com>
org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV0Request.java [new file with mode: 0644]
org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV0Parser.java [new file with mode: 0644]
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV0Request.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchV0Request.java
new file mode 100644 (file)
index 0000000..4f1f979
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+ * Copyright (C) 2018, Google LLC.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.eclipse.jgit.transport;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.eclipse.jgit.lib.ObjectId;
+
+/**
+ * Fetch request in the V0/V1 protocol.
+ */
+final class FetchV0Request {
+
+       final Set<ObjectId> wantIds;
+
+       final int depth;
+
+       final Set<ObjectId> clientShallowCommits;
+
+       final long filterBlobLimit;
+
+       final Set<String> clientCapabilities;
+
+       FetchV0Request(Set<ObjectId> wantIds, int depth,
+                       Set<ObjectId> clientShallowCommits, long filterBlobLimit,
+                       Set<String> clientCapabilities) {
+               this.wantIds = wantIds;
+               this.depth = depth;
+               this.clientShallowCommits = clientShallowCommits;
+               this.filterBlobLimit = filterBlobLimit;
+               this.clientCapabilities = clientCapabilities;
+       }
+
+       Set<ObjectId> getWantIds() {
+               return wantIds;
+       }
+
+       int getDepth() {
+               return depth;
+       }
+
+       Set<ObjectId> getClientShallowCommits() {
+               return clientShallowCommits;
+       }
+
+       long getFilterBlobLimit() {
+               return filterBlobLimit;
+       }
+
+       Set<String> getClientCapabilities() {
+               return clientCapabilities;
+       }
+
+       static final class Builder {
+
+               int depth;
+
+               Set<ObjectId> wantIds = new HashSet<>();
+
+               Set<ObjectId> clientShallowCommits = new HashSet<>();
+
+               long filterBlobLimit = -1;
+
+               Set<String> clientCaps = new HashSet<>();
+
+               /**
+                * @param objectId
+                *            object id received in a "want" line
+                * @return this builder
+                */
+               Builder addWantId(ObjectId objectId) {
+                       wantIds.add(objectId);
+                       return this;
+               }
+
+               /**
+                * @param d
+                *            depth set in a "deepen" line
+                * @return this builder
+                */
+               Builder setDepth(int d) {
+                       depth = d;
+                       return this;
+               }
+
+               /**
+                * @param shallowOid
+                *            object id received in a "shallow" line
+                * @return this builder
+                */
+               Builder addClientShallowCommit(ObjectId shallowOid) {
+                       clientShallowCommits.add(shallowOid);
+                       return this;
+               }
+
+               /**
+                * @param clientCapabilities
+                *            client capabilities sent by the client in the first want
+                *            line of the request
+                * @return this builder
+                */
+               Builder addClientCapabilities(Collection<String> clientCapabilities) {
+                       clientCaps.addAll(clientCapabilities);
+                       return this;
+               }
+
+               /**
+                * @param filterBlobLim
+                *            blob limit set in a "filter" line
+                * @return this builder
+                */
+               Builder setFilterBlobLimit(long filterBlobLim) {
+                       filterBlobLimit = filterBlobLim;
+                       return this;
+               }
+
+               FetchV0Request build() {
+                       return new FetchV0Request(wantIds, depth, clientShallowCommits,
+                                       filterBlobLimit, clientCaps);
+               }
+       }
+}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV0Parser.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/ProtocolV0Parser.java
new file mode 100644 (file)
index 0000000..60d5fff
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2018, Google LLC.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.eclipse.jgit.transport;
+
+import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_FILTER;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.text.MessageFormat;
+
+import org.eclipse.jgit.errors.PackProtocolException;
+import org.eclipse.jgit.internal.JGitText;
+import org.eclipse.jgit.internal.transport.parser.FirstWant;
+import org.eclipse.jgit.lib.ObjectId;
+
+/**
+ * Parser for git protocol versions 0 and 1.
+ *
+ * It reads the lines coming through the {@link PacketLineIn} and builds a
+ * {@link FetchV0Request} object.
+ *
+ * It requires a transferConfig object to know if the server supports filters.
+ */
+final class ProtocolV0Parser {
+
+       private final TransferConfig transferConfig;
+
+       ProtocolV0Parser(TransferConfig transferConfig) {
+               this.transferConfig = transferConfig;
+       }
+
+       /**
+        * Parse an incoming protocol v1 upload request arguments from the wire.
+        *
+        * The incoming PacketLineIn is consumed until an END line, but the caller
+        * is responsible for closing it (if needed).
+        *
+        * @param pckIn
+        *            incoming lines. This method will read until an END line.
+        * @return a FetchV0Request with the data received in the wire.
+        * @throws PackProtocolException
+        * @throws IOException
+        */
+       FetchV0Request recvWants(PacketLineIn pckIn)
+                       throws PackProtocolException, IOException {
+               FetchV0Request.Builder reqBuilder = new FetchV0Request.Builder();
+
+               boolean isFirst = true;
+               boolean filterReceived = false;
+
+               for (;;) {
+                       String line;
+                       try {
+                               line = pckIn.readString();
+                       } catch (EOFException eof) {
+                               if (isFirst) {
+                                       break;
+                               }
+                               throw eof;
+                       }
+
+                       if (line == PacketLineIn.END) {
+                               break;
+                       }
+
+                       if (line.startsWith("deepen ")) { //$NON-NLS-1$
+                               int depth = Integer.parseInt(line.substring(7));
+                               if (depth <= 0) {
+                                       throw new PackProtocolException(
+                                                       MessageFormat.format(JGitText.get().invalidDepth,
+                                                                       Integer.valueOf(depth)));
+                               }
+                               reqBuilder.setDepth(depth);
+                               continue;
+                       }
+
+                       if (line.startsWith("shallow ")) { //$NON-NLS-1$
+                               reqBuilder.addClientShallowCommit(
+                                               ObjectId.fromString(line.substring(8)));
+                               continue;
+                       }
+
+                       if (transferConfig.isAllowFilter()
+                                       && line.startsWith(OPTION_FILTER + " ")) { //$NON-NLS-1$
+                               String arg = line.substring(OPTION_FILTER.length() + 1);
+
+                               if (filterReceived) {
+                                       throw new PackProtocolException(
+                                                       JGitText.get().tooManyFilters);
+                               }
+                               filterReceived = true;
+
+                               reqBuilder.setFilterBlobLimit(ProtocolV2Parser.filterLine(arg));
+                               continue;
+                       }
+
+                       if (!line.startsWith("want ") || line.length() < 45) { //$NON-NLS-1$
+                               throw new PackProtocolException(MessageFormat
+                                               .format(JGitText.get().expectedGot, "want", line)); //$NON-NLS-1$
+                       }
+
+                       if (isFirst) {
+                               if (line.length() > 45) {
+                                       FirstWant firstLine = FirstWant.fromLine(line);
+                                       reqBuilder.addClientCapabilities(firstLine.getCapabilities());
+                                       line = firstLine.getLine();
+                               }
+                       }
+
+                       reqBuilder.addWantId(ObjectId.fromString(line.substring(5)));
+                       isFirst = false;
+               }
+
+               return reqBuilder.build();
+       }
+
+}
index 82e174548b9e230bded69002d179f425c0b3f601..5c7bde05f85d1dab8d4454489c4aaab4b81339c2 100644 (file)
@@ -291,7 +291,7 @@ public class UploadPack {
        String userAgent;
 
        /** Raw ObjectIds the client has asked for, before validating them. */
-       private final Set<ObjectId> wantIds = new HashSet<>();
+       private Set<ObjectId> wantIds = new HashSet<>();
 
        /** Objects the client wants to obtain. */
        private final Set<RevObject> wantAll = new HashSet<>();
@@ -820,18 +820,28 @@ public class UploadPack {
 
                        long negotiateStart = System.currentTimeMillis();
                        accumulator.advertised = advertised.size();
-                       recvWants();
-                       if (wantIds.isEmpty()) {
-                               preUploadHook.onBeginNegotiateRound(this, wantIds, 0);
-                               preUploadHook.onEndNegotiateRound(this, wantIds, 0, 0, false);
+
+                       ProtocolV0Parser parser = new ProtocolV0Parser(transferConfig);
+                       FetchV0Request req = parser.recvWants(pckIn);
+
+                       wantIds = req.getWantIds();
+                       clientShallowCommits = req.getClientShallowCommits();
+                       filterBlobLimit = req.getFilterBlobLimit();
+                       options = req.getClientCapabilities();
+                       depth = req.getDepth();
+
+                       if (req.getWantIds().isEmpty()) {
+                               preUploadHook.onBeginNegotiateRound(this, req.getWantIds(), 0);
+                               preUploadHook.onEndNegotiateRound(this, req.getWantIds(), 0, 0,
+                                               false);
                                return;
                        }
-                       accumulator.wants = wantIds.size();
+                       accumulator.wants = req.getWantIds().size();
 
-                       if (options.contains(OPTION_MULTI_ACK_DETAILED)) {
+                       if (req.getClientCapabilities().contains(OPTION_MULTI_ACK_DETAILED)) {
                                multiAck = MultiAck.DETAILED;
-                               noDone = options.contains(OPTION_NO_DONE);
-                       } else if (options.contains(OPTION_MULTI_ACK))
+                               noDone = req.getClientCapabilities().contains(OPTION_NO_DONE);
+                       } else if (req.getClientCapabilities().contains(OPTION_MULTI_ACK))
                                multiAck = MultiAck.CONTINUE;
                        else
                                multiAck = MultiAck.OFF;
@@ -1339,67 +1349,6 @@ public class UploadPack {
                return msgOut;
        }
 
-       private void recvWants() throws IOException {
-               boolean isFirst = true;
-               boolean filterReceived = false;
-               for (;;) {
-                       String line;
-                       try {
-                               line = pckIn.readString();
-                       } catch (EOFException eof) {
-                               if (isFirst)
-                                       break;
-                               throw eof;
-                       }
-
-                       if (line == PacketLineIn.END)
-                               break;
-
-                       if (line.startsWith("deepen ")) { //$NON-NLS-1$
-                               depth = Integer.parseInt(line.substring(7));
-                               if (depth <= 0) {
-                                       throw new PackProtocolException(
-                                                       MessageFormat.format(JGitText.get().invalidDepth,
-                                                                       Integer.valueOf(depth)));
-                               }
-                               continue;
-                       }
-
-                       if (line.startsWith("shallow ")) { //$NON-NLS-1$
-                               clientShallowCommits.add(ObjectId.fromString(line.substring(8)));
-                               continue;
-                       }
-
-                       if (transferConfig.isAllowFilter()
-                                       && line.startsWith(OPTION_FILTER + " ")) { //$NON-NLS-1$
-                               String arg = line.substring(OPTION_FILTER.length() + 1);
-
-                               if (filterReceived) {
-                                       throw new PackProtocolException(JGitText.get().tooManyFilters);
-                               }
-                               filterReceived = true;
-
-                               filterBlobLimit = ProtocolV2Parser.filterLine(arg);
-                               continue;
-                       }
-
-                       if (!line.startsWith("want ") || line.length() < 45) //$NON-NLS-1$
-                               throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedGot, "want", line)); //$NON-NLS-1$
-
-                       if (isFirst) {
-                               if (line.length() > 45) {
-                                       FirstWant firstLine = FirstWant.fromLine(line);
-                                       options = firstLine.getCapabilities();
-                                       line = firstLine.getLine();
-                               } else
-                                       options = Collections.emptySet();
-                       }
-
-                       wantIds.add(ObjectId.fromString(line.substring(5)));
-                       isFirst = false;
-               }
-       }
-
        /**
         * Returns the clone/fetch depth. Valid only after calling recvWants(). A
         * depth of 1 means return only the wants.