Raw
1 #!/bin/sh
2 # Copyright (c) 2006, Junio C Hamano.
3
4 test_description='Per branch config variables affects "git fetch".
5
6 '
7
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-bundle.sh
10
11 if ! test_have_prereq PERL_TEST_HELPERS
12 then
13 skip_all='skipping fetch tests; Perl not available'
14 test_done
15 fi
16
17 test_expect_success setup '
18 echo >file original &&
19 git add file &&
20 git commit -a -m original &&
21 git branch -M main
22 '
23
24 test_expect_success "clone and setup child repos" '
25 git clone . one &&
26 (
27 cd one &&
28 echo >file updated by one &&
29 git commit -a -m "updated by one"
30 ) &&
31 git clone . two &&
32 (
33 cd two &&
34 git config branch.main.remote one &&
35 git config remote.one.url ../one/.git/ &&
36 git config remote.one.fetch refs/heads/main:refs/heads/one
37 ) &&
38 git clone . three &&
39 (
40 cd three &&
41 git config set remote.two.url ../two/.git/ &&
42 git config set remote.two.fetch refs/heads/main:refs/heads/two &&
43 git config set --append remote.two.fetch refs/heads/one:refs/heads/one &&
44 git config set branch.main.remote two &&
45 git config set branch.main.merge refs/heads/one
46 ) &&
47 git clone . bundle &&
48 git clone . seven &&
49 git clone --ref-format=reftable . case_sensitive &&
50 (
51 cd case_sensitive &&
52 git branch branch1 &&
53 git branch bRanch1
54 ) &&
55 git clone --ref-format=reftable . case_sensitive_fd &&
56 (
57 cd case_sensitive_fd &&
58 git branch foo/bar &&
59 git branch Foo
60 ) &&
61 git clone --ref-format=reftable . case_sensitive_df &&
62 (
63 cd case_sensitive_df &&
64 git branch Foo/bar &&
65 git branch foo
66 )
67 '
68
69 test_expect_success "fetch test" '
70 echo >file updated by origin &&
71 git commit -a -m "updated by origin" &&
72 (
73 cd two &&
74 git fetch &&
75 git rev-parse --verify refs/heads/one &&
76 mine=$(git rev-parse refs/heads/one) &&
77 his=$(cd ../one && git rev-parse refs/heads/main) &&
78 test "z$mine" = "z$his"
79 )
80 '
81
82 test_expect_success "fetch test for-merge" '
83 (
84 cd three &&
85 git fetch &&
86 git rev-parse --verify refs/heads/two &&
87 git rev-parse --verify refs/heads/one &&
88 main_in_two=$(cd ../two && git rev-parse main) &&
89 one_in_two=$(cd ../two && git rev-parse one) &&
90 {
91 echo "$one_in_two " &&
92 echo "$main_in_two not-for-merge"
93 } >expected &&
94 cut -f -2 .git/FETCH_HEAD >actual &&
95 test_cmp expected actual
96 )
97 '
98
99 test_expect_success "fetch test remote HEAD" '
100 (
101 cd two &&
102 git fetch &&
103 git rev-parse --verify refs/remotes/origin/HEAD &&
104 git rev-parse --verify refs/remotes/origin/main &&
105 head=$(git rev-parse refs/remotes/origin/HEAD) &&
106 branch=$(git rev-parse refs/remotes/origin/main) &&
107 test "z$head" = "z$branch"
108 )
109 '
110
111 test_expect_success "fetch test remote HEAD in bare repository" '
112 test_when_finished rm -rf barerepo &&
113 (
114 git init --bare barerepo &&
115 cd barerepo &&
116 git remote add upstream ../two &&
117 git fetch upstream &&
118 git rev-parse --verify refs/remotes/upstream/HEAD &&
119 git rev-parse --verify refs/remotes/upstream/main &&
120 head=$(git rev-parse refs/remotes/upstream/HEAD) &&
121 branch=$(git rev-parse refs/remotes/upstream/main) &&
122 test "z$head" = "z$branch"
123 )
124 '
125
126
127 test_expect_success "fetch test remote HEAD change" '
128 (
129 cd two &&
130 git switch -c other &&
131 git push -u origin other &&
132 git rev-parse --verify refs/remotes/origin/HEAD &&
133 git rev-parse --verify refs/remotes/origin/main &&
134 git rev-parse --verify refs/remotes/origin/other &&
135 git remote set-head origin other &&
136 git fetch &&
137 head=$(git rev-parse refs/remotes/origin/HEAD) &&
138 branch=$(git rev-parse refs/remotes/origin/other) &&
139 test "z$head" = "z$branch"
140 )
141 '
142
143 test_expect_success "fetch test followRemoteHEAD never" '
144 git -C two update-ref --no-deref -d refs/remotes/origin/HEAD &&
145 test_config -C two remote.origin.followRemoteHEAD "never" &&
146 GIT_TRACE_PACKET=$PWD/trace.out git -C two fetch &&
147 # Confirm that we do not even ask for HEAD when we are
148 # not going to act on it.
149 test_grep ! "ref-prefix HEAD" trace.out &&
150 test_must_fail git -C two rev-parse --verify refs/remotes/origin/HEAD
151 '
152
153 test_expect_success "fetch test followRemoteHEAD warn no change" '
154 git -C two rev-parse --verify refs/remotes/origin/other &&
155 git -C two remote set-head origin other &&
156 git -C two rev-parse --verify refs/remotes/origin/HEAD &&
157 git -C two rev-parse --verify refs/remotes/origin/main &&
158 test_config -C two remote.origin.followRemoteHEAD "warn" &&
159 git -C two fetch >output &&
160 echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
161 "but we have ${SQ}other${SQ} locally." >expect &&
162 test_cmp expect output &&
163 head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
164 branch=$(git -C two rev-parse refs/remotes/origin/other) &&
165 test "z$head" = "z$branch"
166 '
167
168 test_expect_success "fetch test followRemoteHEAD warn create" '
169 git -C two update-ref --no-deref -d refs/remotes/origin/HEAD &&
170 test_config -C two remote.origin.followRemoteHEAD "warn" &&
171 git -C two rev-parse --verify refs/remotes/origin/main &&
172 output=$(git -C two fetch) &&
173 test "z" = "z$output" &&
174 head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
175 branch=$(git -C two rev-parse refs/remotes/origin/main) &&
176 test "z$head" = "z$branch"
177 '
178
179 test_expect_success "fetch test followRemoteHEAD warn detached" '
180 git -C two update-ref --no-deref -d refs/remotes/origin/HEAD &&
181 git -C two update-ref refs/remotes/origin/HEAD HEAD &&
182 HEAD=$(git -C two log --pretty="%H") &&
183 test_config -C two remote.origin.followRemoteHEAD "warn" &&
184 git -C two fetch >output &&
185 echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
186 "but we have a detached HEAD pointing to" \
187 "${SQ}${HEAD}${SQ} locally." >expect &&
188 test_cmp expect output
189 '
190
191 test_expect_success "fetch test followRemoteHEAD warn quiet" '
192 git -C two rev-parse --verify refs/remotes/origin/other &&
193 git -C two remote set-head origin other &&
194 git -C two rev-parse --verify refs/remotes/origin/HEAD &&
195 git -C two rev-parse --verify refs/remotes/origin/main &&
196 test_config -C two remote.origin.followRemoteHEAD "warn" &&
197 output=$(git -C two fetch --quiet) &&
198 test "z" = "z$output" &&
199 head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
200 branch=$(git -C two rev-parse refs/remotes/origin/other) &&
201 test "z$head" = "z$branch"
202 '
203
204 test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is same" '
205 git -C two rev-parse --verify refs/remotes/origin/other &&
206 git -C two remote set-head origin other &&
207 git -C two rev-parse --verify refs/remotes/origin/HEAD &&
208 git -C two rev-parse --verify refs/remotes/origin/main &&
209 test_config -C two remote.origin.followRemoteHEAD "warn-if-not-main" &&
210 actual=$(git -C two fetch) &&
211 test "z" = "z$actual" &&
212 head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
213 branch=$(git -C two rev-parse refs/remotes/origin/other) &&
214 test "z$head" = "z$branch"
215 '
216
217 test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is different" '
218 git -C two rev-parse --verify refs/remotes/origin/other &&
219 git -C two remote set-head origin other &&
220 git -C two rev-parse --verify refs/remotes/origin/HEAD &&
221 git -C two rev-parse --verify refs/remotes/origin/main &&
222 test_config -C two remote.origin.followRemoteHEAD "warn-if-not-some/different-branch" &&
223 git -C two fetch >actual &&
224 echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \
225 "but we have ${SQ}other${SQ} locally." >expect &&
226 test_cmp expect actual &&
227 head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
228 branch=$(git -C two rev-parse refs/remotes/origin/other) &&
229 test "z$head" = "z$branch"
230 '
231
232 test_expect_success "fetch test followRemoteHEAD always" '
233 git -C two rev-parse --verify refs/remotes/origin/other &&
234 git -C two remote set-head origin other &&
235 git -C two rev-parse --verify refs/remotes/origin/HEAD &&
236 git -C two rev-parse --verify refs/remotes/origin/main &&
237 test_config -C two remote.origin.followRemoteHEAD "always" &&
238 git -C two fetch &&
239 head=$(git -C two rev-parse refs/remotes/origin/HEAD) &&
240 branch=$(git -C two rev-parse refs/remotes/origin/main) &&
241 test "z$head" = "z$branch"
242 '
243
244 test_expect_success 'followRemoteHEAD does not kick in with refspecs' '
245 git -C two remote set-head origin other &&
246 test_config -C two remote.origin.followRemoteHEAD always &&
247 git -C two fetch origin refs/heads/main:refs/remotes/origin/main &&
248 echo refs/remotes/origin/other >expect &&
249 git -C two symbolic-ref refs/remotes/origin/HEAD >actual &&
250 test_cmp expect actual
251 '
252
253 test_expect_success 'followRemoteHEAD create does not overwrite dangling symref' '
254 git -C two remote add -m does-not-exist custom-head ../one &&
255 test_config -C two remote.custom-head.followRemoteHEAD create &&
256 git -C two fetch custom-head &&
257 echo refs/remotes/custom-head/does-not-exist >expect &&
258 git -C two symbolic-ref refs/remotes/custom-head/HEAD >actual &&
259 test_cmp expect actual
260 '
261
262 test_expect_success 'fetch --prune on its own works as expected' '
263 git clone . prune &&
264 (
265 cd prune &&
266 git update-ref refs/remotes/origin/extrabranch main &&
267
268 git fetch --prune origin &&
269 test_must_fail git rev-parse origin/extrabranch
270 )
271 '
272
273 test_expect_success 'fetch --prune with a branch name keeps branches' '
274 git clone . prune-branch &&
275 (
276 cd prune-branch &&
277 git update-ref refs/remotes/origin/extrabranch main &&
278
279 git fetch --prune origin main &&
280 git rev-parse origin/extrabranch
281 )
282 '
283
284 test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
285 git clone . prune-namespace &&
286 (
287 cd prune-namespace &&
288
289 git fetch --prune origin refs/heads/a/*:refs/remotes/origin/a/* &&
290 git rev-parse origin/main
291 )
292 '
293
294 test_expect_success 'fetch --prune handles overlapping refspecs' '
295 git update-ref refs/pull/42/head main &&
296 git clone . prune-overlapping &&
297 (
298 cd prune-overlapping &&
299 git config --add remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
300
301 git fetch --prune origin &&
302 git rev-parse origin/main &&
303 git rev-parse origin/pr/42 &&
304
305 git config --unset-all remote.origin.fetch &&
306 git config remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
307 git config --add remote.origin.fetch refs/heads/*:refs/remotes/origin/* &&
308
309 git fetch --prune origin &&
310 git rev-parse origin/main &&
311 git rev-parse origin/pr/42
312 )
313 '
314
315 test_expect_success 'fetch --prune --tags prunes branches but not tags' '
316 git clone . prune-tags &&
317 (
318 cd prune-tags &&
319 git tag sometag main &&
320 # Create what looks like a remote-tracking branch from an earlier
321 # fetch that has since been deleted from the remote:
322 git update-ref refs/remotes/origin/fake-remote main &&
323
324 git fetch --prune --tags origin &&
325 git rev-parse origin/main &&
326 test_must_fail git rev-parse origin/fake-remote &&
327 git rev-parse sometag
328 )
329 '
330
331 test_expect_success 'fetch --prune --tags with branch does not prune other things' '
332 git clone . prune-tags-branch &&
333 (
334 cd prune-tags-branch &&
335 git tag sometag main &&
336 git update-ref refs/remotes/origin/extrabranch main &&
337
338 git fetch --prune --tags origin main &&
339 git rev-parse origin/extrabranch &&
340 git rev-parse sometag
341 )
342 '
343
344 test_expect_success 'fetch --prune --tags with refspec prunes based on refspec' '
345 git clone . prune-tags-refspec &&
346 (
347 cd prune-tags-refspec &&
348 git tag sometag main &&
349 git update-ref refs/remotes/origin/foo/otherbranch main &&
350 git update-ref refs/remotes/origin/extrabranch main &&
351
352 git fetch --prune --tags origin refs/heads/foo/*:refs/remotes/origin/foo/* &&
353 test_must_fail git rev-parse refs/remotes/origin/foo/otherbranch &&
354 git rev-parse origin/extrabranch &&
355 git rev-parse sometag
356 )
357 '
358
359 test_expect_success 'fetch --tags gets tags even without a configured remote' '
360 REMOTE="$(pwd)/test_tag_1" &&
361 git init test_tag_1 &&
362 (
363 cd test_tag_1 &&
364 test_commit foo
365 ) &&
366 git init test_tag_2 &&
367 (
368 cd test_tag_2 &&
369 git fetch --tags "file://$REMOTE" &&
370 echo "foo" >expect &&
371 git tag >actual &&
372 test_cmp expect actual
373 )
374 '
375
376 test_expect_success REFFILES 'fetch --prune fails to delete branches' '
377 git clone . prune-fail &&
378 (
379 cd prune-fail &&
380 git update-ref refs/remotes/origin/extrabranch main &&
381 git pack-refs --all &&
382 : this will prevent --prune from locking packed-refs for deleting refs, but adding loose refs still succeeds &&
383 >.git/packed-refs.new &&
384
385 test_must_fail git fetch --prune origin
386 )
387 '
388
389 test_expect_success 'fetch --atomic works with a single branch' '
390 test_when_finished "rm -rf atomic" &&
391
392 git clone . atomic &&
393 git branch atomic-branch &&
394 oid=$(git rev-parse atomic-branch) &&
395 echo "$oid" >expected &&
396
397 git -C atomic fetch --atomic origin &&
398 git -C atomic rev-parse origin/atomic-branch >actual &&
399 test_cmp expected actual &&
400 test $oid = "$(git -C atomic rev-parse --verify FETCH_HEAD)"
401 '
402
403 test_expect_success 'fetch --atomic works with multiple branches' '
404 test_when_finished "rm -rf atomic" &&
405
406 git clone . atomic &&
407 git branch atomic-branch-1 &&
408 git branch atomic-branch-2 &&
409 git branch atomic-branch-3 &&
410 git rev-parse refs/heads/atomic-branch-1 refs/heads/atomic-branch-2 refs/heads/atomic-branch-3 >actual &&
411
412 git -C atomic fetch --atomic origin &&
413 git -C atomic rev-parse refs/remotes/origin/atomic-branch-1 refs/remotes/origin/atomic-branch-2 refs/remotes/origin/atomic-branch-3 >expected &&
414 test_cmp expected actual
415 '
416
417 test_expect_success 'fetch --atomic works with mixed branches and tags' '
418 test_when_finished "rm -rf atomic" &&
419
420 git clone . atomic &&
421 git branch atomic-mixed-branch &&
422 git tag atomic-mixed-tag &&
423 git rev-parse refs/heads/atomic-mixed-branch refs/tags/atomic-mixed-tag >actual &&
424
425 git -C atomic fetch --tags --atomic origin &&
426 git -C atomic rev-parse refs/remotes/origin/atomic-mixed-branch refs/tags/atomic-mixed-tag >expected &&
427 test_cmp expected actual
428 '
429
430 test_expect_success 'fetch --atomic prunes references' '
431 test_when_finished "rm -rf atomic" &&
432
433 git branch atomic-prune-delete &&
434 git clone . atomic &&
435 git branch --delete atomic-prune-delete &&
436 git branch atomic-prune-create &&
437 git rev-parse refs/heads/atomic-prune-create >actual &&
438
439 git -C atomic fetch --prune --atomic origin &&
440 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-prune-delete &&
441 git -C atomic rev-parse refs/remotes/origin/atomic-prune-create >expected &&
442 test_cmp expected actual
443 '
444
445 test_expect_success 'fetch --atomic aborts with non-fast-forward update' '
446 test_when_finished "rm -rf atomic" &&
447
448 git branch atomic-non-ff &&
449 git clone . atomic &&
450 git rev-parse HEAD >actual &&
451
452 git branch atomic-new-branch &&
453 parent_commit=$(git rev-parse atomic-non-ff~) &&
454 git update-ref refs/heads/atomic-non-ff $parent_commit &&
455
456 test_must_fail git -C atomic fetch --atomic origin refs/heads/*:refs/remotes/origin/* &&
457 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-new-branch &&
458 git -C atomic rev-parse refs/remotes/origin/atomic-non-ff >expected &&
459 test_cmp expected actual &&
460 test_must_be_empty atomic/.git/FETCH_HEAD
461 '
462
463 test_expect_success 'fetch --atomic executes a single reference transaction only' '
464 test_when_finished "rm -rf atomic" &&
465
466 git clone . atomic &&
467 git branch atomic-hooks-1 &&
468 git branch atomic-hooks-2 &&
469 head_oid=$(git rev-parse HEAD) &&
470
471 cat >expected <<-EOF &&
472 preparing
473 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
474 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
475 prepared
476 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
477 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
478 committed
479 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
480 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
481 preparing
482 $ZERO_OID ref:refs/remotes/origin/main refs/remotes/origin/HEAD
483 EOF
484
485 rm -f atomic/actual &&
486 test_hook -C atomic reference-transaction <<-\EOF &&
487 ( echo "$*" && cat ) >>actual
488 EOF
489
490 git -C atomic fetch --atomic origin &&
491 test_cmp expected atomic/actual
492 '
493
494 test_expect_success 'fetch --atomic aborts all reference updates if hook aborts' '
495 test_when_finished "rm -rf atomic" &&
496
497 git clone . atomic &&
498 git branch atomic-hooks-abort-1 &&
499 git branch atomic-hooks-abort-2 &&
500 git branch atomic-hooks-abort-3 &&
501 git tag atomic-hooks-abort &&
502 head_oid=$(git rev-parse HEAD) &&
503
504 cat >expected <<-EOF &&
505 preparing
506 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
507 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
508 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
509 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
510 aborted
511 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
512 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
513 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
514 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
515 EOF
516
517 rm -f atomic/actual &&
518 test_hook -C atomic/.git reference-transaction <<-\EOF &&
519 ( echo "$*" && cat ) >>actual
520 exit 1
521 EOF
522
523 git -C atomic for-each-ref >expected-refs &&
524 test_must_fail git -C atomic fetch --tags --atomic origin &&
525 git -C atomic for-each-ref >actual-refs &&
526 test_cmp expected-refs actual-refs &&
527 test_must_be_empty atomic/.git/FETCH_HEAD
528 '
529
530 test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' '
531 test_when_finished "rm -rf atomic" &&
532
533 git clone . atomic &&
534 oid=$(git rev-parse HEAD) &&
535
536 git branch atomic-fetch-head-1 &&
537 git -C atomic fetch --atomic origin atomic-fetch-head-1 &&
538 test_line_count = 1 atomic/.git/FETCH_HEAD &&
539
540 git branch atomic-fetch-head-2 &&
541 git -C atomic fetch --atomic --append origin atomic-fetch-head-2 &&
542 test_line_count = 2 atomic/.git/FETCH_HEAD &&
543 cp atomic/.git/FETCH_HEAD expected &&
544
545 test_hook -C atomic reference-transaction <<-\EOF &&
546 exit 1
547 EOF
548
549 git branch atomic-fetch-head-3 &&
550 test_must_fail git -C atomic fetch --atomic --append origin atomic-fetch-head-3 &&
551 test_cmp expected atomic/.git/FETCH_HEAD
552 '
553
554 test_expect_success REFFILES 'fetch --atomic fails transaction if reference locked' '
555 test_when_finished "rm -rf upstream repo" &&
556
557 git init upstream &&
558 git -C upstream commit --allow-empty -m 1 &&
559 git -C upstream switch -c foobar &&
560 git clone --mirror upstream repo &&
561 git -C upstream commit --allow-empty -m 2 &&
562 touch repo/refs/heads/foobar.lock &&
563
564 test_must_fail git -C repo fetch --atomic origin
565 '
566
567 test_expect_success '--refmap="" ignores configured refspec' '
568 git clone . remote-refs &&
569 git -C remote-refs rev-parse remotes/origin/main >old &&
570 git -C remote-refs update-ref refs/remotes/origin/main main~1 &&
571 git -C remote-refs rev-parse remotes/origin/main >new &&
572 git -C remote-refs fetch --refmap= origin "+refs/heads/*:refs/hidden/origin/*" &&
573 git -C remote-refs rev-parse remotes/origin/main >actual &&
574 test_cmp new actual &&
575 git -C remote-refs fetch origin &&
576 git -C remote-refs rev-parse remotes/origin/main >actual &&
577 test_cmp old actual
578 '
579
580 test_expect_success '--refmap="" and --prune' '
581 git -C remote-refs update-ref refs/remotes/origin/foo/otherbranch main &&
582 git -C remote-refs update-ref refs/hidden/foo/otherbranch main &&
583 git -C remote-refs fetch --prune --refmap="" origin +refs/heads/*:refs/hidden/* &&
584 git -C remote-refs rev-parse remotes/origin/foo/otherbranch &&
585 test_must_fail git -C remote-refs rev-parse refs/hidden/foo/otherbranch &&
586 git -C remote-refs fetch --prune origin &&
587 test_must_fail git -C remote-refs rev-parse remotes/origin/foo/otherbranch
588 '
589
590 test_expect_success 'fetch tags when there is no tags' '
591
592 git init notags &&
593 git -C notags fetch -t ..
594
595 '
596
597 test_expect_success 'fetch following tags' '
598
599 git tag -a -m "annotated" anno HEAD &&
600 git tag light HEAD &&
601
602 git init four &&
603 (
604 cd four &&
605 git fetch .. :track &&
606 git show-ref --verify refs/tags/anno &&
607 git show-ref --verify refs/tags/light
608 )
609 '
610
611 test_expect_success 'fetch uses remote ref names to describe new refs' '
612 git init descriptive &&
613 (
614 cd descriptive &&
615 git config remote.o.url .. &&
616 git config remote.o.fetch "refs/heads/*:refs/crazyheads/*" &&
617 git config --add remote.o.fetch "refs/others/*:refs/heads/*" &&
618 git fetch o
619 ) &&
620 git tag -a -m "Descriptive tag" descriptive-tag &&
621 git branch descriptive-branch &&
622 git checkout descriptive-branch &&
623 echo "Nuts" >crazy &&
624 git add crazy &&
625 git commit -a -m "descriptive commit" &&
626 git update-ref refs/others/crazy HEAD &&
627 (
628 cd descriptive &&
629 git fetch o 2>actual &&
630 test_grep "new branch.* -> refs/crazyheads/descriptive-branch$" actual &&
631 test_grep "new tag.* -> descriptive-tag$" actual &&
632 test_grep "new ref.* -> crazy$" actual
633 ) &&
634 git checkout main
635 '
636
637 test_expect_success 'fetch must not resolve short tag name' '
638
639 git init five &&
640 test_must_fail git -C five fetch .. anno:five
641
642 '
643
644 test_expect_success 'fetch can now resolve short remote name' '
645
646 git update-ref refs/remotes/six/HEAD HEAD &&
647
648 git init six &&
649 git -C six fetch .. six:six
650 '
651
652 test_expect_success 'create bundle 1' '
653 echo >file updated again by origin &&
654 git commit -a -m "tip" &&
655 git bundle create --version=3 bundle1 main^..main
656 '
657
658 test_expect_success 'header of bundle looks right' '
659 cat >expect <<-EOF &&
660 # v3 git bundle
661 @object-format=$(test_oid algo)
662 -OID updated by origin
663 OID refs/heads/main
664
665 EOF
666 sed -e "s/$OID_REGEX/OID/g" -e "5q" bundle1 >actual &&
667 test_cmp expect actual
668 '
669
670 test_expect_success 'create bundle 2' '
671 git bundle create bundle2 main~2..main
672 '
673
674 test_expect_success 'unbundle 1' '
675 (
676 cd bundle &&
677 git checkout -b some-branch &&
678 test_must_fail git fetch bundle1 main:main
679 )
680 '
681
682
683 test_expect_success 'bundle 1 has only 3 files ' '
684 test_bundle_object_count bundle1 3
685 '
686
687 test_expect_success 'unbundle 2' '
688 (
689 cd bundle &&
690 git fetch ../bundle2 main:main &&
691 test "tip" = "$(git log -1 --pretty=oneline main | cut -d" " -f2)"
692 )
693 '
694
695 test_expect_success 'bundle does not prerequisite objects' '
696 touch file2 &&
697 git add file2 &&
698 git commit -m add.file2 file2 &&
699 git bundle create bundle3 -1 HEAD &&
700 test_bundle_object_count bundle3 3
701 '
702
703 test_expect_success 'bundle should be able to create a full history' '
704
705 git tag -a -m "1.0" v1.0 main &&
706 git bundle create bundle4 v1.0
707
708 '
709
710 test_expect_success 'fetch with a non-applying branch.<name>.merge' '
711 git config branch.main.remote yeti &&
712 git config branch.main.merge refs/heads/bigfoot &&
713 git config remote.blub.url one &&
714 git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
715 git fetch blub
716 '
717
718 # URL supplied to fetch does not match the url of the configured branch's remote
719 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
720 one_head=$(cd one && git rev-parse HEAD) &&
721 this_head=$(git rev-parse HEAD) &&
722 rm .git/FETCH_HEAD &&
723 git fetch one &&
724 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
725 test $this_head = "$(git rev-parse --verify HEAD)"
726 '
727
728 # URL supplied to fetch matches the url of the configured branch's remote and
729 # the merge spec matches the branch the remote HEAD points to
730 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
731 one_ref=$(cd one && git symbolic-ref HEAD) &&
732 git config branch.main.remote blub &&
733 git config branch.main.merge "$one_ref" &&
734 rm .git/FETCH_HEAD &&
735 git fetch one &&
736 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
737 test $this_head = "$(git rev-parse --verify HEAD)"
738 '
739
740 # URL supplied to fetch matches the url of the configured branch's remote, but
741 # the merge spec does not match the branch the remote HEAD points to
742 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
743 git config branch.main.merge "${one_ref}_not" &&
744 rm .git/FETCH_HEAD &&
745 git fetch one &&
746 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
747 test $this_head = "$(git rev-parse --verify HEAD)"
748 '
749
750 # the strange name is: a\!'b
751 test_expect_success 'quoting of a strangely named repo' '
752 test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
753 grep "fatal: '\''a\\\\!'\''b'\''" result
754 '
755
756 test_expect_success 'bundle should record HEAD correctly' '
757
758 git bundle create bundle5 HEAD main &&
759 git bundle list-heads bundle5 >actual &&
760 for h in HEAD refs/heads/main
761 do
762 echo "$(git rev-parse --verify $h) $h" || return 1
763 done >expect &&
764 test_cmp expect actual
765
766 '
767
768 test_expect_success 'mark initial state of origin/main' '
769 (
770 cd three &&
771 git tag base-origin-main refs/remotes/origin/main
772 )
773 '
774
775 test_expect_success 'explicit fetch should update tracking' '
776
777 git branch -f side &&
778 (
779 cd three &&
780 git update-ref refs/remotes/origin/main base-origin-main &&
781 o=$(git rev-parse --verify refs/remotes/origin/main) &&
782 git fetch origin main &&
783 n=$(git rev-parse --verify refs/remotes/origin/main) &&
784 test "$o" != "$n" &&
785 test_must_fail git rev-parse --verify refs/remotes/origin/side
786 )
787 '
788
789 test_expect_success 'explicit pull should update tracking' '
790
791 git branch -f side &&
792 (
793 cd three &&
794 git update-ref refs/remotes/origin/main base-origin-main &&
795 o=$(git rev-parse --verify refs/remotes/origin/main) &&
796 git pull origin main &&
797 n=$(git rev-parse --verify refs/remotes/origin/main) &&
798 test "$o" != "$n" &&
799 test_must_fail git rev-parse --verify refs/remotes/origin/side
800 )
801 '
802
803 test_expect_success 'explicit --refmap is allowed only with command-line refspec' '
804 (
805 cd three &&
806 test_must_fail git fetch --refmap="*:refs/remotes/none/*"
807 )
808 '
809
810 test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
811 git branch -f side &&
812 (
813 cd three &&
814 git update-ref refs/remotes/origin/main base-origin-main &&
815 o=$(git rev-parse --verify refs/remotes/origin/main) &&
816 git fetch --refmap="refs/heads/*:refs/remotes/other/*" origin main &&
817 n=$(git rev-parse --verify refs/remotes/origin/main) &&
818 test "$o" = "$n" &&
819 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
820 git rev-parse --verify refs/remotes/other/main
821 )
822 '
823
824 test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
825 git branch -f side &&
826 (
827 cd three &&
828 git update-ref refs/remotes/origin/main base-origin-main &&
829 o=$(git rev-parse --verify refs/remotes/origin/main) &&
830 git fetch --refmap="" origin main &&
831 n=$(git rev-parse --verify refs/remotes/origin/main) &&
832 test "$o" = "$n" &&
833 test_must_fail git rev-parse --verify refs/remotes/origin/side
834 )
835 '
836
837 test_expect_success 'configured fetch updates tracking' '
838
839 git branch -f side &&
840 (
841 cd three &&
842 git update-ref refs/remotes/origin/main base-origin-main &&
843 o=$(git rev-parse --verify refs/remotes/origin/main) &&
844 git fetch origin &&
845 n=$(git rev-parse --verify refs/remotes/origin/main) &&
846 test "$o" != "$n" &&
847 git rev-parse --verify refs/remotes/origin/side
848 )
849 '
850
851 test_expect_success 'non-matching refspecs do not confuse tracking update' '
852 git update-ref refs/odd/location HEAD &&
853 (
854 cd three &&
855 git update-ref refs/remotes/origin/main base-origin-main &&
856 git config --add remote.origin.fetch \
857 refs/odd/location:refs/remotes/origin/odd &&
858 o=$(git rev-parse --verify refs/remotes/origin/main) &&
859 git fetch origin main &&
860 n=$(git rev-parse --verify refs/remotes/origin/main) &&
861 test "$o" != "$n" &&
862 test_must_fail git rev-parse --verify refs/remotes/origin/odd
863 )
864 '
865
866 test_expect_success 'pushing nonexistent branch by mistake should not segv' '
867
868 test_must_fail git push seven no:no
869
870 '
871
872 test_expect_success 'auto tag following fetches minimum' '
873
874 git clone .git follow &&
875 git checkout HEAD^0 &&
876 (
877 for i in 1 2 3 4 5 6 7
878 do
879 echo $i >>file &&
880 git commit -m $i -a &&
881 git tag -a -m $i excess-$i || exit 1
882 done
883 ) &&
884 git checkout main &&
885 (
886 cd follow &&
887 git fetch
888 )
889 '
890
891 test_expect_success 'refuse to fetch into the current branch' '
892
893 test_must_fail git fetch . side:main
894
895 '
896
897 test_expect_success 'fetch into the current branch with --update-head-ok' '
898
899 git fetch --update-head-ok . side:main
900
901 '
902
903 test_expect_success 'fetch --dry-run does not touch FETCH_HEAD, but still prints what would be written' '
904 rm -f .git/FETCH_HEAD err &&
905 git fetch --dry-run . 2>err &&
906 ! test -f .git/FETCH_HEAD &&
907 grep FETCH_HEAD err
908 '
909
910 test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD, and does not print what would be written' '
911 rm -f .git/FETCH_HEAD err &&
912 git fetch --no-write-fetch-head . 2>err &&
913 ! test -f .git/FETCH_HEAD &&
914 ! grep FETCH_HEAD err
915 '
916
917 test_expect_success '--write-fetch-head gets defeated by --dry-run' '
918 rm -f .git/FETCH_HEAD &&
919 git fetch --dry-run --write-fetch-head . &&
920 ! test -f .git/FETCH_HEAD
921 '
922
923 test_expect_success "should be able to fetch with duplicate refspecs" '
924 mkdir dups &&
925 (
926 cd dups &&
927 git init &&
928 git config branch.main.remote three &&
929 git config remote.three.url ../three/.git &&
930 git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
931 git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
932 git fetch three
933 )
934 '
935
936 test_expect_success 'LHS of refspec follows ref disambiguation rules' '
937 mkdir lhs-ambiguous &&
938 (
939 cd lhs-ambiguous &&
940 git init server &&
941 test_commit -C server unwanted &&
942 test_commit -C server wanted &&
943
944 git init client &&
945
946 # Check a name coming after "refs" alphabetically ...
947 git -C server update-ref refs/heads/s wanted &&
948 git -C server update-ref refs/heads/refs/heads/s unwanted &&
949 git -C client fetch ../server +refs/heads/s:refs/heads/checkthis &&
950 git -C server rev-parse wanted >expect &&
951 git -C client rev-parse checkthis >actual &&
952 test_cmp expect actual &&
953
954 # ... and one before.
955 git -C server update-ref refs/heads/q wanted &&
956 git -C server update-ref refs/heads/refs/heads/q unwanted &&
957 git -C client fetch ../server +refs/heads/q:refs/heads/checkthis &&
958 git -C server rev-parse wanted >expect &&
959 git -C client rev-parse checkthis >actual &&
960 test_cmp expect actual &&
961
962 # Tags are preferred over branches like refs/{heads,tags}/*
963 git -C server update-ref refs/tags/t wanted &&
964 git -C server update-ref refs/heads/t unwanted &&
965 git -C client fetch ../server +t:refs/heads/checkthis &&
966 git -C server rev-parse wanted >expect &&
967 git -C client rev-parse checkthis >actual
968 )
969 '
970
971 test_expect_success 'fetch.writeCommitGraph' '
972 git clone three write &&
973 (
974 cd three &&
975 test_commit new
976 ) &&
977 (
978 cd write &&
979 git -c fetch.writeCommitGraph fetch origin &&
980 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
981 )
982 '
983
984 test_expect_success 'fetch.writeCommitGraph with submodules' '
985 test_config_global protocol.file.allow always &&
986 git clone dups super &&
987 (
988 cd super &&
989 git submodule add "file://$TRASH_DIRECTORY/three" &&
990 git commit -m "add submodule"
991 ) &&
992 git clone "super" super-clone &&
993 (
994 cd super-clone &&
995 rm -rf .git/objects/info &&
996 git -c fetch.writeCommitGraph=true fetch origin &&
997 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain &&
998 git -c fetch.writeCommitGraph=true fetch --recurse-submodules origin
999 )
1000 '
1001
1002 # fetches from first configured url
1003 test_expect_success 'fetch from multiple configured URLs in single remote' '
1004 git init url1 &&
1005 git remote add multipleurls url1 &&
1006 git remote set-url --add multipleurls url2 &&
1007 git fetch multipleurls
1008 '
1009
1010 # configured prune tests
1011
1012 set_config_tristate () {
1013 # var=$1 val=$2
1014 case "$2" in
1015 unset)
1016 test_unconfig "$1"
1017 ;;
1018 *)
1019 git config "$1" "$2"
1020 key=$(echo $1 | sed -e 's/^remote\.origin/fetch/')
1021 git_fetch_c="$git_fetch_c -c $key=$2"
1022 ;;
1023 esac
1024 }
1025
1026 test_configured_prune () {
1027 test_configured_prune_type "$@" "name"
1028 test_configured_prune_type "$@" "link"
1029 }
1030
1031 test_configured_prune_type () {
1032 fetch_prune=$1
1033 remote_origin_prune=$2
1034 fetch_prune_tags=$3
1035 remote_origin_prune_tags=$4
1036 expected_branch=$5
1037 expected_tag=$6
1038 cmdline=$7
1039 mode=$8
1040
1041 if test -z "$cmdline_setup"
1042 then
1043 test_expect_success 'setup cmdline_setup variable for subsequent test' '
1044 remote_url="file://$(git -C one config remote.origin.url)" &&
1045 remote_fetch="$(git -C one config remote.origin.fetch)" &&
1046 cmdline_setup="\"$remote_url\" \"$remote_fetch\""
1047 '
1048 fi
1049
1050 if test "$mode" = 'link'
1051 then
1052 new_cmdline=""
1053
1054 if test "$cmdline" = ""
1055 then
1056 new_cmdline=$cmdline_setup
1057 else
1058 new_cmdline=$(perl -e '
1059 my ($cmdline, $url) = @ARGV;
1060 $cmdline =~ s[origin(?!/)][quotemeta($url)]ge;
1061 print $cmdline;
1062 ' -- "$cmdline" "$remote_url")
1063 fi
1064
1065 if test "$fetch_prune_tags" = 'true' ||
1066 test "$remote_origin_prune_tags" = 'true'
1067 then
1068 if ! printf '%s' "$cmdline\n" | grep -q refs/remotes/origin/
1069 then
1070 new_cmdline="$new_cmdline refs/tags/*:refs/tags/*"
1071 fi
1072 fi
1073
1074 cmdline="$new_cmdline"
1075 fi
1076
1077 test_expect_success "$mode prune fetch.prune=$1 remote.origin.prune=$2 fetch.pruneTags=$3 remote.origin.pruneTags=$4${7:+ $7}; branch:$5 tag:$6" '
1078 # make sure a newbranch is there in . and also in one
1079 git branch -f newbranch &&
1080 git tag -f newtag &&
1081 (
1082 cd one &&
1083 test_unconfig fetch.prune &&
1084 test_unconfig fetch.pruneTags &&
1085 test_unconfig remote.origin.prune &&
1086 test_unconfig remote.origin.pruneTags &&
1087 git fetch '"$cmdline_setup"' &&
1088 git rev-parse --verify refs/remotes/origin/newbranch &&
1089 git rev-parse --verify refs/tags/newtag
1090 ) &&
1091
1092 # now remove them
1093 git branch -d newbranch &&
1094 git tag -d newtag &&
1095
1096 # then test
1097 (
1098 cd one &&
1099 git_fetch_c="" &&
1100 set_config_tristate fetch.prune $fetch_prune &&
1101 set_config_tristate fetch.pruneTags $fetch_prune_tags &&
1102 set_config_tristate remote.origin.prune $remote_origin_prune &&
1103 set_config_tristate remote.origin.pruneTags $remote_origin_prune_tags &&
1104
1105 if test "$mode" != "link"
1106 then
1107 git_fetch_c=""
1108 fi &&
1109 git$git_fetch_c fetch '"$cmdline"' &&
1110 case "$expected_branch" in
1111 pruned)
1112 test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
1113 ;;
1114 kept)
1115 git rev-parse --verify refs/remotes/origin/newbranch
1116 ;;
1117 esac &&
1118 case "$expected_tag" in
1119 pruned)
1120 test_must_fail git rev-parse --verify refs/tags/newtag
1121 ;;
1122 kept)
1123 git rev-parse --verify refs/tags/newtag
1124 ;;
1125 esac
1126 )
1127 '
1128 }
1129
1130 # $1 config: fetch.prune
1131 # $2 config: remote.<name>.prune
1132 # $3 config: fetch.pruneTags
1133 # $4 config: remote.<name>.pruneTags
1134 # $5 expect: branch to be pruned?
1135 # $6 expect: tag to be pruned?
1136 # $7 git-fetch $cmdline:
1137 #
1138 # $1 $2 $3 $4 $5 $6 $7
1139 test_configured_prune unset unset unset unset kept kept ""
1140 test_configured_prune unset unset unset unset kept kept "--no-prune"
1141 test_configured_prune unset unset unset unset pruned kept "--prune"
1142 test_configured_prune unset unset unset unset kept pruned \
1143 "--prune origin refs/tags/*:refs/tags/*"
1144 test_configured_prune unset unset unset unset pruned pruned \
1145 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
1146
1147 test_configured_prune false unset unset unset kept kept ""
1148 test_configured_prune false unset unset unset kept kept "--no-prune"
1149 test_configured_prune false unset unset unset pruned kept "--prune"
1150
1151 test_configured_prune true unset unset unset pruned kept ""
1152 test_configured_prune true unset unset unset pruned kept "--prune"
1153 test_configured_prune true unset unset unset kept kept "--no-prune"
1154
1155 test_configured_prune unset false unset unset kept kept ""
1156 test_configured_prune unset false unset unset kept kept "--no-prune"
1157 test_configured_prune unset false unset unset pruned kept "--prune"
1158
1159 test_configured_prune false false unset unset kept kept ""
1160 test_configured_prune false false unset unset kept kept "--no-prune"
1161 test_configured_prune false false unset unset pruned kept "--prune"
1162 test_configured_prune false false unset unset kept pruned \
1163 "--prune origin refs/tags/*:refs/tags/*"
1164 test_configured_prune false false unset unset pruned pruned \
1165 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
1166
1167 test_configured_prune true false unset unset kept kept ""
1168 test_configured_prune true false unset unset pruned kept "--prune"
1169 test_configured_prune true false unset unset kept kept "--no-prune"
1170
1171 test_configured_prune unset true unset unset pruned kept ""
1172 test_configured_prune unset true unset unset kept kept "--no-prune"
1173 test_configured_prune unset true unset unset pruned kept "--prune"
1174
1175 test_configured_prune false true unset unset pruned kept ""
1176 test_configured_prune false true unset unset kept kept "--no-prune"
1177 test_configured_prune false true unset unset pruned kept "--prune"
1178
1179 test_configured_prune true true unset unset pruned kept ""
1180 test_configured_prune true true unset unset pruned kept "--prune"
1181 test_configured_prune true true unset unset kept kept "--no-prune"
1182 test_configured_prune true true unset unset kept pruned \
1183 "--prune origin refs/tags/*:refs/tags/*"
1184 test_configured_prune true true unset unset pruned pruned \
1185 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
1186
1187 # --prune-tags on its own does nothing, needs --prune as well, same
1188 # for fetch.pruneTags without fetch.prune
1189 test_configured_prune unset unset unset unset kept kept "--prune-tags"
1190 test_configured_prune unset unset true unset kept kept ""
1191 test_configured_prune unset unset unset true kept kept ""
1192
1193 # These will prune the tags
1194 test_configured_prune unset unset unset unset pruned pruned "--prune --prune-tags"
1195 test_configured_prune true unset true unset pruned pruned ""
1196 test_configured_prune unset true unset true pruned pruned ""
1197
1198 # remote.<name>.pruneTags overrides fetch.pruneTags, just like
1199 # remote.<name>.prune overrides fetch.prune if set.
1200 test_configured_prune true unset true unset pruned pruned ""
1201 test_configured_prune false true false true pruned pruned ""
1202 test_configured_prune true false true false kept kept ""
1203
1204 # When --prune-tags is supplied it's ignored if an explicit refspec is
1205 # given, same for the configuration options.
1206 test_configured_prune unset unset unset unset pruned kept \
1207 "--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"
1208 test_configured_prune unset unset true unset pruned kept \
1209 "--prune origin +refs/heads/*:refs/remotes/origin/*"
1210 test_configured_prune unset unset unset true pruned kept \
1211 "--prune origin +refs/heads/*:refs/remotes/origin/*"
1212
1213 # Pruning that also takes place if a file:// url replaces a named
1214 # remote. However, because there's no implicit
1215 # +refs/heads/*:refs/remotes/origin/* refspec and supplying it on the
1216 # command-line negates --prune-tags, the branches will not be pruned.
1217 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "name"
1218 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "link"
1219 test_configured_prune_type unset unset unset unset pruned pruned "origin --prune --prune-tags" "name"
1220 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1221 test_configured_prune_type unset unset unset unset pruned pruned "--prune --prune-tags origin" "name"
1222 test_configured_prune_type unset unset unset unset kept pruned "--prune --prune-tags origin" "link"
1223 test_configured_prune_type unset unset true unset pruned pruned "--prune origin" "name"
1224 test_configured_prune_type unset unset true unset kept pruned "--prune origin" "link"
1225 test_configured_prune_type unset unset unset true pruned pruned "--prune origin" "name"
1226 test_configured_prune_type unset unset unset true kept pruned "--prune origin" "link"
1227 test_configured_prune_type true unset true unset pruned pruned "origin" "name"
1228 test_configured_prune_type true unset true unset kept pruned "origin" "link"
1229 test_configured_prune_type unset true true unset pruned pruned "origin" "name"
1230 test_configured_prune_type unset true true unset kept pruned "origin" "link"
1231 test_configured_prune_type unset true unset true pruned pruned "origin" "name"
1232 test_configured_prune_type unset true unset true kept pruned "origin" "link"
1233
1234 # When all remote.origin.fetch settings are deleted a --prune
1235 # --prune-tags still implicitly supplies refs/tags/*:refs/tags/* so
1236 # tags, but not tracking branches, will be deleted.
1237 test_expect_success 'remove remote.origin.fetch "one"' '
1238 (
1239 cd one &&
1240 git config --unset-all remote.origin.fetch
1241 )
1242 '
1243 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "name"
1244 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1245
1246 test_expect_success 'all boundary commits are excluded' '
1247 test_commit base &&
1248 test_commit oneside &&
1249 git checkout HEAD^ &&
1250 test_commit otherside &&
1251 git checkout main &&
1252 test_tick &&
1253 git merge otherside &&
1254 ad=$(git log --no-walk --format=%ad HEAD) &&
1255
1256 # If the a different name hash function is used here, then no delta
1257 # pair is found and the bundle does not expand to three objects
1258 # when fixing the thin object.
1259 GIT_TEST_NAME_HASH_VERSION=1 \
1260 git bundle create twoside-boundary.bdl main --since="$ad" &&
1261 test_bundle_object_count --thin twoside-boundary.bdl 3
1262 '
1263
1264 test_expect_success 'fetch --prune prints the remotes url' '
1265 git branch goodbye &&
1266 git clone . only-prunes &&
1267 git branch -D goodbye &&
1268 (
1269 cd only-prunes &&
1270 git fetch --prune origin 2>&1 | head -n1 >../actual
1271 ) &&
1272 echo "From $(pwd)/." >expect &&
1273 test_cmp expect actual
1274 '
1275
1276 test_expect_success 'branchname D/F conflict resolved by --prune' '
1277 git branch dir/file &&
1278 git clone . prune-df-conflict &&
1279 git branch -D dir/file &&
1280 git branch dir &&
1281 (
1282 cd prune-df-conflict &&
1283 git fetch --prune &&
1284 git rev-parse origin/dir >../actual
1285 ) &&
1286 git rev-parse dir >expect &&
1287 test_cmp expect actual
1288 '
1289
1290 test_expect_success 'branchname D/F conflict rejected with targeted error message' '
1291 git clone . df-conflict-error &&
1292 git branch dir_conflict &&
1293 (
1294 cd df-conflict-error &&
1295 git update-ref refs/remotes/origin/dir_conflict/file HEAD &&
1296 test_must_fail git fetch 2>err &&
1297 test_grep "error: some local refs could not be updated; try running" err &&
1298 test_grep " ${SQ}git remote prune origin${SQ} to remove any old, conflicting branches" err &&
1299 git pack-refs --all &&
1300 test_must_fail git fetch 2>err-packed &&
1301 test_grep "error: some local refs could not be updated; try running" err-packed &&
1302 test_grep " ${SQ}git remote prune origin${SQ} to remove any old, conflicting branches" err-packed
1303 )
1304 '
1305
1306 test_expect_success 'fetching a one-level ref works' '
1307 test_commit extra &&
1308 git reset --hard HEAD^ &&
1309 git update-ref refs/foo extra &&
1310 git init one-level &&
1311 (
1312 cd one-level &&
1313 git fetch .. HEAD refs/foo
1314 )
1315 '
1316
1317 test_expect_success 'fetching with auto-gc does not lock up' '
1318 write_script askyesno <<-\EOF &&
1319 echo "$*" &&
1320 false
1321 EOF
1322 git clone "file://$PWD" auto-gc &&
1323 test_commit test2 &&
1324 (
1325 cd auto-gc &&
1326 git config fetch.unpackLimit 1 &&
1327 git config gc.autoPackLimit 1 &&
1328 git config gc.autoDetach false &&
1329 git config maintenance.strategy gc &&
1330 GIT_ASK_YESNO="$TRASH_DIRECTORY/askyesno" git fetch --verbose >fetch.out 2>&1 &&
1331 test_grep "Auto packing the repository" fetch.out &&
1332 ! grep "Should I try again" fetch.out
1333 )
1334 '
1335
1336 for section in fetch transfer
1337 do
1338 test_expect_success "$section.hideRefs affects connectivity check" '
1339 GIT_TRACE="$PWD"/trace git -c $section.hideRefs=refs -c \
1340 $section.hideRefs="!refs/tags/" fetch &&
1341 grep "git rev-list .*--exclude-hidden=fetch" trace
1342 '
1343 done
1344
1345 test_expect_success 'prepare source branch' '
1346 echo one >onebranch &&
1347 git checkout --orphan onebranch &&
1348 git rm --cached -r . &&
1349 git add onebranch &&
1350 git commit -m onebranch &&
1351 git rev-list --objects onebranch -- >actual &&
1352 # 3 objects should be created, at least ...
1353 test 3 -le $(wc -l <actual)
1354 '
1355
1356 validate_store_type () {
1357 git -C dest count-objects -v >actual &&
1358 case "$store_type" in
1359 packed)
1360 grep "^count: 0$" actual ;;
1361 loose)
1362 grep "^packs: 0$" actual ;;
1363 esac || {
1364 echo "store_type is $store_type"
1365 cat actual
1366 false
1367 }
1368 }
1369
1370 test_unpack_limit () {
1371 store_type=$1
1372
1373 case "$store_type" in
1374 packed) fetch_limit=1 transfer_limit=10000 ;;
1375 loose) fetch_limit=10000 transfer_limit=1 ;;
1376 esac
1377
1378 test_expect_success "fetch trumps transfer limit" '
1379 rm -fr dest &&
1380 git --bare init dest &&
1381 git -C dest config fetch.unpacklimit $fetch_limit &&
1382 git -C dest config transfer.unpacklimit $transfer_limit &&
1383 git -C dest fetch .. onebranch &&
1384 validate_store_type
1385 '
1386 }
1387
1388 test_unpack_limit packed
1389 test_unpack_limit loose
1390
1391 setup_negotiation_tip () {
1392 SERVER="$1"
1393 URL="$2"
1394 USE_PROTOCOL_V2="$3"
1395
1396 rm -rf "$SERVER" client trace &&
1397 git init -b main "$SERVER" &&
1398 test_commit -C "$SERVER" alpha_1 &&
1399 test_commit -C "$SERVER" alpha_2 &&
1400 git -C "$SERVER" checkout --orphan beta &&
1401 test_commit -C "$SERVER" beta_1 &&
1402 test_commit -C "$SERVER" beta_2 &&
1403
1404 git clone "$URL" client &&
1405
1406 if test "$USE_PROTOCOL_V2" -eq 1
1407 then
1408 git -C "$SERVER" config protocol.version 2 &&
1409 git -C client config protocol.version 2
1410 fi &&
1411
1412 test_commit -C "$SERVER" beta_s &&
1413 git -C "$SERVER" checkout main &&
1414 test_commit -C "$SERVER" alpha_s &&
1415 git -C "$SERVER" tag -d alpha_1 alpha_2 beta_1 beta_2
1416 }
1417
1418 check_negotiation_tip () {
1419 # Ensure that {alpha,beta}_1 are sent as "have", but not {alpha_beta}_2
1420 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1421 grep "fetch> have $ALPHA_1" trace &&
1422 BETA_1=$(git -C client rev-parse beta_1) &&
1423 grep "fetch> have $BETA_1" trace &&
1424 ALPHA_2=$(git -C client rev-parse alpha_2) &&
1425 ! grep "fetch> have $ALPHA_2" trace &&
1426 BETA_2=$(git -C client rev-parse beta_2) &&
1427 ! grep "fetch> have $BETA_2" trace
1428 }
1429
1430 test_expect_success '--negotiation-tip limits "have" lines sent' '
1431 setup_negotiation_tip server server 0 &&
1432 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1433 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1434 origin alpha_s beta_s &&
1435 check_negotiation_tip
1436 '
1437
1438 test_expect_success '--negotiation-tip understands globs' '
1439 setup_negotiation_tip server server 0 &&
1440 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1441 --negotiation-tip=*_1 \
1442 origin alpha_s beta_s &&
1443 check_negotiation_tip
1444 '
1445
1446 test_expect_success '--negotiation-tip understands abbreviated SHA-1' '
1447 setup_negotiation_tip server server 0 &&
1448 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1449 --negotiation-tip=$(git -C client rev-parse --short alpha_1) \
1450 --negotiation-tip=$(git -C client rev-parse --short beta_1) \
1451 origin alpha_s beta_s &&
1452 check_negotiation_tip
1453 '
1454
1455 test_expect_success '--negotiation-tip rejects missing OIDs' '
1456 setup_negotiation_tip server server 0 &&
1457 test_must_fail git -C client fetch \
1458 --negotiation-tip=alpha_1 \
1459 --negotiation-tip=$(test_oid zero) \
1460 origin alpha_s beta_s 2>err &&
1461 cat >fatal-expect <<-EOF &&
1462 fatal: the object $(test_oid zero) does not exist
1463 EOF
1464 grep fatal: err >fatal-actual &&
1465 test_cmp fatal-expect fatal-actual
1466 '
1467
1468 test_expect_success '--negotiation-tip ignores missing refs and invalid hashes' '
1469 setup_negotiation_tip server server 0 &&
1470 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1471 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1472 --negotiation-tip=no-such-ref \
1473 --negotiation-tip=invalid-hash \
1474 origin alpha_s beta_s &&
1475 check_negotiation_tip
1476 '
1477
1478 test_expect_success '--negotiation-restrict limits "have" lines sent' '
1479 setup_negotiation_tip server server 0 &&
1480 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1481 --negotiation-restrict=alpha_1 --negotiation-restrict=beta_1 \
1482 origin alpha_s beta_s &&
1483 check_negotiation_tip
1484 '
1485
1486 test_expect_success '--negotiation-restrict understands globs' '
1487 setup_negotiation_tip server server 0 &&
1488 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1489 --negotiation-restrict=*_1 \
1490 origin alpha_s beta_s &&
1491 check_negotiation_tip
1492 '
1493
1494 test_expect_success '--negotiation-restrict and --negotiation-tip can be mixed' '
1495 setup_negotiation_tip server server 0 &&
1496 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1497 --negotiation-restrict=alpha_1 \
1498 --negotiation-tip=beta_1 \
1499 origin alpha_s beta_s &&
1500 check_negotiation_tip
1501 '
1502
1503 test_expect_success 'remote.<name>.negotiationRestrict used as default' '
1504 setup_negotiation_tip server server 0 &&
1505
1506 # test the reset of the list on an empty value
1507 git -C client config --add remote.origin.negotiationRestrict alpha_2 &&
1508 git -C client config --add remote.origin.negotiationRestrict "" &&
1509 git -C client config --add remote.origin.negotiationRestrict alpha_1 &&
1510 git -C client config --add remote.origin.negotiationRestrict beta_1 &&
1511 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1512 origin alpha_s beta_s &&
1513 check_negotiation_tip
1514 '
1515
1516 test_expect_success 'CLI --negotiation-restrict overrides remote config' '
1517 setup_negotiation_tip server server 0 &&
1518 git -C client config --add remote.origin.negotiationRestrict alpha_1 &&
1519 git -C client config --add remote.origin.negotiationRestrict beta_1 &&
1520 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1521 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1522 --negotiation-restrict=alpha_1 \
1523 origin alpha_s beta_s &&
1524 test_grep "fetch> have $ALPHA_1" trace &&
1525 BETA_1=$(git -C client rev-parse beta_1) &&
1526 test_grep ! "fetch> have $BETA_1" trace
1527 '
1528
1529 test_expect_success '--negotiation-include includes configured refs as haves' '
1530 test_when_finished rm -f trace &&
1531 setup_negotiation_tip server server 0 &&
1532
1533 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1534 --negotiation-restrict=alpha_1 \
1535 --negotiation-include=refs/tags/beta_1 \
1536 origin alpha_s beta_s &&
1537
1538 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1539 test_grep "fetch> have $ALPHA_1" trace &&
1540 BETA_1=$(git -C client rev-parse beta_1) &&
1541 test_grep "fetch> have $BETA_1" trace
1542 '
1543
1544 test_expect_success '--negotiation-include works with glob patterns' '
1545 test_when_finished rm -f trace &&
1546 setup_negotiation_tip server server 0 &&
1547
1548 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1549 --negotiation-restrict=alpha_1 \
1550 --negotiation-include="refs/tags/beta_*" \
1551 origin alpha_s beta_s &&
1552
1553 BETA_1=$(git -C client rev-parse beta_1) &&
1554 test_grep "fetch> have $BETA_1" trace &&
1555 BETA_2=$(git -C client rev-parse beta_2) &&
1556 test_grep "fetch> have $BETA_2" trace
1557 '
1558
1559 test_expect_success '--negotiation-include is additive with negotiation' '
1560 test_when_finished rm -f trace &&
1561 setup_negotiation_tip server server 0 &&
1562
1563 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1564 --negotiation-include=refs/tags/beta_1 \
1565 origin alpha_s beta_s &&
1566
1567 BETA_1=$(git -C client rev-parse beta_1) &&
1568 test_grep "fetch> have $BETA_1" trace
1569 '
1570
1571 test_expect_success '--negotiation-include ignores non-existent refs silently' '
1572 setup_negotiation_tip server server 0 &&
1573
1574 git -C client fetch --quiet \
1575 --negotiation-restrict=alpha_1 \
1576 --negotiation-include=refs/tags/nonexistent \
1577 origin alpha_s beta_s 2>err &&
1578 test_must_be_empty err
1579 '
1580
1581 test_expect_success '--negotiation-include avoids duplicates with negotiator' '
1582 test_when_finished rm -f trace &&
1583 setup_negotiation_tip server server 0 &&
1584
1585 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1586 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1587 --negotiation-restrict=alpha_1 \
1588 --negotiation-include=refs/tags/alpha_1 \
1589 origin alpha_s beta_s &&
1590
1591 test_grep "fetch> have $ALPHA_1" trace >matches &&
1592 test_line_count = 1 matches
1593 '
1594
1595 test_expect_success 'remote.<name>.negotiationInclude used as default for --negotiation-include' '
1596 test_when_finished rm -f trace &&
1597 setup_negotiation_tip server server 0 &&
1598
1599 # test the reset of the list on an empty value
1600 git -C client config --add remote.origin.negotiationInclude refs/tags/alpha_1 &&
1601 git -C client config --add remote.origin.negotiationInclude "" &&
1602 git -C client config --add remote.origin.negotiationInclude refs/tags/beta_1 &&
1603 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1604 --negotiation-restrict=beta_2 \
1605 origin alpha_s beta_s &&
1606
1607 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1608 test_grep ! "fetch> have $ALPHA_1" trace &&
1609 BETA_1=$(git -C client rev-parse beta_1) &&
1610 test_grep "fetch> have $BETA_1" trace
1611 '
1612
1613 test_expect_success 'remote.<name>.negotiationInclude works with glob patterns' '
1614 test_when_finished rm -f trace &&
1615 setup_negotiation_tip server server 0 &&
1616
1617 git -C client config --add remote.origin.negotiationInclude "refs/tags/beta_*" &&
1618 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1619 --negotiation-restrict=alpha_1 \
1620 origin alpha_s beta_s &&
1621
1622 BETA_1=$(git -C client rev-parse beta_1) &&
1623 test_grep "fetch> have $BETA_1" trace &&
1624 BETA_2=$(git -C client rev-parse beta_2) &&
1625 test_grep "fetch> have $BETA_2" trace
1626 '
1627
1628 test_expect_success 'CLI --negotiation-include overrides remote.<name>.negotiationInclude' '
1629 test_when_finished rm -f trace &&
1630 setup_negotiation_tip server server 0 &&
1631
1632 git -C client config --add remote.origin.negotiationInclude refs/tags/beta_2 &&
1633 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1634 --negotiation-restrict=alpha_1 \
1635 --negotiation-include=refs/tags/beta_1 \
1636 origin alpha_s beta_s &&
1637
1638 BETA_1=$(git -C client rev-parse beta_1) &&
1639 test_grep "fetch> have $BETA_1" trace &&
1640 BETA_2=$(git -C client rev-parse beta_2) &&
1641 test_grep ! "fetch> have $BETA_2" trace
1642 '
1643
1644 test_expect_success '--negotiation-include avoids duplicates with v0' '
1645 test_when_finished rm -f trace &&
1646 setup_negotiation_tip server server 0 &&
1647
1648 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1649 GIT_TRACE_PACKET="$(pwd)/trace" git -C client \
1650 -c protocol.version=0 fetch \
1651 --negotiation-restrict=alpha_1 \
1652 --negotiation-include=refs/tags/alpha_1 \
1653 origin alpha_s beta_s &&
1654
1655 test_grep "fetch> have $ALPHA_1" trace >matches &&
1656 test_line_count = 1 matches
1657 '
1658
1659 test_expect_success SYMLINKS 'clone does not get confused by a D/F conflict' '
1660 git init df-conflict &&
1661 (
1662 cd df-conflict &&
1663 ln -s .git a &&
1664 git add a &&
1665 test_tick &&
1666 git commit -m symlink &&
1667 test_commit a- &&
1668 rm a &&
1669 mkdir -p a/hooks &&
1670 write_script a/hooks/post-checkout <<-EOF &&
1671 echo WHOOPSIE >&2
1672 echo whoopsie >"$TRASH_DIRECTORY"/whoops
1673 EOF
1674 git add a/hooks/post-checkout &&
1675 test_tick &&
1676 git commit -m post-checkout
1677 ) &&
1678 git clone df-conflict clone 2>err &&
1679 test_grep ! WHOOPS err &&
1680 test_path_is_missing whoops
1681 '
1682
1683 test_expect_success CASE_INSENSITIVE_FS,REFFILES 'existing references in a case insensitive filesystem' '
1684 test_when_finished rm -rf case_insensitive &&
1685 (
1686 git init --bare case_insensitive &&
1687 cd case_insensitive &&
1688 git remote add origin -- ../case_sensitive &&
1689 test_must_fail git fetch -f origin "refs/heads/*:refs/heads/*" 2>err &&
1690 test_grep "You${SQ}re on a case-insensitive filesystem" err &&
1691 git rev-parse refs/heads/main >expect &&
1692 git rev-parse refs/heads/branch1 >actual &&
1693 test_cmp expect actual
1694 )
1695 '
1696
1697 test_expect_success REFFILES 'existing reference lock in repo' '
1698 test_when_finished rm -rf base repo &&
1699 (
1700 git init --ref-format=reftable base &&
1701 cd base &&
1702 echo >file update &&
1703 git add . &&
1704 git commit -m "updated" &&
1705 git branch -M main &&
1706
1707 git update-ref refs/heads/foo @ &&
1708 git update-ref refs/heads/branch @ &&
1709 cd .. &&
1710
1711 git init --ref-format=files --bare repo &&
1712 cd repo &&
1713 git remote add origin ../base &&
1714 touch refs/heads/foo.lock &&
1715 test_must_fail git fetch -f origin "refs/heads/*:refs/heads/*" 2>err &&
1716 test_grep -e "error: cannot lock ref ${SQ}refs/heads/foo${SQ}: Unable to create" -e "refs/heads/foo.lock${SQ}: File exists." err &&
1717 git rev-parse refs/heads/main >expect &&
1718 git rev-parse refs/heads/branch >actual &&
1719 test_cmp expect actual
1720 )
1721 '
1722
1723 test_expect_success CASE_INSENSITIVE_FS,REFFILES 'F/D conflict on case insensitive filesystem' '
1724 test_when_finished rm -rf case_insensitive &&
1725 (
1726 git init --bare case_insensitive &&
1727 cd case_insensitive &&
1728 git remote add origin -- ../case_sensitive_fd &&
1729 test_must_fail git fetch -f origin "refs/heads/*:refs/heads/*" 2>err &&
1730 test_grep "cannot process ${SQ}refs/remotes/origin/foo${SQ} and ${SQ}refs/remotes/origin/foo/bar${SQ} at the same time" err &&
1731 git rev-parse refs/heads/main >expect &&
1732 git rev-parse refs/heads/foo/bar >actual &&
1733 test_cmp expect actual
1734 )
1735 '
1736
1737 test_expect_success CASE_INSENSITIVE_FS,REFFILES 'D/F conflict on case insensitive filesystem' '
1738 test_when_finished rm -rf case_insensitive &&
1739 (
1740 git init --bare case_insensitive &&
1741 cd case_insensitive &&
1742 git remote add origin -- ../case_sensitive_df &&
1743 test_must_fail git fetch -f origin "refs/heads/*:refs/heads/*" 2>err &&
1744 test_grep "cannot lock ref ${SQ}refs/remotes/origin/foo${SQ}: there is a non-empty directory ${SQ}./refs/remotes/origin/foo${SQ} blocking reference ${SQ}refs/remotes/origin/foo${SQ}" err &&
1745 git rev-parse refs/heads/main >expect &&
1746 git rev-parse refs/heads/Foo/bar >actual &&
1747 test_cmp expect actual
1748 )
1749 '
1750
1751 test_expect_success REFFILES 'D/F conflict on case sensitive filesystem with lock' '
1752 test_when_finished rm -rf base repo &&
1753 (
1754 git init --ref-format=reftable base &&
1755 cd base &&
1756 echo >file update &&
1757 git add . &&
1758 git commit -m "updated" &&
1759 git branch -M main &&
1760
1761 git update-ref refs/heads/foo @ &&
1762 git update-ref refs/heads/branch @ &&
1763 cd .. &&
1764
1765 git init --ref-format=files --bare repo &&
1766 cd repo &&
1767 git remote add origin ../base &&
1768 mkdir refs/heads/foo &&
1769 touch refs/heads/foo/random.lock &&
1770 test_must_fail git fetch origin "refs/heads/*:refs/heads/*" 2>err &&
1771 test_grep "some local refs could not be updated; try running" err &&
1772 git rev-parse refs/heads/main >expect &&
1773 git rev-parse refs/heads/branch >actual &&
1774 test_cmp expect actual
1775 )
1776 '
1777
1778 test_expect_success 'fetch --tags fetches existing tags' '
1779 test_when_finished rm -rf base repo &&
1780
1781 git init base &&
1782 git -C base commit --allow-empty -m "empty-commit" &&
1783
1784 git clone --bare base repo &&
1785
1786 git -C base tag tag-1 &&
1787 git -C repo for-each-ref >out &&
1788 test_grep ! "tag-1" out &&
1789 git -C repo fetch --tags &&
1790 git -C repo for-each-ref >out &&
1791 test_grep "tag-1" out
1792 '
1793
1794 test_expect_success 'fetch --tags fetches non-conflicting tags' '
1795 test_when_finished rm -rf base repo &&
1796
1797 git init base &&
1798 git -C base commit --allow-empty -m "empty-commit" &&
1799 git -C base tag tag-1 &&
1800
1801 git clone --bare base repo &&
1802
1803 git -C base tag tag-2 &&
1804 git -C repo for-each-ref >out &&
1805 test_grep ! "tag-2" out &&
1806
1807 git -C base commit --allow-empty -m "second empty-commit" &&
1808 git -C base tag -f tag-1 &&
1809
1810 test_must_fail git -C repo fetch --tags 2>out &&
1811 test_grep "tag-1 (would clobber existing tag)" out &&
1812 git -C repo for-each-ref >out &&
1813 test_grep "tag-2" out
1814 '
1815
1816 test_expect_success "backfill tags when providing a refspec" '
1817 test_when_finished rm -rf source target &&
1818
1819 git init source &&
1820 git -C source commit --allow-empty --message common &&
1821 git clone file://"$(pwd)"/source target &&
1822 (
1823 cd source &&
1824 test_commit history &&
1825 test_commit fetch-me
1826 ) &&
1827
1828 # The "history" tag is backfilled even though we requested
1829 # to only fetch HEAD
1830 git -C target fetch origin HEAD:branch &&
1831 git -C target tag -l >actual &&
1832 cat >expect <<-\EOF &&
1833 fetch-me
1834 history
1835 EOF
1836 test_cmp expect actual
1837 '
1838
1839 test_expect_success REFFILES "FETCH_HEAD is updated even if ref updates fail" '
1840 test_when_finished rm -rf base repo &&
1841
1842 git init base &&
1843 (
1844 cd base &&
1845 test_commit "updated" &&
1846
1847 git update-ref refs/heads/foo @ &&
1848 git update-ref refs/heads/branch @
1849 ) &&
1850
1851 git init --bare repo &&
1852 (
1853 cd repo &&
1854 rm -f FETCH_HEAD &&
1855 git remote add origin ../base &&
1856 >refs/heads/foo.lock &&
1857 test_must_fail git fetch -f origin "refs/heads/*:refs/heads/*" 2>err &&
1858 test_grep -e "error: cannot lock ref ${SQ}refs/heads/foo${SQ}: Unable to create" -e "refs/heads/foo.lock${SQ}: File exists." err &&
1859 test_grep "branch ${SQ}branch${SQ} of ../base" FETCH_HEAD &&
1860 test_grep "branch ${SQ}foo${SQ} of ../base" FETCH_HEAD
1861 )
1862 '
1863
1864 test_expect_success "upstream tracking info is added with --set-upstream" '
1865 test_when_finished rm -rf base repo &&
1866
1867 git init --initial-branch=main base &&
1868 test_commit -C base "updated" &&
1869
1870 git init --bare --initial-branch=main repo &&
1871 (
1872 cd repo &&
1873 git remote add origin ../base &&
1874 git fetch origin --set-upstream main &&
1875 git config get branch.main.remote >actual &&
1876 echo "origin" >expect &&
1877 test_cmp expect actual
1878 )
1879 '
1880
1881 test_expect_success REFFILES "upstream tracking info is added even with conflicts" '
1882 test_when_finished rm -rf base repo &&
1883
1884 git init --initial-branch=main base &&
1885 test_commit -C base "updated" &&
1886
1887 git init --bare --initial-branch=main repo &&
1888 (
1889 cd repo &&
1890 git remote add origin ../base &&
1891 test_must_fail git config get branch.main.remote &&
1892
1893 mkdir -p refs/remotes/origin &&
1894 >refs/remotes/origin/main.lock &&
1895 test_must_fail git fetch origin --set-upstream main &&
1896 git config get branch.main.remote >actual &&
1897 echo "origin" >expect &&
1898 test_cmp expect actual
1899 )
1900 '
1901
1902 test_expect_success REFFILES "HEAD is updated even with conflicts" '
1903 test_when_finished rm -rf base repo &&
1904
1905 git init base &&
1906 (
1907 cd base &&
1908 test_commit "updated" &&
1909
1910 git update-ref refs/heads/foo @ &&
1911 git update-ref refs/heads/branch @
1912 ) &&
1913
1914 git init --bare repo &&
1915 (
1916 cd repo &&
1917 git remote add origin ../base &&
1918
1919 test_path_is_missing refs/remotes/origin/HEAD &&
1920 mkdir -p refs/remotes/origin &&
1921 >refs/remotes/origin/branch.lock &&
1922 test_must_fail git fetch origin &&
1923 test -f refs/remotes/origin/HEAD
1924 )
1925 '
1926
1927 . "$TEST_DIRECTORY"/lib-httpd.sh
1928 start_httpd
1929
1930 test_expect_success '--negotiation-tip limits "have" lines sent with HTTP protocol v2' '
1931 setup_negotiation_tip "$HTTPD_DOCUMENT_ROOT_PATH/server" \
1932 "$HTTPD_URL/smart/server" 1 &&
1933 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1934 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1935 origin alpha_s beta_s &&
1936 check_negotiation_tip
1937 '
1938
1939 # DO NOT add non-httpd-specific tests here, because the last part of this
1940 # test script is only executed when httpd is available and enabled.
1941
1942 test_done