t7800: improve test descriptions with empty arguments
[alt-git.git] / t / t5510-fetch.sh
blob91e87b1dd3a5dceb8af2ab614e1d968866dc8b28
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 : this will prevent --prune from locking packed-refs for deleting refs, but adding loose refs still succeeds &&
173 >.git/packed-refs.new &&
175 test_must_fail git fetch --prune origin
178 test_expect_success 'fetch --atomic works with a single branch' '
179 test_when_finished "rm -rf \"$D\"/atomic" &&
181 cd "$D" &&
182 git clone . atomic &&
183 git branch atomic-branch &&
184 oid=$(git rev-parse atomic-branch) &&
185 echo "$oid" >expected &&
187 git -C atomic fetch --atomic origin &&
188 git -C atomic rev-parse origin/atomic-branch >actual &&
189 test_cmp expected actual &&
190 test $oid = "$(git -C atomic rev-parse --verify FETCH_HEAD)"
193 test_expect_success 'fetch --atomic works with multiple branches' '
194 test_when_finished "rm -rf \"$D\"/atomic" &&
196 cd "$D" &&
197 git clone . atomic &&
198 git branch atomic-branch-1 &&
199 git branch atomic-branch-2 &&
200 git branch atomic-branch-3 &&
201 git rev-parse refs/heads/atomic-branch-1 refs/heads/atomic-branch-2 refs/heads/atomic-branch-3 >actual &&
203 git -C atomic fetch --atomic origin &&
204 git -C atomic rev-parse refs/remotes/origin/atomic-branch-1 refs/remotes/origin/atomic-branch-2 refs/remotes/origin/atomic-branch-3 >expected &&
205 test_cmp expected actual
208 test_expect_success 'fetch --atomic works with mixed branches and tags' '
209 test_when_finished "rm -rf \"$D\"/atomic" &&
211 cd "$D" &&
212 git clone . atomic &&
213 git branch atomic-mixed-branch &&
214 git tag atomic-mixed-tag &&
215 git rev-parse refs/heads/atomic-mixed-branch refs/tags/atomic-mixed-tag >actual &&
217 git -C atomic fetch --tags --atomic origin &&
218 git -C atomic rev-parse refs/remotes/origin/atomic-mixed-branch refs/tags/atomic-mixed-tag >expected &&
219 test_cmp expected actual
222 test_expect_success 'fetch --atomic prunes references' '
223 test_when_finished "rm -rf \"$D\"/atomic" &&
225 cd "$D" &&
226 git branch atomic-prune-delete &&
227 git clone . atomic &&
228 git branch --delete atomic-prune-delete &&
229 git branch atomic-prune-create &&
230 git rev-parse refs/heads/atomic-prune-create >actual &&
232 git -C atomic fetch --prune --atomic origin &&
233 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-prune-delete &&
234 git -C atomic rev-parse refs/remotes/origin/atomic-prune-create >expected &&
235 test_cmp expected actual
238 test_expect_success 'fetch --atomic aborts with non-fast-forward update' '
239 test_when_finished "rm -rf \"$D\"/atomic" &&
241 cd "$D" &&
242 git branch atomic-non-ff &&
243 git clone . atomic &&
244 git rev-parse HEAD >actual &&
246 git branch atomic-new-branch &&
247 parent_commit=$(git rev-parse atomic-non-ff~) &&
248 git update-ref refs/heads/atomic-non-ff $parent_commit &&
250 test_must_fail git -C atomic fetch --atomic origin refs/heads/*:refs/remotes/origin/* &&
251 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-new-branch &&
252 git -C atomic rev-parse refs/remotes/origin/atomic-non-ff >expected &&
253 test_cmp expected actual &&
254 test_must_be_empty atomic/.git/FETCH_HEAD
257 test_expect_success 'fetch --atomic executes a single reference transaction only' '
258 test_when_finished "rm -rf \"$D\"/atomic" &&
260 cd "$D" &&
261 git clone . atomic &&
262 git branch atomic-hooks-1 &&
263 git branch atomic-hooks-2 &&
264 head_oid=$(git rev-parse HEAD) &&
266 cat >expected <<-EOF &&
267 prepared
268 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
269 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
270 committed
271 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
272 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
275 rm -f atomic/actual &&
276 test_hook -C atomic reference-transaction <<-\EOF &&
277 ( echo "$*" && cat ) >>actual
280 git -C atomic fetch --atomic origin &&
281 test_cmp expected atomic/actual
284 test_expect_success 'fetch --atomic aborts all reference updates if hook aborts' '
285 test_when_finished "rm -rf \"$D\"/atomic" &&
287 cd "$D" &&
288 git clone . atomic &&
289 git branch atomic-hooks-abort-1 &&
290 git branch atomic-hooks-abort-2 &&
291 git branch atomic-hooks-abort-3 &&
292 git tag atomic-hooks-abort &&
293 head_oid=$(git rev-parse HEAD) &&
295 cat >expected <<-EOF &&
296 prepared
297 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
298 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
299 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
300 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
301 aborted
302 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
303 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
304 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
305 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
308 rm -f atomic/actual &&
309 test_hook -C atomic/.git reference-transaction <<-\EOF &&
310 ( echo "$*" && cat ) >>actual
311 exit 1
314 git -C atomic for-each-ref >expected-refs &&
315 test_must_fail git -C atomic fetch --tags --atomic origin &&
316 git -C atomic for-each-ref >actual-refs &&
317 test_cmp expected-refs actual-refs &&
318 test_must_be_empty atomic/.git/FETCH_HEAD
321 test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' '
322 test_when_finished "rm -rf \"$D\"/atomic" &&
324 cd "$D" &&
325 git clone . atomic &&
326 oid=$(git rev-parse HEAD) &&
328 git branch atomic-fetch-head-1 &&
329 git -C atomic fetch --atomic origin atomic-fetch-head-1 &&
330 test_line_count = 1 atomic/.git/FETCH_HEAD &&
332 git branch atomic-fetch-head-2 &&
333 git -C atomic fetch --atomic --append origin atomic-fetch-head-2 &&
334 test_line_count = 2 atomic/.git/FETCH_HEAD &&
335 cp atomic/.git/FETCH_HEAD expected &&
337 test_hook -C atomic reference-transaction <<-\EOF &&
338 exit 1
341 git branch atomic-fetch-head-3 &&
342 test_must_fail git -C atomic fetch --atomic --append origin atomic-fetch-head-3 &&
343 test_cmp expected atomic/.git/FETCH_HEAD
346 test_expect_success '--refmap="" ignores configured refspec' '
347 cd "$TRASH_DIRECTORY" &&
348 git clone "$D" remote-refs &&
349 git -C remote-refs rev-parse remotes/origin/main >old &&
350 git -C remote-refs update-ref refs/remotes/origin/main main~1 &&
351 git -C remote-refs rev-parse remotes/origin/main >new &&
352 git -C remote-refs fetch --refmap= origin "+refs/heads/*:refs/hidden/origin/*" &&
353 git -C remote-refs rev-parse remotes/origin/main >actual &&
354 test_cmp new actual &&
355 git -C remote-refs fetch origin &&
356 git -C remote-refs rev-parse remotes/origin/main >actual &&
357 test_cmp old actual
360 test_expect_success '--refmap="" and --prune' '
361 git -C remote-refs update-ref refs/remotes/origin/foo/otherbranch main &&
362 git -C remote-refs update-ref refs/hidden/foo/otherbranch main &&
363 git -C remote-refs fetch --prune --refmap="" origin +refs/heads/*:refs/hidden/* &&
364 git -C remote-refs rev-parse remotes/origin/foo/otherbranch &&
365 test_must_fail git -C remote-refs rev-parse refs/hidden/foo/otherbranch &&
366 git -C remote-refs fetch --prune origin &&
367 test_must_fail git -C remote-refs rev-parse remotes/origin/foo/otherbranch
370 test_expect_success 'fetch tags when there is no tags' '
372 cd "$D" &&
374 mkdir notags &&
375 cd notags &&
376 git init &&
378 git fetch -t ..
382 test_expect_success 'fetch following tags' '
384 cd "$D" &&
385 git tag -a -m "annotated" anno HEAD &&
386 git tag light HEAD &&
388 mkdir four &&
389 cd four &&
390 git init &&
392 git fetch .. :track &&
393 git show-ref --verify refs/tags/anno &&
394 git show-ref --verify refs/tags/light
398 test_expect_success 'fetch uses remote ref names to describe new refs' '
399 cd "$D" &&
400 git init descriptive &&
402 cd descriptive &&
403 git config remote.o.url .. &&
404 git config remote.o.fetch "refs/heads/*:refs/crazyheads/*" &&
405 git config --add remote.o.fetch "refs/others/*:refs/heads/*" &&
406 git fetch o
407 ) &&
408 git tag -a -m "Descriptive tag" descriptive-tag &&
409 git branch descriptive-branch &&
410 git checkout descriptive-branch &&
411 echo "Nuts" >crazy &&
412 git add crazy &&
413 git commit -a -m "descriptive commit" &&
414 git update-ref refs/others/crazy HEAD &&
416 cd descriptive &&
417 git fetch o 2>actual &&
418 test_grep "new branch.* -> refs/crazyheads/descriptive-branch$" actual &&
419 test_grep "new tag.* -> descriptive-tag$" actual &&
420 test_grep "new ref.* -> crazy$" actual
421 ) &&
422 git checkout main
425 test_expect_success 'fetch must not resolve short tag name' '
427 cd "$D" &&
429 mkdir five &&
430 cd five &&
431 git init &&
433 test_must_fail git fetch .. anno:five
437 test_expect_success 'fetch can now resolve short remote name' '
439 cd "$D" &&
440 git update-ref refs/remotes/six/HEAD HEAD &&
442 mkdir six &&
443 cd six &&
444 git init &&
446 git fetch .. six:six
449 test_expect_success 'create bundle 1' '
450 cd "$D" &&
451 echo >file updated again by origin &&
452 git commit -a -m "tip" &&
453 git bundle create --version=3 bundle1 main^..main
456 test_expect_success 'header of bundle looks right' '
457 cat >expect <<-EOF &&
458 # v3 git bundle
459 @object-format=$(test_oid algo)
460 -OID updated by origin
461 OID refs/heads/main
464 sed -e "s/$OID_REGEX/OID/g" -e "5q" "$D"/bundle1 >actual &&
465 test_cmp expect actual
468 test_expect_success 'create bundle 2' '
469 cd "$D" &&
470 git bundle create bundle2 main~2..main
473 test_expect_success 'unbundle 1' '
474 cd "$D/bundle" &&
475 git checkout -b some-branch &&
476 test_must_fail git fetch "$D/bundle1" main:main
480 test_expect_success 'bundle 1 has only 3 files ' '
481 cd "$D" &&
482 test_bundle_object_count bundle1 3
485 test_expect_success 'unbundle 2' '
486 cd "$D/bundle" &&
487 git fetch ../bundle2 main:main &&
488 test "tip" = "$(git log -1 --pretty=oneline main | cut -d" " -f2)"
491 test_expect_success 'bundle does not prerequisite objects' '
492 cd "$D" &&
493 touch file2 &&
494 git add file2 &&
495 git commit -m add.file2 file2 &&
496 git bundle create bundle3 -1 HEAD &&
497 test_bundle_object_count bundle3 3
500 test_expect_success 'bundle should be able to create a full history' '
502 cd "$D" &&
503 git tag -a -m "1.0" v1.0 main &&
504 git bundle create bundle4 v1.0
508 test_expect_success 'fetch with a non-applying branch.<name>.merge' '
509 git config branch.main.remote yeti &&
510 git config branch.main.merge refs/heads/bigfoot &&
511 git config remote.blub.url one &&
512 git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
513 git fetch blub
516 # URL supplied to fetch does not match the url of the configured branch's remote
517 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
518 one_head=$(cd one && git rev-parse HEAD) &&
519 this_head=$(git rev-parse HEAD) &&
520 git update-ref -d FETCH_HEAD &&
521 git fetch one &&
522 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
523 test $this_head = "$(git rev-parse --verify HEAD)"
526 # URL supplied to fetch matches the url of the configured branch's remote and
527 # the merge spec matches the branch the remote HEAD points to
528 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
529 one_ref=$(cd one && git symbolic-ref HEAD) &&
530 git config branch.main.remote blub &&
531 git config branch.main.merge "$one_ref" &&
532 git update-ref -d FETCH_HEAD &&
533 git fetch one &&
534 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
535 test $this_head = "$(git rev-parse --verify HEAD)"
538 # URL supplied to fetch matches the url of the configured branch's remote, but
539 # the merge spec does not match the branch the remote HEAD points to
540 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
541 git config branch.main.merge "${one_ref}_not" &&
542 git update-ref -d FETCH_HEAD &&
543 git fetch one &&
544 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
545 test $this_head = "$(git rev-parse --verify HEAD)"
548 # the strange name is: a\!'b
549 test_expect_success 'quoting of a strangely named repo' '
550 test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
551 grep "fatal: '\''a\\\\!'\''b'\''" result
554 test_expect_success 'bundle should record HEAD correctly' '
556 cd "$D" &&
557 git bundle create bundle5 HEAD main &&
558 git bundle list-heads bundle5 >actual &&
559 for h in HEAD refs/heads/main
561 echo "$(git rev-parse --verify $h) $h" || return 1
562 done >expect &&
563 test_cmp expect actual
567 test_expect_success 'mark initial state of origin/main' '
569 cd three &&
570 git tag base-origin-main refs/remotes/origin/main
574 test_expect_success 'explicit fetch should update tracking' '
576 cd "$D" &&
577 git branch -f side &&
579 cd three &&
580 git update-ref refs/remotes/origin/main base-origin-main &&
581 o=$(git rev-parse --verify refs/remotes/origin/main) &&
582 git fetch origin main &&
583 n=$(git rev-parse --verify refs/remotes/origin/main) &&
584 test "$o" != "$n" &&
585 test_must_fail git rev-parse --verify refs/remotes/origin/side
589 test_expect_success 'explicit pull should update tracking' '
591 cd "$D" &&
592 git branch -f side &&
594 cd three &&
595 git update-ref refs/remotes/origin/main base-origin-main &&
596 o=$(git rev-parse --verify refs/remotes/origin/main) &&
597 git pull origin main &&
598 n=$(git rev-parse --verify refs/remotes/origin/main) &&
599 test "$o" != "$n" &&
600 test_must_fail git rev-parse --verify refs/remotes/origin/side
604 test_expect_success 'explicit --refmap is allowed only with command-line refspec' '
605 cd "$D" &&
607 cd three &&
608 test_must_fail git fetch --refmap="*:refs/remotes/none/*"
612 test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
613 cd "$D" &&
614 git branch -f side &&
616 cd three &&
617 git update-ref refs/remotes/origin/main base-origin-main &&
618 o=$(git rev-parse --verify refs/remotes/origin/main) &&
619 git fetch --refmap="refs/heads/*:refs/remotes/other/*" origin main &&
620 n=$(git rev-parse --verify refs/remotes/origin/main) &&
621 test "$o" = "$n" &&
622 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
623 git rev-parse --verify refs/remotes/other/main
627 test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
628 cd "$D" &&
629 git branch -f side &&
631 cd three &&
632 git update-ref refs/remotes/origin/main base-origin-main &&
633 o=$(git rev-parse --verify refs/remotes/origin/main) &&
634 git fetch --refmap="" origin main &&
635 n=$(git rev-parse --verify refs/remotes/origin/main) &&
636 test "$o" = "$n" &&
637 test_must_fail git rev-parse --verify refs/remotes/origin/side
641 test_expect_success 'configured fetch updates tracking' '
643 cd "$D" &&
644 git branch -f side &&
646 cd three &&
647 git update-ref refs/remotes/origin/main base-origin-main &&
648 o=$(git rev-parse --verify refs/remotes/origin/main) &&
649 git fetch origin &&
650 n=$(git rev-parse --verify refs/remotes/origin/main) &&
651 test "$o" != "$n" &&
652 git rev-parse --verify refs/remotes/origin/side
656 test_expect_success 'non-matching refspecs do not confuse tracking update' '
657 cd "$D" &&
658 git update-ref refs/odd/location HEAD &&
660 cd three &&
661 git update-ref refs/remotes/origin/main base-origin-main &&
662 git config --add remote.origin.fetch \
663 refs/odd/location:refs/remotes/origin/odd &&
664 o=$(git rev-parse --verify refs/remotes/origin/main) &&
665 git fetch origin main &&
666 n=$(git rev-parse --verify refs/remotes/origin/main) &&
667 test "$o" != "$n" &&
668 test_must_fail git rev-parse --verify refs/remotes/origin/odd
672 test_expect_success 'pushing nonexistent branch by mistake should not segv' '
674 cd "$D" &&
675 test_must_fail git push seven no:no
679 test_expect_success 'auto tag following fetches minimum' '
681 cd "$D" &&
682 git clone .git follow &&
683 git checkout HEAD^0 &&
685 for i in 1 2 3 4 5 6 7
687 echo $i >>file &&
688 git commit -m $i -a &&
689 git tag -a -m $i excess-$i || exit 1
690 done
691 ) &&
692 git checkout main &&
694 cd follow &&
695 git fetch
699 test_expect_success 'refuse to fetch into the current branch' '
701 test_must_fail git fetch . side:main
705 test_expect_success 'fetch into the current branch with --update-head-ok' '
707 git fetch --update-head-ok . side:main
711 test_expect_success 'fetch --dry-run does not touch FETCH_HEAD, but still prints what would be written' '
712 rm -f .git/FETCH_HEAD err &&
713 git fetch --dry-run . 2>err &&
714 ! test -f .git/FETCH_HEAD &&
715 grep FETCH_HEAD err
718 test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD, and does not print what would be written' '
719 rm -f .git/FETCH_HEAD err &&
720 git fetch --no-write-fetch-head . 2>err &&
721 ! test -f .git/FETCH_HEAD &&
722 ! grep FETCH_HEAD err
725 test_expect_success '--write-fetch-head gets defeated by --dry-run' '
726 rm -f .git/FETCH_HEAD &&
727 git fetch --dry-run --write-fetch-head . &&
728 ! test -f .git/FETCH_HEAD
731 test_expect_success "should be able to fetch with duplicate refspecs" '
732 mkdir dups &&
734 cd dups &&
735 git init &&
736 git config branch.main.remote three &&
737 git config remote.three.url ../three/.git &&
738 git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
739 git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
740 git fetch three
744 test_expect_success 'LHS of refspec follows ref disambiguation rules' '
745 mkdir lhs-ambiguous &&
747 cd lhs-ambiguous &&
748 git init server &&
749 test_commit -C server unwanted &&
750 test_commit -C server wanted &&
752 git init client &&
754 # Check a name coming after "refs" alphabetically ...
755 git -C server update-ref refs/heads/s wanted &&
756 git -C server update-ref refs/heads/refs/heads/s unwanted &&
757 git -C client fetch ../server +refs/heads/s:refs/heads/checkthis &&
758 git -C server rev-parse wanted >expect &&
759 git -C client rev-parse checkthis >actual &&
760 test_cmp expect actual &&
762 # ... and one before.
763 git -C server update-ref refs/heads/q wanted &&
764 git -C server update-ref refs/heads/refs/heads/q unwanted &&
765 git -C client fetch ../server +refs/heads/q:refs/heads/checkthis &&
766 git -C server rev-parse wanted >expect &&
767 git -C client rev-parse checkthis >actual &&
768 test_cmp expect actual &&
770 # Tags are preferred over branches like refs/{heads,tags}/*
771 git -C server update-ref refs/tags/t wanted &&
772 git -C server update-ref refs/heads/t unwanted &&
773 git -C client fetch ../server +t:refs/heads/checkthis &&
774 git -C server rev-parse wanted >expect &&
775 git -C client rev-parse checkthis >actual
779 test_expect_success 'fetch.writeCommitGraph' '
780 git clone three write &&
782 cd three &&
783 test_commit new
784 ) &&
786 cd write &&
787 git -c fetch.writeCommitGraph fetch origin &&
788 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
792 test_expect_success 'fetch.writeCommitGraph with submodules' '
793 test_config_global protocol.file.allow always &&
794 git clone dups super &&
796 cd super &&
797 git submodule add "file://$TRASH_DIRECTORY/three" &&
798 git commit -m "add submodule"
799 ) &&
800 git clone "super" super-clone &&
802 cd super-clone &&
803 rm -rf .git/objects/info &&
804 git -c fetch.writeCommitGraph=true fetch origin &&
805 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain &&
806 git -c fetch.writeCommitGraph=true fetch --recurse-submodules origin
810 # fetches from first configured url
811 test_expect_success 'fetch from multiple configured URLs in single remote' '
812 git init url1 &&
813 git remote add multipleurls url1 &&
814 git remote set-url --add multipleurls url2 &&
815 git fetch multipleurls
818 # configured prune tests
820 set_config_tristate () {
821 # var=$1 val=$2
822 case "$2" in
823 unset)
824 test_unconfig "$1"
827 git config "$1" "$2"
828 key=$(echo $1 | sed -e 's/^remote\.origin/fetch/')
829 git_fetch_c="$git_fetch_c -c $key=$2"
831 esac
834 test_configured_prune () {
835 test_configured_prune_type "$@" "name"
836 test_configured_prune_type "$@" "link"
839 test_configured_prune_type () {
840 fetch_prune=$1
841 remote_origin_prune=$2
842 fetch_prune_tags=$3
843 remote_origin_prune_tags=$4
844 expected_branch=$5
845 expected_tag=$6
846 cmdline=$7
847 mode=$8
849 if test -z "$cmdline_setup"
850 then
851 test_expect_success 'setup cmdline_setup variable for subsequent test' '
852 remote_url="file://$(git -C one config remote.origin.url)" &&
853 remote_fetch="$(git -C one config remote.origin.fetch)" &&
854 cmdline_setup="\"$remote_url\" \"$remote_fetch\""
858 if test "$mode" = 'link'
859 then
860 new_cmdline=""
862 if test "$cmdline" = ""
863 then
864 new_cmdline=$cmdline_setup
865 else
866 new_cmdline=$(perl -e '
867 my ($cmdline, $url) = @ARGV;
868 $cmdline =~ s[origin(?!/)][quotemeta($url)]ge;
869 print $cmdline;
870 ' -- "$cmdline" "$remote_url")
873 if test "$fetch_prune_tags" = 'true' ||
874 test "$remote_origin_prune_tags" = 'true'
875 then
876 if ! printf '%s' "$cmdline\n" | grep -q refs/remotes/origin/
877 then
878 new_cmdline="$new_cmdline refs/tags/*:refs/tags/*"
882 cmdline="$new_cmdline"
885 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" '
886 # make sure a newbranch is there in . and also in one
887 git branch -f newbranch &&
888 git tag -f newtag &&
890 cd one &&
891 test_unconfig fetch.prune &&
892 test_unconfig fetch.pruneTags &&
893 test_unconfig remote.origin.prune &&
894 test_unconfig remote.origin.pruneTags &&
895 git fetch '"$cmdline_setup"' &&
896 git rev-parse --verify refs/remotes/origin/newbranch &&
897 git rev-parse --verify refs/tags/newtag
898 ) &&
900 # now remove them
901 git branch -d newbranch &&
902 git tag -d newtag &&
904 # then test
906 cd one &&
907 git_fetch_c="" &&
908 set_config_tristate fetch.prune $fetch_prune &&
909 set_config_tristate fetch.pruneTags $fetch_prune_tags &&
910 set_config_tristate remote.origin.prune $remote_origin_prune &&
911 set_config_tristate remote.origin.pruneTags $remote_origin_prune_tags &&
913 if test "$mode" != "link"
914 then
915 git_fetch_c=""
916 fi &&
917 git$git_fetch_c fetch '"$cmdline"' &&
918 case "$expected_branch" in
919 pruned)
920 test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
922 kept)
923 git rev-parse --verify refs/remotes/origin/newbranch
925 esac &&
926 case "$expected_tag" in
927 pruned)
928 test_must_fail git rev-parse --verify refs/tags/newtag
930 kept)
931 git rev-parse --verify refs/tags/newtag
933 esac
938 # $1 config: fetch.prune
939 # $2 config: remote.<name>.prune
940 # $3 config: fetch.pruneTags
941 # $4 config: remote.<name>.pruneTags
942 # $5 expect: branch to be pruned?
943 # $6 expect: tag to be pruned?
944 # $7 git-fetch $cmdline:
946 # $1 $2 $3 $4 $5 $6 $7
947 test_configured_prune unset unset unset unset kept kept ""
948 test_configured_prune unset unset unset unset kept kept "--no-prune"
949 test_configured_prune unset unset unset unset pruned kept "--prune"
950 test_configured_prune unset unset unset unset kept pruned \
951 "--prune origin refs/tags/*:refs/tags/*"
952 test_configured_prune unset unset unset unset pruned pruned \
953 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
955 test_configured_prune false unset unset unset kept kept ""
956 test_configured_prune false unset unset unset kept kept "--no-prune"
957 test_configured_prune false unset unset unset pruned kept "--prune"
959 test_configured_prune true unset unset unset pruned kept ""
960 test_configured_prune true unset unset unset pruned kept "--prune"
961 test_configured_prune true unset unset unset kept kept "--no-prune"
963 test_configured_prune unset false unset unset kept kept ""
964 test_configured_prune unset false unset unset kept kept "--no-prune"
965 test_configured_prune unset false unset unset pruned kept "--prune"
967 test_configured_prune false false unset unset kept kept ""
968 test_configured_prune false false unset unset kept kept "--no-prune"
969 test_configured_prune false false unset unset pruned kept "--prune"
970 test_configured_prune false false unset unset kept pruned \
971 "--prune origin refs/tags/*:refs/tags/*"
972 test_configured_prune false false unset unset pruned pruned \
973 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
975 test_configured_prune true false unset unset kept kept ""
976 test_configured_prune true false unset unset pruned kept "--prune"
977 test_configured_prune true false unset unset kept kept "--no-prune"
979 test_configured_prune unset true unset unset pruned kept ""
980 test_configured_prune unset true unset unset kept kept "--no-prune"
981 test_configured_prune unset true unset unset pruned kept "--prune"
983 test_configured_prune false true unset unset pruned kept ""
984 test_configured_prune false true unset unset kept kept "--no-prune"
985 test_configured_prune false true unset unset pruned kept "--prune"
987 test_configured_prune true true unset unset pruned kept ""
988 test_configured_prune true true unset unset pruned kept "--prune"
989 test_configured_prune true true unset unset kept kept "--no-prune"
990 test_configured_prune true true unset unset kept pruned \
991 "--prune origin refs/tags/*:refs/tags/*"
992 test_configured_prune true true unset unset pruned pruned \
993 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
995 # --prune-tags on its own does nothing, needs --prune as well, same
996 # for fetch.pruneTags without fetch.prune
997 test_configured_prune unset unset unset unset kept kept "--prune-tags"
998 test_configured_prune unset unset true unset kept kept ""
999 test_configured_prune unset unset unset true kept kept ""
1001 # These will prune the tags
1002 test_configured_prune unset unset unset unset pruned pruned "--prune --prune-tags"
1003 test_configured_prune true unset true unset pruned pruned ""
1004 test_configured_prune unset true unset true pruned pruned ""
1006 # remote.<name>.pruneTags overrides fetch.pruneTags, just like
1007 # remote.<name>.prune overrides fetch.prune if set.
1008 test_configured_prune true unset true unset pruned pruned ""
1009 test_configured_prune false true false true pruned pruned ""
1010 test_configured_prune true false true false kept kept ""
1012 # When --prune-tags is supplied it's ignored if an explicit refspec is
1013 # given, same for the configuration options.
1014 test_configured_prune unset unset unset unset pruned kept \
1015 "--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"
1016 test_configured_prune unset unset true unset pruned kept \
1017 "--prune origin +refs/heads/*:refs/remotes/origin/*"
1018 test_configured_prune unset unset unset true pruned kept \
1019 "--prune origin +refs/heads/*:refs/remotes/origin/*"
1021 # Pruning that also takes place if a file:// url replaces a named
1022 # remote. However, because there's no implicit
1023 # +refs/heads/*:refs/remotes/origin/* refspec and supplying it on the
1024 # command-line negates --prune-tags, the branches will not be pruned.
1025 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "name"
1026 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "link"
1027 test_configured_prune_type unset unset unset unset pruned pruned "origin --prune --prune-tags" "name"
1028 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1029 test_configured_prune_type unset unset unset unset pruned pruned "--prune --prune-tags origin" "name"
1030 test_configured_prune_type unset unset unset unset kept pruned "--prune --prune-tags origin" "link"
1031 test_configured_prune_type unset unset true unset pruned pruned "--prune origin" "name"
1032 test_configured_prune_type unset unset true unset kept pruned "--prune origin" "link"
1033 test_configured_prune_type unset unset unset true pruned pruned "--prune origin" "name"
1034 test_configured_prune_type unset unset unset true kept pruned "--prune origin" "link"
1035 test_configured_prune_type true unset true unset pruned pruned "origin" "name"
1036 test_configured_prune_type true unset true unset kept pruned "origin" "link"
1037 test_configured_prune_type unset true true unset pruned pruned "origin" "name"
1038 test_configured_prune_type unset true true unset kept pruned "origin" "link"
1039 test_configured_prune_type unset true unset true pruned pruned "origin" "name"
1040 test_configured_prune_type unset true unset true kept pruned "origin" "link"
1042 # When all remote.origin.fetch settings are deleted a --prune
1043 # --prune-tags still implicitly supplies refs/tags/*:refs/tags/* so
1044 # tags, but not tracking branches, will be deleted.
1045 test_expect_success 'remove remote.origin.fetch "one"' '
1047 cd one &&
1048 git config --unset-all remote.origin.fetch
1051 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "name"
1052 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1054 test_expect_success 'all boundary commits are excluded' '
1055 test_commit base &&
1056 test_commit oneside &&
1057 git checkout HEAD^ &&
1058 test_commit otherside &&
1059 git checkout main &&
1060 test_tick &&
1061 git merge otherside &&
1062 ad=$(git log --no-walk --format=%ad HEAD) &&
1063 git bundle create twoside-boundary.bdl main --since="$ad" &&
1064 test_bundle_object_count --thin twoside-boundary.bdl 3
1067 test_expect_success 'fetch --prune prints the remotes url' '
1068 git branch goodbye &&
1069 git clone . only-prunes &&
1070 git branch -D goodbye &&
1072 cd only-prunes &&
1073 git fetch --prune origin 2>&1 | head -n1 >../actual
1074 ) &&
1075 echo "From ${D}/." >expect &&
1076 test_cmp expect actual
1079 test_expect_success 'branchname D/F conflict resolved by --prune' '
1080 git branch dir/file &&
1081 git clone . prune-df-conflict &&
1082 git branch -D dir/file &&
1083 git branch dir &&
1085 cd prune-df-conflict &&
1086 git fetch --prune &&
1087 git rev-parse origin/dir >../actual
1088 ) &&
1089 git rev-parse dir >expect &&
1090 test_cmp expect actual
1093 test_expect_success 'fetching a one-level ref works' '
1094 test_commit extra &&
1095 git reset --hard HEAD^ &&
1096 git update-ref refs/foo extra &&
1097 git init one-level &&
1099 cd one-level &&
1100 git fetch .. HEAD refs/foo
1104 test_expect_success 'fetching with auto-gc does not lock up' '
1105 write_script askyesno <<-\EOF &&
1106 echo "$*" &&
1107 false
1109 git clone "file://$D" auto-gc &&
1110 test_commit test2 &&
1112 cd auto-gc &&
1113 git config fetch.unpackLimit 1 &&
1114 git config gc.autoPackLimit 1 &&
1115 git config gc.autoDetach false &&
1116 GIT_ASK_YESNO="$D/askyesno" git fetch --verbose >fetch.out 2>&1 &&
1117 test_grep "Auto packing the repository" fetch.out &&
1118 ! grep "Should I try again" fetch.out
1122 for section in fetch transfer
1124 test_expect_success "$section.hideRefs affects connectivity check" '
1125 GIT_TRACE="$PWD"/trace git -c $section.hideRefs=refs -c \
1126 $section.hideRefs="!refs/tags/" fetch &&
1127 grep "git rev-list .*--exclude-hidden=fetch" trace
1129 done
1131 test_expect_success 'prepare source branch' '
1132 echo one >onebranch &&
1133 git checkout --orphan onebranch &&
1134 git rm --cached -r . &&
1135 git add onebranch &&
1136 git commit -m onebranch &&
1137 git rev-list --objects onebranch -- >actual &&
1138 # 3 objects should be created, at least ...
1139 test 3 -le $(wc -l <actual)
1142 validate_store_type () {
1143 git -C dest count-objects -v >actual &&
1144 case "$store_type" in
1145 packed)
1146 grep "^count: 0$" actual ;;
1147 loose)
1148 grep "^packs: 0$" actual ;;
1149 esac || {
1150 echo "store_type is $store_type"
1151 cat actual
1152 false
1156 test_unpack_limit () {
1157 store_type=$1
1159 case "$store_type" in
1160 packed) fetch_limit=1 transfer_limit=10000 ;;
1161 loose) fetch_limit=10000 transfer_limit=1 ;;
1162 esac
1164 test_expect_success "fetch trumps transfer limit" '
1165 rm -fr dest &&
1166 git --bare init dest &&
1167 git -C dest config fetch.unpacklimit $fetch_limit &&
1168 git -C dest config transfer.unpacklimit $transfer_limit &&
1169 git -C dest fetch .. onebranch &&
1170 validate_store_type
1174 test_unpack_limit packed
1175 test_unpack_limit loose
1177 setup_negotiation_tip () {
1178 SERVER="$1"
1179 URL="$2"
1180 USE_PROTOCOL_V2="$3"
1182 rm -rf "$SERVER" client trace &&
1183 git init -b main "$SERVER" &&
1184 test_commit -C "$SERVER" alpha_1 &&
1185 test_commit -C "$SERVER" alpha_2 &&
1186 git -C "$SERVER" checkout --orphan beta &&
1187 test_commit -C "$SERVER" beta_1 &&
1188 test_commit -C "$SERVER" beta_2 &&
1190 git clone "$URL" client &&
1192 if test "$USE_PROTOCOL_V2" -eq 1
1193 then
1194 git -C "$SERVER" config protocol.version 2 &&
1195 git -C client config protocol.version 2
1196 fi &&
1198 test_commit -C "$SERVER" beta_s &&
1199 git -C "$SERVER" checkout main &&
1200 test_commit -C "$SERVER" alpha_s &&
1201 git -C "$SERVER" tag -d alpha_1 alpha_2 beta_1 beta_2
1204 check_negotiation_tip () {
1205 # Ensure that {alpha,beta}_1 are sent as "have", but not {alpha_beta}_2
1206 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1207 grep "fetch> have $ALPHA_1" trace &&
1208 BETA_1=$(git -C client rev-parse beta_1) &&
1209 grep "fetch> have $BETA_1" trace &&
1210 ALPHA_2=$(git -C client rev-parse alpha_2) &&
1211 ! grep "fetch> have $ALPHA_2" trace &&
1212 BETA_2=$(git -C client rev-parse beta_2) &&
1213 ! grep "fetch> have $BETA_2" trace
1216 test_expect_success '--negotiation-tip limits "have" lines sent' '
1217 setup_negotiation_tip server server 0 &&
1218 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1219 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1220 origin alpha_s beta_s &&
1221 check_negotiation_tip
1224 test_expect_success '--negotiation-tip understands globs' '
1225 setup_negotiation_tip server server 0 &&
1226 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1227 --negotiation-tip=*_1 \
1228 origin alpha_s beta_s &&
1229 check_negotiation_tip
1232 test_expect_success '--negotiation-tip understands abbreviated SHA-1' '
1233 setup_negotiation_tip server server 0 &&
1234 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1235 --negotiation-tip=$(git -C client rev-parse --short alpha_1) \
1236 --negotiation-tip=$(git -C client rev-parse --short beta_1) \
1237 origin alpha_s beta_s &&
1238 check_negotiation_tip
1241 test_expect_success '--negotiation-tip rejects missing OIDs' '
1242 setup_negotiation_tip server server 0 &&
1243 test_must_fail git -C client fetch \
1244 --negotiation-tip=alpha_1 \
1245 --negotiation-tip=$(test_oid zero) \
1246 origin alpha_s beta_s 2>err &&
1247 cat >fatal-expect <<-EOF &&
1248 fatal: the object $(test_oid zero) does not exist
1250 grep fatal: err >fatal-actual &&
1251 test_cmp fatal-expect fatal-actual
1254 . "$TEST_DIRECTORY"/lib-httpd.sh
1255 start_httpd
1257 test_expect_success '--negotiation-tip limits "have" lines sent with HTTP protocol v2' '
1258 setup_negotiation_tip "$HTTPD_DOCUMENT_ROOT_PATH/server" \
1259 "$HTTPD_URL/smart/server" 1 &&
1260 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1261 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1262 origin alpha_s beta_s &&
1263 check_negotiation_tip
1266 # DO NOT add non-httpd-specific tests here, because the last part of this
1267 # test script is only executed when httpd is available and enabled.
1269 test_done