]> source.dussan.org Git - jgit.git/commitdiff
UploadPack: do not check reachability of visible SHA1s 96/193496/11
authorLuca Milanesio <luca.milanesio@gmail.com>
Thu, 19 May 2022 10:12:28 +0000 (11:12 +0100)
committerLuca Milanesio <luca.milanesio@gmail.com>
Sat, 25 Jun 2022 10:45:05 +0000 (06:45 -0400)
When JGit needs to serve a Git client requesting SHA1s
during the want phase, it needs to make a full reachability
check from the advertised refs to the ones requested to
keep all objects in the correct scope of confidentiality
allowed by the avertised refs.

The check is also performed when the SHA1 corresponds to
one of the tips of the advertised refs which is a waste of
resources.

Example:

fetch> ref-prefix refs/heads/foo
fetch< 900505eb8ce8ced2a1757906da1b25c357b9654e refs/heads/foo
fetch< 0000
fetch> command=fetch
fetch> 0001
fetch> thin-pack
fetch> ofs-delta
fetch> want 900505eb8ce8ced2a1757906da1b25c357b9654e

The SHA1 in the want is the tip of refs/heads/foo and therefore
the full reachability check can be shortened and resolved more
quickly.

Change-Id: I49bd9e2464e0bd3bca2abf14c6e9df550d07383b
Signed-off-by: Luca Milanesio <luca.milanesio@gmail.com>
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

index 9080f51d7a10dce5c0da309a23dc65fd5d927ebe..c389ce44f46d174bd7ddf6c8aff1b5790d35223e 100644 (file)
@@ -1981,12 +1981,16 @@ public class UploadPack {
                        throws IOException {
 
                ObjectReader reader = up.getRevWalk().getObjectReader();
+               Set<ObjectId> directlyVisibleObjects = refIdSet(visibleRefs);
+               List<ObjectId> nonTipWants = notAdvertisedWants.stream()
+                               .filter(not(directlyVisibleObjects::contains))
+                               .collect(Collectors.toList());
 
                try (RevWalk walk = new RevWalk(reader)) {
                        walk.setRetainBody(false);
                        // Missing "wants" throw exception here
                        List<RevObject> wantsAsObjs = objectIdsToRevObjects(walk,
-                                       notAdvertisedWants);
+                                       nonTipWants);
                        List<RevCommit> wantsAsCommits = wantsAsObjs.stream()
                                        .filter(obj -> obj instanceof RevCommit)
                                        .map(obj -> (RevCommit) obj)
@@ -2046,6 +2050,10 @@ public class UploadPack {
                }
        }
 
+       private static <T> Predicate<T> not(Predicate<T> t) {
+           return t.negate();
+       }
+
        static Stream<Ref> importantRefsFirst(
                        Collection<Ref> visibleRefs) {
                Predicate<Ref> startsWithRefsHeads = ref -> ref.getName()