From 9694d89ea1c2725904f75eb1e8002e4b7eeec516 Mon Sep 17 00:00:00 2001 From: Ivan Frade Date: Tue, 18 Sep 2018 15:52:48 -0700 Subject: [PATCH] UploadPack: Use request instead of field for depth 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 --- .../eclipse/jgit/transport/UploadPack.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) 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 752fa4045a..0df350819e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -302,9 +302,6 @@ public class UploadPack { /** Shallow commits the client already has. */ private Set 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); -- 2.39.5