]> git.ipfire.org Git - thirdparty/git.git/blob - t/t5526-fetch-submodules.sh
Merge branch 'rs/bisect-start-leakfix' into maint-2.38
[thirdparty/git.git] / t / t5526-fetch-submodules.sh
1 #!/bin/sh
2 # Copyright (c) 2010, Jens Lehmann
3
4 test_description='Recursive "git fetch" for submodules'
5
6 GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
7 export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
8
9 . ./test-lib.sh
10
11 pwd=$(pwd)
12
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
21 }
22
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
31 }
32
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
41 }
42
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
49 }
50
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 () {
56 (
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 ) &&
65 (
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
73 )
74 }
75
76 # For each superproject in the test setup, update its submodule, add the
77 # submodule and create a new commit with the submodule change.
78 #
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 () {
82 (
83 cd submodule &&
84 (
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
98 }
99
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.
103 #
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
127 }
128
129 test_expect_success setup '
130 git config --global protocol.file.allow always &&
131 mkdir deepsubmodule &&
132 (
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 &&
141 (
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 &&
154 (
155 cd downstream &&
156 git submodule update --init --recursive
157 )
158 '
159
160 test_expect_success "fetch --recurse-submodules recurses into submodules" '
161 add_submodule_commits &&
162 (
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
168 '
169
170 test_expect_success "submodule.recurse option triggers recursive fetch" '
171 add_submodule_commits &&
172 (
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
178 '
179
180 test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" '
181 add_submodule_commits &&
182 (
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
189 '
190
191 test_expect_success "fetch alone only fetches superproject" '
192 add_submodule_commits &&
193 (
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
199 '
200
201 test_expect_success "fetch --no-recurse-submodules only fetches superproject" '
202 (
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
208 '
209
210 test_expect_success "using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
211 (
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
218 '
219
220 test_expect_success "--no-recurse-submodules overrides .gitmodules config" '
221 add_submodule_commits &&
222 (
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
228 '
229
230 test_expect_success "using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
231 (
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
238 '
239
240 test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
241 (
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
249 '
250
251 test_expect_success "--quiet propagates to submodules" '
252 (
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
258 '
259
260 test_expect_success "--quiet propagates to parallel submodules" '
261 (
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
267 '
268
269 test_expect_success "--dry-run propagates to submodules" '
270 add_submodule_commits &&
271 (
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
277 '
278
279 test_expect_success "Without --dry-run propagates to submodules" '
280 (
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
286 '
287
288 test_expect_success "recurseSubmodules=true propagates into submodules" '
289 add_submodule_commits &&
290 (
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
297 '
298
299 test_expect_success "--recurse-submodules overrides config in submodule" '
300 add_submodule_commits &&
301 (
302 cd downstream &&
303 (
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
311 '
312
313 test_expect_success "--no-recurse-submodules overrides config setting" '
314 add_submodule_commits &&
315 (
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
322 '
323
324 test_expect_success "Recursion doesn't happen when no new commits are fetched in the superproject" '
325 (
326 cd downstream &&
327 (
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
336 '
337
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 &&
344 (
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
350 '
351
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 &&
361 (
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
367 '
368
369 test_expect_success "Recursion picks up config in submodule" '
370 (
371 cd downstream &&
372 git fetch --recurse-submodules &&
373 (
374 cd submodule &&
375 git config fetch.recurseSubmodules true
376 )
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 &&
383 (
384 cd downstream &&
385 git fetch >../actual.out 2>../actual.err &&
386 (
387 cd submodule &&
388 git config --unset fetch.recurseSubmodules
389 )
390 ) &&
391 verify_fetch_result actual.err &&
392 test_must_be_empty actual.out
393 '
394
395 test_expect_success "Recursion picks up all submodules when necessary" '
396 add_submodule_commits &&
397 add_superproject_commits &&
398 (
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
404 '
405
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 &&
408 (
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
416 '
417
418 test_expect_success "'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
419 add_submodule_commits &&
420 add_superproject_commits &&
421 (
422 cd downstream &&
423 git config fetch.recurseSubmodules false &&
424 (
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 &&
430 (
431 cd submodule &&
432 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
433 )
434 ) &&
435 test_must_be_empty actual.out &&
436 verify_fetch_result actual.err
437 '
438
439 # These tests verify that we can fetch submodules that aren't in the
440 # index.
441 #
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' '
445 (
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
452 )
453 '
454
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
459 (
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) &&
467
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 &&
471
472 test_must_be_empty actual.out &&
473 verify_fetch_result actual.err
474 '
475
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
480 (
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) &&
488
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 &&
492
493 test_must_be_empty actual.out &&
494 verify_fetch_result actual.err
495 '
496
497 test_expect_success "'--recurse-submodules' should ignore changed, inactive submodules" '
498 add_submodule_commits &&
499 add_superproject_commits &&
500
501 # Fetch the new superproject commit
502 (
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
514 '
515
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 &&
521 (
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 &&
533 (
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
541 )
542 '
543
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 &&
549
550 # Create new commit in origin/super-sub2-only
551 git checkout super-sub2-only &&
552 (
553 cd submodule2 &&
554 test_commit --no-tag foo
555 ) &&
556 git add submodule2 &&
557 git commit -m "new submodule2" &&
558
559 git checkout super &&
560 (
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 &&
569
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
576 EOF
577 verify_fetch_result actual.err
578 '
579
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 &&
589 (
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
595 '
596
597 test_expect_success "'fetch.recurseSubmodules=on-demand' overrides global config" '
598 (
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 &&
609 (
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 &&
615 (
616 cd downstream &&
617 git config --unset fetch.recurseSubmodules
618 ) &&
619 test_must_be_empty actual.out &&
620 verify_fetch_result actual.err
621 '
622
623 test_expect_success "'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
624 (
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 &&
635 (
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 &&
641 (
642 cd downstream &&
643 git config --unset submodule.submodule.fetchRecurseSubmodules
644 ) &&
645 test_must_be_empty actual.out &&
646 verify_fetch_result actual.err
647 '
648
649 test_expect_success "don't fetch submodule when newly recorded commits are already present" '
650 (
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 &&
661 (
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 &&
667 (
668 cd submodule &&
669 git checkout -q sub
670 )
671 '
672
673 test_expect_success "'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
674 (
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 &&
685 (
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"
704 '
705
706 test_expect_success 'fetching submodules respects parallel settings' '
707 git config fetch.recurseSubmodules true &&
708 (
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 )
720 '
721
722 test_expect_success 'fetching submodule into a broken repository' '
723 # Prepare src and src/sub nested in it
724 git init src &&
725 (
726 cd src &&
727 git init sub &&
728 git -C sub commit --allow-empty -m "initial in sub" &&
729 git submodule add -- ./sub sub &&
730 git commit -m "initial in top"
731 ) &&
732
733 # Clone the old-fashoned way
734 git clone src dst &&
735 git -C dst clone ../src/sub sub &&
736
737 # Make sure that old-fashoned layout is still supported
738 git -C dst status &&
739
740 # "diff" would find no change
741 git -C dst diff --exit-code &&
742
743 # Recursive-fetch works fine
744 git -C dst fetch --recurse-submodules &&
745
746 # Break the receiving submodule
747 rm -f dst/sub/.git/HEAD &&
748
749 # NOTE: without the fix the following tests will recurse forever!
750 # They should terminate with an error.
751
752 test_must_fail git -C dst status &&
753 test_must_fail git -C dst diff &&
754 test_must_fail git -C dst fetch --recurse-submodules
755 '
756
757 test_expect_success "fetch new commits when submodule got renamed" '
758 git clone . downstream_rename &&
759 (
760 cd downstream_rename &&
761 git submodule update --init --recursive &&
762 git checkout -b rename &&
763 git mv submodule submodule_renamed &&
764 (
765 cd submodule_renamed &&
766 git checkout -b rename_sub &&
767 echo a >a &&
768 git add a &&
769 git commit -ma &&
770 git push origin rename_sub &&
771 git rev-parse HEAD >../../expect
772 ) &&
773 git add submodule_renamed &&
774 git commit -m "update renamed submodule" &&
775 git push origin rename
776 ) &&
777 (
778 cd downstream &&
779 git fetch --recurse-submodules=on-demand &&
780 (
781 cd submodule &&
782 git rev-parse origin/rename_sub >../../actual
783 )
784 ) &&
785 test_cmp expect actual
786 '
787
788 test_expect_success "fetch new submodule commits on-demand outside standard refspec" '
789 # add a second submodule and ensure it is around in downstream first
790 git clone submodule sub1 &&
791 git submodule add ./sub1 &&
792 git commit -m "adding a second submodule" &&
793 git -C downstream pull &&
794 git -C downstream submodule update --init --recursive &&
795
796 git checkout --detach &&
797
798 C=$(git -C submodule commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
799 git -C submodule update-ref refs/changes/1 $C &&
800 git update-index --cacheinfo 160000 $C submodule &&
801 test_tick &&
802
803 D=$(git -C sub1 commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
804 git -C sub1 update-ref refs/changes/2 $D &&
805 git update-index --cacheinfo 160000 $D sub1 &&
806
807 git commit -m "updated submodules outside of refs/heads" &&
808 E=$(git rev-parse HEAD) &&
809 git update-ref refs/changes/3 $E &&
810 (
811 cd downstream &&
812 git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
813 git -C submodule cat-file -t $C &&
814 git -C sub1 cat-file -t $D &&
815 git checkout --recurse-submodules FETCH_HEAD
816 )
817 '
818
819 test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD' '
820 # depends on the previous test for setup
821
822 C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
823 git -C submodule update-ref refs/changes/4 $C &&
824 git update-index --cacheinfo 160000 $C submodule &&
825 test_tick &&
826
827 D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
828 git -C sub1 update-ref refs/changes/5 $D &&
829 git update-index --cacheinfo 160000 $D sub1 &&
830
831 git commit -m "updated submodules outside of refs/heads" &&
832 E=$(git rev-parse HEAD) &&
833 git update-ref refs/changes/6 $E &&
834 (
835 cd downstream &&
836 git fetch --recurse-submodules origin refs/changes/6 &&
837 git -C submodule cat-file -t $C &&
838 git -C sub1 cat-file -t $D &&
839 git checkout --recurse-submodules FETCH_HEAD
840 )
841 '
842
843 test_expect_success 'fetch new submodule commits on-demand without .gitmodules entry' '
844 # depends on the previous test for setup
845
846 git config -f .gitmodules --remove-section submodule.sub1 &&
847 git add .gitmodules &&
848 git commit -m "delete gitmodules file" &&
849 git checkout -B super &&
850 git -C downstream fetch &&
851 git -C downstream checkout origin/super &&
852
853 C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
854 git -C submodule update-ref refs/changes/7 $C &&
855 git update-index --cacheinfo 160000 $C submodule &&
856 test_tick &&
857
858 D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
859 git -C sub1 update-ref refs/changes/8 $D &&
860 git update-index --cacheinfo 160000 $D sub1 &&
861
862 git commit -m "updated submodules outside of refs/heads" &&
863 E=$(git rev-parse HEAD) &&
864 git update-ref refs/changes/9 $E &&
865 (
866 cd downstream &&
867 git fetch --recurse-submodules origin refs/changes/9 &&
868 git -C submodule cat-file -t $C &&
869 git -C sub1 cat-file -t $D &&
870 git checkout --recurse-submodules FETCH_HEAD
871 )
872 '
873
874 test_expect_success 'fetch new submodule commit intermittently referenced by superproject' '
875 # depends on the previous test for setup
876
877 D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
878 E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
879 F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
880
881 git -C sub1 update-ref refs/changes/10 $D &&
882 git update-index --cacheinfo 160000 $D sub1 &&
883 git commit -m "updated submodules outside of refs/heads" &&
884
885 git -C sub1 update-ref refs/changes/11 $E &&
886 git update-index --cacheinfo 160000 $E sub1 &&
887 git commit -m "updated submodules outside of refs/heads" &&
888
889 git -C sub1 update-ref refs/changes/12 $F &&
890 git update-index --cacheinfo 160000 $F sub1 &&
891 git commit -m "updated submodules outside of refs/heads" &&
892
893 G=$(git rev-parse HEAD) &&
894 git update-ref refs/changes/13 $G &&
895 (
896 cd downstream &&
897 git fetch --recurse-submodules origin refs/changes/13 &&
898
899 git -C sub1 cat-file -t $D &&
900 git -C sub1 cat-file -t $E &&
901 git -C sub1 cat-file -t $F
902 )
903 '
904
905 add_commit_push () {
906 dir="$1" &&
907 msg="$2" &&
908 shift 2 &&
909 git -C "$dir" add "$@" &&
910 git -C "$dir" commit -a -m "$msg" &&
911 git -C "$dir" push
912 }
913
914 compare_refs_in_dir () {
915 fail= &&
916 if test "x$1" = 'x!'
917 then
918 fail='!' &&
919 shift
920 fi &&
921 git -C "$1" rev-parse --verify "$2" >expect &&
922 git -C "$3" rev-parse --verify "$4" >actual &&
923 eval $fail test_cmp expect actual
924 }
925
926
927 test_expect_success 'setup nested submodule fetch test' '
928 # does not depend on any previous test setups
929
930 for repo in outer middle inner
931 do
932 git init --bare $repo &&
933 git clone $repo ${repo}_content &&
934 echo "$repo" >"${repo}_content/file" &&
935 add_commit_push ${repo}_content "initial" file ||
936 return 1
937 done &&
938
939 git clone outer A &&
940 git -C A submodule add "$pwd/middle" &&
941 git -C A/middle/ submodule add "$pwd/inner" &&
942 add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
943 add_commit_push A/ "adding middle sub" .gitmodules middle &&
944
945 git clone outer B &&
946 git -C B/ submodule update --init middle &&
947
948 compare_refs_in_dir A HEAD B HEAD &&
949 compare_refs_in_dir A/middle HEAD B/middle HEAD &&
950 test_path_is_file B/file &&
951 test_path_is_file B/middle/file &&
952 test_path_is_missing B/middle/inner/file &&
953
954 echo "change on inner repo of A" >"A/middle/inner/file" &&
955 add_commit_push A/middle/inner "change on inner" file &&
956 add_commit_push A/middle "change on inner" inner &&
957 add_commit_push A "change on inner" middle
958 '
959
960 test_expect_success 'fetching a superproject containing an uninitialized sub/sub project' '
961 # depends on previous test for setup
962
963 git -C B/ fetch &&
964 compare_refs_in_dir A origin/HEAD B origin/HEAD
965 '
966
967 fetch_with_recursion_abort () {
968 # In a regression the following git call will run into infinite recursion.
969 # To handle that, we connect the sed command to the git call by a pipe
970 # so that sed can kill the infinite recursion when detected.
971 # The recursion creates git output like:
972 # Fetching submodule sub
973 # Fetching submodule sub/sub <-- [1]
974 # Fetching submodule sub/sub/sub
975 # ...
976 # [1] sed will stop reading and cause git to eventually stop and die
977
978 git -C "$1" fetch --recurse-submodules 2>&1 |
979 sed "/Fetching submodule $2[^$]/q" >out &&
980 ! grep "Fetching submodule $2[^$]" out
981 }
982
983 test_expect_success 'setup recursive fetch with uninit submodule' '
984 # does not depend on any previous test setups
985
986 test_create_repo super &&
987 test_commit -C super initial &&
988 test_create_repo sub &&
989 test_commit -C sub initial &&
990 git -C sub rev-parse HEAD >expect &&
991
992 git -C super submodule add ../sub &&
993 git -C super commit -m "add sub" &&
994
995 git clone super superclone &&
996 git -C superclone submodule status >out &&
997 sed -e "s/^-//" -e "s/ sub.*$//" out >actual &&
998 test_cmp expect actual
999 '
1000
1001 test_expect_success 'recursive fetch with uninit submodule' '
1002 # depends on previous test for setup
1003
1004 fetch_with_recursion_abort superclone sub &&
1005 git -C superclone submodule status >out &&
1006 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1007 test_cmp expect actual
1008 '
1009
1010 test_expect_success 'recursive fetch after deinit a submodule' '
1011 # depends on previous test for setup
1012
1013 git -C superclone submodule update --init sub &&
1014 git -C superclone submodule deinit -f sub &&
1015
1016 fetch_with_recursion_abort superclone sub &&
1017 git -C superclone submodule status >out &&
1018 sed -e "s/^-//" -e "s/ sub$//" out >actual &&
1019 test_cmp expect actual
1020 '
1021
1022 test_expect_success 'setup repo with upstreams that share a submodule name' '
1023 mkdir same-name-1 &&
1024 (
1025 cd same-name-1 &&
1026 git init -b main &&
1027 test_commit --no-tag a
1028 ) &&
1029 git clone same-name-1 same-name-2 &&
1030 # same-name-1 and same-name-2 both add a submodule with the
1031 # name "submodule"
1032 (
1033 cd same-name-1 &&
1034 mkdir submodule &&
1035 git -C submodule init -b main &&
1036 test_commit -C submodule --no-tag a1 &&
1037 git submodule add "$pwd/same-name-1/submodule" &&
1038 git add submodule &&
1039 git commit -m "super-a1"
1040 ) &&
1041 (
1042 cd same-name-2 &&
1043 mkdir submodule &&
1044 git -C submodule init -b main &&
1045 test_commit -C submodule --no-tag a2 &&
1046 git submodule add "$pwd/same-name-2/submodule" &&
1047 git add submodule &&
1048 git commit -m "super-a2"
1049 ) &&
1050 git clone same-name-1 -o same-name-1 same-name-downstream &&
1051 (
1052 cd same-name-downstream &&
1053 git remote add same-name-2 ../same-name-2 &&
1054 git fetch --all &&
1055 # init downstream with same-name-1
1056 git submodule update --init
1057 )
1058 '
1059
1060 test_expect_success 'fetch --recurse-submodules updates name-conflicted, populated submodule' '
1061 test_when_finished "git -C same-name-downstream checkout main" &&
1062 (
1063 cd same-name-1 &&
1064 test_commit -C submodule --no-tag b1 &&
1065 git add submodule &&
1066 git commit -m "super-b1"
1067 ) &&
1068 (
1069 cd same-name-2 &&
1070 test_commit -C submodule --no-tag b2 &&
1071 git add submodule &&
1072 git commit -m "super-b2"
1073 ) &&
1074 (
1075 cd same-name-downstream &&
1076 # even though the .gitmodules is correct, we cannot
1077 # fetch from same-name-2
1078 git checkout same-name-2/main &&
1079 git fetch --recurse-submodules same-name-1 &&
1080 test_must_fail git fetch --recurse-submodules same-name-2
1081 ) &&
1082 super_head1=$(git -C same-name-1 rev-parse HEAD) &&
1083 git -C same-name-downstream cat-file -e $super_head1 &&
1084
1085 super_head2=$(git -C same-name-2 rev-parse HEAD) &&
1086 git -C same-name-downstream cat-file -e $super_head2 &&
1087
1088 sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1089 git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
1090
1091 sub_head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1092 test_must_fail git -C same-name-downstream/submodule cat-file -e $sub_head2
1093 '
1094
1095 test_expect_success 'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
1096 (
1097 cd same-name-1 &&
1098 test_commit -C submodule --no-tag c1 &&
1099 git add submodule &&
1100 git commit -m "super-c1"
1101 ) &&
1102 (
1103 cd same-name-2 &&
1104 test_commit -C submodule --no-tag c2 &&
1105 git add submodule &&
1106 git commit -m "super-c2"
1107 ) &&
1108 (
1109 cd same-name-downstream &&
1110 git checkout main &&
1111 git rm .gitmodules &&
1112 git rm submodule &&
1113 git commit -m "no submodules" &&
1114 git fetch --recurse-submodules same-name-1
1115 ) &&
1116 head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1117 head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
1118 (
1119 cd same-name-downstream/.git/modules/submodule &&
1120 # The submodule has core.worktree pointing to the "git
1121 # rm"-ed directory, overwrite the invalid value. See
1122 # comment in get_fetch_task_from_changed() for more
1123 # information.
1124 git --work-tree=. cat-file -e $head1 &&
1125 test_must_fail git --work-tree=. cat-file -e $head2
1126 )
1127 '
1128
1129 test_expect_success 'fetch --all with --recurse-submodules' '
1130 test_when_finished "rm -fr src_clone" &&
1131 git clone --recurse-submodules src src_clone &&
1132 (
1133 cd src_clone &&
1134 git config submodule.recurse true &&
1135 git config fetch.parallel 0 &&
1136 git fetch --all 2>../fetch-log
1137 ) &&
1138 grep "^Fetching submodule sub$" fetch-log >fetch-subs &&
1139 test_line_count = 1 fetch-subs
1140 '
1141
1142 test_expect_success 'fetch --all with --recurse-submodules with multiple' '
1143 test_when_finished "rm -fr src_clone" &&
1144 git clone --recurse-submodules src src_clone &&
1145 (
1146 cd src_clone &&
1147 git remote add secondary ../src &&
1148 git config submodule.recurse true &&
1149 git config fetch.parallel 0 &&
1150 git fetch --all 2>../fetch-log
1151 ) &&
1152 grep "Fetching submodule sub" fetch-log >fetch-subs &&
1153 test_line_count = 2 fetch-subs
1154 '
1155
1156 test_done