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] f8edeaa05d
[2] change Id39771a6e42d8082099acde11249306828a053c0

Bug: 573390
Change-Id: I8fe75f03bf1fea7c11e0d67c8637bd05dd1f9b89
Signed-off-by: kylezhao <kylezhao@tencent.com>
This commit is contained in:
kylezhao 2021-05-20 11:29:59 +08:00 committed by Matthias Sohn
parent 5d5a0d5376
commit d789fe2f4d
3 changed files with 62 additions and 1 deletions

View File

@ -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;

View File

@ -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

View File

@ -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);