]> source.dussan.org Git - jgit.git/commitdiff
UploadPack: Use request instead of field for depth 53/129753/4
authorIvan Frade <ifrade@google.com>
Tue, 18 Sep 2018 22:52:48 +0000 (15:52 -0700)
committerIvan Frade <ifrade@google.com>
Tue, 16 Oct 2018 23:13:04 +0000 (16:13 -0700)
One more step in removing state from UploadPack, using the request
object instead.

Unfortunately, hooks get from UploadPack information about the current
request. Changing the hooks to receive the request is a public API
change, so at the moment lets keep a reference to the current request.

This kills half the benefit of using a request object vs fields, but
at least we still get better modularity.

Change-Id: I86803d876a621b727c66ee73f2880c93190629e9
Signed-off-by: Ivan Frade <ifrade@google.com>
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

index 752fa4045ac06208a0f485062751463c97807390..0df350819eb0a4a2b52af5183686cc9ba1b26cf4 100644 (file)
@@ -302,9 +302,6 @@ public class UploadPack {
        /** Shallow commits the client already has. */
        private Set<ObjectId> clientShallowCommits = new HashSet<>();
 
-       /** Desired depth from the client on a shallow request. */
-       private int depth;
-
        /** Commit time of the oldest common commit, in seconds. */
        private int oldestTime;
 
@@ -338,6 +335,15 @@ public class UploadPack {
 
        private PackStatistics statistics;
 
+       /**
+        * Request this instance is handling.
+        *
+        * We need to keep a reference to it for {@link PreUploadHook pre upload
+        * hooks}. They receive a reference this instance and invoke methods like
+        * getDepth() to get information about the request.
+        */
+       private FetchRequest currentRequest;
+
        /**
         * Create a new pack upload for an open repository.
         *
@@ -809,11 +815,11 @@ public class UploadPack {
 
                        ProtocolV0Parser parser = new ProtocolV0Parser(transferConfig);
                        req = parser.recvWants(pckIn);
+                       currentRequest = req;
 
                        wantIds = req.getWantIds();
                        clientShallowCommits = req.getClientShallowCommits();
                        options = req.getClientCapabilities();
-                       depth = req.getDepth();
 
                        if (req.getWantIds().isEmpty()) {
                                preUploadHook.onBeginNegotiateRound(this, req.getWantIds(), 0);
@@ -833,7 +839,7 @@ public class UploadPack {
 
                        if (!clientShallowCommits.isEmpty())
                                verifyClientShallow(clientShallowCommits);
-                       if (depth != 0 || req.getDeepenSince() != 0) {
+                       if (req.getDepth() != 0 || req.getDeepenSince() != 0) {
                                computeShallowsAndUnshallows(req, shallow -> {
                                        pckOut.writeString("shallow " + shallow.name() + '\n'); //$NON-NLS-1$
                                }, unshallow -> {
@@ -844,7 +850,7 @@ public class UploadPack {
                        }
                        if (!clientShallowCommits.isEmpty())
                                walk.assumeShallow(clientShallowCommits);
-                       sendPack = negotiate(accumulator);
+                       sendPack = negotiate(req, accumulator);
                        accumulator.timeNegotiating += System.currentTimeMillis()
                                        - negotiateStart;
 
@@ -944,6 +950,7 @@ public class UploadPack {
                ProtocolV2Parser parser = new ProtocolV2Parser(transferConfig);
                FetchV2Request req = parser.parseFetchRequest(pckIn,
                                db.getRefDatabase());
+               currentRequest = req;
                rawOut.stopBuffering();
 
                protocolV2Hook.onFetch(req);
@@ -953,7 +960,6 @@ public class UploadPack {
                options = req.getClientCapabilities();
                wantIds = req.getWantIds();
                clientShallowCommits = req.getClientShallowCommits();
-               depth = req.getDepth();
 
                boolean sectionSent = false;
                boolean mayHaveShallow = req.getDepth() != 0
@@ -1343,9 +1349,9 @@ public class UploadPack {
         * @since 4.0
         */
        public int getDepth() {
-               if (options == null)
+               if (currentRequest == null)
                        throw new RequestNotYetReadException();
-               return depth;
+               return currentRequest.getDepth();
        }
 
        /**
@@ -1367,7 +1373,8 @@ public class UploadPack {
                return UserAgent.getAgent(options, userAgent);
        }
 
-       private boolean negotiate(PackStatistics.Accumulator accumulator)
+       private boolean negotiate(FetchRequest req,
+                       PackStatistics.Accumulator accumulator)
                        throws IOException {
                okToGiveUp = Boolean.FALSE;
 
@@ -1383,7 +1390,7 @@ public class UploadPack {
                                // disconnected, and will try another request with actual want/have.
                                // Don't report the EOF here, its a bug in the protocol that the client
                                // just disconnects without sending an END.
-                               if (!biDirectionalPipe && depth > 0)
+                               if (!biDirectionalPipe && req.getDepth() > 0)
                                        return false;
                                throw eof;
                        }
@@ -1899,7 +1906,8 @@ public class UploadPack {
                        } else {
                                pw.setUseCachedPacks(true);
                        }
-                       pw.setUseBitmaps(depth == 0 && clientShallowCommits.isEmpty());
+                       pw.setUseBitmaps(
+                                       req.getDepth() == 0 && clientShallowCommits.isEmpty());
                        pw.setClientShallowCommits(clientShallowCommits);
                        pw.setReuseDeltaCommits(true);
                        pw.setDeltaBaseAsOffset(options.contains(OPTION_OFS_DELTA));
@@ -1922,9 +1930,10 @@ public class UploadPack {
                        }
 
                        RevWalk rw = walk;
-                       if (depth > 0 || req.getDeepenSince() != 0) {
-                               int walkDepth = depth == 0 ? Integer.MAX_VALUE : depth - 1;
-                               pw.setShallowPack(depth, unshallowCommits);
+                       if (req.getDepth() > 0 || req.getDeepenSince() != 0) {
+                               int walkDepth = req.getDepth() == 0 ? Integer.MAX_VALUE
+                                               : req.getDepth() - 1;
+                               pw.setShallowPack(req.getDepth(), unshallowCommits);
                                rw = new DepthWalk.RevWalk(walk.getObjectReader(), walkDepth);
                                ((DepthWalk.RevWalk) rw).setDeepenSince(req.getDeepenSince());
                                rw.assumeShallow(clientShallowCommits);