]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5526-fetch-submodules.sh
Merge branch 'rs/bisect-start-leakfix' into maint-2.38
[thirdparty/git.git] / t / t5526-fetch-submodules.sh
CommitLineData
7dce19d3
JL
1#!/bin/sh
2# Copyright (c) 2010, Jens Lehmann
3
4test_description='Recursive "git fetch" for submodules'
5
13a2f620
JT
6GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
7export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
8
7dce19d3
JL
9. ./test-lib.sh
10
11pwd=$(pwd)
12
6e94bd64
GC
13write_expected_sub () {
14 NEW_HEAD=$1 &&
b90d9f76 15 SUPER_HEAD=$2 &&
6e94bd64 16 cat >"$pwd/expect.err.sub" <<-EOF
b90d9f76 17 Fetching submodule submodule${SUPER_HEAD:+ at commit $SUPER_HEAD}
6e94bd64
GC
18 From $pwd/submodule
19 OLD_HEAD..$NEW_HEAD sub -> origin/sub
20 EOF
21}
22
b90d9f76
GC
23write_expected_sub2 () {
24 NEW_HEAD=$1 &&
25 SUPER_HEAD=$2 &&
26 cat >"$pwd/expect.err.sub2" <<-EOF
27 Fetching submodule submodule2${SUPER_HEAD:+ at commit $SUPER_HEAD}
28 From $pwd/submodule2
29 OLD_HEAD..$NEW_HEAD sub2 -> origin/sub2
30 EOF
31}
32
6e94bd64
GC
33write_expected_deep () {
34 NEW_HEAD=$1 &&
b90d9f76 35 SUB_HEAD=$2 &&
6e94bd64 36 cat >"$pwd/expect.err.deep" <<-EOF
b90d9f76 37 Fetching submodule submodule/subdir/deepsubmodule${SUB_HEAD:+ at commit $SUB_HEAD}
6e94bd64
GC
38 From $pwd/deepsubmodule
39 OLD_HEAD..$NEW_HEAD deep -> origin/deep
40 EOF
41}
42
43write_expected_super () {
44 NEW_HEAD=$1 &&
45 cat >"$pwd/expect.err.super" <<-EOF
46 From $pwd/.
47 OLD_HEAD..$NEW_HEAD super -> origin/super
48 EOF
49}
50
f3117dfd
GC
51# For each submodule in the test setup, this creates a commit and writes
52# a file that contains the expected err if that new commit were fetched.
53# These output files get concatenated in the right order by
54# verify_fetch_result().
d1d1572e 55add_submodule_commits () {
7dce19d3
JL
56 (
57 cd submodule &&
7dce19d3
JL
58 echo new >> subfile &&
59 test_tick &&
60 git add subfile &&
61 git commit -m new subfile &&
6e94bd64
GC
62 new_head=$(git rev-parse --short HEAD) &&
63 write_expected_sub $new_head
7dce19d3
JL
64 ) &&
65 (
66 cd deepsubmodule &&
7dce19d3
JL
67 echo new >> deepsubfile &&
68 test_tick &&
69 git add deepsubfile &&
70 git commit -m new deepsubfile &&
6e94bd64
GC
71 new_head=$(git rev-parse --short HEAD) &&
72 write_expected_deep $new_head
7dce19d3
JL
73 )
74}
75
d1d1572e
GC
76# For each superproject in the test setup, update its submodule, add the
77# submodule and create a new commit with the submodule change.
78#
79# This requires add_submodule_commits() to be called first, otherwise
80# the submodules will not have changed and cannot be "git add"-ed.
81add_superproject_commits () {
82 (
83 cd submodule &&
84 (
85 cd subdir/deepsubmodule &&
86 git fetch &&
87 git checkout -q FETCH_HEAD
88 ) &&
89 git add subdir/deepsubmodule &&
90 git commit -m "new deep submodule"
91 ) &&
92 git add submodule &&
93 git commit -m "new submodule" &&
94 super_head=$(git rev-parse --short HEAD) &&
95 sub_head=$(git -C submodule rev-parse --short HEAD) &&
96 write_expected_super $super_head &&
97 write_expected_sub $sub_head
98}
99
f3117dfd
GC
100# Verifies that the expected repositories were fetched. This is done by
101# concatenating the files expect.err.[super|sub|deep] in the correct
102# order and comparing it to the actual stderr.
103#
104# If a repo should not be fetched in the test, its corresponding
105# expect.err file should be rm-ed.
106verify_fetch_result () {
107 ACTUAL_ERR=$1 &&
108 rm -f expect.err.combined &&
109 if test -f expect.err.super
110 then
111 cat expect.err.super >>expect.err.combined
112 fi &&
113 if test -f expect.err.sub
114 then
115 cat expect.err.sub >>expect.err.combined
116 fi &&
117 if test -f expect.err.deep
118 then
119 cat expect.err.deep >>expect.err.combined
120 fi &&
b90d9f76
GC
121 if test -f expect.err.sub2
122 then
123 cat expect.err.sub2 >>expect.err.combined
124 fi &&
6e94bd64
GC
125 sed -e 's/[0-9a-f][0-9a-f]*\.\./OLD_HEAD\.\./' "$ACTUAL_ERR" >actual.err.cmp &&
126 test_cmp expect.err.combined actual.err.cmp
f3117dfd
GC
127}
128
7dce19d3 129test_expect_success setup '
225d2d50 130 git config --global protocol.file.allow always &&
7dce19d3
JL
131 mkdir deepsubmodule &&
132 (
133 cd deepsubmodule &&
134 git init &&
135 echo deepsubcontent > deepsubfile &&
136 git add deepsubfile &&
b618a2d9
JS
137 git commit -m new deepsubfile &&
138 git branch -M deep
7dce19d3
JL
139 ) &&
140 mkdir submodule &&
141 (
142 cd submodule &&
143 git init &&
144 echo subcontent > subfile &&
145 git add subfile &&
ea2d325b 146 git submodule add "$pwd/deepsubmodule" subdir/deepsubmodule &&
b618a2d9
JS
147 git commit -a -m new &&
148 git branch -M sub
7dce19d3
JL
149 ) &&
150 git submodule add "$pwd/submodule" submodule &&
151 git commit -am initial &&
b618a2d9 152 git branch -M super &&
7dce19d3
JL
153 git clone . downstream &&
154 (
155 cd downstream &&
156 git submodule update --init --recursive
fbf71645 157 )
7dce19d3
JL
158'
159
71f4a935 160test_expect_success "fetch --recurse-submodules recurses into submodules" '
d1d1572e 161 add_submodule_commits &&
7dce19d3
JL
162 (
163 cd downstream &&
164 git fetch --recurse-submodules >../actual.out 2>../actual.err
cabdee2c 165 ) &&
fbf71645 166 test_must_be_empty actual.out &&
f3117dfd 167 verify_fetch_result actual.err
7dce19d3
JL
168'
169
71f4a935 170test_expect_success "submodule.recurse option triggers recursive fetch" '
d1d1572e 171 add_submodule_commits &&
58f4203e
SB
172 (
173 cd downstream &&
174 git -c submodule.recurse fetch >../actual.out 2>../actual.err
175 ) &&
176 test_must_be_empty actual.out &&
f3117dfd 177 verify_fetch_result actual.err
58f4203e
SB
178'
179
71f4a935 180test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" '
d1d1572e 181 add_submodule_commits &&
62104ba1
SB
182 (
183 cd downstream &&
9b2ac68f 184 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
62104ba1
SB
185 ) &&
186 test_must_be_empty actual.out &&
f3117dfd 187 verify_fetch_result actual.err &&
62104ba1
SB
188 grep "2 tasks" trace.out
189'
190
7dce19d3 191test_expect_success "fetch alone only fetches superproject" '
d1d1572e 192 add_submodule_commits &&
7dce19d3
JL
193 (
194 cd downstream &&
195 git fetch >../actual.out 2>../actual.err
196 ) &&
ec10b018
SG
197 test_must_be_empty actual.out &&
198 test_must_be_empty actual.err
7dce19d3
JL
199'
200
c1a3c364
JL
201test_expect_success "fetch --no-recurse-submodules only fetches superproject" '
202 (
203 cd downstream &&
204 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
205 ) &&
ec10b018
SG
206 test_must_be_empty actual.out &&
207 test_must_be_empty actual.err
c1a3c364
JL
208'
209
71f4a935 210test_expect_success "using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
c1a3c364
JL
211 (
212 cd downstream &&
213 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
214 git fetch >../actual.out 2>../actual.err
cabdee2c 215 ) &&
fbf71645 216 test_must_be_empty actual.out &&
f3117dfd 217 verify_fetch_result actual.err
c1a3c364
JL
218'
219
220test_expect_success "--no-recurse-submodules overrides .gitmodules config" '
d1d1572e 221 add_submodule_commits &&
c1a3c364
JL
222 (
223 cd downstream &&
224 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
225 ) &&
ec10b018
SG
226 test_must_be_empty actual.out &&
227 test_must_be_empty actual.err
c1a3c364
JL
228'
229
230test_expect_success "using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
231 (
232 cd downstream &&
233 git config submodule.submodule.fetchRecurseSubmodules false &&
234 git fetch >../actual.out 2>../actual.err
235 ) &&
ec10b018
SG
236 test_must_be_empty actual.out &&
237 test_must_be_empty actual.err
c1a3c364
JL
238'
239
71f4a935 240test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
c1a3c364
JL
241 (
242 cd downstream &&
243 git fetch --recurse-submodules >../actual.out 2>../actual.err &&
a2a56468 244 git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
c1a3c364 245 git config --unset submodule.submodule.fetchRecurseSubmodules
cabdee2c 246 ) &&
fbf71645 247 test_must_be_empty actual.out &&
f3117dfd 248 verify_fetch_result actual.err
c1a3c364
JL
249'
250
7dce19d3
JL
251test_expect_success "--quiet propagates to submodules" '
252 (
253 cd downstream &&
254 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
255 ) &&
ec10b018
SG
256 test_must_be_empty actual.out &&
257 test_must_be_empty actual.err
7dce19d3
JL
258'
259
62104ba1
SB
260test_expect_success "--quiet propagates to parallel submodules" '
261 (
262 cd downstream &&
263 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
264 ) &&
ec10b018
SG
265 test_must_be_empty actual.out &&
266 test_must_be_empty actual.err
62104ba1
SB
267'
268
71f4a935 269test_expect_success "--dry-run propagates to submodules" '
d1d1572e 270 add_submodule_commits &&
7dce19d3
JL
271 (
272 cd downstream &&
273 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
cabdee2c 274 ) &&
fbf71645 275 test_must_be_empty actual.out &&
f3117dfd 276 verify_fetch_result actual.err
502681cd
ÆAB
277'
278
71f4a935 279test_expect_success "Without --dry-run propagates to submodules" '
7dce19d3
JL
280 (
281 cd downstream &&
282 git fetch --recurse-submodules >../actual.out 2>../actual.err
cabdee2c 283 ) &&
fbf71645 284 test_must_be_empty actual.out &&
f3117dfd 285 verify_fetch_result actual.err
7dce19d3
JL
286'
287
71f4a935 288test_expect_success "recurseSubmodules=true propagates into submodules" '
d1d1572e 289 add_submodule_commits &&
be254a0e
JL
290 (
291 cd downstream &&
2aac933c 292 git config fetch.recurseSubmodules true &&
be254a0e 293 git fetch >../actual.out 2>../actual.err
cabdee2c 294 ) &&
fbf71645 295 test_must_be_empty actual.out &&
f3117dfd 296 verify_fetch_result actual.err
be254a0e
JL
297'
298
71f4a935 299test_expect_success "--recurse-submodules overrides config in submodule" '
d1d1572e 300 add_submodule_commits &&
be254a0e
JL
301 (
302 cd downstream &&
303 (
304 cd submodule &&
305 git config fetch.recurseSubmodules false
306 ) &&
307 git fetch --recurse-submodules >../actual.out 2>../actual.err
cabdee2c 308 ) &&
fbf71645 309 test_must_be_empty actual.out &&
f3117dfd 310 verify_fetch_result actual.err
be254a0e
JL
311'
312
313test_expect_success "--no-recurse-submodules overrides config setting" '
d1d1572e 314 add_submodule_commits &&
be254a0e
JL
315 (
316 cd downstream &&
2aac933c 317 git config fetch.recurseSubmodules true &&
be254a0e
JL
318 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
319 ) &&
ec10b018
SG
320 test_must_be_empty actual.out &&
321 test_must_be_empty actual.err
be254a0e
JL
322'
323
71f4a935 324test_expect_success "Recursion doesn't happen when no new commits are fetched in the superproject" '
88a21979
JL
325 (
326 cd downstream &&
327 (
328 cd submodule &&
329 git config --unset fetch.recurseSubmodules
330 ) &&
2aac933c 331 git config --unset fetch.recurseSubmodules &&
88a21979
JL
332 git fetch >../actual.out 2>../actual.err
333 ) &&
ec10b018
SG
334 test_must_be_empty actual.out &&
335 test_must_be_empty actual.err
88a21979
JL
336'
337
71f4a935 338test_expect_success "Recursion stops when no new submodule commits are fetched" '
88a21979
JL
339 git add submodule &&
340 git commit -m "new submodule" &&
6e94bd64
GC
341 new_head=$(git rev-parse --short HEAD) &&
342 write_expected_super $new_head &&
f3117dfd 343 rm expect.err.deep &&
88a21979
JL
344 (
345 cd downstream &&
346 git fetch >../actual.out 2>../actual.err
347 ) &&
f3117dfd 348 verify_fetch_result actual.err &&
fbf71645 349 test_must_be_empty actual.out
88a21979
JL
350'
351
71f4a935 352test_expect_success "Recursion doesn't happen when new superproject commits don't change any submodules" '
d1d1572e 353 add_submodule_commits &&
88a21979
JL
354 echo a > file &&
355 git add file &&
356 git commit -m "new file" &&
6e94bd64
GC
357 new_head=$(git rev-parse --short HEAD) &&
358 write_expected_super $new_head &&
f3117dfd
GC
359 rm expect.err.sub &&
360 rm expect.err.deep &&
88a21979
JL
361 (
362 cd downstream &&
363 git fetch >../actual.out 2>../actual.err
364 ) &&
ec10b018 365 test_must_be_empty actual.out &&
f3117dfd 366 verify_fetch_result actual.err
88a21979
JL
367'
368
71f4a935 369test_expect_success "Recursion picks up config in submodule" '
88a21979
JL
370 (
371 cd downstream &&
372 git fetch --recurse-submodules &&
373 (
374 cd submodule &&
375 git config fetch.recurseSubmodules true
376 )
377 ) &&
d1d1572e 378 add_submodule_commits &&
88a21979
JL
379 git add submodule &&
380 git commit -m "new submodule" &&
6e94bd64
GC
381 new_head=$(git rev-parse --short HEAD) &&
382 write_expected_super $new_head &&
88a21979
JL
383 (
384 cd downstream &&
385 git fetch >../actual.out 2>../actual.err &&
386 (
387 cd submodule &&
388 git config --unset fetch.recurseSubmodules
389 )
390 ) &&
f3117dfd 391 verify_fetch_result actual.err &&
fbf71645 392 test_must_be_empty actual.out
88a21979
JL
393'
394
71f4a935 395test_expect_success "Recursion picks up all submodules when necessary" '
d1d1572e
GC
396 add_submodule_commits &&
397 add_superproject_commits &&
88a21979
JL
398 (
399 cd downstream &&
400 git fetch >../actual.out 2>../actual.err
401 ) &&
f3117dfd 402 verify_fetch_result actual.err &&
fbf71645 403 test_must_be_empty actual.out
88a21979
JL
404'
405
71f4a935 406test_expect_success "'--recurse-submodules=on-demand' doesn't recurse when no new commits are fetched in the superproject (and ignores config)" '
d1d1572e 407 add_submodule_commits &&
8f0700dd
JL
408 (
409 cd downstream &&
410 git config fetch.recurseSubmodules true &&
411 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
412 git config --unset fetch.recurseSubmodules
413 ) &&
ec10b018
SG
414 test_must_be_empty actual.out &&
415 test_must_be_empty actual.err
8f0700dd
JL
416'
417
71f4a935 418test_expect_success "'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
d1d1572e
GC
419 add_submodule_commits &&
420 add_superproject_commits &&
8f0700dd
JL
421 (
422 cd downstream &&
423 git config fetch.recurseSubmodules false &&
424 (
425 cd submodule &&
ea2d325b 426 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
8f0700dd
JL
427 ) &&
428 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
51b85471 429 git config --unset fetch.recurseSubmodules &&
8f0700dd
JL
430 (
431 cd submodule &&
ea2d325b 432 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
8f0700dd
JL
433 )
434 ) &&
fbf71645 435 test_must_be_empty actual.out &&
f3117dfd 436 verify_fetch_result actual.err
8f0700dd
JL
437'
438
b90d9f76
GC
439# These tests verify that we can fetch submodules that aren't in the
440# index.
441#
442# First, test the simple case where the index is empty and we only fetch
443# submodules that are not in the index.
444test_expect_success 'setup downstream branch without submodules' '
445 (
446 cd downstream &&
447 git checkout --recurse-submodules -b no-submodules &&
448 git rm .gitmodules &&
449 git rm submodule &&
450 git commit -m "no submodules" &&
451 git checkout --recurse-submodules super
452 )
453'
454
455test_expect_success "'--recurse-submodules=on-demand' should fetch submodule commits if the submodule is changed but the index has no submodules" '
456 add_submodule_commits &&
457 add_superproject_commits &&
458 # Fetch the new superproject commit
459 (
460 cd downstream &&
461 git switch --recurse-submodules no-submodules &&
462 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
463 ) &&
464 super_head=$(git rev-parse --short HEAD) &&
465 sub_head=$(git -C submodule rev-parse --short HEAD) &&
466 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
467
468 # assert that these are fetched from commits, not the index
469 write_expected_sub $sub_head $super_head &&
470 write_expected_deep $deep_head $sub_head &&
471
472 test_must_be_empty actual.out &&
473 verify_fetch_result actual.err
474'
475
476test_expect_success "'--recurse-submodules' should fetch submodule commits if the submodule is changed but the index has no submodules" '
477 add_submodule_commits &&
478 add_superproject_commits &&
479 # Fetch the new superproject commit
480 (
481 cd downstream &&
482 git switch --recurse-submodules no-submodules &&
483 git fetch --recurse-submodules >../actual.out 2>../actual.err
484 ) &&
485 super_head=$(git rev-parse --short HEAD) &&
486 sub_head=$(git -C submodule rev-parse --short HEAD) &&
487 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
488
489 # assert that these are fetched from commits, not the index
490 write_expected_sub $sub_head $super_head &&
491 write_expected_deep $deep_head $sub_head &&
492
493 test_must_be_empty actual.out &&
494 verify_fetch_result actual.err
495'
496
497test_expect_success "'--recurse-submodules' should ignore changed, inactive submodules" '
498 add_submodule_commits &&
499 add_superproject_commits &&
500
501 # Fetch the new superproject commit
502 (
503 cd downstream &&
504 git switch --recurse-submodules no-submodules &&
505 git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
506 ) &&
507 test_must_be_empty actual.out &&
508 super_head=$(git rev-parse --short HEAD) &&
509 write_expected_super $super_head &&
510 # Neither should be fetched because the submodule is inactive
511 rm expect.err.sub &&
512 rm expect.err.deep &&
513 verify_fetch_result actual.err
514'
515
516# Now that we know we can fetch submodules that are not in the index,
517# test that we can fetch index and non-index submodules in the same
518# operation.
519test_expect_success 'setup downstream branch with other submodule' '
520 mkdir submodule2 &&
521 (
522 cd submodule2 &&
523 git init &&
524 echo sub2content >sub2file &&
525 git add sub2file &&
526 git commit -a -m new &&
527 git branch -M sub2
528 ) &&
529 git checkout -b super-sub2-only &&
530 git submodule add "$pwd/submodule2" submodule2 &&
531 git commit -m "add sub2" &&
532 git checkout super &&
533 (
534 cd downstream &&
535 git fetch --recurse-submodules origin &&
536 git checkout super-sub2-only &&
537 # Explicitly run "git submodule update" because sub2 is new
538 # and has not been cloned.
539 git submodule update --init &&
540 git checkout --recurse-submodules super
541 )
542'
543
544test_expect_success "'--recurse-submodules' should fetch submodule commits in changed submodules and the index" '
545 test_when_finished "rm expect.err.sub2" &&
546 # Create new commit in origin/super
547 add_submodule_commits &&
548 add_superproject_commits &&
549
550 # Create new commit in origin/super-sub2-only
551 git checkout super-sub2-only &&
552 (
553 cd submodule2 &&
554 test_commit --no-tag foo
555 ) &&
556 git add submodule2 &&
557 git commit -m "new submodule2" &&
558
559 git checkout super &&
560 (
561 cd downstream &&
562 git fetch --recurse-submodules >../actual.out 2>../actual.err
563 ) &&
564 test_must_be_empty actual.out &&
565 sub2_head=$(git -C submodule2 rev-parse --short HEAD) &&
566 super_head=$(git rev-parse --short super) &&
567 super_sub2_only_head=$(git rev-parse --short super-sub2-only) &&
568 write_expected_sub2 $sub2_head $super_sub2_only_head &&
569
570 # write_expected_super cannot handle >1 branch. Since this is a
571 # one-off, construct expect.err.super manually.
572 cat >"$pwd/expect.err.super" <<-EOF &&
573 From $pwd/.
574 OLD_HEAD..$super_head super -> origin/super
575 OLD_HEAD..$super_sub2_only_head super-sub2-only -> origin/super-sub2-only
576 EOF
577 verify_fetch_result actual.err
578'
579
71f4a935 580test_expect_success "'--recurse-submodules=on-demand' stops when no new submodule commits are found in the superproject (and ignores config)" '
d1d1572e 581 add_submodule_commits &&
8f0700dd
JL
582 echo a >> file &&
583 git add file &&
584 git commit -m "new file" &&
6e94bd64
GC
585 new_head=$(git rev-parse --short HEAD) &&
586 write_expected_super $new_head &&
f3117dfd
GC
587 rm expect.err.sub &&
588 rm expect.err.deep &&
8f0700dd
JL
589 (
590 cd downstream &&
591 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
592 ) &&
ec10b018 593 test_must_be_empty actual.out &&
f3117dfd 594 verify_fetch_result actual.err
8f0700dd
JL
595'
596
71f4a935 597test_expect_success "'fetch.recurseSubmodules=on-demand' overrides global config" '
1fb25502
JL
598 (
599 cd downstream &&
600 git fetch --recurse-submodules
601 ) &&
d1d1572e 602 add_submodule_commits &&
1fb25502 603 git config --global fetch.recurseSubmodules false &&
1fb25502
JL
604 git add submodule &&
605 git commit -m "new submodule" &&
6e94bd64
GC
606 new_head=$(git rev-parse --short HEAD) &&
607 write_expected_super $new_head &&
f3117dfd 608 rm expect.err.deep &&
1fb25502
JL
609 (
610 cd downstream &&
611 git config fetch.recurseSubmodules on-demand &&
612 git fetch >../actual.out 2>../actual.err
613 ) &&
614 git config --global --unset fetch.recurseSubmodules &&
615 (
616 cd downstream &&
617 git config --unset fetch.recurseSubmodules
618 ) &&
fbf71645 619 test_must_be_empty actual.out &&
f3117dfd 620 verify_fetch_result actual.err
1fb25502
JL
621'
622
71f4a935 623test_expect_success "'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
bf42b384
JL
624 (
625 cd downstream &&
626 git fetch --recurse-submodules
627 ) &&
d1d1572e 628 add_submodule_commits &&
bf42b384 629 git config fetch.recurseSubmodules false &&
bf42b384
JL
630 git add submodule &&
631 git commit -m "new submodule" &&
6e94bd64
GC
632 new_head=$(git rev-parse --short HEAD) &&
633 write_expected_super $new_head &&
f3117dfd 634 rm expect.err.deep &&
bf42b384
JL
635 (
636 cd downstream &&
637 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
638 git fetch >../actual.out 2>../actual.err
639 ) &&
640 git config --unset fetch.recurseSubmodules &&
641 (
642 cd downstream &&
643 git config --unset submodule.submodule.fetchRecurseSubmodules
644 ) &&
fbf71645 645 test_must_be_empty actual.out &&
f3117dfd 646 verify_fetch_result actual.err
bf42b384
JL
647'
648
71f4a935 649test_expect_success "don't fetch submodule when newly recorded commits are already present" '
c16c3e40
JL
650 (
651 cd submodule &&
652 git checkout -q HEAD^^
653 ) &&
c16c3e40
JL
654 git add submodule &&
655 git commit -m "submodule rewound" &&
6e94bd64
GC
656 new_head=$(git rev-parse --short HEAD) &&
657 write_expected_super $new_head &&
f3117dfd
GC
658 rm expect.err.sub &&
659 # This file does not exist, but rm -f for readability
660 rm -f expect.err.deep &&
c16c3e40
JL
661 (
662 cd downstream &&
663 git fetch >../actual.out 2>../actual.err
664 ) &&
ec10b018 665 test_must_be_empty actual.out &&
f3117dfd 666 verify_fetch_result actual.err &&
01ce1225
HV
667 (
668 cd submodule &&
b618a2d9 669 git checkout -q sub
01ce1225
HV
670 )
671'
672
71f4a935 673test_expect_success "'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
01ce1225
HV
674 (
675 cd downstream &&
676 git fetch --recurse-submodules
677 ) &&
d1d1572e 678 add_submodule_commits &&
01ce1225
HV
679 git add submodule &&
680 git rm .gitmodules &&
681 git commit -m "new submodule without .gitmodules" &&
6e94bd64
GC
682 new_head=$(git rev-parse --short HEAD) &&
683 write_expected_super $new_head &&
f3117dfd 684 rm expect.err.deep &&
01ce1225
HV
685 (
686 cd downstream &&
687 rm .gitmodules &&
688 git config fetch.recurseSubmodules on-demand &&
689 # fake submodule configuration to avoid skipping submodule handling
690 git config -f .gitmodules submodule.fake.path fake &&
691 git config -f .gitmodules submodule.fake.url fakeurl &&
692 git add .gitmodules &&
693 git config --unset submodule.submodule.url &&
694 git fetch >../actual.out 2>../actual.err &&
695 # cleanup
696 git config --unset fetch.recurseSubmodules &&
697 git reset --hard
698 ) &&
1c5e94f4 699 test_must_be_empty actual.out &&
f3117dfd 700 verify_fetch_result actual.err &&
01ce1225
HV
701 git checkout HEAD^ -- .gitmodules &&
702 git add .gitmodules &&
703 git commit -m "new submodule restored .gitmodules"
c16c3e40
JL
704'
705
a028a193
SB
706test_expect_success 'fetching submodules respects parallel settings' '
707 git config fetch.recurseSubmodules true &&
708 (
709 cd downstream &&
e3a9d1ac
JH
710 GIT_TRACE=$(pwd)/trace.out git fetch &&
711 grep "1 tasks" trace.out &&
a028a193
SB
712 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 7 &&
713 grep "7 tasks" trace.out &&
714 git config submodule.fetchJobs 8 &&
715 GIT_TRACE=$(pwd)/trace.out git fetch &&
716 grep "8 tasks" trace.out &&
717 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 9 &&
718 grep "9 tasks" trace.out
719 )
720'
721
10f5c526
JH
722test_expect_success 'fetching submodule into a broken repository' '
723 # Prepare src and src/sub nested in it
724 git init src &&
725 (
726 cd src &&
727 git init sub &&
728 git -C sub commit --allow-empty -m "initial in sub" &&
729 git submodule add -- ./sub sub &&
730 git commit -m "initial in top"
731 ) &&
732
733 # Clone the old-fashoned way
734 git clone src dst &&
735 git -C dst clone ../src/sub sub &&
736
737 # Make sure that old-fashoned layout is still supported
738 git -C dst status &&
739
740 # "diff" would find no change
741 git -C dst diff --exit-code &&
742
743 # Recursive-fetch works fine
744 git -C dst fetch --recurse-submodules &&
745
746 # Break the receiving submodule
747 rm -f dst/sub/.git/HEAD &&
748
749 # NOTE: without the fix the following tests will recurse forever!
750 # They should terminate with an error.
751
752 test_must_fail git -C dst status &&
753 test_must_fail git -C dst diff &&
754 test_must_fail git -C dst fetch --recurse-submodules
755'
756
c68f8375
HV
757test_expect_success "fetch new commits when submodule got renamed" '
758 git clone . downstream_rename &&
759 (
760 cd downstream_rename &&
c3749f6e 761 git submodule update --init --recursive &&
c68f8375
HV
762 git checkout -b rename &&
763 git mv submodule submodule_renamed &&
764 (
765 cd submodule_renamed &&
766 git checkout -b rename_sub &&
767 echo a >a &&
768 git add a &&
769 git commit -ma &&
770 git push origin rename_sub &&
771 git rev-parse HEAD >../../expect
772 ) &&
773 git add submodule_renamed &&
774 git commit -m "update renamed submodule" &&
775 git push origin rename
776 ) &&
777 (
778 cd downstream &&
779 git fetch --recurse-submodules=on-demand &&
780 (
781 cd submodule &&
782 git rev-parse origin/rename_sub >../../actual
783 )
784 ) &&
785 test_cmp expect actual
786'
787
be76c212
SB
788test_expect_success "fetch new submodule commits on-demand outside standard refspec" '
789 # add a second submodule and ensure it is around in downstream first
790 git clone submodule sub1 &&
791 git submodule add ./sub1 &&
792 git commit -m "adding a second submodule" &&
793 git -C downstream pull &&
794 git -C downstream submodule update --init --recursive &&
795
796 git checkout --detach &&
797
798 C=$(git -C submodule commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
799 git -C submodule update-ref refs/changes/1 $C &&
800 git update-index --cacheinfo 160000 $C submodule &&
801 test_tick &&
802
803 D=$(git -C sub1 commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
804 git -C sub1 update-ref refs/changes/2 $D &&
805 git update-index --cacheinfo 160000 $D sub1 &&
806
807 git commit -m "updated submodules outside of refs/heads" &&
808 E=$(git rev-parse HEAD) &&
809 git update-ref refs/changes/3 $E &&
810 (
811 cd downstream &&
812 git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
813 git -C submodule cat-file -t $C &&
814 git -C sub1 cat-file -t $D &&
815 git checkout --recurse-submodules FETCH_HEAD
816 )
817'
818
819test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD' '
820 # depends on the previous test for setup
821
822 C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
823 git -C submodule update-ref refs/changes/4 $C &&
824 git update-index --cacheinfo 160000 $C submodule &&
825 test_tick &&
826
827 D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
828 git -C sub1 update-ref refs/changes/5 $D &&
829 git update-index --cacheinfo 160000 $D sub1 &&
830
831 git commit -m "updated submodules outside of refs/heads" &&
832 E=$(git rev-parse HEAD) &&
833 git update-ref refs/changes/6 $E &&
834 (
835 cd downstream &&
836 git fetch --recurse-submodules origin refs/changes/6 &&
837 git -C submodule cat-file -t $C &&
838 git -C sub1 cat-file -t $D &&
839 git checkout --recurse-submodules FETCH_HEAD
840 )
841'
842
843test_expect_success 'fetch new submodule commits on-demand without .gitmodules entry' '
844 # depends on the previous test for setup
845
846 git config -f .gitmodules --remove-section submodule.sub1 &&
847 git add .gitmodules &&
848 git commit -m "delete gitmodules file" &&
b618a2d9 849 git checkout -B super &&
be76c212 850 git -C downstream fetch &&
b618a2d9 851 git -C downstream checkout origin/super &&
be76c212
SB
852
853 C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
854 git -C submodule update-ref refs/changes/7 $C &&
855 git update-index --cacheinfo 160000 $C submodule &&
856 test_tick &&
857
858 D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
859 git -C sub1 update-ref refs/changes/8 $D &&
860 git update-index --cacheinfo 160000 $D sub1 &&
861
862 git commit -m "updated submodules outside of refs/heads" &&
863 E=$(git rev-parse HEAD) &&
864 git update-ref refs/changes/9 $E &&
865 (
866 cd downstream &&
867 git fetch --recurse-submodules origin refs/changes/9 &&
868 git -C submodule cat-file -t $C &&
869 git -C sub1 cat-file -t $D &&
870 git checkout --recurse-submodules FETCH_HEAD
871 )
872'
873
874test_expect_success 'fetch new submodule commit intermittently referenced by superproject' '
875 # depends on the previous test for setup
876
877 D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
878 E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
879 F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
880
881 git -C sub1 update-ref refs/changes/10 $D &&
882 git update-index --cacheinfo 160000 $D sub1 &&
883 git commit -m "updated submodules outside of refs/heads" &&
884
885 git -C sub1 update-ref refs/changes/11 $E &&
886 git update-index --cacheinfo 160000 $E sub1 &&
887 git commit -m "updated submodules outside of refs/heads" &&
888
889 git -C sub1 update-ref refs/changes/12 $F &&
890 git update-index --cacheinfo 160000 $F sub1 &&
891 git commit -m "updated submodules outside of refs/heads" &&
892
893 G=$(git rev-parse HEAD) &&
894 git update-ref refs/changes/13 $G &&
895 (
896 cd downstream &&
897 git fetch --recurse-submodules origin refs/changes/13 &&
898
899 git -C sub1 cat-file -t $D &&
900 git -C sub1 cat-file -t $E &&
901 git -C sub1 cat-file -t $F
902 )
903'
904
505a2765
PK
905add_commit_push () {
906 dir="$1" &&
907 msg="$2" &&
908 shift 2 &&
909 git -C "$dir" add "$@" &&
910 git -C "$dir" commit -a -m "$msg" &&
911 git -C "$dir" push
912}
913
914compare_refs_in_dir () {
915 fail= &&
916 if test "x$1" = 'x!'
917 then
918 fail='!' &&
919 shift
920 fi &&
921 git -C "$1" rev-parse --verify "$2" >expect &&
922 git -C "$3" rev-parse --verify "$4" >actual &&
923 eval $fail test_cmp expect actual
924}
925
926
927test_expect_success 'setup nested submodule fetch test' '
928 # does not depend on any previous test setups
929
930 for repo in outer middle inner
931 do
932 git init --bare $repo &&
933 git clone $repo ${repo}_content &&
934 echo "$repo" >"${repo}_content/file" &&
935 add_commit_push ${repo}_content "initial" file ||
936 return 1
937 done &&
938
939 git clone outer A &&
940 git -C A submodule add "$pwd/middle" &&
941 git -C A/middle/ submodule add "$pwd/inner" &&
942 add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
943 add_commit_push A/ "adding middle sub" .gitmodules middle &&
944
945 git clone outer B &&
946 git -C B/ submodule update --init middle &&
947
948 compare_refs_in_dir A HEAD B HEAD &&
949 compare_refs_in_dir A/middle HEAD B/middle HEAD &&
950 test_path_is_file B/file &&
951 test_path_is_file B/middle/file &&
952 test_path_is_missing B/middle/inner/file &&
953
954 echo "change on inner repo of A" >"A/middle/inner/file" &&
955 add_commit_push A/middle/inner "change on inner" file &&
956 add_commit_push A/middle "change on inner" inner &&
957 add_commit_push A "change on inner" middle
958'
959
960test_expect_success 'fetching a superproject containing an uninitialized sub/sub project' '
961 # depends on previous test for setup
962
963 git -C B/ fetch &&
964 compare_refs_in_dir A origin/HEAD B origin/HEAD
965'
966
967fetch_with_recursion_abort () {
968 # In a regression the following git call will run into infinite recursion.
969 # To handle that, we connect the sed command to the git call by a pipe
970 # so that sed can kill the infinite recursion when detected.
971 # The recursion creates git output like:
972 # Fetching submodule sub
973 # Fetching submodule sub/sub <-- [1]
974 # Fetching submodule sub/sub/sub
975 # ...
976 # [1] sed will stop reading and cause git to eventually stop and die
977
978 git -C "$1" fetch --recurse-submodules 2>&1 |
979 sed "/Fetching submodule $2[^$]/q" >out &&
980 ! grep "Fetching submodule $2[^$]" out
981}
982
983test_expect_success 'setup recursive fetch with uninit submodule' '
984 # does not depend on any previous test setups
985
986 test_create_repo super &&
987 test_commit -C super initial &&
988 test_create_repo sub &&
989 test_commit -C sub initial &&
990 git -C sub rev-parse HEAD >expect &&
991
992 git -C super submodule add ../sub &&
993 git -C super commit -m "add sub" &&
994
995 git clone super superclone &&
996 git -C superclone submodule status >out &&
997 sed -e "s/^-//" -e "s/ sub.*$//" out >actual &&
998 test_cmp expect actual
999'
1000
1001test_expect_success 'recursive fetch with uninit submodule' '
1002 # depends on previous test for setup
1003
1004 fetch_with_recursion_abort superclone sub &&
1005 git -C superclone submodule status >out &&
1006 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1007 test_cmp expect actual
1008'
1009
1010test_expect_success 'recursive fetch after deinit a submodule' '
1011 # depends on previous test for setup
1012
1013 git -C superclone submodule update --init sub &&
1014 git -C superclone submodule deinit -f sub &&
1015
1016 fetch_with_recursion_abort superclone sub &&
1017 git -C superclone submodule status >out &&
1018 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1019 test_cmp expect actual
1020'
1021
b90d9f76
GC
1022test_expect_success 'setup repo with upstreams that share a submodule name' '
1023 mkdir same-name-1 &&
1024 (
1025 cd same-name-1 &&
1026 git init -b main &&
1027 test_commit --no-tag a
1028 ) &&
1029 git clone same-name-1 same-name-2 &&
1030 # same-name-1 and same-name-2 both add a submodule with the
1031 # name "submodule"
1032 (
1033 cd same-name-1 &&
1034 mkdir submodule &&
1035 git -C submodule init -b main &&
1036 test_commit -C submodule --no-tag a1 &&
1037 git submodule add "$pwd/same-name-1/submodule" &&
1038 git add submodule &&
1039 git commit -m "super-a1"
1040 ) &&
1041 (
1042 cd same-name-2 &&
1043 mkdir submodule &&
1044 git -C submodule init -b main &&
1045 test_commit -C submodule --no-tag a2 &&
1046 git submodule add "$pwd/same-name-2/submodule" &&
1047 git add submodule &&
1048 git commit -m "super-a2"
1049 ) &&
1050 git clone same-name-1 -o same-name-1 same-name-downstream &&
1051 (
1052 cd same-name-downstream &&
1053 git remote add same-name-2 ../same-name-2 &&
1054 git fetch --all &&
1055 # init downstream with same-name-1
1056 git submodule update --init
1057 )
1058'
1059
1060test_expect_success 'fetch --recurse-submodules updates name-conflicted, populated submodule' '
1061 test_when_finished "git -C same-name-downstream checkout main" &&
1062 (
1063 cd same-name-1 &&
1064 test_commit -C submodule --no-tag b1 &&
1065 git add submodule &&
1066 git commit -m "super-b1"
1067 ) &&
1068 (
1069 cd same-name-2 &&
1070 test_commit -C submodule --no-tag b2 &&
1071 git add submodule &&
1072 git commit -m "super-b2"
1073 ) &&
1074 (
1075 cd same-name-downstream &&
1076 # even though the .gitmodules is correct, we cannot
1077 # fetch from same-name-2
1078 git checkout same-name-2/main &&
1079 git fetch --recurse-submodules same-name-1 &&
1080 test_must_fail git fetch --recurse-submodules same-name-2
1081 ) &&
1082 super_head1=$(git -C same-name-1 rev-parse HEAD) &&
1083 git -C same-name-downstream cat-file -e $super_head1 &&
1084
1085 super_head2=$(git -C same-name-2 rev-parse HEAD) &&
1086 git -C same-name-downstream cat-file -e $super_head2 &&
1087
1088 sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1089 git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
1090
1091 sub_head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1092 test_must_fail git -C same-name-downstream/submodule cat-file -e $sub_head2
1093'
1094
1095test_expect_success 'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
1096 (
1097 cd same-name-1 &&
1098 test_commit -C submodule --no-tag c1 &&
1099 git add submodule &&
1100 git commit -m "super-c1"
1101 ) &&
1102 (
1103 cd same-name-2 &&
1104 test_commit -C submodule --no-tag c2 &&
1105 git add submodule &&
1106 git commit -m "super-c2"
1107 ) &&
1108 (
1109 cd same-name-downstream &&
1110 git checkout main &&
1111 git rm .gitmodules &&
1112 git rm submodule &&
1113 git commit -m "no submodules" &&
1114 git fetch --recurse-submodules same-name-1
1115 ) &&
1116 head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1117 head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1118 (
1119 cd same-name-downstream/.git/modules/submodule &&
1120 # The submodule has core.worktree pointing to the "git
1121 # rm"-ed directory, overwrite the invalid value. See
1122 # comment in get_fetch_task_from_changed() for more
1123 # information.
1124 git --work-tree=. cat-file -e $head1 &&
1125 test_must_fail git --work-tree=. cat-file -e $head2
1126 )
1127'
1128
0353c688
JH
1129test_expect_success 'fetch --all with --recurse-submodules' '
1130 test_when_finished "rm -fr src_clone" &&
1131 git clone --recurse-submodules src src_clone &&
1132 (
1133 cd src_clone &&
1134 git config submodule.recurse true &&
1135 git config fetch.parallel 0 &&
1136 git fetch --all 2>../fetch-log
1137 ) &&
1138 grep "^Fetching submodule sub$" fetch-log >fetch-subs &&
1139 test_line_count = 1 fetch-subs
1140'
1141
1142test_expect_success 'fetch --all with --recurse-submodules with multiple' '
1143 test_when_finished "rm -fr src_clone" &&
1144 git clone --recurse-submodules src src_clone &&
1145 (
1146 cd src_clone &&
1147 git remote add secondary ../src &&
1148 git config submodule.recurse true &&
1149 git config fetch.parallel 0 &&
1150 git fetch --all 2>../fetch-log
1151 ) &&
1152 grep "Fetching submodule sub" fetch-log >fetch-subs &&
1153 test_line_count = 2 fetch-subs
1154'
1155
7dce19d3 1156test_done