Skip to content

Commit ca2ea3e

Browse files
committed
Merge branch 'ab/clone-default-object-filter' into seen
"git clone" learns to pay attention to "clone.<url>.defaultObjectFilter" configuration and behave as if the "--filter=<filter-spec>" option was given on the command line. * ab/clone-default-object-filter: clone: add clone.<url>.defaultObjectFilter config
2 parents 46decc4 + acefb71 commit ca2ea3e

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

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: 54 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"
@@ -761,6 +762,51 @@ static int git_clone_config(const char *k, const char *v,
761762
return git_default_config(k, v, ctx, cb);
762763
}
763764

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

1110+
if (!filter_options.choice && !filter_options.no_filter) {
1111+
char *config_filter = get_default_object_filter(repo);
1112+
if (config_filter) {
1113+
parse_list_objects_filter(&filter_options, config_filter);
1114+
free(config_filter);
1115+
}
1116+
}
1117+
10641118
/* no need to be strict, transport_set_option() will validate it again */
10651119
if (option_depth && atoi(option_depth) < 1)
10661120
die(_("depth %s is not a positive number"), option_depth);

t/t5616-partial-clone.sh

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,132 @@ 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+
git -C default-filter-src add . &&
732+
git -C default-filter-src commit -m "initial" &&
733+
734+
git clone --bare "file://$(pwd)/default-filter-src" default-filter-srv.bare &&
735+
git -C default-filter-srv.bare config --local uploadpack.allowfilter 1 &&
736+
git -C default-filter-srv.bare config --local uploadpack.allowanysha1inwant 1
737+
'
738+
739+
test_expect_success 'clone with clone.<url>.defaultObjectFilter applies filter' '
740+
test_when_finished "rm -r default-filter-clone" &&
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+
test_when_finished "rm -r default-filter-override" &&
756+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
757+
git -c "clone.$SERVER_URL.defaultObjectFilter=blob:limit=1k" \
758+
clone --filter=blob:none "$SERVER_URL" default-filter-override &&
759+
760+
echo "blob:none" >expect &&
761+
git -C default-filter-override config --local remote.origin.partialclonefilter >actual &&
762+
test_cmp expect actual
763+
'
764+
765+
test_expect_success 'clone with clone.<url>.defaultObjectFilter=blob:none works' '
766+
test_when_finished "rm -r default-filter-blobnone" &&
767+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
768+
git -c "clone.$SERVER_URL.defaultObjectFilter=blob:none" clone \
769+
"$SERVER_URL" default-filter-blobnone &&
770+
771+
echo true >expect &&
772+
git -C default-filter-blobnone config --local remote.origin.promisor >actual &&
773+
test_cmp expect actual &&
774+
775+
echo "blob:none" >expect &&
776+
git -C default-filter-blobnone config --local remote.origin.partialclonefilter >actual &&
777+
test_cmp expect actual
778+
'
779+
780+
test_expect_success 'clone.<url>.defaultObjectFilter with tree:0 works' '
781+
test_when_finished "rm -r default-filter-tree0" &&
782+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
783+
git -c "clone.$SERVER_URL.defaultObjectFilter=tree:0" clone \
784+
"$SERVER_URL" default-filter-tree0 &&
785+
786+
echo true >expect &&
787+
git -C default-filter-tree0 config --local remote.origin.promisor >actual &&
788+
test_cmp expect actual &&
789+
790+
echo "tree:0" >expect &&
791+
git -C default-filter-tree0 config --local remote.origin.partialclonefilter >actual &&
792+
test_cmp expect actual
793+
'
794+
795+
test_expect_success 'most specific URL match wins for clone.defaultObjectFilter' '
796+
test_when_finished "rm -r default-filter-url-specific" &&
797+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
798+
git \
799+
-c "clone.file://.defaultObjectFilter=blob:limit=1k" \
800+
-c "clone.$SERVER_URL.defaultObjectFilter=blob:none" \
801+
clone "$SERVER_URL" default-filter-url-specific &&
802+
803+
echo "blob:none" >expect &&
804+
git -C default-filter-url-specific config --local remote.origin.partialclonefilter >actual &&
805+
test_cmp expect actual
806+
'
807+
808+
test_expect_success 'non-matching URL does not apply clone.defaultObjectFilter' '
809+
test_when_finished "rm -r default-filter-url-nomatch" &&
810+
git \
811+
-c "clone.https://other.example.com/.defaultObjectFilter=blob:none" \
812+
clone "file://$(pwd)/default-filter-srv.bare" default-filter-url-nomatch &&
813+
814+
test_must_fail git -C default-filter-url-nomatch config --local remote.origin.promisor
815+
'
816+
817+
test_expect_success 'bare clone.defaultObjectFilter applies to all clones' '
818+
test_when_finished "rm -r default-filter-bare-key" &&
819+
git -c clone.defaultObjectFilter=blob:none \
820+
clone "file://$(pwd)/default-filter-srv.bare" default-filter-bare-key &&
821+
822+
echo true >expect &&
823+
git -C default-filter-bare-key config --local remote.origin.promisor >actual &&
824+
test_cmp expect actual &&
825+
826+
echo "blob:none" >expect &&
827+
git -C default-filter-bare-key config --local remote.origin.partialclonefilter >actual &&
828+
test_cmp expect actual
829+
'
830+
831+
test_expect_success 'URL-specific clone.defaultObjectFilter overrides bare form' '
832+
test_when_finished "rm -r default-filter-url-over-bare" &&
833+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
834+
git \
835+
-c clone.defaultObjectFilter=blob:limit=1k \
836+
-c "clone.$SERVER_URL.defaultObjectFilter=blob:none" \
837+
clone "$SERVER_URL" default-filter-url-over-bare &&
838+
839+
echo "blob:none" >expect &&
840+
git -C default-filter-url-over-bare config --local remote.origin.partialclonefilter >actual &&
841+
test_cmp expect actual
842+
'
843+
844+
test_expect_success '--no-filter defeats clone.defaultObjectFilter' '
845+
test_when_finished "rm -r default-filter-no-filter" &&
846+
SERVER_URL="file://$(pwd)/default-filter-srv.bare" &&
847+
git -c "clone.$SERVER_URL.defaultObjectFilter=blob:none" \
848+
clone --no-filter "$SERVER_URL" default-filter-no-filter &&
849+
850+
test_must_fail git -C default-filter-no-filter config --local remote.origin.promisor
851+
'
726852

727853
. "$TEST_DIRECTORY"/lib-httpd.sh
728854
start_httpd

0 commit comments

Comments
 (0)