Sync with 2.44.1
[git/gitster.git] / t / t5510-fetch.sh
blob9441793d063a766feb653f611c869f6d9cbd7f20
1 #!/bin/sh
2 # Copyright (c) 2006, Junio C Hamano.
4 test_description='Per branch config variables affects "git fetch".
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-bundle.sh
11 D=$(pwd)
13 test_expect_success setup '
14 echo >file original &&
15 git add file &&
16 git commit -a -m original &&
17 git branch -M main
20 test_expect_success "clone and setup child repos" '
21 git clone . one &&
23 cd one &&
24 echo >file updated by one &&
25 git commit -a -m "updated by one"
26 ) &&
27 git clone . two &&
29 cd two &&
30 git config branch.main.remote one &&
31 git config remote.one.url ../one/.git/ &&
32 git config remote.one.fetch refs/heads/main:refs/heads/one
33 ) &&
34 git clone . three &&
36 cd three &&
37 git config branch.main.remote two &&
38 git config branch.main.merge refs/heads/one &&
39 mkdir -p .git/remotes &&
40 cat >.git/remotes/two <<-\EOF
41 URL: ../two/.git/
42 Pull: refs/heads/main:refs/heads/two
43 Pull: refs/heads/one:refs/heads/one
44 EOF
45 ) &&
46 git clone . bundle &&
47 git clone . seven
50 test_expect_success "fetch test" '
51 cd "$D" &&
52 echo >file updated by origin &&
53 git commit -a -m "updated by origin" &&
54 cd two &&
55 git fetch &&
56 git rev-parse --verify refs/heads/one &&
57 mine=$(git rev-parse refs/heads/one) &&
58 his=$(cd ../one && git rev-parse refs/heads/main) &&
59 test "z$mine" = "z$his"
62 test_expect_success "fetch test for-merge" '
63 cd "$D" &&
64 cd three &&
65 git fetch &&
66 git rev-parse --verify refs/heads/two &&
67 git rev-parse --verify refs/heads/one &&
68 main_in_two=$(cd ../two && git rev-parse main) &&
69 one_in_two=$(cd ../two && git rev-parse one) &&
71 echo "$one_in_two " &&
72 echo "$main_in_two not-for-merge"
73 } >expected &&
74 cut -f -2 .git/FETCH_HEAD >actual &&
75 test_cmp expected actual'
77 test_expect_success 'fetch --prune on its own works as expected' '
78 cd "$D" &&
79 git clone . prune &&
80 cd prune &&
81 git update-ref refs/remotes/origin/extrabranch main &&
83 git fetch --prune origin &&
84 test_must_fail git rev-parse origin/extrabranch
87 test_expect_success 'fetch --prune with a branch name keeps branches' '
88 cd "$D" &&
89 git clone . prune-branch &&
90 cd prune-branch &&
91 git update-ref refs/remotes/origin/extrabranch main &&
93 git fetch --prune origin main &&
94 git rev-parse origin/extrabranch
97 test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
98 cd "$D" &&
99 git clone . prune-namespace &&
100 cd prune-namespace &&
102 git fetch --prune origin refs/heads/a/*:refs/remotes/origin/a/* &&
103 git rev-parse origin/main
106 test_expect_success 'fetch --prune handles overlapping refspecs' '
107 cd "$D" &&
108 git update-ref refs/pull/42/head main &&
109 git clone . prune-overlapping &&
110 cd prune-overlapping &&
111 git config --add remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
113 git fetch --prune origin &&
114 git rev-parse origin/main &&
115 git rev-parse origin/pr/42 &&
117 git config --unset-all remote.origin.fetch &&
118 git config remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
119 git config --add remote.origin.fetch refs/heads/*:refs/remotes/origin/* &&
121 git fetch --prune origin &&
122 git rev-parse origin/main &&
123 git rev-parse origin/pr/42
126 test_expect_success 'fetch --prune --tags prunes branches but not tags' '
127 cd "$D" &&
128 git clone . prune-tags &&
129 cd prune-tags &&
130 git tag sometag main &&
131 # Create what looks like a remote-tracking branch from an earlier
132 # fetch that has since been deleted from the remote:
133 git update-ref refs/remotes/origin/fake-remote main &&
135 git fetch --prune --tags origin &&
136 git rev-parse origin/main &&
137 test_must_fail git rev-parse origin/fake-remote &&
138 git rev-parse sometag
141 test_expect_success 'fetch --prune --tags with branch does not prune other things' '
142 cd "$D" &&
143 git clone . prune-tags-branch &&
144 cd prune-tags-branch &&
145 git tag sometag main &&
146 git update-ref refs/remotes/origin/extrabranch main &&
148 git fetch --prune --tags origin main &&
149 git rev-parse origin/extrabranch &&
150 git rev-parse sometag
153 test_expect_success 'fetch --prune --tags with refspec prunes based on refspec' '
154 cd "$D" &&
155 git clone . prune-tags-refspec &&
156 cd prune-tags-refspec &&
157 git tag sometag main &&
158 git update-ref refs/remotes/origin/foo/otherbranch main &&
159 git update-ref refs/remotes/origin/extrabranch main &&
161 git fetch --prune --tags origin refs/heads/foo/*:refs/remotes/origin/foo/* &&
162 test_must_fail git rev-parse refs/remotes/origin/foo/otherbranch &&
163 git rev-parse origin/extrabranch &&
164 git rev-parse sometag
167 test_expect_success REFFILES 'fetch --prune fails to delete branches' '
168 cd "$D" &&
169 git clone . prune-fail &&
170 cd prune-fail &&
171 git update-ref refs/remotes/origin/extrabranch main &&
172 git pack-refs --all &&
173 : this will prevent --prune from locking packed-refs for deleting refs, but adding loose refs still succeeds &&
174 >.git/packed-refs.new &&
176 test_must_fail git fetch --prune origin
179 test_expect_success 'fetch --atomic works with a single branch' '
180 test_when_finished "rm -rf \"$D\"/atomic" &&
182 cd "$D" &&
183 git clone . atomic &&
184 git branch atomic-branch &&
185 oid=$(git rev-parse atomic-branch) &&
186 echo "$oid" >expected &&
188 git -C atomic fetch --atomic origin &&
189 git -C atomic rev-parse origin/atomic-branch >actual &&
190 test_cmp expected actual &&
191 test $oid = "$(git -C atomic rev-parse --verify FETCH_HEAD)"
194 test_expect_success 'fetch --atomic works with multiple branches' '
195 test_when_finished "rm -rf \"$D\"/atomic" &&
197 cd "$D" &&
198 git clone . atomic &&
199 git branch atomic-branch-1 &&
200 git branch atomic-branch-2 &&
201 git branch atomic-branch-3 &&
202 git rev-parse refs/heads/atomic-branch-1 refs/heads/atomic-branch-2 refs/heads/atomic-branch-3 >actual &&
204 git -C atomic fetch --atomic origin &&
205 git -C atomic rev-parse refs/remotes/origin/atomic-branch-1 refs/remotes/origin/atomic-branch-2 refs/remotes/origin/atomic-branch-3 >expected &&
206 test_cmp expected actual
209 test_expect_success 'fetch --atomic works with mixed branches and tags' '
210 test_when_finished "rm -rf \"$D\"/atomic" &&
212 cd "$D" &&
213 git clone . atomic &&
214 git branch atomic-mixed-branch &&
215 git tag atomic-mixed-tag &&
216 git rev-parse refs/heads/atomic-mixed-branch refs/tags/atomic-mixed-tag >actual &&
218 git -C atomic fetch --tags --atomic origin &&
219 git -C atomic rev-parse refs/remotes/origin/atomic-mixed-branch refs/tags/atomic-mixed-tag >expected &&
220 test_cmp expected actual
223 test_expect_success 'fetch --atomic prunes references' '
224 test_when_finished "rm -rf \"$D\"/atomic" &&
226 cd "$D" &&
227 git branch atomic-prune-delete &&
228 git clone . atomic &&
229 git branch --delete atomic-prune-delete &&
230 git branch atomic-prune-create &&
231 git rev-parse refs/heads/atomic-prune-create >actual &&
233 git -C atomic fetch --prune --atomic origin &&
234 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-prune-delete &&
235 git -C atomic rev-parse refs/remotes/origin/atomic-prune-create >expected &&
236 test_cmp expected actual
239 test_expect_success 'fetch --atomic aborts with non-fast-forward update' '
240 test_when_finished "rm -rf \"$D\"/atomic" &&
242 cd "$D" &&
243 git branch atomic-non-ff &&
244 git clone . atomic &&
245 git rev-parse HEAD >actual &&
247 git branch atomic-new-branch &&
248 parent_commit=$(git rev-parse atomic-non-ff~) &&
249 git update-ref refs/heads/atomic-non-ff $parent_commit &&
251 test_must_fail git -C atomic fetch --atomic origin refs/heads/*:refs/remotes/origin/* &&
252 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-new-branch &&
253 git -C atomic rev-parse refs/remotes/origin/atomic-non-ff >expected &&
254 test_cmp expected actual &&
255 test_must_be_empty atomic/.git/FETCH_HEAD
258 test_expect_success 'fetch --atomic executes a single reference transaction only' '
259 test_when_finished "rm -rf \"$D\"/atomic" &&
261 cd "$D" &&
262 git clone . atomic &&
263 git branch atomic-hooks-1 &&
264 git branch atomic-hooks-2 &&
265 head_oid=$(git rev-parse HEAD) &&
267 cat >expected <<-EOF &&
268 prepared
269 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
270 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
271 committed
272 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
273 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
276 rm -f atomic/actual &&
277 test_hook -C atomic reference-transaction <<-\EOF &&
278 ( echo "$*" && cat ) >>actual
281 git -C atomic fetch --atomic origin &&
282 test_cmp expected atomic/actual
285 test_expect_success 'fetch --atomic aborts all reference updates if hook aborts' '
286 test_when_finished "rm -rf \"$D\"/atomic" &&
288 cd "$D" &&
289 git clone . atomic &&
290 git branch atomic-hooks-abort-1 &&
291 git branch atomic-hooks-abort-2 &&
292 git branch atomic-hooks-abort-3 &&
293 git tag atomic-hooks-abort &&
294 head_oid=$(git rev-parse HEAD) &&
296 cat >expected <<-EOF &&
297 prepared
298 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
299 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
300 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
301 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
302 aborted
303 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
304 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
305 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
306 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
309 rm -f atomic/actual &&
310 test_hook -C atomic/.git reference-transaction <<-\EOF &&
311 ( echo "$*" && cat ) >>actual
312 exit 1
315 git -C atomic for-each-ref >expected-refs &&
316 test_must_fail git -C atomic fetch --tags --atomic origin &&
317 git -C atomic for-each-ref >actual-refs &&
318 test_cmp expected-refs actual-refs &&
319 test_must_be_empty atomic/.git/FETCH_HEAD
322 test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' '
323 test_when_finished "rm -rf \"$D\"/atomic" &&
325 cd "$D" &&
326 git clone . atomic &&
327 oid=$(git rev-parse HEAD) &&
329 git branch atomic-fetch-head-1 &&
330 git -C atomic fetch --atomic origin atomic-fetch-head-1 &&
331 test_line_count = 1 atomic/.git/FETCH_HEAD &&
333 git branch atomic-fetch-head-2 &&
334 git -C atomic fetch --atomic --append origin atomic-fetch-head-2 &&
335 test_line_count = 2 atomic/.git/FETCH_HEAD &&
336 cp atomic/.git/FETCH_HEAD expected &&
338 test_hook -C atomic reference-transaction <<-\EOF &&
339 exit 1
342 git branch atomic-fetch-head-3 &&
343 test_must_fail git -C atomic fetch --atomic --append origin atomic-fetch-head-3 &&
344 test_cmp expected atomic/.git/FETCH_HEAD
347 test_expect_success '--refmap="" ignores configured refspec' '
348 cd "$TRASH_DIRECTORY" &&
349 git clone "$D" remote-refs &&
350 git -C remote-refs rev-parse remotes/origin/main >old &&
351 git -C remote-refs update-ref refs/remotes/origin/main main~1 &&
352 git -C remote-refs rev-parse remotes/origin/main >new &&
353 git -C remote-refs fetch --refmap= origin "+refs/heads/*:refs/hidden/origin/*" &&
354 git -C remote-refs rev-parse remotes/origin/main >actual &&
355 test_cmp new actual &&
356 git -C remote-refs fetch origin &&
357 git -C remote-refs rev-parse remotes/origin/main >actual &&
358 test_cmp old actual
361 test_expect_success '--refmap="" and --prune' '
362 git -C remote-refs update-ref refs/remotes/origin/foo/otherbranch main &&
363 git -C remote-refs update-ref refs/hidden/foo/otherbranch main &&
364 git -C remote-refs fetch --prune --refmap="" origin +refs/heads/*:refs/hidden/* &&
365 git -C remote-refs rev-parse remotes/origin/foo/otherbranch &&
366 test_must_fail git -C remote-refs rev-parse refs/hidden/foo/otherbranch &&
367 git -C remote-refs fetch --prune origin &&
368 test_must_fail git -C remote-refs rev-parse remotes/origin/foo/otherbranch
371 test_expect_success 'fetch tags when there is no tags' '
373 cd "$D" &&
375 mkdir notags &&
376 cd notags &&
377 git init &&
379 git fetch -t ..
383 test_expect_success 'fetch following tags' '
385 cd "$D" &&
386 git tag -a -m "annotated" anno HEAD &&
387 git tag light HEAD &&
389 mkdir four &&
390 cd four &&
391 git init &&
393 git fetch .. :track &&
394 git show-ref --verify refs/tags/anno &&
395 git show-ref --verify refs/tags/light
399 test_expect_success 'fetch uses remote ref names to describe new refs' '
400 cd "$D" &&
401 git init descriptive &&
403 cd descriptive &&
404 git config remote.o.url .. &&
405 git config remote.o.fetch "refs/heads/*:refs/crazyheads/*" &&
406 git config --add remote.o.fetch "refs/others/*:refs/heads/*" &&
407 git fetch o
408 ) &&
409 git tag -a -m "Descriptive tag" descriptive-tag &&
410 git branch descriptive-branch &&
411 git checkout descriptive-branch &&
412 echo "Nuts" >crazy &&
413 git add crazy &&
414 git commit -a -m "descriptive commit" &&
415 git update-ref refs/others/crazy HEAD &&
417 cd descriptive &&
418 git fetch o 2>actual &&
419 test_grep "new branch.* -> refs/crazyheads/descriptive-branch$" actual &&
420 test_grep "new tag.* -> descriptive-tag$" actual &&
421 test_grep "new ref.* -> crazy$" actual
422 ) &&
423 git checkout main
426 test_expect_success 'fetch must not resolve short tag name' '
428 cd "$D" &&
430 mkdir five &&
431 cd five &&
432 git init &&
434 test_must_fail git fetch .. anno:five
438 test_expect_success 'fetch can now resolve short remote name' '
440 cd "$D" &&
441 git update-ref refs/remotes/six/HEAD HEAD &&
443 mkdir six &&
444 cd six &&
445 git init &&
447 git fetch .. six:six
450 test_expect_success 'create bundle 1' '
451 cd "$D" &&
452 echo >file updated again by origin &&
453 git commit -a -m "tip" &&
454 git bundle create --version=3 bundle1 main^..main
457 test_expect_success 'header of bundle looks right' '
458 cat >expect <<-EOF &&
459 # v3 git bundle
460 @object-format=$(test_oid algo)
461 -OID updated by origin
462 OID refs/heads/main
465 sed -e "s/$OID_REGEX/OID/g" -e "5q" "$D"/bundle1 >actual &&
466 test_cmp expect actual
469 test_expect_success 'create bundle 2' '
470 cd "$D" &&
471 git bundle create bundle2 main~2..main
474 test_expect_success 'unbundle 1' '
475 cd "$D/bundle" &&
476 git checkout -b some-branch &&
477 test_must_fail git fetch "$D/bundle1" main:main
481 test_expect_success 'bundle 1 has only 3 files ' '
482 cd "$D" &&
483 test_bundle_object_count bundle1 3
486 test_expect_success 'unbundle 2' '
487 cd "$D/bundle" &&
488 git fetch ../bundle2 main:main &&
489 test "tip" = "$(git log -1 --pretty=oneline main | cut -d" " -f2)"
492 test_expect_success 'bundle does not prerequisite objects' '
493 cd "$D" &&
494 touch file2 &&
495 git add file2 &&
496 git commit -m add.file2 file2 &&
497 git bundle create bundle3 -1 HEAD &&
498 test_bundle_object_count bundle3 3
501 test_expect_success 'bundle should be able to create a full history' '
503 cd "$D" &&
504 git tag -a -m "1.0" v1.0 main &&
505 git bundle create bundle4 v1.0
509 test_expect_success 'fetch with a non-applying branch.<name>.merge' '
510 git config branch.main.remote yeti &&
511 git config branch.main.merge refs/heads/bigfoot &&
512 git config remote.blub.url one &&
513 git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
514 git fetch blub
517 # URL supplied to fetch does not match the url of the configured branch's remote
518 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
519 one_head=$(cd one && git rev-parse HEAD) &&
520 this_head=$(git rev-parse HEAD) &&
521 git update-ref -d FETCH_HEAD &&
522 git fetch one &&
523 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
524 test $this_head = "$(git rev-parse --verify HEAD)"
527 # URL supplied to fetch matches the url of the configured branch's remote and
528 # the merge spec matches the branch the remote HEAD points to
529 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
530 one_ref=$(cd one && git symbolic-ref HEAD) &&
531 git config branch.main.remote blub &&
532 git config branch.main.merge "$one_ref" &&
533 git update-ref -d FETCH_HEAD &&
534 git fetch one &&
535 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
536 test $this_head = "$(git rev-parse --verify HEAD)"
539 # URL supplied to fetch matches the url of the configured branch's remote, but
540 # the merge spec does not match the branch the remote HEAD points to
541 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
542 git config branch.main.merge "${one_ref}_not" &&
543 git update-ref -d FETCH_HEAD &&
544 git fetch one &&
545 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
546 test $this_head = "$(git rev-parse --verify HEAD)"
549 # the strange name is: a\!'b
550 test_expect_success 'quoting of a strangely named repo' '
551 test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
552 grep "fatal: '\''a\\\\!'\''b'\''" result
555 test_expect_success 'bundle should record HEAD correctly' '
557 cd "$D" &&
558 git bundle create bundle5 HEAD main &&
559 git bundle list-heads bundle5 >actual &&
560 for h in HEAD refs/heads/main
562 echo "$(git rev-parse --verify $h) $h" || return 1
563 done >expect &&
564 test_cmp expect actual
568 test_expect_success 'mark initial state of origin/main' '
570 cd three &&
571 git tag base-origin-main refs/remotes/origin/main
575 test_expect_success 'explicit fetch should update tracking' '
577 cd "$D" &&
578 git branch -f side &&
580 cd three &&
581 git update-ref refs/remotes/origin/main base-origin-main &&
582 o=$(git rev-parse --verify refs/remotes/origin/main) &&
583 git fetch origin main &&
584 n=$(git rev-parse --verify refs/remotes/origin/main) &&
585 test "$o" != "$n" &&
586 test_must_fail git rev-parse --verify refs/remotes/origin/side
590 test_expect_success 'explicit pull should update tracking' '
592 cd "$D" &&
593 git branch -f side &&
595 cd three &&
596 git update-ref refs/remotes/origin/main base-origin-main &&
597 o=$(git rev-parse --verify refs/remotes/origin/main) &&
598 git pull origin main &&
599 n=$(git rev-parse --verify refs/remotes/origin/main) &&
600 test "$o" != "$n" &&
601 test_must_fail git rev-parse --verify refs/remotes/origin/side
605 test_expect_success 'explicit --refmap is allowed only with command-line refspec' '
606 cd "$D" &&
608 cd three &&
609 test_must_fail git fetch --refmap="*:refs/remotes/none/*"
613 test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
614 cd "$D" &&
615 git branch -f side &&
617 cd three &&
618 git update-ref refs/remotes/origin/main base-origin-main &&
619 o=$(git rev-parse --verify refs/remotes/origin/main) &&
620 git fetch --refmap="refs/heads/*:refs/remotes/other/*" origin main &&
621 n=$(git rev-parse --verify refs/remotes/origin/main) &&
622 test "$o" = "$n" &&
623 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
624 git rev-parse --verify refs/remotes/other/main
628 test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
629 cd "$D" &&
630 git branch -f side &&
632 cd three &&
633 git update-ref refs/remotes/origin/main base-origin-main &&
634 o=$(git rev-parse --verify refs/remotes/origin/main) &&
635 git fetch --refmap="" origin main &&
636 n=$(git rev-parse --verify refs/remotes/origin/main) &&
637 test "$o" = "$n" &&
638 test_must_fail git rev-parse --verify refs/remotes/origin/side
642 test_expect_success 'configured fetch updates tracking' '
644 cd "$D" &&
645 git branch -f side &&
647 cd three &&
648 git update-ref refs/remotes/origin/main base-origin-main &&
649 o=$(git rev-parse --verify refs/remotes/origin/main) &&
650 git fetch origin &&
651 n=$(git rev-parse --verify refs/remotes/origin/main) &&
652 test "$o" != "$n" &&
653 git rev-parse --verify refs/remotes/origin/side
657 test_expect_success 'non-matching refspecs do not confuse tracking update' '
658 cd "$D" &&
659 git update-ref refs/odd/location HEAD &&
661 cd three &&
662 git update-ref refs/remotes/origin/main base-origin-main &&
663 git config --add remote.origin.fetch \
664 refs/odd/location:refs/remotes/origin/odd &&
665 o=$(git rev-parse --verify refs/remotes/origin/main) &&
666 git fetch origin main &&
667 n=$(git rev-parse --verify refs/remotes/origin/main) &&
668 test "$o" != "$n" &&
669 test_must_fail git rev-parse --verify refs/remotes/origin/odd
673 test_expect_success 'pushing nonexistent branch by mistake should not segv' '
675 cd "$D" &&
676 test_must_fail git push seven no:no
680 test_expect_success 'auto tag following fetches minimum' '
682 cd "$D" &&
683 git clone .git follow &&
684 git checkout HEAD^0 &&
686 for i in 1 2 3 4 5 6 7
688 echo $i >>file &&
689 git commit -m $i -a &&
690 git tag -a -m $i excess-$i || exit 1
691 done
692 ) &&
693 git checkout main &&
695 cd follow &&
696 git fetch
700 test_expect_success 'refuse to fetch into the current branch' '
702 test_must_fail git fetch . side:main
706 test_expect_success 'fetch into the current branch with --update-head-ok' '
708 git fetch --update-head-ok . side:main
712 test_expect_success 'fetch --dry-run does not touch FETCH_HEAD, but still prints what would be written' '
713 rm -f .git/FETCH_HEAD err &&
714 git fetch --dry-run . 2>err &&
715 ! test -f .git/FETCH_HEAD &&
716 grep FETCH_HEAD err
719 test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD, and does not print what would be written' '
720 rm -f .git/FETCH_HEAD err &&
721 git fetch --no-write-fetch-head . 2>err &&
722 ! test -f .git/FETCH_HEAD &&
723 ! grep FETCH_HEAD err
726 test_expect_success '--write-fetch-head gets defeated by --dry-run' '
727 rm -f .git/FETCH_HEAD &&
728 git fetch --dry-run --write-fetch-head . &&
729 ! test -f .git/FETCH_HEAD
732 test_expect_success "should be able to fetch with duplicate refspecs" '
733 mkdir dups &&
735 cd dups &&
736 git init &&
737 git config branch.main.remote three &&
738 git config remote.three.url ../three/.git &&
739 git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
740 git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
741 git fetch three
745 test_expect_success 'LHS of refspec follows ref disambiguation rules' '
746 mkdir lhs-ambiguous &&
748 cd lhs-ambiguous &&
749 git init server &&
750 test_commit -C server unwanted &&
751 test_commit -C server wanted &&
753 git init client &&
755 # Check a name coming after "refs" alphabetically ...
756 git -C server update-ref refs/heads/s wanted &&
757 git -C server update-ref refs/heads/refs/heads/s unwanted &&
758 git -C client fetch ../server +refs/heads/s:refs/heads/checkthis &&
759 git -C server rev-parse wanted >expect &&
760 git -C client rev-parse checkthis >actual &&
761 test_cmp expect actual &&
763 # ... and one before.
764 git -C server update-ref refs/heads/q wanted &&
765 git -C server update-ref refs/heads/refs/heads/q unwanted &&
766 git -C client fetch ../server +refs/heads/q:refs/heads/checkthis &&
767 git -C server rev-parse wanted >expect &&
768 git -C client rev-parse checkthis >actual &&
769 test_cmp expect actual &&
771 # Tags are preferred over branches like refs/{heads,tags}/*
772 git -C server update-ref refs/tags/t wanted &&
773 git -C server update-ref refs/heads/t unwanted &&
774 git -C client fetch ../server +t:refs/heads/checkthis &&
775 git -C server rev-parse wanted >expect &&
776 git -C client rev-parse checkthis >actual
780 test_expect_success 'fetch.writeCommitGraph' '
781 git clone three write &&
783 cd three &&
784 test_commit new
785 ) &&
787 cd write &&
788 git -c fetch.writeCommitGraph fetch origin &&
789 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
793 test_expect_success 'fetch.writeCommitGraph with submodules' '
794 test_config_global protocol.file.allow always &&
795 git clone dups super &&
797 cd super &&
798 git submodule add "file://$TRASH_DIRECTORY/three" &&
799 git commit -m "add submodule"
800 ) &&
801 git clone "super" super-clone &&
803 cd super-clone &&
804 rm -rf .git/objects/info &&
805 git -c fetch.writeCommitGraph=true fetch origin &&
806 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain &&
807 git -c fetch.writeCommitGraph=true fetch --recurse-submodules origin
811 # fetches from first configured url
812 test_expect_success 'fetch from multiple configured URLs in single remote' '
813 git init url1 &&
814 git remote add multipleurls url1 &&
815 git remote set-url --add multipleurls url2 &&
816 git fetch multipleurls
819 # configured prune tests
821 set_config_tristate () {
822 # var=$1 val=$2
823 case "$2" in
824 unset)
825 test_unconfig "$1"
828 git config "$1" "$2"
829 key=$(echo $1 | sed -e 's/^remote\.origin/fetch/')
830 git_fetch_c="$git_fetch_c -c $key=$2"
832 esac
835 test_configured_prune () {
836 test_configured_prune_type "$@" "name"
837 test_configured_prune_type "$@" "link"
840 test_configured_prune_type () {
841 fetch_prune=$1
842 remote_origin_prune=$2
843 fetch_prune_tags=$3
844 remote_origin_prune_tags=$4
845 expected_branch=$5
846 expected_tag=$6
847 cmdline=$7
848 mode=$8
850 if test -z "$cmdline_setup"
851 then
852 test_expect_success 'setup cmdline_setup variable for subsequent test' '
853 remote_url="file://$(git -C one config remote.origin.url)" &&
854 remote_fetch="$(git -C one config remote.origin.fetch)" &&
855 cmdline_setup="\"$remote_url\" \"$remote_fetch\""
859 if test "$mode" = 'link'
860 then
861 new_cmdline=""
863 if test "$cmdline" = ""
864 then
865 new_cmdline=$cmdline_setup
866 else
867 new_cmdline=$(perl -e '
868 my ($cmdline, $url) = @ARGV;
869 $cmdline =~ s[origin(?!/)][quotemeta($url)]ge;
870 print $cmdline;
871 ' -- "$cmdline" "$remote_url")
874 if test "$fetch_prune_tags" = 'true' ||
875 test "$remote_origin_prune_tags" = 'true'
876 then
877 if ! printf '%s' "$cmdline\n" | grep -q refs/remotes/origin/
878 then
879 new_cmdline="$new_cmdline refs/tags/*:refs/tags/*"
883 cmdline="$new_cmdline"
886 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" '
887 # make sure a newbranch is there in . and also in one
888 git branch -f newbranch &&
889 git tag -f newtag &&
891 cd one &&
892 test_unconfig fetch.prune &&
893 test_unconfig fetch.pruneTags &&
894 test_unconfig remote.origin.prune &&
895 test_unconfig remote.origin.pruneTags &&
896 git fetch '"$cmdline_setup"' &&
897 git rev-parse --verify refs/remotes/origin/newbranch &&
898 git rev-parse --verify refs/tags/newtag
899 ) &&
901 # now remove them
902 git branch -d newbranch &&
903 git tag -d newtag &&
905 # then test
907 cd one &&
908 git_fetch_c="" &&
909 set_config_tristate fetch.prune $fetch_prune &&
910 set_config_tristate fetch.pruneTags $fetch_prune_tags &&
911 set_config_tristate remote.origin.prune $remote_origin_prune &&
912 set_config_tristate remote.origin.pruneTags $remote_origin_prune_tags &&
914 if test "$mode" != "link"
915 then
916 git_fetch_c=""
917 fi &&
918 git$git_fetch_c fetch '"$cmdline"' &&
919 case "$expected_branch" in
920 pruned)
921 test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
923 kept)
924 git rev-parse --verify refs/remotes/origin/newbranch
926 esac &&
927 case "$expected_tag" in
928 pruned)
929 test_must_fail git rev-parse --verify refs/tags/newtag
931 kept)
932 git rev-parse --verify refs/tags/newtag
934 esac
939 # $1 config: fetch.prune
940 # $2 config: remote.<name>.prune
941 # $3 config: fetch.pruneTags
942 # $4 config: remote.<name>.pruneTags
943 # $5 expect: branch to be pruned?
944 # $6 expect: tag to be pruned?
945 # $7 git-fetch $cmdline:
947 # $1 $2 $3 $4 $5 $6 $7
948 test_configured_prune unset unset unset unset kept kept ""
949 test_configured_prune unset unset unset unset kept kept "--no-prune"
950 test_configured_prune unset unset unset unset pruned kept "--prune"
951 test_configured_prune unset unset unset unset kept pruned \
952 "--prune origin refs/tags/*:refs/tags/*"
953 test_configured_prune unset unset unset unset pruned pruned \
954 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
956 test_configured_prune false unset unset unset kept kept ""
957 test_configured_prune false unset unset unset kept kept "--no-prune"
958 test_configured_prune false unset unset unset pruned kept "--prune"
960 test_configured_prune true unset unset unset pruned kept ""
961 test_configured_prune true unset unset unset pruned kept "--prune"
962 test_configured_prune true unset unset unset kept kept "--no-prune"
964 test_configured_prune unset false unset unset kept kept ""
965 test_configured_prune unset false unset unset kept kept "--no-prune"
966 test_configured_prune unset false unset unset pruned kept "--prune"
968 test_configured_prune false false unset unset kept kept ""
969 test_configured_prune false false unset unset kept kept "--no-prune"
970 test_configured_prune false false unset unset pruned kept "--prune"
971 test_configured_prune false false unset unset kept pruned \
972 "--prune origin refs/tags/*:refs/tags/*"
973 test_configured_prune false false unset unset pruned pruned \
974 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
976 test_configured_prune true false unset unset kept kept ""
977 test_configured_prune true false unset unset pruned kept "--prune"
978 test_configured_prune true false unset unset kept kept "--no-prune"
980 test_configured_prune unset true unset unset pruned kept ""
981 test_configured_prune unset true unset unset kept kept "--no-prune"
982 test_configured_prune unset true unset unset pruned kept "--prune"
984 test_configured_prune false true unset unset pruned kept ""
985 test_configured_prune false true unset unset kept kept "--no-prune"
986 test_configured_prune false true unset unset pruned kept "--prune"
988 test_configured_prune true true unset unset pruned kept ""
989 test_configured_prune true true unset unset pruned kept "--prune"
990 test_configured_prune true true unset unset kept kept "--no-prune"
991 test_configured_prune true true unset unset kept pruned \
992 "--prune origin refs/tags/*:refs/tags/*"
993 test_configured_prune true true unset unset pruned pruned \
994 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
996 # --prune-tags on its own does nothing, needs --prune as well, same
997 # for fetch.pruneTags without fetch.prune
998 test_configured_prune unset unset unset unset kept kept "--prune-tags"
999 test_configured_prune unset unset true unset kept kept ""
1000 test_configured_prune unset unset unset true kept kept ""
1002 # These will prune the tags
1003 test_configured_prune unset unset unset unset pruned pruned "--prune --prune-tags"
1004 test_configured_prune true unset true unset pruned pruned ""
1005 test_configured_prune unset true unset true pruned pruned ""
1007 # remote.<name>.pruneTags overrides fetch.pruneTags, just like
1008 # remote.<name>.prune overrides fetch.prune if set.
1009 test_configured_prune true unset true unset pruned pruned ""
1010 test_configured_prune false true false true pruned pruned ""
1011 test_configured_prune true false true false kept kept ""
1013 # When --prune-tags is supplied it's ignored if an explicit refspec is
1014 # given, same for the configuration options.
1015 test_configured_prune unset unset unset unset pruned kept \
1016 "--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"
1017 test_configured_prune unset unset true unset pruned kept \
1018 "--prune origin +refs/heads/*:refs/remotes/origin/*"
1019 test_configured_prune unset unset unset true pruned kept \
1020 "--prune origin +refs/heads/*:refs/remotes/origin/*"
1022 # Pruning that also takes place if a file:// url replaces a named
1023 # remote. However, because there's no implicit
1024 # +refs/heads/*:refs/remotes/origin/* refspec and supplying it on the
1025 # command-line negates --prune-tags, the branches will not be pruned.
1026 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "name"
1027 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "link"
1028 test_configured_prune_type unset unset unset unset pruned pruned "origin --prune --prune-tags" "name"
1029 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1030 test_configured_prune_type unset unset unset unset pruned pruned "--prune --prune-tags origin" "name"
1031 test_configured_prune_type unset unset unset unset kept pruned "--prune --prune-tags origin" "link"
1032 test_configured_prune_type unset unset true unset pruned pruned "--prune origin" "name"
1033 test_configured_prune_type unset unset true unset kept pruned "--prune origin" "link"
1034 test_configured_prune_type unset unset unset true pruned pruned "--prune origin" "name"
1035 test_configured_prune_type unset unset unset true kept pruned "--prune origin" "link"
1036 test_configured_prune_type true unset true unset pruned pruned "origin" "name"
1037 test_configured_prune_type true unset true unset kept pruned "origin" "link"
1038 test_configured_prune_type unset true true unset pruned pruned "origin" "name"
1039 test_configured_prune_type unset true true unset kept pruned "origin" "link"
1040 test_configured_prune_type unset true unset true pruned pruned "origin" "name"
1041 test_configured_prune_type unset true unset true kept pruned "origin" "link"
1043 # When all remote.origin.fetch settings are deleted a --prune
1044 # --prune-tags still implicitly supplies refs/tags/*:refs/tags/* so
1045 # tags, but not tracking branches, will be deleted.
1046 test_expect_success 'remove remote.origin.fetch "one"' '
1048 cd one &&
1049 git config --unset-all remote.origin.fetch
1052 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "name"
1053 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1055 test_expect_success 'all boundary commits are excluded' '
1056 test_commit base &&
1057 test_commit oneside &&
1058 git checkout HEAD^ &&
1059 test_commit otherside &&
1060 git checkout main &&
1061 test_tick &&
1062 git merge otherside &&
1063 ad=$(git log --no-walk --format=%ad HEAD) &&
1064 git bundle create twoside-boundary.bdl main --since="$ad" &&
1065 test_bundle_object_count --thin twoside-boundary.bdl 3
1068 test_expect_success 'fetch --prune prints the remotes url' '
1069 git branch goodbye &&
1070 git clone . only-prunes &&
1071 git branch -D goodbye &&
1073 cd only-prunes &&
1074 git fetch --prune origin 2>&1 | head -n1 >../actual
1075 ) &&
1076 echo "From ${D}/." >expect &&
1077 test_cmp expect actual
1080 test_expect_success 'branchname D/F conflict resolved by --prune' '
1081 git branch dir/file &&
1082 git clone . prune-df-conflict &&
1083 git branch -D dir/file &&
1084 git branch dir &&
1086 cd prune-df-conflict &&
1087 git fetch --prune &&
1088 git rev-parse origin/dir >../actual
1089 ) &&
1090 git rev-parse dir >expect &&
1091 test_cmp expect actual
1094 test_expect_success 'fetching a one-level ref works' '
1095 test_commit extra &&
1096 git reset --hard HEAD^ &&
1097 git update-ref refs/foo extra &&
1098 git init one-level &&
1100 cd one-level &&
1101 git fetch .. HEAD refs/foo
1105 test_expect_success 'fetching with auto-gc does not lock up' '
1106 write_script askyesno <<-\EOF &&
1107 echo "$*" &&
1108 false
1110 git clone "file://$D" auto-gc &&
1111 test_commit test2 &&
1113 cd auto-gc &&
1114 git config fetch.unpackLimit 1 &&
1115 git config gc.autoPackLimit 1 &&
1116 git config gc.autoDetach false &&
1117 GIT_ASK_YESNO="$D/askyesno" git fetch --verbose >fetch.out 2>&1 &&
1118 test_grep "Auto packing the repository" fetch.out &&
1119 ! grep "Should I try again" fetch.out
1123 for section in fetch transfer
1125 test_expect_success "$section.hideRefs affects connectivity check" '
1126 GIT_TRACE="$PWD"/trace git -c $section.hideRefs=refs -c \
1127 $section.hideRefs="!refs/tags/" fetch &&
1128 grep "git rev-list .*--exclude-hidden=fetch" trace
1130 done
1132 test_expect_success 'prepare source branch' '
1133 echo one >onebranch &&
1134 git checkout --orphan onebranch &&
1135 git rm --cached -r . &&
1136 git add onebranch &&
1137 git commit -m onebranch &&
1138 git rev-list --objects onebranch -- >actual &&
1139 # 3 objects should be created, at least ...
1140 test 3 -le $(wc -l <actual)
1143 validate_store_type () {
1144 git -C dest count-objects -v >actual &&
1145 case "$store_type" in
1146 packed)
1147 grep "^count: 0$" actual ;;
1148 loose)
1149 grep "^packs: 0$" actual ;;
1150 esac || {
1151 echo "store_type is $store_type"
1152 cat actual
1153 false
1157 test_unpack_limit () {
1158 store_type=$1
1160 case "$store_type" in
1161 packed) fetch_limit=1 transfer_limit=10000 ;;
1162 loose) fetch_limit=10000 transfer_limit=1 ;;
1163 esac
1165 test_expect_success "fetch trumps transfer limit" '
1166 rm -fr dest &&
1167 git --bare init dest &&
1168 git -C dest config fetch.unpacklimit $fetch_limit &&
1169 git -C dest config transfer.unpacklimit $transfer_limit &&
1170 git -C dest fetch .. onebranch &&
1171 validate_store_type
1175 test_unpack_limit packed
1176 test_unpack_limit loose
1178 setup_negotiation_tip () {
1179 SERVER="$1"
1180 URL="$2"
1181 USE_PROTOCOL_V2="$3"
1183 rm -rf "$SERVER" client trace &&
1184 git init -b main "$SERVER" &&
1185 test_commit -C "$SERVER" alpha_1 &&
1186 test_commit -C "$SERVER" alpha_2 &&
1187 git -C "$SERVER" checkout --orphan beta &&
1188 test_commit -C "$SERVER" beta_1 &&
1189 test_commit -C "$SERVER" beta_2 &&
1191 git clone "$URL" client &&
1193 if test "$USE_PROTOCOL_V2" -eq 1
1194 then
1195 git -C "$SERVER" config protocol.version 2 &&
1196 git -C client config protocol.version 2
1197 fi &&
1199 test_commit -C "$SERVER" beta_s &&
1200 git -C "$SERVER" checkout main &&
1201 test_commit -C "$SERVER" alpha_s &&
1202 git -C "$SERVER" tag -d alpha_1 alpha_2 beta_1 beta_2
1205 check_negotiation_tip () {
1206 # Ensure that {alpha,beta}_1 are sent as "have", but not {alpha_beta}_2
1207 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1208 grep "fetch> have $ALPHA_1" trace &&
1209 BETA_1=$(git -C client rev-parse beta_1) &&
1210 grep "fetch> have $BETA_1" trace &&
1211 ALPHA_2=$(git -C client rev-parse alpha_2) &&
1212 ! grep "fetch> have $ALPHA_2" trace &&
1213 BETA_2=$(git -C client rev-parse beta_2) &&
1214 ! grep "fetch> have $BETA_2" trace
1217 test_expect_success '--negotiation-tip limits "have" lines sent' '
1218 setup_negotiation_tip server server 0 &&
1219 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1220 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1221 origin alpha_s beta_s &&
1222 check_negotiation_tip
1225 test_expect_success '--negotiation-tip understands globs' '
1226 setup_negotiation_tip server server 0 &&
1227 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1228 --negotiation-tip=*_1 \
1229 origin alpha_s beta_s &&
1230 check_negotiation_tip
1233 test_expect_success '--negotiation-tip understands abbreviated SHA-1' '
1234 setup_negotiation_tip server server 0 &&
1235 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1236 --negotiation-tip=$(git -C client rev-parse --short alpha_1) \
1237 --negotiation-tip=$(git -C client rev-parse --short beta_1) \
1238 origin alpha_s beta_s &&
1239 check_negotiation_tip
1242 test_expect_success '--negotiation-tip rejects missing OIDs' '
1243 setup_negotiation_tip server server 0 &&
1244 test_must_fail git -C client fetch \
1245 --negotiation-tip=alpha_1 \
1246 --negotiation-tip=$(test_oid zero) \
1247 origin alpha_s beta_s 2>err &&
1248 cat >fatal-expect <<-EOF &&
1249 fatal: the object $(test_oid zero) does not exist
1251 grep fatal: err >fatal-actual &&
1252 test_cmp fatal-expect fatal-actual
1255 test_expect_success SYMLINKS 'clone does not get confused by a D/F conflict' '
1256 git init df-conflict &&
1258 cd df-conflict &&
1259 ln -s .git a &&
1260 git add a &&
1261 test_tick &&
1262 git commit -m symlink &&
1263 test_commit a- &&
1264 rm a &&
1265 mkdir -p a/hooks &&
1266 write_script a/hooks/post-checkout <<-EOF &&
1267 echo WHOOPSIE >&2
1268 echo whoopsie >"$TRASH_DIRECTORY"/whoops
1270 git add a/hooks/post-checkout &&
1271 test_tick &&
1272 git commit -m post-checkout
1273 ) &&
1274 git clone df-conflict clone 2>err &&
1275 test_grep ! WHOOPS err &&
1276 test_path_is_missing whoops
1279 . "$TEST_DIRECTORY"/lib-httpd.sh
1280 start_httpd
1282 test_expect_success '--negotiation-tip limits "have" lines sent with HTTP protocol v2' '
1283 setup_negotiation_tip "$HTTPD_DOCUMENT_ROOT_PATH/server" \
1284 "$HTTPD_URL/smart/server" 1 &&
1285 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1286 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1287 origin alpha_s beta_s &&
1288 check_negotiation_tip
1291 # DO NOT add non-httpd-specific tests here, because the last part of this
1292 # test script is only executed when httpd is available and enabled.
1294 test_done