3 # Copyright (c) 2005 Junio C Hamano
6 test_description
='git pack-object
11 test_expect_success
'setup' '
13 perl -e "print \"a\" x 4096;" >a &&
14 perl -e "print \"b\" x 4096;" >b &&
15 perl -e "print \"c\" x 4096;" >c &&
16 test-tool genrandom "seed a" 2097152 >a_big &&
17 test-tool genrandom "seed b" 2097152 >b_big &&
18 git update-index --add a a_big b b_big c &&
19 cat c >d && echo foo >>d && git update-index --add d &&
20 tree=$(git write-tree) &&
21 commit=$(git commit-tree $tree </dev/null) &&
25 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
28 git diff-tree --root -p $commit &&
31 t=$(git cat-file -t $object) &&
32 git cat-file $t $object || return 1
37 test_expect_success
'setup pack-object <stdin' '
38 git init pack-object-stdin &&
39 test_commit -C pack-object-stdin one &&
40 test_commit -C pack-object-stdin two
44 test_expect_success
'pack-object <stdin parsing: basic [|--revs]' '
46 $(git -C pack-object-stdin rev-parse one)
49 git -C pack-object-stdin pack-objects basic-stdin <in &&
50 idx=$(echo pack-object-stdin/basic-stdin-*.idx) &&
51 git show-index <"$idx" >actual &&
52 test_line_count = 1 actual &&
54 git -C pack-object-stdin pack-objects --revs basic-stdin-revs <in &&
55 idx=$(echo pack-object-stdin/basic-stdin-revs-*.idx) &&
56 git show-index <"$idx" >actual &&
57 test_line_count = 3 actual
60 test_expect_success
'pack-object <stdin parsing: [|--revs] bad line' '
62 $(git -C pack-object-stdin rev-parse one)
64 $(git -C pack-object-stdin rev-parse two)
67 sed "s/^> //g" >err.expect <<-EOF &&
68 fatal: expected object ID, got garbage:
72 test_must_fail git -C pack-object-stdin pack-objects bad-line-stdin <in 2>err.actual &&
73 test_cmp err.expect err.actual &&
75 cat >err.expect <<-EOF &&
76 fatal: bad revision '"'"'garbage'"'"'
78 test_must_fail git -C pack-object-stdin pack-objects --revs bad-line-stdin-revs <in 2>err.actual &&
79 test_cmp err.expect err.actual
82 test_expect_success
'pack-object <stdin parsing: [|--revs] empty line' '
84 $(git -C pack-object-stdin rev-parse one)
86 $(git -C pack-object-stdin rev-parse two)
89 sed -e "s/^> //g" -e "s/Z$//g" >err.expect <<-EOF &&
90 fatal: expected object ID, got garbage:
94 test_must_fail git -C pack-object-stdin pack-objects empty-line-stdin <in 2>err.actual &&
95 test_cmp err.expect err.actual &&
97 git -C pack-object-stdin pack-objects --revs empty-line-stdin-revs <in &&
98 idx=$(echo pack-object-stdin/empty-line-stdin-revs-*.idx) &&
99 git show-index <"$idx" >actual &&
100 test_line_count = 3 actual
103 test_expect_success
'pack-object <stdin parsing: [|--revs] with --stdin' '
105 $(git -C pack-object-stdin rev-parse one)
106 $(git -C pack-object-stdin rev-parse two)
109 # There is the "--stdin-packs is incompatible with --revs"
110 # test below, but we should make sure that the revision.c
111 # --stdin is not picked up
112 cat >err.expect <<-EOF &&
113 fatal: disallowed abbreviated or ambiguous option '"'"'stdin'"'"'
115 test_must_fail git -C pack-object-stdin pack-objects stdin-with-stdin-option --stdin <in 2>err.actual &&
116 test_cmp err.expect err.actual &&
118 test_must_fail git -C pack-object-stdin pack-objects --stdin --revs stdin-with-stdin-option-revs 2>err.actual <in &&
119 test_cmp err.expect err.actual
122 test_expect_success
'pack-object <stdin parsing: --stdin-packs handles garbage' '
124 $(git -C pack-object-stdin rev-parse one)
125 $(git -C pack-object-stdin rev-parse two)
128 # That we get "two" and not "one" has to do with OID
129 # ordering. It happens to be the same here under SHA-1 and
130 # SHA-256. See commentary in pack-objects.c
131 cat >err.expect <<-EOF &&
132 fatal: could not find pack '"'"'$(git -C pack-object-stdin rev-parse two)'"'"'
135 -C pack-object-stdin \
136 pack-objects stdin-with-stdin-option --stdin-packs \
138 test_cmp err.expect err.actual
141 # usage: check_deltas <stderr_from_pack_objects> <cmp_op> <nr_deltas>
142 # e.g.: check_deltas stderr -gt 0
144 deltas
=$
(perl
-lne '/delta (\d+)/ and print $1' "$1") &&
146 if ! test "$deltas" "$@"
148 echo >&2 "unexpected number of deltas (compared $delta $*)"
153 test_expect_success
'pack without delta' '
154 packname_1=$(git pack-objects --progress --window=0 test-1 \
155 <obj-list 2>stderr) &&
156 check_deltas stderr = 0
159 test_expect_success
'pack-objects with bogus arguments' '
160 test_must_fail git pack-objects --window=0 test-1 blah blah <obj-list
164 test_when_finished
"rm -rf git2" &&
165 git init
--bare git2
&&
166 git
-C git2 unpack-objects
-n <"$1".pack
&&
167 git
-C git2 unpack-objects
<"$1".pack
&&
168 (cd .git
&& find objects
-type f
-print) |
171 cmp git
2/$path .git
/$path ||
{
178 test_expect_success
'unpack without delta' '
179 check_unpack test-1-${packname_1}
182 test_expect_success
'pack with REF_DELTA' '
183 packname_2=$(git pack-objects --progress test-2 <obj-list 2>stderr) &&
184 check_deltas stderr -gt 0
187 test_expect_success
'unpack with REF_DELTA' '
188 check_unpack test-2-${packname_2}
191 test_expect_success
'pack with OFS_DELTA' '
192 packname_3=$(git pack-objects --progress --delta-base-offset test-3 \
193 <obj-list 2>stderr) &&
194 check_deltas stderr -gt 0
197 test_expect_success
'unpack with OFS_DELTA' '
198 check_unpack test-3-${packname_3}
201 test_expect_success
'compare delta flavors' '
203 defined($_ = -s $_) or die for @ARGV;
204 exit 1 if $ARGV[0] <= $ARGV[1];
205 '\'' test-2-$packname_2.pack test-3-$packname_3.pack
208 check_use_objects
() {
209 test_when_finished
"rm -rf git2" &&
210 git init
--bare git2
&&
211 cp "$1".pack
"$1".idx git
2/objects
/pack
&&
214 git diff-tree
--root -p $commit &&
217 t
=$
(git cat-file
-t $object) &&
218 git cat-file
$t $object ||
exit 1
220 ) <obj-list
>current
&&
224 test_expect_success
'use packed objects' '
225 check_use_objects test-1-${packname_1}
228 test_expect_success
'use packed deltified (REF_DELTA) objects' '
229 check_use_objects test-2-${packname_2}
232 test_expect_success
'use packed deltified (OFS_DELTA) objects' '
233 check_use_objects test-3-${packname_3}
236 test_expect_success
'survive missing objects/pack directory' '
238 rm -fr missing-pack &&
239 mkdir missing-pack &&
242 GOP=.git/objects/pack &&
244 git index-pack --stdin --keep=test <../test-3-${packname_3}.pack &&
245 test -f $GOP/pack-${packname_3}.pack &&
246 cmp $GOP/pack-${packname_3}.pack ../test-3-${packname_3}.pack &&
247 test -f $GOP/pack-${packname_3}.idx &&
248 cmp $GOP/pack-${packname_3}.idx ../test-3-${packname_3}.idx &&
249 test -f $GOP/pack-${packname_3}.keep
253 test_expect_success \
255 'git verify-pack test-1-${packname_1}.idx \
256 test-2-${packname_2}.idx \
257 test-3-${packname_3}.idx'
259 test_expect_success \
261 'git verify-pack -v test-1-${packname_1}.idx \
262 test-2-${packname_2}.idx \
263 test-3-${packname_3}.idx'
265 test_expect_success \
266 'verify-pack catches mismatched .idx and .pack files' \
267 'cat test-1-${packname_1}.idx >test-3.idx &&
268 cat test-2-${packname_2}.pack >test-3.pack &&
269 if git verify-pack test-3.idx
274 test_expect_success \
275 'verify-pack catches a corrupted pack signature' \
276 'cat test-1-${packname_1}.pack >test-3.pack &&
277 echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=2 &&
278 if git verify-pack test-3.idx
283 test_expect_success \
284 'verify-pack catches a corrupted pack version' \
285 'cat test-1-${packname_1}.pack >test-3.pack &&
286 echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=7 &&
287 if git verify-pack test-3.idx
292 test_expect_success \
293 'verify-pack catches a corrupted type/size of the 1st packed object data' \
294 'cat test-1-${packname_1}.pack >test-3.pack &&
295 echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=12 &&
296 if git verify-pack test-3.idx
301 test_expect_success \
302 'verify-pack catches a corrupted sum of the index file itself' \
303 'l=$(wc -c <test-3.idx) &&
305 cat test-1-${packname_1}.pack >test-3.pack &&
306 printf "%20s" "" | dd of=test-3.idx count=20 bs=1 conv=notrunc seek=$l &&
307 if git verify-pack test-3.pack
312 test_expect_success \
313 'build pack index for an existing pack' \
314 'cat test-1-${packname_1}.pack >test-3.pack &&
315 git index-pack -o tmp.idx test-3.pack &&
316 cmp tmp.idx test-1-${packname_1}.idx &&
318 git index-pack test-3.pack &&
319 cmp test-3.idx test-1-${packname_1}.idx &&
321 cat test-2-${packname_2}.pack >test-3.pack &&
322 git index-pack -o tmp.idx test-2-${packname_2}.pack &&
323 cmp tmp.idx test-2-${packname_2}.idx &&
325 git index-pack test-3.pack &&
326 cmp test-3.idx test-2-${packname_2}.idx &&
328 cat test-3-${packname_3}.pack >test-3.pack &&
329 git index-pack -o tmp.idx test-3-${packname_3}.pack &&
330 cmp tmp.idx test-3-${packname_3}.idx &&
332 git index-pack test-3.pack &&
333 cmp test-3.idx test-3-${packname_3}.idx &&
335 cat test-1-${packname_1}.pack >test-4.pack &&
337 git index-pack --keep=why test-4.pack &&
338 cmp test-1-${packname_1}.idx test-4.idx &&
339 test -f test-4.keep &&
343 test_expect_success
'unpacking with --strict' '
345 for j in a b c d e f g
347 for i in 0 1 2 3 4 5 6 7 8 9
349 o=$(echo $j$i | git hash-object -w --stdin) &&
350 echo "100644 $o 0 $j$i" || return 1
354 git update-index --index-info <LIST &&
355 LIST=$(git write-tree) &&
357 head -n 10 LIST | git update-index --index-info &&
358 LI=$(git write-tree) &&
360 tail -n 10 LIST | git update-index --index-info &&
361 ST=$(git write-tree) &&
362 git rev-list --objects "$LIST" "$LI" "$ST" >actual &&
363 PACK5=$( git pack-objects test-5 <actual ) &&
364 PACK6=$( test_write_lines "$LIST" "$LI" "$ST" | git pack-objects test-6 ) &&
365 test_create_repo test-5 &&
368 git unpack-objects --strict <../test-5-$PACK5.pack &&
369 git ls-tree -r $LIST &&
370 git ls-tree -r $LI &&
373 test_create_repo test-6 &&
375 # tree-only into empty repo -- many unreachables
377 test_must_fail git unpack-objects --strict <../test-6-$PACK6.pack
380 # already populated -- no unreachables
382 git unpack-objects --strict <../test-6-$PACK6.pack
386 test_expect_success
'index-pack with --strict' '
388 for j in a b c d e f g
390 for i in 0 1 2 3 4 5 6 7 8 9
392 o=$(echo $j$i | git hash-object -w --stdin) &&
393 echo "100644 $o 0 $j$i" || return 1
397 git update-index --index-info <LIST &&
398 LIST=$(git write-tree) &&
400 head -n 10 LIST | git update-index --index-info &&
401 LI=$(git write-tree) &&
403 tail -n 10 LIST | git update-index --index-info &&
404 ST=$(git write-tree) &&
405 git rev-list --objects "$LIST" "$LI" "$ST" >actual &&
406 PACK5=$( git pack-objects test-5 <actual ) &&
407 PACK6=$( test_write_lines "$LIST" "$LI" "$ST" | git pack-objects test-6 ) &&
408 test_create_repo test-7 &&
411 git index-pack --strict --stdin <../test-5-$PACK5.pack &&
412 git ls-tree -r $LIST &&
413 git ls-tree -r $LI &&
416 test_create_repo test-8 &&
418 # tree-only into empty repo -- many unreachables
420 test_must_fail git index-pack --strict --stdin <../test-6-$PACK6.pack
423 # already populated -- no unreachables
425 git index-pack --strict --stdin <../test-6-$PACK6.pack
429 test_expect_success
'honor pack.packSizeLimit' '
430 git config pack.packSizeLimit 3m &&
431 packname_10=$(git pack-objects test-10 <obj-list) &&
432 test 2 = $(ls test-10-*.pack | wc -l)
435 test_expect_success
'verify resulting packs' '
436 git verify-pack test-10-*.pack
439 test_expect_success
'tolerate packsizelimit smaller than biggest object' '
440 git config pack.packSizeLimit 1 &&
441 packname_11=$(git pack-objects test-11 <obj-list) &&
442 test 5 = $(ls test-11-*.pack | wc -l)
445 test_expect_success
'verify resulting packs' '
446 git verify-pack test-11-*.pack
449 test_expect_success
'set up pack for non-repo tests' '
450 # make sure we have a pack with no matching index file
451 cp test-1-*.pack foo.pack
454 test_expect_success
'index-pack --stdin complains of non-repo' '
455 nongit test_must_fail git index-pack --object-format=$(test_oid algo) --stdin <foo.pack &&
456 test_path_is_missing non-repo/.git
459 test_expect_success
'index-pack <pack> works in non-repo' '
460 nongit git index-pack --object-format=$(test_oid algo) ../foo.pack &&
461 test_path_is_file foo.idx
464 test_expect_success
'index-pack --strict <pack> works in non-repo' '
466 nongit git index-pack --strict --object-format=$(test_oid algo) ../foo.pack &&
467 test_path_is_file foo.idx
470 test_expect_success
!PTHREADS
,!FAIL_PREREQS \
471 'index-pack --threads=N or pack.threads=N warns when no pthreads' '
472 test_must_fail git index-pack --threads=2 2>err &&
473 grep ^warning: err >warnings &&
474 test_line_count = 1 warnings &&
475 grep -F "no threads support, ignoring --threads=2" err &&
477 test_must_fail git -c pack.threads=2 index-pack 2>err &&
478 grep ^warning: err >warnings &&
479 test_line_count = 1 warnings &&
480 grep -F "no threads support, ignoring pack.threads" err &&
482 test_must_fail git -c pack.threads=2 index-pack --threads=4 2>err &&
483 grep ^warning: err >warnings &&
484 test_line_count = 2 warnings &&
485 grep -F "no threads support, ignoring --threads=4" err &&
486 grep -F "no threads support, ignoring pack.threads" err
489 test_expect_success
!PTHREADS
,!FAIL_PREREQS \
490 'pack-objects --threads=N or pack.threads=N warns when no pthreads' '
491 git pack-objects --threads=2 --stdout --all </dev/null >/dev/null 2>err &&
492 grep ^warning: err >warnings &&
493 test_line_count = 1 warnings &&
494 grep -F "no threads support, ignoring --threads" err &&
496 git -c pack.threads=2 pack-objects --stdout --all </dev/null >/dev/null 2>err &&
497 grep ^warning: err >warnings &&
498 test_line_count = 1 warnings &&
499 grep -F "no threads support, ignoring pack.threads" err &&
501 git -c pack.threads=2 pack-objects --threads=4 --stdout --all </dev/null >/dev/null 2>err &&
502 grep ^warning: err >warnings &&
503 test_line_count = 2 warnings &&
504 grep -F "no threads support, ignoring --threads" err &&
505 grep -F "no threads support, ignoring pack.threads" err
508 test_expect_success
'pack-objects in too-many-packs mode' '
509 GIT_TEST_FULL_IN_PACK_ARRAY=1 git repack -ad &&
513 test_expect_success
'setup: fake a SHA1 hash collision' '
517 long_a=$(git hash-object -w ../a | sed -e "s!^..!&/!") &&
518 long_b=$(git hash-object -w ../b | sed -e "s!^..!&/!") &&
519 test -f .git/objects/$long_b &&
520 cp -f .git/objects/$long_a \
525 test_expect_success
'make sure index-pack detects the SHA1 collision' '
528 test_must_fail git index-pack -o ../bad.idx ../test-3.pack 2>msg &&
529 test_i18ngrep "SHA1 COLLISION FOUND" msg
533 test_expect_success
'make sure index-pack detects the SHA1 collision (large blobs)' '
536 test_must_fail git -c core.bigfilethreshold=1 index-pack -o ../bad.idx ../test-3.pack 2>msg &&
537 test_i18ngrep "SHA1 COLLISION FOUND" msg
541 test_expect_success
'prefetch objects' '
542 rm -rf server client &&
545 test_config -C server uploadpack.allowanysha1inwant 1 &&
546 test_config -C server uploadpack.allowfilter 1 &&
547 test_config -C server protocol.version 2 &&
549 echo one >server/one &&
550 git -C server add one &&
551 git -C server commit -m one &&
552 git -C server branch one_branch &&
554 echo two_a >server/two_a &&
555 echo two_b >server/two_b &&
556 git -C server add two_a two_b &&
557 git -C server commit -m two &&
559 echo three >server/three &&
560 git -C server add three &&
561 git -C server commit -m three &&
562 git -C server branch three_branch &&
564 # Clone, fetch "two" with blobs excluded, and re-push it. This requires
565 # the client to have the blobs of "two" - verify that these are
566 # prefetched in one batch.
567 git clone --filter=blob:none --single-branch -b one_branch \
568 "file://$(pwd)/server" client &&
569 test_config -C client protocol.version 2 &&
570 TWO=$(git -C server rev-parse three_branch^) &&
571 git -C client fetch --filter=blob:none origin "$TWO" &&
572 GIT_TRACE_PACKET=$(pwd)/trace git -C client push origin "$TWO":refs/heads/two_branch &&
573 grep "fetch> done" trace >donelines &&
574 test_line_count = 1 donelines
577 test_expect_success
'setup for --stdin-packs tests' '
578 git init stdin-packs &&
588 git pack-objects .git/objects/pack/pack-$id \
589 --incremental --revs <<-EOF || exit 1
594 ls -la .git/objects/pack
598 test_expect_success
'--stdin-packs with excluded packs' '
602 PACK_A="$(basename .git/objects/pack/pack-A-*.pack)" &&
603 PACK_B="$(basename .git/objects/pack/pack-B-*.pack)" &&
604 PACK_C="$(basename .git/objects/pack/pack-C-*.pack)" &&
606 git pack-objects test --stdin-packs <<-EOF &&
613 git show-index <$(ls .git/objects/pack/pack-A-*.idx) &&
614 git show-index <$(ls .git/objects/pack/pack-C-*.idx)
616 git show-index <$(ls test-*.idx) >actual.raw &&
618 cut -d" " -f2 <expect.raw | sort >expect &&
619 cut -d" " -f2 <actual.raw | sort >actual &&
620 test_cmp expect actual
624 test_expect_success
'--stdin-packs is incompatible with --filter' '
627 test_must_fail git pack-objects --stdin-packs --stdout \
628 --filter=blob:none </dev/null 2>err &&
629 test_i18ngrep "cannot use --filter with --stdin-packs" err
633 test_expect_success
'--stdin-packs is incompatible with --revs' '
636 test_must_fail git pack-objects --stdin-packs --revs out \
638 test_i18ngrep "cannot use internal rev list with --stdin-packs" err
642 test_expect_success
'--stdin-packs with loose objects' '
646 PACK_A="$(basename .git/objects/pack/pack-A-*.pack)" &&
647 PACK_B="$(basename .git/objects/pack/pack-B-*.pack)" &&
648 PACK_C="$(basename .git/objects/pack/pack-C-*.pack)" &&
650 test_commit D && # loose
652 git pack-objects test2 --stdin-packs --unpacked <<-EOF &&
659 git show-index <$(ls .git/objects/pack/pack-A-*.idx) &&
660 git show-index <$(ls .git/objects/pack/pack-C-*.idx) &&
661 git rev-list --objects --no-object-names \
662 refs/tags/C..refs/tags/D
666 git show-index <$(ls test2-*.idx) >actual.raw &&
668 cut -d" " -f2 <expect.raw | sort >expect &&
669 cut -d" " -f2 <actual.raw | sort >actual &&
670 test_cmp expect actual
674 test_expect_success
'--stdin-packs with broken links' '
678 # make an unreachable object with a bogus parent
679 git cat-file -p HEAD >commit &&
680 sed "s/$(git rev-parse HEAD^)/$(test_oid zero)/" <commit |
681 git hash-object -w -t commit --stdin >in &&
683 git pack-objects .git/objects/pack/pack-D <in &&
685 PACK_A="$(basename .git/objects/pack/pack-A-*.pack)" &&
686 PACK_B="$(basename .git/objects/pack/pack-B-*.pack)" &&
687 PACK_C="$(basename .git/objects/pack/pack-C-*.pack)" &&
688 PACK_D="$(basename .git/objects/pack/pack-D-*.pack)" &&
690 git pack-objects test3 --stdin-packs --unpacked <<-EOF &&
698 git show-index <$(ls .git/objects/pack/pack-A-*.idx) &&
699 git show-index <$(ls .git/objects/pack/pack-C-*.idx) &&
700 git show-index <$(ls .git/objects/pack/pack-D-*.idx) &&
701 git rev-list --objects --no-object-names \
702 refs/tags/C..refs/tags/D
704 git show-index <$(ls test3-*.idx) >actual.raw &&
706 cut -d" " -f2 <expect.raw | sort >expect &&
707 cut -d" " -f2 <actual.raw | sort >actual &&
708 test_cmp expect actual
712 test_expect_success
'negative window clamps to 0' '
713 git pack-objects --progress --window=-1 neg-window <obj-list 2>stderr &&
714 check_deltas stderr = 0