3 # Copyright (c) 2005 Junio C Hamano
6 test_description
='git pack-object'
8 TEST_PASSES_SANITIZE_LEAK
=true
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
'negative window clamps to 0' '
160 git pack-objects --progress --window=-1 neg-window <obj-list 2>stderr &&
161 check_deltas stderr = 0
164 test_expect_success
'pack-objects with bogus arguments' '
165 test_must_fail git pack-objects --window=0 test-1 blah blah <obj-list
169 local packname
="$1" &&
170 local object_list
="$2" &&
171 local git_config
="$3" &&
172 test_when_finished
"rm -rf git2" &&
173 git
$git_config init
--bare git2
&&
175 git
$git_config -C git2 unpack-objects
-n <"$packname".pack
&&
176 git
$git_config -C git2 unpack-objects
<"$packname".pack
&&
177 git
$git_config -C git2 cat-file
--batch-check="%(objectname)"
178 ) <"$object_list" >current
&&
179 cmp "$object_list" current
182 test_expect_success
'unpack without delta' '
183 check_unpack test-1-${packname_1} obj-list
186 BATCH_CONFIGURATION
='-c core.fsync=loose-object -c core.fsyncmethod=batch'
188 test_expect_success
'unpack without delta (core.fsyncmethod=batch)' '
189 check_unpack test-1-${packname_1} obj-list "$BATCH_CONFIGURATION"
192 test_expect_success
'pack with REF_DELTA' '
193 packname_2=$(git pack-objects --progress test-2 <obj-list 2>stderr) &&
194 check_deltas stderr -gt 0
197 test_expect_success
'unpack with REF_DELTA' '
198 check_unpack test-2-${packname_2} obj-list
201 test_expect_success
'unpack with REF_DELTA (core.fsyncmethod=batch)' '
202 check_unpack test-2-${packname_2} obj-list "$BATCH_CONFIGURATION"
205 test_expect_success
'pack with OFS_DELTA' '
206 packname_3=$(git pack-objects --progress --delta-base-offset test-3 \
207 <obj-list 2>stderr) &&
208 check_deltas stderr -gt 0
211 test_expect_success
'unpack with OFS_DELTA' '
212 check_unpack test-3-${packname_3} obj-list
215 test_expect_success
'unpack with OFS_DELTA (core.fsyncmethod=batch)' '
216 check_unpack test-3-${packname_3} obj-list "$BATCH_CONFIGURATION"
219 test_expect_success
'compare delta flavors' '
221 defined($_ = -s $_) or die for @ARGV;
222 exit 1 if $ARGV[0] <= $ARGV[1];
223 '\'' test-2-$packname_2.pack test-3-$packname_3.pack
226 check_use_objects
() {
227 test_when_finished
"rm -rf git2" &&
228 git init
--bare git2
&&
229 cp "$1".pack
"$1".idx git
2/objects
/pack
&&
232 git diff-tree
--root -p $commit &&
235 t
=$
(git cat-file
-t $object) &&
236 git cat-file
$t $object ||
exit 1
238 ) <obj-list
>current
&&
242 test_expect_success
'use packed objects' '
243 check_use_objects test-1-${packname_1}
246 test_expect_success
'use packed deltified (REF_DELTA) objects' '
247 check_use_objects test-2-${packname_2}
250 test_expect_success
'use packed deltified (OFS_DELTA) objects' '
251 check_use_objects test-3-${packname_3}
254 test_expect_success
'survive missing objects/pack directory' '
256 rm -fr missing-pack &&
257 mkdir missing-pack &&
260 GOP=.git/objects/pack &&
262 git index-pack --stdin --keep=test <../test-3-${packname_3}.pack &&
263 test -f $GOP/pack-${packname_3}.pack &&
264 cmp $GOP/pack-${packname_3}.pack ../test-3-${packname_3}.pack &&
265 test -f $GOP/pack-${packname_3}.idx &&
266 cmp $GOP/pack-${packname_3}.idx ../test-3-${packname_3}.idx &&
267 test -f $GOP/pack-${packname_3}.keep
271 test_expect_success
'verify pack' '
272 git verify-pack test-1-${packname_1}.idx \
273 test-2-${packname_2}.idx \
274 test-3-${packname_3}.idx
277 test_expect_success
'verify pack -v' '
278 git verify-pack -v test-1-${packname_1}.idx \
279 test-2-${packname_2}.idx \
280 test-3-${packname_3}.idx
283 test_expect_success
'verify-pack catches mismatched .idx and .pack files' '
284 cat test-1-${packname_1}.idx >test-3.idx &&
285 cat test-2-${packname_2}.pack >test-3.pack &&
286 if git verify-pack test-3.idx
292 test_expect_success
'verify-pack catches a corrupted pack signature' '
293 cat test-1-${packname_1}.pack >test-3.pack &&
294 echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=2 &&
295 if git verify-pack test-3.idx
301 test_expect_success
'verify-pack catches a corrupted pack version' '
302 cat test-1-${packname_1}.pack >test-3.pack &&
303 echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=7 &&
304 if git verify-pack test-3.idx
310 test_expect_success
'verify-pack catches a corrupted type/size of the 1st packed object data' '
311 cat test-1-${packname_1}.pack >test-3.pack &&
312 echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=12 &&
313 if git verify-pack test-3.idx
319 test_expect_success
'verify-pack catches a corrupted sum of the index file itself' '
320 l=$(wc -c <test-3.idx) &&
322 cat test-1-${packname_1}.pack >test-3.pack &&
323 printf "%20s" "" | dd of=test-3.idx count=20 bs=1 conv=notrunc seek=$l &&
324 if git verify-pack test-3.pack
330 test_expect_success
'build pack index for an existing pack' '
331 cat test-1-${packname_1}.pack >test-3.pack &&
332 git index-pack -o tmp.idx test-3.pack &&
333 cmp tmp.idx test-1-${packname_1}.idx &&
335 git index-pack --promisor=message test-3.pack &&
336 cmp test-3.idx test-1-${packname_1}.idx &&
337 echo message >expect &&
338 test_cmp expect test-3.promisor &&
340 cat test-2-${packname_2}.pack >test-3.pack &&
341 git index-pack -o tmp.idx test-2-${packname_2}.pack &&
342 cmp tmp.idx test-2-${packname_2}.idx &&
344 git index-pack test-3.pack &&
345 cmp test-3.idx test-2-${packname_2}.idx &&
347 cat test-3-${packname_3}.pack >test-3.pack &&
348 git index-pack -o tmp.idx test-3-${packname_3}.pack &&
349 cmp tmp.idx test-3-${packname_3}.idx &&
351 git index-pack test-3.pack &&
352 cmp test-3.idx test-3-${packname_3}.idx &&
354 cat test-1-${packname_1}.pack >test-4.pack &&
356 git index-pack --keep=why test-4.pack &&
357 cmp test-1-${packname_1}.idx test-4.idx &&
358 test -f test-4.keep &&
363 test_expect_success
'unpacking with --strict' '
365 for j in a b c d e f g
367 for i in 0 1 2 3 4 5 6 7 8 9
369 o=$(echo $j$i | git hash-object -w --stdin) &&
370 echo "100644 $o 0 $j$i" || return 1
374 git update-index --index-info <LIST &&
375 LIST=$(git write-tree) &&
377 head -n 10 LIST | git update-index --index-info &&
378 LI=$(git write-tree) &&
380 tail -n 10 LIST | git update-index --index-info &&
381 ST=$(git write-tree) &&
382 git rev-list --objects "$LIST" "$LI" "$ST" >actual &&
383 PACK5=$( git pack-objects test-5 <actual ) &&
384 PACK6=$( test_write_lines "$LIST" "$LI" "$ST" | git pack-objects test-6 ) &&
385 test_create_repo test-5 &&
388 git unpack-objects --strict <../test-5-$PACK5.pack &&
389 git ls-tree -r $LIST &&
390 git ls-tree -r $LI &&
393 test_create_repo test-6 &&
395 # tree-only into empty repo -- many unreachables
397 test_must_fail git unpack-objects --strict <../test-6-$PACK6.pack
400 # already populated -- no unreachables
402 git unpack-objects --strict <../test-6-$PACK6.pack
406 test_expect_success
'index-pack with --strict' '
408 for j in a b c d e f g
410 for i in 0 1 2 3 4 5 6 7 8 9
412 o=$(echo $j$i | git hash-object -w --stdin) &&
413 echo "100644 $o 0 $j$i" || return 1
417 git update-index --index-info <LIST &&
418 LIST=$(git write-tree) &&
420 head -n 10 LIST | git update-index --index-info &&
421 LI=$(git write-tree) &&
423 tail -n 10 LIST | git update-index --index-info &&
424 ST=$(git write-tree) &&
425 git rev-list --objects "$LIST" "$LI" "$ST" >actual &&
426 PACK5=$( git pack-objects test-5 <actual ) &&
427 PACK6=$( test_write_lines "$LIST" "$LI" "$ST" | git pack-objects test-6 ) &&
428 test_create_repo test-7 &&
431 git index-pack --strict --stdin <../test-5-$PACK5.pack &&
432 git ls-tree -r $LIST &&
433 git ls-tree -r $LI &&
436 test_create_repo test-8 &&
438 # tree-only into empty repo -- many unreachables
440 test_must_fail git index-pack --strict --stdin <../test-6-$PACK6.pack
443 # already populated -- no unreachables
445 git index-pack --strict --stdin <../test-6-$PACK6.pack
449 test_expect_success
'setup for --strict and --fsck-objects downgrading fsck msgs' '
453 test_commit first hello &&
454 cat >commit <<-EOF &&
455 tree $(git rev-parse HEAD^{tree})
456 parent $(git rev-parse HEAD)
460 commit: this is a commit with bad emails
463 git hash-object --literally -t commit -w --stdin <commit >commit_list &&
464 git pack-objects test <commit_list >pack-name
468 test_with_bad_commit
() {
469 must_fail_arg
="$1" &&
470 must_pass_arg
="$2" &&
473 test_must_fail git index-pack
"$must_fail_arg" "test-$(cat pack-name).pack" &&
474 git index-pack
"$must_pass_arg" "test-$(cat pack-name).pack"
478 test_expect_success
'index-pack with --strict downgrading fsck msgs' '
479 test_with_bad_commit --strict --strict="missingEmail=ignore"
482 test_expect_success
'index-pack with --fsck-objects downgrading fsck msgs' '
483 test_with_bad_commit --fsck-objects --fsck-objects="missingEmail=ignore"
486 test_expect_success
'cleanup for --strict and --fsck-objects downgrading fsck msgs' '
490 test_expect_success
'honor pack.packSizeLimit' '
491 git config pack.packSizeLimit 3m &&
492 packname_10=$(git pack-objects test-10 <obj-list) &&
493 test 2 = $(ls test-10-*.pack | wc -l)
496 test_expect_success
'verify resulting packs' '
497 git verify-pack test-10-*.pack
500 test_expect_success
'tolerate packsizelimit smaller than biggest object' '
501 git config pack.packSizeLimit 1 &&
502 packname_11=$(git pack-objects test-11 <obj-list) &&
503 test 5 = $(ls test-11-*.pack | wc -l)
506 test_expect_success
'verify resulting packs' '
507 git verify-pack test-11-*.pack
510 test_expect_success
'set up pack for non-repo tests' '
511 # make sure we have a pack with no matching index file
512 cp test-1-*.pack foo.pack
515 test_expect_success
'index-pack --stdin complains of non-repo' '
516 nongit test_must_fail git index-pack --object-format=$(test_oid algo) --stdin <foo.pack &&
517 test_path_is_missing non-repo/.git
520 test_expect_success
'index-pack <pack> works in non-repo' '
521 nongit git index-pack --object-format=$(test_oid algo) ../foo.pack &&
522 test_path_is_file foo.idx
525 test_expect_success
'index-pack --strict <pack> works in non-repo' '
527 nongit git index-pack --strict --object-format=$(test_oid algo) ../foo.pack &&
528 test_path_is_file foo.idx
531 test_expect_success
!PTHREADS
,!FAIL_PREREQS \
532 'index-pack --threads=N or pack.threads=N warns when no pthreads' '
533 test_must_fail git index-pack --threads=2 2>err &&
534 grep ^warning: err >warnings &&
535 test_line_count = 1 warnings &&
536 grep -F "no threads support, ignoring --threads=2" err &&
538 test_must_fail git -c pack.threads=2 index-pack 2>err &&
539 grep ^warning: err >warnings &&
540 test_line_count = 1 warnings &&
541 grep -F "no threads support, ignoring pack.threads" err &&
543 test_must_fail git -c pack.threads=2 index-pack --threads=4 2>err &&
544 grep ^warning: err >warnings &&
545 test_line_count = 2 warnings &&
546 grep -F "no threads support, ignoring --threads=4" err &&
547 grep -F "no threads support, ignoring pack.threads" err
550 test_expect_success
!PTHREADS
,!FAIL_PREREQS \
551 'pack-objects --threads=N or pack.threads=N warns when no pthreads' '
552 git pack-objects --threads=2 --stdout --all </dev/null >/dev/null 2>err &&
553 grep ^warning: err >warnings &&
554 test_line_count = 1 warnings &&
555 grep -F "no threads support, ignoring --threads" err &&
557 git -c pack.threads=2 pack-objects --stdout --all </dev/null >/dev/null 2>err &&
558 grep ^warning: err >warnings &&
559 test_line_count = 1 warnings &&
560 grep -F "no threads support, ignoring pack.threads" err &&
562 git -c pack.threads=2 pack-objects --threads=4 --stdout --all </dev/null >/dev/null 2>err &&
563 grep ^warning: err >warnings &&
564 test_line_count = 2 warnings &&
565 grep -F "no threads support, ignoring --threads" err &&
566 grep -F "no threads support, ignoring pack.threads" err
569 test_expect_success
'pack-objects in too-many-packs mode' '
570 GIT_TEST_FULL_IN_PACK_ARRAY=1 git repack -ad &&
574 test_expect_success
'setup: fake a SHA1 hash collision' '
578 long_a=$(git hash-object -w ../a | sed -e "s!^..!&/!") &&
579 long_b=$(git hash-object -w ../b | sed -e "s!^..!&/!") &&
580 test -f .git/objects/$long_b &&
581 cp -f .git/objects/$long_a \
586 test_expect_success
'make sure index-pack detects the SHA1 collision' '
589 test_must_fail git index-pack -o ../bad.idx ../test-3.pack 2>msg &&
590 test_grep "SHA1 COLLISION FOUND" msg
594 test_expect_success
'make sure index-pack detects the SHA1 collision (large blobs)' '
597 test_must_fail git -c core.bigfilethreshold=1 index-pack -o ../bad.idx ../test-3.pack 2>msg &&
598 test_grep "SHA1 COLLISION FOUND" msg
602 test_expect_success
'prefetch objects' '
603 rm -rf server client &&
606 test_config -C server uploadpack.allowanysha1inwant 1 &&
607 test_config -C server uploadpack.allowfilter 1 &&
608 test_config -C server protocol.version 2 &&
610 echo one >server/one &&
611 git -C server add one &&
612 git -C server commit -m one &&
613 git -C server branch one_branch &&
615 echo two_a >server/two_a &&
616 echo two_b >server/two_b &&
617 git -C server add two_a two_b &&
618 git -C server commit -m two &&
620 echo three >server/three &&
621 git -C server add three &&
622 git -C server commit -m three &&
623 git -C server branch three_branch &&
625 # Clone, fetch "two" with blobs excluded, and re-push it. This requires
626 # the client to have the blobs of "two" - verify that these are
627 # prefetched in one batch.
628 git clone --filter=blob:none --single-branch -b one_branch \
629 "file://$(pwd)/server" client &&
630 test_config -C client protocol.version 2 &&
631 TWO=$(git -C server rev-parse three_branch^) &&
632 git -C client fetch --filter=blob:none origin "$TWO" &&
633 GIT_TRACE_PACKET=$(pwd)/trace git -C client push origin "$TWO":refs/heads/two_branch &&
634 grep "fetch> done" trace >donelines &&
635 test_line_count = 1 donelines
638 for hash in sha1 sha256
640 test_expect_success
"verify-pack with $hash packfile" '
641 test_when_finished "rm -rf repo" &&
642 git init --object-format=$hash repo &&
643 test_commit -C repo initial &&
644 git -C repo repack -ad &&
645 git -C repo verify-pack "$(pwd)"/repo/.git/objects/pack/*.idx &&
648 nongit git verify-pack "$(pwd)"/repo/.git/objects/pack/*.idx
650 # We have no way to identify the hash used by packfiles
651 # or indices, so we always fall back to SHA1.
652 nongit test_must_fail git verify-pack "$(pwd)"/repo/.git/objects/pack/*.idx &&
653 # But with an explicit object format we should succeed.
654 nongit git verify-pack --object-format=$hash "$(pwd)"/repo/.git/objects/pack/*.idx
658 test_expect_success
"index-pack outside of a $hash repository" '
659 test_when_finished "rm -rf repo" &&
660 git init --object-format=$hash repo &&
661 test_commit -C repo initial &&
662 git -C repo repack -ad &&
663 git -C repo index-pack --verify "$(pwd)"/repo/.git/objects/pack/*.pack &&
666 nongit git index-pack --verify "$(pwd)"/repo/.git/objects/pack/*.pack
668 # We have no way to identify the hash used by packfiles
669 # or indices, so we always fall back to SHA1.
670 nongit test_must_fail git index-pack --verify "$(pwd)"/repo/.git/objects/pack/*.pack 2>err &&
671 # But with an explicit object format we should succeed.
672 nongit git index-pack --object-format=$hash --verify "$(pwd)"/repo/.git/objects/pack/*.pack