Merge branch 'cl/config-regexp-docfix' into maint-2.46
[git/gitster.git] / t / t5319-multi-pack-index.sh
blobace5ac3b612696bc7b86bbd63bae135dc70ae711
1 #!/bin/sh
3 test_description='multi-pack-indexes'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-chunk.sh
7 GIT_TEST_MULTI_PACK_INDEX=0
8 objdir=.git/objects
10 HASH_LEN=$(test_oid rawsz)
12 midx_read_expect () {
13 NUM_PACKS=$1
14 NUM_OBJECTS=$2
15 NUM_CHUNKS=$3
16 OBJECT_DIR=$4
17 EXTRA_CHUNKS="$5"
19 cat <<-EOF &&
20 header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
21 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
22 num_objects: $NUM_OBJECTS
23 packs:
24 EOF
25 if test $NUM_PACKS -ge 1
26 then
27 ls $OBJECT_DIR/pack/ | grep idx | sort
28 fi &&
29 printf "object-dir: $OBJECT_DIR\n"
30 } >expect &&
31 test-tool read-midx $OBJECT_DIR >actual &&
32 test_cmp expect actual
35 test_expect_success 'setup' '
36 test_oid_cache <<-EOF
37 idxoff sha1:2999
38 idxoff sha256:3739
40 packnameoff sha1:652
41 packnameoff sha256:940
43 fanoutoff sha1:1
44 fanoutoff sha256:3
45 EOF
48 test_expect_success "don't write midx with no packs" '
49 test_must_fail git multi-pack-index --object-dir=. write &&
50 test_path_is_missing pack/multi-pack-index
53 test_expect_success SHA1 'warn if a midx contains no oid' '
54 cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
55 test_must_fail git multi-pack-index verify &&
56 rm $objdir/pack/multi-pack-index
59 generate_objects () {
60 i=$1
61 iii=$(printf '%03i' $i)
63 test-tool genrandom "bar" 200 &&
64 test-tool genrandom "baz $iii" 50
65 } >wide_delta_$iii &&
67 test-tool genrandom "foo"$i 100 &&
68 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
69 test-tool genrandom "foo"$(( $i + 2 )) 100
70 } >deep_delta_$iii &&
72 echo $iii &&
73 test-tool genrandom "$iii" 8192
74 } >file_$iii &&
75 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
78 commit_and_list_objects () {
80 echo 101 &&
81 test-tool genrandom 100 8192;
82 } >file_101 &&
83 git update-index --add file_101 &&
84 tree=$(git write-tree) &&
85 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
87 echo $tree &&
88 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
89 } >obj-list &&
90 git reset --hard $commit
93 test_expect_success 'create objects' '
94 test_commit initial &&
95 for i in $(test_seq 1 5)
97 generate_objects $i || return 1
98 done &&
99 commit_and_list_objects
102 test_expect_success 'write midx with one v1 pack' '
103 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
104 test_when_finished rm $objdir/pack/test-$pack.pack \
105 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
106 git multi-pack-index --object-dir=$objdir write &&
107 midx_read_expect 1 18 4 $objdir
110 midx_git_two_modes () {
111 git -c core.multiPackIndex=false $1 >expect &&
112 git -c core.multiPackIndex=true $1 >actual &&
113 if [ "$2" = "sorted" ]
114 then
115 sort <expect >expect.sorted &&
116 mv expect.sorted expect &&
117 sort <actual >actual.sorted &&
118 mv actual.sorted actual
119 fi &&
120 test_cmp expect actual
123 compare_results_with_midx () {
124 MSG=$1
125 test_expect_success "check normal git operations: $MSG" '
126 midx_git_two_modes "rev-list --objects --all" &&
127 midx_git_two_modes "log --raw" &&
128 midx_git_two_modes "count-objects --verbose" &&
129 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
130 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
134 test_expect_success 'write midx with one v2 pack' '
135 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
136 git multi-pack-index --object-dir=$objdir write &&
137 midx_read_expect 1 18 4 $objdir
140 compare_results_with_midx "one v2 pack"
142 test_expect_success 'corrupt idx reports errors' '
143 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
144 mv $objdir/pack/$idx backup-$idx &&
145 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
147 # This is the minimum size for a sha-1 based .idx; this lets
148 # us pass perfunctory tests, but anything that actually opens and reads
149 # the idx file will complain.
150 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
152 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
153 grep "index unavailable" err
156 test_expect_success 'add more objects' '
157 for i in $(test_seq 6 10)
159 generate_objects $i || return 1
160 done &&
161 commit_and_list_objects
164 test_expect_success 'write midx with two packs' '
165 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
166 git multi-pack-index --object-dir=$objdir write &&
167 midx_read_expect 2 34 4 $objdir
170 compare_results_with_midx "two packs"
172 test_expect_success 'write midx with --stdin-packs' '
173 rm -fr $objdir/pack/multi-pack-index &&
175 idx="$(find $objdir/pack -name "test-2-*.idx")" &&
176 basename "$idx" >in &&
178 git multi-pack-index write --stdin-packs <in &&
180 test-tool read-midx $objdir | grep "\.idx$" >packs &&
182 test_cmp packs in
185 compare_results_with_midx "mixed mode (one pack + extra)"
187 test_expect_success 'write with no objects and preferred pack' '
188 test_when_finished "rm -rf empty" &&
189 git init empty &&
190 test_must_fail git -C empty multi-pack-index write \
191 --stdin-packs --preferred-pack=does-not-exist </dev/null 2>err &&
192 cat >expect <<-EOF &&
193 warning: unknown preferred pack: ${SQ}does-not-exist${SQ}
194 error: no pack files to index.
196 test_cmp expect err
199 test_expect_success 'write progress off for redirected stderr' '
200 git multi-pack-index --object-dir=$objdir write 2>err &&
201 test_line_count = 0 err
204 test_expect_success 'write force progress on for stderr' '
205 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --progress 2>err &&
206 test_file_not_empty err
209 test_expect_success 'write with the --no-progress option' '
210 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --no-progress 2>err &&
211 test_line_count = 0 err
214 test_expect_success 'add more packs' '
215 for j in $(test_seq 11 20)
217 generate_objects $j &&
218 commit_and_list_objects &&
219 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list || return 1
220 done
223 compare_results_with_midx "mixed mode (two packs + extra)"
225 test_expect_success 'write midx with twelve packs' '
226 git multi-pack-index --object-dir=$objdir write &&
227 midx_read_expect 12 74 4 $objdir
230 compare_results_with_midx "twelve packs"
232 test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
233 git init repo &&
234 git clone -s repo alternate &&
236 test_when_finished "rm -rf repo alternate" &&
239 cd repo &&
240 test_commit base &&
241 git repack -d
242 ) &&
244 ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
245 theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
246 touch "$ours" "$theirs" &&
249 cd alternate &&
250 git multi-pack-index --object-dir ../repo/.git/objects write
251 ) &&
253 # writing a midx in "repo" should not remove the .rev file in the
254 # alternate
255 test_path_is_file repo/.git/objects/pack/multi-pack-index &&
256 test_path_is_file $ours &&
257 test_path_is_missing $theirs
260 test_expect_success 'warn on improper hash version' '
261 git init --object-format=sha1 sha1 &&
263 cd sha1 &&
264 git config core.multiPackIndex true &&
265 test_commit 1 &&
266 git repack -a &&
267 git multi-pack-index write &&
268 mv .git/objects/pack/multi-pack-index ../mpi-sha1
269 ) &&
270 git init --object-format=sha256 sha256 &&
272 cd sha256 &&
273 git config core.multiPackIndex true &&
274 test_commit 1 &&
275 git repack -a &&
276 git multi-pack-index write &&
277 mv .git/objects/pack/multi-pack-index ../mpi-sha256
278 ) &&
280 cd sha1 &&
281 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
282 git log -1 2>err &&
283 test_grep "multi-pack-index hash version 2 does not match version 1" err
284 ) &&
286 cd sha256 &&
287 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
288 git log -1 2>err &&
289 test_grep "multi-pack-index hash version 1 does not match version 2" err
293 test_expect_success 'midx picks objects from preferred pack' '
294 test_when_finished rm -rf preferred.git &&
295 git init --bare preferred.git &&
297 cd preferred.git &&
299 a=$(echo "a" | git hash-object -w --stdin) &&
300 b=$(echo "b" | git hash-object -w --stdin) &&
301 c=$(echo "c" | git hash-object -w --stdin) &&
303 # Set up two packs, duplicating the object "B" at different
304 # offsets.
306 # Note that the "BC" pack (the one we choose as preferred) sorts
307 # lexically after the "AB" pack, meaning that omitting the
308 # --preferred-pack argument would cause this test to fail (since
309 # the MIDX code would select the copy of "b" in the "AB" pack).
310 git pack-objects objects/pack/test-AB <<-EOF &&
314 bc=$(git pack-objects objects/pack/test-BC <<-EOF
318 ) &&
320 git multi-pack-index --object-dir=objects \
321 write --preferred-pack=test-BC-$bc.idx 2>err &&
322 test_must_be_empty err &&
324 test-tool read-midx --show-objects objects >out &&
326 ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
327 cut -d" " -f1) &&
328 printf "%s %s\tobjects/pack/test-BC-%s.pack\n" \
329 "$b" "$ofs" "$bc" >expect &&
330 grep ^$b out >actual &&
332 test_cmp expect actual
336 test_expect_success 'preferred packs must be non-empty' '
337 test_when_finished rm -rf preferred.git &&
338 git init preferred.git &&
340 cd preferred.git &&
342 test_commit base &&
343 git repack -ad &&
345 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
347 test_must_fail git multi-pack-index write \
348 --preferred-pack=pack-$empty.pack 2>err &&
349 grep "with no objects" err
353 test_expect_success 'preferred pack from existing MIDX without bitmaps' '
354 git init preferred-without-bitmaps &&
356 cd preferred-without-bitmaps &&
358 test_commit one &&
359 pack="$(git pack-objects --all $objdir/pack/pack </dev/null)" &&
361 git multi-pack-index write &&
363 # make another pack so that the subsequent MIDX write
364 # has something to do
365 test_commit two &&
366 git repack -d &&
368 # write a new MIDX without bitmaps reusing the singular
369 # pack from the existing MIDX as the preferred pack in
370 # the new MIDX
371 git multi-pack-index write --preferred-pack=pack-$pack.pack
376 test_expect_success 'verify multi-pack-index success' '
377 git multi-pack-index verify --object-dir=$objdir
380 test_expect_success 'verify progress off for redirected stderr' '
381 git multi-pack-index verify --object-dir=$objdir 2>err &&
382 test_line_count = 0 err
385 test_expect_success 'verify force progress on for stderr' '
386 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
387 test_file_not_empty err
390 test_expect_success 'verify with the --no-progress option' '
391 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
392 test_line_count = 0 err
395 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
396 corrupt_midx_and_verify() {
397 POS=$1 &&
398 DATA="${2:-\0}" &&
399 OBJDIR=$3 &&
400 GREPSTR="$4" &&
401 COMMAND="$5" &&
402 if test -z "$COMMAND"
403 then
404 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
405 fi &&
406 FILE=$OBJDIR/pack/multi-pack-index &&
407 chmod a+w $FILE &&
408 test_when_finished mv midx-backup $FILE &&
409 cp $FILE midx-backup &&
410 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
411 test_must_fail $COMMAND 2>test_err &&
412 grep -v "^+" test_err >err &&
413 test_grep "$GREPSTR" err
416 test_expect_success 'verify bad signature' '
417 corrupt_midx_and_verify 0 "\00" $objdir \
418 "multi-pack-index signature"
421 NUM_OBJECTS=74
422 MIDX_BYTE_VERSION=4
423 MIDX_BYTE_OID_VERSION=5
424 MIDX_BYTE_CHUNK_COUNT=6
425 MIDX_HEADER_SIZE=12
426 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
427 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
428 MIDX_NUM_CHUNKS=5
429 MIDX_CHUNK_LOOKUP_WIDTH=12
430 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
431 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
432 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
433 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
434 MIDX_OID_FANOUT_WIDTH=4
435 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
436 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
437 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
438 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
439 MIDX_OFFSET_WIDTH=8
440 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
441 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
443 test_expect_success 'verify bad version' '
444 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
445 "multi-pack-index version"
448 test_expect_success 'verify bad OID version' '
449 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
450 "hash version"
453 test_expect_success 'verify truncated chunk count' '
454 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
455 "final chunk has non-zero id"
458 test_expect_success 'verify extended chunk count' '
459 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
460 "terminating chunk id appears earlier than expected"
463 test_expect_success 'verify missing required chunk' '
464 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
465 "required pack-name chunk missing"
468 test_expect_success 'verify invalid chunk offset' '
469 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
470 "improper chunk offset(s)"
473 test_expect_success 'verify packnames out of order' '
474 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
475 "pack names out of order"
478 test_expect_success 'verify packnames out of order' '
479 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
480 "failed to load pack"
483 test_expect_success 'verify oid fanout out of order' '
484 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
485 "oid fanout out of order"
488 test_expect_success 'verify oid lookup out of order' '
489 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
490 "oid lookup out of order"
493 test_expect_success 'verify incorrect pack-int-id' '
494 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
495 "bad pack-int-id"
498 test_expect_success 'verify incorrect offset' '
499 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
500 "incorrect object offset"
503 test_expect_success 'git-fsck incorrect offset' '
504 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
505 "incorrect object offset" \
506 "git -c core.multiPackIndex=true fsck" &&
507 test_unconfig core.multiPackIndex &&
508 test_must_fail git fsck &&
509 git -c core.multiPackIndex=false fsck
512 test_expect_success 'git fsck shows MIDX output with --progress' '
513 git fsck --progress 2>err &&
514 grep "Verifying OID order in multi-pack-index" err &&
515 grep "Verifying object offsets" err
518 test_expect_success 'git fsck suppresses MIDX output with --no-progress' '
519 git fsck --no-progress 2>err &&
520 ! grep "Verifying OID order in multi-pack-index" err &&
521 ! grep "Verifying object offsets" err
524 test_expect_success 'corrupt MIDX is not reused' '
525 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
526 "incorrect object offset" &&
527 git multi-pack-index write 2>err &&
528 test_grep checksum.mismatch err &&
529 git multi-pack-index verify
532 test_expect_success 'verify incorrect checksum' '
533 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
534 corrupt_midx_and_verify $pos \
535 "\377\377\377\377\377\377\377\377\377\377" \
536 $objdir "incorrect checksum"
539 test_expect_success 'repack progress off for redirected stderr' '
540 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
541 test_line_count = 0 err
544 test_expect_success 'repack force progress on for stderr' '
545 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
546 test_file_not_empty err
549 test_expect_success 'repack with the --no-progress option' '
550 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
551 test_line_count = 0 err
554 test_expect_success 'repack removes multi-pack-index when deleting packs' '
555 test_path_is_file $objdir/pack/multi-pack-index &&
556 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
557 # multi-pack-index after repacking, but set "core.multiPackIndex" to
558 # true so that "git repack" can read the existing MIDX.
559 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
560 test_path_is_missing $objdir/pack/multi-pack-index
563 test_expect_success 'repack preserves multi-pack-index when creating packs' '
564 git init preserve &&
565 test_when_finished "rm -fr preserve" &&
567 cd preserve &&
568 packdir=.git/objects/pack &&
569 midx=$packdir/multi-pack-index &&
571 test_commit 1 &&
572 pack1=$(git pack-objects --all $packdir/pack) &&
573 touch $packdir/pack-$pack1.keep &&
574 test_commit 2 &&
575 pack2=$(git pack-objects --revs $packdir/pack) &&
576 touch $packdir/pack-$pack2.keep &&
578 git multi-pack-index write &&
579 cp $midx $midx.bak &&
581 cat >pack-input <<-EOF &&
582 HEAD
583 ^HEAD~1
585 test_commit 3 &&
586 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
587 test_commit 4 &&
588 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
590 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
591 ls -la $packdir &&
592 test_path_is_file $packdir/pack-$pack1.pack &&
593 test_path_is_file $packdir/pack-$pack2.pack &&
594 test_path_is_missing $packdir/pack-$pack3.pack &&
595 test_path_is_missing $packdir/pack-$pack4.pack &&
596 test_cmp_bin $midx.bak $midx
600 compare_results_with_midx "after repack"
602 test_expect_success 'multi-pack-index and pack-bitmap' '
603 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
604 git -c repack.writeBitmaps=true repack -ad &&
605 git multi-pack-index write &&
606 git rev-list --test-bitmap HEAD
609 test_expect_success 'multi-pack-index and alternates' '
610 git init --bare alt.git &&
611 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
612 echo content1 >file1 &&
613 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
614 git cat-file blob $altblob &&
615 git rev-list --all
618 compare_results_with_midx "with alternate (local midx)"
620 test_expect_success 'multi-pack-index in an alternate' '
621 mv .git/objects/pack/* alt.git/objects/pack &&
622 test_commit add_local_objects &&
623 git repack --local &&
624 git multi-pack-index write &&
625 midx_read_expect 1 3 4 $objdir &&
626 git reset --hard HEAD~1 &&
627 rm -f .git/objects/pack/*
630 compare_results_with_midx "with alternate (remote midx)"
632 # usage: corrupt_data <file> <pos> [<data>]
633 corrupt_data () {
634 file=$1
635 pos=$2
636 data="${3:-\0}"
637 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
640 # Force 64-bit offsets by manipulating the idx file.
641 # This makes the IDX file _incorrect_ so be careful to clean up after!
642 test_expect_success 'force some 64-bit offsets with pack-objects' '
643 mkdir objects64 &&
644 mkdir objects64/pack &&
645 for i in $(test_seq 1 11)
647 generate_objects 11 || return 1
648 done &&
649 commit_and_list_objects &&
650 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
651 idx64=objects64/pack/test-64-$pack64.idx &&
652 chmod u+w $idx64 &&
653 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
654 # objects64 is not a real repository, but can serve as an alternate
655 # anyway so we can write a MIDX into it
656 git init repo &&
657 test_when_finished "rm -fr repo" &&
659 cd repo &&
660 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
661 midx64=$(git multi-pack-index --object-dir=../objects64 write)
662 ) &&
663 midx_read_expect 1 63 5 objects64 " large-offsets"
666 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
667 git multi-pack-index verify --object-dir=objects64
670 NUM_OBJECTS=63
671 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
672 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
673 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
674 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
675 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
677 test_expect_success 'verify incorrect 64-bit offset' '
678 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
679 "incorrect object offset"
682 test_expect_success 'setup expire tests' '
683 mkdir dup &&
685 cd dup &&
686 git init &&
687 test-tool genrandom "data" 4096 >large_file.txt &&
688 git update-index --add large_file.txt &&
689 for i in $(test_seq 1 20)
691 test_commit $i || exit 1
692 done &&
693 git branch A HEAD &&
694 git branch B HEAD~8 &&
695 git branch C HEAD~13 &&
696 git branch D HEAD~16 &&
697 git branch E HEAD~18 &&
698 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
699 refs/heads/A
700 ^refs/heads/B
702 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
703 refs/heads/B
704 ^refs/heads/C
706 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
707 refs/heads/C
708 ^refs/heads/D
710 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
711 refs/heads/D
712 ^refs/heads/E
714 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
715 refs/heads/E
717 git multi-pack-index write &&
718 cp -r .git/objects/pack .git/objects/pack-backup
722 test_expect_success 'expire does not remove any packs' '
724 cd dup &&
725 ls .git/objects/pack >expect &&
726 git multi-pack-index expire &&
727 ls .git/objects/pack >actual &&
728 test_cmp expect actual
732 test_expect_success 'expire progress off for redirected stderr' '
734 cd dup &&
735 git multi-pack-index expire 2>err &&
736 test_line_count = 0 err
740 test_expect_success 'expire force progress on for stderr' '
742 cd dup &&
743 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
744 test_file_not_empty err
748 test_expect_success 'expire with the --no-progress option' '
750 cd dup &&
751 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
752 test_line_count = 0 err
756 test_expect_success 'expire removes unreferenced packs' '
758 cd dup &&
759 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
760 refs/heads/A
761 ^refs/heads/C
763 git multi-pack-index write &&
764 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
765 git multi-pack-index expire &&
766 ls .git/objects/pack >actual &&
767 test_cmp expect actual &&
768 ls .git/objects/pack/ | grep idx >expect-idx &&
769 test-tool read-midx .git/objects | grep idx >actual-midx &&
770 test_cmp expect-idx actual-midx &&
771 git multi-pack-index verify &&
772 git fsck
776 test_expect_success 'repack with minimum size does not alter existing packs' '
778 cd dup &&
779 rm -rf .git/objects/pack &&
780 mv .git/objects/pack-backup .git/objects/pack &&
781 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
782 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
783 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
784 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
785 ls .git/objects/pack >expect &&
786 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
787 git multi-pack-index repack --batch-size=$MINSIZE &&
788 ls .git/objects/pack >actual &&
789 test_cmp expect actual
793 test_expect_success 'repack respects repack.packKeptObjects=false' '
794 test_when_finished rm -f dup/.git/objects/pack/*keep &&
796 cd dup &&
797 ls .git/objects/pack/*idx >idx-list &&
798 test_line_count = 5 idx-list &&
799 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
800 test_line_count = 5 keep-list &&
801 for keep in $(cat keep-list)
803 touch $keep || return 1
804 done &&
805 git multi-pack-index repack --batch-size=0 &&
806 ls .git/objects/pack/*idx >idx-list &&
807 test_line_count = 5 idx-list &&
808 test-tool read-midx .git/objects | grep idx >midx-list &&
809 test_line_count = 5 midx-list &&
810 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
811 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
812 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
813 ls .git/objects/pack/*idx >idx-list &&
814 test_line_count = 5 idx-list &&
815 test-tool read-midx .git/objects | grep idx >midx-list &&
816 test_line_count = 5 midx-list
820 test_expect_success 'repack creates a new pack' '
822 cd dup &&
823 ls .git/objects/pack/*idx >idx-list &&
824 test_line_count = 5 idx-list &&
825 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
826 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
827 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
828 ls .git/objects/pack/*idx >idx-list &&
829 test_line_count = 6 idx-list &&
830 test-tool read-midx .git/objects | grep idx >midx-list &&
831 test_line_count = 6 midx-list
835 test_expect_success 'repack (all) ignores cruft pack' '
836 git init repo &&
837 test_when_finished "rm -fr repo" &&
839 cd repo &&
841 test_commit base &&
842 test_commit --no-tag unreachable &&
844 git reset --hard base &&
845 git reflog expire --all --expire=all &&
846 git repack --cruft -d &&
848 git multi-pack-index write &&
850 find $objdir/pack | sort >before &&
851 git multi-pack-index repack --batch-size=0 &&
852 find $objdir/pack | sort >after &&
854 test_cmp before after
858 test_expect_success 'repack (--batch-size) ignores cruft pack' '
859 git init repo &&
860 test_when_finished "rm -fr repo" &&
862 cd repo &&
864 test_commit_bulk 5 &&
865 test_commit --no-tag unreachable &&
867 git reset --hard HEAD^ &&
868 git reflog expire --all --expire=all &&
869 git repack --cruft -d &&
871 test_commit four &&
873 find $objdir/pack -type f -name "*.pack" | sort >before &&
874 git repack -d &&
875 find $objdir/pack -type f -name "*.pack" | sort >after &&
877 pack="$(comm -13 before after)" &&
878 test_file_size "$pack" >sz &&
879 # Set --batch-size to twice the size of the pack created
880 # in the previous step, since this is enough to
881 # accommodate it and the cruft pack.
883 # This means that the MIDX machinery *could* combine the
884 # new and cruft packs together.
886 # We ensure that it does not below.
887 batch="$((($(cat sz) * 2)))" &&
889 git multi-pack-index write &&
891 find $objdir/pack | sort >before &&
892 git multi-pack-index repack --batch-size=$batch &&
893 find $objdir/pack | sort >after &&
895 test_cmp before after
899 test_expect_success 'expire removes repacked packs' '
901 cd dup &&
902 ls -al .git/objects/pack/*pack &&
903 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
904 git multi-pack-index expire &&
905 ls -S .git/objects/pack/*pack >actual &&
906 test_cmp expect actual &&
907 test-tool read-midx .git/objects | grep idx >midx-list &&
908 test_line_count = 4 midx-list
912 test_expect_success 'expire works when adding new packs' '
914 cd dup &&
915 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
916 refs/heads/A
917 ^refs/heads/B
919 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
920 refs/heads/B
921 ^refs/heads/C
923 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
924 refs/heads/C
925 ^refs/heads/D
927 git multi-pack-index write &&
928 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
929 refs/heads/D
930 ^refs/heads/E
932 git multi-pack-index write &&
933 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
934 refs/heads/E
936 git multi-pack-index expire &&
937 ls .git/objects/pack/ | grep idx >expect &&
938 test-tool read-midx .git/objects | grep idx >actual &&
939 test_cmp expect actual &&
940 git multi-pack-index verify
944 test_expect_success 'expire respects .keep files' '
946 cd dup &&
947 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
948 refs/heads/A
950 git multi-pack-index write &&
951 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
952 touch $PACKA.keep &&
953 git multi-pack-index expire &&
954 test_path_is_file $PACKA.idx &&
955 test_path_is_file $PACKA.keep &&
956 test_path_is_file $PACKA.pack &&
957 test-tool read-midx .git/objects | grep idx >midx-list &&
958 test_line_count = 2 midx-list
962 test_expect_success 'expiring unreferenced cruft pack retains pack' '
963 git init repo &&
964 test_when_finished "rm -fr repo" &&
966 cd repo &&
968 test_commit base &&
969 test_commit --no-tag unreachable &&
970 unreachable=$(git rev-parse HEAD) &&
972 git reset --hard base &&
973 git reflog expire --all --expire=all &&
974 git repack --cruft -d &&
975 mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
977 echo "base..$unreachable" >in &&
978 pack="$(git pack-objects --revs --delta-base-offset \
979 $objdir/pack/pack <in)" &&
981 # Preferring the contents of "$pack" will leave the
982 # cruft pack unreferenced (ie., none of the objects
983 # contained in the cruft pack will have their MIDX copy
984 # selected from the cruft pack).
985 git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
986 git multi-pack-index expire &&
988 test_path_is_file "$mtimes"
992 test_expect_success 'repack --batch-size=0 repacks everything' '
993 cp -r dup dup2 &&
995 cd dup &&
996 rm .git/objects/pack/*.keep &&
997 ls .git/objects/pack/*idx >idx-list &&
998 test_line_count = 2 idx-list &&
999 git multi-pack-index repack --batch-size=0 &&
1000 ls .git/objects/pack/*idx >idx-list &&
1001 test_line_count = 3 idx-list &&
1002 test-tool read-midx .git/objects | grep idx >midx-list &&
1003 test_line_count = 3 midx-list &&
1004 git multi-pack-index expire &&
1005 ls -al .git/objects/pack/*idx >idx-list &&
1006 test_line_count = 1 idx-list &&
1007 git multi-pack-index repack --batch-size=0 &&
1008 ls -al .git/objects/pack/*idx >new-idx-list &&
1009 test_cmp idx-list new-idx-list
1013 test_expect_success 'repack --batch-size=<large> repacks everything' '
1015 cd dup2 &&
1016 rm .git/objects/pack/*.keep &&
1017 ls .git/objects/pack/*idx >idx-list &&
1018 test_line_count = 2 idx-list &&
1019 git multi-pack-index repack --batch-size=2000000 &&
1020 ls .git/objects/pack/*idx >idx-list &&
1021 test_line_count = 3 idx-list &&
1022 test-tool read-midx .git/objects | grep idx >midx-list &&
1023 test_line_count = 3 midx-list &&
1024 git multi-pack-index expire &&
1025 ls -al .git/objects/pack/*idx >idx-list &&
1026 test_line_count = 1 idx-list
1030 test_expect_success 'repack/expire loop' '
1031 git init repack-expire &&
1032 test_when_finished "rm -fr repack-expire" &&
1034 cd repack-expire &&
1036 test_commit_bulk 5 &&
1038 # Create three overlapping pack-files
1039 git rev-list --objects HEAD~3 >in-1 &&
1040 git rev-list --objects HEAD~4..HEAD~2 >in-2 &&
1041 git rev-list --objects HEAD~3..HEAD >in-3 &&
1043 # Create disconnected blobs
1044 obj1=$(git hash-object -w in-1) &&
1045 obj2=$(git hash-object -w in-2) &&
1046 obj3=$(git hash-object -w in-3) &&
1048 echo $obj2 >>in-2 &&
1049 echo $obj3 >>in-3 &&
1051 for i in $(test_seq 3)
1053 git pack-objects .git/objects/pack/test-$i <in-$i \
1054 || return 1
1055 done &&
1057 rm -fr .git/objects/pack/pack-* &&
1058 git multi-pack-index write &&
1060 for i in $(test_seq 3)
1062 for file in $(ls .git/objects/pack/test-$i*)
1064 test-tool chmtime =+$((3600*$i-25000)) $file || return 1
1065 done || return 1
1066 done &&
1068 pack1=$(ls .git/objects/pack/test-1-*.pack) &&
1069 pack2=$(ls .git/objects/pack/test-2-*.pack) &&
1070 pack3=$(ls .git/objects/pack/test-3-*.pack) &&
1072 # Prevent test-1 from being rewritten.
1073 touch "${pack1%.pack}.keep" &&
1075 # This repack-expire loop should repack all non-kept packs
1076 # into a new pack and then delete the old packs.
1077 git multi-pack-index repack &&
1078 git multi-pack-index expire &&
1080 test_path_is_missing $pack3 &&
1081 test_path_is_missing $pack2
1085 test_expect_success 'load reverse index when missing .idx, .pack' '
1086 git init repo &&
1087 test_when_finished "rm -fr repo" &&
1089 cd repo &&
1091 git config core.multiPackIndex true &&
1093 test_commit base &&
1094 git repack -ad &&
1095 git multi-pack-index write &&
1097 git rev-parse HEAD >tip &&
1098 pack=$(ls .git/objects/pack/pack-*.pack) &&
1099 idx=$(ls .git/objects/pack/pack-*.idx) &&
1101 mv $idx $idx.bak &&
1102 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1104 mv $idx.bak $idx &&
1106 mv $pack $pack.bak &&
1107 git cat-file --batch-check="%(objectsize:disk)" <tip
1111 test_expect_success 'usage shown without sub-command' '
1112 test_expect_code 129 git multi-pack-index 2>err &&
1113 ! test_grep "unrecognized subcommand" err
1116 test_expect_success 'complains when run outside of a repository' '
1117 nongit test_must_fail git multi-pack-index write 2>err &&
1118 grep "not a git repository" err
1121 test_expect_success 'repack with delta islands' '
1122 git init repo &&
1123 test_when_finished "rm -fr repo" &&
1125 cd repo &&
1127 test_commit first &&
1128 git repack &&
1129 test_commit second &&
1130 git repack &&
1132 git multi-pack-index write &&
1133 git -c repack.useDeltaIslands=true multi-pack-index repack
1137 corrupt_chunk () {
1138 midx=.git/objects/pack/multi-pack-index &&
1139 test_when_finished "rm -rf $midx" &&
1140 git repack -ad --write-midx &&
1141 corrupt_chunk_file $midx "$@"
1144 test_expect_success 'reader notices too-small oid fanout chunk' '
1145 corrupt_chunk OIDF clear 00000000 &&
1146 test_must_fail git log 2>err &&
1147 cat >expect <<-\EOF &&
1148 error: multi-pack-index OID fanout is of the wrong size
1149 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1151 test_cmp expect err
1154 test_expect_success 'reader notices too-small oid lookup chunk' '
1155 corrupt_chunk OIDL clear 00000000 &&
1156 test_must_fail git log 2>err &&
1157 cat >expect <<-\EOF &&
1158 error: multi-pack-index OID lookup chunk is the wrong size
1159 fatal: multi-pack-index required OID lookup chunk missing or corrupted
1161 test_cmp expect err
1164 test_expect_success 'reader notices too-small pack names chunk' '
1165 # There is no NUL to terminate the name here, so the
1166 # chunk is too short.
1167 corrupt_chunk PNAM clear 70656666 &&
1168 test_must_fail git log 2>err &&
1169 cat >expect <<-\EOF &&
1170 fatal: multi-pack-index pack-name chunk is too short
1172 test_cmp expect err
1175 test_expect_success 'reader handles unaligned chunks' '
1176 # A 9-byte PNAM means all of the subsequent chunks
1177 # will no longer be 4-byte aligned, but it is still
1178 # a valid one-pack chunk on its own (it is "foo.pack\0").
1179 corrupt_chunk PNAM clear 666f6f2e7061636b00 &&
1180 git -c core.multipackindex=false log >expect.out &&
1181 git -c core.multipackindex=true log >out 2>err &&
1182 test_cmp expect.out out &&
1183 cat >expect.err <<-\EOF &&
1184 error: chunk id 4f494446 not 4-byte aligned
1186 test_cmp expect.err err
1189 test_expect_success 'reader notices too-small object offset chunk' '
1190 corrupt_chunk OOFF clear 00000000 &&
1191 test_must_fail git log 2>err &&
1192 cat >expect <<-\EOF &&
1193 error: multi-pack-index object offset chunk is the wrong size
1194 fatal: multi-pack-index required object offsets chunk missing or corrupted
1196 test_cmp expect err
1199 test_expect_success 'reader bounds-checks large offset table' '
1200 # re-use the objects64 dir here to cheaply get access to a midx
1201 # with large offsets.
1202 git init repo &&
1203 test_when_finished "rm -rf repo" &&
1205 cd repo &&
1206 (cd ../objects64 && pwd) >.git/objects/info/alternates &&
1207 git multi-pack-index --object-dir=../objects64 write &&
1208 midx=../objects64/pack/multi-pack-index &&
1209 corrupt_chunk_file $midx LOFF clear &&
1210 # using only %(objectsize) is important here; see the commit
1211 # message for more details
1212 test_must_fail git cat-file --batch-all-objects \
1213 --batch-check="%(objectsize)" 2>err &&
1214 cat >expect <<-\EOF &&
1215 fatal: multi-pack-index large offset out of bounds
1217 test_cmp expect err
1221 test_expect_success 'reader notices too-small revindex chunk' '
1222 # We only get a revindex with bitmaps (and likewise only
1223 # load it when they are asked for).
1224 test_config repack.writeBitmaps true &&
1225 corrupt_chunk RIDX clear 00000000 &&
1226 git -c core.multipackIndex=false rev-list \
1227 --all --use-bitmap-index >expect.out &&
1228 git -c core.multipackIndex=true rev-list \
1229 --all --use-bitmap-index >out 2>err &&
1230 test_cmp expect.out out &&
1231 cat >expect.err <<-\EOF &&
1232 error: multi-pack-index reverse-index chunk is the wrong size
1233 warning: multi-pack bitmap is missing required reverse index
1235 test_cmp expect.err err
1238 test_expect_success 'reader notices out-of-bounds fanout' '
1239 # This is similar to the out-of-bounds fanout test in t5318. The values
1240 # in adjacent entries should be large but not identical (they
1241 # are used as hi/lo starts for a binary search, which would then abort
1242 # immediately).
1243 corrupt_chunk OIDF 0 $(printf "%02x000000" $(test_seq 0 254)) &&
1244 test_must_fail git log 2>err &&
1245 cat >expect <<-\EOF &&
1246 error: oid fanout out of order: fanout[254] = fe000000 > 5c = fanout[255]
1247 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1249 test_cmp expect err
1252 test_expect_success 'bitmapped packs are stored via the BTMP chunk' '
1253 test_when_finished "rm -fr repo" &&
1254 git init repo &&
1256 cd repo &&
1258 for i in 1 2 3 4 5
1260 test_commit "$i" &&
1261 git repack -d || return 1
1262 done &&
1264 find $objdir/pack -type f -name "*.idx" | xargs -n 1 basename |
1265 sort >packs &&
1267 git multi-pack-index write --stdin-packs <packs &&
1268 test_must_fail test-tool read-midx --bitmap $objdir 2>err &&
1269 cat >expect <<-\EOF &&
1270 error: MIDX does not contain the BTMP chunk
1272 test_cmp expect err &&
1274 git multi-pack-index write --stdin-packs --bitmap \
1275 --preferred-pack="$(head -n1 <packs)" <packs &&
1276 test-tool read-midx --bitmap $objdir >actual &&
1277 for i in $(test_seq $(wc -l <packs))
1279 sed -ne "${i}s/\.idx$/\.pack/p" packs &&
1280 echo " bitmap_pos: $((($i - 1) * 3))" &&
1281 echo " bitmap_nr: 3" || return 1
1282 done >expect &&
1283 test_cmp expect actual
1287 test_done