@matheusbsbs / matheus-bagliano / commits / b13fd5c1a2
Fork 0

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 <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Aug 19, 2009 at 03:45 UTC b13fd5c1a2bd450cdf7b853e0c4861f361882a18
3 files changed +36 -3
Documentation/git-submodule.txt
+5 -2
@@ -14,7 +14,7 @@ SYNOPSIS
14 'git submodule' [--quiet] status [--cached] [--] [<path>...]
15 'git submodule' [--quiet] init [--] [<path>...]
16 'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--rebase]
17 - [--reference <repository>] [--merge] [--] [<path>...]
17 + [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
18 'git submodule' [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
19 'git submodule' [--quiet] foreach [--recursive] <command>
20 'git submodule' [--quiet] sync [--] [<path>...]
@@ -122,6 +122,9 @@ update::
122 If the submodule is not yet initialized, and you just want to use the
123 setting as stored in .gitmodules, you can automatically initialize the
124 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.
128
129 summary::
130 Show commit summary between the given commit (defaults to HEAD) and
@@ -213,7 +216,7 @@ OPTIONS
216 for linkgit:git-clone[1]'s --reference and --shared options carefully.
217
218 --recursive::
216 - This option is only valid for the foreach command.
219 + This option is only valid for foreach and update commands.
220 Traverse submodules recursively. The operation is performed not
221 only in the submodules of the current repo, but also
222 in any nested submodules inside those submodules (and so on).
git-submodule.sh
+12 -1
@@ -8,7 +8,7 @@ dashless=$(basename "$0" | sed -e 's/-/ /')
8 USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> <path>
9 or: $dashless [--quiet] status [--cached] [--] [<path>...]
10 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>...]
12 or: $dashless [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
13 or: $dashless [--quiet] foreach [--recursive] <command>
14 or: $dashless [--quiet] sync [--] [<path>...]"
@@ -352,6 +352,7 @@ cmd_init()
352 cmd_update()
353 {
354 # parse $args after "submodule ... update".
355 + orig_args="$@"
356 while test $# -ne 0
357 do
358 case "$1" in
@@ -384,6 +385,10 @@ cmd_update()
385 shift
386 update="merge"
387 ;;
388 + --recursive)
389 + shift
390 + recursive=1
391 + ;;
392 --)
393 shift
394 break
@@ -470,6 +475,12 @@ cmd_update()
475 die "Unable to $action '$sha1' in submodule path '$path'"
476 say "Submodule path '$path': $msg '$sha1'"
477 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
484 done
485 }
486
t/t7407-submodule-foreach.sh
+19
@@ -175,4 +175,23 @@ test_expect_success 'test "foreach --quiet --recursive"' '
175 test_cmp expect actual
176 '
177
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 +
197 test_done