Skip to contentSkip to conversation

submodule: plumb --filter to cloned submodules

PATCH In review Josh Steadmon 2 participants 2 messages Jul 17, 2019 – Jul 24, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 submodule: plumb --filter to cloned submodules

When cloning a repo with a --filter and with --recurse-submodules enabled, the partial clone filter only applies to the top-level repo. This can lead to unexpected bandwidth and disk usage for projects which include large submodules.

Fix this by plumbing the --filter argument from git-clone through git-submodule and git-submodule--helper.

---
 builtin/clone.c                    |  9 +++++++
 builtin/submodule--helper.c        | 41 +++++++++++++++++++++++++-----
 git-submodule.sh                   | 17 ++++++++++++-
 t/t5617-clone-submodules-remote.sh | 19 ++++++++++++++
 4 files changed, 79 insertions(+), 7 deletions(-)
builtin/clone.c +9 -0
index a4fe72879d..2e7ecbd019 100644
@@ -798,6 +798,15 @@ static int checkout(int submodule_progress)
 			argv_array_push(&args, "--no-fetch");
 		}
 
+		if (filter_options.choice) {
+			struct strbuf expanded_filter = STRBUF_INIT;
+			expand_list_objects_filter_spec(&filter_options,
+							&expanded_filter);
+			argv_array_pushf(&args, "--filter=%s",
+					 expanded_filter.buf);
+			strbuf_release(&expanded_filter);
+		}
+
 		err = run_command_v_opt(args.argv, RUN_GIT_CMD);
 		argv_array_clear(&args);
 	}
builtin/submodule--helper.c +35 -6

139 lines not on this page · raw message

git-submodule.sh +16 -1
index c7f58c5756..64c5bdaacc 100755
@@ -10,7 +10,7 @@ USAGE="[--quiet] [--cached]
    or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
    or: $dashless [--quiet] init [--] [<path>...]
    or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
-   or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
+   or: $dashless [--quiet] update [--init [--filter=<filter-spec>]] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
    or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
    or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
    or: $dashless [--quiet] foreach [--recursive] <command>
@@ -45,6 +45,7 @@ custom_name=
 depth=
 progress=
 dissociate=
+filter=
 
 die_if_unmatched ()
 {
@@ -520,6 +521,14 @@ cmd_update()
 		--jobs=*)
 			jobs=$1
 			;;
+		--filter)
+			case "$2" in '') usage ;; esac
+			filter="--filter=$2"
+			shift
+			;;
+		--filter=*)
+			filter=$1
+			;;
 		--)
 			shift
 			break
@@ -534,6 +543,11 @@ cmd_update()
 		shift
 	done
 
+	if test -n "$filter" && test "$init" != "1"
+	then
+		usage
+	fi
+
 	if test -n "$init"
 	then
 		cmd_init "--" "$@" || return
@@ -550,6 +564,7 @@ cmd_update()
 		${depth:+--depth "$depth"} \
 		$recommend_shallow \
 		$jobs \
+		$filter \
 		-- \
 		"$@" || echo "#unmatched" $?
 	} | {
t/t5617-clone-submodules-remote.sh +19 -0
index 37fcce9c40..49448e5a88 100755
@@ -24,6 +24,13 @@ test_expect_success 'setup' '
 	)
 '
 
+# bare clone giving "srv.bare" for use as our server.
+test_expect_success 'setup bare clone for server' '
+	git clone --bare "file://$(pwd)/." srv.bare &&
+	git -C srv.bare config --local uploadpack.allowfilter 1 &&
+	git -C srv.bare config --local uploadpack.allowanysha1inwant 1
+'
+
 test_expect_success 'clone with --no-remote-submodules' '
 	test_when_finished "rm -rf super_clone" &&
 	git clone --recurse-submodules --no-remote-submodules "file://$pwd/." super_clone &&
@@ -51,4 +58,16 @@ test_expect_success 'check the default is --no-remote-submodules' '
 	)
 '
 
+# do basic partial clone from "srv.bare"
+# confirm partial clone was registered in the local config for super and sub.
+test_expect_success 'clone with --filter' '
+	git clone --recurse-submodules --filter blob:none "file://$pwd/srv.bare" super_clone &&
+	test "$(git -C super_clone config --local core.repositoryformatversion)" = "1" &&
+	test "$(git -C super_clone config --local extensions.partialclone)" = "origin" &&
+	test "$(git -C super_clone config --local core.partialclonefilter)" = "blob:none" &&
+	test "$(git -C super_clone/sub config --local core.repositoryformatversion)" = "1" &&
+	test "$(git -C super_clone/sub config --local extensions.partialclone)" = "origin" &&
+	test "$(git -C super_clone/sub config --local core.partialclonefilter)" = "blob:none"
+'
+
 test_done
Signature · 1 line
2.22.0.657.g960e92d24f-goog
#1 lore raw
Quoted 9 lines

When cloning a repo with a --filter and with --recurse-submodules enabled, the partial clone filter only applies to the top-level repo. This can lead to unexpected bandwidth and disk usage for projects which include large submodules.

Fix this by plumbing the --filter argument from git-clone through git-submodule and git-submodule--helper.

Sorry for not catching this earlier. The cloning itself might work, but I don't think we support partial-clone submodules right now. In particular, anything that operates on submodules in-process like grep (which either adds submodule objects as alternates or calls the object-loading functions using a different struct repo) will not work, because we only support lazy loading on the_repository. So if we want this feature, there is some other work that we will need to do first.