From d789fe2f4d8a6ca0c22108bed4e6e7a6e96d04dd Mon Sep 17 00:00:00 2001 From: kylezhao Date: Thu, 20 May 2021 11:29:59 +0800 Subject: [PATCH] UploadPack: use allow-any-sha1-in-want configuration C git 2.11 supports setting the equivalent of RequestPolicy.ANY with uploadpack.allowAnySHA1InWant[1]. Parse this into TransportConfig and use it from UploadPack. Add additional tests for [2] and this change. We can execute "git clone --filter=blob:none --no-checkout" successfully with config uploadPack.allowFilter is true. But when we checkout, the git will fetch other missing objects required by the checkout(this is why we need this config). When both uploadPack.allowFilter and uploadPack.allowAnySHA1InWant are true, jgit will support partial clone. If you are using an extremely large monorepo, this feature can help. It allows users to work on an incomplete repo which reduces disk usage. [1] https://github.com/git/git/commit/f8edeaa05d8623a9f6dad408237496c51101aad8 [2] change Id39771a6e42d8082099acde11249306828a053c0 Bug: 573390 Change-Id: I8fe75f03bf1fea7c11e0d67c8637bd05dd1f9b89 Signed-off-by: kylezhao --- .../jgit/transport/UploadPackTest.java | 44 +++++++++++++++++++ .../jgit/transport/TransferConfig.java | 15 ++++++- .../eclipse/jgit/transport/UploadPack.java | 4 ++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java index 4ad7fa314..0e97d7b15 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/UploadPackTest.java @@ -2726,6 +2726,50 @@ public void testNotAdvertisedWantsV2FetchRequestPolicyReachableCommit() throws E assertEquals(1, stats.getNotAdvertisedWants()); } + @Test + public void testAllowAnySha1InWantConfig() { + server.getConfig().setBoolean("uploadpack", null, "allowanysha1inwant", + true); + + try (UploadPack uploadPack = new UploadPack(server)) { + assertEquals(RequestPolicy.ANY, uploadPack.getRequestPolicy()); + } + } + + @Test + public void testAllowReachableSha1InWantConfig() { + server.getConfig().setBoolean("uploadpack", null, + "allowreachablesha1inwant", true); + + try (UploadPack uploadPack = new UploadPack(server)) { + assertEquals(RequestPolicy.REACHABLE_COMMIT, + uploadPack.getRequestPolicy()); + } + } + + @Test + public void testAllowTipSha1InWantConfig() { + server.getConfig().setBoolean("uploadpack", null, "allowtipsha1inwant", + true); + + try (UploadPack uploadPack = new UploadPack(server)) { + assertEquals(RequestPolicy.TIP, uploadPack.getRequestPolicy()); + } + } + + @Test + public void testAllowReachableTipSha1InWantConfig() { + server.getConfig().setBoolean("uploadpack", null, + "allowreachablesha1inwant", true); + server.getConfig().setBoolean("uploadpack", null, "allowtipsha1inwant", + true); + + try (UploadPack uploadPack = new UploadPack(server)) { + assertEquals(RequestPolicy.REACHABLE_COMMIT_TIP, + uploadPack.getRequestPolicy()); + } + } + private class RefCallsCountingRepository extends InMemoryRepository { private final InMemoryRepository.MemRefDatabase refdb; private int numRefCalls; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java index 805166a40..064201a62 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransferConfig.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008, 2020 Google Inc. and others + * Copyright (C) 2008, 2023 Google Inc. and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0 which is available at @@ -118,6 +118,7 @@ static ProtocolVersion parse(@Nullable String name) { private final boolean allowRefInWant; private final boolean allowTipSha1InWant; private final boolean allowReachableSha1InWant; + private final boolean allowAnySha1InWant; private final boolean allowFilter; private final boolean allowSidebandAll; @@ -202,6 +203,8 @@ public TransferConfig(Config rc) { "uploadpack", "allowtipsha1inwant", false); allowReachableSha1InWant = rc.getBoolean( "uploadpack", "allowreachablesha1inwant", false); + allowAnySha1InWant = rc.getBoolean("uploadpack", "allowanysha1inwant", + false); allowFilter = rc.getBoolean( "uploadpack", "allowfilter", false); protocolVersion = ProtocolVersion.parse(rc @@ -283,6 +286,16 @@ public boolean isAllowReachableSha1InWant() { return allowReachableSha1InWant; } + /** + * Whether to allow clients to request any SHA-1s + * + * @return allow clients to request any SHA-1s? + * @since 6.5 + */ + public boolean isAllowAnySha1InWant() { + return allowAnySha1InWant; + } + /** * @return true if clients are allowed to specify a "filter" line * @since 5.0 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 38c7cb94b..b64870647 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -681,6 +681,10 @@ public void setPackConfig(@Nullable PackConfig pc) { */ public void setTransferConfig(@Nullable TransferConfig tc) { this.transferConfig = tc != null ? tc : new TransferConfig(db); + if (transferConfig.isAllowAnySha1InWant()) { + setRequestPolicy(RequestPolicy.ANY); + return; + } if (transferConfig.isAllowTipSha1InWant()) { setRequestPolicy(transferConfig.isAllowReachableSha1InWant() ? RequestPolicy.REACHABLE_COMMIT_TIP : RequestPolicy.TIP);