@matheusbsbs / matheus-bagliano / commits / 15fc56a853
Fork 0

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

Johan Herland committed Aug 19, 2009 at 03:45 UTC 15fc56a853648c60697df691c5cd8a11ad718611
3 files changed +124 -4
Documentation/git-submodule.txt
+9 -1
@@ -16,7 +16,7 @@ SYNOPSIS
16 'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--rebase]
17 [--reference <repository>] [--merge] [--] [<path>...]
18 'git submodule' [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
19 -'git submodule' [--quiet] foreach <command>
19 +'git submodule' [--quiet] foreach [--recursive] <command>
20 'git submodule' [--quiet] sync [--] [<path>...]
21
22
@@ -138,6 +138,8 @@ foreach::
138 Any submodules defined in the superproject but not checked out are
139 ignored by this command. Unless given --quiet, foreach prints the name
140 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).
143 A non-zero return from the command in any submodule causes
144 the processing to terminate. This can be overridden by adding '|| :'
145 to the end of the command.
@@ -210,6 +212,12 @@ OPTIONS
212 *NOTE*: Do *not* use this option unless you have read the note
213 for linkgit:git-clone[1]'s --reference and --shared options carefully.
214
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 +
221 <path>...::
222 Paths to submodule(s). When specified this will restrict the command
223 to only operate on the submodules found at the specified paths.
git-submodule.sh
+16 -3
@@ -10,7 +10,7 @@ USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> <p
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--] [<path>...]
12 or: $dashless [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
13 - or: $dashless [--quiet] foreach <command>
13 + or: $dashless [--quiet] foreach [--recursive] <command>
14 or: $dashless [--quiet] sync [--] [<path>...]"
15 OPTIONS_SPEC=
16 . git-sh-setup
@@ -23,6 +23,7 @@ reference=
23 cached=
24 nofetch=
25 update=
26 +prefix=
27
28 # Resolve relative url by appending to parent's url
29 resolve_relative_url ()
@@ -249,6 +250,9 @@ cmd_foreach()
250 -q|--quiet)
251 GIT_QUIET=1
252 ;;
253 + --recursive)
254 + recursive=1
255 + ;;
256 -*)
257 usage
258 ;;
@@ -264,9 +268,18 @@ cmd_foreach()
268 do
269 if test -e "$path"/.git
270 then
267 - say "Entering '$path'"
271 + say "Entering '$prefix$path'"
272 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 + ) ||
283 die "Stopping at '$path'; script returned non-zero status."
284 fi
285 done
t/t7407-submodule-foreach.sh
+99
@@ -76,4 +76,103 @@ test_expect_success 'test basic "submodule foreach" usage' '
76 test_cmp expect actual
77 '
78
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 +
178 test_done