Merge branch 'aj/fix-read-tree-from-scratch' into next
[git] / t / t7407-submodule-foreach.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009 Johan Herland
4 #
5
6 test_description='Test "git submodule foreach"
7
8 This test verifies that "git submodule foreach" correctly visits all submodules
9 that are currently checked out.
10 '
11
12 . ./test-lib.sh
13
14
15 test_expect_success 'setup a submodule tree' '
16         echo file > file &&
17         git add file &&
18         test_tick &&
19         git commit -m upstream
20         git clone . super &&
21         git clone super submodule &&
22         (
23                 cd super &&
24                 git submodule add ../submodule sub1 &&
25                 git submodule add ../submodule sub2 &&
26                 git submodule add ../submodule sub3 &&
27                 git config -f .gitmodules --rename-section \
28                         submodule.sub1 submodule.foo1 &&
29                 git config -f .gitmodules --rename-section \
30                         submodule.sub2 submodule.foo2 &&
31                 git config -f .gitmodules --rename-section \
32                         submodule.sub3 submodule.foo3 &&
33                 git add .gitmodules
34                 test_tick &&
35                 git commit -m "submodules" &&
36                 git submodule init sub1 &&
37                 git submodule init sub2 &&
38                 git submodule init sub3
39         ) &&
40         (
41                 cd submodule &&
42                 echo different > file &&
43                 git add file &&
44                 test_tick &&
45                 git commit -m "different"
46         ) &&
47         (
48                 cd super &&
49                 (
50                         cd sub3 &&
51                         git pull
52                 ) &&
53                 git add sub3 &&
54                 test_tick &&
55                 git commit -m "update sub3"
56         )
57 '
58
59 sub1sha1=$(cd super/sub1 && git rev-parse HEAD)
60 sub3sha1=$(cd super/sub3 && git rev-parse HEAD)
61
62 cat > expect <<EOF
63 Entering 'sub1'
64 foo1-sub1-$sub1sha1
65 Entering 'sub3'
66 foo3-sub3-$sub3sha1
67 EOF
68
69 test_expect_success 'test basic "submodule foreach" usage' '
70         git clone super clone &&
71         (
72                 cd clone &&
73                 git submodule update --init -- sub1 sub3 &&
74                 git submodule foreach "echo \$name-\$path-\$sha1" > ../actual
75         ) &&
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_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 nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD)
198 nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD)
199 nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD)
200 submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD)
201 sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD)
202 sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD)
203 sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD)
204 sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD)
205 sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD)
206
207 cat > expect <<EOF
208  $nested1sha1 nested1 (heads/master)
209  $nested2sha1 nested1/nested2 (heads/master)
210  $nested3sha1 nested1/nested2/nested3 (heads/master)
211  $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
212  $sub1sha1 sub1 ($sub1sha1_short)
213  $sub2sha1 sub2 ($sub2sha1_short)
214  $sub3sha1 sub3 (heads/master)
215 EOF
216
217 test_expect_success 'test "status --recursive"' '
218         (
219                 cd clone3 &&
220                 git submodule status --recursive > ../actual
221         ) &&
222         test_cmp expect actual
223 '
224
225 test_expect_success 'use "git clone --recursive" to checkout all submodules' '
226         git clone --recursive super clone4 &&
227         test -d clone4/.git &&
228         test -d clone4/sub1/.git &&
229         test -d clone4/sub2/.git &&
230         test -d clone4/sub3/.git &&
231         test -d clone4/nested1/.git &&
232         test -d clone4/nested1/nested2/.git &&
233         test -d clone4/nested1/nested2/nested3/.git &&
234         test -d clone4/nested1/nested2/nested3/submodule/.git
235 '
236
237 test_done