parse-options: recognize abbreviated negated option with arg
[git.git] / t / t5526-fetch-submodules.sh
blob5e566205ba4b95830bd97c97b0092cb4a3356304
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 "fetch --recurse-submodules honors --no-write-fetch-head" '
172 cd downstream &&
173 git submodule foreach --recursive \
174 sh -c "cd \"\$(git rev-parse --git-dir)\" && rm -f FETCH_HEAD" &&
176 git fetch --recurse-submodules --no-write-fetch-head &&
178 git submodule foreach --recursive \
179 sh -c "cd \"\$(git rev-parse --git-dir)\" && ! test -f FETCH_HEAD"
183 test_expect_success "submodule.recurse option triggers recursive fetch" '
184 add_submodule_commits &&
186 cd downstream &&
187 git -c submodule.recurse fetch >../actual.out 2>../actual.err
188 ) &&
189 test_must_be_empty actual.out &&
190 verify_fetch_result actual.err
193 test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" '
194 test_when_finished "rm -f trace.out" &&
195 add_submodule_commits &&
197 cd downstream &&
198 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
199 ) &&
200 test_must_be_empty actual.out &&
201 verify_fetch_result actual.err &&
202 grep "2 tasks" trace.out
205 test_expect_success "fetch alone only fetches superproject" '
206 add_submodule_commits &&
208 cd downstream &&
209 git fetch >../actual.out 2>../actual.err
210 ) &&
211 test_must_be_empty actual.out &&
212 test_must_be_empty actual.err
215 test_expect_success "fetch --no-recurse-submodules only fetches superproject" '
217 cd downstream &&
218 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
219 ) &&
220 test_must_be_empty actual.out &&
221 test_must_be_empty actual.err
224 test_expect_success "using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
226 cd downstream &&
227 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
228 git fetch >../actual.out 2>../actual.err
229 ) &&
230 test_must_be_empty actual.out &&
231 verify_fetch_result actual.err
234 test_expect_success "--no-recurse-submodules overrides .gitmodules config" '
235 add_submodule_commits &&
237 cd downstream &&
238 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
239 ) &&
240 test_must_be_empty actual.out &&
241 test_must_be_empty actual.err
244 test_expect_success "using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
246 cd downstream &&
247 git config submodule.submodule.fetchRecurseSubmodules false &&
248 git fetch >../actual.out 2>../actual.err
249 ) &&
250 test_must_be_empty actual.out &&
251 test_must_be_empty actual.err
254 test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
256 cd downstream &&
257 git fetch --recurse-submodules >../actual.out 2>../actual.err &&
258 git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
259 git config --unset submodule.submodule.fetchRecurseSubmodules
260 ) &&
261 test_must_be_empty actual.out &&
262 verify_fetch_result actual.err
265 test_expect_success "--quiet propagates to submodules" '
267 cd downstream &&
268 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
269 ) &&
270 test_must_be_empty actual.out &&
271 test_must_be_empty actual.err
274 test_expect_success "--quiet propagates to parallel submodules" '
276 cd downstream &&
277 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
278 ) &&
279 test_must_be_empty actual.out &&
280 test_must_be_empty actual.err
283 test_expect_success "--dry-run propagates to submodules" '
284 add_submodule_commits &&
286 cd downstream &&
287 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
288 ) &&
289 test_must_be_empty actual.out &&
290 verify_fetch_result actual.err
293 test_expect_success "Without --dry-run propagates to submodules" '
295 cd downstream &&
296 git fetch --recurse-submodules >../actual.out 2>../actual.err
297 ) &&
298 test_must_be_empty actual.out &&
299 verify_fetch_result actual.err
302 test_expect_success "recurseSubmodules=true propagates into submodules" '
303 add_submodule_commits &&
305 cd downstream &&
306 git config fetch.recurseSubmodules true &&
307 git fetch >../actual.out 2>../actual.err
308 ) &&
309 test_must_be_empty actual.out &&
310 verify_fetch_result actual.err
313 test_expect_success "--recurse-submodules overrides config in submodule" '
314 add_submodule_commits &&
316 cd downstream &&
318 cd submodule &&
319 git config fetch.recurseSubmodules false
320 ) &&
321 git fetch --recurse-submodules >../actual.out 2>../actual.err
322 ) &&
323 test_must_be_empty actual.out &&
324 verify_fetch_result actual.err
327 test_expect_success "--no-recurse-submodules overrides config setting" '
328 add_submodule_commits &&
330 cd downstream &&
331 git config fetch.recurseSubmodules true &&
332 git fetch --no-recurse-submodules >../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 doesn't happen when no new commits are fetched in the superproject" '
340 cd downstream &&
342 cd submodule &&
343 git config --unset fetch.recurseSubmodules
344 ) &&
345 git config --unset fetch.recurseSubmodules &&
346 git fetch >../actual.out 2>../actual.err
347 ) &&
348 test_must_be_empty actual.out &&
349 test_must_be_empty actual.err
352 test_expect_success "Recursion stops when no new submodule commits are fetched" '
353 git add submodule &&
354 git commit -m "new submodule" &&
355 new_head=$(git rev-parse --short HEAD) &&
356 write_expected_super $new_head &&
357 rm expect.err.deep &&
359 cd downstream &&
360 git fetch >../actual.out 2>../actual.err
361 ) &&
362 verify_fetch_result actual.err &&
363 test_must_be_empty actual.out
366 test_expect_success "Recursion doesn't happen when new superproject commits don't change any submodules" '
367 add_submodule_commits &&
368 echo a > file &&
369 git add file &&
370 git commit -m "new file" &&
371 new_head=$(git rev-parse --short HEAD) &&
372 write_expected_super $new_head &&
373 rm expect.err.sub &&
374 rm expect.err.deep &&
376 cd downstream &&
377 git fetch >../actual.out 2>../actual.err
378 ) &&
379 test_must_be_empty actual.out &&
380 verify_fetch_result actual.err
383 test_expect_success "Recursion picks up config in submodule" '
385 cd downstream &&
386 git fetch --recurse-submodules &&
388 cd submodule &&
389 git config fetch.recurseSubmodules true
391 ) &&
392 add_submodule_commits &&
393 git add submodule &&
394 git commit -m "new submodule" &&
395 new_head=$(git rev-parse --short HEAD) &&
396 write_expected_super $new_head &&
398 cd downstream &&
399 git fetch >../actual.out 2>../actual.err &&
401 cd submodule &&
402 git config --unset fetch.recurseSubmodules
404 ) &&
405 verify_fetch_result actual.err &&
406 test_must_be_empty actual.out
409 test_expect_success "Recursion picks up all submodules when necessary" '
410 add_submodule_commits &&
411 add_superproject_commits &&
413 cd downstream &&
414 git fetch >../actual.out 2>../actual.err
415 ) &&
416 verify_fetch_result actual.err &&
417 test_must_be_empty actual.out
420 test_expect_success "'--recurse-submodules=on-demand' doesn't recurse when no new commits are fetched in the superproject (and ignores config)" '
421 add_submodule_commits &&
423 cd downstream &&
424 git config fetch.recurseSubmodules true &&
425 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
426 git config --unset fetch.recurseSubmodules
427 ) &&
428 test_must_be_empty actual.out &&
429 test_must_be_empty actual.err
432 test_expect_success "'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
433 add_submodule_commits &&
434 add_superproject_commits &&
436 cd downstream &&
437 git config fetch.recurseSubmodules false &&
439 cd submodule &&
440 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
441 ) &&
442 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
443 git config --unset fetch.recurseSubmodules &&
445 cd submodule &&
446 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
448 ) &&
449 test_must_be_empty actual.out &&
450 verify_fetch_result actual.err
453 # These tests verify that we can fetch submodules that aren't in the
454 # index.
456 # First, test the simple case where the index is empty and we only fetch
457 # submodules that are not in the index.
458 test_expect_success 'setup downstream branch without submodules' '
460 cd downstream &&
461 git checkout --recurse-submodules -b no-submodules &&
462 git rm .gitmodules &&
463 git rm submodule &&
464 git commit -m "no submodules" &&
465 git checkout --recurse-submodules super
469 test_expect_success "'--recurse-submodules=on-demand' should fetch submodule commits if the submodule is changed but the index has no submodules" '
470 add_submodule_commits &&
471 add_superproject_commits &&
472 # Fetch the new superproject commit
474 cd downstream &&
475 git switch --recurse-submodules no-submodules &&
476 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
477 ) &&
478 super_head=$(git rev-parse --short HEAD) &&
479 sub_head=$(git -C submodule rev-parse --short HEAD) &&
480 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
482 # assert that these are fetched from commits, not the index
483 write_expected_sub $sub_head $super_head &&
484 write_expected_deep $deep_head $sub_head &&
486 test_must_be_empty actual.out &&
487 verify_fetch_result actual.err
490 test_expect_success "'--recurse-submodules' should fetch submodule commits if the submodule is changed but the index has no submodules" '
491 add_submodule_commits &&
492 add_superproject_commits &&
493 # Fetch the new superproject commit
495 cd downstream &&
496 git switch --recurse-submodules no-submodules &&
497 git fetch --recurse-submodules >../actual.out 2>../actual.err
498 ) &&
499 super_head=$(git rev-parse --short HEAD) &&
500 sub_head=$(git -C submodule rev-parse --short HEAD) &&
501 deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
503 # assert that these are fetched from commits, not the index
504 write_expected_sub $sub_head $super_head &&
505 write_expected_deep $deep_head $sub_head &&
507 test_must_be_empty actual.out &&
508 verify_fetch_result actual.err
511 test_expect_success "'--recurse-submodules' should ignore changed, inactive submodules" '
512 add_submodule_commits &&
513 add_superproject_commits &&
515 # Fetch the new superproject commit
517 cd downstream &&
518 git switch --recurse-submodules no-submodules &&
519 git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
520 ) &&
521 test_must_be_empty actual.out &&
522 super_head=$(git rev-parse --short HEAD) &&
523 write_expected_super $super_head &&
524 # Neither should be fetched because the submodule is inactive
525 rm expect.err.sub &&
526 rm expect.err.deep &&
527 verify_fetch_result actual.err
530 # Now that we know we can fetch submodules that are not in the index,
531 # test that we can fetch index and non-index submodules in the same
532 # operation.
533 test_expect_success 'setup downstream branch with other submodule' '
534 mkdir submodule2 &&
536 cd submodule2 &&
537 git init &&
538 echo sub2content >sub2file &&
539 git add sub2file &&
540 git commit -a -m new &&
541 git branch -M sub2
542 ) &&
543 git checkout -b super-sub2-only &&
544 git submodule add "$pwd/submodule2" submodule2 &&
545 git commit -m "add sub2" &&
546 git checkout super &&
548 cd downstream &&
549 git fetch --recurse-submodules origin &&
550 git checkout super-sub2-only &&
551 # Explicitly run "git submodule update" because sub2 is new
552 # and has not been cloned.
553 git submodule update --init &&
554 git checkout --recurse-submodules super
558 test_expect_success "'--recurse-submodules' should fetch submodule commits in changed submodules and the index" '
559 test_when_finished "rm expect.err.sub2" &&
560 # Create new commit in origin/super
561 add_submodule_commits &&
562 add_superproject_commits &&
564 # Create new commit in origin/super-sub2-only
565 git checkout super-sub2-only &&
567 cd submodule2 &&
568 test_commit --no-tag foo
569 ) &&
570 git add submodule2 &&
571 git commit -m "new submodule2" &&
573 git checkout super &&
575 cd downstream &&
576 git fetch --recurse-submodules >../actual.out 2>../actual.err
577 ) &&
578 test_must_be_empty actual.out &&
579 sub2_head=$(git -C submodule2 rev-parse --short HEAD) &&
580 super_head=$(git rev-parse --short super) &&
581 super_sub2_only_head=$(git rev-parse --short super-sub2-only) &&
582 write_expected_sub2 $sub2_head $super_sub2_only_head &&
584 # write_expected_super cannot handle >1 branch. Since this is a
585 # one-off, construct expect.err.super manually.
586 cat >"$pwd/expect.err.super" <<-EOF &&
587 From $pwd/.
588 OLD_HEAD..$super_head super -> origin/super
589 OLD_HEAD..$super_sub2_only_head super-sub2-only -> origin/super-sub2-only
591 verify_fetch_result actual.err
594 test_expect_success "'--recurse-submodules=on-demand' stops when no new submodule commits are found in the superproject (and ignores config)" '
595 add_submodule_commits &&
596 echo a >> file &&
597 git add file &&
598 git commit -m "new file" &&
599 new_head=$(git rev-parse --short HEAD) &&
600 write_expected_super $new_head &&
601 rm expect.err.sub &&
602 rm expect.err.deep &&
604 cd downstream &&
605 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
606 ) &&
607 test_must_be_empty actual.out &&
608 verify_fetch_result actual.err
611 test_expect_success "'fetch.recurseSubmodules=on-demand' overrides global config" '
613 cd downstream &&
614 git fetch --recurse-submodules
615 ) &&
616 add_submodule_commits &&
617 git config --global fetch.recurseSubmodules false &&
618 git add submodule &&
619 git commit -m "new submodule" &&
620 new_head=$(git rev-parse --short HEAD) &&
621 write_expected_super $new_head &&
622 rm expect.err.deep &&
624 cd downstream &&
625 git config fetch.recurseSubmodules on-demand &&
626 git fetch >../actual.out 2>../actual.err
627 ) &&
628 git config --global --unset fetch.recurseSubmodules &&
630 cd downstream &&
631 git config --unset fetch.recurseSubmodules
632 ) &&
633 test_must_be_empty actual.out &&
634 verify_fetch_result actual.err
637 test_expect_success "'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
639 cd downstream &&
640 git fetch --recurse-submodules
641 ) &&
642 add_submodule_commits &&
643 git config fetch.recurseSubmodules false &&
644 git add submodule &&
645 git commit -m "new submodule" &&
646 new_head=$(git rev-parse --short HEAD) &&
647 write_expected_super $new_head &&
648 rm expect.err.deep &&
650 cd downstream &&
651 git config submodule.submodule.fetchRecurseSubmodules on-demand &&
652 git fetch >../actual.out 2>../actual.err
653 ) &&
654 git config --unset fetch.recurseSubmodules &&
656 cd downstream &&
657 git config --unset submodule.submodule.fetchRecurseSubmodules
658 ) &&
659 test_must_be_empty actual.out &&
660 verify_fetch_result actual.err
663 test_expect_success "don't fetch submodule when newly recorded commits are already present" '
665 cd submodule &&
666 git checkout -q HEAD^^
667 ) &&
668 git add submodule &&
669 git commit -m "submodule rewound" &&
670 new_head=$(git rev-parse --short HEAD) &&
671 write_expected_super $new_head &&
672 rm expect.err.sub &&
673 # This file does not exist, but rm -f for readability
674 rm -f expect.err.deep &&
676 cd downstream &&
677 git fetch >../actual.out 2>../actual.err
678 ) &&
679 test_must_be_empty actual.out &&
680 verify_fetch_result actual.err &&
682 cd submodule &&
683 git checkout -q sub
687 test_expect_success "'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
689 cd downstream &&
690 git fetch --recurse-submodules
691 ) &&
692 add_submodule_commits &&
693 git add submodule &&
694 git rm .gitmodules &&
695 git commit -m "new submodule without .gitmodules" &&
696 new_head=$(git rev-parse --short HEAD) &&
697 write_expected_super $new_head &&
698 rm expect.err.deep &&
700 cd downstream &&
701 rm .gitmodules &&
702 git config fetch.recurseSubmodules on-demand &&
703 # fake submodule configuration to avoid skipping submodule handling
704 git config -f .gitmodules submodule.fake.path fake &&
705 git config -f .gitmodules submodule.fake.url fakeurl &&
706 git add .gitmodules &&
707 git config --unset submodule.submodule.url &&
708 git fetch >../actual.out 2>../actual.err &&
709 # cleanup
710 git config --unset fetch.recurseSubmodules &&
711 git reset --hard
712 ) &&
713 test_must_be_empty actual.out &&
714 verify_fetch_result actual.err &&
715 git checkout HEAD^ -- .gitmodules &&
716 git add .gitmodules &&
717 git commit -m "new submodule restored .gitmodules"
720 test_expect_success 'fetching submodules respects parallel settings' '
721 git config fetch.recurseSubmodules true &&
722 test_when_finished "rm -f downstream/trace.out" &&
724 cd downstream &&
725 GIT_TRACE=$(pwd)/trace.out git fetch &&
726 grep "1 tasks" trace.out &&
727 >trace.out &&
729 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 7 &&
730 grep "7 tasks" trace.out &&
731 >trace.out &&
733 git config submodule.fetchJobs 8 &&
734 GIT_TRACE=$(pwd)/trace.out git fetch &&
735 grep "8 tasks" trace.out &&
736 >trace.out &&
738 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 9 &&
739 grep "9 tasks" trace.out &&
740 >trace.out &&
742 GIT_TRACE=$(pwd)/trace.out git -c submodule.fetchJobs=0 fetch &&
743 grep "preparing to run up to [0-9]* tasks" trace.out &&
744 ! grep "up to 0 tasks" trace.out &&
745 >trace.out
749 test_expect_success 'fetching submodule into a broken repository' '
750 # Prepare src and src/sub nested in it
751 git init src &&
753 cd src &&
754 git init sub &&
755 git -C sub commit --allow-empty -m "initial in sub" &&
756 git submodule add -- ./sub sub &&
757 git commit -m "initial in top"
758 ) &&
760 # Clone the old-fashoned way
761 git clone src dst &&
762 git -C dst clone ../src/sub sub &&
764 # Make sure that old-fashoned layout is still supported
765 git -C dst status &&
767 # "diff" would find no change
768 git -C dst diff --exit-code &&
770 # Recursive-fetch works fine
771 git -C dst fetch --recurse-submodules &&
773 # Break the receiving submodule
774 rm -r dst/sub/.git/objects &&
776 # NOTE: without the fix the following tests will recurse forever!
777 # They should terminate with an error.
779 test_must_fail git -C dst status &&
780 test_must_fail git -C dst diff &&
781 test_must_fail git -C dst fetch --recurse-submodules
784 test_expect_success "fetch new commits when submodule got renamed" '
785 git clone . downstream_rename &&
787 cd downstream_rename &&
788 git submodule update --init --recursive &&
789 git checkout -b rename &&
790 git mv submodule submodule_renamed &&
792 cd submodule_renamed &&
793 git checkout -b rename_sub &&
794 echo a >a &&
795 git add a &&
796 git commit -ma &&
797 git push origin rename_sub &&
798 git rev-parse HEAD >../../expect
799 ) &&
800 git add submodule_renamed &&
801 git commit -m "update renamed submodule" &&
802 git push origin rename
803 ) &&
805 cd downstream &&
806 git fetch --recurse-submodules=on-demand &&
808 cd submodule &&
809 git rev-parse origin/rename_sub >../../actual
811 ) &&
812 test_cmp expect actual
815 test_expect_success "fetch new submodule commits on-demand outside standard refspec" '
816 # add a second submodule and ensure it is around in downstream first
817 git clone submodule sub1 &&
818 git submodule add ./sub1 &&
819 git commit -m "adding a second submodule" &&
820 git -C downstream pull &&
821 git -C downstream submodule update --init --recursive &&
823 git checkout --detach &&
825 C=$(git -C submodule commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
826 git -C submodule update-ref refs/changes/1 $C &&
827 git update-index --cacheinfo 160000 $C submodule &&
828 test_tick &&
830 D=$(git -C sub1 commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
831 git -C sub1 update-ref refs/changes/2 $D &&
832 git update-index --cacheinfo 160000 $D sub1 &&
834 git commit -m "updated submodules outside of refs/heads" &&
835 E=$(git rev-parse HEAD) &&
836 git update-ref refs/changes/3 $E &&
838 cd downstream &&
839 git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
840 git -C submodule cat-file -t $C &&
841 git -C sub1 cat-file -t $D &&
842 git checkout --recurse-submodules FETCH_HEAD
846 test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD' '
847 # depends on the previous test for setup
849 C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
850 git -C submodule update-ref refs/changes/4 $C &&
851 git update-index --cacheinfo 160000 $C submodule &&
852 test_tick &&
854 D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
855 git -C sub1 update-ref refs/changes/5 $D &&
856 git update-index --cacheinfo 160000 $D sub1 &&
858 git commit -m "updated submodules outside of refs/heads" &&
859 E=$(git rev-parse HEAD) &&
860 git update-ref refs/changes/6 $E &&
862 cd downstream &&
863 git fetch --recurse-submodules origin refs/changes/6 &&
864 git -C submodule cat-file -t $C &&
865 git -C sub1 cat-file -t $D &&
866 git checkout --recurse-submodules FETCH_HEAD
870 test_expect_success 'fetch new submodule commits on-demand without .gitmodules entry' '
871 # depends on the previous test for setup
873 git config -f .gitmodules --remove-section submodule.sub1 &&
874 git add .gitmodules &&
875 git commit -m "delete gitmodules file" &&
876 git checkout -B super &&
877 git -C downstream fetch &&
878 git -C downstream checkout origin/super &&
880 C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
881 git -C submodule update-ref refs/changes/7 $C &&
882 git update-index --cacheinfo 160000 $C submodule &&
883 test_tick &&
885 D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
886 git -C sub1 update-ref refs/changes/8 $D &&
887 git update-index --cacheinfo 160000 $D sub1 &&
889 git commit -m "updated submodules outside of refs/heads" &&
890 E=$(git rev-parse HEAD) &&
891 git update-ref refs/changes/9 $E &&
893 cd downstream &&
894 git fetch --recurse-submodules origin refs/changes/9 &&
895 git -C submodule cat-file -t $C &&
896 git -C sub1 cat-file -t $D &&
897 git checkout --recurse-submodules FETCH_HEAD
901 test_expect_success 'fetch new submodule commit intermittently referenced by superproject' '
902 # depends on the previous test for setup
904 D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
905 E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
906 F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
908 git -C sub1 update-ref refs/changes/10 $D &&
909 git update-index --cacheinfo 160000 $D sub1 &&
910 git commit -m "updated submodules outside of refs/heads" &&
912 git -C sub1 update-ref refs/changes/11 $E &&
913 git update-index --cacheinfo 160000 $E sub1 &&
914 git commit -m "updated submodules outside of refs/heads" &&
916 git -C sub1 update-ref refs/changes/12 $F &&
917 git update-index --cacheinfo 160000 $F sub1 &&
918 git commit -m "updated submodules outside of refs/heads" &&
920 G=$(git rev-parse HEAD) &&
921 git update-ref refs/changes/13 $G &&
923 cd downstream &&
924 git fetch --recurse-submodules origin refs/changes/13 &&
926 git -C sub1 cat-file -t $D &&
927 git -C sub1 cat-file -t $E &&
928 git -C sub1 cat-file -t $F
932 add_commit_push () {
933 dir="$1" &&
934 msg="$2" &&
935 shift 2 &&
936 git -C "$dir" add "$@" &&
937 git -C "$dir" commit -a -m "$msg" &&
938 git -C "$dir" push
941 compare_refs_in_dir () {
942 fail= &&
943 if test "x$1" = 'x!'
944 then
945 fail='!' &&
946 shift
947 fi &&
948 git -C "$1" rev-parse --verify "$2" >expect &&
949 git -C "$3" rev-parse --verify "$4" >actual &&
950 eval $fail test_cmp expect actual
954 test_expect_success 'setup nested submodule fetch test' '
955 # does not depend on any previous test setups
957 for repo in outer middle inner
959 git init --bare $repo &&
960 git clone $repo ${repo}_content &&
961 echo "$repo" >"${repo}_content/file" &&
962 add_commit_push ${repo}_content "initial" file ||
963 return 1
964 done &&
966 git clone outer A &&
967 git -C A submodule add "$pwd/middle" &&
968 git -C A/middle/ submodule add "$pwd/inner" &&
969 add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
970 add_commit_push A/ "adding middle sub" .gitmodules middle &&
972 git clone outer B &&
973 git -C B/ submodule update --init middle &&
975 compare_refs_in_dir A HEAD B HEAD &&
976 compare_refs_in_dir A/middle HEAD B/middle HEAD &&
977 test_path_is_file B/file &&
978 test_path_is_file B/middle/file &&
979 test_path_is_missing B/middle/inner/file &&
981 echo "change on inner repo of A" >"A/middle/inner/file" &&
982 add_commit_push A/middle/inner "change on inner" file &&
983 add_commit_push A/middle "change on inner" inner &&
984 add_commit_push A "change on inner" middle
987 test_expect_success 'fetching a superproject containing an uninitialized sub/sub project' '
988 # depends on previous test for setup
990 git -C B/ fetch &&
991 compare_refs_in_dir A origin/HEAD B origin/HEAD
994 fetch_with_recursion_abort () {
995 # In a regression the following git call will run into infinite recursion.
996 # To handle that, we connect the sed command to the git call by a pipe
997 # so that sed can kill the infinite recursion when detected.
998 # The recursion creates git output like:
999 # Fetching submodule sub
1000 # Fetching submodule sub/sub <-- [1]
1001 # Fetching submodule sub/sub/sub
1002 # ...
1003 # [1] sed will stop reading and cause git to eventually stop and die
1005 git -C "$1" fetch --recurse-submodules 2>&1 |
1006 sed "/Fetching submodule $2[^$]/q" >out &&
1007 ! grep "Fetching submodule $2[^$]" out
1010 test_expect_success 'setup recursive fetch with uninit submodule' '
1011 # does not depend on any previous test setups
1013 test_create_repo super &&
1014 test_commit -C super initial &&
1015 test_create_repo sub &&
1016 test_commit -C sub initial &&
1017 git -C sub rev-parse HEAD >expect &&
1019 git -C super submodule add ../sub &&
1020 git -C super commit -m "add sub" &&
1022 git clone super superclone &&
1023 git -C superclone submodule status >out &&
1024 sed -e "s/^-//" -e "s/ sub.*$//" out >actual &&
1025 test_cmp expect actual
1028 test_expect_success 'recursive fetch with uninit submodule' '
1029 # depends on previous test for setup
1031 fetch_with_recursion_abort superclone sub &&
1032 git -C superclone submodule status >out &&
1033 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1034 test_cmp expect actual
1037 test_expect_success 'recursive fetch after deinit a submodule' '
1038 # depends on previous test for setup
1040 git -C superclone submodule update --init sub &&
1041 git -C superclone submodule deinit -f sub &&
1043 fetch_with_recursion_abort superclone sub &&
1044 git -C superclone submodule status >out &&
1045 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1046 test_cmp expect actual
1049 test_expect_success 'setup repo with upstreams that share a submodule name' '
1050 mkdir same-name-1 &&
1052 cd same-name-1 &&
1053 git init -b main &&
1054 test_commit --no-tag a
1055 ) &&
1056 git clone same-name-1 same-name-2 &&
1057 # same-name-1 and same-name-2 both add a submodule with the
1058 # name "submodule"
1060 cd same-name-1 &&
1061 mkdir submodule &&
1062 git -C submodule init -b main &&
1063 test_commit -C submodule --no-tag a1 &&
1064 git submodule add "$pwd/same-name-1/submodule" &&
1065 git add submodule &&
1066 git commit -m "super-a1"
1067 ) &&
1069 cd same-name-2 &&
1070 mkdir submodule &&
1071 git -C submodule init -b main &&
1072 test_commit -C submodule --no-tag a2 &&
1073 git submodule add "$pwd/same-name-2/submodule" &&
1074 git add submodule &&
1075 git commit -m "super-a2"
1076 ) &&
1077 git clone same-name-1 -o same-name-1 same-name-downstream &&
1079 cd same-name-downstream &&
1080 git remote add same-name-2 ../same-name-2 &&
1081 git fetch --all &&
1082 # init downstream with same-name-1
1083 git submodule update --init
1087 test_expect_success 'fetch --recurse-submodules updates name-conflicted, populated submodule' '
1088 test_when_finished "git -C same-name-downstream checkout main" &&
1090 cd same-name-1 &&
1091 test_commit -C submodule --no-tag b1 &&
1092 git add submodule &&
1093 git commit -m "super-b1"
1094 ) &&
1096 cd same-name-2 &&
1097 test_commit -C submodule --no-tag b2 &&
1098 git add submodule &&
1099 git commit -m "super-b2"
1100 ) &&
1102 cd same-name-downstream &&
1103 # even though the .gitmodules is correct, we cannot
1104 # fetch from same-name-2
1105 git checkout same-name-2/main &&
1106 git fetch --recurse-submodules same-name-1 &&
1107 test_must_fail git fetch --recurse-submodules same-name-2
1108 ) &&
1109 super_head1=$(git -C same-name-1 rev-parse HEAD) &&
1110 git -C same-name-downstream cat-file -e $super_head1 &&
1112 super_head2=$(git -C same-name-2 rev-parse HEAD) &&
1113 git -C same-name-downstream cat-file -e $super_head2 &&
1115 sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1116 git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
1118 sub_head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1119 test_must_fail git -C same-name-downstream/submodule cat-file -e $sub_head2
1122 test_expect_success 'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
1124 cd same-name-1 &&
1125 test_commit -C submodule --no-tag c1 &&
1126 git add submodule &&
1127 git commit -m "super-c1"
1128 ) &&
1130 cd same-name-2 &&
1131 test_commit -C submodule --no-tag c2 &&
1132 git add submodule &&
1133 git commit -m "super-c2"
1134 ) &&
1136 cd same-name-downstream &&
1137 git checkout main &&
1138 git rm .gitmodules &&
1139 git rm submodule &&
1140 git commit -m "no submodules" &&
1141 git fetch --recurse-submodules same-name-1
1142 ) &&
1143 head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1144 head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1146 cd same-name-downstream/.git/modules/submodule &&
1147 # The submodule has core.worktree pointing to the "git
1148 # rm"-ed directory, overwrite the invalid value. See
1149 # comment in get_fetch_task_from_changed() for more
1150 # information.
1151 git --work-tree=. cat-file -e $head1 &&
1152 test_must_fail git --work-tree=. cat-file -e $head2
1156 test_expect_success 'fetch --all with --recurse-submodules' '
1157 test_when_finished "rm -fr src_clone" &&
1158 git clone --recurse-submodules src src_clone &&
1160 cd src_clone &&
1161 git config submodule.recurse true &&
1162 git config fetch.parallel 0 &&
1163 git fetch --all 2>../fetch-log
1164 ) &&
1165 grep "^Fetching submodule sub$" fetch-log >fetch-subs &&
1166 test_line_count = 1 fetch-subs
1169 test_expect_success 'fetch --all with --recurse-submodules with multiple' '
1170 test_when_finished "rm -fr src_clone" &&
1171 git clone --recurse-submodules src src_clone &&
1173 cd src_clone &&
1174 git remote add secondary ../src &&
1175 git config submodule.recurse true &&
1176 git config fetch.parallel 0 &&
1177 git fetch --all 2>../fetch-log
1178 ) &&
1179 grep "Fetching submodule sub" fetch-log >fetch-subs &&
1180 test_line_count = 2 fetch-subs
1183 test_expect_success "fetch --all with --no-recurse-submodules only fetches superproject" '
1184 test_when_finished "rm -rf src_clone" &&
1186 git clone --recurse-submodules src src_clone &&
1188 cd src_clone &&
1189 git remote add secondary ../src &&
1190 git config submodule.recurse true &&
1191 git fetch --all --no-recurse-submodules 2>../fetch-log
1192 ) &&
1193 ! grep "Fetching submodule" fetch-log
1196 test_done