The eleventh batch
[git/gitster.git] / t / t5319-multi-pack-index.sh
blobfbbc218d04af4a56fcb66cb43a8ff12eeb55c6ce
1 #!/bin/sh
3 test_description='multi-pack-indexes'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-chunk.sh
8 . "$TEST_DIRECTORY"/lib-midx.sh
10 GIT_TEST_MULTI_PACK_INDEX=0
11 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
12 GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL=0
13 objdir=.git/objects
15 HASH_LEN=$(test_oid rawsz)
17 midx_read_expect () {
18 NUM_PACKS=$1
19 NUM_OBJECTS=$2
20 NUM_CHUNKS=$3
21 OBJECT_DIR=$4
22 EXTRA_CHUNKS="$5"
24 cat <<-EOF &&
25 header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
26 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
27 num_objects: $NUM_OBJECTS
28 packs:
29 EOF
30 if test $NUM_PACKS -ge 1
31 then
32 ls $OBJECT_DIR/pack/ | grep idx | sort
33 fi &&
34 printf "object-dir: $OBJECT_DIR\n"
35 } >expect &&
36 test-tool read-midx $OBJECT_DIR >actual &&
37 test_cmp expect actual
40 test_expect_success 'setup' '
41 test_oid_cache <<-EOF
42 idxoff sha1:2999
43 idxoff sha256:3739
45 packnameoff sha1:652
46 packnameoff sha256:940
48 fanoutoff sha1:1
49 fanoutoff sha256:3
50 EOF
53 test_expect_success "don't write midx with no packs" '
54 test_must_fail git multi-pack-index --object-dir=. write &&
55 test_path_is_missing pack/multi-pack-index
58 test_expect_success SHA1 'warn if a midx contains no oid' '
59 cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
60 test_must_fail git multi-pack-index verify &&
61 rm $objdir/pack/multi-pack-index
64 generate_objects () {
65 i=$1
66 iii=$(printf '%03i' $i)
68 test-tool genrandom "bar" 200 &&
69 test-tool genrandom "baz $iii" 50
70 } >wide_delta_$iii &&
72 test-tool genrandom "foo"$i 100 &&
73 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
74 test-tool genrandom "foo"$(( $i + 2 )) 100
75 } >deep_delta_$iii &&
77 echo $iii &&
78 test-tool genrandom "$iii" 8192
79 } >file_$iii &&
80 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
83 commit_and_list_objects () {
85 echo 101 &&
86 test-tool genrandom 100 8192;
87 } >file_101 &&
88 git update-index --add file_101 &&
89 tree=$(git write-tree) &&
90 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
92 echo $tree &&
93 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
94 } >obj-list &&
95 git reset --hard $commit
98 test_expect_success 'create objects' '
99 test_commit initial &&
100 for i in $(test_seq 1 5)
102 generate_objects $i || return 1
103 done &&
104 commit_and_list_objects
107 test_expect_success 'write midx with one v1 pack' '
108 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
109 test_when_finished rm $objdir/pack/test-$pack.pack \
110 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
111 git multi-pack-index --object-dir=$objdir write &&
112 midx_read_expect 1 18 4 $objdir
115 test_expect_success 'write midx with one v2 pack' '
116 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
117 git multi-pack-index --object-dir=$objdir write &&
118 midx_read_expect 1 18 4 $objdir
121 compare_results_with_midx "one v2 pack"
123 test_expect_success 'corrupt idx reports errors' '
124 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
125 mv $objdir/pack/$idx backup-$idx &&
126 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
128 # This is the minimum size for a sha-1 based .idx; this lets
129 # us pass perfunctory tests, but anything that actually opens and reads
130 # the idx file will complain.
131 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
133 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
134 grep "index unavailable" err
137 test_expect_success 'add more objects' '
138 for i in $(test_seq 6 10)
140 generate_objects $i || return 1
141 done &&
142 commit_and_list_objects
145 test_expect_success 'write midx with two packs' '
146 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
147 git multi-pack-index --object-dir=$objdir write &&
148 midx_read_expect 2 34 4 $objdir
151 compare_results_with_midx "two packs"
153 test_expect_success 'write midx with --stdin-packs' '
154 rm -fr $objdir/pack/multi-pack-index &&
156 idx="$(find $objdir/pack -name "test-2-*.idx")" &&
157 basename "$idx" >in &&
159 git multi-pack-index write --stdin-packs <in &&
161 test-tool read-midx $objdir | grep "\.idx$" >packs &&
163 test_cmp packs in
166 compare_results_with_midx "mixed mode (one pack + extra)"
168 test_expect_success 'write with no objects and preferred pack' '
169 test_when_finished "rm -rf empty" &&
170 git init empty &&
171 test_must_fail git -C empty multi-pack-index write \
172 --stdin-packs --preferred-pack=does-not-exist </dev/null 2>err &&
173 cat >expect <<-EOF &&
174 warning: unknown preferred pack: ${SQ}does-not-exist${SQ}
175 error: no pack files to index.
177 test_cmp expect err
180 test_expect_success 'write progress off for redirected stderr' '
181 git multi-pack-index --object-dir=$objdir write 2>err &&
182 test_line_count = 0 err
185 test_expect_success 'write force progress on for stderr' '
186 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --progress 2>err &&
187 test_file_not_empty err
190 test_expect_success 'write with the --no-progress option' '
191 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --no-progress 2>err &&
192 test_line_count = 0 err
195 test_expect_success 'add more packs' '
196 for j in $(test_seq 11 20)
198 generate_objects $j &&
199 commit_and_list_objects &&
200 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list || return 1
201 done
204 compare_results_with_midx "mixed mode (two packs + extra)"
206 test_expect_success 'write midx with twelve packs' '
207 git multi-pack-index --object-dir=$objdir write &&
208 midx_read_expect 12 74 4 $objdir
211 compare_results_with_midx "twelve packs"
213 test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
214 git init repo &&
215 git clone -s repo alternate &&
217 test_when_finished "rm -rf repo alternate" &&
220 cd repo &&
221 test_commit base &&
222 git repack -d
223 ) &&
225 ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
226 theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
227 touch "$ours" "$theirs" &&
230 cd alternate &&
231 git multi-pack-index --object-dir ../repo/.git/objects write
232 ) &&
234 # writing a midx in "repo" should not remove the .rev file in the
235 # alternate
236 test_path_is_file repo/.git/objects/pack/multi-pack-index &&
237 test_path_is_file $ours &&
238 test_path_is_missing $theirs
241 test_expect_success 'warn on improper hash version' '
242 git init --object-format=sha1 sha1 &&
244 cd sha1 &&
245 git config core.multiPackIndex true &&
246 test_commit 1 &&
247 git repack -a &&
248 git multi-pack-index write &&
249 mv .git/objects/pack/multi-pack-index ../mpi-sha1
250 ) &&
251 git init --object-format=sha256 sha256 &&
253 cd sha256 &&
254 git config core.multiPackIndex true &&
255 test_commit 1 &&
256 git repack -a &&
257 git multi-pack-index write &&
258 mv .git/objects/pack/multi-pack-index ../mpi-sha256
259 ) &&
261 cd sha1 &&
262 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
263 git log -1 2>err &&
264 test_grep "multi-pack-index hash version 2 does not match version 1" err
265 ) &&
267 cd sha256 &&
268 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
269 git log -1 2>err &&
270 test_grep "multi-pack-index hash version 1 does not match version 2" err
274 test_expect_success 'midx picks objects from preferred pack' '
275 test_when_finished rm -rf preferred.git &&
276 git init --bare preferred.git &&
278 cd preferred.git &&
280 a=$(echo "a" | git hash-object -w --stdin) &&
281 b=$(echo "b" | git hash-object -w --stdin) &&
282 c=$(echo "c" | git hash-object -w --stdin) &&
284 # Set up two packs, duplicating the object "B" at different
285 # offsets.
287 # Note that the "BC" pack (the one we choose as preferred) sorts
288 # lexically after the "AB" pack, meaning that omitting the
289 # --preferred-pack argument would cause this test to fail (since
290 # the MIDX code would select the copy of "b" in the "AB" pack).
291 git pack-objects objects/pack/test-AB <<-EOF &&
295 bc=$(git pack-objects objects/pack/test-BC <<-EOF
299 ) &&
301 git multi-pack-index --object-dir=objects \
302 write --preferred-pack=test-BC-$bc.idx 2>err &&
303 test_must_be_empty err &&
305 test-tool read-midx --show-objects objects >out &&
307 ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
308 cut -d" " -f1) &&
309 printf "%s %s\tobjects/pack/test-BC-%s.pack\n" \
310 "$b" "$ofs" "$bc" >expect &&
311 grep ^$b out >actual &&
313 test_cmp expect actual
317 test_expect_success 'preferred packs must be non-empty' '
318 test_when_finished rm -rf preferred.git &&
319 git init preferred.git &&
321 cd preferred.git &&
323 test_commit base &&
324 git repack -ad &&
326 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
328 test_must_fail git multi-pack-index write \
329 --preferred-pack=pack-$empty.pack 2>err &&
330 grep "with no objects" err
334 test_expect_success 'preferred pack from existing MIDX without bitmaps' '
335 git init preferred-without-bitmaps &&
337 cd preferred-without-bitmaps &&
339 test_commit one &&
340 pack="$(git pack-objects --all $objdir/pack/pack </dev/null)" &&
342 git multi-pack-index write &&
344 # make another pack so that the subsequent MIDX write
345 # has something to do
346 test_commit two &&
347 git repack -d &&
349 # write a new MIDX without bitmaps reusing the singular
350 # pack from the existing MIDX as the preferred pack in
351 # the new MIDX
352 git multi-pack-index write --preferred-pack=pack-$pack.pack
357 test_expect_success 'verify multi-pack-index success' '
358 git multi-pack-index verify --object-dir=$objdir
361 test_expect_success 'verify progress off for redirected stderr' '
362 git multi-pack-index verify --object-dir=$objdir 2>err &&
363 test_line_count = 0 err
366 test_expect_success 'verify force progress on for stderr' '
367 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
368 test_file_not_empty err
371 test_expect_success 'verify with the --no-progress option' '
372 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
373 test_line_count = 0 err
376 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
377 corrupt_midx_and_verify() {
378 POS=$1 &&
379 DATA="${2:-\0}" &&
380 OBJDIR=$3 &&
381 GREPSTR="$4" &&
382 COMMAND="$5" &&
383 if test -z "$COMMAND"
384 then
385 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
386 fi &&
387 FILE=$OBJDIR/pack/multi-pack-index &&
388 chmod a+w $FILE &&
389 test_when_finished mv midx-backup $FILE &&
390 cp $FILE midx-backup &&
391 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
392 test_must_fail $COMMAND 2>test_err &&
393 grep -v "^+" test_err >err &&
394 test_grep "$GREPSTR" err
397 test_expect_success 'verify bad signature' '
398 corrupt_midx_and_verify 0 "\00" $objdir \
399 "multi-pack-index signature"
402 NUM_OBJECTS=74
403 MIDX_BYTE_VERSION=4
404 MIDX_BYTE_OID_VERSION=5
405 MIDX_BYTE_CHUNK_COUNT=6
406 MIDX_HEADER_SIZE=12
407 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
408 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
409 MIDX_NUM_CHUNKS=5
410 MIDX_CHUNK_LOOKUP_WIDTH=12
411 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
412 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
413 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
414 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
415 MIDX_OID_FANOUT_WIDTH=4
416 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
417 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
418 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
419 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
420 MIDX_OFFSET_WIDTH=8
421 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
422 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
424 test_expect_success 'verify bad version' '
425 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
426 "multi-pack-index version"
429 test_expect_success 'verify bad OID version' '
430 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
431 "hash version"
434 test_expect_success 'verify truncated chunk count' '
435 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
436 "final chunk has non-zero id"
439 test_expect_success 'verify extended chunk count' '
440 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
441 "terminating chunk id appears earlier than expected"
444 test_expect_success 'verify missing required chunk' '
445 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
446 "required pack-name chunk missing"
449 test_expect_success 'verify invalid chunk offset' '
450 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
451 "improper chunk offset(s)"
454 test_expect_success 'verify packnames out of order' '
455 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
456 "pack names out of order"
459 test_expect_success 'verify packnames out of order' '
460 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
461 "failed to load pack"
464 test_expect_success 'verify oid fanout out of order' '
465 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
466 "oid fanout out of order"
469 test_expect_success 'verify oid lookup out of order' '
470 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
471 "oid lookup out of order"
474 test_expect_success 'verify incorrect pack-int-id' '
475 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
476 "bad pack-int-id"
479 test_expect_success 'verify incorrect offset' '
480 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
481 "incorrect object offset"
484 test_expect_success 'git-fsck incorrect offset' '
485 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
486 "incorrect object offset" \
487 "git -c core.multiPackIndex=true fsck" &&
488 test_unconfig core.multiPackIndex &&
489 test_must_fail git fsck &&
490 git -c core.multiPackIndex=false fsck
493 test_expect_success 'git fsck shows MIDX output with --progress' '
494 git fsck --progress 2>err &&
495 grep "Verifying OID order in multi-pack-index" err &&
496 grep "Verifying object offsets" err
499 test_expect_success 'git fsck suppresses MIDX output with --no-progress' '
500 git fsck --no-progress 2>err &&
501 ! grep "Verifying OID order in multi-pack-index" err &&
502 ! grep "Verifying object offsets" err
505 test_expect_success 'corrupt MIDX is not reused' '
506 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
507 "incorrect object offset" &&
508 git multi-pack-index write 2>err &&
509 test_grep checksum.mismatch err &&
510 git multi-pack-index verify
513 test_expect_success 'verify incorrect checksum' '
514 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
515 corrupt_midx_and_verify $pos \
516 "\377\377\377\377\377\377\377\377\377\377" \
517 $objdir "incorrect checksum"
520 test_expect_success 'repack progress off for redirected stderr' '
521 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
522 test_line_count = 0 err
525 test_expect_success 'repack force progress on for stderr' '
526 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
527 test_file_not_empty err
530 test_expect_success 'repack with the --no-progress option' '
531 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
532 test_line_count = 0 err
535 test_expect_success 'repack removes multi-pack-index when deleting packs' '
536 test_path_is_file $objdir/pack/multi-pack-index &&
537 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
538 # multi-pack-index after repacking, but set "core.multiPackIndex" to
539 # true so that "git repack" can read the existing MIDX.
540 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
541 test_path_is_missing $objdir/pack/multi-pack-index
544 test_expect_success 'repack preserves multi-pack-index when creating packs' '
545 git init preserve &&
546 test_when_finished "rm -fr preserve" &&
548 cd preserve &&
549 packdir=.git/objects/pack &&
550 midx=$packdir/multi-pack-index &&
552 test_commit 1 &&
553 pack1=$(git pack-objects --all $packdir/pack) &&
554 touch $packdir/pack-$pack1.keep &&
555 test_commit 2 &&
556 pack2=$(git pack-objects --revs $packdir/pack) &&
557 touch $packdir/pack-$pack2.keep &&
559 git multi-pack-index write &&
560 cp $midx $midx.bak &&
562 cat >pack-input <<-EOF &&
563 HEAD
564 ^HEAD~1
566 test_commit 3 &&
567 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
568 test_commit 4 &&
569 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
571 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
572 ls -la $packdir &&
573 test_path_is_file $packdir/pack-$pack1.pack &&
574 test_path_is_file $packdir/pack-$pack2.pack &&
575 test_path_is_missing $packdir/pack-$pack3.pack &&
576 test_path_is_missing $packdir/pack-$pack4.pack &&
577 test_cmp_bin $midx.bak $midx
581 compare_results_with_midx "after repack"
583 test_expect_success 'multi-pack-index and pack-bitmap' '
584 git -c repack.writeBitmaps=true repack -ad &&
585 git multi-pack-index write &&
586 git rev-list --test-bitmap HEAD
589 test_expect_success 'multi-pack-index and alternates' '
590 git init --bare alt.git &&
591 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
592 echo content1 >file1 &&
593 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
594 git cat-file blob $altblob &&
595 git rev-list --all
598 compare_results_with_midx "with alternate (local midx)"
600 test_expect_success 'multi-pack-index in an alternate' '
601 mv .git/objects/pack/* alt.git/objects/pack &&
602 test_commit add_local_objects &&
603 git repack --local &&
604 git multi-pack-index write &&
605 midx_read_expect 1 3 4 $objdir &&
606 git reset --hard HEAD~1 &&
607 rm -f .git/objects/pack/*
610 compare_results_with_midx "with alternate (remote midx)"
612 # usage: corrupt_data <file> <pos> [<data>]
613 corrupt_data () {
614 file=$1
615 pos=$2
616 data="${3:-\0}"
617 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
620 # Force 64-bit offsets by manipulating the idx file.
621 # This makes the IDX file _incorrect_ so be careful to clean up after!
622 test_expect_success 'force some 64-bit offsets with pack-objects' '
623 mkdir objects64 &&
624 mkdir objects64/pack &&
625 for i in $(test_seq 1 11)
627 generate_objects 11 || return 1
628 done &&
629 commit_and_list_objects &&
630 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
631 idx64=objects64/pack/test-64-$pack64.idx &&
632 chmod u+w $idx64 &&
633 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
634 # objects64 is not a real repository, but can serve as an alternate
635 # anyway so we can write a MIDX into it
636 git init repo &&
637 test_when_finished "rm -fr repo" &&
639 cd repo &&
640 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
641 midx64=$(git multi-pack-index --object-dir=../objects64 write)
642 ) &&
643 midx_read_expect 1 63 5 objects64 " large-offsets"
646 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
647 git multi-pack-index verify --object-dir=objects64
650 NUM_OBJECTS=63
651 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
652 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
653 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
654 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
655 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
657 test_expect_success 'verify incorrect 64-bit offset' '
658 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
659 "incorrect object offset"
662 test_expect_success 'setup expire tests' '
663 mkdir dup &&
665 cd dup &&
666 git init &&
667 test-tool genrandom "data" 4096 >large_file.txt &&
668 git update-index --add large_file.txt &&
669 for i in $(test_seq 1 20)
671 test_commit $i || exit 1
672 done &&
673 git branch A HEAD &&
674 git branch B HEAD~8 &&
675 git branch C HEAD~13 &&
676 git branch D HEAD~16 &&
677 git branch E HEAD~18 &&
678 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
679 refs/heads/A
680 ^refs/heads/B
682 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
683 refs/heads/B
684 ^refs/heads/C
686 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
687 refs/heads/C
688 ^refs/heads/D
690 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
691 refs/heads/D
692 ^refs/heads/E
694 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
695 refs/heads/E
697 git multi-pack-index write &&
698 cp -r .git/objects/pack .git/objects/pack-backup
702 test_expect_success 'expire does not remove any packs' '
704 cd dup &&
705 ls .git/objects/pack >expect &&
706 git multi-pack-index expire &&
707 ls .git/objects/pack >actual &&
708 test_cmp expect actual
712 test_expect_success 'expire progress off for redirected stderr' '
714 cd dup &&
715 git multi-pack-index expire 2>err &&
716 test_line_count = 0 err
720 test_expect_success 'expire force progress on for stderr' '
722 cd dup &&
723 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
724 test_file_not_empty err
728 test_expect_success 'expire with the --no-progress option' '
730 cd dup &&
731 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
732 test_line_count = 0 err
736 test_expect_success 'expire removes unreferenced packs' '
738 cd dup &&
739 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
740 refs/heads/A
741 ^refs/heads/C
743 git multi-pack-index write &&
744 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
745 git multi-pack-index expire &&
746 ls .git/objects/pack >actual &&
747 test_cmp expect actual &&
748 ls .git/objects/pack/ | grep idx >expect-idx &&
749 test-tool read-midx .git/objects | grep idx >actual-midx &&
750 test_cmp expect-idx actual-midx &&
751 git multi-pack-index verify &&
752 git fsck
756 test_expect_success 'repack with minimum size does not alter existing packs' '
758 cd dup &&
759 rm -rf .git/objects/pack &&
760 mv .git/objects/pack-backup .git/objects/pack &&
761 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
762 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
763 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
764 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
765 ls .git/objects/pack >expect &&
766 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
767 git multi-pack-index repack --batch-size=$MINSIZE &&
768 ls .git/objects/pack >actual &&
769 test_cmp expect actual
773 test_expect_success 'repack respects repack.packKeptObjects=false' '
774 test_when_finished rm -f dup/.git/objects/pack/*keep &&
776 cd dup &&
777 ls .git/objects/pack/*idx >idx-list &&
778 test_line_count = 5 idx-list &&
779 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
780 test_line_count = 5 keep-list &&
781 for keep in $(cat keep-list)
783 touch $keep || return 1
784 done &&
785 git multi-pack-index repack --batch-size=0 &&
786 ls .git/objects/pack/*idx >idx-list &&
787 test_line_count = 5 idx-list &&
788 test-tool read-midx .git/objects | grep idx >midx-list &&
789 test_line_count = 5 midx-list &&
790 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
791 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
792 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
793 ls .git/objects/pack/*idx >idx-list &&
794 test_line_count = 5 idx-list &&
795 test-tool read-midx .git/objects | grep idx >midx-list &&
796 test_line_count = 5 midx-list
800 test_expect_success 'repack creates a new pack' '
802 cd dup &&
803 ls .git/objects/pack/*idx >idx-list &&
804 test_line_count = 5 idx-list &&
805 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
806 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
807 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
808 ls .git/objects/pack/*idx >idx-list &&
809 test_line_count = 6 idx-list &&
810 test-tool read-midx .git/objects | grep idx >midx-list &&
811 test_line_count = 6 midx-list
815 test_expect_success 'repack (all) ignores cruft pack' '
816 git init repo &&
817 test_when_finished "rm -fr repo" &&
819 cd repo &&
821 test_commit base &&
822 test_commit --no-tag unreachable &&
824 git reset --hard base &&
825 git reflog expire --all --expire=all &&
826 git repack --cruft -d &&
828 git multi-pack-index write &&
830 find $objdir/pack | sort >before &&
831 git multi-pack-index repack --batch-size=0 &&
832 find $objdir/pack | sort >after &&
834 test_cmp before after
838 test_expect_success 'repack (--batch-size) ignores cruft pack' '
839 git init repo &&
840 test_when_finished "rm -fr repo" &&
842 cd repo &&
844 test_commit_bulk 5 &&
845 test_commit --no-tag unreachable &&
847 git reset --hard HEAD^ &&
848 git reflog expire --all --expire=all &&
849 git repack --cruft -d &&
851 test_commit four &&
853 find $objdir/pack -type f -name "*.pack" | sort >before &&
854 git repack -d &&
855 find $objdir/pack -type f -name "*.pack" | sort >after &&
857 pack="$(comm -13 before after)" &&
858 test_file_size "$pack" >sz &&
859 # Set --batch-size to twice the size of the pack created
860 # in the previous step, since this is enough to
861 # accommodate it and the cruft pack.
863 # This means that the MIDX machinery *could* combine the
864 # new and cruft packs together.
866 # We ensure that it does not below.
867 batch="$((($(cat sz) * 2)))" &&
869 git multi-pack-index write &&
871 find $objdir/pack | sort >before &&
872 git multi-pack-index repack --batch-size=$batch &&
873 find $objdir/pack | sort >after &&
875 test_cmp before after
879 test_expect_success 'expire removes repacked packs' '
881 cd dup &&
882 ls -al .git/objects/pack/*pack &&
883 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
884 git multi-pack-index expire &&
885 ls -S .git/objects/pack/*pack >actual &&
886 test_cmp expect actual &&
887 test-tool read-midx .git/objects | grep idx >midx-list &&
888 test_line_count = 4 midx-list
892 test_expect_success 'expire works when adding new packs' '
894 cd dup &&
895 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
896 refs/heads/A
897 ^refs/heads/B
899 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
900 refs/heads/B
901 ^refs/heads/C
903 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
904 refs/heads/C
905 ^refs/heads/D
907 git multi-pack-index write &&
908 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
909 refs/heads/D
910 ^refs/heads/E
912 git multi-pack-index write &&
913 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
914 refs/heads/E
916 git multi-pack-index expire &&
917 ls .git/objects/pack/ | grep idx >expect &&
918 test-tool read-midx .git/objects | grep idx >actual &&
919 test_cmp expect actual &&
920 git multi-pack-index verify
924 test_expect_success 'expire respects .keep files' '
926 cd dup &&
927 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
928 refs/heads/A
930 git multi-pack-index write &&
931 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
932 touch $PACKA.keep &&
933 git multi-pack-index expire &&
934 test_path_is_file $PACKA.idx &&
935 test_path_is_file $PACKA.keep &&
936 test_path_is_file $PACKA.pack &&
937 test-tool read-midx .git/objects | grep idx >midx-list &&
938 test_line_count = 2 midx-list
942 test_expect_success 'expiring unreferenced cruft pack retains pack' '
943 git init repo &&
944 test_when_finished "rm -fr repo" &&
946 cd repo &&
948 test_commit base &&
949 test_commit --no-tag unreachable &&
950 unreachable=$(git rev-parse HEAD) &&
952 git reset --hard base &&
953 git reflog expire --all --expire=all &&
954 git repack --cruft -d &&
955 mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
957 echo "base..$unreachable" >in &&
958 pack="$(git pack-objects --revs --delta-base-offset \
959 $objdir/pack/pack <in)" &&
961 # Preferring the contents of "$pack" will leave the
962 # cruft pack unreferenced (ie., none of the objects
963 # contained in the cruft pack will have their MIDX copy
964 # selected from the cruft pack).
965 git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
966 git multi-pack-index expire &&
968 test_path_is_file "$mtimes"
972 test_expect_success 'repack --batch-size=0 repacks everything' '
973 cp -r dup dup2 &&
975 cd dup &&
976 rm .git/objects/pack/*.keep &&
977 ls .git/objects/pack/*idx >idx-list &&
978 test_line_count = 2 idx-list &&
979 git multi-pack-index repack --batch-size=0 &&
980 ls .git/objects/pack/*idx >idx-list &&
981 test_line_count = 3 idx-list &&
982 test-tool read-midx .git/objects | grep idx >midx-list &&
983 test_line_count = 3 midx-list &&
984 git multi-pack-index expire &&
985 ls -al .git/objects/pack/*idx >idx-list &&
986 test_line_count = 1 idx-list &&
987 git multi-pack-index repack --batch-size=0 &&
988 ls -al .git/objects/pack/*idx >new-idx-list &&
989 test_cmp idx-list new-idx-list
993 test_expect_success 'repack --batch-size=<large> repacks everything' '
995 cd dup2 &&
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=2000000 &&
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
1010 test_expect_success 'repack/expire loop' '
1011 git init repack-expire &&
1012 test_when_finished "rm -fr repack-expire" &&
1014 cd repack-expire &&
1016 test_commit_bulk 5 &&
1018 # Create three overlapping pack-files
1019 git rev-list --objects HEAD~3 >in-1 &&
1020 git rev-list --objects HEAD~4..HEAD~2 >in-2 &&
1021 git rev-list --objects HEAD~3..HEAD >in-3 &&
1023 # Create disconnected blobs
1024 obj1=$(git hash-object -w in-1) &&
1025 obj2=$(git hash-object -w in-2) &&
1026 obj3=$(git hash-object -w in-3) &&
1028 echo $obj2 >>in-2 &&
1029 echo $obj3 >>in-3 &&
1031 for i in $(test_seq 3)
1033 git pack-objects .git/objects/pack/test-$i <in-$i \
1034 || return 1
1035 done &&
1037 rm -fr .git/objects/pack/pack-* &&
1038 git multi-pack-index write &&
1040 for i in $(test_seq 3)
1042 for file in $(ls .git/objects/pack/test-$i*)
1044 test-tool chmtime =+$((3600*$i-25000)) $file || return 1
1045 done || return 1
1046 done &&
1048 pack1=$(ls .git/objects/pack/test-1-*.pack) &&
1049 pack2=$(ls .git/objects/pack/test-2-*.pack) &&
1050 pack3=$(ls .git/objects/pack/test-3-*.pack) &&
1052 # Prevent test-1 from being rewritten.
1053 touch "${pack1%.pack}.keep" &&
1055 # This repack-expire loop should repack all non-kept packs
1056 # into a new pack and then delete the old packs.
1057 git multi-pack-index repack &&
1058 git multi-pack-index expire &&
1060 test_path_is_missing $pack3 &&
1061 test_path_is_missing $pack2
1065 test_expect_success 'load reverse index when missing .idx, .pack' '
1066 git init repo &&
1067 test_when_finished "rm -fr repo" &&
1069 cd repo &&
1071 git config core.multiPackIndex true &&
1073 test_commit base &&
1074 git repack -ad &&
1075 git multi-pack-index write &&
1077 git rev-parse HEAD >tip &&
1078 pack=$(ls .git/objects/pack/pack-*.pack) &&
1079 idx=$(ls .git/objects/pack/pack-*.idx) &&
1081 mv $idx $idx.bak &&
1082 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1084 mv $idx.bak $idx &&
1086 mv $pack $pack.bak &&
1087 git cat-file --batch-check="%(objectsize:disk)" <tip
1091 test_expect_success 'usage shown without sub-command' '
1092 test_expect_code 129 git multi-pack-index 2>err &&
1093 ! test_grep "unrecognized subcommand" err
1096 test_expect_success 'complains when run outside of a repository' '
1097 nongit test_must_fail git multi-pack-index write 2>err &&
1098 grep "not a git repository" err
1101 test_expect_success 'repack with delta islands' '
1102 git init repo &&
1103 test_when_finished "rm -fr repo" &&
1105 cd repo &&
1107 test_commit first &&
1108 git repack &&
1109 test_commit second &&
1110 git repack &&
1112 git multi-pack-index write &&
1113 git -c repack.useDeltaIslands=true multi-pack-index repack
1117 corrupt_chunk () {
1118 midx=.git/objects/pack/multi-pack-index &&
1119 test_when_finished "rm -rf $midx" &&
1120 git repack -ad --write-midx &&
1121 corrupt_chunk_file $midx "$@"
1124 test_expect_success 'reader notices too-small oid fanout chunk' '
1125 corrupt_chunk OIDF clear 00000000 &&
1126 test_must_fail git log 2>err &&
1127 cat >expect <<-\EOF &&
1128 error: multi-pack-index OID fanout is of the wrong size
1129 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1131 test_cmp expect err
1134 test_expect_success 'reader notices too-small oid lookup chunk' '
1135 corrupt_chunk OIDL clear 00000000 &&
1136 test_must_fail git log 2>err &&
1137 cat >expect <<-\EOF &&
1138 error: multi-pack-index OID lookup chunk is the wrong size
1139 fatal: multi-pack-index required OID lookup chunk missing or corrupted
1141 test_cmp expect err
1144 test_expect_success 'reader notices too-small pack names chunk' '
1145 # There is no NUL to terminate the name here, so the
1146 # chunk is too short.
1147 corrupt_chunk PNAM clear 70656666 &&
1148 test_must_fail git log 2>err &&
1149 cat >expect <<-\EOF &&
1150 fatal: multi-pack-index pack-name chunk is too short
1152 test_cmp expect err
1155 test_expect_success 'reader handles unaligned chunks' '
1156 # A 9-byte PNAM means all of the subsequent chunks
1157 # will no longer be 4-byte aligned, but it is still
1158 # a valid one-pack chunk on its own (it is "foo.pack\0").
1159 corrupt_chunk PNAM clear 666f6f2e7061636b00 &&
1160 git -c core.multipackindex=false log >expect.out &&
1161 git -c core.multipackindex=true log >out 2>err &&
1162 test_cmp expect.out out &&
1163 cat >expect.err <<-\EOF &&
1164 error: chunk id 4f494446 not 4-byte aligned
1166 test_cmp expect.err err
1169 test_expect_success 'reader notices too-small object offset chunk' '
1170 corrupt_chunk OOFF clear 00000000 &&
1171 test_must_fail git log 2>err &&
1172 cat >expect <<-\EOF &&
1173 error: multi-pack-index object offset chunk is the wrong size
1174 fatal: multi-pack-index required object offsets chunk missing or corrupted
1176 test_cmp expect err
1179 test_expect_success 'reader bounds-checks large offset table' '
1180 # re-use the objects64 dir here to cheaply get access to a midx
1181 # with large offsets.
1182 git init repo &&
1183 test_when_finished "rm -rf repo" &&
1185 cd repo &&
1186 (cd ../objects64 && pwd) >.git/objects/info/alternates &&
1187 git multi-pack-index --object-dir=../objects64 write &&
1188 midx=../objects64/pack/multi-pack-index &&
1189 corrupt_chunk_file $midx LOFF clear &&
1190 # using only %(objectsize) is important here; see the commit
1191 # message for more details
1192 test_must_fail git cat-file --batch-all-objects \
1193 --batch-check="%(objectsize)" 2>err &&
1194 cat >expect <<-\EOF &&
1195 fatal: multi-pack-index large offset out of bounds
1197 test_cmp expect err
1201 test_expect_success 'reader notices too-small revindex chunk' '
1202 # We only get a revindex with bitmaps (and likewise only
1203 # load it when they are asked for).
1204 test_config repack.writeBitmaps true &&
1205 corrupt_chunk RIDX clear 00000000 &&
1206 git -c core.multipackIndex=false rev-list \
1207 --all --use-bitmap-index >expect.out &&
1208 git -c core.multipackIndex=true rev-list \
1209 --all --use-bitmap-index >out 2>err &&
1210 test_cmp expect.out out &&
1211 cat >expect.err <<-\EOF &&
1212 error: multi-pack-index reverse-index chunk is the wrong size
1213 warning: multi-pack bitmap is missing required reverse index
1215 test_cmp expect.err err
1218 test_expect_success 'reader notices out-of-bounds fanout' '
1219 # This is similar to the out-of-bounds fanout test in t5318. The values
1220 # in adjacent entries should be large but not identical (they
1221 # are used as hi/lo starts for a binary search, which would then abort
1222 # immediately).
1223 corrupt_chunk OIDF 0 $(printf "%02x000000" $(test_seq 0 254)) &&
1224 test_must_fail git log 2>err &&
1225 cat >expect <<-\EOF &&
1226 error: oid fanout out of order: fanout[254] = fe000000 > 5c = fanout[255]
1227 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1229 test_cmp expect err
1232 test_expect_success 'bitmapped packs are stored via the BTMP chunk' '
1233 test_when_finished "rm -fr repo" &&
1234 git init repo &&
1236 cd repo &&
1238 for i in 1 2 3 4 5
1240 test_commit "$i" &&
1241 git repack -d || return 1
1242 done &&
1244 find $objdir/pack -type f -name "*.idx" | xargs -n 1 basename |
1245 sort >packs &&
1247 git multi-pack-index write --stdin-packs <packs &&
1248 test_must_fail test-tool read-midx --bitmap $objdir 2>err &&
1249 cat >expect <<-\EOF &&
1250 error: MIDX does not contain the BTMP chunk
1252 test_cmp expect err &&
1254 git multi-pack-index write --stdin-packs --bitmap \
1255 --preferred-pack="$(head -n1 <packs)" <packs &&
1256 test-tool read-midx --bitmap $objdir >actual &&
1257 for i in $(test_seq $(wc -l <packs))
1259 sed -ne "${i}s/\.idx$/\.pack/p" packs &&
1260 echo " bitmap_pos: $((($i - 1) * 3))" &&
1261 echo " bitmap_nr: 3" || return 1
1262 done >expect &&
1263 test_cmp expect actual
1267 test_done