Merge branch 'rs/t4202-invert-grep-test-fix'
[git/debian.git] / t / t5510-fetch.sh
blob20f7110ec108fda462f5e9a0a43221d2c66e7fe1
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 'fetch --atomic works with a single branch' '
168 test_when_finished "rm -rf \"$D\"/atomic" &&
170 cd "$D" &&
171 git clone . atomic &&
172 git branch atomic-branch &&
173 oid=$(git rev-parse atomic-branch) &&
174 echo "$oid" >expected &&
176 git -C atomic fetch --atomic origin &&
177 git -C atomic rev-parse origin/atomic-branch >actual &&
178 test_cmp expected actual &&
179 test $oid = "$(git -C atomic rev-parse --verify FETCH_HEAD)"
182 test_expect_success 'fetch --atomic works with multiple branches' '
183 test_when_finished "rm -rf \"$D\"/atomic" &&
185 cd "$D" &&
186 git clone . atomic &&
187 git branch atomic-branch-1 &&
188 git branch atomic-branch-2 &&
189 git branch atomic-branch-3 &&
190 git rev-parse refs/heads/atomic-branch-1 refs/heads/atomic-branch-2 refs/heads/atomic-branch-3 >actual &&
192 git -C atomic fetch --atomic origin &&
193 git -C atomic rev-parse refs/remotes/origin/atomic-branch-1 refs/remotes/origin/atomic-branch-2 refs/remotes/origin/atomic-branch-3 >expected &&
194 test_cmp expected actual
197 test_expect_success 'fetch --atomic works with mixed branches and tags' '
198 test_when_finished "rm -rf \"$D\"/atomic" &&
200 cd "$D" &&
201 git clone . atomic &&
202 git branch atomic-mixed-branch &&
203 git tag atomic-mixed-tag &&
204 git rev-parse refs/heads/atomic-mixed-branch refs/tags/atomic-mixed-tag >actual &&
206 git -C atomic fetch --tags --atomic origin &&
207 git -C atomic rev-parse refs/remotes/origin/atomic-mixed-branch refs/tags/atomic-mixed-tag >expected &&
208 test_cmp expected actual
211 test_expect_success 'fetch --atomic prunes references' '
212 test_when_finished "rm -rf \"$D\"/atomic" &&
214 cd "$D" &&
215 git branch atomic-prune-delete &&
216 git clone . atomic &&
217 git branch --delete atomic-prune-delete &&
218 git branch atomic-prune-create &&
219 git rev-parse refs/heads/atomic-prune-create >actual &&
221 git -C atomic fetch --prune --atomic origin &&
222 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-prune-delete &&
223 git -C atomic rev-parse refs/remotes/origin/atomic-prune-create >expected &&
224 test_cmp expected actual
227 test_expect_success 'fetch --atomic aborts with non-fast-forward update' '
228 test_when_finished "rm -rf \"$D\"/atomic" &&
230 cd "$D" &&
231 git branch atomic-non-ff &&
232 git clone . atomic &&
233 git rev-parse HEAD >actual &&
235 git branch atomic-new-branch &&
236 parent_commit=$(git rev-parse atomic-non-ff~) &&
237 git update-ref refs/heads/atomic-non-ff $parent_commit &&
239 test_must_fail git -C atomic fetch --atomic origin refs/heads/*:refs/remotes/origin/* &&
240 test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-new-branch &&
241 git -C atomic rev-parse refs/remotes/origin/atomic-non-ff >expected &&
242 test_cmp expected actual &&
243 test_must_be_empty atomic/.git/FETCH_HEAD
246 test_expect_success 'fetch --atomic executes a single reference transaction only' '
247 test_when_finished "rm -rf \"$D\"/atomic" &&
249 cd "$D" &&
250 git clone . atomic &&
251 git branch atomic-hooks-1 &&
252 git branch atomic-hooks-2 &&
253 head_oid=$(git rev-parse HEAD) &&
255 cat >expected <<-EOF &&
256 prepared
257 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
258 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
259 committed
260 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
261 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
264 rm -f atomic/actual &&
265 write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
266 ( echo "$*" && cat ) >>actual
269 git -C atomic fetch --atomic origin &&
270 test_cmp expected atomic/actual
273 test_expect_success 'fetch --atomic aborts all reference updates if hook aborts' '
274 test_when_finished "rm -rf \"$D\"/atomic" &&
276 cd "$D" &&
277 git clone . atomic &&
278 git branch atomic-hooks-abort-1 &&
279 git branch atomic-hooks-abort-2 &&
280 git branch atomic-hooks-abort-3 &&
281 git tag atomic-hooks-abort &&
282 head_oid=$(git rev-parse HEAD) &&
284 cat >expected <<-EOF &&
285 prepared
286 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
287 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
288 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
289 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
290 aborted
291 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
292 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
293 $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
294 $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
297 rm -f atomic/actual &&
298 write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
299 ( echo "$*" && cat ) >>actual
300 exit 1
303 git -C atomic for-each-ref >expected-refs &&
304 test_must_fail git -C atomic fetch --tags --atomic origin &&
305 git -C atomic for-each-ref >actual-refs &&
306 test_cmp expected-refs actual-refs &&
307 test_must_be_empty atomic/.git/FETCH_HEAD
310 test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' '
311 test_when_finished "rm -rf \"$D\"/atomic" &&
313 cd "$D" &&
314 git clone . atomic &&
315 oid=$(git rev-parse HEAD) &&
317 git branch atomic-fetch-head-1 &&
318 git -C atomic fetch --atomic origin atomic-fetch-head-1 &&
319 test_line_count = 1 atomic/.git/FETCH_HEAD &&
321 git branch atomic-fetch-head-2 &&
322 git -C atomic fetch --atomic --append origin atomic-fetch-head-2 &&
323 test_line_count = 2 atomic/.git/FETCH_HEAD &&
324 cp atomic/.git/FETCH_HEAD expected &&
326 write_script atomic/.git/hooks/reference-transaction <<-\EOF &&
327 exit 1
330 git branch atomic-fetch-head-3 &&
331 test_must_fail git -C atomic fetch --atomic --append origin atomic-fetch-head-3 &&
332 test_cmp expected atomic/.git/FETCH_HEAD
335 test_expect_success '--refmap="" ignores configured refspec' '
336 cd "$TRASH_DIRECTORY" &&
337 git clone "$D" remote-refs &&
338 git -C remote-refs rev-parse remotes/origin/main >old &&
339 git -C remote-refs update-ref refs/remotes/origin/main main~1 &&
340 git -C remote-refs rev-parse remotes/origin/main >new &&
341 git -C remote-refs fetch --refmap= origin "+refs/heads/*:refs/hidden/origin/*" &&
342 git -C remote-refs rev-parse remotes/origin/main >actual &&
343 test_cmp new actual &&
344 git -C remote-refs fetch origin &&
345 git -C remote-refs rev-parse remotes/origin/main >actual &&
346 test_cmp old actual
349 test_expect_success '--refmap="" and --prune' '
350 git -C remote-refs update-ref refs/remotes/origin/foo/otherbranch main &&
351 git -C remote-refs update-ref refs/hidden/foo/otherbranch main &&
352 git -C remote-refs fetch --prune --refmap="" origin +refs/heads/*:refs/hidden/* &&
353 git -C remote-refs rev-parse remotes/origin/foo/otherbranch &&
354 test_must_fail git -C remote-refs rev-parse refs/hidden/foo/otherbranch &&
355 git -C remote-refs fetch --prune origin &&
356 test_must_fail git -C remote-refs rev-parse remotes/origin/foo/otherbranch
359 test_expect_success 'fetch tags when there is no tags' '
361 cd "$D" &&
363 mkdir notags &&
364 cd notags &&
365 git init &&
367 git fetch -t ..
371 test_expect_success 'fetch following tags' '
373 cd "$D" &&
374 git tag -a -m "annotated" anno HEAD &&
375 git tag light HEAD &&
377 mkdir four &&
378 cd four &&
379 git init &&
381 git fetch .. :track &&
382 git show-ref --verify refs/tags/anno &&
383 git show-ref --verify refs/tags/light
387 test_expect_success 'fetch uses remote ref names to describe new refs' '
388 cd "$D" &&
389 git init descriptive &&
391 cd descriptive &&
392 git config remote.o.url .. &&
393 git config remote.o.fetch "refs/heads/*:refs/crazyheads/*" &&
394 git config --add remote.o.fetch "refs/others/*:refs/heads/*" &&
395 git fetch o
396 ) &&
397 git tag -a -m "Descriptive tag" descriptive-tag &&
398 git branch descriptive-branch &&
399 git checkout descriptive-branch &&
400 echo "Nuts" >crazy &&
401 git add crazy &&
402 git commit -a -m "descriptive commit" &&
403 git update-ref refs/others/crazy HEAD &&
405 cd descriptive &&
406 git fetch o 2>actual &&
407 test_i18ngrep "new branch.* -> refs/crazyheads/descriptive-branch$" actual &&
408 test_i18ngrep "new tag.* -> descriptive-tag$" actual &&
409 test_i18ngrep "new ref.* -> crazy$" actual
410 ) &&
411 git checkout main
414 test_expect_success 'fetch must not resolve short tag name' '
416 cd "$D" &&
418 mkdir five &&
419 cd five &&
420 git init &&
422 test_must_fail git fetch .. anno:five
426 test_expect_success 'fetch can now resolve short remote name' '
428 cd "$D" &&
429 git update-ref refs/remotes/six/HEAD HEAD &&
431 mkdir six &&
432 cd six &&
433 git init &&
435 git fetch .. six:six
438 test_expect_success 'create bundle 1' '
439 cd "$D" &&
440 echo >file updated again by origin &&
441 git commit -a -m "tip" &&
442 git bundle create --version=3 bundle1 main^..main
445 test_expect_success 'header of bundle looks right' '
446 cat >expect <<-EOF &&
447 # v3 git bundle
448 @object-format=$(test_oid algo)
449 -OID updated by origin
450 OID refs/heads/main
453 sed -e "s/$OID_REGEX/OID/g" -e "5q" "$D"/bundle1 >actual &&
454 test_cmp expect actual
457 test_expect_success 'create bundle 2' '
458 cd "$D" &&
459 git bundle create bundle2 main~2..main
462 test_expect_success 'unbundle 1' '
463 cd "$D/bundle" &&
464 git checkout -b some-branch &&
465 test_must_fail git fetch "$D/bundle1" main:main
469 test_expect_success 'bundle 1 has only 3 files ' '
470 cd "$D" &&
471 test_bundle_object_count bundle1 3
474 test_expect_success 'unbundle 2' '
475 cd "$D/bundle" &&
476 git fetch ../bundle2 main:main &&
477 test "tip" = "$(git log -1 --pretty=oneline main | cut -d" " -f2)"
480 test_expect_success 'bundle does not prerequisite objects' '
481 cd "$D" &&
482 touch file2 &&
483 git add file2 &&
484 git commit -m add.file2 file2 &&
485 git bundle create bundle3 -1 HEAD &&
486 test_bundle_object_count bundle3 3
489 test_expect_success 'bundle should be able to create a full history' '
491 cd "$D" &&
492 git tag -a -m "1.0" v1.0 main &&
493 git bundle create bundle4 v1.0
497 test_expect_success 'fetch with a non-applying branch.<name>.merge' '
498 git config branch.main.remote yeti &&
499 git config branch.main.merge refs/heads/bigfoot &&
500 git config remote.blub.url one &&
501 git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
502 git fetch blub
505 # URL supplied to fetch does not match the url of the configured branch's remote
506 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
507 one_head=$(cd one && git rev-parse HEAD) &&
508 this_head=$(git rev-parse HEAD) &&
509 git update-ref -d FETCH_HEAD &&
510 git fetch one &&
511 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
512 test $this_head = "$(git rev-parse --verify HEAD)"
515 # URL supplied to fetch matches the url of the configured branch's remote and
516 # the merge spec matches the branch the remote HEAD points to
517 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
518 one_ref=$(cd one && git symbolic-ref HEAD) &&
519 git config branch.main.remote blub &&
520 git config branch.main.merge "$one_ref" &&
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, but
528 # the merge spec does not match the branch the remote HEAD points to
529 test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
530 git config branch.main.merge "${one_ref}_not" &&
531 git update-ref -d FETCH_HEAD &&
532 git fetch one &&
533 test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
534 test $this_head = "$(git rev-parse --verify HEAD)"
537 # the strange name is: a\!'b
538 test_expect_success 'quoting of a strangely named repo' '
539 test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
540 grep "fatal: '\''a\\\\!'\''b'\''" result
543 test_expect_success 'bundle should record HEAD correctly' '
545 cd "$D" &&
546 git bundle create bundle5 HEAD main &&
547 git bundle list-heads bundle5 >actual &&
548 for h in HEAD refs/heads/main
550 echo "$(git rev-parse --verify $h) $h" || return 1
551 done >expect &&
552 test_cmp expect actual
556 test_expect_success 'mark initial state of origin/main' '
558 cd three &&
559 git tag base-origin-main refs/remotes/origin/main
563 test_expect_success 'explicit fetch should update tracking' '
565 cd "$D" &&
566 git branch -f side &&
568 cd three &&
569 git update-ref refs/remotes/origin/main base-origin-main &&
570 o=$(git rev-parse --verify refs/remotes/origin/main) &&
571 git fetch origin main &&
572 n=$(git rev-parse --verify refs/remotes/origin/main) &&
573 test "$o" != "$n" &&
574 test_must_fail git rev-parse --verify refs/remotes/origin/side
578 test_expect_success 'explicit pull should update tracking' '
580 cd "$D" &&
581 git branch -f side &&
583 cd three &&
584 git update-ref refs/remotes/origin/main base-origin-main &&
585 o=$(git rev-parse --verify refs/remotes/origin/main) &&
586 git pull origin main &&
587 n=$(git rev-parse --verify refs/remotes/origin/main) &&
588 test "$o" != "$n" &&
589 test_must_fail git rev-parse --verify refs/remotes/origin/side
593 test_expect_success 'explicit --refmap is allowed only with command-line refspec' '
594 cd "$D" &&
596 cd three &&
597 test_must_fail git fetch --refmap="*:refs/remotes/none/*"
601 test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
602 cd "$D" &&
603 git branch -f side &&
605 cd three &&
606 git update-ref refs/remotes/origin/main base-origin-main &&
607 o=$(git rev-parse --verify refs/remotes/origin/main) &&
608 git fetch --refmap="refs/heads/*:refs/remotes/other/*" origin main &&
609 n=$(git rev-parse --verify refs/remotes/origin/main) &&
610 test "$o" = "$n" &&
611 test_must_fail git rev-parse --verify refs/remotes/origin/side &&
612 git rev-parse --verify refs/remotes/other/main
616 test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
617 cd "$D" &&
618 git branch -f side &&
620 cd three &&
621 git update-ref refs/remotes/origin/main base-origin-main &&
622 o=$(git rev-parse --verify refs/remotes/origin/main) &&
623 git fetch --refmap="" origin main &&
624 n=$(git rev-parse --verify refs/remotes/origin/main) &&
625 test "$o" = "$n" &&
626 test_must_fail git rev-parse --verify refs/remotes/origin/side
630 test_expect_success 'configured fetch updates tracking' '
632 cd "$D" &&
633 git branch -f side &&
635 cd three &&
636 git update-ref refs/remotes/origin/main base-origin-main &&
637 o=$(git rev-parse --verify refs/remotes/origin/main) &&
638 git fetch origin &&
639 n=$(git rev-parse --verify refs/remotes/origin/main) &&
640 test "$o" != "$n" &&
641 git rev-parse --verify refs/remotes/origin/side
645 test_expect_success 'non-matching refspecs do not confuse tracking update' '
646 cd "$D" &&
647 git update-ref refs/odd/location HEAD &&
649 cd three &&
650 git update-ref refs/remotes/origin/main base-origin-main &&
651 git config --add remote.origin.fetch \
652 refs/odd/location:refs/remotes/origin/odd &&
653 o=$(git rev-parse --verify refs/remotes/origin/main) &&
654 git fetch origin main &&
655 n=$(git rev-parse --verify refs/remotes/origin/main) &&
656 test "$o" != "$n" &&
657 test_must_fail git rev-parse --verify refs/remotes/origin/odd
661 test_expect_success 'pushing nonexistent branch by mistake should not segv' '
663 cd "$D" &&
664 test_must_fail git push seven no:no
668 test_expect_success 'auto tag following fetches minimum' '
670 cd "$D" &&
671 git clone .git follow &&
672 git checkout HEAD^0 &&
674 for i in 1 2 3 4 5 6 7
676 echo $i >>file &&
677 git commit -m $i -a &&
678 git tag -a -m $i excess-$i || exit 1
679 done
680 ) &&
681 git checkout main &&
683 cd follow &&
684 git fetch
688 test_expect_success 'refuse to fetch into the current branch' '
690 test_must_fail git fetch . side:main
694 test_expect_success 'fetch into the current branch with --update-head-ok' '
696 git fetch --update-head-ok . side:main
700 test_expect_success 'fetch --dry-run does not touch FETCH_HEAD, but still prints what would be written' '
701 rm -f .git/FETCH_HEAD err &&
702 git fetch --dry-run . 2>err &&
703 ! test -f .git/FETCH_HEAD &&
704 grep FETCH_HEAD err
707 test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD, and does not print what would be written' '
708 rm -f .git/FETCH_HEAD err &&
709 git fetch --no-write-fetch-head . 2>err &&
710 ! test -f .git/FETCH_HEAD &&
711 ! grep FETCH_HEAD err
714 test_expect_success '--write-fetch-head gets defeated by --dry-run' '
715 rm -f .git/FETCH_HEAD &&
716 git fetch --dry-run --write-fetch-head . &&
717 ! test -f .git/FETCH_HEAD
720 test_expect_success "should be able to fetch with duplicate refspecs" '
721 mkdir dups &&
723 cd dups &&
724 git init &&
725 git config branch.main.remote three &&
726 git config remote.three.url ../three/.git &&
727 git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
728 git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
729 git fetch three
733 test_expect_success 'LHS of refspec follows ref disambiguation rules' '
734 mkdir lhs-ambiguous &&
736 cd lhs-ambiguous &&
737 git init server &&
738 test_commit -C server unwanted &&
739 test_commit -C server wanted &&
741 git init client &&
743 # Check a name coming after "refs" alphabetically ...
744 git -C server update-ref refs/heads/s wanted &&
745 git -C server update-ref refs/heads/refs/heads/s unwanted &&
746 git -C client fetch ../server +refs/heads/s:refs/heads/checkthis &&
747 git -C server rev-parse wanted >expect &&
748 git -C client rev-parse checkthis >actual &&
749 test_cmp expect actual &&
751 # ... and one before.
752 git -C server update-ref refs/heads/q wanted &&
753 git -C server update-ref refs/heads/refs/heads/q unwanted &&
754 git -C client fetch ../server +refs/heads/q:refs/heads/checkthis &&
755 git -C server rev-parse wanted >expect &&
756 git -C client rev-parse checkthis >actual &&
757 test_cmp expect actual &&
759 # Tags are preferred over branches like refs/{heads,tags}/*
760 git -C server update-ref refs/tags/t wanted &&
761 git -C server update-ref refs/heads/t unwanted &&
762 git -C client fetch ../server +t:refs/heads/checkthis &&
763 git -C server rev-parse wanted >expect &&
764 git -C client rev-parse checkthis >actual
768 test_expect_success 'fetch.writeCommitGraph' '
769 git clone three write &&
771 cd three &&
772 test_commit new
773 ) &&
775 cd write &&
776 git -c fetch.writeCommitGraph fetch origin &&
777 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
781 test_expect_success 'fetch.writeCommitGraph with submodules' '
782 git clone dups super &&
784 cd super &&
785 git submodule add "file://$TRASH_DIRECTORY/three" &&
786 git commit -m "add submodule"
787 ) &&
788 git clone "super" super-clone &&
790 cd super-clone &&
791 rm -rf .git/objects/info &&
792 git -c fetch.writeCommitGraph=true fetch origin &&
793 test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
797 # configured prune tests
799 set_config_tristate () {
800 # var=$1 val=$2
801 case "$2" in
802 unset)
803 test_unconfig "$1"
806 git config "$1" "$2"
807 key=$(echo $1 | sed -e 's/^remote\.origin/fetch/')
808 git_fetch_c="$git_fetch_c -c $key=$2"
810 esac
813 test_configured_prune () {
814 test_configured_prune_type "$@" "name"
815 test_configured_prune_type "$@" "link"
818 test_configured_prune_type () {
819 fetch_prune=$1
820 remote_origin_prune=$2
821 fetch_prune_tags=$3
822 remote_origin_prune_tags=$4
823 expected_branch=$5
824 expected_tag=$6
825 cmdline=$7
826 mode=$8
828 if test -z "$cmdline_setup"
829 then
830 test_expect_success 'setup cmdline_setup variable for subsequent test' '
831 remote_url="file://$(git -C one config remote.origin.url)" &&
832 remote_fetch="$(git -C one config remote.origin.fetch)" &&
833 cmdline_setup="\"$remote_url\" \"$remote_fetch\""
837 if test "$mode" = 'link'
838 then
839 new_cmdline=""
841 if test "$cmdline" = ""
842 then
843 new_cmdline=$cmdline_setup
844 else
845 new_cmdline=$(printf "%s" "$cmdline" | perl -pe 's[origin(?!/)]["'"$remote_url"'"]g')
848 if test "$fetch_prune_tags" = 'true' ||
849 test "$remote_origin_prune_tags" = 'true'
850 then
851 if ! printf '%s' "$cmdline\n" | grep -q refs/remotes/origin/
852 then
853 new_cmdline="$new_cmdline refs/tags/*:refs/tags/*"
857 cmdline="$new_cmdline"
860 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" '
861 # make sure a newbranch is there in . and also in one
862 git branch -f newbranch &&
863 git tag -f newtag &&
865 cd one &&
866 test_unconfig fetch.prune &&
867 test_unconfig fetch.pruneTags &&
868 test_unconfig remote.origin.prune &&
869 test_unconfig remote.origin.pruneTags &&
870 git fetch '"$cmdline_setup"' &&
871 git rev-parse --verify refs/remotes/origin/newbranch &&
872 git rev-parse --verify refs/tags/newtag
873 ) &&
875 # now remove them
876 git branch -d newbranch &&
877 git tag -d newtag &&
879 # then test
881 cd one &&
882 git_fetch_c="" &&
883 set_config_tristate fetch.prune $fetch_prune &&
884 set_config_tristate fetch.pruneTags $fetch_prune_tags &&
885 set_config_tristate remote.origin.prune $remote_origin_prune &&
886 set_config_tristate remote.origin.pruneTags $remote_origin_prune_tags &&
888 if test "$mode" != "link"
889 then
890 git_fetch_c=""
891 fi &&
892 git$git_fetch_c fetch '"$cmdline"' &&
893 case "$expected_branch" in
894 pruned)
895 test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
897 kept)
898 git rev-parse --verify refs/remotes/origin/newbranch
900 esac &&
901 case "$expected_tag" in
902 pruned)
903 test_must_fail git rev-parse --verify refs/tags/newtag
905 kept)
906 git rev-parse --verify refs/tags/newtag
908 esac
913 # $1 config: fetch.prune
914 # $2 config: remote.<name>.prune
915 # $3 config: fetch.pruneTags
916 # $4 config: remote.<name>.pruneTags
917 # $5 expect: branch to be pruned?
918 # $6 expect: tag to be pruned?
919 # $7 git-fetch $cmdline:
921 # $1 $2 $3 $4 $5 $6 $7
922 test_configured_prune unset unset unset unset kept kept ""
923 test_configured_prune unset unset unset unset kept kept "--no-prune"
924 test_configured_prune unset unset unset unset pruned kept "--prune"
925 test_configured_prune unset unset unset unset kept pruned \
926 "--prune origin refs/tags/*:refs/tags/*"
927 test_configured_prune unset unset unset unset pruned pruned \
928 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
930 test_configured_prune false unset unset unset kept kept ""
931 test_configured_prune false unset unset unset kept kept "--no-prune"
932 test_configured_prune false unset unset unset pruned kept "--prune"
934 test_configured_prune true unset unset unset pruned kept ""
935 test_configured_prune true unset unset unset pruned kept "--prune"
936 test_configured_prune true unset unset unset kept kept "--no-prune"
938 test_configured_prune unset false unset unset kept kept ""
939 test_configured_prune unset false unset unset kept kept "--no-prune"
940 test_configured_prune unset false unset unset pruned kept "--prune"
942 test_configured_prune false false unset unset kept kept ""
943 test_configured_prune false false unset unset kept kept "--no-prune"
944 test_configured_prune false false unset unset pruned kept "--prune"
945 test_configured_prune false false unset unset kept pruned \
946 "--prune origin refs/tags/*:refs/tags/*"
947 test_configured_prune false false unset unset pruned pruned \
948 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
950 test_configured_prune true false unset unset kept kept ""
951 test_configured_prune true false unset unset pruned kept "--prune"
952 test_configured_prune true false unset unset kept kept "--no-prune"
954 test_configured_prune unset true unset unset pruned kept ""
955 test_configured_prune unset true unset unset kept kept "--no-prune"
956 test_configured_prune unset true unset unset pruned kept "--prune"
958 test_configured_prune false true unset unset pruned kept ""
959 test_configured_prune false true unset unset kept kept "--no-prune"
960 test_configured_prune false true unset unset pruned kept "--prune"
962 test_configured_prune true true unset unset pruned kept ""
963 test_configured_prune true true unset unset pruned kept "--prune"
964 test_configured_prune true true unset unset kept kept "--no-prune"
965 test_configured_prune true true unset unset kept pruned \
966 "--prune origin refs/tags/*:refs/tags/*"
967 test_configured_prune true true unset unset pruned pruned \
968 "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
970 # --prune-tags on its own does nothing, needs --prune as well, same
971 # for fetch.pruneTags without fetch.prune
972 test_configured_prune unset unset unset unset kept kept "--prune-tags"
973 test_configured_prune unset unset true unset kept kept ""
974 test_configured_prune unset unset unset true kept kept ""
976 # These will prune the tags
977 test_configured_prune unset unset unset unset pruned pruned "--prune --prune-tags"
978 test_configured_prune true unset true unset pruned pruned ""
979 test_configured_prune unset true unset true pruned pruned ""
981 # remote.<name>.pruneTags overrides fetch.pruneTags, just like
982 # remote.<name>.prune overrides fetch.prune if set.
983 test_configured_prune true unset true unset pruned pruned ""
984 test_configured_prune false true false true pruned pruned ""
985 test_configured_prune true false true false kept kept ""
987 # When --prune-tags is supplied it's ignored if an explicit refspec is
988 # given, same for the configuration options.
989 test_configured_prune unset unset unset unset pruned kept \
990 "--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"
991 test_configured_prune unset unset true unset pruned kept \
992 "--prune origin +refs/heads/*:refs/remotes/origin/*"
993 test_configured_prune unset unset unset true pruned kept \
994 "--prune origin +refs/heads/*:refs/remotes/origin/*"
996 # Pruning that also takes place if a file:// url replaces a named
997 # remote. However, because there's no implicit
998 # +refs/heads/*:refs/remotes/origin/* refspec and supplying it on the
999 # command-line negates --prune-tags, the branches will not be pruned.
1000 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "name"
1001 test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "link"
1002 test_configured_prune_type unset unset unset unset pruned pruned "origin --prune --prune-tags" "name"
1003 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1004 test_configured_prune_type unset unset unset unset pruned pruned "--prune --prune-tags origin" "name"
1005 test_configured_prune_type unset unset unset unset kept pruned "--prune --prune-tags origin" "link"
1006 test_configured_prune_type unset unset true unset pruned pruned "--prune origin" "name"
1007 test_configured_prune_type unset unset true unset kept pruned "--prune origin" "link"
1008 test_configured_prune_type unset unset unset true pruned pruned "--prune origin" "name"
1009 test_configured_prune_type unset unset unset true kept pruned "--prune origin" "link"
1010 test_configured_prune_type true unset true unset pruned pruned "origin" "name"
1011 test_configured_prune_type true unset true unset kept pruned "origin" "link"
1012 test_configured_prune_type unset true true unset pruned pruned "origin" "name"
1013 test_configured_prune_type unset true true unset kept pruned "origin" "link"
1014 test_configured_prune_type unset true unset true pruned pruned "origin" "name"
1015 test_configured_prune_type unset true unset true kept pruned "origin" "link"
1017 # When all remote.origin.fetch settings are deleted a --prune
1018 # --prune-tags still implicitly supplies refs/tags/*:refs/tags/* so
1019 # tags, but not tracking branches, will be deleted.
1020 test_expect_success 'remove remote.origin.fetch "one"' '
1022 cd one &&
1023 git config --unset-all remote.origin.fetch
1026 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "name"
1027 test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
1029 test_expect_success 'all boundary commits are excluded' '
1030 test_commit base &&
1031 test_commit oneside &&
1032 git checkout HEAD^ &&
1033 test_commit otherside &&
1034 git checkout main &&
1035 test_tick &&
1036 git merge otherside &&
1037 ad=$(git log --no-walk --format=%ad HEAD) &&
1038 git bundle create twoside-boundary.bdl main --since="$ad" &&
1039 test_bundle_object_count --thin twoside-boundary.bdl 3
1042 test_expect_success 'fetch --prune prints the remotes url' '
1043 git branch goodbye &&
1044 git clone . only-prunes &&
1045 git branch -D goodbye &&
1047 cd only-prunes &&
1048 git fetch --prune origin 2>&1 | head -n1 >../actual
1049 ) &&
1050 echo "From ${D}/." >expect &&
1051 test_cmp expect actual
1054 test_expect_success 'branchname D/F conflict resolved by --prune' '
1055 git branch dir/file &&
1056 git clone . prune-df-conflict &&
1057 git branch -D dir/file &&
1058 git branch dir &&
1060 cd prune-df-conflict &&
1061 git fetch --prune &&
1062 git rev-parse origin/dir >../actual
1063 ) &&
1064 git rev-parse dir >expect &&
1065 test_cmp expect actual
1068 test_expect_success 'fetching a one-level ref works' '
1069 test_commit extra &&
1070 git reset --hard HEAD^ &&
1071 git update-ref refs/foo extra &&
1072 git init one-level &&
1074 cd one-level &&
1075 git fetch .. HEAD refs/foo
1079 test_expect_success 'fetching with auto-gc does not lock up' '
1080 write_script askyesno <<-\EOF &&
1081 echo "$*" &&
1082 false
1084 git clone "file://$D" auto-gc &&
1085 test_commit test2 &&
1087 cd auto-gc &&
1088 git config fetch.unpackLimit 1 &&
1089 git config gc.autoPackLimit 1 &&
1090 git config gc.autoDetach false &&
1091 GIT_ASK_YESNO="$D/askyesno" git fetch --verbose >fetch.out 2>&1 &&
1092 test_i18ngrep "Auto packing the repository" fetch.out &&
1093 ! grep "Should I try again" fetch.out
1097 test_expect_success 'fetch aligned output' '
1098 git clone . full-output &&
1099 test_commit looooooooooooong-tag &&
1101 cd full-output &&
1102 git -c fetch.output=full fetch origin >actual 2>&1 &&
1103 grep -e "->" actual | cut -c 22- >../actual
1104 ) &&
1105 cat >expect <<-\EOF &&
1106 main -> origin/main
1107 looooooooooooong-tag -> looooooooooooong-tag
1109 test_cmp expect actual
1112 test_expect_success 'fetch compact output' '
1113 git clone . compact &&
1114 test_commit extraaa &&
1116 cd compact &&
1117 git -c fetch.output=compact fetch origin >actual 2>&1 &&
1118 grep -e "->" actual | cut -c 22- >../actual
1119 ) &&
1120 cat >expect <<-\EOF &&
1121 main -> origin/*
1122 extraaa -> *
1124 test_cmp expect actual
1127 test_expect_success '--no-show-forced-updates' '
1128 mkdir forced-updates &&
1130 cd forced-updates &&
1131 git init &&
1132 test_commit 1 &&
1133 test_commit 2
1134 ) &&
1135 git clone forced-updates forced-update-clone &&
1136 git clone forced-updates no-forced-update-clone &&
1137 git -C forced-updates reset --hard HEAD~1 &&
1139 cd forced-update-clone &&
1140 git fetch --show-forced-updates origin 2>output &&
1141 test_i18ngrep "(forced update)" output
1142 ) &&
1144 cd no-forced-update-clone &&
1145 git fetch --no-show-forced-updates origin 2>output &&
1146 test_i18ngrep ! "(forced update)" output
1150 setup_negotiation_tip () {
1151 SERVER="$1"
1152 URL="$2"
1153 USE_PROTOCOL_V2="$3"
1155 rm -rf "$SERVER" client trace &&
1156 git init -b main "$SERVER" &&
1157 test_commit -C "$SERVER" alpha_1 &&
1158 test_commit -C "$SERVER" alpha_2 &&
1159 git -C "$SERVER" checkout --orphan beta &&
1160 test_commit -C "$SERVER" beta_1 &&
1161 test_commit -C "$SERVER" beta_2 &&
1163 git clone "$URL" client &&
1165 if test "$USE_PROTOCOL_V2" -eq 1
1166 then
1167 git -C "$SERVER" config protocol.version 2 &&
1168 git -C client config protocol.version 2
1169 fi &&
1171 test_commit -C "$SERVER" beta_s &&
1172 git -C "$SERVER" checkout main &&
1173 test_commit -C "$SERVER" alpha_s &&
1174 git -C "$SERVER" tag -d alpha_1 alpha_2 beta_1 beta_2
1177 check_negotiation_tip () {
1178 # Ensure that {alpha,beta}_1 are sent as "have", but not {alpha_beta}_2
1179 ALPHA_1=$(git -C client rev-parse alpha_1) &&
1180 grep "fetch> have $ALPHA_1" trace &&
1181 BETA_1=$(git -C client rev-parse beta_1) &&
1182 grep "fetch> have $BETA_1" trace &&
1183 ALPHA_2=$(git -C client rev-parse alpha_2) &&
1184 ! grep "fetch> have $ALPHA_2" trace &&
1185 BETA_2=$(git -C client rev-parse beta_2) &&
1186 ! grep "fetch> have $BETA_2" trace
1189 test_expect_success '--negotiation-tip limits "have" lines sent' '
1190 setup_negotiation_tip server server 0 &&
1191 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1192 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1193 origin alpha_s beta_s &&
1194 check_negotiation_tip
1197 test_expect_success '--negotiation-tip understands globs' '
1198 setup_negotiation_tip server server 0 &&
1199 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1200 --negotiation-tip=*_1 \
1201 origin alpha_s beta_s &&
1202 check_negotiation_tip
1205 test_expect_success '--negotiation-tip understands abbreviated SHA-1' '
1206 setup_negotiation_tip server server 0 &&
1207 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1208 --negotiation-tip=$(git -C client rev-parse --short alpha_1) \
1209 --negotiation-tip=$(git -C client rev-parse --short beta_1) \
1210 origin alpha_s beta_s &&
1211 check_negotiation_tip
1214 test_expect_success '--negotiation-tip rejects missing OIDs' '
1215 setup_negotiation_tip server server 0 &&
1216 test_must_fail git -C client fetch \
1217 --negotiation-tip=alpha_1 \
1218 --negotiation-tip=$(test_oid zero) \
1219 origin alpha_s beta_s 2>err &&
1220 cat >fatal-expect <<-EOF &&
1221 fatal: the object $(test_oid zero) does not exist
1223 grep fatal: err >fatal-actual &&
1224 test_cmp fatal-expect fatal-actual
1227 . "$TEST_DIRECTORY"/lib-httpd.sh
1228 start_httpd
1230 test_expect_success '--negotiation-tip limits "have" lines sent with HTTP protocol v2' '
1231 setup_negotiation_tip "$HTTPD_DOCUMENT_ROOT_PATH/server" \
1232 "$HTTPD_URL/smart/server" 1 &&
1233 GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
1234 --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
1235 origin alpha_s beta_s &&
1236 check_negotiation_tip
1239 # DO NOT add non-httpd-specific tests here, because the last part of this
1240 # test script is only executed when httpd is available and enabled.
1242 test_done