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
13 write_expected_sub
() {
16 cat >"$pwd/expect.err.sub" <<-EOF
17 Fetching submodule submodule${SUPER_HEAD:+ at commit $SUPER_HEAD}
19 OLD_HEAD..$NEW_HEAD sub -> origin/sub
23 write_expected_sub2
() {
26 cat >"$pwd/expect.err.sub2" <<-EOF
27 Fetching submodule submodule2${SUPER_HEAD:+ at commit $SUPER_HEAD}
29 OLD_HEAD..$NEW_HEAD sub2 -> origin/sub2
33 write_expected_deep
() {
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
43 write_expected_super
() {
45 cat >"$pwd/expect.err.super" <<-EOF
47 OLD_HEAD..$NEW_HEAD super -> origin/super
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
() {
58 echo new
>> subfile
&&
61 git commit
-m new subfile
&&
62 new_head
=$
(git rev-parse
--short HEAD
) &&
63 write_expected_sub
$new_head
67 echo new
>> deepsubfile
&&
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
() {
85 cd subdir
/deepsubmodule
&&
87 git checkout
-q FETCH_HEAD
89 git add subdir
/deepsubmodule
&&
90 git commit
-m "new deep 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
() {
108 rm -f expect.err.combined
&&
109 if test -f expect.err.super
111 cat expect.err.super
>>expect.err.combined
113 if test -f expect.err.sub
115 cat expect.err.sub
>>expect.err.combined
117 if test -f expect.err.deep
119 cat expect.err.deep
>>expect.err.combined
121 if test -f expect.err.sub2
123 cat expect.err.sub2
>>expect.err.combined
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 &&
135 echo deepsubcontent > deepsubfile &&
136 git add deepsubfile &&
137 git commit -m new deepsubfile &&
144 echo subcontent > subfile &&
146 git submodule add "$pwd/deepsubmodule" subdir/deepsubmodule &&
147 git commit -a -m new &&
150 git submodule add "$pwd/submodule" submodule &&
151 git commit -am initial &&
152 git branch -M super &&
153 git clone . downstream &&
156 git submodule update --init --recursive
160 test_expect_success
"fetch --recurse-submodules recurses into submodules" '
161 add_submodule_commits &&
164 git fetch --recurse-submodules >../actual.out 2>../actual.err
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 &&
174 git -c submodule.recurse fetch >../actual.out 2>../actual.err
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 add_submodule_commits &&
184 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
186 test_must_be_empty actual.out &&
187 verify_fetch_result actual.err &&
188 grep "2 tasks" trace.out
191 test_expect_success
"fetch alone only fetches superproject" '
192 add_submodule_commits &&
195 git fetch >../actual.out 2>../actual.err
197 test_must_be_empty actual.out &&
198 test_must_be_empty actual.err
201 test_expect_success
"fetch --no-recurse-submodules only fetches superproject" '
204 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
206 test_must_be_empty actual.out &&
207 test_must_be_empty actual.err
210 test_expect_success
"using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
213 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
214 git fetch >../actual.out 2>../actual.err
216 test_must_be_empty actual.out &&
217 verify_fetch_result actual.err
220 test_expect_success
"--no-recurse-submodules overrides .gitmodules config" '
221 add_submodule_commits &&
224 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
226 test_must_be_empty actual.out &&
227 test_must_be_empty actual.err
230 test_expect_success
"using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
233 git config submodule.submodule.fetchRecurseSubmodules false &&
234 git fetch >../actual.out 2>../actual.err
236 test_must_be_empty actual.out &&
237 test_must_be_empty actual.err
240 test_expect_success
"--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
243 git fetch --recurse-submodules >../actual.out 2>../actual.err &&
244 git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
245 git config --unset submodule.submodule.fetchRecurseSubmodules
247 test_must_be_empty actual.out &&
248 verify_fetch_result actual.err
251 test_expect_success
"--quiet propagates to submodules" '
254 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
256 test_must_be_empty actual.out &&
257 test_must_be_empty actual.err
260 test_expect_success
"--quiet propagates to parallel submodules" '
263 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
265 test_must_be_empty actual.out &&
266 test_must_be_empty actual.err
269 test_expect_success
"--dry-run propagates to submodules" '
270 add_submodule_commits &&
273 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
275 test_must_be_empty actual.out &&
276 verify_fetch_result actual.err
279 test_expect_success
"Without --dry-run propagates to submodules" '
282 git fetch --recurse-submodules >../actual.out 2>../actual.err
284 test_must_be_empty actual.out &&
285 verify_fetch_result actual.err
288 test_expect_success
"recurseSubmodules=true propagates into submodules" '
289 add_submodule_commits &&
292 git config fetch.recurseSubmodules true &&
293 git fetch >../actual.out 2>../actual.err
295 test_must_be_empty actual.out &&
296 verify_fetch_result actual.err
299 test_expect_success
"--recurse-submodules overrides config in submodule" '
300 add_submodule_commits &&
305 git config fetch.recurseSubmodules false
307 git fetch --recurse-submodules >../actual.out 2>../actual.err
309 test_must_be_empty actual.out &&
310 verify_fetch_result actual.err
313 test_expect_success
"--no-recurse-submodules overrides config setting" '
314 add_submodule_commits &&
317 git config fetch.recurseSubmodules true &&
318 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
320 test_must_be_empty actual.out &&
321 test_must_be_empty actual.err
324 test_expect_success
"Recursion doesn't happen when no new commits are fetched in the superproject" '
329 git config --unset fetch.recurseSubmodules
331 git config --unset fetch.recurseSubmodules &&
332 git fetch >../actual.out 2>../actual.err
334 test_must_be_empty actual.out &&
335 test_must_be_empty actual.err
338 test_expect_success
"Recursion stops when no new submodule commits are fetched" '
340 git commit -m "new submodule" &&
341 new_head=$(git rev-parse --short HEAD) &&
342 write_expected_super $new_head &&
343 rm expect.err.deep &&
346 git fetch >../actual.out 2>../actual.err
348 verify_fetch_result actual.err &&
349 test_must_be_empty actual.out
352 test_expect_success
"Recursion doesn't happen when new superproject commits don't change any submodules" '
353 add_submodule_commits &&
356 git commit -m "new file" &&
357 new_head=$(git rev-parse --short HEAD) &&
358 write_expected_super $new_head &&
360 rm expect.err.deep &&
363 git fetch >../actual.out 2>../actual.err
365 test_must_be_empty actual.out &&
366 verify_fetch_result actual.err
369 test_expect_success
"Recursion picks up config in submodule" '
372 git fetch --recurse-submodules &&
375 git config fetch.recurseSubmodules true
378 add_submodule_commits &&
380 git commit -m "new submodule" &&
381 new_head=$(git rev-parse --short HEAD) &&
382 write_expected_super $new_head &&
385 git fetch >../actual.out 2>../actual.err &&
388 git config --unset fetch.recurseSubmodules
391 verify_fetch_result actual.err &&
392 test_must_be_empty actual.out
395 test_expect_success
"Recursion picks up all submodules when necessary" '
396 add_submodule_commits &&
397 add_superproject_commits &&
400 git fetch >../actual.out 2>../actual.err
402 verify_fetch_result actual.err &&
403 test_must_be_empty actual.out
406 test_expect_success
"'--recurse-submodules=on-demand' doesn't recurse when no new commits are fetched in the superproject (and ignores config)" '
407 add_submodule_commits &&
410 git config fetch.recurseSubmodules true &&
411 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
412 git config --unset fetch.recurseSubmodules
414 test_must_be_empty actual.out &&
415 test_must_be_empty actual.err
418 test_expect_success
"'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
419 add_submodule_commits &&
420 add_superproject_commits &&
423 git config fetch.recurseSubmodules false &&
426 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
428 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
429 git config --unset fetch.recurseSubmodules &&
432 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
435 test_must_be_empty actual.out &&
436 verify_fetch_result actual.err
439 # These tests verify that we can fetch submodules that aren't in the
442 # First, test the simple case where the index is empty and we only fetch
443 # submodules that are not in the index.
444 test_expect_success
'setup downstream branch without submodules' '
447 git checkout --recurse-submodules -b no-submodules &&
448 git rm .gitmodules &&
450 git commit -m "no submodules" &&
451 git checkout --recurse-submodules super
455 test_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
461 git switch --recurse-submodules no-submodules &&
462 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
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) &&
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 &&
472 test_must_be_empty actual.out &&
473 verify_fetch_result actual.err
476 test_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
482 git switch --recurse-submodules no-submodules &&
483 git fetch --recurse-submodules >../actual.out 2>../actual.err
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) &&
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 &&
493 test_must_be_empty actual.out &&
494 verify_fetch_result actual.err
497 test_expect_success
"'--recurse-submodules' should ignore changed, inactive submodules" '
498 add_submodule_commits &&
499 add_superproject_commits &&
501 # Fetch the new superproject commit
504 git switch --recurse-submodules no-submodules &&
505 git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
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
512 rm expect.err.deep &&
513 verify_fetch_result actual.err
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
519 test_expect_success
'setup downstream branch with other submodule' '
524 echo sub2content >sub2file &&
526 git commit -a -m new &&
529 git checkout -b super-sub2-only &&
530 git submodule add "$pwd/submodule2" submodule2 &&
531 git commit -m "add sub2" &&
532 git checkout super &&
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
544 test_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 &&
550 # Create new commit in origin/super-sub2-only
551 git checkout super-sub2-only &&
554 test_commit --no-tag foo
556 git add submodule2 &&
557 git commit -m "new submodule2" &&
559 git checkout super &&
562 git fetch --recurse-submodules >../actual.out 2>../actual.err
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 &&
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 &&
574 OLD_HEAD..$super_head super -> origin/super
575 OLD_HEAD..$super_sub2_only_head super-sub2-only -> origin/super-sub2-only
577 verify_fetch_result actual.err
580 test_expect_success
"'--recurse-submodules=on-demand' stops when no new submodule commits are found in the superproject (and ignores config)" '
581 add_submodule_commits &&
584 git commit -m "new file" &&
585 new_head=$(git rev-parse --short HEAD) &&
586 write_expected_super $new_head &&
588 rm expect.err.deep &&
591 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
593 test_must_be_empty actual.out &&
594 verify_fetch_result actual.err
597 test_expect_success
"'fetch.recurseSubmodules=on-demand' overrides global config" '
600 git fetch --recurse-submodules
602 add_submodule_commits &&
603 git config --global fetch.recurseSubmodules false &&
605 git commit -m "new submodule" &&
606 new_head=$(git rev-parse --short HEAD) &&
607 write_expected_super $new_head &&
608 rm expect.err.deep &&
611 git config fetch.recurseSubmodules on-demand &&
612 git fetch >../actual.out 2>../actual.err
614 git config --global --unset fetch.recurseSubmodules &&
617 git config --unset fetch.recurseSubmodules
619 test_must_be_empty actual.out &&
620 verify_fetch_result actual.err
623 test_expect_success
"'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
626 git fetch --recurse-submodules
628 add_submodule_commits &&
629 git config fetch.recurseSubmodules false &&
631 git commit -m "new submodule" &&
632 new_head=$(git rev-parse --short HEAD) &&
633 write_expected_super $new_head &&
634 rm expect.err.deep &&
637 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
638 git fetch >../actual.out 2>../actual.err
640 git config --unset fetch.recurseSubmodules &&
643 git config --unset submodule.submodule.fetchRecurseSubmodules
645 test_must_be_empty actual.out &&
646 verify_fetch_result actual.err
649 test_expect_success
"don't fetch submodule when newly recorded commits are already present" '
652 git checkout -q HEAD^^
655 git commit -m "submodule rewound" &&
656 new_head=$(git rev-parse --short HEAD) &&
657 write_expected_super $new_head &&
659 # This file does not exist, but rm -f for readability
660 rm -f expect.err.deep &&
663 git fetch >../actual.out 2>../actual.err
665 test_must_be_empty actual.out &&
666 verify_fetch_result actual.err &&
673 test_expect_success
"'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
676 git fetch --recurse-submodules
678 add_submodule_commits &&
680 git rm .gitmodules &&
681 git commit -m "new submodule without .gitmodules" &&
682 new_head=$(git rev-parse --short HEAD) &&
683 write_expected_super $new_head &&
684 rm expect.err.deep &&
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 &&
696 git config --unset fetch.recurseSubmodules &&
699 test_must_be_empty actual.out &&
700 verify_fetch_result actual.err &&
701 git checkout HEAD^ -- .gitmodules &&
702 git add .gitmodules &&
703 git commit -m "new submodule restored .gitmodules"
706 test_expect_success
'fetching submodules respects parallel settings' '
707 git config fetch.recurseSubmodules true &&
710 GIT_TRACE=$(pwd)/trace.out git fetch &&
711 grep "1 tasks" trace.out &&
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 &&
721 GIT_TRACE=$(pwd)/trace.out git -c submodule.fetchJobs=0 fetch &&
722 grep "preparing to run up to [0-9]* tasks" trace.out &&
723 ! grep "up to 0 tasks" trace.out &&
728 test_expect_success
'fetching submodule into a broken repository' '
729 # Prepare src and src/sub nested in it
734 git -C sub commit --allow-empty -m "initial in sub" &&
735 git submodule add -- ./sub sub &&
736 git commit -m "initial in top"
739 # Clone the old-fashoned way
741 git -C dst clone ../src/sub sub &&
743 # Make sure that old-fashoned layout is still supported
746 # "diff" would find no change
747 git -C dst diff --exit-code &&
749 # Recursive-fetch works fine
750 git -C dst fetch --recurse-submodules &&
752 # Break the receiving submodule
753 rm -f dst/sub/.git/HEAD &&
755 # NOTE: without the fix the following tests will recurse forever!
756 # They should terminate with an error.
758 test_must_fail git -C dst status &&
759 test_must_fail git -C dst diff &&
760 test_must_fail git -C dst fetch --recurse-submodules
763 test_expect_success
"fetch new commits when submodule got renamed" '
764 git clone . downstream_rename &&
766 cd downstream_rename &&
767 git submodule update --init --recursive &&
768 git checkout -b rename &&
769 git mv submodule submodule_renamed &&
771 cd submodule_renamed &&
772 git checkout -b rename_sub &&
776 git push origin rename_sub &&
777 git rev-parse HEAD >../../expect
779 git add submodule_renamed &&
780 git commit -m "update renamed submodule" &&
781 git push origin rename
785 git fetch --recurse-submodules=on-demand &&
788 git rev-parse origin/rename_sub >../../actual
791 test_cmp expect actual
794 test_expect_success
"fetch new submodule commits on-demand outside standard refspec" '
795 # add a second submodule and ensure it is around in downstream first
796 git clone submodule sub1 &&
797 git submodule add ./sub1 &&
798 git commit -m "adding a second submodule" &&
799 git -C downstream pull &&
800 git -C downstream submodule update --init --recursive &&
802 git checkout --detach &&
804 C=$(git -C submodule commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
805 git -C submodule update-ref refs/changes/1 $C &&
806 git update-index --cacheinfo 160000 $C submodule &&
809 D=$(git -C sub1 commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
810 git -C sub1 update-ref refs/changes/2 $D &&
811 git update-index --cacheinfo 160000 $D sub1 &&
813 git commit -m "updated submodules outside of refs/heads" &&
814 E=$(git rev-parse HEAD) &&
815 git update-ref refs/changes/3 $E &&
818 git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
819 git -C submodule cat-file -t $C &&
820 git -C sub1 cat-file -t $D &&
821 git checkout --recurse-submodules FETCH_HEAD
825 test_expect_success
'fetch new submodule commit on-demand in FETCH_HEAD' '
826 # depends on the previous test for setup
828 C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
829 git -C submodule update-ref refs/changes/4 $C &&
830 git update-index --cacheinfo 160000 $C submodule &&
833 D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
834 git -C sub1 update-ref refs/changes/5 $D &&
835 git update-index --cacheinfo 160000 $D sub1 &&
837 git commit -m "updated submodules outside of refs/heads" &&
838 E=$(git rev-parse HEAD) &&
839 git update-ref refs/changes/6 $E &&
842 git fetch --recurse-submodules origin refs/changes/6 &&
843 git -C submodule cat-file -t $C &&
844 git -C sub1 cat-file -t $D &&
845 git checkout --recurse-submodules FETCH_HEAD
849 test_expect_success
'fetch new submodule commits on-demand without .gitmodules entry' '
850 # depends on the previous test for setup
852 git config -f .gitmodules --remove-section submodule.sub1 &&
853 git add .gitmodules &&
854 git commit -m "delete gitmodules file" &&
855 git checkout -B super &&
856 git -C downstream fetch &&
857 git -C downstream checkout origin/super &&
859 C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
860 git -C submodule update-ref refs/changes/7 $C &&
861 git update-index --cacheinfo 160000 $C submodule &&
864 D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
865 git -C sub1 update-ref refs/changes/8 $D &&
866 git update-index --cacheinfo 160000 $D sub1 &&
868 git commit -m "updated submodules outside of refs/heads" &&
869 E=$(git rev-parse HEAD) &&
870 git update-ref refs/changes/9 $E &&
873 git fetch --recurse-submodules origin refs/changes/9 &&
874 git -C submodule cat-file -t $C &&
875 git -C sub1 cat-file -t $D &&
876 git checkout --recurse-submodules FETCH_HEAD
880 test_expect_success
'fetch new submodule commit intermittently referenced by superproject' '
881 # depends on the previous test for setup
883 D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
884 E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
885 F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
887 git -C sub1 update-ref refs/changes/10 $D &&
888 git update-index --cacheinfo 160000 $D sub1 &&
889 git commit -m "updated submodules outside of refs/heads" &&
891 git -C sub1 update-ref refs/changes/11 $E &&
892 git update-index --cacheinfo 160000 $E sub1 &&
893 git commit -m "updated submodules outside of refs/heads" &&
895 git -C sub1 update-ref refs/changes/12 $F &&
896 git update-index --cacheinfo 160000 $F sub1 &&
897 git commit -m "updated submodules outside of refs/heads" &&
899 G=$(git rev-parse HEAD) &&
900 git update-ref refs/changes/13 $G &&
903 git fetch --recurse-submodules origin refs/changes/13 &&
905 git -C sub1 cat-file -t $D &&
906 git -C sub1 cat-file -t $E &&
907 git -C sub1 cat-file -t $F
915 git
-C "$dir" add
"$@" &&
916 git
-C "$dir" commit
-a -m "$msg" &&
920 compare_refs_in_dir
() {
927 git
-C "$1" rev-parse
--verify "$2" >expect
&&
928 git
-C "$3" rev-parse
--verify "$4" >actual
&&
929 eval $fail test_cmp expect actual
933 test_expect_success
'setup nested submodule fetch test' '
934 # does not depend on any previous test setups
936 for repo in outer middle inner
938 git init --bare $repo &&
939 git clone $repo ${repo}_content &&
940 echo "$repo" >"${repo}_content/file" &&
941 add_commit_push ${repo}_content "initial" file ||
946 git -C A submodule add "$pwd/middle" &&
947 git -C A/middle/ submodule add "$pwd/inner" &&
948 add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
949 add_commit_push A/ "adding middle sub" .gitmodules middle &&
952 git -C B/ submodule update --init middle &&
954 compare_refs_in_dir A HEAD B HEAD &&
955 compare_refs_in_dir A/middle HEAD B/middle HEAD &&
956 test_path_is_file B/file &&
957 test_path_is_file B/middle/file &&
958 test_path_is_missing B/middle/inner/file &&
960 echo "change on inner repo of A" >"A/middle/inner/file" &&
961 add_commit_push A/middle/inner "change on inner" file &&
962 add_commit_push A/middle "change on inner" inner &&
963 add_commit_push A "change on inner" middle
966 test_expect_success
'fetching a superproject containing an uninitialized sub/sub project' '
967 # depends on previous test for setup
970 compare_refs_in_dir A origin/HEAD B origin/HEAD
973 fetch_with_recursion_abort
() {
974 # In a regression the following git call will run into infinite recursion.
975 # To handle that, we connect the sed command to the git call by a pipe
976 # so that sed can kill the infinite recursion when detected.
977 # The recursion creates git output like:
978 # Fetching submodule sub
979 # Fetching submodule sub/sub <-- [1]
980 # Fetching submodule sub/sub/sub
982 # [1] sed will stop reading and cause git to eventually stop and die
984 git
-C "$1" fetch
--recurse-submodules 2>&1 |
985 sed "/Fetching submodule $2[^$]/q" >out
&&
986 ! grep "Fetching submodule $2[^$]" out
989 test_expect_success
'setup recursive fetch with uninit submodule' '
990 # does not depend on any previous test setups
992 test_create_repo super &&
993 test_commit -C super initial &&
994 test_create_repo sub &&
995 test_commit -C sub initial &&
996 git -C sub rev-parse HEAD >expect &&
998 git -C super submodule add ../sub &&
999 git -C super commit -m "add sub" &&
1001 git clone super superclone &&
1002 git -C superclone submodule status >out &&
1003 sed -e "s/^-//" -e "s/ sub.*$//" out >actual &&
1004 test_cmp expect actual
1007 test_expect_success
'recursive fetch with uninit submodule' '
1008 # depends on previous test for setup
1010 fetch_with_recursion_abort superclone sub &&
1011 git -C superclone submodule status >out &&
1012 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1013 test_cmp expect actual
1016 test_expect_success
'recursive fetch after deinit a submodule' '
1017 # depends on previous test for setup
1019 git -C superclone submodule update --init sub &&
1020 git -C superclone submodule deinit -f sub &&
1022 fetch_with_recursion_abort superclone sub &&
1023 git -C superclone submodule status >out &&
1024 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1025 test_cmp expect actual
1028 test_expect_success
'setup repo with upstreams that share a submodule name' '
1029 mkdir same-name-1 &&
1033 test_commit --no-tag a
1035 git clone same-name-1 same-name-2 &&
1036 # same-name-1 and same-name-2 both add a submodule with the
1041 git -C submodule init -b main &&
1042 test_commit -C submodule --no-tag a1 &&
1043 git submodule add "$pwd/same-name-1/submodule" &&
1044 git add submodule &&
1045 git commit -m "super-a1"
1050 git -C submodule init -b main &&
1051 test_commit -C submodule --no-tag a2 &&
1052 git submodule add "$pwd/same-name-2/submodule" &&
1053 git add submodule &&
1054 git commit -m "super-a2"
1056 git clone same-name-1 -o same-name-1 same-name-downstream &&
1058 cd same-name-downstream &&
1059 git remote add same-name-2 ../same-name-2 &&
1061 # init downstream with same-name-1
1062 git submodule update --init
1066 test_expect_success
'fetch --recurse-submodules updates name-conflicted, populated submodule' '
1067 test_when_finished "git -C same-name-downstream checkout main" &&
1070 test_commit -C submodule --no-tag b1 &&
1071 git add submodule &&
1072 git commit -m "super-b1"
1076 test_commit -C submodule --no-tag b2 &&
1077 git add submodule &&
1078 git commit -m "super-b2"
1081 cd same-name-downstream &&
1082 # even though the .gitmodules is correct, we cannot
1083 # fetch from same-name-2
1084 git checkout same-name-2/main &&
1085 git fetch --recurse-submodules same-name-1 &&
1086 test_must_fail git fetch --recurse-submodules same-name-2
1088 super_head1=$(git -C same-name-1 rev-parse HEAD) &&
1089 git -C same-name-downstream cat-file -e $super_head1 &&
1091 super_head2=$(git -C same-name-2 rev-parse HEAD) &&
1092 git -C same-name-downstream cat-file -e $super_head2 &&
1094 sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1095 git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
1097 sub_head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1098 test_must_fail git -C same-name-downstream/submodule cat-file -e $sub_head2
1101 test_expect_success
'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
1104 test_commit -C submodule --no-tag c1 &&
1105 git add submodule &&
1106 git commit -m "super-c1"
1110 test_commit -C submodule --no-tag c2 &&
1111 git add submodule &&
1112 git commit -m "super-c2"
1115 cd same-name-downstream &&
1116 git checkout main &&
1117 git rm .gitmodules &&
1119 git commit -m "no submodules" &&
1120 git fetch --recurse-submodules same-name-1
1122 head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1123 head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1125 cd same-name-downstream/.git/modules/submodule &&
1126 # The submodule has core.worktree pointing to the "git
1127 # rm"-ed directory, overwrite the invalid value. See
1128 # comment in get_fetch_task_from_changed() for more
1130 git --work-tree=. cat-file -e $head1 &&
1131 test_must_fail git --work-tree=. cat-file -e $head2
1135 test_expect_success
'fetch --all with --recurse-submodules' '
1136 test_when_finished "rm -fr src_clone" &&
1137 git clone --recurse-submodules src src_clone &&
1140 git config submodule.recurse true &&
1141 git config fetch.parallel 0 &&
1142 git fetch --all 2>../fetch-log
1144 grep "^Fetching submodule sub$" fetch-log >fetch-subs &&
1145 test_line_count = 1 fetch-subs
1148 test_expect_success
'fetch --all with --recurse-submodules with multiple' '
1149 test_when_finished "rm -fr src_clone" &&
1150 git clone --recurse-submodules src src_clone &&
1153 git remote add secondary ../src &&
1154 git config submodule.recurse true &&
1155 git config fetch.parallel 0 &&
1156 git fetch --all 2>../fetch-log
1158 grep "Fetching submodule sub" fetch-log >fetch-subs &&
1159 test_line_count = 2 fetch-subs