Skip to content

Commit 15fc56a

Browse files
jherlandgitster
authored andcommitted
git submodule foreach: Add --recursive to recurse into nested submodules
In very large and hierarchically structured projects, one may encounter nested submodules. In these situations, it is valuable to not only operate on all the submodules in the current repo (which is what is currently done by 'git submodule foreach'), 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 foreach' 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 9aec7e0 commit 15fc56a

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

Documentation/git-submodule.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SYNOPSIS
1616
'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--rebase]
1717
[--reference <repository>] [--merge] [--] [<path>...]
1818
'git submodule' [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
19-
'git submodule' [--quiet] foreach <command>
19+
'git submodule' [--quiet] foreach [--recursive] <command>
2020
'git submodule' [--quiet] sync [--] [<path>...]
2121

2222

@@ -138,6 +138,8 @@ foreach::
138138
Any submodules defined in the superproject but not checked out are
139139
ignored by this command. Unless given --quiet, foreach prints the name
140140
of each submodule before evaluating the command.
141+
If --recursive is given, submodules are traversed recursively (i.e.
142+
the given shell command is evaluated in nested submodules as well).
141143
A non-zero return from the command in any submodule causes
142144
the processing to terminate. This can be overridden by adding '|| :'
143145
to the end of the command.
@@ -210,6 +212,12 @@ OPTIONS
210212
*NOTE*: Do *not* use this option unless you have read the note
211213
for linkgit:git-clone[1]'s --reference and --shared options carefully.
212214

215+
--recursive::
216+
This option is only valid for the foreach command.
217+
Traverse submodules recursively. The operation is performed not
218+
only in the submodules of the current repo, but also
219+
in any nested submodules inside those submodules (and so on).
220+
213221
<path>...::
214222
Paths to submodule(s). When specified this will restrict the command
215223
to only operate on the submodules found at the specified paths.

git-submodule.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> <p
1010
or: $dashless [--quiet] init [--] [<path>...]
1111
or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--] [<path>...]
1212
or: $dashless [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
13-
or: $dashless [--quiet] foreach <command>
13+
or: $dashless [--quiet] foreach [--recursive] <command>
1414
or: $dashless [--quiet] sync [--] [<path>...]"
1515
OPTIONS_SPEC=
1616
. git-sh-setup
@@ -23,6 +23,7 @@ reference=
2323
cached=
2424
nofetch=
2525
update=
26+
prefix=
2627

2728
# Resolve relative url by appending to parent's url
2829
resolve_relative_url ()
@@ -249,6 +250,9 @@ cmd_foreach()
249250
-q|--quiet)
250251
GIT_QUIET=1
251252
;;
253+
--recursive)
254+
recursive=1
255+
;;
252256
-*)
253257
usage
254258
;;
@@ -264,9 +268,18 @@ cmd_foreach()
264268
do
265269
if test -e "$path"/.git
266270
then
267-
say "Entering '$path'"
271+
say "Entering '$prefix$path'"
268272
name=$(module_name "$path")
269-
(cd "$path" && eval "$@") ||
273+
(
274+
prefix="$prefix$path/"
275+
unset GIT_DIR
276+
cd "$path" &&
277+
eval "$@" &&
278+
if test -n "$recursive"
279+
then
280+
cmd_foreach "--recursive" "$@"
281+
fi
282+
) ||
270283
die "Stopping at '$path'; script returned non-zero status."
271284
fi
272285
done

t/t7407-submodule-foreach.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,103 @@ test_expect_success 'test basic "submodule foreach" usage' '
7676
test_cmp expect actual
7777
'
7878

79+
test_expect_success 'setup nested submodules' '
80+
git clone submodule nested1 &&
81+
git clone submodule nested2 &&
82+
git clone submodule nested3 &&
83+
(
84+
cd nested3 &&
85+
git submodule add ../submodule submodule &&
86+
test_tick &&
87+
git commit -m "submodule" &&
88+
git submodule init submodule
89+
) &&
90+
(
91+
cd nested2 &&
92+
git submodule add ../nested3 nested3 &&
93+
test_tick &&
94+
git commit -m "nested3" &&
95+
git submodule init nested3
96+
) &&
97+
(
98+
cd nested1 &&
99+
git submodule add ../nested2 nested2 &&
100+
test_tick &&
101+
git commit -m "nested2" &&
102+
git submodule init nested2
103+
) &&
104+
(
105+
cd super &&
106+
git submodule add ../nested1 nested1 &&
107+
test_tick &&
108+
git commit -m "nested1" &&
109+
git submodule init nested1
110+
)
111+
'
112+
113+
test_expect_success 'use "submodule foreach" to checkout 2nd level submodule' '
114+
git clone super clone2 &&
115+
(
116+
cd clone2 &&
117+
test ! -d sub1/.git &&
118+
test ! -d sub2/.git &&
119+
test ! -d sub3/.git &&
120+
test ! -d nested1/.git &&
121+
git submodule update --init &&
122+
test -d sub1/.git &&
123+
test -d sub2/.git &&
124+
test -d sub3/.git &&
125+
test -d nested1/.git &&
126+
test ! -d nested1/nested2/.git &&
127+
git submodule foreach "git submodule update --init" &&
128+
test -d nested1/nested2/.git &&
129+
test ! -d nested1/nested2/nested3/.git
130+
)
131+
'
132+
133+
test_expect_success 'use "foreach --recursive" to checkout all submodules' '
134+
(
135+
cd clone2 &&
136+
git submodule foreach --recursive "git submodule update --init" &&
137+
test -d nested1/nested2/nested3/.git &&
138+
test -d nested1/nested2/nested3/submodule/.git
139+
)
140+
'
141+
142+
cat > expect <<EOF
143+
Entering 'nested1'
144+
Entering 'nested1/nested2'
145+
Entering 'nested1/nested2/nested3'
146+
Entering 'nested1/nested2/nested3/submodule'
147+
Entering 'sub1'
148+
Entering 'sub2'
149+
Entering 'sub3'
150+
EOF
151+
152+
test_expect_success 'test messages from "foreach --recursive"' '
153+
(
154+
cd clone2 &&
155+
git submodule foreach --recursive "true" > ../actual
156+
) &&
157+
test_cmp expect actual
158+
'
159+
160+
cat > expect <<EOF
161+
nested1-nested1
162+
nested2-nested2
163+
nested3-nested3
164+
submodule-submodule
165+
foo1-sub1
166+
foo2-sub2
167+
foo3-sub3
168+
EOF
169+
170+
test_expect_success 'test "foreach --quiet --recursive"' '
171+
(
172+
cd clone2 &&
173+
git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
174+
) &&
175+
test_cmp expect actual
176+
'
177+
79178
test_done

0 commit comments

Comments
 (0)