]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5500-fetch-pack.sh
Sync with Git 2.45.1
[thirdparty/git.git] / t / t5500-fetch-pack.sh
CommitLineData
6b17c674
JS
1#!/bin/sh
2#
3# Copyright (c) 2005 Johannes Schindelin
4#
5
3902985a 6test_description='Testing multi_ack pack fetching'
6b17c674 7
3275f4e8 8GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
9export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
6b17c674
JS
11. ./test-lib.sh
12
13# Test fetch-pack/upload-pack pair.
14
15# Some convenience functions
16
1010437d 17add () {
3902985a
SB
18 name=$1 &&
19 text="$@" &&
2feed907 20 branch=$(echo $name | sed -e 's/^\(.\).*$/\1/') &&
3902985a 21 parents="" &&
6b17c674 22
3902985a 23 shift &&
6b17c674 24 while test $1; do
3902985a 25 parents="$parents -p $1" &&
6b17c674 26 shift
3902985a 27 done &&
6b17c674 28
3902985a
SB
29 echo "$text" > test.txt &&
30 git update-index --add test.txt &&
31 tree=$(git write-tree) &&
6b17c674 32 # make sure timestamps are in correct order
3902985a
SB
33 test_tick &&
34 commit=$(echo "$text" | git commit-tree $tree $parents) &&
35 eval "$name=$commit; export $name" &&
cbc5cf7c 36 git update-ref "refs/heads/$branch" "$commit" &&
6b17c674
JS
37 eval ${branch}TIP=$commit
38}
39
1010437d 40pull_to_client () {
3902985a
SB
41 number=$1 &&
42 heads=$2 &&
43 count=$3 &&
44 test_expect_success "$number pull" '
45 (
46 cd client &&
47 git fetch-pack -k -v .. $heads &&
48
49 case "$heads" in
50 *A*)
cbc5cf7c 51 git update-ref refs/heads/A "$ATIP";;
3902985a
SB
52 esac &&
53 case "$heads" in *B*)
cbc5cf7c 54 git update-ref refs/heads/B "$BTIP";;
3902985a 55 esac &&
bdbc17e8
MD
56
57 git symbolic-ref HEAD refs/heads/$(
58 echo $heads |
59 sed -e "s/^\(.\).*$/\1/"
60 ) &&
3902985a
SB
61
62 git fsck --full &&
63
64 mv .git/objects/pack/pack-* . &&
2feed907 65 p=$(ls -1 pack-*.pack) &&
3902985a
SB
66 git unpack-objects <$p &&
67 git fsck --full &&
68
2feed907
EP
69 idx=$(echo pack-*.idx) &&
70 pack_count=$(git show-index <$idx | wc -l) &&
3902985a
SB
71 test $pack_count = $count &&
72 rm -f pack-*
73 )
74 '
6b17c674
JS
75}
76
77# Here begins the actual testing
78
79# A1 - ... - A20 - A21
80# \
81# B1 - B2 - .. - B70
82
83# client pulls A20, B1. Then tracks only B. Then pulls A.
84
3902985a 85test_expect_success 'setup' '
6b17c674 86 mkdir client &&
3902985a
SB
87 (
88 cd client &&
89 git init &&
90 git config transfer.unpacklimit 0
91 ) &&
92 add A1 &&
93 prev=1 &&
94 cur=2 &&
95 while [ $cur -le 10 ]; do
96 add A$cur $(eval echo \$A$prev) &&
97 prev=$cur &&
d0fd9931 98 cur=$(($cur+1)) || return 1
3902985a 99 done &&
a48fcd83 100 add B1 $A1 &&
cbc5cf7c
DT
101 git update-ref refs/heads/A "$ATIP" &&
102 git update-ref refs/heads/B "$BTIP" &&
3902985a
SB
103 git symbolic-ref HEAD refs/heads/B
104'
6b17c674 105
e9d866e3 106pull_to_client 1st "refs/heads/B refs/heads/A" $((11*3))
6b17c674 107
3902985a
SB
108test_expect_success 'post 1st pull setup' '
109 add A11 $A10 &&
110 prev=1 &&
111 cur=2 &&
112 while [ $cur -le 65 ]; do
113 add B$cur $(eval echo \$B$prev) &&
114 prev=$cur &&
d0fd9931 115 cur=$(($cur+1)) || return 1
3902985a
SB
116 done
117'
6b17c674 118
e9d866e3 119pull_to_client 2nd "refs/heads/B" $((64*3))
6b17c674 120
e9d866e3 121pull_to_client 3rd "refs/heads/A" $((1*3))
16ad3579 122
3e6e0edd
NTND
123test_expect_success 'single branch clone' '
124 git clone --single-branch "file://$(pwd)/." singlebranch
125'
126
127test_expect_success 'single branch object count' '
128 GIT_DIR=singlebranch/.git git count-objects -v |
129 grep "^in-pack:" > count.singlebranch &&
130 echo "in-pack: 198" >expected &&
131 test_cmp expected count.singlebranch
132'
133
0ec4b165 134test_expect_success 'single given branch clone' '
b8f58c20
RC
135 GIT_TRACE2_EVENT="$(pwd)/branch-a/trace2_event" \
136 git clone --single-branch --branch A "file://$(pwd)/." branch-a &&
137 test_must_fail git --git-dir=branch-a/.git rev-parse origin/B &&
138 grep \"fetch-info\".*\"haves\":0 branch-a/trace2_event &&
139 grep \"fetch-info\".*\"wants\":1 branch-a/trace2_event
0ec4b165
NTND
140'
141
682c7d2f 142test_expect_success 'clone shallow depth 1' '
b8f58c20
RC
143 GIT_TRACE2_EVENT="$(pwd)/shallow0/trace2_event" \
144 git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0 &&
145 test "$(git --git-dir=shallow0/.git rev-list --count HEAD)" = 1 &&
146 grep \"fetch-info\".*\"depth\":1 shallow0/trace2_event
682c7d2f
NTND
147'
148
6035d6aa
NTND
149test_expect_success 'clone shallow depth 1 with fsck' '
150 git config --global fetch.fsckobjects true &&
151 git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0fsck &&
2feed907 152 test "$(git --git-dir=shallow0fsck/.git rev-list --count HEAD)" = 1 &&
6035d6aa
NTND
153 git config --global --unset fetch.fsckobjects
154'
155
3902985a 156test_expect_success 'clone shallow' '
3e6e0edd 157 git clone --no-single-branch --depth 2 "file://$(pwd)/." shallow
3902985a 158'
37818d7d 159
682c7d2f 160test_expect_success 'clone shallow depth count' '
2feed907 161 test "$(git --git-dir=shallow/.git rev-list --count HEAD)" = 2
682c7d2f
NTND
162'
163
3902985a
SB
164test_expect_success 'clone shallow object count' '
165 (
166 cd shallow &&
167 git count-objects -v
168 ) > count.shallow &&
682c7d2f 169 grep "^in-pack: 12" count.shallow
37818d7d 170'
16ad3579 171
3902985a
SB
172test_expect_success 'clone shallow object count (part 2)' '
173 sed -e "/^in-pack:/d" -e "/^packs:/d" -e "/^size-pack:/d" \
174 -e "/: 0$/d" count.shallow > count_output &&
ec10b018 175 test_must_be_empty count_output
3902985a 176'
16ad3579 177
3902985a
SB
178test_expect_success 'fsck in shallow repo' '
179 (
180 cd shallow &&
181 git fsck --full
182 )
183'
16ad3579 184
86386829
NP
185test_expect_success 'simple fetch in shallow repo' '
186 (
187 cd shallow &&
188 git fetch
189 )
190'
191
192test_expect_success 'no changes expected' '
193 (
194 cd shallow &&
195 git count-objects -v
196 ) > count.shallow.2 &&
197 cmp count.shallow count.shallow.2
198'
199
200test_expect_success 'fetch same depth in shallow repo' '
201 (
202 cd shallow &&
203 git fetch --depth=2
204 )
205'
206
207test_expect_success 'no changes expected' '
208 (
209 cd shallow &&
210 git count-objects -v
211 ) > count.shallow.3 &&
212 cmp count.shallow count.shallow.3
213'
16ad3579 214
3902985a
SB
215test_expect_success 'add two more' '
216 add B66 $B65 &&
217 add B67 $B66
218'
16ad3579 219
3902985a
SB
220test_expect_success 'pull in shallow repo' '
221 (
222 cd shallow &&
223 git pull .. B
224 )
225'
16ad3579 226
3902985a
SB
227test_expect_success 'clone shallow object count' '
228 (
229 cd shallow &&
230 git count-objects -v
231 ) > count.shallow &&
232 grep "^count: 6" count.shallow
233'
16ad3579 234
3902985a
SB
235test_expect_success 'add two more (part 2)' '
236 add B68 $B67 &&
237 add B69 $B68
238'
16ad3579 239
3902985a
SB
240test_expect_success 'deepening pull in shallow repo' '
241 (
242 cd shallow &&
b8f58c20
RC
243 GIT_TRACE2_EVENT="$(pwd)/trace2_event" \
244 git pull --depth 4 .. B &&
245 grep \"fetch-info\".*\"depth\":4 trace2_event &&
246 grep \"fetch-info\".*\"shallows\":2 trace2_event
3902985a
SB
247 )
248'
16ad3579 249
3902985a
SB
250test_expect_success 'clone shallow object count' '
251 (
252 cd shallow &&
253 git count-objects -v
254 ) > count.shallow &&
255 grep "^count: 12" count.shallow
256'
16ad3579 257
3902985a
SB
258test_expect_success 'deepening fetch in shallow repo' '
259 (
260 cd shallow &&
261 git fetch --depth 4 .. A:A
262 )
263'
16ad3579 264
3902985a
SB
265test_expect_success 'clone shallow object count' '
266 (
267 cd shallow &&
268 git count-objects -v
269 ) > count.shallow &&
270 grep "^count: 18" count.shallow
271'
16ad3579 272
3902985a
SB
273test_expect_success 'pull in shallow repo with missing merge base' '
274 (
275 cd shallow &&
51b85471 276 git fetch --depth 4 .. A &&
e379fdf3 277 test_must_fail git merge --allow-unrelated-histories FETCH_HEAD
3902985a
SB
278 )
279'
16ad3579 280
86386829
NP
281test_expect_success 'additional simple shallow deepenings' '
282 (
283 cd shallow &&
284 git fetch --depth=8 &&
285 git fetch --depth=10 &&
286 git fetch --depth=11
287 )
288'
289
682c7d2f 290test_expect_success 'clone shallow depth count' '
2feed907 291 test "$(git --git-dir=shallow/.git rev-list --count HEAD)" = 11
682c7d2f
NTND
292'
293
86386829
NP
294test_expect_success 'clone shallow object count' '
295 (
296 cd shallow &&
e379fdf3 297 git prune &&
86386829
NP
298 git count-objects -v
299 ) > count.shallow &&
e379fdf3 300 grep "^count: 54" count.shallow
86386829
NP
301'
302
4dcb167f
NTND
303test_expect_success 'fetch --no-shallow on full repo' '
304 test_must_fail git fetch --noshallow
305'
306
307test_expect_success 'fetch --depth --no-shallow' '
308 (
309 cd shallow &&
310 test_must_fail git fetch --depth=1 --noshallow
311 )
312'
313
314test_expect_success 'turn shallow to complete repository' '
315 (
316 cd shallow &&
b8f58c20
RC
317 GIT_TRACE2_EVENT="$(pwd)/trace2_event" \
318 git fetch --unshallow &&
4dcb167f 319 ! test -f .git/shallow &&
b8f58c20
RC
320 git fsck --full &&
321 grep \"fetch-info\".*\"shallows\":2 trace2_event &&
322 grep \"fetch-info\".*\"depth\":2147483647 trace2_event
4dcb167f
NTND
323 )
324'
325
3e6e0edd
NTND
326test_expect_success 'clone shallow without --no-single-branch' '
327 git clone --depth 1 "file://$(pwd)/." shallow2
328'
329
330test_expect_success 'clone shallow object count' '
331 (
332 cd shallow2 &&
333 git count-objects -v
334 ) > count.shallow2 &&
682c7d2f 335 grep "^in-pack: 3" count.shallow2
3e6e0edd
NTND
336'
337
338test_expect_success 'clone shallow with --branch' '
339 git clone --depth 1 --branch A "file://$(pwd)/." shallow3
340'
341
342test_expect_success 'clone shallow object count' '
682c7d2f 343 echo "in-pack: 3" > count3.expected &&
3e6e0edd
NTND
344 GIT_DIR=shallow3/.git git count-objects -v |
345 grep "^in-pack" > count3.actual &&
346 test_cmp count3.expected count3.actual
347'
348
3e6e0edd
NTND
349test_expect_success 'clone shallow with detached HEAD' '
350 git checkout HEAD^ &&
351 git clone --depth 1 "file://$(pwd)/." shallow5 &&
352 git checkout - &&
353 GIT_DIR=shallow5/.git git rev-parse HEAD >actual &&
354 git rev-parse HEAD^ >expected &&
355 test_cmp expected actual
356'
357
358test_expect_success 'shallow clone pulling tags' '
359 git tag -a -m A TAGA1 A &&
360 git tag -a -m B TAGB1 B &&
361 git tag TAGA2 A &&
362 git tag TAGB2 B &&
363 git clone --depth 1 "file://$(pwd)/." shallow6 &&
364
365 cat >taglist.expected <<\EOF &&
366TAGB1
367TAGB2
368EOF
369 GIT_DIR=shallow6/.git git tag -l >taglist.actual &&
370 test_cmp taglist.expected taglist.actual &&
371
682c7d2f 372 echo "in-pack: 4" > count6.expected &&
3e6e0edd
NTND
373 GIT_DIR=shallow6/.git git count-objects -v |
374 grep "^in-pack" > count6.actual &&
375 test_cmp count6.expected count6.actual
376'
377
5a7d5b68
NTND
378test_expect_success 'shallow cloning single tag' '
379 git clone --depth 1 --branch=TAGB1 "file://$(pwd)/." shallow7 &&
380 cat >taglist.expected <<\EOF &&
381TAGB1
382TAGB2
383EOF
384 GIT_DIR=shallow7/.git git tag -l >taglist.actual &&
385 test_cmp taglist.expected taglist.actual &&
386
682c7d2f 387 echo "in-pack: 4" > count7.expected &&
5a7d5b68
NTND
388 GIT_DIR=shallow7/.git git count-objects -v |
389 grep "^in-pack" > count7.actual &&
390 test_cmp count7.expected count7.actual
391'
392
06f15bf1
JK
393test_expect_success 'clone shallow with packed refs' '
394 git pack-refs --all &&
395 git clone --depth 1 --branch A "file://$(pwd)/." shallow8 &&
396 echo "in-pack: 4" > count8.expected &&
397 GIT_DIR=shallow8/.git git count-objects -v |
398 grep "^in-pack" > count8.actual &&
399 test_cmp count8.expected count8.actual
400'
401
4fa3f00a 402test_expect_success 'in_vain not triggered before first ACK' '
2b695ecd 403 rm -rf myserver myclient &&
4fa3f00a
JT
404 git init myserver &&
405 test_commit -C myserver foo &&
406 git clone "file://$(pwd)/myserver" myclient &&
407
408 # MAX_IN_VAIN is 256. Because of batching, the client will send 496
409 # (16+32+64+128+256) commits, not 256, before giving up. So create 496
410 # irrelevant commits.
411 test_commit_bulk -C myclient 496 &&
412
413 # The new commit that the client wants to fetch.
414 test_commit -C myserver bar &&
415
2b695ecd 416 git -C myclient fetch --progress origin 2>log &&
6789275d 417 test_grep "remote: Total 3 " log
4fa3f00a
JT
418'
419
2f0a093d 420test_expect_success 'in_vain resetted upon ACK' '
a29263cf 421 test_when_finished rm -f log trace2 &&
2b695ecd 422 rm -rf myserver myclient &&
2f0a093d
JT
423 git init myserver &&
424
3275f4e8 425 # Linked list of commits on main. The first is common; the rest are
2f0a093d 426 # not.
3275f4e8 427 test_commit -C myserver first_main_commit &&
2f0a093d
JT
428 git clone "file://$(pwd)/myserver" myclient &&
429 test_commit_bulk -C myclient 255 &&
430
431 # Another linked list of commits on anotherbranch with no connection to
3275f4e8 432 # main. The first is common; the rest are not.
2f0a093d
JT
433 git -C myserver checkout --orphan anotherbranch &&
434 test_commit -C myserver first_anotherbranch_commit &&
435 git -C myclient fetch origin anotherbranch:refs/heads/anotherbranch &&
436 git -C myclient checkout anotherbranch &&
437 test_commit_bulk -C myclient 255 &&
438
439 # The new commit that the client wants to fetch.
3275f4e8 440 git -C myserver checkout main &&
2f0a093d
JT
441 test_commit -C myserver to_fetch &&
442
443 # The client will send (as "have"s) all 256 commits in anotherbranch
444 # first. The 256th commit is common between the client and the server,
445 # and should reset in_vain. This allows negotiation to continue until
446 # the client reports that first_anotherbranch_commit is common.
a29263cf
JS
447 GIT_TRACE2_EVENT="$(pwd)/trace2" git -C myclient fetch --progress origin main 2>log &&
448 grep \"key\":\"total_rounds\",\"value\":\"6\" trace2 &&
6789275d 449 test_grep "Total 3 " log
2f0a093d
JT
450'
451
71d5f938
MH
452test_expect_success 'fetch in shallow repo unreachable shallow objects' '
453 (
454 git clone --bare --branch B --single-branch "file://$(pwd)/." no-reflog &&
455 git clone --depth 1 "file://$(pwd)/no-reflog" shallow9 &&
456 cd no-reflog &&
457 git tag -d TAGB1 TAGB2 &&
458 git update-ref refs/heads/B B~~ &&
459 git gc --prune=now &&
460 cd ../shallow9 &&
461 git fetch origin &&
462 git fsck --no-dangling
463 )
464'
f21d2a78
MK
465test_expect_success 'fetch creating new shallow root' '
466 (
467 git clone "file://$(pwd)/." shallow10 &&
468 git commit --allow-empty -m empty &&
469 cd shallow10 &&
470 git fetch --depth=1 --progress 2>actual &&
471 # This should fetch only the empty commit, no tree or
472 # blob objects
6789275d 473 test_grep "remote: Total 1" actual
f21d2a78
MK
474 )
475'
71d5f938 476
b2a9f4da
IT
477test_expect_success 'setup tests for the --stdin parameter' '
478 for head in C D E F
479 do
d0fd9931 480 add $head || return 1
b2a9f4da
IT
481 done &&
482 for head in A B C D E F
483 do
d0fd9931 484 git tag $head $head || return 1
b2a9f4da 485 done &&
99094a7a 486 cat >input <<-\EOF &&
b2a9f4da
IT
487 refs/heads/C
488 refs/heads/A
489 refs/heads/D
490 refs/tags/C
491 refs/heads/B
492 refs/tags/A
493 refs/heads/E
494 refs/tags/B
495 refs/tags/E
496 refs/tags/D
497 EOF
498 sort <input >expect &&
499 (
500 echo refs/heads/E &&
501 echo refs/tags/E &&
502 cat input
503 ) >input.dup
504'
505
4316ff30 506test_expect_success 'setup fetch refs from cmdline v[12]' '
8a1b0978 507 cp -r client client0 &&
4316ff30
JT
508 cp -r client client1 &&
509 cp -r client client2
b2a9f4da
IT
510'
511
8a1b0978 512for version in '' 0 1 2
4316ff30
JT
513do
514 test_expect_success "protocol.version=$version fetch refs from cmdline" "
515 (
516 cd client$version &&
517 GIT_TEST_PROTOCOL_VERSION=$version git fetch-pack --no-progress .. \$(cat ../input)
518 ) >output &&
519 cut -d ' ' -f 2 <output | sort >actual &&
520 test_cmp expect actual
521 "
522done
523
b2a9f4da
IT
524test_expect_success 'fetch refs from stdin' '
525 (
526 cd client &&
527 git fetch-pack --stdin --no-progress .. <../input
528 ) >output &&
529 cut -d " " -f 2 <output | sort >actual &&
530 test_cmp expect actual
531'
532
533test_expect_success 'fetch mixed refs from cmdline and stdin' '
534 (
535 cd client &&
536 tail -n +5 ../input |
537 git fetch-pack --stdin --no-progress .. $(head -n 4 ../input)
538 ) >output &&
539 cut -d " " -f 2 <output | sort >actual &&
540 test_cmp expect actual
541'
542
543test_expect_success 'test duplicate refs from stdin' '
544 (
545 cd client &&
4c58f13b 546 git fetch-pack --stdin --no-progress .. <../input.dup
b2a9f4da
IT
547 ) >output &&
548 cut -d " " -f 2 <output | sort >actual &&
549 test_cmp expect actual
550'
551
3b082004
MH
552test_expect_success 'set up tests of missing reference' '
553 cat >expect-error <<-\EOF
554 error: no such remote ref refs/heads/xyzzy
555 EOF
556'
557
778e7543 558test_expect_success 'test lonely missing ref' '
3b082004
MH
559 (
560 cd client &&
fa06eb6f
SG
561 test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy 2>../error-m
562 ) &&
1108cea7 563 test_cmp expect-error error-m
3b082004
MH
564'
565
566test_expect_success 'test missing ref after existing' '
567 (
568 cd client &&
fa06eb6f
SG
569 test_must_fail git fetch-pack --no-progress .. refs/heads/A refs/heads/xyzzy 2>../error-em
570 ) &&
1108cea7 571 test_cmp expect-error error-em
3b082004
MH
572'
573
574test_expect_success 'test missing ref before existing' '
575 (
576 cd client &&
fa06eb6f
SG
577 test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy refs/heads/A 2>../error-me
578 ) &&
1108cea7 579 test_cmp expect-error error-me
3b082004
MH
580'
581
5f0fc645 582test_expect_success 'test --all, --depth, and explicit head' '
8db43d29
MH
583 (
584 cd client &&
585 git fetch-pack --no-progress --all --depth=1 .. refs/heads/A
586 ) >out-adh 2>error-adh
587'
588
5f0fc645 589test_expect_success 'test --all, --depth, and explicit tag' '
8db43d29
MH
590 git tag OLDTAG refs/heads/B~5 &&
591 (
592 cd client &&
593 git fetch-pack --no-progress --all --depth=1 .. refs/tags/OLDTAG
594 ) >out-adt 2>error-adt
595'
596
e9502c0a
JK
597test_expect_success 'test --all with tag to non-tip' '
598 git commit --allow-empty -m non-tip &&
599 git commit --allow-empty -m tip &&
600 git tag -m "annotated" non-tip HEAD^ &&
601 (
602 cd client &&
603 git fetch-pack --all ..
604 )
605'
606
c12c9df5
KS
607test_expect_success 'test --all wrt tag to non-commits' '
608 # create tag-to-{blob,tree,commit,tag}, making sure all tagged objects
609 # are reachable only via created tag references.
610 blob=$(echo "hello blob" | git hash-object -t blob -w --stdin) &&
611 git tag -a -m "tag -> blob" tag-to-blob $blob &&
5e834a4f 612
c12c9df5
KS
613 tree=$(printf "100644 blob $blob\tfile" | git mktree) &&
614 git tag -a -m "tag -> tree" tag-to-tree $tree &&
5e834a4f 615
c12c9df5
KS
616 tree2=$(printf "100644 blob $blob\tfile2" | git mktree) &&
617 commit=$(git commit-tree -m "hello commit" $tree) &&
618 git tag -a -m "tag -> commit" tag-to-commit $commit &&
5e834a4f 619
c12c9df5 620 blob2=$(echo "hello blob2" | git hash-object -t blob -w --stdin) &&
5e834a4f
JK
621 tag=$(git mktag <<-EOF
622 object $blob2
623 type blob
624 tag tag-to-blob2
625 tagger author A U Thor <author@example.com> 0 +0000
626
627 hello tag
628 EOF
629 ) &&
c12c9df5 630 git tag -a -m "tag -> tag" tag-to-tag $tag &&
5e834a4f 631
c12c9df5
KS
632 # `fetch-pack --all` should succeed fetching all those objects.
633 mkdir fetchall &&
634 (
635 cd fetchall &&
636 git init &&
637 git fetch-pack --all .. &&
638 git cat-file blob $blob >/dev/null &&
639 git cat-file tree $tree >/dev/null &&
640 git cat-file commit $commit >/dev/null &&
641 git cat-file tag $tag >/dev/null
642 )
643'
644
6da8bdcb
NTND
645test_expect_success 'shallow fetch with tags does not break the repository' '
646 mkdir repo1 &&
647 (
648 cd repo1 &&
649 git init &&
650 test_commit 1 &&
651 test_commit 2 &&
652 test_commit 3 &&
653 mkdir repo2 &&
654 cd repo2 &&
655 git init &&
3275f4e8 656 git fetch --depth=2 ../.git main:branch &&
6da8bdcb
NTND
657 git fsck
658 )
659'
4a8d202c
GSF
660
661test_expect_success 'fetch-pack can fetch a raw sha1' '
662 git init hidden &&
663 (
664 cd hidden &&
665 test_commit 1 &&
666 test_commit 2 &&
667 git update-ref refs/hidden/one HEAD^ &&
668 git config transfer.hiderefs refs/hidden &&
669 git config uploadpack.allowtipsha1inwant true
670 ) &&
671 git fetch-pack hidden $(git -C hidden rev-parse refs/hidden/one)
672'
673
fdb69d33
JT
674test_expect_success 'fetch-pack can fetch a raw sha1 that is advertised as a ref' '
675 rm -rf server client &&
676 git init server &&
677 test_commit -C server 1 &&
678
679 git init client &&
680 git -C client fetch-pack ../server \
3275f4e8 681 $(git -C server rev-parse refs/heads/main)
fdb69d33
JT
682'
683
684test_expect_success 'fetch-pack can fetch a raw sha1 overlapping a named ref' '
685 rm -rf server client &&
686 git init server &&
687 test_commit -C server 1 &&
688 test_commit -C server 2 &&
689
690 git init client &&
691 git -C client fetch-pack ../server \
692 $(git -C server rev-parse refs/tags/1) refs/tags/1
693'
694
695test_expect_success 'fetch-pack cannot fetch a raw sha1 that is not advertised as a ref' '
696 rm -rf server &&
697
698 git init server &&
699 test_commit -C server 5 &&
700 git -C server tag -d 5 &&
701 test_commit -C server 6 &&
702
703 git init client &&
ab0c5f50
JT
704 # Some protocol versions (e.g. 2) support fetching
705 # unadvertised objects, so restrict this test to v0.
8a1b0978 706 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -C client fetch-pack ../server \
3275f4e8 707 $(git -C server rev-parse refs/heads/main^) 2>err &&
6789275d 708 test_grep "Server does not allow request for unadvertised object" err
fdb69d33
JT
709'
710
854aeb7b
TB
711check_prot_path () {
712 cat >expected <<-EOF &&
713 Diag: url=$1
714 Diag: protocol=$2
715 Diag: path=$3
716 EOF
717 git fetch-pack --diag-url "$1" | grep -v hostandport= >actual &&
718 test_cmp expected actual
719}
720
3f55ccab 721check_prot_host_port_path () {
3f55ccab
TB
722 case "$2" in
723 *ssh*)
724 pp=ssh
725 uah=userandhost
726 ehost=$(echo $3 | tr -d "[]")
727 diagport="Diag: port=$4"
728 ;;
729 *)
730 pp=$p
731 uah=hostandport
732 ehost=$(echo $3$4 | sed -e "s/22$/:22/" -e "s/NONE//")
733 diagport=""
734 ;;
735 esac
736 cat >exp <<-EOF &&
854aeb7b 737 Diag: url=$1
3f55ccab
TB
738 Diag: protocol=$pp
739 Diag: $uah=$ehost
740 $diagport
741 Diag: path=$5
854aeb7b 742 EOF
3f55ccab 743 grep -v "^$" exp >expected
854aeb7b
TB
744 git fetch-pack --diag-url "$1" >actual &&
745 test_cmp expected actual
746}
747
748for r in repo re:po re/po
749do
750 # git or ssh with scheme
751 for p in "ssh+git" "git+ssh" git ssh
752 do
3f55ccab 753 for h in host user@host user@[::1] user@::1
854aeb7b 754 do
6b6c5f7a
TB
755 for c in "" :
756 do
757 test_expect_success "fetch-pack --diag-url $p://$h$c/$r" '
758 check_prot_host_port_path $p://$h/$r $p "$h" NONE "/$r"
759 '
760 # "/~" -> "~" conversion
761 test_expect_success "fetch-pack --diag-url $p://$h$c/~$r" '
762 check_prot_host_port_path $p://$h/~$r $p "$h" NONE "~$r"
763 '
764 done
3f55ccab
TB
765 done
766 for h in host User@host User@[::1]
767 do
768 test_expect_success "fetch-pack --diag-url $p://$h:22/$r" '
769 check_prot_host_port_path $p://$h:22/$r $p "$h" 22 "/$r"
854aeb7b
TB
770 '
771 done
772 done
773 # file with scheme
774 for p in file
775 do
ebb8d2c9 776 test_expect_success !MINGW "fetch-pack --diag-url $p://$h/$r" '
854aeb7b
TB
777 check_prot_path $p://$h/$r $p "/$r"
778 '
ebb8d2c9
TB
779 test_expect_success MINGW "fetch-pack --diag-url $p://$h/$r" '
780 check_prot_path $p://$h/$r $p "//$h/$r"
781 '
782 test_expect_success MINGW "fetch-pack --diag-url $p:///$r" '
783 check_prot_path $p:///$r $p "/$r"
784 '
854aeb7b 785 # No "/~" -> "~" conversion for file
ebb8d2c9 786 test_expect_success !MINGW "fetch-pack --diag-url $p://$h/~$r" '
854aeb7b
TB
787 check_prot_path $p://$h/~$r $p "/~$r"
788 '
ebb8d2c9
TB
789 test_expect_success MINGW "fetch-pack --diag-url $p://$h/~$r" '
790 check_prot_path $p://$h/~$r $p "//$h/~$r"
791 '
854aeb7b 792 done
6a599748
TB
793 # file without scheme
794 for h in nohost nohost:12 [::1] [::1]:23 [ [:aa
795 do
796 test_expect_success "fetch-pack --diag-url ./$h:$r" '
797 check_prot_path ./$h:$r $p "./$h:$r"
798 '
799 # No "/~" -> "~" conversion for file
800 test_expect_success "fetch-pack --diag-url ./$p:$h/~$r" '
801 check_prot_path ./$p:$h/~$r $p "./$p:$h/~$r"
802 '
803 done
804 #ssh without scheme
805 p=ssh
806 for h in host [::1]
807 do
6a599748 808 test_expect_success "fetch-pack --diag-url $h:$r" '
3f55ccab 809 check_prot_host_port_path $h:$r $p "$h" NONE "$r"
6a599748
TB
810 '
811 # Do "/~" -> "~" conversion
812 test_expect_success "fetch-pack --diag-url $h:/~$r" '
3f55ccab 813 check_prot_host_port_path $h:/~$r $p "$h" NONE "~$r"
6a599748
TB
814 '
815 done
854aeb7b 816done
6da8bdcb 817
c59ab2e5
TB
818test_expect_success MINGW 'fetch-pack --diag-url file://c:/repo' '
819 check_prot_path file://c:/repo file c:/repo
820'
821test_expect_success MINGW 'fetch-pack --diag-url c:repo' '
822 check_prot_path c:repo file c:repo
823'
824
6d43a0ce
NTND
825test_expect_success 'clone shallow since ...' '
826 test_create_repo shallow-since &&
827 (
828 cd shallow-since &&
829 GIT_COMMITTER_DATE="100000000 +0700" git commit --allow-empty -m one &&
830 GIT_COMMITTER_DATE="200000000 +0700" git commit --allow-empty -m two &&
831 GIT_COMMITTER_DATE="300000000 +0700" git commit --allow-empty -m three &&
832 git clone --shallow-since "300000000 +0700" "file://$(pwd)/." ../shallow11 &&
833 git -C ../shallow11 log --pretty=tformat:%s HEAD >actual &&
834 echo three >expected &&
835 test_cmp expected actual
836 )
837'
838
839test_expect_success 'fetch shallow since ...' '
b8f58c20
RC
840 GIT_TRACE2_EVENT=$(pwd)/shallow11/trace2_event \
841 git -C shallow11 fetch --shallow-since "200000000 +0700" origin &&
3275f4e8 842 git -C shallow11 log --pretty=tformat:%s origin/main >actual &&
6d43a0ce
NTND
843 cat >expected <<-\EOF &&
844 three
845 two
846 EOF
b8f58c20
RC
847 test_cmp expected actual &&
848 grep \"fetch-info\".*\"deepen-since\":true shallow11/trace2_event
6d43a0ce
NTND
849'
850
e34de73c
NTND
851test_expect_success 'clone shallow since selects no commits' '
852 test_create_repo shallow-since-the-future &&
853 (
854 cd shallow-since-the-future &&
855 GIT_COMMITTER_DATE="100000000 +0700" git commit --allow-empty -m one &&
856 GIT_COMMITTER_DATE="200000000 +0700" git commit --allow-empty -m two &&
857 GIT_COMMITTER_DATE="300000000 +0700" git commit --allow-empty -m three &&
858 test_must_fail git clone --shallow-since "900000000 +0700" "file://$(pwd)/." ../shallow111
859 )
860'
861
6abada18
JK
862# A few subtle things about the request in this test:
863#
864# - the server must have commit-graphs present and enabled
865#
866# - the history is such that our want/have share a common ancestor ("base"
867# here)
868#
869# - we send only a single have, which is fewer than a normal client would
870# send. This ensures that we don't parse "base" up front with
871# parse_object(), but rather traverse to it as a parent while deciding if we
872# can stop the "have" negotiation, and call parse_commit(). The former
873# sees the actual object data and so always loads the three oid, whereas the
874# latter will try to load it lazily.
875#
876# - we must use protocol v2, because it handles the "have" negotiation before
877# processing the shallow directives
878#
879test_expect_success 'shallow since with commit graph and already-seen commit' '
880 test_create_repo shallow-since-graph &&
881 (
882 cd shallow-since-graph &&
883 test_commit base &&
3275f4e8 884 test_commit main &&
6abada18
JK
885 git checkout -b other HEAD^ &&
886 test_commit other &&
887 git commit-graph write --reachable &&
888 git config core.commitGraph true &&
889
890 GIT_PROTOCOL=version=2 git upload-pack . <<-EOF >/dev/null
891 0012command=fetch
f0af95f4 892 $(echo "object-format=$(test_oid algo)" | packetize)
6abada18 893 00010013deepen-since 1
f0af95f4 894 $(echo "want $(git rev-parse other)" | packetize)
3275f4e8 895 $(echo "have $(git rev-parse main)" | packetize)
6abada18
JK
896 0000
897 EOF
898 )
899'
900
cdc37277
NTND
901test_expect_success 'shallow clone exclude tag two' '
902 test_create_repo shallow-exclude &&
903 (
904 cd shallow-exclude &&
905 test_commit one &&
906 test_commit two &&
907 test_commit three &&
908 git clone --shallow-exclude two "file://$(pwd)/." ../shallow12 &&
909 git -C ../shallow12 log --pretty=tformat:%s HEAD >actual &&
910 echo three >expected &&
911 test_cmp expected actual
912 )
913'
914
915test_expect_success 'fetch exclude tag one' '
916 git -C shallow12 fetch --shallow-exclude one origin &&
3275f4e8 917 git -C shallow12 log --pretty=tformat:%s origin/main >actual &&
cdc37277
NTND
918 test_write_lines three two >expected &&
919 test_cmp expected actual
920'
921
cccf74e2
NTND
922test_expect_success 'fetching deepen' '
923 test_create_repo shallow-deepen &&
924 (
925 cd shallow-deepen &&
926 test_commit one &&
927 test_commit two &&
928 test_commit three &&
929 git clone --depth 1 "file://$(pwd)/." deepen &&
930 test_commit four &&
3275f4e8 931 git -C deepen log --pretty=tformat:%s main >actual &&
cccf74e2
NTND
932 echo three >expected &&
933 test_cmp expected actual &&
934 git -C deepen fetch --deepen=1 &&
3275f4e8 935 git -C deepen log --pretty=tformat:%s origin/main >actual &&
cccf74e2
NTND
936 cat >expected <<-\EOF &&
937 four
938 three
939 two
940 EOF
941 test_cmp expected actual
942 )
943'
944
a68c5b9e
EN
945test_negotiation_algorithm_default () {
946 test_when_finished rm -rf clientv0 clientv2 &&
af1c90d1
JT
947 rm -rf server client &&
948 git init server &&
949 test_commit -C server both_have_1 &&
950 git -C server tag -d both_have_1 &&
951 test_commit -C server both_have_2 &&
952
953 git clone server client &&
954 test_commit -C server server_has &&
955 test_commit -C client client_has &&
956
957 # In both protocol v0 and v2, ensure that the parent of both_have_2 is
958 # not sent as a "have" line. The client should know that the server has
959 # both_have_2, so it only needs to inform the server that it has
960 # both_have_2, and the server can infer the rest.
961
962 rm -f trace &&
963 cp -r client clientv0 &&
964 GIT_TRACE_PACKET="$(pwd)/trace" git -C clientv0 \
a68c5b9e 965 "$@" fetch origin server_has both_have_2 &&
af1c90d1
JT
966 grep "have $(git -C client rev-parse client_has)" trace &&
967 grep "have $(git -C client rev-parse both_have_2)" trace &&
968 ! grep "have $(git -C client rev-parse both_have_2^)" trace &&
969
970 rm -f trace &&
971 cp -r client clientv2 &&
972 GIT_TRACE_PACKET="$(pwd)/trace" git -C clientv2 -c protocol.version=2 \
a68c5b9e 973 "$@" fetch origin server_has both_have_2 &&
af1c90d1
JT
974 grep "have $(git -C client rev-parse client_has)" trace &&
975 grep "have $(git -C client rev-parse both_have_2)" trace &&
976 ! grep "have $(git -C client rev-parse both_have_2^)" trace
a68c5b9e
EN
977}
978
979test_expect_success 'use ref advertisement to prune "have" lines sent' '
980 test_negotiation_algorithm_default
981'
982
983test_expect_success 'same as last but with config overrides' '
984 test_negotiation_algorithm_default \
985 -c feature.experimental=true \
714edc62 986 -c fetch.negotiationAlgorithm=consecutive
af1c90d1
JT
987'
988
a9a136c2
EN
989test_expect_success 'ensure bogus fetch.negotiationAlgorithm yields error' '
990 test_when_finished rm -rf clientv0 &&
991 cp -r client clientv0 &&
992 test_must_fail git -C clientv0 --fetch.negotiationAlgorithm=bogus \
993 fetch origin server_has both_have_2
af1c90d1
JT
994'
995
0b6069fe
JT
996test_expect_success 'filtering by size' '
997 rm -rf server client &&
998 test_create_repo server &&
999 test_commit -C server one &&
1000 test_config -C server uploadpack.allowfilter 1 &&
1001
1002 test_create_repo client &&
b8f58c20
RC
1003 GIT_TRACE2_EVENT=$(pwd)/client/trace2_event \
1004 git -C client fetch-pack --filter=blob:limit=0 ../server HEAD &&
0b6069fe
JT
1005
1006 # Ensure that object is not inadvertently fetched
07ef3c66
JN
1007 commit=$(git -C server rev-parse HEAD) &&
1008 blob=$(git hash-object server/one.t) &&
1009 git -C client rev-list --objects --missing=allow-any "$commit" >oids &&
b8f58c20
RC
1010 ! grep "$blob" oids &&
1011
1012 grep \"fetch-info\".*\"filter\":\"blob:limit\" client/trace2_event
0b6069fe
JT
1013'
1014
1015test_expect_success 'filtering by size has no effect if support for it is not advertised' '
1016 rm -rf server client &&
1017 test_create_repo server &&
1018 test_commit -C server one &&
1019
1020 test_create_repo client &&
1021 git -C client fetch-pack --filter=blob:limit=0 ../server HEAD 2> err &&
1022
1023 # Ensure that object is fetched
07ef3c66
JN
1024 commit=$(git -C server rev-parse HEAD) &&
1025 blob=$(git hash-object server/one.t) &&
1026 git -C client rev-list --objects --missing=allow-any "$commit" >oids &&
1027 grep "$blob" oids &&
0b6069fe 1028
6789275d 1029 test_grep "filtering not recognized by server" err
0b6069fe
JT
1030'
1031
acb0c572
JH
1032fetch_filter_blob_limit_zero () {
1033 SERVER="$1"
1034 URL="$2"
1035
1036 rm -rf "$SERVER" client &&
1037 test_create_repo "$SERVER" &&
1038 test_commit -C "$SERVER" one &&
1039 test_config -C "$SERVER" uploadpack.allowfilter 1 &&
1040
1041 git clone "$URL" client &&
acb0c572
JH
1042
1043 test_commit -C "$SERVER" two &&
1044
1045 git -C client fetch --filter=blob:limit=0 origin HEAD:somewhere &&
1046
1047 # Ensure that commit is fetched, but blob is not
07ef3c66
JN
1048 commit=$(git -C "$SERVER" rev-parse two) &&
1049 blob=$(git hash-object server/two.t) &&
1050 git -C client rev-list --objects --missing=allow-any "$commit" >oids &&
1051 grep "$commit" oids &&
1052 ! grep "$blob" oids
acb0c572
JH
1053}
1054
1055test_expect_success 'fetch with --filter=blob:limit=0' '
1056 fetch_filter_blob_limit_zero server server
1057'
1058
1059. "$TEST_DIRECTORY"/lib-httpd.sh
1060start_httpd
1061
1062test_expect_success 'fetch with --filter=blob:limit=0 and HTTP' '
1063 fetch_filter_blob_limit_zero "$HTTPD_DOCUMENT_ROOT_PATH/server" "$HTTPD_URL/smart/server"
1064'
1065
decfe05b
SG
1066# DO NOT add non-httpd-specific tests here, because the last part of this
1067# test script is only executed when httpd is available and enabled.
1068
6b17c674 1069test_done