Merge branch 'jk/shell-portability'
[git/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
6b17c674
JS
8. ./test-lib.sh
9
10# Test fetch-pack/upload-pack pair.
11
12# Some convenience functions
13
1010437d 14add () {
3902985a
SB
15 name=$1 &&
16 text="$@" &&
2feed907 17 branch=$(echo $name | sed -e 's/^\(.\).*$/\1/') &&
3902985a 18 parents="" &&
6b17c674 19
3902985a 20 shift &&
6b17c674 21 while test $1; do
3902985a 22 parents="$parents -p $1" &&
6b17c674 23 shift
3902985a 24 done &&
6b17c674 25
3902985a
SB
26 echo "$text" > test.txt &&
27 git update-index --add test.txt &&
28 tree=$(git write-tree) &&
6b17c674 29 # make sure timestamps are in correct order
3902985a
SB
30 test_tick &&
31 commit=$(echo "$text" | git commit-tree $tree $parents) &&
32 eval "$name=$commit; export $name" &&
33 echo $commit > .git/refs/heads/$branch &&
6b17c674
JS
34 eval ${branch}TIP=$commit
35}
36
1010437d 37pull_to_client () {
3902985a
SB
38 number=$1 &&
39 heads=$2 &&
40 count=$3 &&
41 test_expect_success "$number pull" '
42 (
43 cd client &&
44 git fetch-pack -k -v .. $heads &&
45
46 case "$heads" in
47 *A*)
48 echo $ATIP > .git/refs/heads/A;;
49 esac &&
50 case "$heads" in *B*)
51 echo $BTIP > .git/refs/heads/B;;
52 esac &&
2feed907
EP
53 git symbolic-ref HEAD refs/heads/$(echo $heads \
54 | sed -e "s/^\(.\).*$/\1/") &&
3902985a
SB
55
56 git fsck --full &&
57
58 mv .git/objects/pack/pack-* . &&
2feed907 59 p=$(ls -1 pack-*.pack) &&
3902985a
SB
60 git unpack-objects <$p &&
61 git fsck --full &&
62
2feed907
EP
63 idx=$(echo pack-*.idx) &&
64 pack_count=$(git show-index <$idx | wc -l) &&
3902985a
SB
65 test $pack_count = $count &&
66 rm -f pack-*
67 )
68 '
6b17c674
JS
69}
70
71# Here begins the actual testing
72
73# A1 - ... - A20 - A21
74# \
75# B1 - B2 - .. - B70
76
77# client pulls A20, B1. Then tracks only B. Then pulls A.
78
3902985a 79test_expect_success 'setup' '
6b17c674 80 mkdir client &&
3902985a
SB
81 (
82 cd client &&
83 git init &&
84 git config transfer.unpacklimit 0
85 ) &&
86 add A1 &&
87 prev=1 &&
88 cur=2 &&
89 while [ $cur -le 10 ]; do
90 add A$cur $(eval echo \$A$prev) &&
91 prev=$cur &&
92 cur=$(($cur+1))
93 done &&
a48fcd83 94 add B1 $A1 &&
3902985a
SB
95 echo $ATIP > .git/refs/heads/A &&
96 echo $BTIP > .git/refs/heads/B &&
97 git symbolic-ref HEAD refs/heads/B
98'
6b17c674 99
e9d866e3 100pull_to_client 1st "refs/heads/B refs/heads/A" $((11*3))
6b17c674 101
3902985a
SB
102test_expect_success 'post 1st pull setup' '
103 add A11 $A10 &&
104 prev=1 &&
105 cur=2 &&
106 while [ $cur -le 65 ]; do
107 add B$cur $(eval echo \$B$prev) &&
108 prev=$cur &&
109 cur=$(($cur+1))
110 done
111'
6b17c674 112
e9d866e3 113pull_to_client 2nd "refs/heads/B" $((64*3))
6b17c674 114
e9d866e3 115pull_to_client 3rd "refs/heads/A" $((1*3))
16ad3579 116
3e6e0edd
NTND
117test_expect_success 'single branch clone' '
118 git clone --single-branch "file://$(pwd)/." singlebranch
119'
120
121test_expect_success 'single branch object count' '
122 GIT_DIR=singlebranch/.git git count-objects -v |
123 grep "^in-pack:" > count.singlebranch &&
124 echo "in-pack: 198" >expected &&
125 test_cmp expected count.singlebranch
126'
127
0ec4b165
NTND
128test_expect_success 'single given branch clone' '
129 git clone --single-branch --branch A "file://$(pwd)/." branch-a &&
130 test_must_fail git --git-dir=branch-a/.git rev-parse origin/B
131'
132
682c7d2f
NTND
133test_expect_success 'clone shallow depth 1' '
134 git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0 &&
2feed907 135 test "$(git --git-dir=shallow0/.git rev-list --count HEAD)" = 1
682c7d2f
NTND
136'
137
6035d6aa
NTND
138test_expect_success 'clone shallow depth 1 with fsck' '
139 git config --global fetch.fsckobjects true &&
140 git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0fsck &&
2feed907 141 test "$(git --git-dir=shallow0fsck/.git rev-list --count HEAD)" = 1 &&
6035d6aa
NTND
142 git config --global --unset fetch.fsckobjects
143'
144
3902985a 145test_expect_success 'clone shallow' '
3e6e0edd 146 git clone --no-single-branch --depth 2 "file://$(pwd)/." shallow
3902985a 147'
37818d7d 148
682c7d2f 149test_expect_success 'clone shallow depth count' '
2feed907 150 test "$(git --git-dir=shallow/.git rev-list --count HEAD)" = 2
682c7d2f
NTND
151'
152
3902985a
SB
153test_expect_success 'clone shallow object count' '
154 (
155 cd shallow &&
156 git count-objects -v
157 ) > count.shallow &&
682c7d2f 158 grep "^in-pack: 12" count.shallow
37818d7d 159'
16ad3579 160
3902985a
SB
161test_expect_success 'clone shallow object count (part 2)' '
162 sed -e "/^in-pack:/d" -e "/^packs:/d" -e "/^size-pack:/d" \
163 -e "/: 0$/d" count.shallow > count_output &&
164 ! test -s count_output
165'
16ad3579 166
3902985a
SB
167test_expect_success 'fsck in shallow repo' '
168 (
169 cd shallow &&
170 git fsck --full
171 )
172'
16ad3579 173
86386829
NP
174test_expect_success 'simple fetch in shallow repo' '
175 (
176 cd shallow &&
177 git fetch
178 )
179'
180
181test_expect_success 'no changes expected' '
182 (
183 cd shallow &&
184 git count-objects -v
185 ) > count.shallow.2 &&
186 cmp count.shallow count.shallow.2
187'
188
189test_expect_success 'fetch same depth in shallow repo' '
190 (
191 cd shallow &&
192 git fetch --depth=2
193 )
194'
195
196test_expect_success 'no changes expected' '
197 (
198 cd shallow &&
199 git count-objects -v
200 ) > count.shallow.3 &&
201 cmp count.shallow count.shallow.3
202'
16ad3579 203
3902985a
SB
204test_expect_success 'add two more' '
205 add B66 $B65 &&
206 add B67 $B66
207'
16ad3579 208
3902985a
SB
209test_expect_success 'pull in shallow repo' '
210 (
211 cd shallow &&
212 git pull .. B
213 )
214'
16ad3579 215
3902985a
SB
216test_expect_success 'clone shallow object count' '
217 (
218 cd shallow &&
219 git count-objects -v
220 ) > count.shallow &&
221 grep "^count: 6" count.shallow
222'
16ad3579 223
3902985a
SB
224test_expect_success 'add two more (part 2)' '
225 add B68 $B67 &&
226 add B69 $B68
227'
16ad3579 228
3902985a
SB
229test_expect_success 'deepening pull in shallow repo' '
230 (
231 cd shallow &&
232 git pull --depth 4 .. B
233 )
234'
16ad3579 235
3902985a
SB
236test_expect_success 'clone shallow object count' '
237 (
238 cd shallow &&
239 git count-objects -v
240 ) > count.shallow &&
241 grep "^count: 12" count.shallow
242'
16ad3579 243
3902985a
SB
244test_expect_success 'deepening fetch in shallow repo' '
245 (
246 cd shallow &&
247 git fetch --depth 4 .. A:A
248 )
249'
16ad3579 250
3902985a
SB
251test_expect_success 'clone shallow object count' '
252 (
253 cd shallow &&
254 git count-objects -v
255 ) > count.shallow &&
256 grep "^count: 18" count.shallow
257'
16ad3579 258
3902985a
SB
259test_expect_success 'pull in shallow repo with missing merge base' '
260 (
261 cd shallow &&
e379fdf3
JH
262 git fetch --depth 4 .. A
263 test_must_fail git merge --allow-unrelated-histories FETCH_HEAD
3902985a
SB
264 )
265'
16ad3579 266
86386829
NP
267test_expect_success 'additional simple shallow deepenings' '
268 (
269 cd shallow &&
270 git fetch --depth=8 &&
271 git fetch --depth=10 &&
272 git fetch --depth=11
273 )
274'
275
682c7d2f 276test_expect_success 'clone shallow depth count' '
2feed907 277 test "$(git --git-dir=shallow/.git rev-list --count HEAD)" = 11
682c7d2f
NTND
278'
279
86386829
NP
280test_expect_success 'clone shallow object count' '
281 (
282 cd shallow &&
e379fdf3 283 git prune &&
86386829
NP
284 git count-objects -v
285 ) > count.shallow &&
e379fdf3 286 grep "^count: 54" count.shallow
86386829
NP
287'
288
4dcb167f
NTND
289test_expect_success 'fetch --no-shallow on full repo' '
290 test_must_fail git fetch --noshallow
291'
292
293test_expect_success 'fetch --depth --no-shallow' '
294 (
295 cd shallow &&
296 test_must_fail git fetch --depth=1 --noshallow
297 )
298'
299
300test_expect_success 'turn shallow to complete repository' '
301 (
302 cd shallow &&
303 git fetch --unshallow &&
304 ! test -f .git/shallow &&
305 git fsck --full
306 )
307'
308
3e6e0edd
NTND
309test_expect_success 'clone shallow without --no-single-branch' '
310 git clone --depth 1 "file://$(pwd)/." shallow2
311'
312
313test_expect_success 'clone shallow object count' '
314 (
315 cd shallow2 &&
316 git count-objects -v
317 ) > count.shallow2 &&
682c7d2f 318 grep "^in-pack: 3" count.shallow2
3e6e0edd
NTND
319'
320
321test_expect_success 'clone shallow with --branch' '
322 git clone --depth 1 --branch A "file://$(pwd)/." shallow3
323'
324
325test_expect_success 'clone shallow object count' '
682c7d2f 326 echo "in-pack: 3" > count3.expected &&
3e6e0edd
NTND
327 GIT_DIR=shallow3/.git git count-objects -v |
328 grep "^in-pack" > count3.actual &&
329 test_cmp count3.expected count3.actual
330'
331
3e6e0edd
NTND
332test_expect_success 'clone shallow with detached HEAD' '
333 git checkout HEAD^ &&
334 git clone --depth 1 "file://$(pwd)/." shallow5 &&
335 git checkout - &&
336 GIT_DIR=shallow5/.git git rev-parse HEAD >actual &&
337 git rev-parse HEAD^ >expected &&
338 test_cmp expected actual
339'
340
341test_expect_success 'shallow clone pulling tags' '
342 git tag -a -m A TAGA1 A &&
343 git tag -a -m B TAGB1 B &&
344 git tag TAGA2 A &&
345 git tag TAGB2 B &&
346 git clone --depth 1 "file://$(pwd)/." shallow6 &&
347
348 cat >taglist.expected <<\EOF &&
349TAGB1
350TAGB2
351EOF
352 GIT_DIR=shallow6/.git git tag -l >taglist.actual &&
353 test_cmp taglist.expected taglist.actual &&
354
682c7d2f 355 echo "in-pack: 4" > count6.expected &&
3e6e0edd
NTND
356 GIT_DIR=shallow6/.git git count-objects -v |
357 grep "^in-pack" > count6.actual &&
358 test_cmp count6.expected count6.actual
359'
360
5a7d5b68
NTND
361test_expect_success 'shallow cloning single tag' '
362 git clone --depth 1 --branch=TAGB1 "file://$(pwd)/." shallow7 &&
363 cat >taglist.expected <<\EOF &&
364TAGB1
365TAGB2
366EOF
367 GIT_DIR=shallow7/.git git tag -l >taglist.actual &&
368 test_cmp taglist.expected taglist.actual &&
369
682c7d2f 370 echo "in-pack: 4" > count7.expected &&
5a7d5b68
NTND
371 GIT_DIR=shallow7/.git git count-objects -v |
372 grep "^in-pack" > count7.actual &&
373 test_cmp count7.expected count7.actual
374'
375
06f15bf1
JK
376test_expect_success 'clone shallow with packed refs' '
377 git pack-refs --all &&
378 git clone --depth 1 --branch A "file://$(pwd)/." shallow8 &&
379 echo "in-pack: 4" > count8.expected &&
380 GIT_DIR=shallow8/.git git count-objects -v |
381 grep "^in-pack" > count8.actual &&
382 test_cmp count8.expected count8.actual
383'
384
71d5f938
MH
385test_expect_success 'fetch in shallow repo unreachable shallow objects' '
386 (
387 git clone --bare --branch B --single-branch "file://$(pwd)/." no-reflog &&
388 git clone --depth 1 "file://$(pwd)/no-reflog" shallow9 &&
389 cd no-reflog &&
390 git tag -d TAGB1 TAGB2 &&
391 git update-ref refs/heads/B B~~ &&
392 git gc --prune=now &&
393 cd ../shallow9 &&
394 git fetch origin &&
395 git fsck --no-dangling
396 )
397'
f21d2a78
MK
398test_expect_success 'fetch creating new shallow root' '
399 (
400 git clone "file://$(pwd)/." shallow10 &&
401 git commit --allow-empty -m empty &&
402 cd shallow10 &&
403 git fetch --depth=1 --progress 2>actual &&
404 # This should fetch only the empty commit, no tree or
405 # blob objects
406 grep "remote: Total 1" actual
407 )
408'
71d5f938 409
b2a9f4da
IT
410test_expect_success 'setup tests for the --stdin parameter' '
411 for head in C D E F
412 do
413 add $head
414 done &&
415 for head in A B C D E F
416 do
417 git tag $head $head
418 done &&
99094a7a 419 cat >input <<-\EOF &&
b2a9f4da
IT
420 refs/heads/C
421 refs/heads/A
422 refs/heads/D
423 refs/tags/C
424 refs/heads/B
425 refs/tags/A
426 refs/heads/E
427 refs/tags/B
428 refs/tags/E
429 refs/tags/D
430 EOF
431 sort <input >expect &&
432 (
433 echo refs/heads/E &&
434 echo refs/tags/E &&
435 cat input
436 ) >input.dup
437'
438
439test_expect_success 'fetch refs from cmdline' '
440 (
441 cd client &&
442 git fetch-pack --no-progress .. $(cat ../input)
443 ) >output &&
444 cut -d " " -f 2 <output | sort >actual &&
445 test_cmp expect actual
446'
447
448test_expect_success 'fetch refs from stdin' '
449 (
450 cd client &&
451 git fetch-pack --stdin --no-progress .. <../input
452 ) >output &&
453 cut -d " " -f 2 <output | sort >actual &&
454 test_cmp expect actual
455'
456
457test_expect_success 'fetch mixed refs from cmdline and stdin' '
458 (
459 cd client &&
460 tail -n +5 ../input |
461 git fetch-pack --stdin --no-progress .. $(head -n 4 ../input)
462 ) >output &&
463 cut -d " " -f 2 <output | sort >actual &&
464 test_cmp expect actual
465'
466
467test_expect_success 'test duplicate refs from stdin' '
468 (
469 cd client &&
4c58f13b 470 git fetch-pack --stdin --no-progress .. <../input.dup
b2a9f4da
IT
471 ) >output &&
472 cut -d " " -f 2 <output | sort >actual &&
473 test_cmp expect actual
474'
475
3b082004
MH
476test_expect_success 'set up tests of missing reference' '
477 cat >expect-error <<-\EOF
478 error: no such remote ref refs/heads/xyzzy
479 EOF
480'
481
778e7543 482test_expect_success 'test lonely missing ref' '
3b082004
MH
483 (
484 cd client &&
485 test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy
486 ) >/dev/null 2>error-m &&
487 test_cmp expect-error error-m
488'
489
490test_expect_success 'test missing ref after existing' '
491 (
492 cd client &&
493 test_must_fail git fetch-pack --no-progress .. refs/heads/A refs/heads/xyzzy
494 ) >/dev/null 2>error-em &&
495 test_cmp expect-error error-em
496'
497
498test_expect_success 'test missing ref before existing' '
499 (
500 cd client &&
501 test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy refs/heads/A
502 ) >/dev/null 2>error-me &&
503 test_cmp expect-error error-me
504'
505
5f0fc645 506test_expect_success 'test --all, --depth, and explicit head' '
8db43d29
MH
507 (
508 cd client &&
509 git fetch-pack --no-progress --all --depth=1 .. refs/heads/A
510 ) >out-adh 2>error-adh
511'
512
5f0fc645 513test_expect_success 'test --all, --depth, and explicit tag' '
8db43d29
MH
514 git tag OLDTAG refs/heads/B~5 &&
515 (
516 cd client &&
517 git fetch-pack --no-progress --all --depth=1 .. refs/tags/OLDTAG
518 ) >out-adt 2>error-adt
519'
520
6da8bdcb
NTND
521test_expect_success 'shallow fetch with tags does not break the repository' '
522 mkdir repo1 &&
523 (
524 cd repo1 &&
525 git init &&
526 test_commit 1 &&
527 test_commit 2 &&
528 test_commit 3 &&
529 mkdir repo2 &&
530 cd repo2 &&
531 git init &&
532 git fetch --depth=2 ../.git master:branch &&
533 git fsck
534 )
535'
4a8d202c
GSF
536
537test_expect_success 'fetch-pack can fetch a raw sha1' '
538 git init hidden &&
539 (
540 cd hidden &&
541 test_commit 1 &&
542 test_commit 2 &&
543 git update-ref refs/hidden/one HEAD^ &&
544 git config transfer.hiderefs refs/hidden &&
545 git config uploadpack.allowtipsha1inwant true
546 ) &&
547 git fetch-pack hidden $(git -C hidden rev-parse refs/hidden/one)
548'
549
854aeb7b
TB
550check_prot_path () {
551 cat >expected <<-EOF &&
552 Diag: url=$1
553 Diag: protocol=$2
554 Diag: path=$3
555 EOF
556 git fetch-pack --diag-url "$1" | grep -v hostandport= >actual &&
557 test_cmp expected actual
558}
559
3f55ccab 560check_prot_host_port_path () {
3f55ccab
TB
561 case "$2" in
562 *ssh*)
563 pp=ssh
564 uah=userandhost
565 ehost=$(echo $3 | tr -d "[]")
566 diagport="Diag: port=$4"
567 ;;
568 *)
569 pp=$p
570 uah=hostandport
571 ehost=$(echo $3$4 | sed -e "s/22$/:22/" -e "s/NONE//")
572 diagport=""
573 ;;
574 esac
575 cat >exp <<-EOF &&
854aeb7b 576 Diag: url=$1
3f55ccab
TB
577 Diag: protocol=$pp
578 Diag: $uah=$ehost
579 $diagport
580 Diag: path=$5
854aeb7b 581 EOF
3f55ccab 582 grep -v "^$" exp >expected
854aeb7b
TB
583 git fetch-pack --diag-url "$1" >actual &&
584 test_cmp expected actual
585}
586
587for r in repo re:po re/po
588do
589 # git or ssh with scheme
590 for p in "ssh+git" "git+ssh" git ssh
591 do
3f55ccab 592 for h in host user@host user@[::1] user@::1
854aeb7b 593 do
6b6c5f7a
TB
594 for c in "" :
595 do
596 test_expect_success "fetch-pack --diag-url $p://$h$c/$r" '
597 check_prot_host_port_path $p://$h/$r $p "$h" NONE "/$r"
598 '
599 # "/~" -> "~" conversion
600 test_expect_success "fetch-pack --diag-url $p://$h$c/~$r" '
601 check_prot_host_port_path $p://$h/~$r $p "$h" NONE "~$r"
602 '
603 done
3f55ccab
TB
604 done
605 for h in host User@host User@[::1]
606 do
607 test_expect_success "fetch-pack --diag-url $p://$h:22/$r" '
608 check_prot_host_port_path $p://$h:22/$r $p "$h" 22 "/$r"
854aeb7b
TB
609 '
610 done
611 done
612 # file with scheme
613 for p in file
614 do
615 test_expect_success "fetch-pack --diag-url $p://$h/$r" '
616 check_prot_path $p://$h/$r $p "/$r"
617 '
618 # No "/~" -> "~" conversion for file
619 test_expect_success "fetch-pack --diag-url $p://$h/~$r" '
620 check_prot_path $p://$h/~$r $p "/~$r"
621 '
622 done
6a599748
TB
623 # file without scheme
624 for h in nohost nohost:12 [::1] [::1]:23 [ [:aa
625 do
626 test_expect_success "fetch-pack --diag-url ./$h:$r" '
627 check_prot_path ./$h:$r $p "./$h:$r"
628 '
629 # No "/~" -> "~" conversion for file
630 test_expect_success "fetch-pack --diag-url ./$p:$h/~$r" '
631 check_prot_path ./$p:$h/~$r $p "./$p:$h/~$r"
632 '
633 done
634 #ssh without scheme
635 p=ssh
636 for h in host [::1]
637 do
6a599748 638 test_expect_success "fetch-pack --diag-url $h:$r" '
3f55ccab 639 check_prot_host_port_path $h:$r $p "$h" NONE "$r"
6a599748
TB
640 '
641 # Do "/~" -> "~" conversion
642 test_expect_success "fetch-pack --diag-url $h:/~$r" '
3f55ccab 643 check_prot_host_port_path $h:/~$r $p "$h" NONE "~$r"
6a599748
TB
644 '
645 done
854aeb7b 646done
6da8bdcb 647
c59ab2e5
TB
648test_expect_success MINGW 'fetch-pack --diag-url file://c:/repo' '
649 check_prot_path file://c:/repo file c:/repo
650'
651test_expect_success MINGW 'fetch-pack --diag-url c:repo' '
652 check_prot_path c:repo file c:repo
653'
654
6b17c674 655test_done