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
"fetch --recurse-submodules honors --no-write-fetch-head" '
173 git submodule foreach --recursive \
174 sh -c "cd \"\$(git rev-parse --git-dir)\" && rm -f FETCH_HEAD" &&
176 git fetch --recurse-submodules --no-write-fetch-head &&
178 git submodule foreach --recursive \
179 sh -c "cd \"\$(git rev-parse --git-dir)\" && ! test -f FETCH_HEAD"
183 test_expect_success
"submodule.recurse option triggers recursive fetch" '
184 add_submodule_commits &&
187 git -c submodule.recurse fetch >../actual.out 2>../actual.err
189 test_must_be_empty actual.out &&
190 verify_fetch_result actual.err
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 &&
198 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
200 test_must_be_empty actual.out &&
201 verify_fetch_result actual.err &&
202 grep "2 tasks" trace.out
205 test_expect_success
"fetch alone only fetches superproject" '
206 add_submodule_commits &&
209 git fetch >../actual.out 2>../actual.err
211 test_must_be_empty actual.out &&
212 test_must_be_empty actual.err
215 test_expect_success
"fetch --no-recurse-submodules only fetches superproject" '
218 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
220 test_must_be_empty actual.out &&
221 test_must_be_empty actual.err
224 test_expect_success
"using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
227 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
228 git fetch >../actual.out 2>../actual.err
230 test_must_be_empty actual.out &&
231 verify_fetch_result actual.err
234 test_expect_success
"--no-recurse-submodules overrides .gitmodules config" '
235 add_submodule_commits &&
238 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
240 test_must_be_empty actual.out &&
241 test_must_be_empty actual.err
244 test_expect_success
"using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
247 git config submodule.submodule.fetchRecurseSubmodules false &&
248 git fetch >../actual.out 2>../actual.err
250 test_must_be_empty actual.out &&
251 test_must_be_empty actual.err
254 test_expect_success
"--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
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
261 test_must_be_empty actual.out &&
262 verify_fetch_result actual.err
265 test_expect_success
"--quiet propagates to submodules" '
268 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
270 test_must_be_empty actual.out &&
271 test_must_be_empty actual.err
274 test_expect_success
"--quiet propagates to parallel submodules" '
277 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
279 test_must_be_empty actual.out &&
280 test_must_be_empty actual.err
283 test_expect_success
"--dry-run propagates to submodules" '
284 add_submodule_commits &&
287 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
289 test_must_be_empty actual.out &&
290 verify_fetch_result actual.err
293 test_expect_success
"Without --dry-run propagates to submodules" '
296 git fetch --recurse-submodules >../actual.out 2>../actual.err
298 test_must_be_empty actual.out &&
299 verify_fetch_result actual.err
302 test_expect_success
"recurseSubmodules=true propagates into submodules" '
303 add_submodule_commits &&
306 git config fetch.recurseSubmodules true &&
307 git fetch >../actual.out 2>../actual.err
309 test_must_be_empty actual.out &&
310 verify_fetch_result actual.err
313 test_expect_success
"--recurse-submodules overrides config in submodule" '
314 add_submodule_commits &&
319 git config fetch.recurseSubmodules false
321 git fetch --recurse-submodules >../actual.out 2>../actual.err
323 test_must_be_empty actual.out &&
324 verify_fetch_result actual.err
327 test_expect_success
"--no-recurse-submodules overrides config setting" '
328 add_submodule_commits &&
331 git config fetch.recurseSubmodules true &&
332 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
334 test_must_be_empty actual.out &&
335 test_must_be_empty actual.err
338 test_expect_success
"Recursion doesn't happen when no new commits are fetched in the superproject" '
343 git config --unset fetch.recurseSubmodules
345 git config --unset fetch.recurseSubmodules &&
346 git fetch >../actual.out 2>../actual.err
348 test_must_be_empty actual.out &&
349 test_must_be_empty actual.err
352 test_expect_success
"Recursion stops when no new submodule commits are fetched" '
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 &&
360 git fetch >../actual.out 2>../actual.err
362 verify_fetch_result actual.err &&
363 test_must_be_empty actual.out
366 test_expect_success
"Recursion doesn't happen when new superproject commits don't change any submodules" '
367 add_submodule_commits &&
370 git commit -m "new file" &&
371 new_head=$(git rev-parse --short HEAD) &&
372 write_expected_super $new_head &&
374 rm expect.err.deep &&
377 git fetch >../actual.out 2>../actual.err
379 test_must_be_empty actual.out &&
380 verify_fetch_result actual.err
383 test_expect_success
"Recursion picks up config in submodule" '
386 git fetch --recurse-submodules &&
389 git config fetch.recurseSubmodules true
392 add_submodule_commits &&
394 git commit -m "new submodule" &&
395 new_head=$(git rev-parse --short HEAD) &&
396 write_expected_super $new_head &&
399 git fetch >../actual.out 2>../actual.err &&
402 git config --unset fetch.recurseSubmodules
405 verify_fetch_result actual.err &&
406 test_must_be_empty actual.out
409 test_expect_success
"Recursion picks up all submodules when necessary" '
410 add_submodule_commits &&
411 add_superproject_commits &&
414 git fetch >../actual.out 2>../actual.err
416 verify_fetch_result actual.err &&
417 test_must_be_empty actual.out
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 &&
424 git config fetch.recurseSubmodules true &&
425 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
426 git config --unset fetch.recurseSubmodules
428 test_must_be_empty actual.out &&
429 test_must_be_empty actual.err
432 test_expect_success
"'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
433 add_submodule_commits &&
434 add_superproject_commits &&
437 git config fetch.recurseSubmodules false &&
440 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
442 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
443 git config --unset fetch.recurseSubmodules &&
446 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
449 test_must_be_empty actual.out &&
450 verify_fetch_result actual.err
453 # These tests verify that we can fetch submodules that aren't in the
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' '
461 git checkout --recurse-submodules -b no-submodules &&
462 git rm .gitmodules &&
464 git commit -m "no submodules" &&
465 git checkout --recurse-submodules super
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
475 git switch --recurse-submodules no-submodules &&
476 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
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) &&
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 &&
486 test_must_be_empty actual.out &&
487 verify_fetch_result actual.err
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
496 git switch --recurse-submodules no-submodules &&
497 git fetch --recurse-submodules >../actual.out 2>../actual.err
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) &&
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 &&
507 test_must_be_empty actual.out &&
508 verify_fetch_result actual.err
511 test_expect_success
"'--recurse-submodules' should ignore changed, inactive submodules" '
512 add_submodule_commits &&
513 add_superproject_commits &&
515 # Fetch the new superproject commit
518 git switch --recurse-submodules no-submodules &&
519 git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
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
526 rm expect.err.deep &&
527 verify_fetch_result actual.err
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
533 test_expect_success
'setup downstream branch with other submodule' '
538 echo sub2content >sub2file &&
540 git commit -a -m new &&
543 git checkout -b super-sub2-only &&
544 git submodule add "$pwd/submodule2" submodule2 &&
545 git commit -m "add sub2" &&
546 git checkout super &&
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
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 &&
564 # Create new commit in origin/super-sub2-only
565 git checkout super-sub2-only &&
568 test_commit --no-tag foo
570 git add submodule2 &&
571 git commit -m "new submodule2" &&
573 git checkout super &&
576 git fetch --recurse-submodules >../actual.out 2>../actual.err
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 &&
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 &&
588 OLD_HEAD..$super_head super -> origin/super
589 OLD_HEAD..$super_sub2_only_head super-sub2-only -> origin/super-sub2-only
591 verify_fetch_result actual.err
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 &&
598 git commit -m "new file" &&
599 new_head=$(git rev-parse --short HEAD) &&
600 write_expected_super $new_head &&
602 rm expect.err.deep &&
605 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
607 test_must_be_empty actual.out &&
608 verify_fetch_result actual.err
611 test_expect_success
"'fetch.recurseSubmodules=on-demand' overrides global config" '
614 git fetch --recurse-submodules
616 add_submodule_commits &&
617 git config --global fetch.recurseSubmodules false &&
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 &&
625 git config fetch.recurseSubmodules on-demand &&
626 git fetch >../actual.out 2>../actual.err
628 git config --global --unset fetch.recurseSubmodules &&
631 git config --unset fetch.recurseSubmodules
633 test_must_be_empty actual.out &&
634 verify_fetch_result actual.err
637 test_expect_success
"'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
640 git fetch --recurse-submodules
642 add_submodule_commits &&
643 git config fetch.recurseSubmodules false &&
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 &&
651 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
652 git fetch >../actual.out 2>../actual.err
654 git config --unset fetch.recurseSubmodules &&
657 git config --unset submodule.submodule.fetchRecurseSubmodules
659 test_must_be_empty actual.out &&
660 verify_fetch_result actual.err
663 test_expect_success
"don't fetch submodule when newly recorded commits are already present" '
666 git checkout -q HEAD^^
669 git commit -m "submodule rewound" &&
670 new_head=$(git rev-parse --short HEAD) &&
671 write_expected_super $new_head &&
673 # This file does not exist, but rm -f for readability
674 rm -f expect.err.deep &&
677 git fetch >../actual.out 2>../actual.err
679 test_must_be_empty actual.out &&
680 verify_fetch_result actual.err &&
687 test_expect_success
"'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
690 git fetch --recurse-submodules
692 add_submodule_commits &&
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 &&
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 &&
710 git config --unset fetch.recurseSubmodules &&
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"
720 test_expect_success
'fetching submodules respects parallel settings' '
721 git config fetch.recurseSubmodules true &&
722 test_when_finished "rm -f downstream/trace.out" &&
725 GIT_TRACE=$(pwd)/trace.out git fetch &&
726 grep "1 tasks" trace.out &&
729 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 7 &&
730 grep "7 tasks" trace.out &&
733 git config submodule.fetchJobs 8 &&
734 GIT_TRACE=$(pwd)/trace.out git fetch &&
735 grep "8 tasks" trace.out &&
738 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 9 &&
739 grep "9 tasks" trace.out &&
742 GIT_TRACE=$(pwd)/trace.out git -c submodule.fetchJobs=0 fetch &&
743 grep "preparing to run up to [0-9]* tasks" trace.out &&
744 ! grep "up to 0 tasks" trace.out &&
749 test_expect_success
'fetching submodule into a broken repository' '
750 # Prepare src and src/sub nested in it
755 git -C sub commit --allow-empty -m "initial in sub" &&
756 git submodule add -- ./sub sub &&
757 git commit -m "initial in top"
760 # Clone the old-fashoned way
762 git -C dst clone ../src/sub sub &&
764 # Make sure that old-fashoned layout is still supported
767 # "diff" would find no change
768 git -C dst diff --exit-code &&
770 # Recursive-fetch works fine
771 git -C dst fetch --recurse-submodules &&
773 # Break the receiving submodule
774 rm -f dst/sub/.git/HEAD &&
776 # NOTE: without the fix the following tests will recurse forever!
777 # They should terminate with an error.
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
784 test_expect_success
"fetch new commits when submodule got renamed" '
785 git clone . downstream_rename &&
787 cd downstream_rename &&
788 git submodule update --init --recursive &&
789 git checkout -b rename &&
790 git mv submodule submodule_renamed &&
792 cd submodule_renamed &&
793 git checkout -b rename_sub &&
797 git push origin rename_sub &&
798 git rev-parse HEAD >../../expect
800 git add submodule_renamed &&
801 git commit -m "update renamed submodule" &&
802 git push origin rename
806 git fetch --recurse-submodules=on-demand &&
809 git rev-parse origin/rename_sub >../../actual
812 test_cmp expect actual
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 &&
823 git checkout --detach &&
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 &&
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 &&
834 git commit -m "updated submodules outside of refs/heads" &&
835 E=$(git rev-parse HEAD) &&
836 git update-ref refs/changes/3 $E &&
839 git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
840 git -C submodule cat-file -t $C &&
841 git -C sub1 cat-file -t $D &&
842 git checkout --recurse-submodules FETCH_HEAD
846 test_expect_success
'fetch new submodule commit on-demand in FETCH_HEAD' '
847 # depends on the previous test for setup
849 C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
850 git -C submodule update-ref refs/changes/4 $C &&
851 git update-index --cacheinfo 160000 $C submodule &&
854 D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
855 git -C sub1 update-ref refs/changes/5 $D &&
856 git update-index --cacheinfo 160000 $D sub1 &&
858 git commit -m "updated submodules outside of refs/heads" &&
859 E=$(git rev-parse HEAD) &&
860 git update-ref refs/changes/6 $E &&
863 git fetch --recurse-submodules origin refs/changes/6 &&
864 git -C submodule cat-file -t $C &&
865 git -C sub1 cat-file -t $D &&
866 git checkout --recurse-submodules FETCH_HEAD
870 test_expect_success
'fetch new submodule commits on-demand without .gitmodules entry' '
871 # depends on the previous test for setup
873 git config -f .gitmodules --remove-section submodule.sub1 &&
874 git add .gitmodules &&
875 git commit -m "delete gitmodules file" &&
876 git checkout -B super &&
877 git -C downstream fetch &&
878 git -C downstream checkout origin/super &&
880 C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
881 git -C submodule update-ref refs/changes/7 $C &&
882 git update-index --cacheinfo 160000 $C submodule &&
885 D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
886 git -C sub1 update-ref refs/changes/8 $D &&
887 git update-index --cacheinfo 160000 $D sub1 &&
889 git commit -m "updated submodules outside of refs/heads" &&
890 E=$(git rev-parse HEAD) &&
891 git update-ref refs/changes/9 $E &&
894 git fetch --recurse-submodules origin refs/changes/9 &&
895 git -C submodule cat-file -t $C &&
896 git -C sub1 cat-file -t $D &&
897 git checkout --recurse-submodules FETCH_HEAD
901 test_expect_success
'fetch new submodule commit intermittently referenced by superproject' '
902 # depends on the previous test for setup
904 D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
905 E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
906 F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
908 git -C sub1 update-ref refs/changes/10 $D &&
909 git update-index --cacheinfo 160000 $D sub1 &&
910 git commit -m "updated submodules outside of refs/heads" &&
912 git -C sub1 update-ref refs/changes/11 $E &&
913 git update-index --cacheinfo 160000 $E sub1 &&
914 git commit -m "updated submodules outside of refs/heads" &&
916 git -C sub1 update-ref refs/changes/12 $F &&
917 git update-index --cacheinfo 160000 $F sub1 &&
918 git commit -m "updated submodules outside of refs/heads" &&
920 G=$(git rev-parse HEAD) &&
921 git update-ref refs/changes/13 $G &&
924 git fetch --recurse-submodules origin refs/changes/13 &&
926 git -C sub1 cat-file -t $D &&
927 git -C sub1 cat-file -t $E &&
928 git -C sub1 cat-file -t $F
936 git
-C "$dir" add
"$@" &&
937 git
-C "$dir" commit
-a -m "$msg" &&
941 compare_refs_in_dir
() {
948 git
-C "$1" rev-parse
--verify "$2" >expect
&&
949 git
-C "$3" rev-parse
--verify "$4" >actual
&&
950 eval $fail test_cmp expect actual
954 test_expect_success
'setup nested submodule fetch test' '
955 # does not depend on any previous test setups
957 for repo in outer middle inner
959 git init --bare $repo &&
960 git clone $repo ${repo}_content &&
961 echo "$repo" >"${repo}_content/file" &&
962 add_commit_push ${repo}_content "initial" file ||
967 git -C A submodule add "$pwd/middle" &&
968 git -C A/middle/ submodule add "$pwd/inner" &&
969 add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
970 add_commit_push A/ "adding middle sub" .gitmodules middle &&
973 git -C B/ submodule update --init middle &&
975 compare_refs_in_dir A HEAD B HEAD &&
976 compare_refs_in_dir A/middle HEAD B/middle HEAD &&
977 test_path_is_file B/file &&
978 test_path_is_file B/middle/file &&
979 test_path_is_missing B/middle/inner/file &&
981 echo "change on inner repo of A" >"A/middle/inner/file" &&
982 add_commit_push A/middle/inner "change on inner" file &&
983 add_commit_push A/middle "change on inner" inner &&
984 add_commit_push A "change on inner" middle
987 test_expect_success
'fetching a superproject containing an uninitialized sub/sub project' '
988 # depends on previous test for setup
991 compare_refs_in_dir A origin/HEAD B origin/HEAD
994 fetch_with_recursion_abort
() {
995 # In a regression the following git call will run into infinite recursion.
996 # To handle that, we connect the sed command to the git call by a pipe
997 # so that sed can kill the infinite recursion when detected.
998 # The recursion creates git output like:
999 # Fetching submodule sub
1000 # Fetching submodule sub/sub <-- [1]
1001 # Fetching submodule sub/sub/sub
1003 # [1] sed will stop reading and cause git to eventually stop and die
1005 git
-C "$1" fetch
--recurse-submodules 2>&1 |
1006 sed "/Fetching submodule $2[^$]/q" >out
&&
1007 ! grep "Fetching submodule $2[^$]" out
1010 test_expect_success
'setup recursive fetch with uninit submodule' '
1011 # does not depend on any previous test setups
1013 test_create_repo super &&
1014 test_commit -C super initial &&
1015 test_create_repo sub &&
1016 test_commit -C sub initial &&
1017 git -C sub rev-parse HEAD >expect &&
1019 git -C super submodule add ../sub &&
1020 git -C super commit -m "add sub" &&
1022 git clone super superclone &&
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
'recursive fetch with uninit submodule' '
1029 # depends on previous test for setup
1031 fetch_with_recursion_abort superclone sub &&
1032 git -C superclone submodule status >out &&
1033 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1034 test_cmp expect actual
1037 test_expect_success
'recursive fetch after deinit a submodule' '
1038 # depends on previous test for setup
1040 git -C superclone submodule update --init sub &&
1041 git -C superclone submodule deinit -f sub &&
1043 fetch_with_recursion_abort superclone sub &&
1044 git -C superclone submodule status >out &&
1045 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1046 test_cmp expect actual
1049 test_expect_success
'setup repo with upstreams that share a submodule name' '
1050 mkdir same-name-1 &&
1054 test_commit --no-tag a
1056 git clone same-name-1 same-name-2 &&
1057 # same-name-1 and same-name-2 both add a submodule with the
1062 git -C submodule init -b main &&
1063 test_commit -C submodule --no-tag a1 &&
1064 git submodule add "$pwd/same-name-1/submodule" &&
1065 git add submodule &&
1066 git commit -m "super-a1"
1071 git -C submodule init -b main &&
1072 test_commit -C submodule --no-tag a2 &&
1073 git submodule add "$pwd/same-name-2/submodule" &&
1074 git add submodule &&
1075 git commit -m "super-a2"
1077 git clone same-name-1 -o same-name-1 same-name-downstream &&
1079 cd same-name-downstream &&
1080 git remote add same-name-2 ../same-name-2 &&
1082 # init downstream with same-name-1
1083 git submodule update --init
1087 test_expect_success
'fetch --recurse-submodules updates name-conflicted, populated submodule' '
1088 test_when_finished "git -C same-name-downstream checkout main" &&
1091 test_commit -C submodule --no-tag b1 &&
1092 git add submodule &&
1093 git commit -m "super-b1"
1097 test_commit -C submodule --no-tag b2 &&
1098 git add submodule &&
1099 git commit -m "super-b2"
1102 cd same-name-downstream &&
1103 # even though the .gitmodules is correct, we cannot
1104 # fetch from same-name-2
1105 git checkout same-name-2/main &&
1106 git fetch --recurse-submodules same-name-1 &&
1107 test_must_fail git fetch --recurse-submodules same-name-2
1109 super_head1=$(git -C same-name-1 rev-parse HEAD) &&
1110 git -C same-name-downstream cat-file -e $super_head1 &&
1112 super_head2=$(git -C same-name-2 rev-parse HEAD) &&
1113 git -C same-name-downstream cat-file -e $super_head2 &&
1115 sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1116 git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
1118 sub_head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1119 test_must_fail git -C same-name-downstream/submodule cat-file -e $sub_head2
1122 test_expect_success
'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
1125 test_commit -C submodule --no-tag c1 &&
1126 git add submodule &&
1127 git commit -m "super-c1"
1131 test_commit -C submodule --no-tag c2 &&
1132 git add submodule &&
1133 git commit -m "super-c2"
1136 cd same-name-downstream &&
1137 git checkout main &&
1138 git rm .gitmodules &&
1140 git commit -m "no submodules" &&
1141 git fetch --recurse-submodules same-name-1
1143 head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1144 head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1146 cd same-name-downstream/.git/modules/submodule &&
1147 # The submodule has core.worktree pointing to the "git
1148 # rm"-ed directory, overwrite the invalid value. See
1149 # comment in get_fetch_task_from_changed() for more
1151 git --work-tree=. cat-file -e $head1 &&
1152 test_must_fail git --work-tree=. cat-file -e $head2
1156 test_expect_success
'fetch --all with --recurse-submodules' '
1157 test_when_finished "rm -fr src_clone" &&
1158 git clone --recurse-submodules src src_clone &&
1161 git config submodule.recurse true &&
1162 git config fetch.parallel 0 &&
1163 git fetch --all 2>../fetch-log
1165 grep "^Fetching submodule sub$" fetch-log >fetch-subs &&
1166 test_line_count = 1 fetch-subs
1169 test_expect_success
'fetch --all with --recurse-submodules with multiple' '
1170 test_when_finished "rm -fr src_clone" &&
1171 git clone --recurse-submodules src src_clone &&
1174 git remote add secondary ../src &&
1175 git config submodule.recurse true &&
1176 git config fetch.parallel 0 &&
1177 git fetch --all 2>../fetch-log
1179 grep "Fetching submodule sub" fetch-log >fetch-subs &&
1180 test_line_count = 2 fetch-subs
1183 test_expect_success
"fetch --all with --no-recurse-submodules only fetches superproject" '
1184 test_when_finished "rm -rf src_clone" &&
1186 git clone --recurse-submodules src src_clone &&
1189 git remote add secondary ../src &&
1190 git config submodule.recurse true &&
1191 git fetch --all --no-recurse-submodules 2>../fetch-log
1193 ! grep "Fetching submodule" fetch-log