git clone: Add --recursive to automatically checkout (nested) submodules
Many projects using submodules expect all submodules to be checked out in order to build/work correctly. A common command sequence for developers on such projects is: git clone url/to/project cd project git submodule update --init (--recursive) This patch introduces the --recursive option to git-clone. The new option causes git-clone to recursively clone and checkout all submodules of the cloned project. Hence, the above command sequence can be reduced to: git clone --recursive url/to/project --recursive is ignored if no checkout is done by the git-clone. The patch also includes documentation and a selftest. Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johan Herland committed
Aug 20, 2009 at 01:07 UTC
e7fed18a89fae97655687e19f13cd802d8d70845
3 files changed
+31
-2
Documentation/git-clone.txt
+9
-1
@@ -12,7 +12,7 @@ SYNOPSIS
12
'git clone' [--template=<template_directory>]
13
[-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
14
[-o <name>] [-u <upload-pack>] [--reference <repository>]
15
- [--depth <depth>] [--] <repository> [<directory>]
15
+ [--depth <depth>] [--recursive] [--] <repository> [<directory>]
16
17
DESCRIPTION
18
-----------
@@ -139,6 +139,14 @@ then the cloned repository will become corrupt.
139
with a long history, and would want to send in fixes
140
as patches.
141
142
+--recursive::
143
+ After the clone is created, initialize all submodules within,
144
+ using their default settings. This is equivalent to running
145
+ 'git submodule update --init --recursive' immediately after
146
+ the clone is finished. This option is ignored if the cloned
147
+ repository does not have a worktree/checkout (i.e. if any of
148
+ `--no-checkout`/`-n`, `--bare`, or `--mirror` is given)
149
+
150
<repository>::
151
The (possibly remote) repository to clone from. See the
152
<<URLS,URLS>> section below for more information on specifying
builtin-clone.c
+10
-1
@@ -38,7 +38,7 @@ static const char * const builtin_clone_usage[] = {
38
};
39
40
static int option_quiet, option_no_checkout, option_bare, option_mirror;
41
-static int option_local, option_no_hardlinks, option_shared;
41
+static int option_local, option_no_hardlinks, option_shared, option_recursive;
42
static char *option_template, *option_reference, *option_depth;
43
static char *option_origin = NULL;
44
static char *option_upload_pack = "git-upload-pack";
@@ -59,6 +59,8 @@ static struct option builtin_clone_options[] = {
59
"don't use local hardlinks, always copy"),
60
OPT_BOOLEAN('s', "shared", &option_shared,
61
"setup as shared repository"),
62
+ OPT_BOOLEAN(0, "recursive", &option_recursive,
63
+ "setup as shared repository"),
64
OPT_STRING(0, "template", &option_template, "path",
65
"path the template repository"),
66
OPT_STRING(0, "reference", &option_reference, "repo",
@@ -73,6 +75,10 @@ static struct option builtin_clone_options[] = {
75
OPT_END()
76
};
77
78
+static const char *argv_submodule[] = {
79
+ "submodule", "update", "--init", "--recursive", NULL
80
+};
81
+
82
static char *get_repo_path(const char *repo, int *is_bundle)
83
{
84
static char *suffix[] = { "/.git", ".git", "" };
@@ -608,6 +614,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
614
615
err |= run_hook(NULL, "post-checkout", sha1_to_hex(null_sha1),
616
sha1_to_hex(remote_head->old_sha1), "1", NULL);
617
+
618
+ if (!err && option_recursive)
619
+ err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
620
}
621
622
strbuf_release(&reflog_msg);
t/t7407-submodule-foreach.sh
+12
@@ -222,4 +222,16 @@ test_expect_success 'test "status --recursive"' '
222
test_cmp expect actual
223
'
224
225
+test_expect_success 'use "git clone --recursive" to checkout all submodules' '
226
+ git clone --recursive super clone4 &&
227
+ test -d clone4/.git &&
228
+ test -d clone4/sub1/.git &&
229
+ test -d clone4/sub2/.git &&
230
+ test -d clone4/sub3/.git &&
231
+ test -d clone4/nested1/.git &&
232
+ test -d clone4/nested1/nested2/.git &&
233
+ test -d clone4/nested1/nested2/nested3/.git &&
234
+ test -d clone4/nested1/nested2/nested3/submodule/.git
235
+'
236
+
237
test_done