pack-objects: simplify --filter handling
[git/debian.git] / t / t5526-fetch-submodules.sh
blob75da8acf8f436f5a50908f8b3a9e3fe194d1d440
1 #!/bin/sh
2 # Copyright (c) 2010, Jens Lehmann
4 test_description='Recursive "git fetch" for submodules'
6 GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
7 export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
9 . ./test-lib.sh
11 pwd=$(pwd)
13 write_expected_sub () {
14 NEW_HEAD=$1 &&
15 SUPER_HEAD=$2 &&
16 cat >"$pwd/expect.err.sub" <<-EOF
17 Fetching submodule submodule${SUPER_HEAD:+ at commit $SUPER_HEAD}
18 From $pwd/submodule
19 OLD_HEAD..$NEW_HEAD sub -> origin/sub
20 EOF
23 write_expected_sub2 () {
24 NEW_HEAD=$1 &&
25 SUPER_HEAD=$2 &&
26 cat >"$pwd/expect.err.sub2" <<-EOF
27 Fetching submodule submodule2${SUPER_HEAD:+ at commit $SUPER_HEAD}
28 From $pwd/submodule2
29 OLD_HEAD..$NEW_HEAD sub2 -> origin/sub2
30 EOF
33 write_expected_deep () {
34 NEW_HEAD=$1 &&
35 SUB_HEAD=$2 &&
36 cat >"$pwd/expect.err.deep" <<-EOF
37 Fetching submodule submodule/subdir/deepsubmodule${SUB_HEAD:+ at commit $SUB_HEAD}
38 From $pwd/deepsubmodule
39 OLD_HEAD..$NEW_HEAD deep -> origin/deep
40 EOF
43 write_expected_super () {
44 NEW_HEAD=$1 &&
45 cat >"$pwd/expect.err.super" <<-EOF
46 From $pwd/.
47 OLD_HEAD..$NEW_HEAD super -> origin/super
48 EOF
51 # For each submodule in the test setup, this creates a commit and writes
52 # a file that contains the expected err if that new commit were fetched.
53 # These output files get concatenated in the right order by
54 # verify_fetch_result().
55 add_submodule_commits () {
57 cd submodule &&
58 echo new >> subfile &&
59 test_tick &&
60 git add subfile &&
61 git commit -m new subfile &&
62 new_head=$(git rev-parse --short HEAD) &&
63 write_expected_sub $new_head
64 ) &&
66 cd deepsubmodule &&
67 echo new >> deepsubfile &&
68 test_tick &&
69 git add deepsubfile &&
70 git commit -m new deepsubfile &&
71 new_head=$(git rev-parse --short HEAD) &&
72 write_expected_deep $new_head
76 # For each superproject in the test setup, update its submodule, add the
77 # submodule and create a new commit with the submodule change.
79 # This requires add_submodule_commits() to be called first, otherwise
80 # the submodules will not have changed and cannot be "git add"-ed.
81 add_superproject_commits () {
83 cd submodule &&
85 cd subdir/deepsubmodule &&
86 git fetch &&
87 git checkout -q FETCH_HEAD
88 ) &&
89 git add subdir/deepsubmodule &&
90 git commit -m "new deep submodule"
91 ) &&
92 git add submodule &&
93 git commit -m "new submodule" &&
94 super_head=$(git rev-parse --short HEAD) &&
95 sub_head=$(git -C submodule rev-parse --short HEAD) &&
96 write_expected_super $super_head &&
97 write_expected_sub $sub_head
100 # Verifies that the expected repositories were fetched. This is done by
101 # concatenating the files expect.err.[super|sub|deep] in the correct
102 # order and comparing it to the actual stderr.
104 # If a repo should not be fetched in the test, its corresponding
105 # expect.err file should be rm-ed.
106 verify_fetch_result () {
107 ACTUAL_ERR=$1 &&
108 rm -f expect.err.combined &&
109 if test -f expect.err.super
110 then
111 cat expect.err.super >>expect.err.combined
112 fi &&
113 if test -f expect.err.sub
114 then
115 cat expect.err.sub >>expect.err.combined
116 fi &&
117 if test -f expect.err.deep
118 then
119 cat expect.err.deep >>expect.err.combined
120 fi &&
121 if test -f expect.err.sub2
122 then
123 cat expect.err.sub2 >>expect.err.combined
124 fi &&
125 sed -e 's/[0-9a-f][0-9a-f]*\.\./OLD_HEAD\.\./' "$ACTUAL_ERR" >actual.err.cmp &&
126 test_cmp expect.err.combined actual.err.cmp
129 test_expect_success setup '
130 git config --global protocol.file.allow always &&
131 mkdir deepsubmodule &&
133 cd deepsubmodule &&
134 git init &&
135 echo deepsubcontent > deepsubfile &&
136 git add deepsubfile &&
137 git commit -m new deepsubfile &&
138 git branch -M deep
139 ) &&
140 mkdir submodule &&
142 cd submodule &&
143 git init &&
144 echo subcontent > subfile &&
145 git add subfile &&
146 git submodule add "$pwd/deepsubmodule" subdir/deepsubmodule &&
147 git commit -a -m new &&
148 git branch -M sub
149 ) &&
150 git submodule add "$pwd/submodule" submodule &&
151 git commit -am initial &&
152 git branch -M super &&
153 git clone . downstream &&
155 cd downstream &&
156 git submodule update --init --recursive
160 test_expect_success "fetch --recurse-submodules recurses into submodules" '
161 add_submodule_commits &&
163 cd downstream &&
164 git fetch --recurse-submodules >../actual.out 2>../actual.err
165 ) &&
166 test_must_be_empty actual.out &&
167 verify_fetch_result actual.err
170 test_expect_success "submodule.recurse option triggers recursive fetch" '
171 add_submodule_commits &&
173 cd downstream &&
174 git -c submodule.recurse fetch >../actual.out 2>../actual.err
175 ) &&
176 test_must_be_empty actual.out &&
177 verify_fetch_result actual.err
180 test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" '
181 add_submodule_commits &&
183 cd downstream &&
184 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
185 ) &&
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 &&
194 cd downstream &&
195 git fetch >../actual.out 2>../actual.err
196 ) &&
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" '
203 cd downstream &&
204 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
205 ) &&
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" '
212 cd downstream &&
213 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
214 git fetch >../actual.out 2>../actual.err
215 ) &&
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 &&
223 cd downstream &&
224 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
225 ) &&
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" '
232 cd downstream &&
233 git config submodule.submodule.fetchRecurseSubmodules false &&
234 git fetch >../actual.out 2>../actual.err
235 ) &&
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" '
242 cd downstream &&
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
246 ) &&
247 test_must_be_empty actual.out &&
248 verify_fetch_result actual.err
251 test_expect_success "--quiet propagates to submodules" '
253 cd downstream &&
254 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
255 ) &&
256 test_must_be_empty actual.out &&
257 test_must_be_empty actual.err
260 test_expect_success "--quiet propagates to parallel submodules" '
262 cd downstream &&
263 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
264 ) &&
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 &&
272 cd downstream &&
273 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
274 ) &&
275 test_must_be_empty actual.out &&
276 verify_fetch_result actual.err
279 test_expect_success "Without --dry-run propagates to submodules" '
281 cd downstream &&
282 git fetch --recurse-submodules >../actual.out 2>../actual.err
283 ) &&
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 &&
291 cd downstream &&
292 git config fetch.recurseSubmodules true &&
293 git fetch >../actual.out 2>../actual.err
294 ) &&
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 &&
302 cd downstream &&
304 cd submodule &&
305 git config fetch.recurseSubmodules false
306 ) &&
307 git fetch --recurse-submodules >../actual.out 2>../actual.err
308 ) &&
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 &&
316 cd downstream &&
317 git config fetch.recurseSubmodules true &&
318 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
319 ) &&
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" '
326 cd downstream &&
328 cd submodule &&
329 git config --unset fetch.recurseSubmodules
330 ) &&
331 git config --unset fetch.recurseSubmodules &&
332 git fetch >../actual.out 2>../actual.err
333 ) &&
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" '
339 git add submodule &&
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 &&
345 cd downstream &&
346 git fetch >../actual.out 2>../actual.err
347 ) &&
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 &&
354 echo a > file &&
355 git add file &&
356 git commit -m "new file" &&
357 new_head=$(git rev-parse --short HEAD) &&
358 write_expected_super $new_head &&
359 rm expect.err.sub &&
360 rm expect.err.deep &&
362 cd downstream &&
363 git fetch >../actual.out 2>../actual.err
364 ) &&
365 test_must_be_empty actual.out &&
366 verify_fetch_result actual.err
369 test_expect_success "Recursion picks up config in submodule" '
371 cd downstream &&
372 git fetch --recurse-submodules &&
374 cd submodule &&
375 git config fetch.recurseSubmodules true
377 ) &&
378 add_submodule_commits &&
379 git add submodule &&
380 git commit -m "new submodule" &&
381 new_head=$(git rev-parse --short HEAD) &&
382 write_expected_super $new_head &&
384 cd downstream &&
385 git fetch >../actual.out 2>../actual.err &&
387 cd submodule &&
388 git config --unset fetch.recurseSubmodules
390 ) &&
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 &&
399 cd downstream &&
400 git fetch >../actual.out 2>../actual.err
401 ) &&
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 &&
409 cd downstream &&
410 git config fetch.recurseSubmodules true &&
411 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
412 git config --unset fetch.recurseSubmodules
413 ) &&
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 &&
422 cd downstream &&
423 git config fetch.recurseSubmodules false &&
425 cd submodule &&
426 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
427 ) &&
428 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
429 git config --unset fetch.recurseSubmodules &&
431 cd submodule &&
432 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
434 ) &&
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
440 # index.
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' '
446 cd downstream &&
447 git checkout --recurse-submodules -b no-submodules &&
448 git rm .gitmodules &&
449 git rm submodule &&
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
460 cd downstream &&
461 git switch --recurse-submodules no-submodules &&
462 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
463 ) &&
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
481 cd downstream &&
482 git switch --recurse-submodules no-submodules &&
483 git fetch --recurse-submodules >../actual.out 2>../actual.err
484 ) &&
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
503 cd downstream &&
504 git switch --recurse-submodules no-submodules &&
505 git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
506 ) &&
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
511 rm expect.err.sub &&
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
518 # operation.
519 test_expect_success 'setup downstream branch with other submodule' '
520 mkdir submodule2 &&
522 cd submodule2 &&
523 git init &&
524 echo sub2content >sub2file &&
525 git add sub2file &&
526 git commit -a -m new &&
527 git branch -M sub2
528 ) &&
529 git checkout -b super-sub2-only &&
530 git submodule add "$pwd/submodule2" submodule2 &&
531 git commit -m "add sub2" &&
532 git checkout super &&
534 cd downstream &&
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 &&
553 cd submodule2 &&
554 test_commit --no-tag foo
555 ) &&
556 git add submodule2 &&
557 git commit -m "new submodule2" &&
559 git checkout super &&
561 cd downstream &&
562 git fetch --recurse-submodules >../actual.out 2>../actual.err
563 ) &&
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 &&
573 From $pwd/.
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 &&
582 echo a >> file &&
583 git add file &&
584 git commit -m "new file" &&
585 new_head=$(git rev-parse --short HEAD) &&
586 write_expected_super $new_head &&
587 rm expect.err.sub &&
588 rm expect.err.deep &&
590 cd downstream &&
591 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
592 ) &&
593 test_must_be_empty actual.out &&
594 verify_fetch_result actual.err
597 test_expect_success "'fetch.recurseSubmodules=on-demand' overrides global config" '
599 cd downstream &&
600 git fetch --recurse-submodules
601 ) &&
602 add_submodule_commits &&
603 git config --global fetch.recurseSubmodules false &&
604 git add submodule &&
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 &&
610 cd downstream &&
611 git config fetch.recurseSubmodules on-demand &&
612 git fetch >../actual.out 2>../actual.err
613 ) &&
614 git config --global --unset fetch.recurseSubmodules &&
616 cd downstream &&
617 git config --unset fetch.recurseSubmodules
618 ) &&
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" '
625 cd downstream &&
626 git fetch --recurse-submodules
627 ) &&
628 add_submodule_commits &&
629 git config fetch.recurseSubmodules false &&
630 git add submodule &&
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 &&
636 cd downstream &&
637 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
638 git fetch >../actual.out 2>../actual.err
639 ) &&
640 git config --unset fetch.recurseSubmodules &&
642 cd downstream &&
643 git config --unset submodule.submodule.fetchRecurseSubmodules
644 ) &&
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" '
651 cd submodule &&
652 git checkout -q HEAD^^
653 ) &&
654 git add submodule &&
655 git commit -m "submodule rewound" &&
656 new_head=$(git rev-parse --short HEAD) &&
657 write_expected_super $new_head &&
658 rm expect.err.sub &&
659 # This file does not exist, but rm -f for readability
660 rm -f expect.err.deep &&
662 cd downstream &&
663 git fetch >../actual.out 2>../actual.err
664 ) &&
665 test_must_be_empty actual.out &&
666 verify_fetch_result actual.err &&
668 cd submodule &&
669 git checkout -q sub
673 test_expect_success "'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
675 cd downstream &&
676 git fetch --recurse-submodules
677 ) &&
678 add_submodule_commits &&
679 git add submodule &&
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 &&
686 cd downstream &&
687 rm .gitmodules &&
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 &&
695 # cleanup
696 git config --unset fetch.recurseSubmodules &&
697 git reset --hard
698 ) &&
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 &&
709 cd downstream &&
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 &&
719 >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 &&
724 >trace.out
728 test_expect_success 'fetching submodule into a broken repository' '
729 # Prepare src and src/sub nested in it
730 git init src &&
732 cd src &&
733 git init sub &&
734 git -C sub commit --allow-empty -m "initial in sub" &&
735 git submodule add -- ./sub sub &&
736 git commit -m "initial in top"
737 ) &&
739 # Clone the old-fashoned way
740 git clone src dst &&
741 git -C dst clone ../src/sub sub &&
743 # Make sure that old-fashoned layout is still supported
744 git -C dst status &&
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 &&
773 echo a >a &&
774 git add a &&
775 git commit -ma &&
776 git push origin rename_sub &&
777 git rev-parse HEAD >../../expect
778 ) &&
779 git add submodule_renamed &&
780 git commit -m "update renamed submodule" &&
781 git push origin rename
782 ) &&
784 cd downstream &&
785 git fetch --recurse-submodules=on-demand &&
787 cd submodule &&
788 git rev-parse origin/rename_sub >../../actual
790 ) &&
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 &&
807 test_tick &&
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 &&
817 cd downstream &&
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 &&
831 test_tick &&
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 &&
841 cd downstream &&
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 &&
862 test_tick &&
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 &&
872 cd downstream &&
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 &&
902 cd downstream &&
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
911 add_commit_push () {
912 dir="$1" &&
913 msg="$2" &&
914 shift 2 &&
915 git -C "$dir" add "$@" &&
916 git -C "$dir" commit -a -m "$msg" &&
917 git -C "$dir" push
920 compare_refs_in_dir () {
921 fail= &&
922 if test "x$1" = 'x!'
923 then
924 fail='!' &&
925 shift
926 fi &&
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 ||
942 return 1
943 done &&
945 git clone outer A &&
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 &&
951 git clone outer B &&
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
969 git -C B/ fetch &&
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
981 # ...
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 &&
1031 cd same-name-1 &&
1032 git init -b main &&
1033 test_commit --no-tag a
1034 ) &&
1035 git clone same-name-1 same-name-2 &&
1036 # same-name-1 and same-name-2 both add a submodule with the
1037 # name "submodule"
1039 cd same-name-1 &&
1040 mkdir submodule &&
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"
1046 ) &&
1048 cd same-name-2 &&
1049 mkdir submodule &&
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"
1055 ) &&
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 &&
1060 git fetch --all &&
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" &&
1069 cd same-name-1 &&
1070 test_commit -C submodule --no-tag b1 &&
1071 git add submodule &&
1072 git commit -m "super-b1"
1073 ) &&
1075 cd same-name-2 &&
1076 test_commit -C submodule --no-tag b2 &&
1077 git add submodule &&
1078 git commit -m "super-b2"
1079 ) &&
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
1087 ) &&
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' '
1103 cd same-name-1 &&
1104 test_commit -C submodule --no-tag c1 &&
1105 git add submodule &&
1106 git commit -m "super-c1"
1107 ) &&
1109 cd same-name-2 &&
1110 test_commit -C submodule --no-tag c2 &&
1111 git add submodule &&
1112 git commit -m "super-c2"
1113 ) &&
1115 cd same-name-downstream &&
1116 git checkout main &&
1117 git rm .gitmodules &&
1118 git rm submodule &&
1119 git commit -m "no submodules" &&
1120 git fetch --recurse-submodules same-name-1
1121 ) &&
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
1129 # information.
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 &&
1139 cd src_clone &&
1140 git config submodule.recurse true &&
1141 git config fetch.parallel 0 &&
1142 git fetch --all 2>../fetch-log
1143 ) &&
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 &&
1152 cd 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
1157 ) &&
1158 grep "Fetching submodule sub" fetch-log >fetch-subs &&
1159 test_line_count = 2 fetch-subs
1162 test_done