Merge branch 'jc/calloc-fix'
[git] / t / t7403-submodule-sync.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2008 David Aguilar
4 #
5
6 test_description='git submodule sync
7
8 These tests exercise the "git submodule sync" subcommand.
9 '
10
11 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
12 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13
14 . ./test-lib.sh
15
16 test_expect_success setup '
17         echo file >file &&
18         git add file &&
19         test_tick &&
20         git commit -m upstream &&
21         git clone . super &&
22         git clone super submodule &&
23         (
24                 cd submodule &&
25                 git submodule add ../submodule sub-submodule &&
26                 test_tick &&
27                 git commit -m "sub-submodule"
28         ) &&
29         (
30                 cd super &&
31                 git submodule add ../submodule submodule &&
32                 test_tick &&
33                 git commit -m "submodule"
34         ) &&
35         git clone super super-clone &&
36         (
37                 cd super-clone &&
38                 git submodule update --init --recursive
39         ) &&
40         git clone super empty-clone &&
41         (
42                 cd empty-clone &&
43                 git submodule init
44         ) &&
45         git clone super top-only-clone &&
46         git clone super relative-clone &&
47         (
48                 cd relative-clone &&
49                 git submodule update --init --recursive
50         ) &&
51         git clone super recursive-clone &&
52         (
53                 cd recursive-clone &&
54                 git submodule update --init --recursive
55         )
56 '
57
58 test_expect_success 'change submodule' '
59         (
60                 cd submodule &&
61                 echo second line >>file &&
62                 test_tick &&
63                 git commit -a -m "change submodule"
64         )
65 '
66
67 reset_submodule_urls () {
68         (
69                 root=$(pwd) &&
70                 cd super-clone/submodule &&
71                 git config remote.origin.url "$root/submodule"
72         ) &&
73         (
74                 root=$(pwd) &&
75                 cd super-clone/submodule/sub-submodule &&
76                 git config remote.origin.url "$root/submodule"
77         )
78 }
79
80 test_expect_success 'change submodule url' '
81         (
82                 cd super &&
83                 cd submodule &&
84                 git checkout main &&
85                 git pull
86         ) &&
87         mv submodule moved-submodule &&
88         (
89                 cd moved-submodule &&
90                 git config -f .gitmodules submodule.sub-submodule.url ../moved-submodule &&
91                 test_tick &&
92                 git commit -a -m moved-sub-submodule
93         ) &&
94         (
95                 cd super &&
96                 git config -f .gitmodules submodule.submodule.url ../moved-submodule &&
97                 test_tick &&
98                 git commit -a -m moved-submodule
99         )
100 '
101
102 test_expect_success '"git submodule sync" should update submodule URLs' '
103         (
104                 cd super-clone &&
105                 git pull --no-recurse-submodules &&
106                 git submodule sync
107         ) &&
108         test -d "$(
109                 cd super-clone/submodule &&
110                 git config remote.origin.url
111         )" &&
112         test ! -d "$(
113                 cd super-clone/submodule/sub-submodule &&
114                 git config remote.origin.url
115         )" &&
116         (
117                 cd super-clone/submodule &&
118                 git checkout main &&
119                 git pull
120         ) &&
121         (
122                 cd super-clone &&
123                 test -d "$(git config submodule.submodule.url)"
124         )
125 '
126
127 test_expect_success '"git submodule sync --recursive" should update all submodule URLs' '
128         (
129                 cd super-clone &&
130                 (
131                         cd submodule &&
132                         git pull --no-recurse-submodules
133                 ) &&
134                 git submodule sync --recursive
135         ) &&
136         test -d "$(
137                 cd super-clone/submodule &&
138                 git config remote.origin.url
139         )" &&
140         test -d "$(
141                 cd super-clone/submodule/sub-submodule &&
142                 git config remote.origin.url
143         )" &&
144         (
145                 cd super-clone/submodule/sub-submodule &&
146                 git checkout main &&
147                 git pull
148         )
149 '
150
151 test_expect_success 'reset submodule URLs' '
152         reset_submodule_urls super-clone
153 '
154
155 test_expect_success '"git submodule sync" should update submodule URLs - subdirectory' '
156         (
157                 cd super-clone &&
158                 git pull --no-recurse-submodules &&
159                 mkdir -p sub &&
160                 cd sub &&
161                 git submodule sync >../../output
162         ) &&
163         test_i18ngrep "\\.\\./submodule" output &&
164         test -d "$(
165                 cd super-clone/submodule &&
166                 git config remote.origin.url
167         )" &&
168         test ! -d "$(
169                 cd super-clone/submodule/sub-submodule &&
170                 git config remote.origin.url
171         )" &&
172         (
173                 cd super-clone/submodule &&
174                 git checkout main &&
175                 git pull
176         ) &&
177         (
178                 cd super-clone &&
179                 test -d "$(git config submodule.submodule.url)"
180         )
181 '
182
183 test_expect_success '"git submodule sync --recursive" should update all submodule URLs - subdirectory' '
184         (
185                 cd super-clone &&
186                 (
187                         cd submodule &&
188                         git pull --no-recurse-submodules
189                 ) &&
190                 mkdir -p sub &&
191                 cd sub &&
192                 git submodule sync --recursive >../../output
193         ) &&
194         test_i18ngrep "\\.\\./submodule/sub-submodule" output &&
195         test -d "$(
196                 cd super-clone/submodule &&
197                 git config remote.origin.url
198         )" &&
199         test -d "$(
200                 cd super-clone/submodule/sub-submodule &&
201                 git config remote.origin.url
202         )" &&
203         (
204                 cd super-clone/submodule/sub-submodule &&
205                 git checkout main &&
206                 git pull
207         )
208 '
209
210 test_expect_success '"git submodule sync" should update known submodule URLs' '
211         (
212                 cd empty-clone &&
213                 git pull &&
214                 git submodule sync &&
215                 test -d "$(git config submodule.submodule.url)"
216         )
217 '
218
219 test_expect_success '"git submodule sync" should not vivify uninteresting submodule' '
220         (
221                 cd top-only-clone &&
222                 git pull &&
223                 git submodule sync &&
224                 test -z "$(git config submodule.submodule.url)" &&
225                 git submodule sync submodule &&
226                 test -z "$(git config submodule.submodule.url)"
227         )
228 '
229
230 test_expect_success '"git submodule sync" handles origin URL of the form foo' '
231         (
232                 cd relative-clone &&
233                 git remote set-url origin foo &&
234                 git submodule sync &&
235                 (
236                         cd submodule &&
237                         #actual fails with: "cannot strip off url foo
238                         test "$(git config remote.origin.url)" = "../submodule"
239                 )
240         )
241 '
242
243 test_expect_success '"git submodule sync" handles origin URL of the form foo/bar' '
244         (
245                 cd relative-clone &&
246                 git remote set-url origin foo/bar &&
247                 git submodule sync &&
248                 (
249                         cd submodule &&
250                         #actual foo/submodule
251                         test "$(git config remote.origin.url)" = "../foo/submodule"
252                 ) &&
253                 (
254                         cd submodule/sub-submodule &&
255                         test "$(git config remote.origin.url)" != "../../foo/submodule"
256                 )
257         )
258 '
259
260 test_expect_success '"git submodule sync --recursive" propagates changes in origin' '
261         (
262                 cd recursive-clone &&
263                 git remote set-url origin foo/bar &&
264                 git submodule sync --recursive &&
265                 (
266                         cd submodule &&
267                         #actual foo/submodule
268                         test "$(git config remote.origin.url)" = "../foo/submodule"
269                 ) &&
270                 (
271                         cd submodule/sub-submodule &&
272                         test "$(git config remote.origin.url)" = "../../foo/submodule"
273                 )
274         )
275 '
276
277 test_expect_success '"git submodule sync" handles origin URL of the form ./foo' '
278         (
279                 cd relative-clone &&
280                 git remote set-url origin ./foo &&
281                 git submodule sync &&
282                 (
283                         cd submodule &&
284                         #actual ./submodule
285                         test "$(git config remote.origin.url)" = "../submodule"
286                 )
287         )
288 '
289
290 test_expect_success '"git submodule sync" handles origin URL of the form ./foo/bar' '
291         (
292                 cd relative-clone &&
293                 git remote set-url origin ./foo/bar &&
294                 git submodule sync &&
295                 (
296                         cd submodule &&
297                         #actual ./foo/submodule
298                         test "$(git config remote.origin.url)" = "../foo/submodule"
299                 )
300         )
301 '
302
303 test_expect_success '"git submodule sync" handles origin URL of the form ../foo' '
304         (
305                 cd relative-clone &&
306                 git remote set-url origin ../foo &&
307                 git submodule sync &&
308                 (
309                         cd submodule &&
310                         #actual ../submodule
311                         test "$(git config remote.origin.url)" = "../../submodule"
312                 )
313         )
314 '
315
316 test_expect_success '"git submodule sync" handles origin URL of the form ../foo/bar' '
317         (
318                 cd relative-clone &&
319                 git remote set-url origin ../foo/bar &&
320                 git submodule sync &&
321                 (
322                         cd submodule &&
323                         #actual ../foo/submodule
324                         test "$(git config remote.origin.url)" = "../../foo/submodule"
325                 )
326         )
327 '
328
329 test_expect_success '"git submodule sync" handles origin URL of the form ../foo/bar with deeply nested submodule' '
330         (
331                 cd relative-clone &&
332                 git remote set-url origin ../foo/bar &&
333                 mkdir -p a/b/c &&
334                 (
335                         cd a/b/c &&
336                         git init &&
337                         >.gitignore &&
338                         git add .gitignore &&
339                         test_tick &&
340                         git commit -m "initial commit"
341                 ) &&
342                 git submodule add ../bar/a/b/c ./a/b/c &&
343                 git submodule sync &&
344                 (
345                         cd a/b/c &&
346                         #actual ../foo/bar/a/b/c
347                         test "$(git config remote.origin.url)" = "../../../../foo/bar/a/b/c"
348                 )
349         )
350 '
351
352
353 test_done