Merge branch 'master' of git://repo.or.cz/alt-git
[git/mingw.git] / t / t7407-submodule-foreach.sh
blob7792cb0b378a175d31df7d61865358c8343ebc19
1 #!/bin/sh
3 # Copyright (c) 2009 Johan Herland
6 test_description='Test "git submodule foreach"
8 This test verifies that "git submodule foreach" correctly visits all submodules
9 that are currently checked out.
12 . ./test-lib.sh
14 case $(uname -s) in
15 *MINGW*) GIT_TEST_CMP="diff -uw";;
16 esac
19 test_expect_success 'setup a submodule tree' '
20 echo file > file &&
21 git add file &&
22 test_tick &&
23 git commit -m upstream &&
24 git clone . super &&
25 git clone super submodule &&
27 cd super &&
28 git submodule add ../submodule sub1 &&
29 git submodule add ../submodule sub2 &&
30 git submodule add ../submodule sub3 &&
31 git config -f .gitmodules --rename-section \
32 submodule.sub1 submodule.foo1 &&
33 git config -f .gitmodules --rename-section \
34 submodule.sub2 submodule.foo2 &&
35 git config -f .gitmodules --rename-section \
36 submodule.sub3 submodule.foo3 &&
37 git add .gitmodules &&
38 test_tick &&
39 git commit -m "submodules" &&
40 git submodule init sub1 &&
41 git submodule init sub2 &&
42 git submodule init sub3
43 ) &&
45 cd submodule &&
46 echo different > file &&
47 git add file &&
48 test_tick &&
49 git commit -m "different"
50 ) &&
52 cd super &&
54 cd sub3 &&
55 git pull
56 ) &&
57 git add sub3 &&
58 test_tick &&
59 git commit -m "update sub3"
63 sub1sha1=$(cd super/sub1 && git rev-parse HEAD)
64 sub3sha1=$(cd super/sub3 && git rev-parse HEAD)
66 pwd=$(pwd)
68 cat > expect <<EOF
69 Entering 'sub1'
70 $pwd/clone-foo1-sub1-$sub1sha1
71 Entering 'sub3'
72 $pwd/clone-foo3-sub3-$sub3sha1
73 EOF
75 test_expect_success 'test basic "submodule foreach" usage' '
76 git clone super clone &&
78 cd clone &&
79 git submodule update --init -- sub1 sub3 &&
80 git submodule foreach "echo \$toplevel-\$name-\$path-\$sha1" > ../actual &&
81 git config foo.bar zar &&
82 git submodule foreach "git config --file \"\$toplevel/.git/config\" foo.bar"
83 ) &&
84 test_i18ncmp expect actual
87 test_expect_success 'setup nested submodules' '
88 git clone submodule nested1 &&
89 git clone submodule nested2 &&
90 git clone submodule nested3 &&
92 cd nested3 &&
93 git submodule add ../submodule submodule &&
94 test_tick &&
95 git commit -m "submodule" &&
96 git submodule init submodule
97 ) &&
99 cd nested2 &&
100 git submodule add ../nested3 nested3 &&
101 test_tick &&
102 git commit -m "nested3" &&
103 git submodule init nested3
104 ) &&
106 cd nested1 &&
107 git submodule add ../nested2 nested2 &&
108 test_tick &&
109 git commit -m "nested2" &&
110 git submodule init nested2
111 ) &&
113 cd super &&
114 git submodule add ../nested1 nested1 &&
115 test_tick &&
116 git commit -m "nested1" &&
117 git submodule init nested1
121 test_expect_success 'use "submodule foreach" to checkout 2nd level submodule' '
122 git clone super clone2 &&
124 cd clone2 &&
125 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
126 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
127 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
128 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
129 git submodule update --init &&
130 git rev-parse --resolve-git-dir sub1/.git &&
131 git rev-parse --resolve-git-dir sub2/.git &&
132 git rev-parse --resolve-git-dir sub3/.git &&
133 git rev-parse --resolve-git-dir nested1/.git &&
134 test_must_fail git rev-parse --resolve-git-dir nested1/nested2/.git &&
135 git submodule foreach "git submodule update --init" &&
136 git rev-parse --resolve-git-dir nested1/nested1/nested2/.git
137 test_must_fail git rev-parse --resolve-git-dir nested1/nested2/nested3/.git
141 test_expect_success 'use "foreach --recursive" to checkout all submodules' '
143 cd clone2 &&
144 git submodule foreach --recursive "git submodule update --init" &&
145 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
146 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
150 cat > expect <<EOF
151 Entering 'nested1'
152 Entering 'nested1/nested2'
153 Entering 'nested1/nested2/nested3'
154 Entering 'nested1/nested2/nested3/submodule'
155 Entering 'sub1'
156 Entering 'sub2'
157 Entering 'sub3'
160 test_expect_success 'test messages from "foreach --recursive"' '
162 cd clone2 &&
163 git submodule foreach --recursive "true" > ../actual
164 ) &&
165 test_i18ncmp expect actual
168 cat > expect <<EOF
169 nested1-nested1
170 nested2-nested2
171 nested3-nested3
172 submodule-submodule
173 foo1-sub1
174 foo2-sub2
175 foo3-sub3
178 test_expect_success 'test "foreach --quiet --recursive"' '
180 cd clone2 &&
181 git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
182 ) &&
183 test_cmp expect actual
186 test_expect_success 'use "update --recursive" to checkout all submodules' '
187 git clone super clone3 &&
189 cd clone3 &&
190 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
191 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
192 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
193 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
194 git submodule update --init --recursive &&
195 git rev-parse --resolve-git-dir sub1/.git &&
196 git rev-parse --resolve-git-dir sub2/.git &&
197 git rev-parse --resolve-git-dir sub3/.git &&
198 git rev-parse --resolve-git-dir nested1/.git &&
199 git rev-parse --resolve-git-dir nested1/nested2/.git &&
200 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
201 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
205 nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD)
206 nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD)
207 nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD)
208 submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD)
209 sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD)
210 sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD)
211 sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD)
212 sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD)
213 sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD)
215 cat > expect <<EOF
216 $nested1sha1 nested1 (heads/master)
217 $nested2sha1 nested1/nested2 (heads/master)
218 $nested3sha1 nested1/nested2/nested3 (heads/master)
219 $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
220 $sub1sha1 sub1 ($sub1sha1_short)
221 $sub2sha1 sub2 ($sub2sha1_short)
222 $sub3sha1 sub3 (heads/master)
225 test_expect_success 'test "status --recursive"' '
227 cd clone3 &&
228 git submodule status --recursive > ../actual
229 ) &&
230 test_cmp expect actual
233 sed -e "/nested2 /s/.*/+$nested2sha1 nested1\/nested2 (file2~1)/;/sub[1-3]/d" < expect > expect2
234 mv -f expect2 expect
236 test_expect_success 'ensure "status --cached --recursive" preserves the --cached flag' '
238 cd clone3 &&
240 cd nested1/nested2 &&
241 test_commit file2
242 ) &&
243 git submodule status --cached --recursive -- nested1 > ../actual
244 ) &&
245 if test_have_prereq MINGW
246 then
247 dos2unix actual
248 fi &&
249 test_cmp expect actual
252 test_expect_success 'use "git clone --recursive" to checkout all submodules' '
253 git clone --recursive super clone4 &&
255 cd clone4 &&
256 git rev-parse --resolve-git-dir .git &&
257 git rev-parse --resolve-git-dir sub1/.git &&
258 git rev-parse --resolve-git-dir sub2/.git &&
259 git rev-parse --resolve-git-dir sub3/.git &&
260 git rev-parse --resolve-git-dir nested1/.git &&
261 git rev-parse --resolve-git-dir nested1/nested2/.git &&
262 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
263 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
267 test_expect_success 'test "update --recursive" with a flag with spaces' '
268 git clone super "common objects" &&
269 git clone super clone5 &&
271 cd clone5 &&
272 test_must_fail git rev-parse --resolve-git-dir d nested1/.git &&
273 git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" &&
274 git rev-parse --resolve-git-dir nested1/.git &&
275 git rev-parse --resolve-git-dir nested1/nested2/.git &&
276 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
277 test -f .git/modules/nested1/objects/info/alternates &&
278 test -f .git/modules/nested1/modules/nested2/objects/info/alternates &&
279 test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates
283 test_expect_success 'use "update --recursive nested1" to checkout all submodules rooted in nested1' '
284 git clone super clone6 &&
286 cd clone6 &&
287 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
288 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
289 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
290 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
291 git submodule update --init --recursive -- nested1 &&
292 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
293 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
294 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
295 git rev-parse --resolve-git-dir nested1/.git &&
296 git rev-parse --resolve-git-dir nested1/nested2/.git &&
297 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
298 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
302 test_expect_success 'command passed to foreach retains notion of stdin' '
304 cd super &&
305 git submodule foreach echo success >../expected &&
306 yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
307 ) &&
308 test_cmp expected actual
311 test_expect_success 'command passed to foreach --recursive retains notion of stdin' '
313 cd clone2 &&
314 git submodule foreach --recursive echo success >../expected &&
315 yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
316 ) &&
317 test_cmp expected actual
320 test_done