]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5526-fetch-submodules.sh
t2104: style fixes
[thirdparty/git.git] / t / t5526-fetch-submodules.sh
CommitLineData
7dce19d3
JL
1#!/bin/sh
2# Copyright (c) 2010, Jens Lehmann
3
4test_description='Recursive "git fetch" for submodules'
5
13a2f620
JT
6GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
7export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
8
7dce19d3
JL
9. ./test-lib.sh
10
11pwd=$(pwd)
12
6e94bd64
GC
13write_expected_sub () {
14 NEW_HEAD=$1 &&
b90d9f76 15 SUPER_HEAD=$2 &&
6e94bd64 16 cat >"$pwd/expect.err.sub" <<-EOF
b90d9f76 17 Fetching submodule submodule${SUPER_HEAD:+ at commit $SUPER_HEAD}
6e94bd64
GC
18 From $pwd/submodule
19 OLD_HEAD..$NEW_HEAD sub -> origin/sub
20 EOF
21}
22
b90d9f76
GC
23write_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
6e94bd64
GC
33write_expected_deep () {
34 NEW_HEAD=$1 &&
b90d9f76 35 SUB_HEAD=$2 &&
6e94bd64 36 cat >"$pwd/expect.err.deep" <<-EOF
b90d9f76 37 Fetching submodule submodule/subdir/deepsubmodule${SUB_HEAD:+ at commit $SUB_HEAD}
6e94bd64
GC
38 From $pwd/deepsubmodule
39 OLD_HEAD..$NEW_HEAD deep -> origin/deep
40 EOF
41}
42
43write_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
f3117dfd
GC
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().
d1d1572e 55add_submodule_commits () {
7dce19d3
JL
56 (
57 cd submodule &&
7dce19d3
JL
58 echo new >> subfile &&
59 test_tick &&
60 git add subfile &&
61 git commit -m new subfile &&
6e94bd64
GC
62 new_head=$(git rev-parse --short HEAD) &&
63 write_expected_sub $new_head
7dce19d3
JL
64 ) &&
65 (
66 cd deepsubmodule &&
7dce19d3
JL
67 echo new >> deepsubfile &&
68 test_tick &&
69 git add deepsubfile &&
70 git commit -m new deepsubfile &&
6e94bd64
GC
71 new_head=$(git rev-parse --short HEAD) &&
72 write_expected_deep $new_head
7dce19d3
JL
73 )
74}
75
d1d1572e
GC
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.
81add_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
f3117dfd
GC
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.
106verify_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 &&
b90d9f76
GC
121 if test -f expect.err.sub2
122 then
123 cat expect.err.sub2 >>expect.err.combined
124 fi &&
6e94bd64
GC
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
f3117dfd
GC
127}
128
7dce19d3 129test_expect_success setup '
225d2d50 130 git config --global protocol.file.allow always &&
7dce19d3
JL
131 mkdir deepsubmodule &&
132 (
133 cd deepsubmodule &&
134 git init &&
135 echo deepsubcontent > deepsubfile &&
136 git add deepsubfile &&
b618a2d9
JS
137 git commit -m new deepsubfile &&
138 git branch -M deep
7dce19d3
JL
139 ) &&
140 mkdir submodule &&
141 (
142 cd submodule &&
143 git init &&
144 echo subcontent > subfile &&
145 git add subfile &&
ea2d325b 146 git submodule add "$pwd/deepsubmodule" subdir/deepsubmodule &&
b618a2d9
JS
147 git commit -a -m new &&
148 git branch -M sub
7dce19d3
JL
149 ) &&
150 git submodule add "$pwd/submodule" submodule &&
151 git commit -am initial &&
b618a2d9 152 git branch -M super &&
7dce19d3
JL
153 git clone . downstream &&
154 (
155 cd downstream &&
156 git submodule update --init --recursive
fbf71645 157 )
7dce19d3
JL
158'
159
71f4a935 160test_expect_success "fetch --recurse-submodules recurses into submodules" '
d1d1572e 161 add_submodule_commits &&
7dce19d3
JL
162 (
163 cd downstream &&
164 git fetch --recurse-submodules >../actual.out 2>../actual.err
cabdee2c 165 ) &&
fbf71645 166 test_must_be_empty actual.out &&
f3117dfd 167 verify_fetch_result actual.err
7dce19d3
JL
168'
169
15184ae9
EW
170test_expect_success "fetch --recurse-submodules honors --no-write-fetch-head" '
171 (
172 cd downstream &&
173 git submodule foreach --recursive \
174 sh -c "cd \"\$(git rev-parse --git-dir)\" && rm -f FETCH_HEAD" &&
175
176 git fetch --recurse-submodules --no-write-fetch-head &&
177
178 git submodule foreach --recursive \
179 sh -c "cd \"\$(git rev-parse --git-dir)\" && ! test -f FETCH_HEAD"
180 )
181'
182
71f4a935 183test_expect_success "submodule.recurse option triggers recursive fetch" '
d1d1572e 184 add_submodule_commits &&
58f4203e
SB
185 (
186 cd downstream &&
187 git -c submodule.recurse fetch >../actual.out 2>../actual.err
188 ) &&
189 test_must_be_empty actual.out &&
f3117dfd 190 verify_fetch_result actual.err
58f4203e
SB
191'
192
71f4a935 193test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" '
ac48da5a 194 test_when_finished "rm -f trace.out" &&
d1d1572e 195 add_submodule_commits &&
62104ba1
SB
196 (
197 cd downstream &&
9b2ac68f 198 GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
62104ba1
SB
199 ) &&
200 test_must_be_empty actual.out &&
f3117dfd 201 verify_fetch_result actual.err &&
62104ba1
SB
202 grep "2 tasks" trace.out
203'
204
7dce19d3 205test_expect_success "fetch alone only fetches superproject" '
d1d1572e 206 add_submodule_commits &&
7dce19d3
JL
207 (
208 cd downstream &&
209 git fetch >../actual.out 2>../actual.err
210 ) &&
ec10b018
SG
211 test_must_be_empty actual.out &&
212 test_must_be_empty actual.err
7dce19d3
JL
213'
214
c1a3c364
JL
215test_expect_success "fetch --no-recurse-submodules only fetches superproject" '
216 (
217 cd downstream &&
218 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
219 ) &&
ec10b018
SG
220 test_must_be_empty actual.out &&
221 test_must_be_empty actual.err
c1a3c364
JL
222'
223
71f4a935 224test_expect_success "using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
c1a3c364
JL
225 (
226 cd downstream &&
227 git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
228 git fetch >../actual.out 2>../actual.err
cabdee2c 229 ) &&
fbf71645 230 test_must_be_empty actual.out &&
f3117dfd 231 verify_fetch_result actual.err
c1a3c364
JL
232'
233
234test_expect_success "--no-recurse-submodules overrides .gitmodules config" '
d1d1572e 235 add_submodule_commits &&
c1a3c364
JL
236 (
237 cd downstream &&
238 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
239 ) &&
ec10b018
SG
240 test_must_be_empty actual.out &&
241 test_must_be_empty actual.err
c1a3c364
JL
242'
243
244test_expect_success "using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
245 (
246 cd downstream &&
247 git config submodule.submodule.fetchRecurseSubmodules false &&
248 git fetch >../actual.out 2>../actual.err
249 ) &&
ec10b018
SG
250 test_must_be_empty actual.out &&
251 test_must_be_empty actual.err
c1a3c364
JL
252'
253
71f4a935 254test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
c1a3c364
JL
255 (
256 cd downstream &&
257 git fetch --recurse-submodules >../actual.out 2>../actual.err &&
a2a56468 258 git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
c1a3c364 259 git config --unset submodule.submodule.fetchRecurseSubmodules
cabdee2c 260 ) &&
fbf71645 261 test_must_be_empty actual.out &&
f3117dfd 262 verify_fetch_result actual.err
c1a3c364
JL
263'
264
7dce19d3
JL
265test_expect_success "--quiet propagates to submodules" '
266 (
267 cd downstream &&
268 git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
269 ) &&
ec10b018
SG
270 test_must_be_empty actual.out &&
271 test_must_be_empty actual.err
7dce19d3
JL
272'
273
62104ba1
SB
274test_expect_success "--quiet propagates to parallel submodules" '
275 (
276 cd downstream &&
277 git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
278 ) &&
ec10b018
SG
279 test_must_be_empty actual.out &&
280 test_must_be_empty actual.err
62104ba1
SB
281'
282
71f4a935 283test_expect_success "--dry-run propagates to submodules" '
d1d1572e 284 add_submodule_commits &&
7dce19d3
JL
285 (
286 cd downstream &&
287 git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
cabdee2c 288 ) &&
fbf71645 289 test_must_be_empty actual.out &&
f3117dfd 290 verify_fetch_result actual.err
502681cd
ÆAB
291'
292
71f4a935 293test_expect_success "Without --dry-run propagates to submodules" '
7dce19d3
JL
294 (
295 cd downstream &&
296 git fetch --recurse-submodules >../actual.out 2>../actual.err
cabdee2c 297 ) &&
fbf71645 298 test_must_be_empty actual.out &&
f3117dfd 299 verify_fetch_result actual.err
7dce19d3
JL
300'
301
71f4a935 302test_expect_success "recurseSubmodules=true propagates into submodules" '
d1d1572e 303 add_submodule_commits &&
be254a0e
JL
304 (
305 cd downstream &&
2aac933c 306 git config fetch.recurseSubmodules true &&
be254a0e 307 git fetch >../actual.out 2>../actual.err
cabdee2c 308 ) &&
fbf71645 309 test_must_be_empty actual.out &&
f3117dfd 310 verify_fetch_result actual.err
be254a0e
JL
311'
312
71f4a935 313test_expect_success "--recurse-submodules overrides config in submodule" '
d1d1572e 314 add_submodule_commits &&
be254a0e
JL
315 (
316 cd downstream &&
317 (
318 cd submodule &&
319 git config fetch.recurseSubmodules false
320 ) &&
321 git fetch --recurse-submodules >../actual.out 2>../actual.err
cabdee2c 322 ) &&
fbf71645 323 test_must_be_empty actual.out &&
f3117dfd 324 verify_fetch_result actual.err
be254a0e
JL
325'
326
327test_expect_success "--no-recurse-submodules overrides config setting" '
d1d1572e 328 add_submodule_commits &&
be254a0e
JL
329 (
330 cd downstream &&
2aac933c 331 git config fetch.recurseSubmodules true &&
be254a0e
JL
332 git fetch --no-recurse-submodules >../actual.out 2>../actual.err
333 ) &&
ec10b018
SG
334 test_must_be_empty actual.out &&
335 test_must_be_empty actual.err
be254a0e
JL
336'
337
71f4a935 338test_expect_success "Recursion doesn't happen when no new commits are fetched in the superproject" '
88a21979
JL
339 (
340 cd downstream &&
341 (
342 cd submodule &&
343 git config --unset fetch.recurseSubmodules
344 ) &&
2aac933c 345 git config --unset fetch.recurseSubmodules &&
88a21979
JL
346 git fetch >../actual.out 2>../actual.err
347 ) &&
ec10b018
SG
348 test_must_be_empty actual.out &&
349 test_must_be_empty actual.err
88a21979
JL
350'
351
71f4a935 352test_expect_success "Recursion stops when no new submodule commits are fetched" '
88a21979
JL
353 git add submodule &&
354 git commit -m "new submodule" &&
6e94bd64
GC
355 new_head=$(git rev-parse --short HEAD) &&
356 write_expected_super $new_head &&
f3117dfd 357 rm expect.err.deep &&
88a21979
JL
358 (
359 cd downstream &&
360 git fetch >../actual.out 2>../actual.err
361 ) &&
f3117dfd 362 verify_fetch_result actual.err &&
fbf71645 363 test_must_be_empty actual.out
88a21979
JL
364'
365
71f4a935 366test_expect_success "Recursion doesn't happen when new superproject commits don't change any submodules" '
d1d1572e 367 add_submodule_commits &&
88a21979
JL
368 echo a > file &&
369 git add file &&
370 git commit -m "new file" &&
6e94bd64
GC
371 new_head=$(git rev-parse --short HEAD) &&
372 write_expected_super $new_head &&
f3117dfd
GC
373 rm expect.err.sub &&
374 rm expect.err.deep &&
88a21979
JL
375 (
376 cd downstream &&
377 git fetch >../actual.out 2>../actual.err
378 ) &&
ec10b018 379 test_must_be_empty actual.out &&
f3117dfd 380 verify_fetch_result actual.err
88a21979
JL
381'
382
71f4a935 383test_expect_success "Recursion picks up config in submodule" '
88a21979
JL
384 (
385 cd downstream &&
386 git fetch --recurse-submodules &&
387 (
388 cd submodule &&
389 git config fetch.recurseSubmodules true
390 )
391 ) &&
d1d1572e 392 add_submodule_commits &&
88a21979
JL
393 git add submodule &&
394 git commit -m "new submodule" &&
6e94bd64
GC
395 new_head=$(git rev-parse --short HEAD) &&
396 write_expected_super $new_head &&
88a21979
JL
397 (
398 cd downstream &&
399 git fetch >../actual.out 2>../actual.err &&
400 (
401 cd submodule &&
402 git config --unset fetch.recurseSubmodules
403 )
404 ) &&
f3117dfd 405 verify_fetch_result actual.err &&
fbf71645 406 test_must_be_empty actual.out
88a21979
JL
407'
408
71f4a935 409test_expect_success "Recursion picks up all submodules when necessary" '
d1d1572e
GC
410 add_submodule_commits &&
411 add_superproject_commits &&
88a21979
JL
412 (
413 cd downstream &&
414 git fetch >../actual.out 2>../actual.err
415 ) &&
f3117dfd 416 verify_fetch_result actual.err &&
fbf71645 417 test_must_be_empty actual.out
88a21979
JL
418'
419
71f4a935 420test_expect_success "'--recurse-submodules=on-demand' doesn't recurse when no new commits are fetched in the superproject (and ignores config)" '
d1d1572e 421 add_submodule_commits &&
8f0700dd
JL
422 (
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 ) &&
ec10b018
SG
428 test_must_be_empty actual.out &&
429 test_must_be_empty actual.err
8f0700dd
JL
430'
431
71f4a935 432test_expect_success "'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
d1d1572e
GC
433 add_submodule_commits &&
434 add_superproject_commits &&
8f0700dd
JL
435 (
436 cd downstream &&
437 git config fetch.recurseSubmodules false &&
438 (
439 cd submodule &&
ea2d325b 440 git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
8f0700dd
JL
441 ) &&
442 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
51b85471 443 git config --unset fetch.recurseSubmodules &&
8f0700dd
JL
444 (
445 cd submodule &&
ea2d325b 446 git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
8f0700dd
JL
447 )
448 ) &&
fbf71645 449 test_must_be_empty actual.out &&
f3117dfd 450 verify_fetch_result actual.err
8f0700dd
JL
451'
452
b90d9f76
GC
453# These tests verify that we can fetch submodules that aren't in the
454# index.
455#
456# First, test the simple case where the index is empty and we only fetch
457# submodules that are not in the index.
458test_expect_success 'setup downstream branch without submodules' '
459 (
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
466 )
467'
468
469test_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
473 (
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) &&
481
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 &&
485
486 test_must_be_empty actual.out &&
487 verify_fetch_result actual.err
488'
489
490test_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
494 (
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) &&
502
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 &&
506
507 test_must_be_empty actual.out &&
508 verify_fetch_result actual.err
509'
510
511test_expect_success "'--recurse-submodules' should ignore changed, inactive submodules" '
512 add_submodule_commits &&
513 add_superproject_commits &&
514
515 # Fetch the new superproject commit
516 (
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
528'
529
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.
533test_expect_success 'setup downstream branch with other submodule' '
534 mkdir submodule2 &&
535 (
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 &&
547 (
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
555 )
556'
557
558test_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 &&
563
564 # Create new commit in origin/super-sub2-only
565 git checkout super-sub2-only &&
566 (
567 cd submodule2 &&
568 test_commit --no-tag foo
569 ) &&
570 git add submodule2 &&
571 git commit -m "new submodule2" &&
572
573 git checkout super &&
574 (
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 &&
583
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
590 EOF
591 verify_fetch_result actual.err
592'
593
71f4a935 594test_expect_success "'--recurse-submodules=on-demand' stops when no new submodule commits are found in the superproject (and ignores config)" '
d1d1572e 595 add_submodule_commits &&
8f0700dd
JL
596 echo a >> file &&
597 git add file &&
598 git commit -m "new file" &&
6e94bd64
GC
599 new_head=$(git rev-parse --short HEAD) &&
600 write_expected_super $new_head &&
f3117dfd
GC
601 rm expect.err.sub &&
602 rm expect.err.deep &&
8f0700dd
JL
603 (
604 cd downstream &&
605 git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
606 ) &&
ec10b018 607 test_must_be_empty actual.out &&
f3117dfd 608 verify_fetch_result actual.err
8f0700dd
JL
609'
610
71f4a935 611test_expect_success "'fetch.recurseSubmodules=on-demand' overrides global config" '
1fb25502
JL
612 (
613 cd downstream &&
614 git fetch --recurse-submodules
615 ) &&
d1d1572e 616 add_submodule_commits &&
1fb25502 617 git config --global fetch.recurseSubmodules false &&
1fb25502
JL
618 git add submodule &&
619 git commit -m "new submodule" &&
6e94bd64
GC
620 new_head=$(git rev-parse --short HEAD) &&
621 write_expected_super $new_head &&
f3117dfd 622 rm expect.err.deep &&
1fb25502
JL
623 (
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 &&
629 (
630 cd downstream &&
631 git config --unset fetch.recurseSubmodules
632 ) &&
fbf71645 633 test_must_be_empty actual.out &&
f3117dfd 634 verify_fetch_result actual.err
1fb25502
JL
635'
636
71f4a935 637test_expect_success "'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
bf42b384
JL
638 (
639 cd downstream &&
640 git fetch --recurse-submodules
641 ) &&
d1d1572e 642 add_submodule_commits &&
bf42b384 643 git config fetch.recurseSubmodules false &&
bf42b384
JL
644 git add submodule &&
645 git commit -m "new submodule" &&
6e94bd64
GC
646 new_head=$(git rev-parse --short HEAD) &&
647 write_expected_super $new_head &&
f3117dfd 648 rm expect.err.deep &&
bf42b384
JL
649 (
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 &&
655 (
656 cd downstream &&
657 git config --unset submodule.submodule.fetchRecurseSubmodules
658 ) &&
fbf71645 659 test_must_be_empty actual.out &&
f3117dfd 660 verify_fetch_result actual.err
bf42b384
JL
661'
662
71f4a935 663test_expect_success "don't fetch submodule when newly recorded commits are already present" '
c16c3e40
JL
664 (
665 cd submodule &&
666 git checkout -q HEAD^^
667 ) &&
c16c3e40
JL
668 git add submodule &&
669 git commit -m "submodule rewound" &&
6e94bd64
GC
670 new_head=$(git rev-parse --short HEAD) &&
671 write_expected_super $new_head &&
f3117dfd
GC
672 rm expect.err.sub &&
673 # This file does not exist, but rm -f for readability
674 rm -f expect.err.deep &&
c16c3e40
JL
675 (
676 cd downstream &&
677 git fetch >../actual.out 2>../actual.err
678 ) &&
ec10b018 679 test_must_be_empty actual.out &&
f3117dfd 680 verify_fetch_result actual.err &&
01ce1225
HV
681 (
682 cd submodule &&
b618a2d9 683 git checkout -q sub
01ce1225
HV
684 )
685'
686
71f4a935 687test_expect_success "'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
01ce1225
HV
688 (
689 cd downstream &&
690 git fetch --recurse-submodules
691 ) &&
d1d1572e 692 add_submodule_commits &&
01ce1225
HV
693 git add submodule &&
694 git rm .gitmodules &&
695 git commit -m "new submodule without .gitmodules" &&
6e94bd64
GC
696 new_head=$(git rev-parse --short HEAD) &&
697 write_expected_super $new_head &&
f3117dfd 698 rm expect.err.deep &&
01ce1225
HV
699 (
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 ) &&
1c5e94f4 713 test_must_be_empty actual.out &&
f3117dfd 714 verify_fetch_result actual.err &&
01ce1225
HV
715 git checkout HEAD^ -- .gitmodules &&
716 git add .gitmodules &&
717 git commit -m "new submodule restored .gitmodules"
c16c3e40
JL
718'
719
a028a193
SB
720test_expect_success 'fetching submodules respects parallel settings' '
721 git config fetch.recurseSubmodules true &&
ac48da5a 722 test_when_finished "rm -f downstream/trace.out" &&
a028a193
SB
723 (
724 cd downstream &&
e3a9d1ac
JH
725 GIT_TRACE=$(pwd)/trace.out git fetch &&
726 grep "1 tasks" trace.out &&
ac48da5a
ÆAB
727 >trace.out &&
728
a028a193
SB
729 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 7 &&
730 grep "7 tasks" trace.out &&
ac48da5a
ÆAB
731 >trace.out &&
732
a028a193
SB
733 git config submodule.fetchJobs 8 &&
734 GIT_TRACE=$(pwd)/trace.out git fetch &&
735 grep "8 tasks" trace.out &&
ac48da5a
ÆAB
736 >trace.out &&
737
a028a193 738 GIT_TRACE=$(pwd)/trace.out git fetch --jobs 9 &&
51243f9f
ÆAB
739 grep "9 tasks" trace.out &&
740 >trace.out &&
741
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
a028a193
SB
746 )
747'
748
10f5c526
JH
749test_expect_success 'fetching submodule into a broken repository' '
750 # Prepare src and src/sub nested in it
751 git init src &&
752 (
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 ) &&
759
760 # Clone the old-fashoned way
761 git clone src dst &&
762 git -C dst clone ../src/sub sub &&
763
764 # Make sure that old-fashoned layout is still supported
765 git -C dst status &&
766
767 # "diff" would find no change
768 git -C dst diff --exit-code &&
769
770 # Recursive-fetch works fine
771 git -C dst fetch --recurse-submodules &&
772
773 # Break the receiving submodule
7a746904 774 rm -r dst/sub/.git/objects &&
10f5c526
JH
775
776 # NOTE: without the fix the following tests will recurse forever!
777 # They should terminate with an error.
778
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
782'
783
c68f8375
HV
784test_expect_success "fetch new commits when submodule got renamed" '
785 git clone . downstream_rename &&
786 (
787 cd downstream_rename &&
c3749f6e 788 git submodule update --init --recursive &&
c68f8375
HV
789 git checkout -b rename &&
790 git mv submodule submodule_renamed &&
791 (
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 ) &&
804 (
805 cd downstream &&
806 git fetch --recurse-submodules=on-demand &&
807 (
808 cd submodule &&
809 git rev-parse origin/rename_sub >../../actual
810 )
811 ) &&
812 test_cmp expect actual
813'
814
be76c212
SB
815test_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 &&
822
823 git checkout --detach &&
824
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 &&
829
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 &&
833
834 git commit -m "updated submodules outside of refs/heads" &&
835 E=$(git rev-parse HEAD) &&
836 git update-ref refs/changes/3 $E &&
837 (
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
843 )
844'
845
846test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD' '
847 # depends on the previous test for setup
848
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 &&
853
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 &&
857
858 git commit -m "updated submodules outside of refs/heads" &&
859 E=$(git rev-parse HEAD) &&
860 git update-ref refs/changes/6 $E &&
861 (
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
867 )
868'
869
870test_expect_success 'fetch new submodule commits on-demand without .gitmodules entry' '
871 # depends on the previous test for setup
872
873 git config -f .gitmodules --remove-section submodule.sub1 &&
874 git add .gitmodules &&
875 git commit -m "delete gitmodules file" &&
b618a2d9 876 git checkout -B super &&
be76c212 877 git -C downstream fetch &&
b618a2d9 878 git -C downstream checkout origin/super &&
be76c212
SB
879
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 &&
884
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 &&
888
889 git commit -m "updated submodules outside of refs/heads" &&
890 E=$(git rev-parse HEAD) &&
891 git update-ref refs/changes/9 $E &&
892 (
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
898 )
899'
900
901test_expect_success 'fetch new submodule commit intermittently referenced by superproject' '
902 # depends on the previous test for setup
903
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}) &&
907
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" &&
911
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" &&
915
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" &&
919
920 G=$(git rev-parse HEAD) &&
921 git update-ref refs/changes/13 $G &&
922 (
923 cd downstream &&
924 git fetch --recurse-submodules origin refs/changes/13 &&
925
926 git -C sub1 cat-file -t $D &&
927 git -C sub1 cat-file -t $E &&
928 git -C sub1 cat-file -t $F
929 )
930'
931
505a2765
PK
932add_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
939}
940
941compare_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
951}
952
953
954test_expect_success 'setup nested submodule fetch test' '
955 # does not depend on any previous test setups
956
957 for repo in outer middle inner
958 do
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 &&
965
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 &&
971
972 git clone outer B &&
973 git -C B/ submodule update --init middle &&
974
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 &&
980
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
985'
986
987test_expect_success 'fetching a superproject containing an uninitialized sub/sub project' '
988 # depends on previous test for setup
989
990 git -C B/ fetch &&
991 compare_refs_in_dir A origin/HEAD B origin/HEAD
992'
993
994fetch_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
1004
1005 git -C "$1" fetch --recurse-submodules 2>&1 |
1006 sed "/Fetching submodule $2[^$]/q" >out &&
1007 ! grep "Fetching submodule $2[^$]" out
1008}
1009
1010test_expect_success 'setup recursive fetch with uninit submodule' '
1011 # does not depend on any previous test setups
1012
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 &&
1018
1019 git -C super submodule add ../sub &&
1020 git -C super commit -m "add sub" &&
1021
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
1026'
1027
1028test_expect_success 'recursive fetch with uninit submodule' '
1029 # depends on previous test for setup
1030
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
1035'
1036
1037test_expect_success 'recursive fetch after deinit a submodule' '
1038 # depends on previous test for setup
1039
1040 git -C superclone submodule update --init sub &&
1041 git -C superclone submodule deinit -f sub &&
1042
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
1047'
1048
b90d9f76
GC
1049test_expect_success 'setup repo with upstreams that share a submodule name' '
1050 mkdir same-name-1 &&
1051 (
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"
1059 (
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 ) &&
1068 (
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 &&
1078 (
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
1084 )
1085'
1086
1087test_expect_success 'fetch --recurse-submodules updates name-conflicted, populated submodule' '
1088 test_when_finished "git -C same-name-downstream checkout main" &&
1089 (
1090 cd same-name-1 &&
1091 test_commit -C submodule --no-tag b1 &&
1092 git add submodule &&
1093 git commit -m "super-b1"
1094 ) &&
1095 (
1096 cd same-name-2 &&
1097 test_commit -C submodule --no-tag b2 &&
1098 git add submodule &&
1099 git commit -m "super-b2"
1100 ) &&
1101 (
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 &&
1111
1112 super_head2=$(git -C same-name-2 rev-parse HEAD) &&
1113 git -C same-name-downstream cat-file -e $super_head2 &&
1114
1115 sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
1116 git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
1117
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
1120'
1121
1122test_expect_success 'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
1123 (
1124 cd same-name-1 &&
1125 test_commit -C submodule --no-tag c1 &&
1126 git add submodule &&
1127 git commit -m "super-c1"
1128 ) &&
1129 (
1130 cd same-name-2 &&
1131 test_commit -C submodule --no-tag c2 &&
1132 git add submodule &&
1133 git commit -m "super-c2"
1134 ) &&
1135 (
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) &&
1145 (
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
1153 )
1154'
1155
0353c688
JH
1156test_expect_success 'fetch --all with --recurse-submodules' '
1157 test_when_finished "rm -fr src_clone" &&
1158 git clone --recurse-submodules src src_clone &&
1159 (
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
1167'
1168
1169test_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 &&
1172 (
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
1181'
1182
5667141e
PS
1183test_expect_success "fetch --all with --no-recurse-submodules only fetches superproject" '
1184 test_when_finished "rm -rf src_clone" &&
1185
1186 git clone --recurse-submodules src src_clone &&
1187 (
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
1194'
1195
7dce19d3 1196test_done