clone_submodule: avoid using `access()` on directories
[git.git] / t / t5526-fetch-submodules.sh
blobb9546ef8e5e5cd3a734228c4d65ffdb59f568684
1 #!/bin/sh
2 # Copyright (c) 2010, Jens Lehmann
4 test_description='Recursive "git fetch" for submodules'
6 GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
7 export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
9 . ./test-lib.sh
11 pwd=$(pwd)
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
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
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
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
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 () {
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 ) &&
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
76 # For each superproject in the test setup, update its submodule, add the
77 # submodule and create a new commit with the submodule change.
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 () {
83 cd submodule &&
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
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.
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
129 test_expect_success setup '
130 git config --global protocol.file.allow always &&
131 mkdir deepsubmodule &&
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 &&
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 &&
155 cd downstream &&
156 git submodule update --init --recursive
160 test_expect_success "fetch --recurse-submodules recurses into submodules" '
161 add_submodule_commits &&
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
170 test_expect_success "submodule.recurse option triggers recursive fetch" '
171 add_submodule_commits &&
173 cd downstream &&
174 git -c submodule.recurse fetch >../actual.out 2>../actual.err
175 ) &&
176 test_must_be_empty actual.out &&
177 verify_fetch_result actual.err
180 test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" '
181 test_when_finished "rm -f trace.out" &&
182 add_submodule_commits &&
184 cd downstream &&
185 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
186 ) &&
187 test_must_be_empty actual.out &&
188 verify_fetch_result actual.err &&
189 grep "2 tasks" trace.out
192 test_expect_success "fetch alone only fetches superproject" '
193 add_submodule_commits &&
195 cd downstream &&
196 git fetch >../actual.out 2>../actual.err
197 ) &&
198 test_must_be_empty actual.out &&
199 test_must_be_empty actual.err
202 test_expect_success "fetch --no-recurse-submodules only fetches superproject" '
204 cd downstream &&
205 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
206 ) &&
207 test_must_be_empty actual.out &&
208 test_must_be_empty actual.err
211 test_expect_success "using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
213 cd downstream &&
214 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
215 git fetch >../actual.out 2>../actual.err
216 ) &&
217 test_must_be_empty actual.out &&
218 verify_fetch_result actual.err
221 test_expect_success "--no-recurse-submodules overrides .gitmodules config" '
222 add_submodule_commits &&
224 cd downstream &&
225 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
226 ) &&
227 test_must_be_empty actual.out &&
228 test_must_be_empty actual.err
231 test_expect_success "using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
233 cd downstream &&
234 git config submodule.submodule.fetchRecurseSubmodules false &&
235 git fetch >../actual.out 2>../actual.err
236 ) &&
237 test_must_be_empty actual.out &&
238 test_must_be_empty actual.err
241 test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
243 cd downstream &&
244 git fetch --recurse-submodules >../actual.out 2>../actual.err &&
245 git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
246 git config --unset submodule.submodule.fetchRecurseSubmodules
247 ) &&
248 test_must_be_empty actual.out &&
249 verify_fetch_result actual.err
252 test_expect_success "--quiet propagates to submodules" '
254 cd downstream &&
255 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
256 ) &&
257 test_must_be_empty actual.out &&
258 test_must_be_empty actual.err
261 test_expect_success "--quiet propagates to parallel submodules" '
263 cd downstream &&
264 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
265 ) &&
266 test_must_be_empty actual.out &&
267 test_must_be_empty actual.err
270 test_expect_success "--dry-run propagates to submodules" '
271 add_submodule_commits &&
273 cd downstream &&
274 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
275 ) &&
276 test_must_be_empty actual.out &&
277 verify_fetch_result actual.err
280 test_expect_success "Without --dry-run propagates to submodules" '
282 cd downstream &&
283 git fetch --recurse-submodules >../actual.out 2>../actual.err
284 ) &&
285 test_must_be_empty actual.out &&
286 verify_fetch_result actual.err
289 test_expect_success "recurseSubmodules=true propagates into submodules" '
290 add_submodule_commits &&
292 cd downstream &&
293 git config fetch.recurseSubmodules true &&
294 git fetch >../actual.out 2>../actual.err
295 ) &&
296 test_must_be_empty actual.out &&
297 verify_fetch_result actual.err
300 test_expect_success "--recurse-submodules overrides config in submodule" '
301 add_submodule_commits &&
303 cd downstream &&
305 cd submodule &&
306 git config fetch.recurseSubmodules false
307 ) &&
308 git fetch --recurse-submodules >../actual.out 2>../actual.err
309 ) &&
310 test_must_be_empty actual.out &&
311 verify_fetch_result actual.err
314 test_expect_success "--no-recurse-submodules overrides config setting" '
315 add_submodule_commits &&
317 cd downstream &&
318 git config fetch.recurseSubmodules true &&
319 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
320 ) &&
321 test_must_be_empty actual.out &&
322 test_must_be_empty actual.err
325 test_expect_success "Recursion doesn't happen when no new commits are fetched in the superproject" '
327 cd downstream &&
329 cd submodule &&
330 git config --unset fetch.recurseSubmodules
331 ) &&
332 git config --unset fetch.recurseSubmodules &&
333 git fetch >../actual.out 2>../actual.err
334 ) &&
335 test_must_be_empty actual.out &&
336 test_must_be_empty actual.err
339 test_expect_success "Recursion stops when no new submodule commits are fetched" '
340 git add submodule &&
341 git commit -m "new submodule" &&
342 new_head=$(git rev-parse --short HEAD) &&
343 write_expected_super $new_head &&
344 rm expect.err.deep &&
346 cd downstream &&
347 git fetch >../actual.out 2>../actual.err
348 ) &&
349 verify_fetch_result actual.err &&
350 test_must_be_empty actual.out
353 test_expect_success "Recursion doesn't happen when new superproject commits don't change any submodules" '
354 add_submodule_commits &&
355 echo a > file &&
356 git add file &&
357 git commit -m "new file" &&
358 new_head=$(git rev-parse --short HEAD) &&
359 write_expected_super $new_head &&
360 rm expect.err.sub &&
361 rm expect.err.deep &&
363 cd downstream &&
364 git fetch >../actual.out 2>../actual.err
365 ) &&
366 test_must_be_empty actual.out &&
367 verify_fetch_result actual.err
370 test_expect_success "Recursion picks up config in submodule" '
372 cd downstream &&
373 git fetch --recurse-submodules &&
375 cd submodule &&
376 git config fetch.recurseSubmodules true
378 ) &&
379 add_submodule_commits &&
380 git add submodule &&
381 git commit -m "new submodule" &&
382 new_head=$(git rev-parse --short HEAD) &&
383 write_expected_super $new_head &&
385 cd downstream &&
386 git fetch >../actual.out 2>../actual.err &&
388 cd submodule &&
389 git config --unset fetch.recurseSubmodules
391 ) &&
392 verify_fetch_result actual.err &&
393 test_must_be_empty actual.out
396 test_expect_success "Recursion picks up all submodules when necessary" '
397 add_submodule_commits &&
398 add_superproject_commits &&
400 cd downstream &&
401 git fetch >../actual.out 2>../actual.err
402 ) &&
403 verify_fetch_result actual.err &&
404 test_must_be_empty actual.out
407 test_expect_success "'--recurse-submodules=on-demand' doesn't recurse when no new commits are fetched in the superproject (and ignores config)" '
408 add_submodule_commits &&
410 cd downstream &&
411 git config fetch.recurseSubmodules true &&
412 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
413 git config --unset fetch.recurseSubmodules
414 ) &&
415 test_must_be_empty actual.out &&
416 test_must_be_empty actual.err
419 test_expect_success "'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
420 add_submodule_commits &&
421 add_superproject_commits &&
423 cd downstream &&
424 git config fetch.recurseSubmodules false &&
426 cd submodule &&
427 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
428 ) &&
429 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
430 git config --unset fetch.recurseSubmodules &&
432 cd submodule &&
433 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
435 ) &&
436 test_must_be_empty actual.out &&
437 verify_fetch_result actual.err
440 # These tests verify that we can fetch submodules that aren't in the
441 # index.
443 # First, test the simple case where the index is empty and we only fetch
444 # submodules that are not in the index.
445 test_expect_success 'setup downstream branch without submodules' '
447 cd downstream &&
448 git checkout --recurse-submodules -b no-submodules &&
449 git rm .gitmodules &&
450 git rm submodule &&
451 git commit -m "no submodules" &&
452 git checkout --recurse-submodules super
456 test_expect_success "'--recurse-submodules=on-demand' should fetch submodule commits if the submodule is changed but the index has no submodules" '
457 add_submodule_commits &&
458 add_superproject_commits &&
459 # Fetch the new superproject commit
461 cd downstream &&
462 git switch --recurse-submodules no-submodules &&
463 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
464 ) &&
465 super_head=$(git rev-parse --short HEAD) &&
466 sub_head=$(git -C submodule rev-parse --short HEAD) &&
467 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
469 # assert that these are fetched from commits, not the index
470 write_expected_sub $sub_head $super_head &&
471 write_expected_deep $deep_head $sub_head &&
473 test_must_be_empty actual.out &&
474 verify_fetch_result actual.err
477 test_expect_success "'--recurse-submodules' should fetch submodule commits if the submodule is changed but the index has no submodules" '
478 add_submodule_commits &&
479 add_superproject_commits &&
480 # Fetch the new superproject commit
482 cd downstream &&
483 git switch --recurse-submodules no-submodules &&
484 git fetch --recurse-submodules >../actual.out 2>../actual.err
485 ) &&
486 super_head=$(git rev-parse --short HEAD) &&
487 sub_head=$(git -C submodule rev-parse --short HEAD) &&
488 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
490 # assert that these are fetched from commits, not the index
491 write_expected_sub $sub_head $super_head &&
492 write_expected_deep $deep_head $sub_head &&
494 test_must_be_empty actual.out &&
495 verify_fetch_result actual.err
498 test_expect_success "'--recurse-submodules' should ignore changed, inactive submodules" '
499 add_submodule_commits &&
500 add_superproject_commits &&
502 # Fetch the new superproject commit
504 cd downstream &&
505 git switch --recurse-submodules no-submodules &&
506 git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
507 ) &&
508 test_must_be_empty actual.out &&
509 super_head=$(git rev-parse --short HEAD) &&
510 write_expected_super $super_head &&
511 # Neither should be fetched because the submodule is inactive
512 rm expect.err.sub &&
513 rm expect.err.deep &&
514 verify_fetch_result actual.err
517 # Now that we know we can fetch submodules that are not in the index,
518 # test that we can fetch index and non-index submodules in the same
519 # operation.
520 test_expect_success 'setup downstream branch with other submodule' '
521 mkdir submodule2 &&
523 cd submodule2 &&
524 git init &&
525 echo sub2content >sub2file &&
526 git add sub2file &&
527 git commit -a -m new &&
528 git branch -M sub2
529 ) &&
530 git checkout -b super-sub2-only &&
531 git submodule add "$pwd/submodule2" submodule2 &&
532 git commit -m "add sub2" &&
533 git checkout super &&
535 cd downstream &&
536 git fetch --recurse-submodules origin &&
537 git checkout super-sub2-only &&
538 # Explicitly run "git submodule update" because sub2 is new
539 # and has not been cloned.
540 git submodule update --init &&
541 git checkout --recurse-submodules super
545 test_expect_success "'--recurse-submodules' should fetch submodule commits in changed submodules and the index" '
546 test_when_finished "rm expect.err.sub2" &&
547 # Create new commit in origin/super
548 add_submodule_commits &&
549 add_superproject_commits &&
551 # Create new commit in origin/super-sub2-only
552 git checkout super-sub2-only &&
554 cd submodule2 &&
555 test_commit --no-tag foo
556 ) &&
557 git add submodule2 &&
558 git commit -m "new submodule2" &&
560 git checkout super &&
562 cd downstream &&
563 git fetch --recurse-submodules >../actual.out 2>../actual.err
564 ) &&
565 test_must_be_empty actual.out &&
566 sub2_head=$(git -C submodule2 rev-parse --short HEAD) &&
567 super_head=$(git rev-parse --short super) &&
568 super_sub2_only_head=$(git rev-parse --short super-sub2-only) &&
569 write_expected_sub2 $sub2_head $super_sub2_only_head &&
571 # write_expected_super cannot handle >1 branch. Since this is a
572 # one-off, construct expect.err.super manually.
573 cat >"$pwd/expect.err.super" <<-EOF &&
574 From $pwd/.
575 OLD_HEAD..$super_head super -> origin/super
576 OLD_HEAD..$super_sub2_only_head super-sub2-only -> origin/super-sub2-only
578 verify_fetch_result actual.err
581 test_expect_success "'--recurse-submodules=on-demand' stops when no new submodule commits are found in the superproject (and ignores config)" '
582 add_submodule_commits &&
583 echo a >> file &&
584 git add file &&
585 git commit -m "new file" &&
586 new_head=$(git rev-parse --short HEAD) &&
587 write_expected_super $new_head &&
588 rm expect.err.sub &&
589 rm expect.err.deep &&
591 cd downstream &&
592 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
593 ) &&
594 test_must_be_empty actual.out &&
595 verify_fetch_result actual.err
598 test_expect_success "'fetch.recurseSubmodules=on-demand' overrides global config" '
600 cd downstream &&
601 git fetch --recurse-submodules
602 ) &&
603 add_submodule_commits &&
604 git config --global fetch.recurseSubmodules false &&
605 git add submodule &&
606 git commit -m "new submodule" &&
607 new_head=$(git rev-parse --short HEAD) &&
608 write_expected_super $new_head &&
609 rm expect.err.deep &&
611 cd downstream &&
612 git config fetch.recurseSubmodules on-demand &&
613 git fetch >../actual.out 2>../actual.err
614 ) &&
615 git config --global --unset fetch.recurseSubmodules &&
617 cd downstream &&
618 git config --unset fetch.recurseSubmodules
619 ) &&
620 test_must_be_empty actual.out &&
621 verify_fetch_result actual.err
624 test_expect_success "'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
626 cd downstream &&
627 git fetch --recurse-submodules
628 ) &&
629 add_submodule_commits &&
630 git config fetch.recurseSubmodules false &&
631 git add submodule &&
632 git commit -m "new submodule" &&
633 new_head=$(git rev-parse --short HEAD) &&
634 write_expected_super $new_head &&
635 rm expect.err.deep &&
637 cd downstream &&
638 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
639 git fetch >../actual.out 2>../actual.err
640 ) &&
641 git config --unset fetch.recurseSubmodules &&
643 cd downstream &&
644 git config --unset submodule.submodule.fetchRecurseSubmodules
645 ) &&
646 test_must_be_empty actual.out &&
647 verify_fetch_result actual.err
650 test_expect_success "don't fetch submodule when newly recorded commits are already present" '
652 cd submodule &&
653 git checkout -q HEAD^^
654 ) &&
655 git add submodule &&
656 git commit -m "submodule rewound" &&
657 new_head=$(git rev-parse --short HEAD) &&
658 write_expected_super $new_head &&
659 rm expect.err.sub &&
660 # This file does not exist, but rm -f for readability
661 rm -f expect.err.deep &&
663 cd downstream &&
664 git fetch >../actual.out 2>../actual.err
665 ) &&
666 test_must_be_empty actual.out &&
667 verify_fetch_result actual.err &&
669 cd submodule &&
670 git checkout -q sub
674 test_expect_success "'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
676 cd downstream &&
677 git fetch --recurse-submodules
678 ) &&
679 add_submodule_commits &&
680 git add submodule &&
681 git rm .gitmodules &&
682 git commit -m "new submodule without .gitmodules" &&
683 new_head=$(git rev-parse --short HEAD) &&
684 write_expected_super $new_head &&
685 rm expect.err.deep &&
687 cd downstream &&
688 rm .gitmodules &&
689 git config fetch.recurseSubmodules on-demand &&
690 # fake submodule configuration to avoid skipping submodule handling
691 git config -f .gitmodules submodule.fake.path fake &&
692 git config -f .gitmodules submodule.fake.url fakeurl &&
693 git add .gitmodules &&
694 git config --unset submodule.submodule.url &&
695 git fetch >../actual.out 2>../actual.err &&
696 # cleanup
697 git config --unset fetch.recurseSubmodules &&
698 git reset --hard
699 ) &&
700 test_must_be_empty actual.out &&
701 verify_fetch_result actual.err &&
702 git checkout HEAD^ -- .gitmodules &&
703 git add .gitmodules &&
704 git commit -m "new submodule restored .gitmodules"
707 test_expect_success 'fetching submodules respects parallel settings' '
708 git config fetch.recurseSubmodules true &&
709 test_when_finished "rm -f downstream/trace.out" &&
711 cd downstream &&
712 GIT_TRACE=$(pwd)/trace.out git fetch &&
713 grep "1 tasks" trace.out &&
714 >trace.out &&
716 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 7 &&
717 grep "7 tasks" trace.out &&
718 >trace.out &&
720 git config submodule.fetchJobs 8 &&
721 GIT_TRACE=$(pwd)/trace.out git fetch &&
722 grep "8 tasks" trace.out &&
723 >trace.out &&
725 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 9 &&
726 grep "9 tasks" trace.out &&
727 >trace.out &&
729 GIT_TRACE=$(pwd)/trace.out git -c submodule.fetchJobs=0 fetch &&
730 grep "preparing to run up to [0-9]* tasks" trace.out &&
731 ! grep "up to 0 tasks" trace.out &&
732 >trace.out
736 test_expect_success 'fetching submodule into a broken repository' '
737 # Prepare src and src/sub nested in it
738 git init src &&
740 cd src &&
741 git init sub &&
742 git -C sub commit --allow-empty -m "initial in sub" &&
743 git submodule add -- ./sub sub &&
744 git commit -m "initial in top"
745 ) &&
747 # Clone the old-fashoned way
748 git clone src dst &&
749 git -C dst clone ../src/sub sub &&
751 # Make sure that old-fashoned layout is still supported
752 git -C dst status &&
754 # "diff" would find no change
755 git -C dst diff --exit-code &&
757 # Recursive-fetch works fine
758 git -C dst fetch --recurse-submodules &&
760 # Break the receiving submodule
761 rm -f dst/sub/.git/HEAD &&
763 # NOTE: without the fix the following tests will recurse forever!
764 # They should terminate with an error.
766 test_must_fail git -C dst status &&
767 test_must_fail git -C dst diff &&
768 test_must_fail git -C dst fetch --recurse-submodules
771 test_expect_success "fetch new commits when submodule got renamed" '
772 git clone . downstream_rename &&
774 cd downstream_rename &&
775 git submodule update --init --recursive &&
776 git checkout -b rename &&
777 git mv submodule submodule_renamed &&
779 cd submodule_renamed &&
780 git checkout -b rename_sub &&
781 echo a >a &&
782 git add a &&
783 git commit -ma &&
784 git push origin rename_sub &&
785 git rev-parse HEAD >../../expect
786 ) &&
787 git add submodule_renamed &&
788 git commit -m "update renamed submodule" &&
789 git push origin rename
790 ) &&
792 cd downstream &&
793 git fetch --recurse-submodules=on-demand &&
795 cd submodule &&
796 git rev-parse origin/rename_sub >../../actual
798 ) &&
799 test_cmp expect actual
802 test_expect_success "fetch new submodule commits on-demand outside standard refspec" '
803 # add a second submodule and ensure it is around in downstream first
804 git clone submodule sub1 &&
805 git submodule add ./sub1 &&
806 git commit -m "adding a second submodule" &&
807 git -C downstream pull &&
808 git -C downstream submodule update --init --recursive &&
810 git checkout --detach &&
812 C=$(git -C submodule commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
813 git -C submodule update-ref refs/changes/1 $C &&
814 git update-index --cacheinfo 160000 $C submodule &&
815 test_tick &&
817 D=$(git -C sub1 commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
818 git -C sub1 update-ref refs/changes/2 $D &&
819 git update-index --cacheinfo 160000 $D sub1 &&
821 git commit -m "updated submodules outside of refs/heads" &&
822 E=$(git rev-parse HEAD) &&
823 git update-ref refs/changes/3 $E &&
825 cd downstream &&
826 git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
827 git -C submodule cat-file -t $C &&
828 git -C sub1 cat-file -t $D &&
829 git checkout --recurse-submodules FETCH_HEAD
833 test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD' '
834 # depends on the previous test for setup
836 C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
837 git -C submodule update-ref refs/changes/4 $C &&
838 git update-index --cacheinfo 160000 $C submodule &&
839 test_tick &&
841 D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
842 git -C sub1 update-ref refs/changes/5 $D &&
843 git update-index --cacheinfo 160000 $D sub1 &&
845 git commit -m "updated submodules outside of refs/heads" &&
846 E=$(git rev-parse HEAD) &&
847 git update-ref refs/changes/6 $E &&
849 cd downstream &&
850 git fetch --recurse-submodules origin refs/changes/6 &&
851 git -C submodule cat-file -t $C &&
852 git -C sub1 cat-file -t $D &&
853 git checkout --recurse-submodules FETCH_HEAD
857 test_expect_success 'fetch new submodule commits on-demand without .gitmodules entry' '
858 # depends on the previous test for setup
860 git config -f .gitmodules --remove-section submodule.sub1 &&
861 git add .gitmodules &&
862 git commit -m "delete gitmodules file" &&
863 git checkout -B super &&
864 git -C downstream fetch &&
865 git -C downstream checkout origin/super &&
867 C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
868 git -C submodule update-ref refs/changes/7 $C &&
869 git update-index --cacheinfo 160000 $C submodule &&
870 test_tick &&
872 D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
873 git -C sub1 update-ref refs/changes/8 $D &&
874 git update-index --cacheinfo 160000 $D sub1 &&
876 git commit -m "updated submodules outside of refs/heads" &&
877 E=$(git rev-parse HEAD) &&
878 git update-ref refs/changes/9 $E &&
880 cd downstream &&
881 git fetch --recurse-submodules origin refs/changes/9 &&
882 git -C submodule cat-file -t $C &&
883 git -C sub1 cat-file -t $D &&
884 git checkout --recurse-submodules FETCH_HEAD
888 test_expect_success 'fetch new submodule commit intermittently referenced by superproject' '
889 # depends on the previous test for setup
891 D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
892 E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
893 F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
895 git -C sub1 update-ref refs/changes/10 $D &&
896 git update-index --cacheinfo 160000 $D sub1 &&
897 git commit -m "updated submodules outside of refs/heads" &&
899 git -C sub1 update-ref refs/changes/11 $E &&
900 git update-index --cacheinfo 160000 $E sub1 &&
901 git commit -m "updated submodules outside of refs/heads" &&
903 git -C sub1 update-ref refs/changes/12 $F &&
904 git update-index --cacheinfo 160000 $F sub1 &&
905 git commit -m "updated submodules outside of refs/heads" &&
907 G=$(git rev-parse HEAD) &&
908 git update-ref refs/changes/13 $G &&
910 cd downstream &&
911 git fetch --recurse-submodules origin refs/changes/13 &&
913 git -C sub1 cat-file -t $D &&
914 git -C sub1 cat-file -t $E &&
915 git -C sub1 cat-file -t $F
919 add_commit_push () {
920 dir="$1" &&
921 msg="$2" &&
922 shift 2 &&
923 git -C "$dir" add "$@" &&
924 git -C "$dir" commit -a -m "$msg" &&
925 git -C "$dir" push
928 compare_refs_in_dir () {
929 fail= &&
930 if test "x$1" = 'x!'
931 then
932 fail='!' &&
933 shift
934 fi &&
935 git -C "$1" rev-parse --verify "$2" >expect &&
936 git -C "$3" rev-parse --verify "$4" >actual &&
937 eval $fail test_cmp expect actual
941 test_expect_success 'setup nested submodule fetch test' '
942 # does not depend on any previous test setups
944 for repo in outer middle inner
946 git init --bare $repo &&
947 git clone $repo ${repo}_content &&
948 echo "$repo" >"${repo}_content/file" &&
949 add_commit_push ${repo}_content "initial" file ||
950 return 1
951 done &&
953 git clone outer A &&
954 git -C A submodule add "$pwd/middle" &&
955 git -C A/middle/ submodule add "$pwd/inner" &&
956 add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
957 add_commit_push A/ "adding middle sub" .gitmodules middle &&
959 git clone outer B &&
960 git -C B/ submodule update --init middle &&
962 compare_refs_in_dir A HEAD B HEAD &&
963 compare_refs_in_dir A/middle HEAD B/middle HEAD &&
964 test_path_is_file B/file &&
965 test_path_is_file B/middle/file &&
966 test_path_is_missing B/middle/inner/file &&
968 echo "change on inner repo of A" >"A/middle/inner/file" &&
969 add_commit_push A/middle/inner "change on inner" file &&
970 add_commit_push A/middle "change on inner" inner &&
971 add_commit_push A "change on inner" middle
974 test_expect_success 'fetching a superproject containing an uninitialized sub/sub project' '
975 # depends on previous test for setup
977 git -C B/ fetch &&
978 compare_refs_in_dir A origin/HEAD B origin/HEAD
981 fetch_with_recursion_abort () {
982 # In a regression the following git call will run into infinite recursion.
983 # To handle that, we connect the sed command to the git call by a pipe
984 # so that sed can kill the infinite recursion when detected.
985 # The recursion creates git output like:
986 # Fetching submodule sub
987 # Fetching submodule sub/sub <-- [1]
988 # Fetching submodule sub/sub/sub
989 # ...
990 # [1] sed will stop reading and cause git to eventually stop and die
992 git -C "$1" fetch --recurse-submodules 2>&1 |
993 sed "/Fetching submodule $2[^$]/q" >out &&
994 ! grep "Fetching submodule $2[^$]" out
997 test_expect_success 'setup recursive fetch with uninit submodule' '
998 # does not depend on any previous test setups
1000 test_create_repo super &&
1001 test_commit -C super initial &&
1002 test_create_repo sub &&
1003 test_commit -C sub initial &&
1004 git -C sub rev-parse HEAD >expect &&
1006 git -C super submodule add ../sub &&
1007 git -C super commit -m "add sub" &&
1009 git clone super superclone &&
1010 git -C superclone submodule status >out &&
1011 sed -e "s/^-//" -e "s/ sub.*$//" out >actual &&
1012 test_cmp expect actual
1015 test_expect_success 'recursive fetch with uninit submodule' '
1016 # depends on previous test for setup
1018 fetch_with_recursion_abort superclone sub &&
1019 git -C superclone submodule status >out &&
1020 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1021 test_cmp expect actual
1024 test_expect_success 'recursive fetch after deinit a submodule' '
1025 # depends on previous test for setup
1027 git -C superclone submodule update --init sub &&
1028 git -C superclone submodule deinit -f sub &&
1030 fetch_with_recursion_abort superclone sub &&
1031 git -C superclone submodule status >out &&
1032 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1033 test_cmp expect actual
1036 test_expect_success 'setup repo with upstreams that share a submodule name' '
1037 mkdir same-name-1 &&
1039 cd same-name-1 &&
1040 git init -b main &&
1041 test_commit --no-tag a
1042 ) &&
1043 git clone same-name-1 same-name-2 &&
1044 # same-name-1 and same-name-2 both add a submodule with the
1045 # name "submodule"
1047 cd same-name-1 &&
1048 mkdir submodule &&
1049 git -C submodule init -b main &&
1050 test_commit -C submodule --no-tag a1 &&
1051 git submodule add "$pwd/same-name-1/submodule" &&
1052 git add submodule &&
1053 git commit -m "super-a1"
1054 ) &&
1056 cd same-name-2 &&
1057 mkdir submodule &&
1058 git -C submodule init -b main &&
1059 test_commit -C submodule --no-tag a2 &&
1060 git submodule add "$pwd/same-name-2/submodule" &&
1061 git add submodule &&
1062 git commit -m "super-a2"
1063 ) &&
1064 git clone same-name-1 -o same-name-1 same-name-downstream &&
1066 cd same-name-downstream &&
1067 git remote add same-name-2 ../same-name-2 &&
1068 git fetch --all &&
1069 # init downstream with same-name-1
1070 git submodule update --init
1074 test_expect_success 'fetch --recurse-submodules updates name-conflicted, populated submodule' '
1075 test_when_finished "git -C same-name-downstream checkout main" &&
1077 cd same-name-1 &&
1078 test_commit -C submodule --no-tag b1 &&
1079 git add submodule &&
1080 git commit -m "super-b1"
1081 ) &&
1083 cd same-name-2 &&
1084 test_commit -C submodule --no-tag b2 &&
1085 git add submodule &&
1086 git commit -m "super-b2"
1087 ) &&
1089 cd same-name-downstream &&
1090 # even though the .gitmodules is correct, we cannot
1091 # fetch from same-name-2
1092 git checkout same-name-2/main &&
1093 git fetch --recurse-submodules same-name-1 &&
1094 test_must_fail git fetch --recurse-submodules same-name-2
1095 ) &&
1096 super_head1=$(git -C same-name-1 rev-parse HEAD) &&
1097 git -C same-name-downstream cat-file -e $super_head1 &&
1099 super_head2=$(git -C same-name-2 rev-parse HEAD) &&
1100 git -C same-name-downstream cat-file -e $super_head2 &&
1102 sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1103 git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
1105 sub_head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1106 test_must_fail git -C same-name-downstream/submodule cat-file -e $sub_head2
1109 test_expect_success 'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
1111 cd same-name-1 &&
1112 test_commit -C submodule --no-tag c1 &&
1113 git add submodule &&
1114 git commit -m "super-c1"
1115 ) &&
1117 cd same-name-2 &&
1118 test_commit -C submodule --no-tag c2 &&
1119 git add submodule &&
1120 git commit -m "super-c2"
1121 ) &&
1123 cd same-name-downstream &&
1124 git checkout main &&
1125 git rm .gitmodules &&
1126 git rm submodule &&
1127 git commit -m "no submodules" &&
1128 git fetch --recurse-submodules same-name-1
1129 ) &&
1130 head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1131 head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1133 cd same-name-downstream/.git/modules/submodule &&
1134 # The submodule has core.worktree pointing to the "git
1135 # rm"-ed directory, overwrite the invalid value. See
1136 # comment in get_fetch_task_from_changed() for more
1137 # information.
1138 git --work-tree=. cat-file -e $head1 &&
1139 test_must_fail git --work-tree=. cat-file -e $head2
1143 test_expect_success 'fetch --all with --recurse-submodules' '
1144 test_when_finished "rm -fr src_clone" &&
1145 git clone --recurse-submodules src src_clone &&
1147 cd src_clone &&
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 = 1 fetch-subs
1156 test_expect_success 'fetch --all with --recurse-submodules with multiple' '
1157 test_when_finished "rm -fr src_clone" &&
1158 git clone --recurse-submodules src src_clone &&
1160 cd src_clone &&
1161 git remote add secondary ../src &&
1162 git config submodule.recurse true &&
1163 git config fetch.parallel 0 &&
1164 git fetch --all 2>../fetch-log
1165 ) &&
1166 grep "Fetching submodule sub" fetch-log >fetch-subs &&
1167 test_line_count = 2 fetch-subs
1170 test_done