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
() {
15 cat >"$pwd/expect.err.sub" <<-EOF
16 Fetching submodule submodule
18 OLD_HEAD..$NEW_HEAD sub -> origin/sub
22 write_expected_deep
() {
24 cat >"$pwd/expect.err.deep" <<-EOF
25 Fetching submodule submodule/subdir/deepsubmodule
26 From $pwd/deepsubmodule
27 OLD_HEAD..$NEW_HEAD deep -> origin/deep
31 write_expected_super
() {
33 cat >"$pwd/expect.err.super" <<-EOF
35 OLD_HEAD..$NEW_HEAD super -> origin/super
39 # For each submodule in the test setup, this creates a commit and writes
40 # a file that contains the expected err if that new commit were fetched.
41 # These output files get concatenated in the right order by
42 # verify_fetch_result().
43 add_upstream_commit
() {
46 echo new
>> subfile
&&
49 git commit
-m new subfile
&&
50 new_head
=$
(git rev-parse
--short HEAD
) &&
51 write_expected_sub
$new_head
55 echo new
>> deepsubfile
&&
57 git add deepsubfile
&&
58 git commit
-m new deepsubfile
&&
59 new_head
=$
(git rev-parse
--short HEAD
) &&
60 write_expected_deep
$new_head
64 # Verifies that the expected repositories were fetched. This is done by
65 # concatenating the files expect.err.[super|sub|deep] in the correct
66 # order and comparing it to the actual stderr.
68 # If a repo should not be fetched in the test, its corresponding
69 # expect.err file should be rm-ed.
70 verify_fetch_result
() {
72 rm -f expect.err.combined
&&
73 if test -f expect.err.super
75 cat expect.err.super
>>expect.err.combined
77 if test -f expect.err.sub
79 cat expect.err.sub
>>expect.err.combined
81 if test -f expect.err.deep
83 cat expect.err.deep
>>expect.err.combined
85 sed -e 's/[0-9a-f][0-9a-f]*\.\./OLD_HEAD\.\./' "$ACTUAL_ERR" >actual.err.
cmp &&
86 test_cmp expect.err.combined actual.err.
cmp
89 test_expect_success setup
'
90 mkdir deepsubmodule &&
94 echo deepsubcontent > deepsubfile &&
95 git add deepsubfile &&
96 git commit -m new deepsubfile &&
103 echo subcontent > subfile &&
105 git submodule add "$pwd/deepsubmodule" subdir/deepsubmodule &&
106 git commit -a -m new &&
109 git submodule add "$pwd/submodule" submodule &&
110 git commit -am initial &&
111 git branch -M super &&
112 git clone . downstream &&
115 git submodule update --init --recursive
119 test_expect_success
"fetch --recurse-submodules recurses into submodules" '
120 add_upstream_commit &&
123 git fetch --recurse-submodules >../actual.out 2>../actual.err
125 test_must_be_empty actual.out &&
126 verify_fetch_result actual.err
129 test_expect_success
"submodule.recurse option triggers recursive fetch" '
130 add_upstream_commit &&
133 git -c submodule.recurse fetch >../actual.out 2>../actual.err
135 test_must_be_empty actual.out &&
136 verify_fetch_result actual.err
139 test_expect_success
"fetch --recurse-submodules -j2 has the same output behaviour" '
140 add_upstream_commit &&
143 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
145 test_must_be_empty actual.out &&
146 verify_fetch_result actual.err &&
147 grep "2 tasks" trace.out
150 test_expect_success
"fetch alone only fetches superproject" '
151 add_upstream_commit &&
154 git fetch >../actual.out 2>../actual.err
156 test_must_be_empty actual.out &&
157 test_must_be_empty actual.err
160 test_expect_success
"fetch --no-recurse-submodules only fetches superproject" '
163 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
165 test_must_be_empty actual.out &&
166 test_must_be_empty actual.err
169 test_expect_success
"using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
172 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
173 git fetch >../actual.out 2>../actual.err
175 test_must_be_empty actual.out &&
176 verify_fetch_result actual.err
179 test_expect_success
"--no-recurse-submodules overrides .gitmodules config" '
180 add_upstream_commit &&
183 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
185 test_must_be_empty actual.out &&
186 test_must_be_empty actual.err
189 test_expect_success
"using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
192 git config submodule.submodule.fetchRecurseSubmodules false &&
193 git fetch >../actual.out 2>../actual.err
195 test_must_be_empty actual.out &&
196 test_must_be_empty actual.err
199 test_expect_success
"--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
202 git fetch --recurse-submodules >../actual.out 2>../actual.err &&
203 git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
204 git config --unset submodule.submodule.fetchRecurseSubmodules
206 test_must_be_empty actual.out &&
207 verify_fetch_result actual.err
210 test_expect_success
"--quiet propagates to submodules" '
213 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
215 test_must_be_empty actual.out &&
216 test_must_be_empty actual.err
219 test_expect_success
"--quiet propagates to parallel submodules" '
222 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
224 test_must_be_empty actual.out &&
225 test_must_be_empty actual.err
228 test_expect_success
"--dry-run propagates to submodules" '
229 add_upstream_commit &&
232 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
234 test_must_be_empty actual.out &&
235 verify_fetch_result actual.err
238 test_expect_success
"Without --dry-run propagates to submodules" '
241 git fetch --recurse-submodules >../actual.out 2>../actual.err
243 test_must_be_empty actual.out &&
244 verify_fetch_result actual.err
247 test_expect_success
"recurseSubmodules=true propagates into submodules" '
248 add_upstream_commit &&
251 git config fetch.recurseSubmodules true &&
252 git fetch >../actual.out 2>../actual.err
254 test_must_be_empty actual.out &&
255 verify_fetch_result actual.err
258 test_expect_success
"--recurse-submodules overrides config in submodule" '
259 add_upstream_commit &&
264 git config fetch.recurseSubmodules false
266 git fetch --recurse-submodules >../actual.out 2>../actual.err
268 test_must_be_empty actual.out &&
269 verify_fetch_result actual.err
272 test_expect_success
"--no-recurse-submodules overrides config setting" '
273 add_upstream_commit &&
276 git config fetch.recurseSubmodules true &&
277 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
279 test_must_be_empty actual.out &&
280 test_must_be_empty actual.err
283 test_expect_success
"Recursion doesn't happen when no new commits are fetched in the superproject" '
288 git config --unset fetch.recurseSubmodules
290 git config --unset fetch.recurseSubmodules &&
291 git fetch >../actual.out 2>../actual.err
293 test_must_be_empty actual.out &&
294 test_must_be_empty actual.err
297 test_expect_success
"Recursion stops when no new submodule commits are fetched" '
299 git commit -m "new submodule" &&
300 new_head=$(git rev-parse --short HEAD) &&
301 write_expected_super $new_head &&
302 rm expect.err.deep &&
305 git fetch >../actual.out 2>../actual.err
307 verify_fetch_result actual.err &&
308 test_must_be_empty actual.out
311 test_expect_success
"Recursion doesn't happen when new superproject commits don't change any submodules" '
312 add_upstream_commit &&
315 git commit -m "new file" &&
316 new_head=$(git rev-parse --short HEAD) &&
317 write_expected_super $new_head &&
319 rm expect.err.deep &&
322 git fetch >../actual.out 2>../actual.err
324 test_must_be_empty actual.out &&
325 verify_fetch_result actual.err
328 test_expect_success
"Recursion picks up config in submodule" '
331 git fetch --recurse-submodules &&
334 git config fetch.recurseSubmodules true
337 add_upstream_commit &&
339 git commit -m "new submodule" &&
340 new_head=$(git rev-parse --short HEAD) &&
341 write_expected_super $new_head &&
344 git fetch >../actual.out 2>../actual.err &&
347 git config --unset fetch.recurseSubmodules
350 verify_fetch_result actual.err &&
351 test_must_be_empty actual.out
354 test_expect_success
"Recursion picks up all submodules when necessary" '
355 add_upstream_commit &&
359 cd subdir/deepsubmodule &&
361 git checkout -q FETCH_HEAD
363 git add subdir/deepsubmodule &&
364 git commit -m "new deepsubmodule" &&
365 new_head=$(git rev-parse --short HEAD) &&
366 write_expected_sub $new_head
369 git commit -m "new submodule" &&
370 new_head=$(git rev-parse --short HEAD) &&
371 write_expected_super $new_head &&
374 git fetch >../actual.out 2>../actual.err
376 verify_fetch_result actual.err &&
377 test_must_be_empty actual.out
380 test_expect_success
"'--recurse-submodules=on-demand' doesn't recurse when no new commits are fetched in the superproject (and ignores config)" '
381 add_upstream_commit &&
385 cd subdir/deepsubmodule &&
387 git checkout -q FETCH_HEAD
389 git add subdir/deepsubmodule &&
390 git commit -m "new deepsubmodule" &&
391 new_head=$(git rev-parse --short HEAD) &&
392 write_expected_sub $new_head
396 git config fetch.recurseSubmodules true &&
397 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
398 git config --unset fetch.recurseSubmodules
400 test_must_be_empty actual.out &&
401 test_must_be_empty actual.err
404 test_expect_success
"'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
406 git commit -m "new submodule" &&
407 new_head=$(git rev-parse --short HEAD) &&
408 write_expected_super $new_head &&
411 git config fetch.recurseSubmodules false &&
414 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
416 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
417 git config --unset fetch.recurseSubmodules &&
420 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
423 test_must_be_empty actual.out &&
424 verify_fetch_result actual.err
427 test_expect_success
"'--recurse-submodules=on-demand' stops when no new submodule commits are found in the superproject (and ignores config)" '
428 add_upstream_commit &&
431 git commit -m "new file" &&
432 new_head=$(git rev-parse --short HEAD) &&
433 write_expected_super $new_head &&
435 rm expect.err.deep &&
438 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
440 test_must_be_empty actual.out &&
441 verify_fetch_result actual.err
444 test_expect_success
"'fetch.recurseSubmodules=on-demand' overrides global config" '
447 git fetch --recurse-submodules
449 add_upstream_commit &&
450 git config --global fetch.recurseSubmodules false &&
452 git commit -m "new submodule" &&
453 new_head=$(git rev-parse --short HEAD) &&
454 write_expected_super $new_head &&
455 rm expect.err.deep &&
458 git config fetch.recurseSubmodules on-demand &&
459 git fetch >../actual.out 2>../actual.err
461 git config --global --unset fetch.recurseSubmodules &&
464 git config --unset fetch.recurseSubmodules
466 test_must_be_empty actual.out &&
467 verify_fetch_result actual.err
470 test_expect_success
"'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
473 git fetch --recurse-submodules
475 add_upstream_commit &&
476 git config fetch.recurseSubmodules false &&
478 git commit -m "new submodule" &&
479 new_head=$(git rev-parse --short HEAD) &&
480 write_expected_super $new_head &&
481 rm expect.err.deep &&
484 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
485 git fetch >../actual.out 2>../actual.err
487 git config --unset fetch.recurseSubmodules &&
490 git config --unset submodule.submodule.fetchRecurseSubmodules
492 test_must_be_empty actual.out &&
493 verify_fetch_result actual.err
496 test_expect_success
"don't fetch submodule when newly recorded commits are already present" '
499 git checkout -q HEAD^^
502 git commit -m "submodule rewound" &&
503 new_head=$(git rev-parse --short HEAD) &&
504 write_expected_super $new_head &&
506 # This file does not exist, but rm -f for readability
507 rm -f expect.err.deep &&
510 git fetch >../actual.out 2>../actual.err
512 test_must_be_empty actual.out &&
513 verify_fetch_result actual.err &&
520 test_expect_success
"'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
523 git fetch --recurse-submodules
525 add_upstream_commit &&
527 git rm .gitmodules &&
528 git commit -m "new submodule without .gitmodules" &&
529 new_head=$(git rev-parse --short HEAD) &&
530 write_expected_super $new_head &&
531 rm expect.err.deep &&
535 git config fetch.recurseSubmodules on-demand &&
536 # fake submodule configuration to avoid skipping submodule handling
537 git config -f .gitmodules submodule.fake.path fake &&
538 git config -f .gitmodules submodule.fake.url fakeurl &&
539 git add .gitmodules &&
540 git config --unset submodule.submodule.url &&
541 git fetch >../actual.out 2>../actual.err &&
543 git config --unset fetch.recurseSubmodules &&
546 test_must_be_empty actual.out &&
547 verify_fetch_result actual.err &&
548 git checkout HEAD^ -- .gitmodules &&
549 git add .gitmodules &&
550 git commit -m "new submodule restored .gitmodules"
553 test_expect_success
'fetching submodules respects parallel settings' '
554 git config fetch.recurseSubmodules true &&
557 GIT_TRACE=$(pwd)/trace.out git fetch &&
558 grep "1 tasks" trace.out &&
559 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 7 &&
560 grep "7 tasks" trace.out &&
561 git config submodule.fetchJobs 8 &&
562 GIT_TRACE=$(pwd)/trace.out git fetch &&
563 grep "8 tasks" trace.out &&
564 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 9 &&
565 grep "9 tasks" trace.out
569 test_expect_success
'fetching submodule into a broken repository' '
570 # Prepare src and src/sub nested in it
575 git -C sub commit --allow-empty -m "initial in sub" &&
576 git submodule add -- ./sub sub &&
577 git commit -m "initial in top"
580 # Clone the old-fashoned way
582 git -C dst clone ../src/sub sub &&
584 # Make sure that old-fashoned layout is still supported
587 # "diff" would find no change
588 git -C dst diff --exit-code &&
590 # Recursive-fetch works fine
591 git -C dst fetch --recurse-submodules &&
593 # Break the receiving submodule
594 rm -f dst/sub/.git/HEAD &&
596 # NOTE: without the fix the following tests will recurse forever!
597 # They should terminate with an error.
599 test_must_fail git -C dst status &&
600 test_must_fail git -C dst diff &&
601 test_must_fail git -C dst fetch --recurse-submodules
604 test_expect_success
"fetch new commits when submodule got renamed" '
605 git clone . downstream_rename &&
607 cd downstream_rename &&
608 git submodule update --init --recursive &&
609 git checkout -b rename &&
610 git mv submodule submodule_renamed &&
612 cd submodule_renamed &&
613 git checkout -b rename_sub &&
617 git push origin rename_sub &&
618 git rev-parse HEAD >../../expect
620 git add submodule_renamed &&
621 git commit -m "update renamed submodule" &&
622 git push origin rename
626 git fetch --recurse-submodules=on-demand &&
629 git rev-parse origin/rename_sub >../../actual
632 test_cmp expect actual
635 test_expect_success
"fetch new submodule commits on-demand outside standard refspec" '
636 # add a second submodule and ensure it is around in downstream first
637 git clone submodule sub1 &&
638 git submodule add ./sub1 &&
639 git commit -m "adding a second submodule" &&
640 git -C downstream pull &&
641 git -C downstream submodule update --init --recursive &&
643 git checkout --detach &&
645 C=$(git -C submodule commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
646 git -C submodule update-ref refs/changes/1 $C &&
647 git update-index --cacheinfo 160000 $C submodule &&
650 D=$(git -C sub1 commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
651 git -C sub1 update-ref refs/changes/2 $D &&
652 git update-index --cacheinfo 160000 $D sub1 &&
654 git commit -m "updated submodules outside of refs/heads" &&
655 E=$(git rev-parse HEAD) &&
656 git update-ref refs/changes/3 $E &&
659 git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
660 git -C submodule cat-file -t $C &&
661 git -C sub1 cat-file -t $D &&
662 git checkout --recurse-submodules FETCH_HEAD
666 test_expect_success
'fetch new submodule commit on-demand in FETCH_HEAD' '
667 # depends on the previous test for setup
669 C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
670 git -C submodule update-ref refs/changes/4 $C &&
671 git update-index --cacheinfo 160000 $C submodule &&
674 D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
675 git -C sub1 update-ref refs/changes/5 $D &&
676 git update-index --cacheinfo 160000 $D sub1 &&
678 git commit -m "updated submodules outside of refs/heads" &&
679 E=$(git rev-parse HEAD) &&
680 git update-ref refs/changes/6 $E &&
683 git fetch --recurse-submodules origin refs/changes/6 &&
684 git -C submodule cat-file -t $C &&
685 git -C sub1 cat-file -t $D &&
686 git checkout --recurse-submodules FETCH_HEAD
690 test_expect_success
'fetch new submodule commits on-demand without .gitmodules entry' '
691 # depends on the previous test for setup
693 git config -f .gitmodules --remove-section submodule.sub1 &&
694 git add .gitmodules &&
695 git commit -m "delete gitmodules file" &&
696 git checkout -B super &&
697 git -C downstream fetch &&
698 git -C downstream checkout origin/super &&
700 C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
701 git -C submodule update-ref refs/changes/7 $C &&
702 git update-index --cacheinfo 160000 $C submodule &&
705 D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
706 git -C sub1 update-ref refs/changes/8 $D &&
707 git update-index --cacheinfo 160000 $D sub1 &&
709 git commit -m "updated submodules outside of refs/heads" &&
710 E=$(git rev-parse HEAD) &&
711 git update-ref refs/changes/9 $E &&
714 git fetch --recurse-submodules origin refs/changes/9 &&
715 git -C submodule cat-file -t $C &&
716 git -C sub1 cat-file -t $D &&
717 git checkout --recurse-submodules FETCH_HEAD
721 test_expect_success
'fetch new submodule commit intermittently referenced by superproject' '
722 # depends on the previous test for setup
724 D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
725 E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
726 F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
728 git -C sub1 update-ref refs/changes/10 $D &&
729 git update-index --cacheinfo 160000 $D sub1 &&
730 git commit -m "updated submodules outside of refs/heads" &&
732 git -C sub1 update-ref refs/changes/11 $E &&
733 git update-index --cacheinfo 160000 $E sub1 &&
734 git commit -m "updated submodules outside of refs/heads" &&
736 git -C sub1 update-ref refs/changes/12 $F &&
737 git update-index --cacheinfo 160000 $F sub1 &&
738 git commit -m "updated submodules outside of refs/heads" &&
740 G=$(git rev-parse HEAD) &&
741 git update-ref refs/changes/13 $G &&
744 git fetch --recurse-submodules origin refs/changes/13 &&
746 git -C sub1 cat-file -t $D &&
747 git -C sub1 cat-file -t $E &&
748 git -C sub1 cat-file -t $F
756 git
-C "$dir" add
"$@" &&
757 git
-C "$dir" commit
-a -m "$msg" &&
761 compare_refs_in_dir
() {
768 git
-C "$1" rev-parse
--verify "$2" >expect
&&
769 git
-C "$3" rev-parse
--verify "$4" >actual
&&
770 eval $fail test_cmp expect actual
774 test_expect_success
'setup nested submodule fetch test' '
775 # does not depend on any previous test setups
777 for repo in outer middle inner
779 git init --bare $repo &&
780 git clone $repo ${repo}_content &&
781 echo "$repo" >"${repo}_content/file" &&
782 add_commit_push ${repo}_content "initial" file ||
787 git -C A submodule add "$pwd/middle" &&
788 git -C A/middle/ submodule add "$pwd/inner" &&
789 add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
790 add_commit_push A/ "adding middle sub" .gitmodules middle &&
793 git -C B/ submodule update --init middle &&
795 compare_refs_in_dir A HEAD B HEAD &&
796 compare_refs_in_dir A/middle HEAD B/middle HEAD &&
797 test_path_is_file B/file &&
798 test_path_is_file B/middle/file &&
799 test_path_is_missing B/middle/inner/file &&
801 echo "change on inner repo of A" >"A/middle/inner/file" &&
802 add_commit_push A/middle/inner "change on inner" file &&
803 add_commit_push A/middle "change on inner" inner &&
804 add_commit_push A "change on inner" middle
807 test_expect_success
'fetching a superproject containing an uninitialized sub/sub project' '
808 # depends on previous test for setup
811 compare_refs_in_dir A origin/HEAD B origin/HEAD
814 fetch_with_recursion_abort
() {
815 # In a regression the following git call will run into infinite recursion.
816 # To handle that, we connect the sed command to the git call by a pipe
817 # so that sed can kill the infinite recursion when detected.
818 # The recursion creates git output like:
819 # Fetching submodule sub
820 # Fetching submodule sub/sub <-- [1]
821 # Fetching submodule sub/sub/sub
823 # [1] sed will stop reading and cause git to eventually stop and die
825 git
-C "$1" fetch
--recurse-submodules 2>&1 |
826 sed "/Fetching submodule $2[^$]/q" >out
&&
827 ! grep "Fetching submodule $2[^$]" out
830 test_expect_success
'setup recursive fetch with uninit submodule' '
831 # does not depend on any previous test setups
833 test_create_repo super &&
834 test_commit -C super initial &&
835 test_create_repo sub &&
836 test_commit -C sub initial &&
837 git -C sub rev-parse HEAD >expect &&
839 git -C super submodule add ../sub &&
840 git -C super commit -m "add sub" &&
842 git clone super superclone &&
843 git -C superclone submodule status >out &&
844 sed -e "s/^-//" -e "s/ sub.*$//" out >actual &&
845 test_cmp expect actual
848 test_expect_success
'recursive fetch with uninit submodule' '
849 # depends on previous test for setup
851 fetch_with_recursion_abort superclone sub &&
852 git -C superclone submodule status >out &&
853 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
854 test_cmp expect actual
857 test_expect_success
'recursive fetch after deinit a submodule' '
858 # depends on previous test for setup
860 git -C superclone submodule update --init sub &&
861 git -C superclone submodule deinit -f sub &&
863 fetch_with_recursion_abort superclone sub &&
864 git -C superclone submodule status >out &&
865 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
866 test_cmp expect actual