The eleventh batch
[alt-git.git] / t / t5510-fetch.sh
blob0890b9f61c56cc4b936fe1206c0378a8d5b15585
1 #!/bin/sh
2 # Copyright (c) 2006, Junio C Hamano.
4 test_description='Per branch config variables affects "git fetch".
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10 . "$TEST_DIRECTORY"/lib-bundle.sh
12 D=$(pwd)
14 test_expect_success setup '
15 echo >file original &&
16 git add file &&
17 git commit -a -m original &&
18 git branch -M main
21 test_expect_success "clone and setup child repos" '
22 git clone . one &&
24 cd one &&
25 echo >file updated by one &&
26 git commit -a -m "updated by one"
27 ) &&
28 git clone . two &&
30 cd two &&
31 git config branch.main.remote one &&
32 git config remote.one.url ../one/.git/ &&
33 git config remote.one.fetch refs/heads/main:refs/heads/one
34 ) &&
35 git clone . three &&
37 cd three &&
38 git config branch.main.remote two &&
39 git config branch.main.merge refs/heads/one &&
40 mkdir -p .git/remotes &&
41 cat >.git/remotes/two <<-\EOF
42 URL: ../two/.git/
43 Pull: refs/heads/main:refs/heads/two
44 Pull: refs/heads/one:refs/heads/one
45 EOF
46 ) &&
47 git clone . bundle &&
48 git clone . seven
51 test_expect_success "fetch test" '
52 cd "$D" &&
53 echo >file updated by origin &&
54 git commit -a -m "updated by origin" &&
55 cd two &&
56 git fetch &&
57 git rev-parse --verify refs/heads/one &&
58 mine=$(git rev-parse refs/heads/one) &&
59 his=$(cd ../one && git rev-parse refs/heads/main) &&
60 test "z$mine" = "z$his"
63 test_expect_success "fetch test for-merge" '
64 cd "$D" &&
65 cd three &&
66 git fetch &&
67 git rev-parse --verify refs/heads/two &&
68 git rev-parse --verify refs/heads/one &&
69 main_in_two=$(cd ../two && git rev-parse main) &&
70 one_in_two=$(cd ../two && git rev-parse one) &&
72 echo "$one_in_two " &&
73 echo "$main_in_two not-for-merge"
74 } >expected &&
75 cut -f -2 .git/FETCH_HEAD >actual &&
76 test_cmp expected actual'
78 test_expect_success 'fetch --prune on its own works as expected' '
79 cd "$D" &&
80 git clone . prune &&
81 cd prune &&
82 git update-ref refs/remotes/origin/extrabranch main &&
84 git fetch --prune origin &&
85 test_must_fail git rev-parse origin/extrabranch
88 test_expect_success 'fetch --prune with a branch name keeps branches' '
89 cd "$D" &&
90 git clone . prune-branch &&
91 cd prune-branch &&
92 git update-ref refs/remotes/origin/extrabranch main &&
94 git fetch --prune origin main &&
95 git rev-parse origin/extrabranch
98 test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
99 cd "$D" &&
100 git clone . prune-namespace &&
101 cd prune-namespace &&
103 git fetch --prune origin refs/heads/a/*:refs/remotes/origin/a/* &&
104 git rev-parse origin/main
107 test_expect_success 'fetch --prune handles overlapping refspecs' '
108 cd "$D" &&
109 git update-ref refs/pull/42/head main &&
110 git clone . prune-overlapping &&
111 cd prune-overlapping &&
112 git config --add remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
114 git fetch --prune origin &&
115 git rev-parse origin/main &&
116 git rev-parse origin/pr/42 &&
118 git config --unset-all remote.origin.fetch &&
119 git config remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
120 git config --add remote.origin.fetch refs/heads/*:refs/remotes/origin/* &&
122 git fetch --prune origin &&
123 git rev-parse origin/main &&
124 git rev-parse origin/pr/42
127 test_expect_success 'fetch --prune --tags prunes branches but not tags' '
128 cd "$D" &&
129 git clone . prune-tags &&
130 cd prune-tags &&
131 git tag sometag main &&
132 # Create what looks like a remote-tracking branch from an earlier
133 # fetch that has since been deleted from the remote:
134 git update-ref refs/remotes/origin/fake-remote main &&
136 git fetch --prune --tags origin &&
137 git rev-parse origin/main &&
138 test_must_fail git rev-parse origin/fake-remote &&
139 git rev-parse sometag
142 test_expect_success 'fetch --prune --tags with branch does not prune other things' '
143 cd "$D" &&
144 git clone . prune-tags-branch &&
145 cd prune-tags-branch &&
146 git tag sometag main &&
147 git update-ref refs/remotes/origin/extrabranch main &&
149 git fetch --prune --tags origin main &&
150 git rev-parse origin/extrabranch &&
151 git rev-parse sometag
154 test_expect_success 'fetch --prune --tags with refspec prunes based on refspec' '
155 cd "$D" &&
156 git clone . prune-tags-refspec &&
157 cd prune-tags-refspec &&
158 git tag sometag main &&
159 git update-ref refs/remotes/origin/foo/otherbranch main &&
160 git update-ref refs/remotes/origin/extrabranch main &&
162 git fetch --prune --tags origin refs/heads/foo/*:refs/remotes/origin/foo/* &&
163 test_must_fail git rev-parse refs/remotes/origin/foo/otherbranch &&
164 git rev-parse origin/extrabranch &&
165 git rev-parse sometag
168 test_expect_success REFFILES 'fetch --prune fails to delete branches' '
169 cd "$D" &&
170 git clone . prune-fail &&
171 cd prune-fail &&
172 git update-ref refs/remotes/origin/extrabranch main &&
173 git pack-refs --all &&
174 : this will prevent --prune from locking packed-refs for deleting refs, but adding loose refs still succeeds &&
175 >.git/packed-refs.new &&
177 test_must_fail git fetch --prune origin
180 test_expect_success 'fetch --atomic works with a single branch' '
181 test_when_finished "rm -rf \"$D\"/atomic" &&
183 cd "$D" &&
184 git clone . atomic &&
185 git branch atomic-branch &&
186 oid=$(git rev-parse atomic-branch) &&
187 echo "$oid" >expected &&
189 git -C atomic fetch --atomic origin &&
190 git -C atomic rev-parse origin/atomic-branch >actual &&
191 test_cmp expected actual &&
192 test $oid = "$(git -C atomic rev-parse --verify FETCH_HEAD)"
195 test_expect_success 'fetch --atomic works with multiple branches' '
196 test_when_finished "rm -rf \"$D\"/atomic" &&
198 cd "$D" &&
199 git clone . atomic &&
200 git branch atomic-branch-1 &&
201 git branch atomic-branch-2 &&
202 git branch atomic-branch-3 &&
203 git rev-parse refs/heads/atomic-branch-1 refs/heads/atomic-branch-2 refs/heads/atomic-branch-3 >actual &&
205 git -C atomic fetch --atomic origin &&
206 git -C atomic rev-parse refs/remotes/origin/atomic-branch-1 refs/remotes/origin/atomic-branch-2 refs/remotes/origin/atomic-branch-3 >expected &&
207 test_cmp expected actual
210 test_expect_success 'fetch --atomic works with mixed branches and tags' '
211 test_when_finished "rm -rf \"$D\"/atomic" &&
213 cd "$D" &&
214 git clone . atomic &&
215 git branch atomic-mixed-branch &&
216 git tag atomic-mixed-tag &&
217 git rev-parse refs/heads/atomic-mixed-branch refs/tags/atomic-mixed-tag >actual &&
219 git -C atomic fetch --tags --atomic origin &&
220 git -C atomic rev-parse refs/remotes/origin/atomic-mixed-branch refs/tags/atomic-mixed-tag >expected &&
221 test_cmp expected actual
224 test_expect_success 'fetch --atomic prunes references' '
225 test_when_finished "rm -rf \"$D\"/atomic" &&
227 cd "$D" &&
228 git branch atomic-prune-delete &&
229 git clone . atomic &&
230 git branch --delete atomic-prune-delete &&
231 git branch atomic-prune-create &&
232 git rev-parse refs/heads/atomic-prune-create >actual &&
234 git -C atomic fetch --prune --atomic origin &&
235 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-prune-delete &&
236 git -C atomic rev-parse refs/remotes/origin/atomic-prune-create >expected &&
237 test_cmp expected actual
240 test_expect_success 'fetch --atomic aborts with non-fast-forward update' '
241 test_when_finished "rm -rf \"$D\"/atomic" &&
243 cd "$D" &&
244 git branch atomic-non-ff &&
245 git clone . atomic &&
246 git rev-parse HEAD >actual &&
248 git branch atomic-new-branch &&
249 parent_commit=$(git rev-parse atomic-non-ff~) &&
250 git update-ref refs/heads/atomic-non-ff $parent_commit &&
252 test_must_fail git -C atomic fetch --atomic origin refs/heads/*:refs/remotes/origin/* &&
253 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-new-branch &&
254 git -C atomic rev-parse refs/remotes/origin/atomic-non-ff >expected &&
255 test_cmp expected actual &&
256 test_must_be_empty atomic/.git/FETCH_HEAD
259 test_expect_success 'fetch --atomic executes a single reference transaction only' '
260 test_when_finished "rm -rf \"$D\"/atomic" &&
262 cd "$D" &&
263 git clone . atomic &&
264 git branch atomic-hooks-1 &&
265 git branch atomic-hooks-2 &&
266 head_oid=$(git rev-parse HEAD) &&
268 cat >expected <<-EOF &&
269 prepared
270 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
271 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
272 committed
273 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
274 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
277 rm -f atomic/actual &&
278 test_hook -C atomic reference-transaction <<-\EOF &&
279 ( echo "$*" && cat ) >>actual
282 git -C atomic fetch --atomic origin &&
283 test_cmp expected atomic/actual
286 test_expect_success 'fetch --atomic aborts all reference updates if hook aborts' '
287 test_when_finished "rm -rf \"$D\"/atomic" &&
289 cd "$D" &&
290 git clone . atomic &&
291 git branch atomic-hooks-abort-1 &&
292 git branch atomic-hooks-abort-2 &&
293 git branch atomic-hooks-abort-3 &&
294 git tag atomic-hooks-abort &&
295 head_oid=$(git rev-parse HEAD) &&
297 cat >expected <<-EOF &&
298 prepared
299 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
300 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
301 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
302 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
303 aborted
304 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
305 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
306 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
307 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
310 rm -f atomic/actual &&
311 test_hook -C atomic/.git reference-transaction <<-\EOF &&
312 ( echo "$*" && cat ) >>actual
313 exit 1
316 git -C atomic for-each-ref >expected-refs &&
317 test_must_fail git -C atomic fetch --tags --atomic origin &&
318 git -C atomic for-each-ref >actual-refs &&
319 test_cmp expected-refs actual-refs &&
320 test_must_be_empty atomic/.git/FETCH_HEAD
323 test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' '
324 test_when_finished "rm -rf \"$D\"/atomic" &&
326 cd "$D" &&
327 git clone . atomic &&
328 oid=$(git rev-parse HEAD) &&
330 git branch atomic-fetch-head-1 &&
331 git -C atomic fetch --atomic origin atomic-fetch-head-1 &&
332 test_line_count = 1 atomic/.git/FETCH_HEAD &&
334 git branch atomic-fetch-head-2 &&
335 git -C atomic fetch --atomic --append origin atomic-fetch-head-2 &&
336 test_line_count = 2 atomic/.git/FETCH_HEAD &&
337 cp atomic/.git/FETCH_HEAD expected &&
339 test_hook -C atomic reference-transaction <<-\EOF &&
340 exit 1
343 git branch atomic-fetch-head-3 &&
344 test_must_fail git -C atomic fetch --atomic --append origin atomic-fetch-head-3 &&
345 test_cmp expected atomic/.git/FETCH_HEAD
348 test_expect_success '--refmap="" ignores configured refspec' '
349 cd "$TRASH_DIRECTORY" &&
350 git clone "$D" remote-refs &&
351 git -C remote-refs rev-parse remotes/origin/main >old &&
352 git -C remote-refs update-ref refs/remotes/origin/main main~1 &&
353 git -C remote-refs rev-parse remotes/origin/main >new &&
354 git -C remote-refs fetch --refmap= origin "+refs/heads/*:refs/hidden/origin/*" &&
355 git -C remote-refs rev-parse remotes/origin/main >actual &&
356 test_cmp new actual &&
357 git -C remote-refs fetch origin &&
358 git -C remote-refs rev-parse remotes/origin/main >actual &&
359 test_cmp old actual
362 test_expect_success '--refmap="" and --prune' '
363 git -C remote-refs update-ref refs/remotes/origin/foo/otherbranch main &&
364 git -C remote-refs update-ref refs/hidden/foo/otherbranch main &&
365 git -C remote-refs fetch --prune --refmap="" origin +refs/heads/*:refs/hidden/* &&
366 git -C remote-refs rev-parse remotes/origin/foo/otherbranch &&
367 test_must_fail git -C remote-refs rev-parse refs/hidden/foo/otherbranch &&
368 git -C remote-refs fetch --prune origin &&
369 test_must_fail git -C remote-refs rev-parse remotes/origin/foo/otherbranch
372 test_expect_success 'fetch tags when there is no tags' '
374 cd "$D" &&
376 mkdir notags &&
377 cd notags &&
378 git init &&
380 git fetch -t ..
384 test_expect_success 'fetch following tags' '
386 cd "$D" &&
387 git tag -a -m "annotated" anno HEAD &&
388 git tag light HEAD &&
390 mkdir four &&
391 cd four &&
392 git init &&
394 git fetch .. :track &&
395 git show-ref --verify refs/tags/anno &&
396 git show-ref --verify refs/tags/light
400 test_expect_success 'fetch uses remote ref names to describe new refs' '
401 cd "$D" &&
402 git init descriptive &&
404 cd descriptive &&
405 git config remote.o.url .. &&
406 git config remote.o.fetch "refs/heads/*:refs/crazyheads/*" &&
407 git config --add remote.o.fetch "refs/others/*:refs/heads/*" &&
408 git fetch o
409 ) &&
410 git tag -a -m "Descriptive tag" descriptive-tag &&
411 git branch descriptive-branch &&
412 git checkout descriptive-branch &&
413 echo "Nuts" >crazy &&
414 git add crazy &&
415 git commit -a -m "descriptive commit" &&
416 git update-ref refs/others/crazy HEAD &&
418 cd descriptive &&
419 git fetch o 2>actual &&
420 test_grep "new branch.* -> refs/crazyheads/descriptive-branch$" actual &&
421 test_grep "new tag.* -> descriptive-tag$" actual &&
422 test_grep "new ref.* -> crazy$" actual
423 ) &&
424 git checkout main
427 test_expect_success 'fetch must not resolve short tag name' '
429 cd "$D" &&
431 mkdir five &&
432 cd five &&
433 git init &&
435 test_must_fail git fetch .. anno:five
439 test_expect_success 'fetch can now resolve short remote name' '
441 cd "$D" &&
442 git update-ref refs/remotes/six/HEAD HEAD &&
444 mkdir six &&
445 cd six &&
446 git init &&
448 git fetch .. six:six
451 test_expect_success 'create bundle 1' '
452 cd "$D" &&
453 echo >file updated again by origin &&
454 git commit -a -m "tip" &&
455 git bundle create --version=3 bundle1 main^..main
458 test_expect_success 'header of bundle looks right' '
459 cat >expect <<-EOF &&
460 # v3 git bundle
461 @object-format=$(test_oid algo)
462 -OID updated by origin
463 OID refs/heads/main
466 sed -e "s/$OID_REGEX/OID/g" -e "5q" "$D"/bundle1 >actual &&
467 test_cmp expect actual
470 test_expect_success 'create bundle 2' '
471 cd "$D" &&
472 git bundle create bundle2 main~2..main
475 test_expect_success 'unbundle 1' '
476 cd "$D/bundle" &&
477 git checkout -b some-branch &&
478 test_must_fail git fetch "$D/bundle1" main:main
482 test_expect_success 'bundle 1 has only 3 files ' '
483 cd "$D" &&
484 test_bundle_object_count bundle1 3
487 test_expect_success 'unbundle 2' '
488 cd "$D/bundle" &&
489 git fetch ../bundle2 main:main &&
490 test "tip" = "$(git log -1 --pretty=oneline main | cut -d" " -f2)"
493 test_expect_success 'bundle does not prerequisite objects' '
494 cd "$D" &&
495 touch file2 &&
496 git add file2 &&
497 git commit -m add.file2 file2 &&
498 git bundle create bundle3 -1 HEAD &&
499 test_bundle_object_count bundle3 3
502 test_expect_success 'bundle should be able to create a full history' '
504 cd "$D" &&
505 git tag -a -m "1.0" v1.0 main &&
506 git bundle create bundle4 v1.0
510 test_expect_success 'fetch with a non-applying branch.<name>.merge' '
511 git config branch.main.remote yeti &&
512 git config branch.main.merge refs/heads/bigfoot &&
513 git config remote.blub.url one &&
514 git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
515 git fetch blub
518 # URL supplied to fetch does not match the url of the configured branch's remote
519 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
520 one_head=$(cd one && git rev-parse HEAD) &&
521 this_head=$(git rev-parse HEAD) &&
522 rm .git/FETCH_HEAD &&
523 git fetch one &&
524 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
525 test $this_head = "$(git rev-parse --verify HEAD)"
528 # URL supplied to fetch matches the url of the configured branch's remote and
529 # the merge spec matches the branch the remote HEAD points to
530 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
531 one_ref=$(cd one && git symbolic-ref HEAD) &&
532 git config branch.main.remote blub &&
533 git config branch.main.merge "$one_ref" &&
534 rm .git/FETCH_HEAD &&
535 git fetch one &&
536 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
537 test $this_head = "$(git rev-parse --verify HEAD)"
540 # URL supplied to fetch matches the url of the configured branch's remote, but
541 # the merge spec does not match the branch the remote HEAD points to
542 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
543 git config branch.main.merge "${one_ref}_not" &&
544 rm .git/FETCH_HEAD &&
545 git fetch one &&
546 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
547 test $this_head = "$(git rev-parse --verify HEAD)"
550 # the strange name is: a\!'b
551 test_expect_success 'quoting of a strangely named repo' '
552 test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
553 grep "fatal: '\''a\\\\!'\''b'\''" result
556 test_expect_success 'bundle should record HEAD correctly' '
558 cd "$D" &&
559 git bundle create bundle5 HEAD main &&
560 git bundle list-heads bundle5 >actual &&
561 for h in HEAD refs/heads/main
563 echo "$(git rev-parse --verify $h) $h" || return 1
564 done >expect &&
565 test_cmp expect actual
569 test_expect_success 'mark initial state of origin/main' '
571 cd three &&
572 git tag base-origin-main refs/remotes/origin/main
576 test_expect_success 'explicit fetch should update tracking' '
578 cd "$D" &&
579 git branch -f side &&
581 cd three &&
582 git update-ref refs/remotes/origin/main base-origin-main &&
583 o=$(git rev-parse --verify refs/remotes/origin/main) &&
584 git fetch origin main &&
585 n=$(git rev-parse --verify refs/remotes/origin/main) &&
586 test "$o" != "$n" &&
587 test_must_fail git rev-parse --verify refs/remotes/origin/side
591 test_expect_success 'explicit pull should update tracking' '
593 cd "$D" &&
594 git branch -f side &&
596 cd three &&
597 git update-ref refs/remotes/origin/main base-origin-main &&
598 o=$(git rev-parse --verify refs/remotes/origin/main) &&
599 git pull origin main &&
600 n=$(git rev-parse --verify refs/remotes/origin/main) &&
601 test "$o" != "$n" &&
602 test_must_fail git rev-parse --verify refs/remotes/origin/side
606 test_expect_success 'explicit --refmap is allowed only with command-line refspec' '
607 cd "$D" &&
609 cd three &&
610 test_must_fail git fetch --refmap="*:refs/remotes/none/*"
614 test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
615 cd "$D" &&
616 git branch -f side &&
618 cd three &&
619 git update-ref refs/remotes/origin/main base-origin-main &&
620 o=$(git rev-parse --verify refs/remotes/origin/main) &&
621 git fetch --refmap="refs/heads/*:refs/remotes/other/*" origin main &&
622 n=$(git rev-parse --verify refs/remotes/origin/main) &&
623 test "$o" = "$n" &&
624 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
625 git rev-parse --verify refs/remotes/other/main
629 test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
630 cd "$D" &&
631 git branch -f side &&
633 cd three &&
634 git update-ref refs/remotes/origin/main base-origin-main &&
635 o=$(git rev-parse --verify refs/remotes/origin/main) &&
636 git fetch --refmap="" origin main &&
637 n=$(git rev-parse --verify refs/remotes/origin/main) &&
638 test "$o" = "$n" &&
639 test_must_fail git rev-parse --verify refs/remotes/origin/side
643 test_expect_success 'configured fetch updates tracking' '
645 cd "$D" &&
646 git branch -f side &&
648 cd three &&
649 git update-ref refs/remotes/origin/main base-origin-main &&
650 o=$(git rev-parse --verify refs/remotes/origin/main) &&
651 git fetch origin &&
652 n=$(git rev-parse --verify refs/remotes/origin/main) &&
653 test "$o" != "$n" &&
654 git rev-parse --verify refs/remotes/origin/side
658 test_expect_success 'non-matching refspecs do not confuse tracking update' '
659 cd "$D" &&
660 git update-ref refs/odd/location HEAD &&
662 cd three &&
663 git update-ref refs/remotes/origin/main base-origin-main &&
664 git config --add remote.origin.fetch \
665 refs/odd/location:refs/remotes/origin/odd &&
666 o=$(git rev-parse --verify refs/remotes/origin/main) &&
667 git fetch origin main &&
668 n=$(git rev-parse --verify refs/remotes/origin/main) &&
669 test "$o" != "$n" &&
670 test_must_fail git rev-parse --verify refs/remotes/origin/odd
674 test_expect_success 'pushing nonexistent branch by mistake should not segv' '
676 cd "$D" &&
677 test_must_fail git push seven no:no
681 test_expect_success 'auto tag following fetches minimum' '
683 cd "$D" &&
684 git clone .git follow &&
685 git checkout HEAD^0 &&
687 for i in 1 2 3 4 5 6 7
689 echo $i >>file &&
690 git commit -m $i -a &&
691 git tag -a -m $i excess-$i || exit 1
692 done
693 ) &&
694 git checkout main &&
696 cd follow &&
697 git fetch
701 test_expect_success 'refuse to fetch into the current branch' '
703 test_must_fail git fetch . side:main
707 test_expect_success 'fetch into the current branch with --update-head-ok' '
709 git fetch --update-head-ok . side:main
713 test_expect_success 'fetch --dry-run does not touch FETCH_HEAD, but still prints what would be written' '
714 rm -f .git/FETCH_HEAD err &&
715 git fetch --dry-run . 2>err &&
716 ! test -f .git/FETCH_HEAD &&
717 grep FETCH_HEAD err
720 test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD, and does not print what would be written' '
721 rm -f .git/FETCH_HEAD err &&
722 git fetch --no-write-fetch-head . 2>err &&
723 ! test -f .git/FETCH_HEAD &&
724 ! grep FETCH_HEAD err
727 test_expect_success '--write-fetch-head gets defeated by --dry-run' '
728 rm -f .git/FETCH_HEAD &&
729 git fetch --dry-run --write-fetch-head . &&
730 ! test -f .git/FETCH_HEAD
733 test_expect_success "should be able to fetch with duplicate refspecs" '
734 mkdir dups &&
736 cd dups &&
737 git init &&
738 git config branch.main.remote three &&
739 git config remote.three.url ../three/.git &&
740 git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
741 git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
742 git fetch three
746 test_expect_success 'LHS of refspec follows ref disambiguation rules' '
747 mkdir lhs-ambiguous &&
749 cd lhs-ambiguous &&
750 git init server &&
751 test_commit -C server unwanted &&
752 test_commit -C server wanted &&
754 git init client &&
756 # Check a name coming after "refs" alphabetically ...
757 git -C server update-ref refs/heads/s wanted &&
758 git -C server update-ref refs/heads/refs/heads/s unwanted &&
759 git -C client fetch ../server +refs/heads/s:refs/heads/checkthis &&
760 git -C server rev-parse wanted >expect &&
761 git -C client rev-parse checkthis >actual &&
762 test_cmp expect actual &&
764 # ... and one before.
765 git -C server update-ref refs/heads/q wanted &&
766 git -C server update-ref refs/heads/refs/heads/q unwanted &&
767 git -C client fetch ../server +refs/heads/q:refs/heads/checkthis &&
768 git -C server rev-parse wanted >expect &&
769 git -C client rev-parse checkthis >actual &&
770 test_cmp expect actual &&
772 # Tags are preferred over branches like refs/{heads,tags}/*
773 git -C server update-ref refs/tags/t wanted &&
774 git -C server update-ref refs/heads/t unwanted &&
775 git -C client fetch ../server +t:refs/heads/checkthis &&
776 git -C server rev-parse wanted >expect &&
777 git -C client rev-parse checkthis >actual
781 test_expect_success 'fetch.writeCommitGraph' '
782 git clone three write &&
784 cd three &&
785 test_commit new
786 ) &&
788 cd write &&
789 git -c fetch.writeCommitGraph fetch origin &&
790 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
794 test_expect_success 'fetch.writeCommitGraph with submodules' '
795 test_config_global protocol.file.allow always &&
796 git clone dups super &&
798 cd super &&
799 git submodule add "file://$TRASH_DIRECTORY/three" &&
800 git commit -m "add submodule"
801 ) &&
802 git clone "super" super-clone &&
804 cd super-clone &&
805 rm -rf .git/objects/info &&
806 git -c fetch.writeCommitGraph=true fetch origin &&
807 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain &&
808 git -c fetch.writeCommitGraph=true fetch --recurse-submodules origin
812 # fetches from first configured url
813 test_expect_success 'fetch from multiple configured URLs in single remote' '
814 git init url1 &&
815 git remote add multipleurls url1 &&
816 git remote set-url --add multipleurls url2 &&
817 git fetch multipleurls
820 # configured prune tests
822 set_config_tristate () {
823 # var=$1 val=$2
824 case "$2" in
825 unset)
826 test_unconfig "$1"
829 git config "$1" "$2"
830 key=$(echo $1 | sed -e 's/^remote\.origin/fetch/')
831 git_fetch_c="$git_fetch_c -c $key=$2"
833 esac
836 test_configured_prune () {
837 test_configured_prune_type "$@" "name"
838 test_configured_prune_type "$@" "link"
841 test_configured_prune_type () {
842 fetch_prune=$1
843 remote_origin_prune=$2
844 fetch_prune_tags=$3
845 remote_origin_prune_tags=$4
846 expected_branch=$5
847 expected_tag=$6
848 cmdline=$7
849 mode=$8
851 if test -z "$cmdline_setup"
852 then
853 test_expect_success 'setup cmdline_setup variable for subsequent test' '
854 remote_url="file://$(git -C one config remote.origin.url)" &&
855 remote_fetch="$(git -C one config remote.origin.fetch)" &&
856 cmdline_setup="\"$remote_url\" \"$remote_fetch\""
860 if test "$mode" = 'link'
861 then
862 new_cmdline=""
864 if test "$cmdline" = ""
865 then
866 new_cmdline=$cmdline_setup
867 else
868 new_cmdline=$(perl -e '
869 my ($cmdline, $url) = @ARGV;
870 $cmdline =~ s[origin(?!/)][quotemeta($url)]ge;
871 print $cmdline;
872 ' -- "$cmdline" "$remote_url")
875 if test "$fetch_prune_tags" = 'true' ||
876 test "$remote_origin_prune_tags" = 'true'
877 then
878 if ! printf '%s' "$cmdline\n" | grep -q refs/remotes/origin/
879 then
880 new_cmdline="$new_cmdline refs/tags/*:refs/tags/*"
884 cmdline="$new_cmdline"
887 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" '
888 # make sure a newbranch is there in . and also in one
889 git branch -f newbranch &&
890 git tag -f newtag &&
892 cd one &&
893 test_unconfig fetch.prune &&
894 test_unconfig fetch.pruneTags &&
895 test_unconfig remote.origin.prune &&
896 test_unconfig remote.origin.pruneTags &&
897 git fetch '"$cmdline_setup"' &&
898 git rev-parse --verify refs/remotes/origin/newbranch &&
899 git rev-parse --verify refs/tags/newtag
900 ) &&
902 # now remove them
903 git branch -d newbranch &&
904 git tag -d newtag &&
906 # then test
908 cd one &&
909 git_fetch_c="" &&
910 set_config_tristate fetch.prune $fetch_prune &&
911 set_config_tristate fetch.pruneTags $fetch_prune_tags &&
912 set_config_tristate remote.origin.prune $remote_origin_prune &&
913 set_config_tristate remote.origin.pruneTags $remote_origin_prune_tags &&
915 if test "$mode" != "link"
916 then
917 git_fetch_c=""
918 fi &&
919 git$git_fetch_c fetch '"$cmdline"' &&
920 case "$expected_branch" in
921 pruned)
922 test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
924 kept)
925 git rev-parse --verify refs/remotes/origin/newbranch
927 esac &&
928 case "$expected_tag" in
929 pruned)
930 test_must_fail git rev-parse --verify refs/tags/newtag
932 kept)
933 git rev-parse --verify refs/tags/newtag
935 esac
940 # $1 config: fetch.prune
941 # $2 config: remote.<name>.prune
942 # $3 config: fetch.pruneTags
943 # $4 config: remote.<name>.pruneTags
944 # $5 expect: branch to be pruned?
945 # $6 expect: tag to be pruned?
946 # $7 git-fetch $cmdline:
948 # $1 $2 $3 $4 $5 $6 $7
949 test_configured_prune unset unset unset unset kept kept ""
950 test_configured_prune unset unset unset unset kept kept "--no-prune"
951 test_configured_prune unset unset unset unset pruned kept "--prune"
952 test_configured_prune unset unset unset unset kept pruned \
953 "--prune origin refs/tags/*:refs/tags/*"
954 test_configured_prune unset unset unset unset pruned pruned \
955 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
957 test_configured_prune false unset unset unset kept kept ""
958 test_configured_prune false unset unset unset kept kept "--no-prune"
959 test_configured_prune false unset unset unset pruned kept "--prune"
961 test_configured_prune true unset unset unset pruned kept ""
962 test_configured_prune true unset unset unset pruned kept "--prune"
963 test_configured_prune true unset unset unset kept kept "--no-prune"
965 test_configured_prune unset false unset unset kept kept ""
966 test_configured_prune unset false unset unset kept kept "--no-prune"
967 test_configured_prune unset false unset unset pruned kept "--prune"
969 test_configured_prune false false unset unset kept kept ""
970 test_configured_prune false false unset unset kept kept "--no-prune"
971 test_configured_prune false false unset unset pruned kept "--prune"
972 test_configured_prune false false unset unset kept pruned \
973 "--prune origin refs/tags/*:refs/tags/*"
974 test_configured_prune false false unset unset pruned pruned \
975 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
977 test_configured_prune true false unset unset kept kept ""
978 test_configured_prune true false unset unset pruned kept "--prune"
979 test_configured_prune true false unset unset kept kept "--no-prune"
981 test_configured_prune unset true unset unset pruned kept ""
982 test_configured_prune unset true unset unset kept kept "--no-prune"
983 test_configured_prune unset true unset unset pruned kept "--prune"
985 test_configured_prune false true unset unset pruned kept ""
986 test_configured_prune false true unset unset kept kept "--no-prune"
987 test_configured_prune false true unset unset pruned kept "--prune"
989 test_configured_prune true true unset unset pruned kept ""
990 test_configured_prune true true unset unset pruned kept "--prune"
991 test_configured_prune true true unset unset kept kept "--no-prune"
992 test_configured_prune true true unset unset kept pruned \
993 "--prune origin refs/tags/*:refs/tags/*"
994 test_configured_prune true true unset unset pruned pruned \
995 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
997 # --prune-tags on its own does nothing, needs --prune as well, same
998 # for fetch.pruneTags without fetch.prune
999 test_configured_prune unset unset unset unset kept kept "--prune-tags"
1000 test_configured_prune unset unset true unset kept kept ""
1001 test_configured_prune unset unset unset true kept kept ""
1003 # These will prune the tags
1004 test_configured_prune unset unset unset unset pruned pruned "--prune --prune-tags"
1005 test_configured_prune true unset true unset pruned pruned ""
1006 test_configured_prune unset true unset true pruned pruned ""
1008 # remote.<name>.pruneTags overrides fetch.pruneTags, just like
1009 # remote.<name>.prune overrides fetch.prune if set.
1010 test_configured_prune true unset true unset pruned pruned ""
1011 test_configured_prune false true false true pruned pruned ""
1012 test_configured_prune true false true false kept kept ""
1014 # When --prune-tags is supplied it's ignored if an explicit refspec is
1015 # given, same for the configuration options.
1016 test_configured_prune unset unset unset unset pruned kept \
1017 "--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"
1018 test_configured_prune unset unset true unset pruned kept \
1019 "--prune origin +refs/heads/*:refs/remotes/origin/*"
1020 test_configured_prune unset unset unset true pruned kept \
1021 "--prune origin +refs/heads/*:refs/remotes/origin/*"
1023 # Pruning that also takes place if a file:// url replaces a named
1024 # remote. However, because there's no implicit
1025 # +refs/heads/*:refs/remotes/origin/* refspec and supplying it on the
1026 # command-line negates --prune-tags, the branches will not be pruned.
1027 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "name"
1028 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "link"
1029 test_configured_prune_type unset unset unset unset pruned pruned "origin --prune --prune-tags" "name"
1030 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1031 test_configured_prune_type unset unset unset unset pruned pruned "--prune --prune-tags origin" "name"
1032 test_configured_prune_type unset unset unset unset kept pruned "--prune --prune-tags origin" "link"
1033 test_configured_prune_type unset unset true unset pruned pruned "--prune origin" "name"
1034 test_configured_prune_type unset unset true unset kept pruned "--prune origin" "link"
1035 test_configured_prune_type unset unset unset true pruned pruned "--prune origin" "name"
1036 test_configured_prune_type unset unset unset true kept pruned "--prune origin" "link"
1037 test_configured_prune_type true unset true unset pruned pruned "origin" "name"
1038 test_configured_prune_type true unset true unset kept pruned "origin" "link"
1039 test_configured_prune_type unset true true unset pruned pruned "origin" "name"
1040 test_configured_prune_type unset true true unset kept pruned "origin" "link"
1041 test_configured_prune_type unset true unset true pruned pruned "origin" "name"
1042 test_configured_prune_type unset true unset true kept pruned "origin" "link"
1044 # When all remote.origin.fetch settings are deleted a --prune
1045 # --prune-tags still implicitly supplies refs/tags/*:refs/tags/* so
1046 # tags, but not tracking branches, will be deleted.
1047 test_expect_success 'remove remote.origin.fetch "one"' '
1049 cd one &&
1050 git config --unset-all remote.origin.fetch
1053 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "name"
1054 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1056 test_expect_success 'all boundary commits are excluded' '
1057 test_commit base &&
1058 test_commit oneside &&
1059 git checkout HEAD^ &&
1060 test_commit otherside &&
1061 git checkout main &&
1062 test_tick &&
1063 git merge otherside &&
1064 ad=$(git log --no-walk --format=%ad HEAD) &&
1065 git bundle create twoside-boundary.bdl main --since="$ad" &&
1066 test_bundle_object_count --thin twoside-boundary.bdl 3
1069 test_expect_success 'fetch --prune prints the remotes url' '
1070 git branch goodbye &&
1071 git clone . only-prunes &&
1072 git branch -D goodbye &&
1074 cd only-prunes &&
1075 git fetch --prune origin 2>&1 | head -n1 >../actual
1076 ) &&
1077 echo "From ${D}/." >expect &&
1078 test_cmp expect actual
1081 test_expect_success 'branchname D/F conflict resolved by --prune' '
1082 git branch dir/file &&
1083 git clone . prune-df-conflict &&
1084 git branch -D dir/file &&
1085 git branch dir &&
1087 cd prune-df-conflict &&
1088 git fetch --prune &&
1089 git rev-parse origin/dir >../actual
1090 ) &&
1091 git rev-parse dir >expect &&
1092 test_cmp expect actual
1095 test_expect_success 'branchname D/F conflict rejected with targeted error message' '
1096 git clone . df-conflict-error &&
1097 git branch dir_conflict &&
1099 cd df-conflict-error &&
1100 git update-ref refs/remotes/origin/dir_conflict/file HEAD &&
1101 test_must_fail git fetch 2>err &&
1102 test_grep "error: some local refs could not be updated; try running" err &&
1103 test_grep " ${SQ}git remote prune origin${SQ} to remove any old, conflicting branches" err &&
1104 git pack-refs --all &&
1105 test_must_fail git fetch 2>err-packed &&
1106 test_grep "error: some local refs could not be updated; try running" err-packed &&
1107 test_grep " ${SQ}git remote prune origin${SQ} to remove any old, conflicting branches" err-packed
1111 test_expect_success 'fetching a one-level ref works' '
1112 test_commit extra &&
1113 git reset --hard HEAD^ &&
1114 git update-ref refs/foo extra &&
1115 git init one-level &&
1117 cd one-level &&
1118 git fetch .. HEAD refs/foo
1122 test_expect_success 'fetching with auto-gc does not lock up' '
1123 write_script askyesno <<-\EOF &&
1124 echo "$*" &&
1125 false
1127 git clone "file://$D" auto-gc &&
1128 test_commit test2 &&
1130 cd auto-gc &&
1131 git config fetch.unpackLimit 1 &&
1132 git config gc.autoPackLimit 1 &&
1133 git config gc.autoDetach false &&
1134 GIT_ASK_YESNO="$D/askyesno" git fetch --verbose >fetch.out 2>&1 &&
1135 test_grep "Auto packing the repository" fetch.out &&
1136 ! grep "Should I try again" fetch.out
1140 for section in fetch transfer
1142 test_expect_success "$section.hideRefs affects connectivity check" '
1143 GIT_TRACE="$PWD"/trace git -c $section.hideRefs=refs -c \
1144 $section.hideRefs="!refs/tags/" fetch &&
1145 grep "git rev-list .*--exclude-hidden=fetch" trace
1147 done
1149 test_expect_success 'prepare source branch' '
1150 echo one >onebranch &&
1151 git checkout --orphan onebranch &&
1152 git rm --cached -r . &&
1153 git add onebranch &&
1154 git commit -m onebranch &&
1155 git rev-list --objects onebranch -- >actual &&
1156 # 3 objects should be created, at least ...
1157 test 3 -le $(wc -l <actual)
1160 validate_store_type () {
1161 git -C dest count-objects -v >actual &&
1162 case "$store_type" in
1163 packed)
1164 grep "^count: 0$" actual ;;
1165 loose)
1166 grep "^packs: 0$" actual ;;
1167 esac || {
1168 echo "store_type is $store_type"
1169 cat actual
1170 false
1174 test_unpack_limit () {
1175 store_type=$1
1177 case "$store_type" in
1178 packed) fetch_limit=1 transfer_limit=10000 ;;
1179 loose) fetch_limit=10000 transfer_limit=1 ;;
1180 esac
1182 test_expect_success "fetch trumps transfer limit" '
1183 rm -fr dest &&
1184 git --bare init dest &&
1185 git -C dest config fetch.unpacklimit $fetch_limit &&
1186 git -C dest config transfer.unpacklimit $transfer_limit &&
1187 git -C dest fetch .. onebranch &&
1188 validate_store_type
1192 test_unpack_limit packed
1193 test_unpack_limit loose
1195 setup_negotiation_tip () {
1196 SERVER="$1"
1197 URL="$2"
1198 USE_PROTOCOL_V2="$3"
1200 rm -rf "$SERVER" client trace &&
1201 git init -b main "$SERVER" &&
1202 test_commit -C "$SERVER" alpha_1 &&
1203 test_commit -C "$SERVER" alpha_2 &&
1204 git -C "$SERVER" checkout --orphan beta &&
1205 test_commit -C "$SERVER" beta_1 &&
1206 test_commit -C "$SERVER" beta_2 &&
1208 git clone "$URL" client &&
1210 if test "$USE_PROTOCOL_V2" -eq 1
1211 then
1212 git -C "$SERVER" config protocol.version 2 &&
1213 git -C client config protocol.version 2
1214 fi &&
1216 test_commit -C "$SERVER" beta_s &&
1217 git -C "$SERVER" checkout main &&
1218 test_commit -C "$SERVER" alpha_s &&
1219 git -C "$SERVER" tag -d alpha_1 alpha_2 beta_1 beta_2
1222 check_negotiation_tip () {
1223 # Ensure that {alpha,beta}_1 are sent as "have", but not {alpha_beta}_2
1224 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1225 grep "fetch> have $ALPHA_1" trace &&
1226 BETA_1=$(git -C client rev-parse beta_1) &&
1227 grep "fetch> have $BETA_1" trace &&
1228 ALPHA_2=$(git -C client rev-parse alpha_2) &&
1229 ! grep "fetch> have $ALPHA_2" trace &&
1230 BETA_2=$(git -C client rev-parse beta_2) &&
1231 ! grep "fetch> have $BETA_2" trace
1234 test_expect_success '--negotiation-tip limits "have" lines sent' '
1235 setup_negotiation_tip server server 0 &&
1236 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1237 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1238 origin alpha_s beta_s &&
1239 check_negotiation_tip
1242 test_expect_success '--negotiation-tip understands globs' '
1243 setup_negotiation_tip server server 0 &&
1244 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1245 --negotiation-tip=*_1 \
1246 origin alpha_s beta_s &&
1247 check_negotiation_tip
1250 test_expect_success '--negotiation-tip understands abbreviated SHA-1' '
1251 setup_negotiation_tip server server 0 &&
1252 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1253 --negotiation-tip=$(git -C client rev-parse --short alpha_1) \
1254 --negotiation-tip=$(git -C client rev-parse --short beta_1) \
1255 origin alpha_s beta_s &&
1256 check_negotiation_tip
1259 test_expect_success '--negotiation-tip rejects missing OIDs' '
1260 setup_negotiation_tip server server 0 &&
1261 test_must_fail git -C client fetch \
1262 --negotiation-tip=alpha_1 \
1263 --negotiation-tip=$(test_oid zero) \
1264 origin alpha_s beta_s 2>err &&
1265 cat >fatal-expect <<-EOF &&
1266 fatal: the object $(test_oid zero) does not exist
1268 grep fatal: err >fatal-actual &&
1269 test_cmp fatal-expect fatal-actual
1272 test_expect_success SYMLINKS 'clone does not get confused by a D/F conflict' '
1273 git init df-conflict &&
1275 cd df-conflict &&
1276 ln -s .git a &&
1277 git add a &&
1278 test_tick &&
1279 git commit -m symlink &&
1280 test_commit a- &&
1281 rm a &&
1282 mkdir -p a/hooks &&
1283 write_script a/hooks/post-checkout <<-EOF &&
1284 echo WHOOPSIE >&2
1285 echo whoopsie >"$TRASH_DIRECTORY"/whoops
1287 git add a/hooks/post-checkout &&
1288 test_tick &&
1289 git commit -m post-checkout
1290 ) &&
1291 git clone df-conflict clone 2>err &&
1292 test_grep ! WHOOPS err &&
1293 test_path_is_missing whoops
1296 . "$TEST_DIRECTORY"/lib-httpd.sh
1297 start_httpd
1299 test_expect_success '--negotiation-tip limits "have" lines sent with HTTP protocol v2' '
1300 setup_negotiation_tip "$HTTPD_DOCUMENT_ROOT_PATH/server" \
1301 "$HTTPD_URL/smart/server" 1 &&
1302 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1303 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1304 origin alpha_s beta_s &&
1305 check_negotiation_tip
1308 # DO NOT add non-httpd-specific tests here, because the last part of this
1309 # test script is only executed when httpd is available and enabled.
1311 test_done