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 <kylezhao@tencent.com>
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 4ad7fa3..0e97d7b 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 805166a..064201a 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 @@ -284,6 +287,16 @@ public boolean isAllowReachableSha1InWant() { } /** + * 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 38c7cb9..b648706 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);