Skip to content

Commit 79d2167

Browse files
abraithwaitegitster
authored andcommitted
clone: add clone.<url>.defaultObjectFilter config
Add a new configuration option that lets users specify a default partial clone filter, optionally scoped by URL pattern. When cloning a repository whose URL matches a configured pattern, git-clone automatically applies the filter, equivalent to passing --filter on the command line. [clone] defaultObjectFilter = blob:limit=1m [clone "https://github.com/"] defaultObjectFilter = blob:limit=5m [clone "https://internal.corp.com/large-project/"] defaultObjectFilter = blob:none The bare clone.defaultObjectFilter applies to all clones. The URL-qualified form clone.<url>.defaultObjectFilter restricts the setting to matching URLs. URL matching uses the existing urlmatch_config_entry() infrastructure, following the same rules as http.<url>.* — a domain, namespace, or specific project can be matched, and the most specific match wins. The config only affects the initial clone. Once the clone completes, the filter is recorded in remote.<name>.partialCloneFilter, so subsequent fetches inherit it automatically. An explicit --filter on the command line takes precedence, and --no-filter defeats the configured default entirely. Signed-off-by: Alan Braithwaite <alan@braithwaite.dev> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 795c338 commit 79d2167

3 files changed

Lines changed: 202 additions & 0 deletions

File tree

Documentation/config/clone.adoc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,37 @@ endif::[]
2121
If a partial clone filter is provided (see `--filter` in
2222
linkgit:git-rev-list[1]) and `--recurse-submodules` is used, also apply
2323
the filter to submodules.
24+
25+
`clone.defaultObjectFilter`::
26+
`clone.<url>.defaultObjectFilter`::
27+
When set to a filter spec string (e.g., `blob:limit=1m`,
28+
`blob:none`, `tree:0`), linkgit:git-clone[1] will automatically
29+
use `--filter=<value>` to enable partial clone behavior.
30+
Objects matching the filter are excluded from the initial
31+
transfer and lazily fetched on demand (e.g., during checkout).
32+
Subsequent fetches inherit the filter via the per-remote config
33+
that is written during the clone.
34+
+
35+
The bare `clone.defaultObjectFilter` applies to all clones. The
36+
URL-qualified form `clone.<url>.defaultObjectFilter` restricts the
37+
setting to clones whose URL matches `<url>`, following the same
38+
rules as `http.<url>.*` (see linkgit:git-config[1]). The most
39+
specific URL match wins. You can match a domain, a namespace, or a
40+
specific project:
41+
+
42+
----
43+
[clone]
44+
defaultObjectFilter = blob:limit=1m
45+
46+
[clone "https://github.com/"]
47+
defaultObjectFilter = blob:limit=5m
48+
49+
[clone "https://internal.corp.com/large-project/"]
50+
defaultObjectFilter = blob:none
51+
----
52+
+
53+
An explicit `--filter` option on the command line takes precedence
54+
over this config, and `--no-filter` defeats it entirely to force a
55+
full clone. Only affects the initial clone; it has no effect on
56+
later fetches into an existing repository. If the server does not
57+
support object filtering, the setting is silently ignored.

builtin/clone.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "path.h"
4545
#include "pkt-line.h"
4646
#include "list-objects-filter-options.h"
47+
#include "urlmatch.h"
4748
#include "hook.h"
4849
#include "bundle.h"
4950
#include "bundle-uri.h"
@@ -759,6 +760,47 @@ static int git_clone_config(const char *k, const char *v,
759760
return git_default_config(k, v, ctx, cb);
760761
}
761762

763+
static int clone_filter_collect(const char *var, const char *value,
764+
const struct config_context *ctx UNUSED,
765+
void *cb)
766+
{
767+
char **filter_spec_p = cb;
768+
769+
if (!strcmp(var, "clone.defaultobjectfilter")) {
770+
if (!value)
771+
return config_error_nonbool(var);
772+
free(*filter_spec_p);
773+
*filter_spec_p = xstrdup(value);
774+
}
775+
return 0;
776+
}
777+
778+
/*
779+
* Look up clone.defaultObjectFilter or clone.<url>.defaultObjectFilter
780+
* using the urlmatch infrastructure. A URL-qualified entry that matches
781+
* the clone URL takes precedence over the bare form, following the same
782+
* rules as http.<url>.* configuration variables.
783+
*/
784+
static char *get_default_object_filter(const char *url)
785+
{
786+
struct urlmatch_config config = URLMATCH_CONFIG_INIT;
787+
char *filter_spec = NULL;
788+
char *normalized_url;
789+
790+
config.section = "clone";
791+
config.key = "defaultobjectfilter";
792+
config.collect_fn = clone_filter_collect;
793+
config.cb = &filter_spec;
794+
795+
normalized_url = url_normalize(url, &config.url);
796+
797+
repo_config(the_repository, urlmatch_config_entry, &config);
798+
free(normalized_url);
799+
urlmatch_config_release(&config);
800+
801+
return filter_spec;
802+
}
803+
762804
static int write_one_config(const char *key, const char *value,
763805
const struct config_context *ctx,
764806
void *data)
@@ -1059,6 +1101,14 @@ int cmd_clone(int argc,
10591101
} else
10601102
die(_("repository '%s' does not exist"), repo_name);
10611103

1104+
if (!filter_options.choice && !filter_options.no_filter) {
1105+
char *config_filter = get_default_object_filter(repo);
1106+
if (config_filter) {
1107+
parse_list_objects_filter(&filter_options, config_filter);
1108+
free(config_filter);
1109+
}
1110+
}
1111+
10621112
/* no need to be strict, transport_set_option() will validate it again */
10631113
if (option_depth && atoi(option_depth) < 1)
10641114
die(_("depth %s is not a positive number"), option_depth);

t/t5616-partial-clone.sh

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,124 @@ test_expect_success 'after fetching descendants of non-promisor commits, gc work
723723
git -C partial gc --prune=now
724724
'
725725

726+
# Test clone.<url>.defaultObjectFilter config
727+
728+
test_expect_success 'setup for clone.defaultObjectFilter tests' '
729+
git init default-filter-src &&
730+
echo "small" >default-filter-src/small.txt &&
731+
dd if=/dev/zero of=default-filter-src/large.bin bs=1024 count=100 2>/dev/null &&
732+
git -C default-filter-src add . &&
733+
git -C default-filter-src commit -m "initial" &&
734+
735+
git clone --bare "file://$(pwd)/default-filter-src" default-filter-srv.bare &&
736+
git -C default-filter-srv.bare config --local uploadpack.allowfilter 1 &&
737+
git -C default-filter-srv.bare config --local uploadpack.allowanysha1inwant 1
738+
'
739+
740+
test_expect_success 'clone with clone.<url>.defaultObjectFilter applies filter' '
741+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
742+
git -c "clone.$SERVER_URL.defaultObjectFilter=blob:limit=1k" clone \
743+
"$SERVER_URL" default-filter-clone &&
744+
745+
echo true >expect &&
746+
git -C default-filter-clone config --local remote.origin.promisor >actual &&
747+
test_cmp expect actual &&
748+
749+
echo "blob:limit=1024" >expect &&
750+
git -C default-filter-clone config --local remote.origin.partialclonefilter >actual &&
751+
test_cmp expect actual
752+
'
753+
754+
test_expect_success 'clone with --filter overrides clone.<url>.defaultObjectFilter' '
755+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
756+
git -c "clone.$SERVER_URL.defaultObjectFilter=blob:limit=1k" \
757+
clone --filter=blob:none "$SERVER_URL" default-filter-override &&
758+
759+
echo "blob:none" >expect &&
760+
git -C default-filter-override config --local remote.origin.partialclonefilter >actual &&
761+
test_cmp expect actual
762+
'
763+
764+
test_expect_success 'clone with clone.<url>.defaultObjectFilter=blob:none works' '
765+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
766+
git -c "clone.$SERVER_URL.defaultObjectFilter=blob:none" clone \
767+
"$SERVER_URL" default-filter-blobnone &&
768+
769+
echo true >expect &&
770+
git -C default-filter-blobnone config --local remote.origin.promisor >actual &&
771+
test_cmp expect actual &&
772+
773+
echo "blob:none" >expect &&
774+
git -C default-filter-blobnone config --local remote.origin.partialclonefilter >actual &&
775+
test_cmp expect actual
776+
'
777+
778+
test_expect_success 'clone.<url>.defaultObjectFilter with tree:0 works' '
779+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
780+
git -c "clone.$SERVER_URL.defaultObjectFilter=tree:0" clone \
781+
"$SERVER_URL" default-filter-tree0 &&
782+
783+
echo true >expect &&
784+
git -C default-filter-tree0 config --local remote.origin.promisor >actual &&
785+
test_cmp expect actual &&
786+
787+
echo "tree:0" >expect &&
788+
git -C default-filter-tree0 config --local remote.origin.partialclonefilter >actual &&
789+
test_cmp expect actual
790+
'
791+
792+
test_expect_success 'most specific URL match wins for clone.defaultObjectFilter' '
793+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
794+
git \
795+
-c "clone.file://.defaultObjectFilter=blob:limit=1k" \
796+
-c "clone.$SERVER_URL.defaultObjectFilter=blob:none" \
797+
clone "$SERVER_URL" default-filter-url-specific &&
798+
799+
echo "blob:none" >expect &&
800+
git -C default-filter-url-specific config --local remote.origin.partialclonefilter >actual &&
801+
test_cmp expect actual
802+
'
803+
804+
test_expect_success 'non-matching URL does not apply clone.defaultObjectFilter' '
805+
git \
806+
-c "clone.https://other.example.com/.defaultObjectFilter=blob:none" \
807+
clone "file://$(pwd)/default-filter-srv.bare" default-filter-url-nomatch &&
808+
809+
test_must_fail git -C default-filter-url-nomatch config --local remote.origin.promisor
810+
'
811+
812+
test_expect_success 'bare clone.defaultObjectFilter applies to all clones' '
813+
git -c clone.defaultObjectFilter=blob:none \
814+
clone "file://$(pwd)/default-filter-srv.bare" default-filter-bare-key &&
815+
816+
echo true >expect &&
817+
git -C default-filter-bare-key config --local remote.origin.promisor >actual &&
818+
test_cmp expect actual &&
819+
820+
echo "blob:none" >expect &&
821+
git -C default-filter-bare-key config --local remote.origin.partialclonefilter >actual &&
822+
test_cmp expect actual
823+
'
824+
825+
test_expect_success 'URL-specific clone.defaultObjectFilter overrides bare form' '
826+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
827+
git \
828+
-c clone.defaultObjectFilter=blob:limit=1k \
829+
-c "clone.$SERVER_URL.defaultObjectFilter=blob:none" \
830+
clone "$SERVER_URL" default-filter-url-over-bare &&
831+
832+
echo "blob:none" >expect &&
833+
git -C default-filter-url-over-bare config --local remote.origin.partialclonefilter >actual &&
834+
test_cmp expect actual
835+
'
836+
837+
test_expect_success '--no-filter defeats clone.defaultObjectFilter' '
838+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
839+
git -c "clone.$SERVER_URL.defaultObjectFilter=blob:none" \
840+
clone --no-filter "$SERVER_URL" default-filter-no-filter &&
841+
842+
test_must_fail git -C default-filter-no-filter config --local remote.origin.promisor
843+
'
726844

727845
. "$TEST_DIRECTORY"/lib-httpd.sh
728846
start_httpd

0 commit comments

Comments
 (0)