rebase: use correct base for --keep-base when a branch is given
[alt-git.git] / t / t5526-fetch-submodules.sh
blob43dada854443dcf430439dffd6d38d5d5be4f3d0
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 mkdir deepsubmodule &&
132 cd deepsubmodule &&
133 git init &&
134 echo deepsubcontent > deepsubfile &&
135 git add deepsubfile &&
136 git commit -m new deepsubfile &&
137 git branch -M deep
138 ) &&
139 mkdir submodule &&
141 cd submodule &&
142 git init &&
143 echo subcontent > subfile &&
144 git add subfile &&
145 git submodule add "$pwd/deepsubmodule" subdir/deepsubmodule &&
146 git commit -a -m new &&
147 git branch -M sub
148 ) &&
149 git submodule add "$pwd/submodule" submodule &&
150 git commit -am initial &&
151 git branch -M super &&
152 git clone . downstream &&
154 cd downstream &&
155 git submodule update --init --recursive
159 test_expect_success "fetch --recurse-submodules recurses into submodules" '
160 add_submodule_commits &&
162 cd downstream &&
163 git fetch --recurse-submodules >../actual.out 2>../actual.err
164 ) &&
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 &&
172 cd downstream &&
173 git -c submodule.recurse fetch >../actual.out 2>../actual.err
174 ) &&
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 &&
182 cd downstream &&
183 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
184 ) &&
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 &&
193 cd downstream &&
194 git fetch >../actual.out 2>../actual.err
195 ) &&
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" '
202 cd downstream &&
203 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
204 ) &&
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" '
211 cd downstream &&
212 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
213 git fetch >../actual.out 2>../actual.err
214 ) &&
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 &&
222 cd downstream &&
223 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
224 ) &&
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" '
231 cd downstream &&
232 git config submodule.submodule.fetchRecurseSubmodules false &&
233 git fetch >../actual.out 2>../actual.err
234 ) &&
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" '
241 cd downstream &&
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
245 ) &&
246 test_must_be_empty actual.out &&
247 verify_fetch_result actual.err
250 test_expect_success "--quiet propagates to submodules" '
252 cd downstream &&
253 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
254 ) &&
255 test_must_be_empty actual.out &&
256 test_must_be_empty actual.err
259 test_expect_success "--quiet propagates to parallel submodules" '
261 cd downstream &&
262 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
263 ) &&
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 &&
271 cd downstream &&
272 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
273 ) &&
274 test_must_be_empty actual.out &&
275 verify_fetch_result actual.err
278 test_expect_success "Without --dry-run propagates to submodules" '
280 cd downstream &&
281 git fetch --recurse-submodules >../actual.out 2>../actual.err
282 ) &&
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 &&
290 cd downstream &&
291 git config fetch.recurseSubmodules true &&
292 git fetch >../actual.out 2>../actual.err
293 ) &&
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 &&
301 cd downstream &&
303 cd submodule &&
304 git config fetch.recurseSubmodules false
305 ) &&
306 git fetch --recurse-submodules >../actual.out 2>../actual.err
307 ) &&
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 &&
315 cd downstream &&
316 git config fetch.recurseSubmodules true &&
317 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
318 ) &&
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" '
325 cd downstream &&
327 cd submodule &&
328 git config --unset fetch.recurseSubmodules
329 ) &&
330 git config --unset fetch.recurseSubmodules &&
331 git fetch >../actual.out 2>../actual.err
332 ) &&
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" '
338 git add submodule &&
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 &&
344 cd downstream &&
345 git fetch >../actual.out 2>../actual.err
346 ) &&
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 &&
353 echo a > file &&
354 git add file &&
355 git commit -m "new file" &&
356 new_head=$(git rev-parse --short HEAD) &&
357 write_expected_super $new_head &&
358 rm expect.err.sub &&
359 rm expect.err.deep &&
361 cd downstream &&
362 git fetch >../actual.out 2>../actual.err
363 ) &&
364 test_must_be_empty actual.out &&
365 verify_fetch_result actual.err
368 test_expect_success "Recursion picks up config in submodule" '
370 cd downstream &&
371 git fetch --recurse-submodules &&
373 cd submodule &&
374 git config fetch.recurseSubmodules true
376 ) &&
377 add_submodule_commits &&
378 git add submodule &&
379 git commit -m "new submodule" &&
380 new_head=$(git rev-parse --short HEAD) &&
381 write_expected_super $new_head &&
383 cd downstream &&
384 git fetch >../actual.out 2>../actual.err &&
386 cd submodule &&
387 git config --unset fetch.recurseSubmodules
389 ) &&
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 &&
398 cd downstream &&
399 git fetch >../actual.out 2>../actual.err
400 ) &&
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 &&
408 cd downstream &&
409 git config fetch.recurseSubmodules true &&
410 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
411 git config --unset fetch.recurseSubmodules
412 ) &&
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 &&
421 cd downstream &&
422 git config fetch.recurseSubmodules false &&
424 cd submodule &&
425 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
426 ) &&
427 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
428 git config --unset fetch.recurseSubmodules &&
430 cd submodule &&
431 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
433 ) &&
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
439 # index.
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' '
445 cd downstream &&
446 git checkout --recurse-submodules -b no-submodules &&
447 git rm .gitmodules &&
448 git rm submodule &&
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
459 cd downstream &&
460 git switch --recurse-submodules no-submodules &&
461 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
462 ) &&
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
480 cd downstream &&
481 git switch --recurse-submodules no-submodules &&
482 git fetch --recurse-submodules >../actual.out 2>../actual.err
483 ) &&
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
502 cd downstream &&
503 git switch --recurse-submodules no-submodules &&
504 git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
505 ) &&
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
510 rm expect.err.sub &&
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
517 # operation.
518 test_expect_success 'setup downstream branch with other submodule' '
519 mkdir submodule2 &&
521 cd submodule2 &&
522 git init &&
523 echo sub2content >sub2file &&
524 git add sub2file &&
525 git commit -a -m new &&
526 git branch -M sub2
527 ) &&
528 git checkout -b super-sub2-only &&
529 git submodule add "$pwd/submodule2" submodule2 &&
530 git commit -m "add sub2" &&
531 git checkout super &&
533 cd downstream &&
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 &&
552 cd submodule2 &&
553 test_commit --no-tag foo
554 ) &&
555 git add submodule2 &&
556 git commit -m "new submodule2" &&
558 git checkout super &&
560 cd downstream &&
561 git fetch --recurse-submodules >../actual.out 2>../actual.err
562 ) &&
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 &&
572 From $pwd/.
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 &&
581 echo a >> file &&
582 git add file &&
583 git commit -m "new file" &&
584 new_head=$(git rev-parse --short HEAD) &&
585 write_expected_super $new_head &&
586 rm expect.err.sub &&
587 rm expect.err.deep &&
589 cd downstream &&
590 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
591 ) &&
592 test_must_be_empty actual.out &&
593 verify_fetch_result actual.err
596 test_expect_success "'fetch.recurseSubmodules=on-demand' overrides global config" '
598 cd downstream &&
599 git fetch --recurse-submodules
600 ) &&
601 add_submodule_commits &&
602 git config --global fetch.recurseSubmodules false &&
603 git add submodule &&
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 &&
609 cd downstream &&
610 git config fetch.recurseSubmodules on-demand &&
611 git fetch >../actual.out 2>../actual.err
612 ) &&
613 git config --global --unset fetch.recurseSubmodules &&
615 cd downstream &&
616 git config --unset fetch.recurseSubmodules
617 ) &&
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" '
624 cd downstream &&
625 git fetch --recurse-submodules
626 ) &&
627 add_submodule_commits &&
628 git config fetch.recurseSubmodules false &&
629 git add submodule &&
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 &&
635 cd downstream &&
636 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
637 git fetch >../actual.out 2>../actual.err
638 ) &&
639 git config --unset fetch.recurseSubmodules &&
641 cd downstream &&
642 git config --unset submodule.submodule.fetchRecurseSubmodules
643 ) &&
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" '
650 cd submodule &&
651 git checkout -q HEAD^^
652 ) &&
653 git add submodule &&
654 git commit -m "submodule rewound" &&
655 new_head=$(git rev-parse --short HEAD) &&
656 write_expected_super $new_head &&
657 rm expect.err.sub &&
658 # This file does not exist, but rm -f for readability
659 rm -f expect.err.deep &&
661 cd downstream &&
662 git fetch >../actual.out 2>../actual.err
663 ) &&
664 test_must_be_empty actual.out &&
665 verify_fetch_result actual.err &&
667 cd submodule &&
668 git checkout -q sub
672 test_expect_success "'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
674 cd downstream &&
675 git fetch --recurse-submodules
676 ) &&
677 add_submodule_commits &&
678 git add submodule &&
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 &&
685 cd downstream &&
686 rm .gitmodules &&
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 &&
694 # cleanup
695 git config --unset fetch.recurseSubmodules &&
696 git reset --hard
697 ) &&
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 &&
708 cd downstream &&
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
723 git init src &&
725 cd src &&
726 git init sub &&
727 git -C sub commit --allow-empty -m "initial in sub" &&
728 git submodule add -- ./sub sub &&
729 git commit -m "initial in top"
730 ) &&
732 # Clone the old-fashoned way
733 git clone src dst &&
734 git -C dst clone ../src/sub sub &&
736 # Make sure that old-fashoned layout is still supported
737 git -C dst status &&
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 &&
766 echo a >a &&
767 git add a &&
768 git commit -ma &&
769 git push origin rename_sub &&
770 git rev-parse HEAD >../../expect
771 ) &&
772 git add submodule_renamed &&
773 git commit -m "update renamed submodule" &&
774 git push origin rename
775 ) &&
777 cd downstream &&
778 git fetch --recurse-submodules=on-demand &&
780 cd submodule &&
781 git rev-parse origin/rename_sub >../../actual
783 ) &&
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 &&
800 test_tick &&
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 &&
810 cd downstream &&
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 &&
824 test_tick &&
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 &&
834 cd downstream &&
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 &&
855 test_tick &&
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 &&
865 cd downstream &&
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 &&
895 cd downstream &&
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
904 add_commit_push () {
905 dir="$1" &&
906 msg="$2" &&
907 shift 2 &&
908 git -C "$dir" add "$@" &&
909 git -C "$dir" commit -a -m "$msg" &&
910 git -C "$dir" push
913 compare_refs_in_dir () {
914 fail= &&
915 if test "x$1" = 'x!'
916 then
917 fail='!' &&
918 shift
919 fi &&
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 ||
935 return 1
936 done &&
938 git clone outer A &&
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 &&
944 git clone outer B &&
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
962 git -C B/ fetch &&
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
974 # ...
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 &&
1024 cd same-name-1 &&
1025 git init -b main &&
1026 test_commit --no-tag a
1027 ) &&
1028 git clone same-name-1 same-name-2 &&
1029 # same-name-1 and same-name-2 both add a submodule with the
1030 # name "submodule"
1032 cd same-name-1 &&
1033 mkdir submodule &&
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"
1039 ) &&
1041 cd same-name-2 &&
1042 mkdir submodule &&
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"
1048 ) &&
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 &&
1053 git fetch --all &&
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" &&
1062 cd same-name-1 &&
1063 test_commit -C submodule --no-tag b1 &&
1064 git add submodule &&
1065 git commit -m "super-b1"
1066 ) &&
1068 cd same-name-2 &&
1069 test_commit -C submodule --no-tag b2 &&
1070 git add submodule &&
1071 git commit -m "super-b2"
1072 ) &&
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
1080 ) &&
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' '
1096 cd same-name-1 &&
1097 test_commit -C submodule --no-tag c1 &&
1098 git add submodule &&
1099 git commit -m "super-c1"
1100 ) &&
1102 cd same-name-2 &&
1103 test_commit -C submodule --no-tag c2 &&
1104 git add submodule &&
1105 git commit -m "super-c2"
1106 ) &&
1108 cd same-name-downstream &&
1109 git checkout main &&
1110 git rm .gitmodules &&
1111 git rm submodule &&
1112 git commit -m "no submodules" &&
1113 git fetch --recurse-submodules same-name-1
1114 ) &&
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
1122 # information.
1123 git --work-tree=. cat-file -e $head1 &&
1124 test_must_fail git --work-tree=. cat-file -e $head2
1128 test_done