git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH/RFC 2/2] submodule: modify clone command to recursively shallow clone submodules

From
CMCole Minnaar <cole.minnaar@gmail.com>
Date
Sep 14, 2014, 10:38 UTC
Message-ID
<0a7689bea41b2a4714380bb7b351e92f31a64e7b.1410691049.git.cole.minnaar@gmail.com>
In-Reply-To
<ede7b63c6028591281a7eefea5e9cd45cccd0a93.1410691049.git.cole.minnaar@gmail.com>

When cloning a repository that contains submodules and specifying the `--depth` option to the 'git clone' command, the top level repository will be cloned with the specified depth, but all submodules within the repository will be cloned in their entirety.

Modified 'git clone' to pass the `--depth` option, if specified, to any submodule clone commands. Modified 'git clone' to pass the `--no-single-branch`, if specified, to any submodule clone commands.

Signed-off-by: Cole Minnaar <cole.minnaar@gmail.com>
---
 Documentation/git-clone.txt |  5 ++++-
 builtin/clone.c             | 15 +++++++++------
 2 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
index 0363d00..f2fd8c8 100644
--- a/Documentation/git-clone.txt
+++ b/Documentation/git-clone.txt
@@ -178,7 +178,8 @@ objects from the source repository into a pack in the cloned repository.
 
 --depth <depth>::
 	Create a 'shallow' clone with a history truncated to the
-	specified number of revisions.
+	specified number of revisions. If `--recursive` was also specified,
+	the depth value will be passed to all submodules within when cloning.
 
 --[no-]single-branch::
 	Clone only the history leading to the tip of a single branch,
@@ -192,6 +193,8 @@ objects from the source repository into a pack in the cloned repository.
 	initial cloning.  If the HEAD at the remote did not point at any
 	branch when `--single-branch` clone was made, no remote-tracking
 	branch is created.
+	If `--recursive` was also specified, this option will be passed
+	to all submodules when cloning.
 
 --recursive::
 --recurse-submodules::
diff --git a/builtin/clone.c b/builtin/clone.c
index dd4092b..c906d8e 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -48,6 +48,7 @@ static int option_verbosity;
 static int option_progress = -1;
 static struct string_list option_config;
 static struct string_list option_reference;
+statis struct argv_array argv_submodule_cmd = ARGV_ARRAY_INIT;
 
 static int opt_parse_reference(const struct option *opt, const char *arg, int unset)
 {
@@ -100,10 +101,6 @@ static struct option builtin_clone_options[] = {
 	OPT_END()
 };
 
-static const char *argv_submodule[] = {
-	"submodule", "update", "--init", "--recursive", NULL
-};
-
 static char *get_repo_path(const char *repo, int *is_bundle)
 {
 	static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
@@ -663,8 +660,14 @@ static int checkout(void)
 	err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
 			   sha1_to_hex(sha1), "1", NULL);
 
-	if (!err && option_recursive)
-		err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
+	if (!err && option_recursive) {
+		argv_array_pushl(&argv_submodule_cmd, "submodule", "update", "--init", "--recursive", NULL);
+		if (option_depth)
+			argv_array_pushf(&argv_submodule_cmd, "--depth=%d", atoi(option_depth));
+		if (!option_single_branch)
+			argv_array_pushl(&argv_submodule_cmd, "--no-single-branch", NULL);
+		err = run_command_v_opt(argv_submodule_cmd.argv, RUN_GIT_CMD);
+	}
 
 	return err;
 }
-- 
2.1.0.238.gce1d3a9.dirty
Previous: Cole MinnaarNext: Eric Sunshine
Message 2 of 3 in “submodule: add ability to shallowly clone any branch in a repo as a submodule”
  1. 1/2 submodule: add ability to shallowly clone any branch in a repo as a submoduleCole Minnaar, Sep 14, 2014
  2. 2/2 submodule: modify clone command to recursively shallow clone submodulesCole Minnaar, Sep 14, 2014
  3. Eric SunshineSep 14, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.