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 mkdir deepsubmodule &&
134 echo deepsubcontent > deepsubfile &&
135 git add deepsubfile &&
136 git commit -m new deepsubfile &&
143 echo subcontent > subfile &&
145 git submodule add "$pwd/deepsubmodule" subdir/deepsubmodule &&
146 git commit -a -m new &&
149 git submodule add "$pwd/submodule" submodule &&
150 git commit -am initial &&
151 git branch -M super &&
152 git clone . downstream &&
155 git submodule update --init --recursive
159 test_expect_success
"fetch --recurse-submodules recurses into submodules" '
160 add_submodule_commits &&
163 git fetch --recurse-submodules >../actual.out 2>../actual.err
165 test_must_be_empty actual.out &&
166 verify_fetch_result actual.err
169 test_expect_success
"submodule.recurse option triggers recursive fetch" '
170 add_submodule_commits &&
173 git -c submodule.recurse fetch >../actual.out 2>../actual.err
175 test_must_be_empty actual.out &&
176 verify_fetch_result actual.err
179 test_expect_success
"fetch --recurse-submodules -j2 has the same output behaviour" '
180 add_submodule_commits &&
183 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
185 test_must_be_empty actual.out &&
186 verify_fetch_result actual.err &&
187 grep "2 tasks" trace.out
190 test_expect_success
"fetch alone only fetches superproject" '
191 add_submodule_commits &&
194 git fetch >../actual.out 2>../actual.err
196 test_must_be_empty actual.out &&
197 test_must_be_empty actual.err
200 test_expect_success
"fetch --no-recurse-submodules only fetches superproject" '
203 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
205 test_must_be_empty actual.out &&
206 test_must_be_empty actual.err
209 test_expect_success
"using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
212 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
213 git fetch >../actual.out 2>../actual.err
215 test_must_be_empty actual.out &&
216 verify_fetch_result actual.err
219 test_expect_success
"--no-recurse-submodules overrides .gitmodules config" '
220 add_submodule_commits &&
223 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
225 test_must_be_empty actual.out &&
226 test_must_be_empty actual.err
229 test_expect_success
"using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
232 git config submodule.submodule.fetchRecurseSubmodules false &&
233 git fetch >../actual.out 2>../actual.err
235 test_must_be_empty actual.out &&
236 test_must_be_empty actual.err
239 test_expect_success
"--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
242 git fetch --recurse-submodules >../actual.out 2>../actual.err &&
243 git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
244 git config --unset submodule.submodule.fetchRecurseSubmodules
246 test_must_be_empty actual.out &&
247 verify_fetch_result actual.err
250 test_expect_success
"--quiet propagates to submodules" '
253 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
255 test_must_be_empty actual.out &&
256 test_must_be_empty actual.err
259 test_expect_success
"--quiet propagates to parallel submodules" '
262 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
264 test_must_be_empty actual.out &&
265 test_must_be_empty actual.err
268 test_expect_success
"--dry-run propagates to submodules" '
269 add_submodule_commits &&
272 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
274 test_must_be_empty actual.out &&
275 verify_fetch_result actual.err
278 test_expect_success
"Without --dry-run propagates to submodules" '
281 git fetch --recurse-submodules >../actual.out 2>../actual.err
283 test_must_be_empty actual.out &&
284 verify_fetch_result actual.err
287 test_expect_success
"recurseSubmodules=true propagates into submodules" '
288 add_submodule_commits &&
291 git config fetch.recurseSubmodules true &&
292 git fetch >../actual.out 2>../actual.err
294 test_must_be_empty actual.out &&
295 verify_fetch_result actual.err
298 test_expect_success
"--recurse-submodules overrides config in submodule" '
299 add_submodule_commits &&
304 git config fetch.recurseSubmodules false
306 git fetch --recurse-submodules >../actual.out 2>../actual.err
308 test_must_be_empty actual.out &&
309 verify_fetch_result actual.err
312 test_expect_success
"--no-recurse-submodules overrides config setting" '
313 add_submodule_commits &&
316 git config fetch.recurseSubmodules true &&
317 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
319 test_must_be_empty actual.out &&
320 test_must_be_empty actual.err
323 test_expect_success
"Recursion doesn't happen when no new commits are fetched in the superproject" '
328 git config --unset fetch.recurseSubmodules
330 git config --unset fetch.recurseSubmodules &&
331 git fetch >../actual.out 2>../actual.err
333 test_must_be_empty actual.out &&
334 test_must_be_empty actual.err
337 test_expect_success
"Recursion stops when no new submodule commits are fetched" '
339 git commit -m "new submodule" &&
340 new_head=$(git rev-parse --short HEAD) &&
341 write_expected_super $new_head &&
342 rm expect.err.deep &&
345 git fetch >../actual.out 2>../actual.err
347 verify_fetch_result actual.err &&
348 test_must_be_empty actual.out
351 test_expect_success
"Recursion doesn't happen when new superproject commits don't change any submodules" '
352 add_submodule_commits &&
355 git commit -m "new file" &&
356 new_head=$(git rev-parse --short HEAD) &&
357 write_expected_super $new_head &&
359 rm expect.err.deep &&
362 git fetch >../actual.out 2>../actual.err
364 test_must_be_empty actual.out &&
365 verify_fetch_result actual.err
368 test_expect_success
"Recursion picks up config in submodule" '
371 git fetch --recurse-submodules &&
374 git config fetch.recurseSubmodules true
377 add_submodule_commits &&
379 git commit -m "new submodule" &&
380 new_head=$(git rev-parse --short HEAD) &&
381 write_expected_super $new_head &&
384 git fetch >../actual.out 2>../actual.err &&
387 git config --unset fetch.recurseSubmodules
390 verify_fetch_result actual.err &&
391 test_must_be_empty actual.out
394 test_expect_success
"Recursion picks up all submodules when necessary" '
395 add_submodule_commits &&
396 add_superproject_commits &&
399 git fetch >../actual.out 2>../actual.err
401 verify_fetch_result actual.err &&
402 test_must_be_empty actual.out
405 test_expect_success
"'--recurse-submodules=on-demand' doesn't recurse when no new commits are fetched in the superproject (and ignores config)" '
406 add_submodule_commits &&
409 git config fetch.recurseSubmodules true &&
410 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
411 git config --unset fetch.recurseSubmodules
413 test_must_be_empty actual.out &&
414 test_must_be_empty actual.err
417 test_expect_success
"'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
418 add_submodule_commits &&
419 add_superproject_commits &&
422 git config fetch.recurseSubmodules false &&
425 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
427 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
428 git config --unset fetch.recurseSubmodules &&
431 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
434 test_must_be_empty actual.out &&
435 verify_fetch_result actual.err
438 # These tests verify that we can fetch submodules that aren't in the
441 # First, test the simple case where the index is empty and we only fetch
442 # submodules that are not in the index.
443 test_expect_success
'setup downstream branch without submodules' '
446 git checkout --recurse-submodules -b no-submodules &&
447 git rm .gitmodules &&
449 git commit -m "no submodules" &&
450 git checkout --recurse-submodules super
454 test_expect_success
"'--recurse-submodules=on-demand' should fetch submodule commits if the submodule is changed but the index has no submodules" '
455 add_submodule_commits &&
456 add_superproject_commits &&
457 # Fetch the new superproject commit
460 git switch --recurse-submodules no-submodules &&
461 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
463 super_head=$(git rev-parse --short HEAD) &&
464 sub_head=$(git -C submodule rev-parse --short HEAD) &&
465 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
467 # assert that these are fetched from commits, not the index
468 write_expected_sub $sub_head $super_head &&
469 write_expected_deep $deep_head $sub_head &&
471 test_must_be_empty actual.out &&
472 verify_fetch_result actual.err
475 test_expect_success
"'--recurse-submodules' should fetch submodule commits if the submodule is changed but the index has no submodules" '
476 add_submodule_commits &&
477 add_superproject_commits &&
478 # Fetch the new superproject commit
481 git switch --recurse-submodules no-submodules &&
482 git fetch --recurse-submodules >../actual.out 2>../actual.err
484 super_head=$(git rev-parse --short HEAD) &&
485 sub_head=$(git -C submodule rev-parse --short HEAD) &&
486 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
488 # assert that these are fetched from commits, not the index
489 write_expected_sub $sub_head $super_head &&
490 write_expected_deep $deep_head $sub_head &&
492 test_must_be_empty actual.out &&
493 verify_fetch_result actual.err
496 test_expect_success
"'--recurse-submodules' should ignore changed, inactive submodules" '
497 add_submodule_commits &&
498 add_superproject_commits &&
500 # Fetch the new superproject commit
503 git switch --recurse-submodules no-submodules &&
504 git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
506 test_must_be_empty actual.out &&
507 super_head=$(git rev-parse --short HEAD) &&
508 write_expected_super $super_head &&
509 # Neither should be fetched because the submodule is inactive
511 rm expect.err.deep &&
512 verify_fetch_result actual.err
515 # Now that we know we can fetch submodules that are not in the index,
516 # test that we can fetch index and non-index submodules in the same
518 test_expect_success
'setup downstream branch with other submodule' '
523 echo sub2content >sub2file &&
525 git commit -a -m new &&
528 git checkout -b super-sub2-only &&
529 git submodule add "$pwd/submodule2" submodule2 &&
530 git commit -m "add sub2" &&
531 git checkout super &&
534 git fetch --recurse-submodules origin &&
535 git checkout super-sub2-only &&
536 # Explicitly run "git submodule update" because sub2 is new
537 # and has not been cloned.
538 git submodule update --init &&
539 git checkout --recurse-submodules super
543 test_expect_success
"'--recurse-submodules' should fetch submodule commits in changed submodules and the index" '
544 test_when_finished "rm expect.err.sub2" &&
545 # Create new commit in origin/super
546 add_submodule_commits &&
547 add_superproject_commits &&
549 # Create new commit in origin/super-sub2-only
550 git checkout super-sub2-only &&
553 test_commit --no-tag foo
555 git add submodule2 &&
556 git commit -m "new submodule2" &&
558 git checkout super &&
561 git fetch --recurse-submodules >../actual.out 2>../actual.err
563 test_must_be_empty actual.out &&
564 sub2_head=$(git -C submodule2 rev-parse --short HEAD) &&
565 super_head=$(git rev-parse --short super) &&
566 super_sub2_only_head=$(git rev-parse --short super-sub2-only) &&
567 write_expected_sub2 $sub2_head $super_sub2_only_head &&
569 # write_expected_super cannot handle >1 branch. Since this is a
570 # one-off, construct expect.err.super manually.
571 cat >"$pwd/expect.err.super" <<-EOF &&
573 OLD_HEAD..$super_head super -> origin/super
574 OLD_HEAD..$super_sub2_only_head super-sub2-only -> origin/super-sub2-only
576 verify_fetch_result actual.err
579 test_expect_success
"'--recurse-submodules=on-demand' stops when no new submodule commits are found in the superproject (and ignores config)" '
580 add_submodule_commits &&
583 git commit -m "new file" &&
584 new_head=$(git rev-parse --short HEAD) &&
585 write_expected_super $new_head &&
587 rm expect.err.deep &&
590 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
592 test_must_be_empty actual.out &&
593 verify_fetch_result actual.err
596 test_expect_success
"'fetch.recurseSubmodules=on-demand' overrides global config" '
599 git fetch --recurse-submodules
601 add_submodule_commits &&
602 git config --global fetch.recurseSubmodules false &&
604 git commit -m "new submodule" &&
605 new_head=$(git rev-parse --short HEAD) &&
606 write_expected_super $new_head &&
607 rm expect.err.deep &&
610 git config fetch.recurseSubmodules on-demand &&
611 git fetch >../actual.out 2>../actual.err
613 git config --global --unset fetch.recurseSubmodules &&
616 git config --unset fetch.recurseSubmodules
618 test_must_be_empty actual.out &&
619 verify_fetch_result actual.err
622 test_expect_success
"'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
625 git fetch --recurse-submodules
627 add_submodule_commits &&
628 git config fetch.recurseSubmodules false &&
630 git commit -m "new submodule" &&
631 new_head=$(git rev-parse --short HEAD) &&
632 write_expected_super $new_head &&
633 rm expect.err.deep &&
636 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
637 git fetch >../actual.out 2>../actual.err
639 git config --unset fetch.recurseSubmodules &&
642 git config --unset submodule.submodule.fetchRecurseSubmodules
644 test_must_be_empty actual.out &&
645 verify_fetch_result actual.err
648 test_expect_success
"don't fetch submodule when newly recorded commits are already present" '
651 git checkout -q HEAD^^
654 git commit -m "submodule rewound" &&
655 new_head=$(git rev-parse --short HEAD) &&
656 write_expected_super $new_head &&
658 # This file does not exist, but rm -f for readability
659 rm -f expect.err.deep &&
662 git fetch >../actual.out 2>../actual.err
664 test_must_be_empty actual.out &&
665 verify_fetch_result actual.err &&
672 test_expect_success
"'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
675 git fetch --recurse-submodules
677 add_submodule_commits &&
679 git rm .gitmodules &&
680 git commit -m "new submodule without .gitmodules" &&
681 new_head=$(git rev-parse --short HEAD) &&
682 write_expected_super $new_head &&
683 rm expect.err.deep &&
687 git config fetch.recurseSubmodules on-demand &&
688 # fake submodule configuration to avoid skipping submodule handling
689 git config -f .gitmodules submodule.fake.path fake &&
690 git config -f .gitmodules submodule.fake.url fakeurl &&
691 git add .gitmodules &&
692 git config --unset submodule.submodule.url &&
693 git fetch >../actual.out 2>../actual.err &&
695 git config --unset fetch.recurseSubmodules &&
698 test_must_be_empty actual.out &&
699 verify_fetch_result actual.err &&
700 git checkout HEAD^ -- .gitmodules &&
701 git add .gitmodules &&
702 git commit -m "new submodule restored .gitmodules"
705 test_expect_success
'fetching submodules respects parallel settings' '
706 git config fetch.recurseSubmodules true &&
709 GIT_TRACE=$(pwd)/trace.out git fetch &&
710 grep "1 tasks" trace.out &&
711 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 7 &&
712 grep "7 tasks" trace.out &&
713 git config submodule.fetchJobs 8 &&
714 GIT_TRACE=$(pwd)/trace.out git fetch &&
715 grep "8 tasks" trace.out &&
716 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 9 &&
717 grep "9 tasks" trace.out
721 test_expect_success
'fetching submodule into a broken repository' '
722 # Prepare src and src/sub nested in it
727 git -C sub commit --allow-empty -m "initial in sub" &&
728 git submodule add -- ./sub sub &&
729 git commit -m "initial in top"
732 # Clone the old-fashoned way
734 git -C dst clone ../src/sub sub &&
736 # Make sure that old-fashoned layout is still supported
739 # "diff" would find no change
740 git -C dst diff --exit-code &&
742 # Recursive-fetch works fine
743 git -C dst fetch --recurse-submodules &&
745 # Break the receiving submodule
746 rm -f dst/sub/.git/HEAD &&
748 # NOTE: without the fix the following tests will recurse forever!
749 # They should terminate with an error.
751 test_must_fail git -C dst status &&
752 test_must_fail git -C dst diff &&
753 test_must_fail git -C dst fetch --recurse-submodules
756 test_expect_success
"fetch new commits when submodule got renamed" '
757 git clone . downstream_rename &&
759 cd downstream_rename &&
760 git submodule update --init --recursive &&
761 git checkout -b rename &&
762 git mv submodule submodule_renamed &&
764 cd submodule_renamed &&
765 git checkout -b rename_sub &&
769 git push origin rename_sub &&
770 git rev-parse HEAD >../../expect
772 git add submodule_renamed &&
773 git commit -m "update renamed submodule" &&
774 git push origin rename
778 git fetch --recurse-submodules=on-demand &&
781 git rev-parse origin/rename_sub >../../actual
784 test_cmp expect actual
787 test_expect_success
"fetch new submodule commits on-demand outside standard refspec" '
788 # add a second submodule and ensure it is around in downstream first
789 git clone submodule sub1 &&
790 git submodule add ./sub1 &&
791 git commit -m "adding a second submodule" &&
792 git -C downstream pull &&
793 git -C downstream submodule update --init --recursive &&
795 git checkout --detach &&
797 C=$(git -C submodule commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
798 git -C submodule update-ref refs/changes/1 $C &&
799 git update-index --cacheinfo 160000 $C submodule &&
802 D=$(git -C sub1 commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
803 git -C sub1 update-ref refs/changes/2 $D &&
804 git update-index --cacheinfo 160000 $D sub1 &&
806 git commit -m "updated submodules outside of refs/heads" &&
807 E=$(git rev-parse HEAD) &&
808 git update-ref refs/changes/3 $E &&
811 git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
812 git -C submodule cat-file -t $C &&
813 git -C sub1 cat-file -t $D &&
814 git checkout --recurse-submodules FETCH_HEAD
818 test_expect_success
'fetch new submodule commit on-demand in FETCH_HEAD' '
819 # depends on the previous test for setup
821 C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
822 git -C submodule update-ref refs/changes/4 $C &&
823 git update-index --cacheinfo 160000 $C submodule &&
826 D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
827 git -C sub1 update-ref refs/changes/5 $D &&
828 git update-index --cacheinfo 160000 $D sub1 &&
830 git commit -m "updated submodules outside of refs/heads" &&
831 E=$(git rev-parse HEAD) &&
832 git update-ref refs/changes/6 $E &&
835 git fetch --recurse-submodules origin refs/changes/6 &&
836 git -C submodule cat-file -t $C &&
837 git -C sub1 cat-file -t $D &&
838 git checkout --recurse-submodules FETCH_HEAD
842 test_expect_success
'fetch new submodule commits on-demand without .gitmodules entry' '
843 # depends on the previous test for setup
845 git config -f .gitmodules --remove-section submodule.sub1 &&
846 git add .gitmodules &&
847 git commit -m "delete gitmodules file" &&
848 git checkout -B super &&
849 git -C downstream fetch &&
850 git -C downstream checkout origin/super &&
852 C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
853 git -C submodule update-ref refs/changes/7 $C &&
854 git update-index --cacheinfo 160000 $C submodule &&
857 D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
858 git -C sub1 update-ref refs/changes/8 $D &&
859 git update-index --cacheinfo 160000 $D sub1 &&
861 git commit -m "updated submodules outside of refs/heads" &&
862 E=$(git rev-parse HEAD) &&
863 git update-ref refs/changes/9 $E &&
866 git fetch --recurse-submodules origin refs/changes/9 &&
867 git -C submodule cat-file -t $C &&
868 git -C sub1 cat-file -t $D &&
869 git checkout --recurse-submodules FETCH_HEAD
873 test_expect_success
'fetch new submodule commit intermittently referenced by superproject' '
874 # depends on the previous test for setup
876 D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
877 E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
878 F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
880 git -C sub1 update-ref refs/changes/10 $D &&
881 git update-index --cacheinfo 160000 $D sub1 &&
882 git commit -m "updated submodules outside of refs/heads" &&
884 git -C sub1 update-ref refs/changes/11 $E &&
885 git update-index --cacheinfo 160000 $E sub1 &&
886 git commit -m "updated submodules outside of refs/heads" &&
888 git -C sub1 update-ref refs/changes/12 $F &&
889 git update-index --cacheinfo 160000 $F sub1 &&
890 git commit -m "updated submodules outside of refs/heads" &&
892 G=$(git rev-parse HEAD) &&
893 git update-ref refs/changes/13 $G &&
896 git fetch --recurse-submodules origin refs/changes/13 &&
898 git -C sub1 cat-file -t $D &&
899 git -C sub1 cat-file -t $E &&
900 git -C sub1 cat-file -t $F
908 git
-C "$dir" add
"$@" &&
909 git
-C "$dir" commit
-a -m "$msg" &&
913 compare_refs_in_dir
() {
920 git
-C "$1" rev-parse
--verify "$2" >expect
&&
921 git
-C "$3" rev-parse
--verify "$4" >actual
&&
922 eval $fail test_cmp expect actual
926 test_expect_success
'setup nested submodule fetch test' '
927 # does not depend on any previous test setups
929 for repo in outer middle inner
931 git init --bare $repo &&
932 git clone $repo ${repo}_content &&
933 echo "$repo" >"${repo}_content/file" &&
934 add_commit_push ${repo}_content "initial" file ||
939 git -C A submodule add "$pwd/middle" &&
940 git -C A/middle/ submodule add "$pwd/inner" &&
941 add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
942 add_commit_push A/ "adding middle sub" .gitmodules middle &&
945 git -C B/ submodule update --init middle &&
947 compare_refs_in_dir A HEAD B HEAD &&
948 compare_refs_in_dir A/middle HEAD B/middle HEAD &&
949 test_path_is_file B/file &&
950 test_path_is_file B/middle/file &&
951 test_path_is_missing B/middle/inner/file &&
953 echo "change on inner repo of A" >"A/middle/inner/file" &&
954 add_commit_push A/middle/inner "change on inner" file &&
955 add_commit_push A/middle "change on inner" inner &&
956 add_commit_push A "change on inner" middle
959 test_expect_success
'fetching a superproject containing an uninitialized sub/sub project' '
960 # depends on previous test for setup
963 compare_refs_in_dir A origin/HEAD B origin/HEAD
966 fetch_with_recursion_abort
() {
967 # In a regression the following git call will run into infinite recursion.
968 # To handle that, we connect the sed command to the git call by a pipe
969 # so that sed can kill the infinite recursion when detected.
970 # The recursion creates git output like:
971 # Fetching submodule sub
972 # Fetching submodule sub/sub <-- [1]
973 # Fetching submodule sub/sub/sub
975 # [1] sed will stop reading and cause git to eventually stop and die
977 git
-C "$1" fetch
--recurse-submodules 2>&1 |
978 sed "/Fetching submodule $2[^$]/q" >out
&&
979 ! grep "Fetching submodule $2[^$]" out
982 test_expect_success
'setup recursive fetch with uninit submodule' '
983 # does not depend on any previous test setups
985 test_create_repo super &&
986 test_commit -C super initial &&
987 test_create_repo sub &&
988 test_commit -C sub initial &&
989 git -C sub rev-parse HEAD >expect &&
991 git -C super submodule add ../sub &&
992 git -C super commit -m "add sub" &&
994 git clone super superclone &&
995 git -C superclone submodule status >out &&
996 sed -e "s/^-//" -e "s/ sub.*$//" out >actual &&
997 test_cmp expect actual
1000 test_expect_success
'recursive fetch with uninit submodule' '
1001 # depends on previous test for setup
1003 fetch_with_recursion_abort superclone sub &&
1004 git -C superclone submodule status >out &&
1005 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1006 test_cmp expect actual
1009 test_expect_success
'recursive fetch after deinit a submodule' '
1010 # depends on previous test for setup
1012 git -C superclone submodule update --init sub &&
1013 git -C superclone submodule deinit -f sub &&
1015 fetch_with_recursion_abort superclone sub &&
1016 git -C superclone submodule status >out &&
1017 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1018 test_cmp expect actual
1021 test_expect_success
'setup repo with upstreams that share a submodule name' '
1022 mkdir same-name-1 &&
1026 test_commit --no-tag a
1028 git clone same-name-1 same-name-2 &&
1029 # same-name-1 and same-name-2 both add a submodule with the
1034 git -C submodule init -b main &&
1035 test_commit -C submodule --no-tag a1 &&
1036 git submodule add "$pwd/same-name-1/submodule" &&
1037 git add submodule &&
1038 git commit -m "super-a1"
1043 git -C submodule init -b main &&
1044 test_commit -C submodule --no-tag a2 &&
1045 git submodule add "$pwd/same-name-2/submodule" &&
1046 git add submodule &&
1047 git commit -m "super-a2"
1049 git clone same-name-1 -o same-name-1 same-name-downstream &&
1051 cd same-name-downstream &&
1052 git remote add same-name-2 ../same-name-2 &&
1054 # init downstream with same-name-1
1055 git submodule update --init
1059 test_expect_success
'fetch --recurse-submodules updates name-conflicted, populated submodule' '
1060 test_when_finished "git -C same-name-downstream checkout main" &&
1063 test_commit -C submodule --no-tag b1 &&
1064 git add submodule &&
1065 git commit -m "super-b1"
1069 test_commit -C submodule --no-tag b2 &&
1070 git add submodule &&
1071 git commit -m "super-b2"
1074 cd same-name-downstream &&
1075 # even though the .gitmodules is correct, we cannot
1076 # fetch from same-name-2
1077 git checkout same-name-2/main &&
1078 git fetch --recurse-submodules same-name-1 &&
1079 test_must_fail git fetch --recurse-submodules same-name-2
1081 super_head1=$(git -C same-name-1 rev-parse HEAD) &&
1082 git -C same-name-downstream cat-file -e $super_head1 &&
1084 super_head2=$(git -C same-name-2 rev-parse HEAD) &&
1085 git -C same-name-downstream cat-file -e $super_head2 &&
1087 sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1088 git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
1090 sub_head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1091 test_must_fail git -C same-name-downstream/submodule cat-file -e $sub_head2
1094 test_expect_success
'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
1097 test_commit -C submodule --no-tag c1 &&
1098 git add submodule &&
1099 git commit -m "super-c1"
1103 test_commit -C submodule --no-tag c2 &&
1104 git add submodule &&
1105 git commit -m "super-c2"
1108 cd same-name-downstream &&
1109 git checkout main &&
1110 git rm .gitmodules &&
1112 git commit -m "no submodules" &&
1113 git fetch --recurse-submodules same-name-1
1115 head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1116 head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1118 cd same-name-downstream/.git/modules/submodule &&
1119 # The submodule has core.worktree pointing to the "git
1120 # rm"-ed directory, overwrite the invalid value. See
1121 # comment in get_fetch_task_from_changed() for more
1123 git --work-tree=. cat-file -e $head1 &&
1124 test_must_fail git --work-tree=. cat-file -e $head2