Raw
1 #!/bin/sh
2 # Copyright (c) 2010, Jens Lehmann
3
4 test_description='Recursive "git fetch" for submodules'
5
6 GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
7 export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
8
9 . ./test-lib.sh
10
11 pwd=$(pwd)
12
13 write_expected_sub () {
14 NEW_HEAD=$1 &&
15 SUPER_HEAD=$2 &&
16 cat >"$pwd/expect.err.sub" <<-EOF
17 Fetching submodule submodule${SUPER_HEAD:+ at commit $SUPER_HEAD}
18 From $pwd/submodule
19 OLD_HEAD..$NEW_HEAD sub -> origin/sub
20 EOF
21 }
22
23 write_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
33 write_expected_deep () {
34 NEW_HEAD=$1 &&
35 SUB_HEAD=$2 &&
36 cat >"$pwd/expect.err.deep" <<-EOF
37 Fetching submodule submodule/subdir/deepsubmodule${SUB_HEAD:+ at commit $SUB_HEAD}
38 From $pwd/deepsubmodule
39 OLD_HEAD..$NEW_HEAD deep -> origin/deep
40 EOF
41 }
42
43 write_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
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().
55 add_submodule_commits () {
56 (
57 cd submodule &&
58 echo new >> subfile &&
59 test_tick &&
60 git add subfile &&
61 git commit -m new subfile &&
62 new_head=$(git rev-parse --short HEAD) &&
63 write_expected_sub $new_head
64 ) &&
65 (
66 cd deepsubmodule &&
67 echo new >> deepsubfile &&
68 test_tick &&
69 git add deepsubfile &&
70 git commit -m new deepsubfile &&
71 new_head=$(git rev-parse --short HEAD) &&
72 write_expected_deep $new_head
73 )
74 }
75
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.
81 add_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
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.
106 verify_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 &&
121 if test -f expect.err.sub2
122 then
123 cat expect.err.sub2 >>expect.err.combined
124 fi &&
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
127 }
128
129 test_expect_success setup '
130 git config --global protocol.file.allow always &&
131 mkdir deepsubmodule &&
132 (
133 cd deepsubmodule &&
134 git init &&
135 echo deepsubcontent > deepsubfile &&
136 git add deepsubfile &&
137 git commit -m new deepsubfile &&
138 git branch -M deep
139 ) &&
140 mkdir submodule &&
141 (
142 cd submodule &&
143 git init &&
144 echo subcontent > subfile &&
145 git add subfile &&
146 git submodule add "$pwd/deepsubmodule" subdir/deepsubmodule &&
147 git commit -a -m new &&
148 git branch -M sub
149 ) &&
150 git submodule add "$pwd/submodule" submodule &&
151 git commit -am initial &&
152 git branch -M super &&
153 git clone . downstream &&
154 (
155 cd downstream &&
156 git submodule update --init --recursive
157 )
158 '
159
160 test_expect_success "fetch --recurse-submodules recurses into submodules" '
161 add_submodule_commits &&
162 (
163 cd downstream &&
164 git fetch --recurse-submodules >../actual.out 2>../actual.err
165 ) &&
166 test_must_be_empty actual.out &&
167 verify_fetch_result actual.err
168 '
169
170 test_expect_success "fetch --recurse-submodules honors --no-write-fetch-head" '
171 (
172 cd downstream &&
173 git submodule foreach --recursive \
174 sh -c "cd \"\$(git rev-parse --git-dir)\" && rm -f FETCH_HEAD" &&
175
176 git fetch --recurse-submodules --no-write-fetch-head &&
177
178 git submodule foreach --recursive \
179 sh -c "cd \"\$(git rev-parse --git-dir)\" && ! test -f FETCH_HEAD"
180 )
181 '
182
183 test_expect_success "submodule.recurse option triggers recursive fetch" '
184 add_submodule_commits &&
185 (
186 cd downstream &&
187 git -c submodule.recurse fetch >../actual.out 2>../actual.err
188 ) &&
189 test_must_be_empty actual.out &&
190 verify_fetch_result actual.err
191 '
192
193 test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" '
194 test_when_finished "rm -f trace.out" &&
195 add_submodule_commits &&
196 (
197 cd downstream &&
198 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
199 ) &&
200 test_must_be_empty actual.out &&
201 verify_fetch_result actual.err &&
202 test_grep "2 tasks" trace.out
203 '
204
205 test_expect_success "fetch alone only fetches superproject" '
206 add_submodule_commits &&
207 (
208 cd downstream &&
209 git fetch >../actual.out 2>../actual.err
210 ) &&
211 test_must_be_empty actual.out &&
212 test_must_be_empty actual.err
213 '
214
215 test_expect_success "fetch --no-recurse-submodules only fetches superproject" '
216 (
217 cd downstream &&
218 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
219 ) &&
220 test_must_be_empty actual.out &&
221 test_must_be_empty actual.err
222 '
223
224 test_expect_success "using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
225 (
226 cd downstream &&
227 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
228 git fetch >../actual.out 2>../actual.err
229 ) &&
230 test_must_be_empty actual.out &&
231 verify_fetch_result actual.err
232 '
233
234 test_expect_success "--no-recurse-submodules overrides .gitmodules config" '
235 add_submodule_commits &&
236 (
237 cd downstream &&
238 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
239 ) &&
240 test_must_be_empty actual.out &&
241 test_must_be_empty actual.err
242 '
243
244 test_expect_success "using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
245 (
246 cd downstream &&
247 git config submodule.submodule.fetchRecurseSubmodules false &&
248 git fetch >../actual.out 2>../actual.err
249 ) &&
250 test_must_be_empty actual.out &&
251 test_must_be_empty actual.err
252 '
253
254 test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
255 (
256 cd downstream &&
257 git fetch --recurse-submodules >../actual.out 2>../actual.err &&
258 git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
259 git config --unset submodule.submodule.fetchRecurseSubmodules
260 ) &&
261 test_must_be_empty actual.out &&
262 verify_fetch_result actual.err
263 '
264
265 test_expect_success "--quiet propagates to submodules" '
266 (
267 cd downstream &&
268 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
269 ) &&
270 test_must_be_empty actual.out &&
271 test_must_be_empty actual.err
272 '
273
274 test_expect_success "--quiet propagates to parallel submodules" '
275 (
276 cd downstream &&
277 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
278 ) &&
279 test_must_be_empty actual.out &&
280 test_must_be_empty actual.err
281 '
282
283 test_expect_success "--dry-run propagates to submodules" '
284 add_submodule_commits &&
285 (
286 cd downstream &&
287 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
288 ) &&
289 test_must_be_empty actual.out &&
290 verify_fetch_result actual.err
291 '
292
293 test_expect_success "Without --dry-run propagates to submodules" '
294 (
295 cd downstream &&
296 git fetch --recurse-submodules >../actual.out 2>../actual.err
297 ) &&
298 test_must_be_empty actual.out &&
299 verify_fetch_result actual.err
300 '
301
302 test_expect_success "recurseSubmodules=true propagates into submodules" '
303 add_submodule_commits &&
304 (
305 cd downstream &&
306 git config fetch.recurseSubmodules true &&
307 git fetch >../actual.out 2>../actual.err
308 ) &&
309 test_must_be_empty actual.out &&
310 verify_fetch_result actual.err
311 '
312
313 test_expect_success "--recurse-submodules overrides config in submodule" '
314 add_submodule_commits &&
315 (
316 cd downstream &&
317 (
318 cd submodule &&
319 git config fetch.recurseSubmodules false
320 ) &&
321 git fetch --recurse-submodules >../actual.out 2>../actual.err
322 ) &&
323 test_must_be_empty actual.out &&
324 verify_fetch_result actual.err
325 '
326
327 test_expect_success "--no-recurse-submodules overrides config setting" '
328 add_submodule_commits &&
329 (
330 cd downstream &&
331 git config fetch.recurseSubmodules true &&
332 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
333 ) &&
334 test_must_be_empty actual.out &&
335 test_must_be_empty actual.err
336 '
337
338 test_expect_success "Recursion doesn't happen when no new commits are fetched in the superproject" '
339 (
340 cd downstream &&
341 (
342 cd submodule &&
343 git config --unset fetch.recurseSubmodules
344 ) &&
345 git config --unset fetch.recurseSubmodules &&
346 git fetch >../actual.out 2>../actual.err
347 ) &&
348 test_must_be_empty actual.out &&
349 test_must_be_empty actual.err
350 '
351
352 test_expect_success "Recursion stops when no new submodule commits are fetched" '
353 git add submodule &&
354 git commit -m "new submodule" &&
355 new_head=$(git rev-parse --short HEAD) &&
356 write_expected_super $new_head &&
357 rm expect.err.deep &&
358 (
359 cd downstream &&
360 git fetch >../actual.out 2>../actual.err
361 ) &&
362 verify_fetch_result actual.err &&
363 test_must_be_empty actual.out
364 '
365
366 test_expect_success "Recursion doesn't happen when new superproject commits don't change any submodules" '
367 add_submodule_commits &&
368 echo a > file &&
369 git add file &&
370 git commit -m "new file" &&
371 new_head=$(git rev-parse --short HEAD) &&
372 write_expected_super $new_head &&
373 rm expect.err.sub &&
374 rm expect.err.deep &&
375 (
376 cd downstream &&
377 git fetch >../actual.out 2>../actual.err
378 ) &&
379 test_must_be_empty actual.out &&
380 verify_fetch_result actual.err
381 '
382
383 test_expect_success "Recursion picks up config in submodule" '
384 (
385 cd downstream &&
386 git fetch --recurse-submodules &&
387 (
388 cd submodule &&
389 git config fetch.recurseSubmodules true
390 )
391 ) &&
392 add_submodule_commits &&
393 git add submodule &&
394 git commit -m "new submodule" &&
395 new_head=$(git rev-parse --short HEAD) &&
396 write_expected_super $new_head &&
397 (
398 cd downstream &&
399 git fetch >../actual.out 2>../actual.err &&
400 (
401 cd submodule &&
402 git config --unset fetch.recurseSubmodules
403 )
404 ) &&
405 verify_fetch_result actual.err &&
406 test_must_be_empty actual.out
407 '
408
409 test_expect_success "Recursion picks up all submodules when necessary" '
410 add_submodule_commits &&
411 add_superproject_commits &&
412 (
413 cd downstream &&
414 git fetch >../actual.out 2>../actual.err
415 ) &&
416 verify_fetch_result actual.err &&
417 test_must_be_empty actual.out
418 '
419
420 test_expect_success "'--recurse-submodules=on-demand' doesn't recurse when no new commits are fetched in the superproject (and ignores config)" '
421 add_submodule_commits &&
422 (
423 cd downstream &&
424 git config fetch.recurseSubmodules true &&
425 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
426 git config --unset fetch.recurseSubmodules
427 ) &&
428 test_must_be_empty actual.out &&
429 test_must_be_empty actual.err
430 '
431
432 test_expect_success "'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
433 add_submodule_commits &&
434 add_superproject_commits &&
435 (
436 cd downstream &&
437 git config fetch.recurseSubmodules false &&
438 (
439 cd submodule &&
440 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
441 ) &&
442 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
443 git config --unset fetch.recurseSubmodules &&
444 (
445 cd submodule &&
446 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
447 )
448 ) &&
449 test_must_be_empty actual.out &&
450 verify_fetch_result actual.err
451 '
452
453 # These tests verify that we can fetch submodules that aren't in the
454 # index.
455 #
456 # First, test the simple case where the index is empty and we only fetch
457 # submodules that are not in the index.
458 test_expect_success 'setup downstream branch without submodules' '
459 (
460 cd downstream &&
461 git checkout --recurse-submodules -b no-submodules &&
462 git rm .gitmodules &&
463 git rm submodule &&
464 git commit -m "no submodules" &&
465 git checkout --recurse-submodules super
466 )
467 '
468
469 test_expect_success "'--recurse-submodules=on-demand' should fetch submodule commits if the submodule is changed but the index has no submodules" '
470 add_submodule_commits &&
471 add_superproject_commits &&
472 # Fetch the new superproject commit
473 (
474 cd downstream &&
475 git switch --recurse-submodules no-submodules &&
476 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
477 ) &&
478 super_head=$(git rev-parse --short HEAD) &&
479 sub_head=$(git -C submodule rev-parse --short HEAD) &&
480 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
481
482 # assert that these are fetched from commits, not the index
483 write_expected_sub $sub_head $super_head &&
484 write_expected_deep $deep_head $sub_head &&
485
486 test_must_be_empty actual.out &&
487 verify_fetch_result actual.err
488 '
489
490 test_expect_success "'--recurse-submodules' should fetch submodule commits if the submodule is changed but the index has no submodules" '
491 add_submodule_commits &&
492 add_superproject_commits &&
493 # Fetch the new superproject commit
494 (
495 cd downstream &&
496 git switch --recurse-submodules no-submodules &&
497 git fetch --recurse-submodules >../actual.out 2>../actual.err
498 ) &&
499 super_head=$(git rev-parse --short HEAD) &&
500 sub_head=$(git -C submodule rev-parse --short HEAD) &&
501 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
502
503 # assert that these are fetched from commits, not the index
504 write_expected_sub $sub_head $super_head &&
505 write_expected_deep $deep_head $sub_head &&
506
507 test_must_be_empty actual.out &&
508 verify_fetch_result actual.err
509 '
510
511 test_expect_success "'--recurse-submodules' should ignore changed, inactive submodules" '
512 add_submodule_commits &&
513 add_superproject_commits &&
514
515 # Fetch the new superproject commit
516 (
517 cd downstream &&
518 git switch --recurse-submodules no-submodules &&
519 git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
520 ) &&
521 test_must_be_empty actual.out &&
522 super_head=$(git rev-parse --short HEAD) &&
523 write_expected_super $super_head &&
524 # Neither should be fetched because the submodule is inactive
525 rm expect.err.sub &&
526 rm expect.err.deep &&
527 verify_fetch_result actual.err
528 '
529
530 # Now that we know we can fetch submodules that are not in the index,
531 # test that we can fetch index and non-index submodules in the same
532 # operation.
533 test_expect_success 'setup downstream branch with other submodule' '
534 mkdir submodule2 &&
535 (
536 cd submodule2 &&
537 git init &&
538 echo sub2content >sub2file &&
539 git add sub2file &&
540 git commit -a -m new &&
541 git branch -M sub2
542 ) &&
543 git checkout -b super-sub2-only &&
544 git submodule add "$pwd/submodule2" submodule2 &&
545 git commit -m "add sub2" &&
546 git checkout super &&
547 (
548 cd downstream &&
549 git fetch --recurse-submodules origin &&
550 git checkout super-sub2-only &&
551 # Explicitly run "git submodule update" because sub2 is new
552 # and has not been cloned.
553 git submodule update --init &&
554 git checkout --recurse-submodules super
555 )
556 '
557
558 test_expect_success "'--recurse-submodules' should fetch submodule commits in changed submodules and the index" '
559 test_when_finished "rm expect.err.sub2" &&
560 # Create new commit in origin/super
561 add_submodule_commits &&
562 add_superproject_commits &&
563
564 # Create new commit in origin/super-sub2-only
565 git checkout super-sub2-only &&
566 (
567 cd submodule2 &&
568 test_commit --no-tag foo
569 ) &&
570 git add submodule2 &&
571 git commit -m "new submodule2" &&
572
573 git checkout super &&
574 (
575 cd downstream &&
576 git fetch --recurse-submodules >../actual.out 2>../actual.err
577 ) &&
578 test_must_be_empty actual.out &&
579 sub2_head=$(git -C submodule2 rev-parse --short HEAD) &&
580 super_head=$(git rev-parse --short super) &&
581 super_sub2_only_head=$(git rev-parse --short super-sub2-only) &&
582 write_expected_sub2 $sub2_head $super_sub2_only_head &&
583
584 # write_expected_super cannot handle >1 branch. Since this is a
585 # one-off, construct expect.err.super manually.
586 cat >"$pwd/expect.err.super" <<-EOF &&
587 From $pwd/.
588 OLD_HEAD..$super_head super -> origin/super
589 OLD_HEAD..$super_sub2_only_head super-sub2-only -> origin/super-sub2-only
590 EOF
591 verify_fetch_result actual.err
592 '
593
594 test_expect_success "'--recurse-submodules=on-demand' stops when no new submodule commits are found in the superproject (and ignores config)" '
595 add_submodule_commits &&
596 echo a >> file &&
597 git add file &&
598 git commit -m "new file" &&
599 new_head=$(git rev-parse --short HEAD) &&
600 write_expected_super $new_head &&
601 rm expect.err.sub &&
602 rm expect.err.deep &&
603 (
604 cd downstream &&
605 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
606 ) &&
607 test_must_be_empty actual.out &&
608 verify_fetch_result actual.err
609 '
610
611 test_expect_success "'fetch.recurseSubmodules=on-demand' overrides global config" '
612 (
613 cd downstream &&
614 git fetch --recurse-submodules
615 ) &&
616 add_submodule_commits &&
617 git config --global fetch.recurseSubmodules false &&
618 git add submodule &&
619 git commit -m "new submodule" &&
620 new_head=$(git rev-parse --short HEAD) &&
621 write_expected_super $new_head &&
622 rm expect.err.deep &&
623 (
624 cd downstream &&
625 git config fetch.recurseSubmodules on-demand &&
626 git fetch >../actual.out 2>../actual.err
627 ) &&
628 git config --global --unset fetch.recurseSubmodules &&
629 (
630 cd downstream &&
631 git config --unset fetch.recurseSubmodules
632 ) &&
633 test_must_be_empty actual.out &&
634 verify_fetch_result actual.err
635 '
636
637 test_expect_success "'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
638 (
639 cd downstream &&
640 git fetch --recurse-submodules
641 ) &&
642 add_submodule_commits &&
643 git config fetch.recurseSubmodules false &&
644 git add submodule &&
645 git commit -m "new submodule" &&
646 new_head=$(git rev-parse --short HEAD) &&
647 write_expected_super $new_head &&
648 rm expect.err.deep &&
649 (
650 cd downstream &&
651 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
652 git fetch >../actual.out 2>../actual.err
653 ) &&
654 git config --unset fetch.recurseSubmodules &&
655 (
656 cd downstream &&
657 git config --unset submodule.submodule.fetchRecurseSubmodules
658 ) &&
659 test_must_be_empty actual.out &&
660 verify_fetch_result actual.err
661 '
662
663 test_expect_success "don't fetch submodule when newly recorded commits are already present" '
664 (
665 cd submodule &&
666 git checkout -q HEAD^^
667 ) &&
668 git add submodule &&
669 git commit -m "submodule rewound" &&
670 new_head=$(git rev-parse --short HEAD) &&
671 write_expected_super $new_head &&
672 rm expect.err.sub &&
673 # This file does not exist, but rm -f for readability
674 rm -f expect.err.deep &&
675 (
676 cd downstream &&
677 git fetch >../actual.out 2>../actual.err
678 ) &&
679 test_must_be_empty actual.out &&
680 verify_fetch_result actual.err &&
681 (
682 cd submodule &&
683 git checkout -q sub
684 )
685 '
686
687 test_expect_success "'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
688 (
689 cd downstream &&
690 git fetch --recurse-submodules
691 ) &&
692 add_submodule_commits &&
693 git add submodule &&
694 git rm .gitmodules &&
695 git commit -m "new submodule without .gitmodules" &&
696 new_head=$(git rev-parse --short HEAD) &&
697 write_expected_super $new_head &&
698 rm expect.err.deep &&
699 (
700 cd downstream &&
701 rm .gitmodules &&
702 git config fetch.recurseSubmodules on-demand &&
703 # fake submodule configuration to avoid skipping submodule handling
704 git config -f .gitmodules submodule.fake.path fake &&
705 git config -f .gitmodules submodule.fake.url fakeurl &&
706 git add .gitmodules &&
707 git config --unset submodule.submodule.url &&
708 git fetch >../actual.out 2>../actual.err &&
709 # cleanup
710 git config --unset fetch.recurseSubmodules &&
711 git reset --hard
712 ) &&
713 test_must_be_empty actual.out &&
714 verify_fetch_result actual.err &&
715 git checkout HEAD^ -- .gitmodules &&
716 git add .gitmodules &&
717 git commit -m "new submodule restored .gitmodules"
718 '
719
720 test_expect_success 'fetching submodules respects parallel settings' '
721 git config fetch.recurseSubmodules true &&
722 test_when_finished "rm -f downstream/trace.out" &&
723 (
724 cd downstream &&
725 GIT_TRACE=$(pwd)/trace.out git fetch &&
726 test_grep "1 tasks" trace.out &&
727 >trace.out &&
728
729 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 7 &&
730 test_grep "7 tasks" trace.out &&
731 >trace.out &&
732
733 git config submodule.fetchJobs 8 &&
734 GIT_TRACE=$(pwd)/trace.out git fetch &&
735 test_grep "8 tasks" trace.out &&
736 >trace.out &&
737
738 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 9 &&
739 test_grep "9 tasks" trace.out &&
740 >trace.out &&
741
742 GIT_TRACE=$(pwd)/trace.out git -c submodule.fetchJobs=0 fetch &&
743 test_grep "preparing to run up to [0-9]* tasks" trace.out &&
744 test_grep ! "up to 0 tasks" trace.out &&
745 >trace.out
746 )
747 '
748
749 test_expect_success 'fetching submodule into a broken repository' '
750 # Prepare src and src/sub nested in it
751 git init src &&
752 (
753 cd src &&
754 git init sub &&
755 git -C sub commit --allow-empty -m "initial in sub" &&
756 git submodule add -- ./sub sub &&
757 git commit -m "initial in top"
758 ) &&
759
760 # Clone the old-fashoned way
761 git clone src dst &&
762 git -C dst clone ../src/sub sub &&
763
764 # Make sure that old-fashoned layout is still supported
765 git -C dst status &&
766
767 # "diff" would find no change
768 git -C dst diff --exit-code &&
769
770 # Recursive-fetch works fine
771 git -C dst fetch --recurse-submodules &&
772
773 # Break the receiving submodule
774 rm -r dst/sub/.git/objects &&
775
776 # NOTE: without the fix the following tests will recurse forever!
777 # They should terminate with an error.
778
779 test_must_fail git -C dst status &&
780 test_must_fail git -C dst diff &&
781 test_must_fail git -C dst fetch --recurse-submodules
782 '
783
784 test_expect_success "fetch new commits when submodule got renamed" '
785 git clone . downstream_rename &&
786 (
787 cd downstream_rename &&
788 git submodule update --init --recursive &&
789 git checkout -b rename &&
790 git mv submodule submodule_renamed &&
791 (
792 cd submodule_renamed &&
793 git checkout -b rename_sub &&
794 echo a >a &&
795 git add a &&
796 git commit -ma &&
797 git push origin rename_sub &&
798 git rev-parse HEAD >../../expect
799 ) &&
800 git add submodule_renamed &&
801 git commit -m "update renamed submodule" &&
802 git push origin rename
803 ) &&
804 (
805 cd downstream &&
806 git fetch --recurse-submodules=on-demand &&
807 (
808 cd submodule &&
809 git rev-parse origin/rename_sub >../../actual
810 )
811 ) &&
812 test_cmp expect actual
813 '
814
815 test_expect_success "fetch new submodule commits on-demand outside standard refspec" '
816 # add a second submodule and ensure it is around in downstream first
817 git clone submodule sub1 &&
818 git submodule add ./sub1 &&
819 git commit -m "adding a second submodule" &&
820 git -C downstream pull &&
821 git -C downstream submodule update --init --recursive &&
822
823 git checkout --detach &&
824
825 C=$(git -C submodule commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
826 git -C submodule update-ref refs/changes/1 $C &&
827 git update-index --cacheinfo 160000 $C submodule &&
828 test_tick &&
829
830 D=$(git -C sub1 commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
831 git -C sub1 update-ref refs/changes/2 $D &&
832 git update-index --cacheinfo 160000 $D sub1 &&
833
834 git commit -m "updated submodules outside of refs/heads" &&
835 E=$(git rev-parse HEAD) &&
836 git update-ref refs/changes/3 $E &&
837 FETCH_TRACE="$(pwd)/trace.out" &&
838 test_when_finished "rm -f \"$FETCH_TRACE\"" &&
839 (
840 cd downstream &&
841 GIT_TRACE="$FETCH_TRACE" git fetch --recurse-submodules origin \
842 refs/changes/3:refs/heads/my_branch &&
843 git -C submodule cat-file -t $C &&
844 git -C sub1 cat-file -t $D &&
845 test_grep "trace: built-in: git submodule--helper get-default-remote sub1" \
846 "$FETCH_TRACE" &&
847 test_grep "trace: built-in: git fetch .* --submodule-prefix=sub1/ origin" \
848 "$FETCH_TRACE" &&
849 git checkout --recurse-submodules FETCH_HEAD
850 )
851 '
852
853 test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD' '
854 # depends on the previous test for setup
855
856 C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
857 git -C submodule update-ref refs/changes/4 $C &&
858 git update-index --cacheinfo 160000 $C submodule &&
859 test_tick &&
860
861 D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
862 git -C sub1 update-ref refs/changes/5 $D &&
863 git update-index --cacheinfo 160000 $D sub1 &&
864
865 git commit -m "updated submodules outside of refs/heads" &&
866 E=$(git rev-parse HEAD) &&
867 git update-ref refs/changes/6 $E &&
868 (
869 cd downstream &&
870 git fetch --recurse-submodules origin refs/changes/6 &&
871 git -C submodule cat-file -t $C &&
872 git -C sub1 cat-file -t $D &&
873 git checkout --recurse-submodules FETCH_HEAD
874 )
875 '
876
877 test_expect_success 'fetch new submodule commits on-demand without .gitmodules entry' '
878 # depends on the previous test for setup
879
880 git config -f .gitmodules --remove-section submodule.sub1 &&
881 git add .gitmodules &&
882 git commit -m "delete gitmodules file" &&
883 git checkout -B super &&
884 git -C downstream fetch &&
885 git -C downstream checkout origin/super &&
886
887 C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
888 git -C submodule update-ref refs/changes/7 $C &&
889 git update-index --cacheinfo 160000 $C submodule &&
890 test_tick &&
891
892 D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
893 git -C sub1 update-ref refs/changes/8 $D &&
894 git update-index --cacheinfo 160000 $D sub1 &&
895
896 git commit -m "updated submodules outside of refs/heads" &&
897 E=$(git rev-parse HEAD) &&
898 git update-ref refs/changes/9 $E &&
899 (
900 cd downstream &&
901 git fetch --recurse-submodules origin refs/changes/9 &&
902 git -C submodule cat-file -t $C &&
903 git -C sub1 cat-file -t $D &&
904 git checkout --recurse-submodules FETCH_HEAD
905 )
906 '
907
908 test_expect_success 'fetch new submodule commit intermittently referenced by superproject' '
909 # depends on the previous test for setup
910
911 D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
912 E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
913 F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
914
915 git -C sub1 update-ref refs/changes/10 $D &&
916 git update-index --cacheinfo 160000 $D sub1 &&
917 git commit -m "updated submodules outside of refs/heads" &&
918
919 git -C sub1 update-ref refs/changes/11 $E &&
920 git update-index --cacheinfo 160000 $E sub1 &&
921 git commit -m "updated submodules outside of refs/heads" &&
922
923 git -C sub1 update-ref refs/changes/12 $F &&
924 git update-index --cacheinfo 160000 $F sub1 &&
925 git commit -m "updated submodules outside of refs/heads" &&
926
927 G=$(git rev-parse HEAD) &&
928 git update-ref refs/changes/13 $G &&
929 (
930 cd downstream &&
931 git fetch --recurse-submodules origin refs/changes/13 &&
932
933 git -C sub1 cat-file -t $D &&
934 git -C sub1 cat-file -t $E &&
935 git -C sub1 cat-file -t $F
936 )
937 '
938
939 test_expect_success 'fetch new submodule commits on-demand outside standard refspec with custom remote name' '
940 # depends on the previous test for setup
941
942 # Rename the remote in sub1 from "origin" to "custom_remote"
943 git -C downstream/sub1 remote rename origin custom_remote &&
944
945 # Create new commits in the original submodules
946 C=$(git -C submodule commit-tree \
947 -m "change outside refs/heads for custom remote" HEAD^{tree}) &&
948 git -C submodule update-ref refs/changes/custom1 $C &&
949 git update-index --cacheinfo 160000 $C submodule &&
950 test_tick &&
951
952 D=$(git -C sub1 commit-tree \
953 -m "change outside refs/heads for custom remote" HEAD^{tree}) &&
954 git -C sub1 update-ref refs/changes/custom2 $D &&
955 git update-index --cacheinfo 160000 $D sub1 &&
956
957 git commit \
958 -m "updated submodules outside of refs/heads for custom remote" &&
959 E=$(git rev-parse HEAD) &&
960 git update-ref refs/changes/custom3 $E &&
961 FETCH_TRACE="$(pwd)/trace.out" &&
962 test_when_finished "rm -f \"$FETCH_TRACE\"" &&
963 (
964 cd downstream &&
965 GIT_TRACE="$FETCH_TRACE" git fetch --recurse-submodules origin \
966 refs/changes/custom3:refs/heads/my_other_branch &&
967 git -C submodule cat-file -t $C &&
968 git -C sub1 cat-file -t $D &&
969 test_grep "trace: built-in: git submodule--helper get-default-remote sub1" \
970 "$FETCH_TRACE" &&
971 test_grep "trace: built-in: git fetch .* --submodule-prefix=sub1/ custom_remote $D" \
972 "$FETCH_TRACE" &&
973 git checkout --recurse-submodules FETCH_HEAD
974 )
975 '
976
977 test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD from custom remote' '
978 # depends on the previous test for setup
979
980 C=$(git -C submodule commit-tree -m "another change outside refs/heads for custom remote" HEAD^{tree}) &&
981 git -C submodule update-ref refs/changes/custom4 $C &&
982 git update-index --cacheinfo 160000 $C submodule &&
983 test_tick &&
984
985 D=$(git -C sub1 commit-tree -m "another change outside refs/heads for custom remote" HEAD^{tree}) &&
986 git -C sub1 update-ref refs/changes/custom5 $D &&
987 git update-index --cacheinfo 160000 $D sub1 &&
988
989 git commit -m "updated submodules outside of refs/heads" &&
990 E=$(git rev-parse HEAD) &&
991 git update-ref refs/changes/custom6 $E &&
992 (
993 cd downstream &&
994 git fetch --recurse-submodules origin refs/changes/custom6 &&
995 git -C submodule cat-file -t $C &&
996 git -C sub1 cat-file -t $D &&
997 git checkout --recurse-submodules FETCH_HEAD
998 )
999 '
1000
1001 add_commit_push () {
1002 dir="$1" &&
1003 msg="$2" &&
1004 shift 2 &&
1005 git -C "$dir" add "$@" &&
1006 git -C "$dir" commit -a -m "$msg" &&
1007 git -C "$dir" push
1008 }
1009
1010 compare_refs_in_dir () {
1011 fail= &&
1012 if test "x$1" = 'x!'
1013 then
1014 fail='!' &&
1015 shift
1016 fi &&
1017 git -C "$1" rev-parse --verify "$2" >expect &&
1018 git -C "$3" rev-parse --verify "$4" >actual &&
1019 eval $fail test_cmp expect actual
1020 }
1021
1022
1023 test_expect_success 'setup nested submodule fetch test' '
1024 # does not depend on any previous test setups
1025
1026 for repo in outer middle inner
1027 do
1028 git init --bare $repo &&
1029 git clone $repo ${repo}_content &&
1030 echo "$repo" >"${repo}_content/file" &&
1031 add_commit_push ${repo}_content "initial" file ||
1032 return 1
1033 done &&
1034
1035 git clone outer A &&
1036 git -C A submodule add "$pwd/middle" &&
1037 git -C A/middle/ submodule add "$pwd/inner" &&
1038 add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
1039 add_commit_push A/ "adding middle sub" .gitmodules middle &&
1040
1041 git clone outer B &&
1042 git -C B/ submodule update --init middle &&
1043
1044 compare_refs_in_dir A HEAD B HEAD &&
1045 compare_refs_in_dir A/middle HEAD B/middle HEAD &&
1046 test_path_is_file B/file &&
1047 test_path_is_file B/middle/file &&
1048 test_path_is_missing B/middle/inner/file &&
1049
1050 echo "change on inner repo of A" >"A/middle/inner/file" &&
1051 add_commit_push A/middle/inner "change on inner" file &&
1052 add_commit_push A/middle "change on inner" inner &&
1053 add_commit_push A "change on inner" middle
1054 '
1055
1056 test_expect_success 'fetching a superproject containing an uninitialized sub/sub project' '
1057 # depends on previous test for setup
1058
1059 git -C B/ fetch &&
1060 compare_refs_in_dir A origin/HEAD B origin/HEAD
1061 '
1062
1063 fetch_with_recursion_abort () {
1064 # In a regression the following git call will run into infinite recursion.
1065 # To handle that, we connect the sed command to the git call by a pipe
1066 # so that sed can kill the infinite recursion when detected.
1067 # The recursion creates git output like:
1068 # Fetching submodule sub
1069 # Fetching submodule sub/sub <-- [1]
1070 # Fetching submodule sub/sub/sub
1071 # ...
1072 # [1] sed will stop reading and cause git to eventually stop and die
1073
1074 git -C "$1" fetch --recurse-submodules 2>&1 |
1075 sed "/Fetching submodule $2[^$]/q" >out &&
1076 ! grep "Fetching submodule $2[^$]" out
1077 }
1078
1079 test_expect_success 'setup recursive fetch with uninit submodule' '
1080 # does not depend on any previous test setups
1081
1082 test_create_repo super &&
1083 test_commit -C super initial &&
1084 test_create_repo sub &&
1085 test_commit -C sub initial &&
1086 git -C sub rev-parse HEAD >expect &&
1087
1088 git -C super submodule add ../sub &&
1089 git -C super commit -m "add sub" &&
1090
1091 git clone super superclone &&
1092 git -C superclone submodule status >out &&
1093 sed -e "s/^-//" -e "s/ sub.*$//" out >actual &&
1094 test_cmp expect actual
1095 '
1096
1097 test_expect_success 'recursive fetch with uninit submodule' '
1098 # depends on previous test for setup
1099
1100 fetch_with_recursion_abort superclone sub &&
1101 git -C superclone submodule status >out &&
1102 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1103 test_cmp expect actual
1104 '
1105
1106 test_expect_success 'recursive fetch after deinit a submodule' '
1107 # depends on previous test for setup
1108
1109 git -C superclone submodule update --init sub &&
1110 git -C superclone submodule deinit -f sub &&
1111
1112 fetch_with_recursion_abort superclone sub &&
1113 git -C superclone submodule status >out &&
1114 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1115 test_cmp expect actual
1116 '
1117
1118 test_expect_success 'setup repo with upstreams that share a submodule name' '
1119 mkdir same-name-1 &&
1120 (
1121 cd same-name-1 &&
1122 git init -b main &&
1123 test_commit --no-tag a
1124 ) &&
1125 git clone same-name-1 same-name-2 &&
1126 # same-name-1 and same-name-2 both add a submodule with the
1127 # name "submodule"
1128 (
1129 cd same-name-1 &&
1130 mkdir submodule &&
1131 git -C submodule init -b main &&
1132 test_commit -C submodule --no-tag a1 &&
1133 git submodule add "$pwd/same-name-1/submodule" &&
1134 git add submodule &&
1135 git commit -m "super-a1"
1136 ) &&
1137 (
1138 cd same-name-2 &&
1139 mkdir submodule &&
1140 git -C submodule init -b main &&
1141 test_commit -C submodule --no-tag a2 &&
1142 git submodule add "$pwd/same-name-2/submodule" &&
1143 git add submodule &&
1144 git commit -m "super-a2"
1145 ) &&
1146 git clone same-name-1 -o same-name-1 same-name-downstream &&
1147 (
1148 cd same-name-downstream &&
1149 git remote add same-name-2 ../same-name-2 &&
1150 git fetch --all &&
1151 # init downstream with same-name-1
1152 git submodule update --init
1153 )
1154 '
1155
1156 test_expect_success 'fetch --recurse-submodules updates name-conflicted, populated submodule' '
1157 test_when_finished "git -C same-name-downstream checkout main" &&
1158 (
1159 cd same-name-1 &&
1160 test_commit -C submodule --no-tag b1 &&
1161 git add submodule &&
1162 git commit -m "super-b1"
1163 ) &&
1164 (
1165 cd same-name-2 &&
1166 test_commit -C submodule --no-tag b2 &&
1167 git add submodule &&
1168 git commit -m "super-b2"
1169 ) &&
1170 (
1171 cd same-name-downstream &&
1172 # even though the .gitmodules is correct, we cannot
1173 # fetch from same-name-2
1174 git checkout same-name-2/main &&
1175 git fetch --recurse-submodules same-name-1 &&
1176 test_must_fail git fetch --recurse-submodules same-name-2
1177 ) &&
1178 super_head1=$(git -C same-name-1 rev-parse HEAD) &&
1179 git -C same-name-downstream cat-file -e $super_head1 &&
1180
1181 super_head2=$(git -C same-name-2 rev-parse HEAD) &&
1182 git -C same-name-downstream cat-file -e $super_head2 &&
1183
1184 sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1185 git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
1186
1187 sub_head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1188 test_must_fail git -C same-name-downstream/submodule cat-file -e $sub_head2
1189 '
1190
1191 test_expect_success 'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
1192 (
1193 cd same-name-1 &&
1194 test_commit -C submodule --no-tag c1 &&
1195 git add submodule &&
1196 git commit -m "super-c1"
1197 ) &&
1198 (
1199 cd same-name-2 &&
1200 test_commit -C submodule --no-tag c2 &&
1201 git add submodule &&
1202 git commit -m "super-c2"
1203 ) &&
1204 (
1205 cd same-name-downstream &&
1206 git checkout main &&
1207 git rm .gitmodules &&
1208 git rm submodule &&
1209 git commit -m "no submodules" &&
1210 git fetch --recurse-submodules same-name-1
1211 ) &&
1212 head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1213 head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1214 (
1215 cd same-name-downstream/.git/modules/submodule &&
1216 # The submodule has core.worktree pointing to the "git
1217 # rm"-ed directory, overwrite the invalid value. See
1218 # comment in get_fetch_task_from_changed() for more
1219 # information.
1220 git --work-tree=. cat-file -e $head1 &&
1221 test_must_fail git --work-tree=. cat-file -e $head2
1222 )
1223 '
1224
1225 test_expect_success 'fetch --all with --recurse-submodules' '
1226 test_when_finished "rm -fr src_clone" &&
1227 git clone --recurse-submodules src src_clone &&
1228 (
1229 cd src_clone &&
1230 git config submodule.recurse true &&
1231 git config fetch.parallel 0 &&
1232 git fetch --all 2>../fetch-log
1233 ) &&
1234 grep "^Fetching submodule sub$" fetch-log >fetch-subs &&
1235 test_line_count = 1 fetch-subs
1236 '
1237
1238 test_expect_success 'fetch --all with --recurse-submodules with multiple' '
1239 test_when_finished "rm -fr src_clone" &&
1240 git clone --recurse-submodules src src_clone &&
1241 (
1242 cd src_clone &&
1243 git remote add secondary ../src &&
1244 git config submodule.recurse true &&
1245 git config fetch.parallel 0 &&
1246 git fetch --all 2>../fetch-log
1247 ) &&
1248 grep "Fetching submodule sub" fetch-log >fetch-subs &&
1249 test_line_count = 2 fetch-subs
1250 '
1251
1252 test_expect_success "fetch --all with --no-recurse-submodules only fetches superproject" '
1253 test_when_finished "rm -rf src_clone" &&
1254
1255 git clone --recurse-submodules src src_clone &&
1256 (
1257 cd src_clone &&
1258 git remote add secondary ../src &&
1259 git config submodule.recurse true &&
1260 git fetch --all --no-recurse-submodules 2>../fetch-log
1261 ) &&
1262 test_grep ! "Fetching submodule" fetch-log
1263 '
1264
1265 # Create an isolated environment for submodule fetch error tests.
1266 #
1267 # Sets up sub_bare (the submodule upstream), super_bare (the superproject
1268 # upstream), super_work (a working clone of super_bare with an initialized
1269 # submodule), and clone (a clone of super_bare with an initialized submodule
1270 # at a reachable commit). The caller can then create an unreachable commit
1271 # and push the superproject to put the clone one commit behind a state it
1272 # cannot fully fetch.
1273 #
1274 # Usage: create_err_env <envdir>
1275 create_err_env () {
1276 local envdir="$1" &&
1277 mkdir "$envdir" &&
1278
1279 git init --bare "$envdir/sub_bare" &&
1280 git clone "$envdir/sub_bare" "$envdir/sub_work" &&
1281 test_commit -C "$envdir/sub_work" "${envdir}_base" &&
1282 git -C "$envdir/sub_work" push &&
1283
1284 git init --bare "$envdir/super_bare" &&
1285 git clone "$envdir/super_bare" "$envdir/super_work" &&
1286 git -C "$envdir/super_work" submodule add \
1287 "$pwd/$envdir/sub_bare" sub &&
1288 git -C "$envdir/super_work" commit -m "add submodule" &&
1289 git -C "$envdir/super_work" push &&
1290
1291 git clone "$envdir/super_bare" "$envdir/clone" &&
1292 git -C "$envdir/clone" submodule update --init
1293 }
1294
1295 # Push a commit to <envdir>/super_bare that records a submodule SHA that is
1296 # present locally in super_work/sub but NOT pushed to sub_bare, making the
1297 # submodule commit unreachable from clone's sub remote.
1298 push_unreachable_commit () {
1299 local envdir="$1" &&
1300 git -C "$envdir/super_work/sub" commit --allow-empty -m "unreachable" &&
1301 git -C "$envdir/super_work" add sub &&
1302 git -C "$envdir/super_work" commit -m "point sub to unreachable commit" &&
1303 git -C "$envdir/super_work" push
1304 }
1305
1306 test_expect_success 'setup for submodule fetch error tests' '
1307 git config --global protocol.file.allow always
1308 '
1309
1310 test_expect_success 'fetch --recurse-submodules fails when submodule commit is unreachable (default)' '
1311 test_when_finished "rm -fr env_default" &&
1312 create_err_env env_default &&
1313 push_unreachable_commit env_default &&
1314 test_must_fail git -C env_default/clone fetch --recurse-submodules 2>err &&
1315 test_grep "Errors during submodule fetch" err
1316 '
1317
1318 test_expect_success 'fetch.submoduleErrors=warn: unreachable submodule commit is non-fatal' '
1319 test_when_finished "rm -fr env_warn_cfg" &&
1320 create_err_env env_warn_cfg &&
1321 push_unreachable_commit env_warn_cfg &&
1322 git -C env_warn_cfg/clone -c fetch.submoduleErrors=warn \
1323 fetch --recurse-submodules 2>err &&
1324 test_grep "Errors during submodule fetch" err
1325 '
1326
1327 test_expect_success '--submodule-errors=warn: unreachable submodule commit is non-fatal' '
1328 test_when_finished "rm -fr env_warn_cli" &&
1329 create_err_env env_warn_cli &&
1330 push_unreachable_commit env_warn_cli &&
1331 git -C env_warn_cli/clone fetch --recurse-submodules \
1332 --submodule-errors=warn 2>err &&
1333 test_grep "Errors during submodule fetch" err
1334 '
1335
1336 test_expect_success '--submodule-errors=fail: unreachable submodule commit is fatal' '
1337 test_when_finished "rm -fr env_fail_cli" &&
1338 create_err_env env_fail_cli &&
1339 push_unreachable_commit env_fail_cli &&
1340 test_must_fail git -C env_fail_cli/clone fetch --recurse-submodules \
1341 --submodule-errors=fail 2>err &&
1342 test_grep "Errors during submodule fetch" err
1343 '
1344
1345 test_expect_success 'fetch.submoduleErrors=warn does not suppress successful fetch' '
1346 # A new reachable submodule commit (pushed to sub_bare) should be
1347 # fetched without any error summary.
1348 test_when_finished "rm -fr env_ok" &&
1349 create_err_env env_ok &&
1350 test_commit -C env_ok/sub_work reachable_ok &&
1351 git -C env_ok/sub_work push &&
1352 git -C env_ok/super_work submodule update --remote &&
1353 git -C env_ok/super_work add sub &&
1354 git -C env_ok/super_work commit -m "point sub to reachable commit" &&
1355 git -C env_ok/super_work push &&
1356 git -C env_ok/clone -c fetch.submoduleErrors=warn \
1357 fetch --recurse-submodules 2>err &&
1358 test_grep ! "Errors during submodule fetch" err
1359 '
1360
1361 test_expect_success 'failed submodule fetch is fatal even when its commits are present locally' '
1362 # Create the same commit (unreferenced, via commit-tree with fixed
1363 # dates) in both super_work/sub and clone/sub, point the gitlink at
1364 # it, and break clone/sub'\''s remote. The commit exists in clone/sub
1365 # but is unreachable, so the submodule stays in the changed list; the
1366 # fetch failure must still be reported even though there is nothing
1367 # left to fetch by commit hash.
1368 test_when_finished "rm -fr env_phase1" &&
1369 create_err_env env_phase1 &&
1370 commit=$(GIT_AUTHOR_DATE="1234567890 +0000" \
1371 GIT_COMMITTER_DATE="1234567890 +0000" \
1372 git -C env_phase1/super_work/sub commit-tree \
1373 "HEAD^{tree}" -p HEAD -m present) &&
1374 present=$(GIT_AUTHOR_DATE="1234567890 +0000" \
1375 GIT_COMMITTER_DATE="1234567890 +0000" \
1376 git -C env_phase1/clone/sub commit-tree \
1377 "HEAD^{tree}" -p HEAD -m present) &&
1378 test "$commit" = "$present" &&
1379 git -C env_phase1/super_work/sub checkout "$commit" &&
1380 git -C env_phase1/super_work add sub &&
1381 git -C env_phase1/super_work commit -m "gitlink to locally-present commit" &&
1382 git -C env_phase1/super_work push &&
1383 git -C env_phase1/clone/sub remote set-url origin "$pwd/env_phase1/missing" &&
1384 test_must_fail git -C env_phase1/clone fetch --recurse-submodules 2>err &&
1385 test_grep "Errors during submodule fetch" err
1386 '
1387
1388 test_expect_success '--submodule-errors=warn is honored by fetch --all' '
1389 # A second remote forces fetch_multiple(), which hands the submodule
1390 # recursion off to per-remote child processes; the option must be
1391 # forwarded to them.
1392 test_when_finished "rm -fr env_all" &&
1393 create_err_env env_all &&
1394 push_unreachable_commit env_all &&
1395 git -C env_all/clone remote add second "$pwd/env_all/super_bare" &&
1396 git -C env_all/clone fetch --all --recurse-submodules \
1397 --submodule-errors=warn 2>err &&
1398 test_grep "Errors during submodule fetch" err
1399 '
1400
1401 test_expect_success '--submodule-errors=fail overrides warn config for fetch --all' '
1402 # The per-remote child processes re-read the repository config, so
1403 # the command-line override must be forwarded to them explicitly.
1404 test_when_finished "rm -fr env_override" &&
1405 create_err_env env_override &&
1406 push_unreachable_commit env_override &&
1407 git -C env_override/clone remote add second "$pwd/env_override/super_bare" &&
1408 git -C env_override/clone config fetch.submoduleErrors warn &&
1409 test_must_fail git -C env_override/clone fetch --all --recurse-submodules \
1410 --submodule-errors=fail 2>err &&
1411 test_grep "Errors during submodule fetch" err
1412 '
1413
1414 test_expect_success 'fetch.submoduleErrors=warn: inaccessible submodule is non-fatal' '
1415 test_when_finished "rm -fr env_access" &&
1416 create_err_env env_access &&
1417 rm env_access/clone/sub/.git &&
1418 rm -r env_access/clone/.git/modules/sub &&
1419 git -C env_access/clone -c fetch.submoduleErrors=warn \
1420 fetch --recurse-submodules 2>err &&
1421 test_grep "Could not access submodule" err &&
1422 test_must_fail git -C env_access/clone fetch --recurse-submodules 2>err &&
1423 test_grep "Could not access submodule" err
1424 '
1425
1426 test_done