Skip to content

Commit b13fd5c

Browse files
jherlandgitster
authored andcommitted
git submodule update: Introduce --recursive to update nested submodules
In very large and hierarchically structured projects, one may encounter nested submodules. In these situations, it is valuable to not only update the submodules in the current repo (which is what is currently done by 'git submodule update'), but also to operate on all submodules at all levels (i.e. recursing into nested submodules as well). This patch teaches the new --recursive option to the 'git submodule update' command. The patch also includes documentation and selftests. Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15fc56a commit b13fd5c

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

Documentation/git-submodule.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SYNOPSIS
1414
'git submodule' [--quiet] status [--cached] [--] [<path>...]
1515
'git submodule' [--quiet] init [--] [<path>...]
1616
'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--rebase]
17-
[--reference <repository>] [--merge] [--] [<path>...]
17+
[--reference <repository>] [--merge] [--recursive] [--] [<path>...]
1818
'git submodule' [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
1919
'git submodule' [--quiet] foreach [--recursive] <command>
2020
'git submodule' [--quiet] sync [--] [<path>...]
@@ -122,6 +122,9 @@ update::
122122
If the submodule is not yet initialized, and you just want to use the
123123
setting as stored in .gitmodules, you can automatically initialize the
124124
submodule with the --init option.
125+
+
126+
If '--recursive' is specified, this command will recurse into the
127+
registered submodules, and update any nested submodules within.
125128

126129
summary::
127130
Show commit summary between the given commit (defaults to HEAD) and
@@ -213,7 +216,7 @@ OPTIONS
213216
for linkgit:git-clone[1]'s --reference and --shared options carefully.
214217

215218
--recursive::
216-
This option is only valid for the foreach command.
219+
This option is only valid for foreach and update commands.
217220
Traverse submodules recursively. The operation is performed not
218221
only in the submodules of the current repo, but also
219222
in any nested submodules inside those submodules (and so on).

git-submodule.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dashless=$(basename "$0" | sed -e 's/-/ /')
88
USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> <path>
99
or: $dashless [--quiet] status [--cached] [--] [<path>...]
1010
or: $dashless [--quiet] init [--] [<path>...]
11-
or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--] [<path>...]
11+
or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
1212
or: $dashless [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
1313
or: $dashless [--quiet] foreach [--recursive] <command>
1414
or: $dashless [--quiet] sync [--] [<path>...]"
@@ -352,6 +352,7 @@ cmd_init()
352352
cmd_update()
353353
{
354354
# parse $args after "submodule ... update".
355+
orig_args="$@"
355356
while test $# -ne 0
356357
do
357358
case "$1" in
@@ -384,6 +385,10 @@ cmd_update()
384385
shift
385386
update="merge"
386387
;;
388+
--recursive)
389+
shift
390+
recursive=1
391+
;;
387392
--)
388393
shift
389394
break
@@ -470,6 +475,12 @@ cmd_update()
470475
die "Unable to $action '$sha1' in submodule path '$path'"
471476
say "Submodule path '$path': $msg '$sha1'"
472477
fi
478+
479+
if test -n "$recursive"
480+
then
481+
(unset GIT_DIR; cd "$path" && cmd_update $orig_args) ||
482+
die "Failed to recurse into submodule path '$path'"
483+
fi
473484
done
474485
}
475486

t/t7407-submodule-foreach.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,23 @@ test_expect_success 'test "foreach --quiet --recursive"' '
175175
test_cmp expect actual
176176
'
177177

178+
test_expect_success 'use "update --recursive" to checkout all submodules' '
179+
git clone super clone3 &&
180+
(
181+
cd clone3 &&
182+
test ! -d sub1/.git &&
183+
test ! -d sub2/.git &&
184+
test ! -d sub3/.git &&
185+
test ! -d nested1/.git &&
186+
git submodule update --init --recursive &&
187+
test -d sub1/.git &&
188+
test -d sub2/.git &&
189+
test -d sub3/.git &&
190+
test -d nested1/.git &&
191+
test -d nested1/nested2/.git &&
192+
test -d nested1/nested2/nested3/.git &&
193+
test -d nested1/nested2/nested3/submodule/.git
194+
)
195+
'
196+
178197
test_done

0 commit comments

Comments
 (0)