Merge branch 'cp/git-flush-is-an-env-bool'
[alt-git.git] / t / t5319-multi-pack-index.sh
blobdd09134db03638fd01e73d89acbac45cceaa6e46
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 'verify multi-pack-index success' '
354 git multi-pack-index verify --object-dir=$objdir
357 test_expect_success 'verify progress off for redirected stderr' '
358 git multi-pack-index verify --object-dir=$objdir 2>err &&
359 test_line_count = 0 err
362 test_expect_success 'verify force progress on for stderr' '
363 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
364 test_file_not_empty err
367 test_expect_success 'verify with the --no-progress option' '
368 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
369 test_line_count = 0 err
372 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
373 corrupt_midx_and_verify() {
374 POS=$1 &&
375 DATA="${2:-\0}" &&
376 OBJDIR=$3 &&
377 GREPSTR="$4" &&
378 COMMAND="$5" &&
379 if test -z "$COMMAND"
380 then
381 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
382 fi &&
383 FILE=$OBJDIR/pack/multi-pack-index &&
384 chmod a+w $FILE &&
385 test_when_finished mv midx-backup $FILE &&
386 cp $FILE midx-backup &&
387 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
388 test_must_fail $COMMAND 2>test_err &&
389 grep -v "^+" test_err >err &&
390 test_grep "$GREPSTR" err
393 test_expect_success 'verify bad signature' '
394 corrupt_midx_and_verify 0 "\00" $objdir \
395 "multi-pack-index signature"
398 NUM_OBJECTS=74
399 MIDX_BYTE_VERSION=4
400 MIDX_BYTE_OID_VERSION=5
401 MIDX_BYTE_CHUNK_COUNT=6
402 MIDX_HEADER_SIZE=12
403 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
404 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
405 MIDX_NUM_CHUNKS=5
406 MIDX_CHUNK_LOOKUP_WIDTH=12
407 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
408 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
409 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
410 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
411 MIDX_OID_FANOUT_WIDTH=4
412 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
413 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
414 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
415 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
416 MIDX_OFFSET_WIDTH=8
417 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
418 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
420 test_expect_success 'verify bad version' '
421 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
422 "multi-pack-index version"
425 test_expect_success 'verify bad OID version' '
426 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
427 "hash version"
430 test_expect_success 'verify truncated chunk count' '
431 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
432 "final chunk has non-zero id"
435 test_expect_success 'verify extended chunk count' '
436 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
437 "terminating chunk id appears earlier than expected"
440 test_expect_success 'verify missing required chunk' '
441 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
442 "required pack-name chunk missing"
445 test_expect_success 'verify invalid chunk offset' '
446 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
447 "improper chunk offset(s)"
450 test_expect_success 'verify packnames out of order' '
451 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
452 "pack names out of order"
455 test_expect_success 'verify packnames out of order' '
456 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
457 "failed to load pack"
460 test_expect_success 'verify oid fanout out of order' '
461 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
462 "oid fanout out of order"
465 test_expect_success 'verify oid lookup out of order' '
466 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
467 "oid lookup out of order"
470 test_expect_success 'verify incorrect pack-int-id' '
471 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
472 "bad pack-int-id"
475 test_expect_success 'verify incorrect offset' '
476 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
477 "incorrect object offset"
480 test_expect_success 'git-fsck incorrect offset' '
481 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
482 "incorrect object offset" \
483 "git -c core.multiPackIndex=true fsck" &&
484 test_unconfig core.multiPackIndex &&
485 test_must_fail git fsck &&
486 git -c core.multiPackIndex=false fsck
489 test_expect_success 'git fsck shows MIDX output with --progress' '
490 git fsck --progress 2>err &&
491 grep "Verifying OID order in multi-pack-index" err &&
492 grep "Verifying object offsets" err
495 test_expect_success 'git fsck suppresses MIDX output with --no-progress' '
496 git fsck --no-progress 2>err &&
497 ! grep "Verifying OID order in multi-pack-index" err &&
498 ! grep "Verifying object offsets" err
501 test_expect_success 'corrupt MIDX is not reused' '
502 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
503 "incorrect object offset" &&
504 git multi-pack-index write 2>err &&
505 test_grep checksum.mismatch err &&
506 git multi-pack-index verify
509 test_expect_success 'verify incorrect checksum' '
510 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
511 corrupt_midx_and_verify $pos \
512 "\377\377\377\377\377\377\377\377\377\377" \
513 $objdir "incorrect checksum"
516 test_expect_success 'repack progress off for redirected stderr' '
517 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
518 test_line_count = 0 err
521 test_expect_success 'repack force progress on for stderr' '
522 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
523 test_file_not_empty err
526 test_expect_success 'repack with the --no-progress option' '
527 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
528 test_line_count = 0 err
531 test_expect_success 'repack removes multi-pack-index when deleting packs' '
532 test_path_is_file $objdir/pack/multi-pack-index &&
533 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
534 # multi-pack-index after repacking, but set "core.multiPackIndex" to
535 # true so that "git repack" can read the existing MIDX.
536 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
537 test_path_is_missing $objdir/pack/multi-pack-index
540 test_expect_success 'repack preserves multi-pack-index when creating packs' '
541 git init preserve &&
542 test_when_finished "rm -fr preserve" &&
544 cd preserve &&
545 packdir=.git/objects/pack &&
546 midx=$packdir/multi-pack-index &&
548 test_commit 1 &&
549 pack1=$(git pack-objects --all $packdir/pack) &&
550 touch $packdir/pack-$pack1.keep &&
551 test_commit 2 &&
552 pack2=$(git pack-objects --revs $packdir/pack) &&
553 touch $packdir/pack-$pack2.keep &&
555 git multi-pack-index write &&
556 cp $midx $midx.bak &&
558 cat >pack-input <<-EOF &&
559 HEAD
560 ^HEAD~1
562 test_commit 3 &&
563 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
564 test_commit 4 &&
565 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
567 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
568 ls -la $packdir &&
569 test_path_is_file $packdir/pack-$pack1.pack &&
570 test_path_is_file $packdir/pack-$pack2.pack &&
571 test_path_is_missing $packdir/pack-$pack3.pack &&
572 test_path_is_missing $packdir/pack-$pack4.pack &&
573 test_cmp_bin $midx.bak $midx
577 compare_results_with_midx "after repack"
579 test_expect_success 'multi-pack-index and pack-bitmap' '
580 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
581 git -c repack.writeBitmaps=true repack -ad &&
582 git multi-pack-index write &&
583 git rev-list --test-bitmap HEAD
586 test_expect_success 'multi-pack-index and alternates' '
587 git init --bare alt.git &&
588 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
589 echo content1 >file1 &&
590 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
591 git cat-file blob $altblob &&
592 git rev-list --all
595 compare_results_with_midx "with alternate (local midx)"
597 test_expect_success 'multi-pack-index in an alternate' '
598 mv .git/objects/pack/* alt.git/objects/pack &&
599 test_commit add_local_objects &&
600 git repack --local &&
601 git multi-pack-index write &&
602 midx_read_expect 1 3 4 $objdir &&
603 git reset --hard HEAD~1 &&
604 rm -f .git/objects/pack/*
607 compare_results_with_midx "with alternate (remote midx)"
609 # usage: corrupt_data <file> <pos> [<data>]
610 corrupt_data () {
611 file=$1
612 pos=$2
613 data="${3:-\0}"
614 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
617 # Force 64-bit offsets by manipulating the idx file.
618 # This makes the IDX file _incorrect_ so be careful to clean up after!
619 test_expect_success 'force some 64-bit offsets with pack-objects' '
620 mkdir objects64 &&
621 mkdir objects64/pack &&
622 for i in $(test_seq 1 11)
624 generate_objects 11 || return 1
625 done &&
626 commit_and_list_objects &&
627 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
628 idx64=objects64/pack/test-64-$pack64.idx &&
629 chmod u+w $idx64 &&
630 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
631 # objects64 is not a real repository, but can serve as an alternate
632 # anyway so we can write a MIDX into it
633 git init repo &&
634 test_when_finished "rm -fr repo" &&
636 cd repo &&
637 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
638 midx64=$(git multi-pack-index --object-dir=../objects64 write)
639 ) &&
640 midx_read_expect 1 63 5 objects64 " large-offsets"
643 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
644 git multi-pack-index verify --object-dir=objects64
647 NUM_OBJECTS=63
648 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
649 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
650 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
651 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
652 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
654 test_expect_success 'verify incorrect 64-bit offset' '
655 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
656 "incorrect object offset"
659 test_expect_success 'setup expire tests' '
660 mkdir dup &&
662 cd dup &&
663 git init &&
664 test-tool genrandom "data" 4096 >large_file.txt &&
665 git update-index --add large_file.txt &&
666 for i in $(test_seq 1 20)
668 test_commit $i || exit 1
669 done &&
670 git branch A HEAD &&
671 git branch B HEAD~8 &&
672 git branch C HEAD~13 &&
673 git branch D HEAD~16 &&
674 git branch E HEAD~18 &&
675 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
676 refs/heads/A
677 ^refs/heads/B
679 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
680 refs/heads/B
681 ^refs/heads/C
683 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
684 refs/heads/C
685 ^refs/heads/D
687 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
688 refs/heads/D
689 ^refs/heads/E
691 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
692 refs/heads/E
694 git multi-pack-index write &&
695 cp -r .git/objects/pack .git/objects/pack-backup
699 test_expect_success 'expire does not remove any packs' '
701 cd dup &&
702 ls .git/objects/pack >expect &&
703 git multi-pack-index expire &&
704 ls .git/objects/pack >actual &&
705 test_cmp expect actual
709 test_expect_success 'expire progress off for redirected stderr' '
711 cd dup &&
712 git multi-pack-index expire 2>err &&
713 test_line_count = 0 err
717 test_expect_success 'expire force progress on for stderr' '
719 cd dup &&
720 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
721 test_file_not_empty err
725 test_expect_success 'expire with the --no-progress option' '
727 cd dup &&
728 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
729 test_line_count = 0 err
733 test_expect_success 'expire removes unreferenced packs' '
735 cd dup &&
736 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
737 refs/heads/A
738 ^refs/heads/C
740 git multi-pack-index write &&
741 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
742 git multi-pack-index expire &&
743 ls .git/objects/pack >actual &&
744 test_cmp expect actual &&
745 ls .git/objects/pack/ | grep idx >expect-idx &&
746 test-tool read-midx .git/objects | grep idx >actual-midx &&
747 test_cmp expect-idx actual-midx &&
748 git multi-pack-index verify &&
749 git fsck
753 test_expect_success 'repack with minimum size does not alter existing packs' '
755 cd dup &&
756 rm -rf .git/objects/pack &&
757 mv .git/objects/pack-backup .git/objects/pack &&
758 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
759 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
760 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
761 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
762 ls .git/objects/pack >expect &&
763 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
764 git multi-pack-index repack --batch-size=$MINSIZE &&
765 ls .git/objects/pack >actual &&
766 test_cmp expect actual
770 test_expect_success 'repack respects repack.packKeptObjects=false' '
771 test_when_finished rm -f dup/.git/objects/pack/*keep &&
773 cd dup &&
774 ls .git/objects/pack/*idx >idx-list &&
775 test_line_count = 5 idx-list &&
776 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
777 test_line_count = 5 keep-list &&
778 for keep in $(cat keep-list)
780 touch $keep || return 1
781 done &&
782 git multi-pack-index repack --batch-size=0 &&
783 ls .git/objects/pack/*idx >idx-list &&
784 test_line_count = 5 idx-list &&
785 test-tool read-midx .git/objects | grep idx >midx-list &&
786 test_line_count = 5 midx-list &&
787 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
788 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
789 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
790 ls .git/objects/pack/*idx >idx-list &&
791 test_line_count = 5 idx-list &&
792 test-tool read-midx .git/objects | grep idx >midx-list &&
793 test_line_count = 5 midx-list
797 test_expect_success 'repack creates a new pack' '
799 cd dup &&
800 ls .git/objects/pack/*idx >idx-list &&
801 test_line_count = 5 idx-list &&
802 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
803 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
804 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
805 ls .git/objects/pack/*idx >idx-list &&
806 test_line_count = 6 idx-list &&
807 test-tool read-midx .git/objects | grep idx >midx-list &&
808 test_line_count = 6 midx-list
812 test_expect_success 'repack (all) ignores cruft pack' '
813 git init repo &&
814 test_when_finished "rm -fr repo" &&
816 cd repo &&
818 test_commit base &&
819 test_commit --no-tag unreachable &&
821 git reset --hard base &&
822 git reflog expire --all --expire=all &&
823 git repack --cruft -d &&
825 git multi-pack-index write &&
827 find $objdir/pack | sort >before &&
828 git multi-pack-index repack --batch-size=0 &&
829 find $objdir/pack | sort >after &&
831 test_cmp before after
835 test_expect_success 'repack (--batch-size) ignores cruft pack' '
836 git init repo &&
837 test_when_finished "rm -fr repo" &&
839 cd repo &&
841 test_commit_bulk 5 &&
842 test_commit --no-tag unreachable &&
844 git reset --hard HEAD^ &&
845 git reflog expire --all --expire=all &&
846 git repack --cruft -d &&
848 test_commit four &&
850 find $objdir/pack -type f -name "*.pack" | sort >before &&
851 git repack -d &&
852 find $objdir/pack -type f -name "*.pack" | sort >after &&
854 pack="$(comm -13 before after)" &&
855 test_file_size "$pack" >sz &&
856 # Set --batch-size to twice the size of the pack created
857 # in the previous step, since this is enough to
858 # accommodate it and the cruft pack.
860 # This means that the MIDX machinery *could* combine the
861 # new and cruft packs together.
863 # We ensure that it does not below.
864 batch="$((($(cat sz) * 2)))" &&
866 git multi-pack-index write &&
868 find $objdir/pack | sort >before &&
869 git multi-pack-index repack --batch-size=$batch &&
870 find $objdir/pack | sort >after &&
872 test_cmp before after
876 test_expect_success 'expire removes repacked packs' '
878 cd dup &&
879 ls -al .git/objects/pack/*pack &&
880 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
881 git multi-pack-index expire &&
882 ls -S .git/objects/pack/*pack >actual &&
883 test_cmp expect actual &&
884 test-tool read-midx .git/objects | grep idx >midx-list &&
885 test_line_count = 4 midx-list
889 test_expect_success 'expire works when adding new packs' '
891 cd dup &&
892 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
893 refs/heads/A
894 ^refs/heads/B
896 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
897 refs/heads/B
898 ^refs/heads/C
900 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
901 refs/heads/C
902 ^refs/heads/D
904 git multi-pack-index write &&
905 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
906 refs/heads/D
907 ^refs/heads/E
909 git multi-pack-index write &&
910 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
911 refs/heads/E
913 git multi-pack-index expire &&
914 ls .git/objects/pack/ | grep idx >expect &&
915 test-tool read-midx .git/objects | grep idx >actual &&
916 test_cmp expect actual &&
917 git multi-pack-index verify
921 test_expect_success 'expire respects .keep files' '
923 cd dup &&
924 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
925 refs/heads/A
927 git multi-pack-index write &&
928 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
929 touch $PACKA.keep &&
930 git multi-pack-index expire &&
931 test_path_is_file $PACKA.idx &&
932 test_path_is_file $PACKA.keep &&
933 test_path_is_file $PACKA.pack &&
934 test-tool read-midx .git/objects | grep idx >midx-list &&
935 test_line_count = 2 midx-list
939 test_expect_success 'expiring unreferenced cruft pack retains pack' '
940 git init repo &&
941 test_when_finished "rm -fr repo" &&
943 cd repo &&
945 test_commit base &&
946 test_commit --no-tag unreachable &&
947 unreachable=$(git rev-parse HEAD) &&
949 git reset --hard base &&
950 git reflog expire --all --expire=all &&
951 git repack --cruft -d &&
952 mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
954 echo "base..$unreachable" >in &&
955 pack="$(git pack-objects --revs --delta-base-offset \
956 $objdir/pack/pack <in)" &&
958 # Preferring the contents of "$pack" will leave the
959 # cruft pack unreferenced (ie., none of the objects
960 # contained in the cruft pack will have their MIDX copy
961 # selected from the cruft pack).
962 git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
963 git multi-pack-index expire &&
965 test_path_is_file "$mtimes"
969 test_expect_success 'repack --batch-size=0 repacks everything' '
970 cp -r dup dup2 &&
972 cd dup &&
973 rm .git/objects/pack/*.keep &&
974 ls .git/objects/pack/*idx >idx-list &&
975 test_line_count = 2 idx-list &&
976 git multi-pack-index repack --batch-size=0 &&
977 ls .git/objects/pack/*idx >idx-list &&
978 test_line_count = 3 idx-list &&
979 test-tool read-midx .git/objects | grep idx >midx-list &&
980 test_line_count = 3 midx-list &&
981 git multi-pack-index expire &&
982 ls -al .git/objects/pack/*idx >idx-list &&
983 test_line_count = 1 idx-list &&
984 git multi-pack-index repack --batch-size=0 &&
985 ls -al .git/objects/pack/*idx >new-idx-list &&
986 test_cmp idx-list new-idx-list
990 test_expect_success 'repack --batch-size=<large> repacks everything' '
992 cd dup2 &&
993 rm .git/objects/pack/*.keep &&
994 ls .git/objects/pack/*idx >idx-list &&
995 test_line_count = 2 idx-list &&
996 git multi-pack-index repack --batch-size=2000000 &&
997 ls .git/objects/pack/*idx >idx-list &&
998 test_line_count = 3 idx-list &&
999 test-tool read-midx .git/objects | grep idx >midx-list &&
1000 test_line_count = 3 midx-list &&
1001 git multi-pack-index expire &&
1002 ls -al .git/objects/pack/*idx >idx-list &&
1003 test_line_count = 1 idx-list
1007 test_expect_success 'load reverse index when missing .idx, .pack' '
1008 git init repo &&
1009 test_when_finished "rm -fr repo" &&
1011 cd repo &&
1013 git config core.multiPackIndex true &&
1015 test_commit base &&
1016 git repack -ad &&
1017 git multi-pack-index write &&
1019 git rev-parse HEAD >tip &&
1020 pack=$(ls .git/objects/pack/pack-*.pack) &&
1021 idx=$(ls .git/objects/pack/pack-*.idx) &&
1023 mv $idx $idx.bak &&
1024 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1026 mv $idx.bak $idx &&
1028 mv $pack $pack.bak &&
1029 git cat-file --batch-check="%(objectsize:disk)" <tip
1033 test_expect_success 'usage shown without sub-command' '
1034 test_expect_code 129 git multi-pack-index 2>err &&
1035 ! test_grep "unrecognized subcommand" err
1038 test_expect_success 'complains when run outside of a repository' '
1039 nongit test_must_fail git multi-pack-index write 2>err &&
1040 grep "not a git repository" err
1043 test_expect_success 'repack with delta islands' '
1044 git init repo &&
1045 test_when_finished "rm -fr repo" &&
1047 cd repo &&
1049 test_commit first &&
1050 git repack &&
1051 test_commit second &&
1052 git repack &&
1054 git multi-pack-index write &&
1055 git -c repack.useDeltaIslands=true multi-pack-index repack
1059 corrupt_chunk () {
1060 midx=.git/objects/pack/multi-pack-index &&
1061 test_when_finished "rm -rf $midx" &&
1062 git repack -ad --write-midx &&
1063 corrupt_chunk_file $midx "$@"
1066 test_expect_success 'reader notices too-small oid fanout chunk' '
1067 corrupt_chunk OIDF clear 00000000 &&
1068 test_must_fail git log 2>err &&
1069 cat >expect <<-\EOF &&
1070 error: multi-pack-index OID fanout is of the wrong size
1071 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1073 test_cmp expect err
1076 test_expect_success 'reader notices too-small oid lookup chunk' '
1077 corrupt_chunk OIDL clear 00000000 &&
1078 test_must_fail git log 2>err &&
1079 cat >expect <<-\EOF &&
1080 error: multi-pack-index OID lookup chunk is the wrong size
1081 fatal: multi-pack-index required OID lookup chunk missing or corrupted
1083 test_cmp expect err
1086 test_expect_success 'reader notices too-small pack names chunk' '
1087 # There is no NUL to terminate the name here, so the
1088 # chunk is too short.
1089 corrupt_chunk PNAM clear 70656666 &&
1090 test_must_fail git log 2>err &&
1091 cat >expect <<-\EOF &&
1092 fatal: multi-pack-index pack-name chunk is too short
1094 test_cmp expect err
1097 test_expect_success 'reader handles unaligned chunks' '
1098 # A 9-byte PNAM means all of the subsequent chunks
1099 # will no longer be 4-byte aligned, but it is still
1100 # a valid one-pack chunk on its own (it is "foo.pack\0").
1101 corrupt_chunk PNAM clear 666f6f2e7061636b00 &&
1102 git -c core.multipackindex=false log >expect.out &&
1103 git -c core.multipackindex=true log >out 2>err &&
1104 test_cmp expect.out out &&
1105 cat >expect.err <<-\EOF &&
1106 error: chunk id 4f494446 not 4-byte aligned
1108 test_cmp expect.err err
1111 test_expect_success 'reader notices too-small object offset chunk' '
1112 corrupt_chunk OOFF clear 00000000 &&
1113 test_must_fail git log 2>err &&
1114 cat >expect <<-\EOF &&
1115 error: multi-pack-index object offset chunk is the wrong size
1116 fatal: multi-pack-index required object offsets chunk missing or corrupted
1118 test_cmp expect err
1121 test_expect_success 'reader bounds-checks large offset table' '
1122 # re-use the objects64 dir here to cheaply get access to a midx
1123 # with large offsets.
1124 git init repo &&
1125 test_when_finished "rm -rf repo" &&
1127 cd repo &&
1128 (cd ../objects64 && pwd) >.git/objects/info/alternates &&
1129 git multi-pack-index --object-dir=../objects64 write &&
1130 midx=../objects64/pack/multi-pack-index &&
1131 corrupt_chunk_file $midx LOFF clear &&
1132 # using only %(objectsize) is important here; see the commit
1133 # message for more details
1134 test_must_fail git cat-file --batch-all-objects \
1135 --batch-check="%(objectsize)" 2>err &&
1136 cat >expect <<-\EOF &&
1137 fatal: multi-pack-index large offset out of bounds
1139 test_cmp expect err
1143 test_expect_success 'reader notices too-small revindex chunk' '
1144 # We only get a revindex with bitmaps (and likewise only
1145 # load it when they are asked for).
1146 test_config repack.writeBitmaps true &&
1147 corrupt_chunk RIDX clear 00000000 &&
1148 git -c core.multipackIndex=false rev-list \
1149 --all --use-bitmap-index >expect.out &&
1150 git -c core.multipackIndex=true rev-list \
1151 --all --use-bitmap-index >out 2>err &&
1152 test_cmp expect.out out &&
1153 cat >expect.err <<-\EOF &&
1154 error: multi-pack-index reverse-index chunk is the wrong size
1155 warning: multi-pack bitmap is missing required reverse index
1157 test_cmp expect.err err
1160 test_expect_success 'reader notices out-of-bounds fanout' '
1161 # This is similar to the out-of-bounds fanout test in t5318. The values
1162 # in adjacent entries should be large but not identical (they
1163 # are used as hi/lo starts for a binary search, which would then abort
1164 # immediately).
1165 corrupt_chunk OIDF 0 $(printf "%02x000000" $(test_seq 0 254)) &&
1166 test_must_fail git log 2>err &&
1167 cat >expect <<-\EOF &&
1168 error: oid fanout out of order: fanout[254] = fe000000 > 5c = fanout[255]
1169 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1171 test_cmp expect err
1174 test_expect_success 'bitmapped packs are stored via the BTMP chunk' '
1175 test_when_finished "rm -fr repo" &&
1176 git init repo &&
1178 cd repo &&
1180 for i in 1 2 3 4 5
1182 test_commit "$i" &&
1183 git repack -d || return 1
1184 done &&
1186 find $objdir/pack -type f -name "*.idx" | xargs -n 1 basename |
1187 sort >packs &&
1189 git multi-pack-index write --stdin-packs <packs &&
1190 test_must_fail test-tool read-midx --bitmap $objdir 2>err &&
1191 cat >expect <<-\EOF &&
1192 error: MIDX does not contain the BTMP chunk
1194 test_cmp expect err &&
1196 git multi-pack-index write --stdin-packs --bitmap \
1197 --preferred-pack="$(head -n1 <packs)" <packs &&
1198 test-tool read-midx --bitmap $objdir >actual &&
1199 for i in $(test_seq $(wc -l <packs))
1201 sed -ne "${i}s/\.idx$/\.pack/p" packs &&
1202 echo " bitmap_pos: $((($i - 1) * 3))" &&
1203 echo " bitmap_nr: 3" || return 1
1204 done >expect &&
1205 test_cmp expect actual
1209 test_done