3 test_description='multi-pack-indexes'
16 header: 4d494458 1 $NUM_CHUNKS $NUM_PACKS
17 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
18 num_objects: $NUM_OBJECTS
21 if test $NUM_PACKS -ge 1
23 ls $OBJECT_DIR/pack/ | grep idx | sort
25 printf "object-dir: $OBJECT_DIR\n"
27 test-tool read-midx $OBJECT_DIR >actual &&
28 test_cmp expect actual
31 test_expect_success 'setup' '
38 packnameoff sha256:940
45 test_expect_success "don't write midx with no packs" '
46 test_must_fail git multi-pack-index --object-dir=. write &&
47 test_path_is_missing pack/multi-pack-index
50 test_expect_success "Warn if a midx contains no oid" '
51 cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
52 test_must_fail git multi-pack-index verify &&
53 rm $objdir/pack/multi-pack-index
58 iii=$(printf '%03i' $i)
60 test-tool genrandom "bar" 200 &&
61 test-tool genrandom "baz $iii" 50
64 test-tool genrandom "foo"$i 100 &&
65 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
66 test-tool genrandom "foo"$(( $i + 2 )) 100
70 test-tool genrandom "$iii" 8192
72 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
75 commit_and_list_objects () {
78 test-tool genrandom 100 8192;
80 git update-index --add file_101 &&
81 tree=$(git write-tree) &&
82 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
85 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
87 git reset --hard $commit
90 test_expect_success 'create objects' '
91 test_commit initial &&
92 for i in $(test_seq 1 5)
96 commit_and_list_objects
99 test_expect_success 'write midx with one v1 pack' '
100 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
101 test_when_finished rm $objdir/pack/test-$pack.pack \
102 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
103 git multi-pack-index --object-dir=$objdir write &&
104 midx_read_expect 1 18 4 $objdir
107 midx_git_two_modes () {
108 git -c core.multiPackIndex=false $1 >expect &&
109 git -c core.multiPackIndex=true $1 >actual &&
110 if [ "$2" = "sorted" ]
112 sort <expect >expect.sorted &&
113 mv expect.sorted expect &&
114 sort <actual >actual.sorted &&
115 mv actual.sorted actual
117 test_cmp expect actual
120 compare_results_with_midx () {
122 test_expect_success "check normal git operations: $MSG" '
123 midx_git_two_modes "rev-list --objects --all" &&
124 midx_git_two_modes "log --raw" &&
125 midx_git_two_modes "count-objects --verbose" &&
126 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
127 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
131 test_expect_success 'write midx with one v2 pack' '
132 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
133 git multi-pack-index --object-dir=$objdir write &&
134 midx_read_expect 1 18 4 $objdir
137 compare_results_with_midx "one v2 pack"
139 test_expect_success 'corrupt idx not opened' '
140 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
141 mv $objdir/pack/$idx backup-$idx &&
142 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
144 # This is the minimum size for a sha-1 based .idx; this lets
145 # us pass perfunctory tests, but anything that actually opens and reads
146 # the idx file will complain.
147 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
149 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
150 test_must_be_empty err
153 test_expect_success 'add more objects' '
154 for i in $(test_seq 6 10)
158 commit_and_list_objects
161 test_expect_success 'write midx with two packs' '
162 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
163 git multi-pack-index --object-dir=$objdir write &&
164 midx_read_expect 2 34 4 $objdir
167 compare_results_with_midx "two packs"
169 test_expect_success 'write progress off for redirected stderr' '
170 git multi-pack-index --object-dir=$objdir write 2>err &&
171 test_line_count = 0 err
174 test_expect_success 'write force progress on for stderr' '
175 git multi-pack-index --object-dir=$objdir --progress write 2>err &&
176 test_file_not_empty err
179 test_expect_success 'write with the --no-progress option' '
180 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&
181 test_line_count = 0 err
184 test_expect_success 'add more packs' '
185 for j in $(test_seq 11 20)
187 generate_objects $j &&
188 commit_and_list_objects &&
189 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
193 compare_results_with_midx "mixed mode (two packs + extra)"
195 test_expect_success 'write midx with twelve packs' '
196 git multi-pack-index --object-dir=$objdir write &&
197 midx_read_expect 12 74 4 $objdir
200 compare_results_with_midx "twelve packs"
202 test_expect_success 'verify multi-pack-index success' '
203 git multi-pack-index verify --object-dir=$objdir
206 test_expect_success 'verify progress off for redirected stderr' '
207 git multi-pack-index verify --object-dir=$objdir 2>err &&
208 test_line_count = 0 err
211 test_expect_success 'verify force progress on for stderr' '
212 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
213 test_file_not_empty err
216 test_expect_success 'verify with the --no-progress option' '
217 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
218 test_line_count = 0 err
221 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
222 corrupt_midx_and_verify() {
228 if test -z "$COMMAND"
230 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
232 FILE=$OBJDIR/pack/multi-pack-index &&
234 test_when_finished mv midx-backup $FILE &&
235 cp $FILE midx-backup &&
236 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
237 test_must_fail $COMMAND 2>test_err &&
238 grep -v "^+" test_err >err &&
239 test_i18ngrep "$GREPSTR" err
242 test_expect_success 'verify bad signature' '
243 corrupt_midx_and_verify 0 "\00" $objdir \
244 "multi-pack-index signature"
247 HASH_LEN=$(test_oid rawsz)
250 MIDX_BYTE_OID_VERSION=5
251 MIDX_BYTE_CHUNK_COUNT=6
253 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
254 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
256 MIDX_CHUNK_LOOKUP_WIDTH=12
257 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
258 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
259 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
260 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
261 MIDX_OID_FANOUT_WIDTH=4
262 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
263 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
264 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
265 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
267 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
268 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
270 test_expect_success 'verify bad version' '
271 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
272 "multi-pack-index version"
275 test_expect_success 'verify bad OID version' '
276 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\02" $objdir \
280 test_expect_success 'verify truncated chunk count' '
281 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
285 test_expect_success 'verify extended chunk count' '
286 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
287 "terminating multi-pack-index chunk id appears earlier than expected"
290 test_expect_success 'verify missing required chunk' '
291 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
295 test_expect_success 'verify invalid chunk offset' '
296 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
297 "invalid chunk offset (too large)"
300 test_expect_success 'verify packnames out of order' '
301 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
302 "pack names out of order"
305 test_expect_success 'verify packnames out of order' '
306 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
307 "failed to load pack"
310 test_expect_success 'verify oid fanout out of order' '
311 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
312 "oid fanout out of order"
315 test_expect_success 'verify oid lookup out of order' '
316 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
317 "oid lookup out of order"
320 test_expect_success 'verify incorrect pack-int-id' '
321 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
325 test_expect_success 'verify incorrect offset' '
326 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
327 "incorrect object offset"
330 test_expect_success 'git-fsck incorrect offset' '
331 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
332 "incorrect object offset" \
333 "git -c core.multipackindex=true fsck"
336 test_expect_success 'repack progress off for redirected stderr' '
337 git multi-pack-index --object-dir=$objdir repack 2>err &&
338 test_line_count = 0 err
341 test_expect_success 'repack force progress on for stderr' '
342 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&
343 test_file_not_empty err
346 test_expect_success 'repack with the --no-progress option' '
347 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&
348 test_line_count = 0 err
351 test_expect_success 'repack removes multi-pack-index when deleting packs' '
352 test_path_is_file $objdir/pack/multi-pack-index &&
353 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
354 # multi-pack-index after repacking, but set "core.multiPackIndex" to
355 # true so that "git repack" can read the existing MIDX.
356 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
357 test_path_is_missing $objdir/pack/multi-pack-index
360 test_expect_success 'repack preserves multi-pack-index when creating packs' '
362 test_when_finished "rm -fr preserve" &&
365 packdir=.git/objects/pack &&
366 midx=$packdir/multi-pack-index &&
369 pack1=$(git pack-objects --all $packdir/pack) &&
370 touch $packdir/pack-$pack1.keep &&
372 pack2=$(git pack-objects --revs $packdir/pack) &&
373 touch $packdir/pack-$pack2.keep &&
375 git multi-pack-index write &&
376 cp $midx $midx.bak &&
378 cat >pack-input <<-EOF &&
383 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
385 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
387 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
389 test_path_is_file $packdir/pack-$pack1.pack &&
390 test_path_is_file $packdir/pack-$pack2.pack &&
391 test_path_is_missing $packdir/pack-$pack3.pack &&
392 test_path_is_missing $packdir/pack-$pack4.pack &&
393 test_cmp_bin $midx.bak $midx
397 compare_results_with_midx "after repack"
399 test_expect_success 'multi-pack-index and pack-bitmap' '
400 git -c repack.writeBitmaps=true repack -ad &&
401 git multi-pack-index write &&
402 git rev-list --test-bitmap HEAD
405 test_expect_success 'multi-pack-index and alternates' '
406 git init --bare alt.git &&
407 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
408 echo content1 >file1 &&
409 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
410 git cat-file blob $altblob &&
414 compare_results_with_midx "with alternate (local midx)"
416 test_expect_success 'multi-pack-index in an alternate' '
417 mv .git/objects/pack/* alt.git/objects/pack &&
418 test_commit add_local_objects &&
419 git repack --local &&
420 git multi-pack-index write &&
421 midx_read_expect 1 3 4 $objdir &&
422 git reset --hard HEAD~1 &&
423 rm -f .git/objects/pack/*
426 compare_results_with_midx "with alternate (remote midx)"
428 # usage: corrupt_data <file> <pos> [<data>]
433 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
436 # Force 64-bit offsets by manipulating the idx file.
437 # This makes the IDX file _incorrect_ so be careful to clean up after!
438 test_expect_success 'force some 64-bit offsets with pack-objects' '
440 mkdir objects64/pack &&
441 for i in $(test_seq 1 11)
445 commit_and_list_objects &&
446 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
447 idx64=objects64/pack/test-64-$pack64.idx &&
449 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
450 midx64=$(git multi-pack-index --object-dir=objects64 write) &&
451 midx_read_expect 1 63 5 objects64 " large-offsets"
454 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
455 git multi-pack-index verify --object-dir=objects64
459 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
460 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
461 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
462 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
463 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
465 test_expect_success 'verify incorrect 64-bit offset' '
466 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
467 "incorrect object offset"
470 test_expect_success 'setup expire tests' '
475 test-tool genrandom "data" 4096 >large_file.txt &&
476 git update-index --add large_file.txt &&
477 for i in $(test_seq 1 20)
482 git branch B HEAD~8 &&
483 git branch C HEAD~13 &&
484 git branch D HEAD~16 &&
485 git branch E HEAD~18 &&
486 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
490 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
494 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
498 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
502 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
505 git multi-pack-index write &&
506 cp -r .git/objects/pack .git/objects/pack-backup
510 test_expect_success 'expire does not remove any packs' '
513 ls .git/objects/pack >expect &&
514 git multi-pack-index expire &&
515 ls .git/objects/pack >actual &&
516 test_cmp expect actual
520 test_expect_success 'expire progress off for redirected stderr' '
523 git multi-pack-index expire 2>err &&
524 test_line_count = 0 err
528 test_expect_success 'expire force progress on for stderr' '
531 git multi-pack-index --progress expire 2>err &&
532 test_file_not_empty err
536 test_expect_success 'expire with the --no-progress option' '
539 git multi-pack-index --no-progress expire 2>err &&
540 test_line_count = 0 err
544 test_expect_success 'expire removes unreferenced packs' '
547 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
551 git multi-pack-index write &&
552 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
553 git multi-pack-index expire &&
554 ls .git/objects/pack >actual &&
555 test_cmp expect actual &&
556 ls .git/objects/pack/ | grep idx >expect-idx &&
557 test-tool read-midx .git/objects | grep idx >actual-midx &&
558 test_cmp expect-idx actual-midx &&
559 git multi-pack-index verify &&
564 test_expect_success 'repack with minimum size does not alter existing packs' '
567 rm -rf .git/objects/pack &&
568 mv .git/objects/pack-backup .git/objects/pack &&
569 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
570 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
571 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
572 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
573 ls .git/objects/pack >expect &&
574 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
575 git multi-pack-index repack --batch-size=$MINSIZE &&
576 ls .git/objects/pack >actual &&
577 test_cmp expect actual
581 test_expect_success 'repack respects repack.packKeptObjects=false' '
582 test_when_finished rm -f dup/.git/objects/pack/*keep &&
585 ls .git/objects/pack/*idx >idx-list &&
586 test_line_count = 5 idx-list &&
587 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
588 test_line_count = 5 keep-list &&
589 for keep in $(cat keep-list)
591 touch $keep || return 1
593 git multi-pack-index repack --batch-size=0 &&
594 ls .git/objects/pack/*idx >idx-list &&
595 test_line_count = 5 idx-list &&
596 test-tool read-midx .git/objects | grep idx >midx-list &&
597 test_line_count = 5 midx-list &&
598 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
599 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
600 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
601 ls .git/objects/pack/*idx >idx-list &&
602 test_line_count = 5 idx-list &&
603 test-tool read-midx .git/objects | grep idx >midx-list &&
604 test_line_count = 5 midx-list
608 test_expect_success 'repack creates a new pack' '
611 ls .git/objects/pack/*idx >idx-list &&
612 test_line_count = 5 idx-list &&
613 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
614 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
615 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
616 ls .git/objects/pack/*idx >idx-list &&
617 test_line_count = 6 idx-list &&
618 test-tool read-midx .git/objects | grep idx >midx-list &&
619 test_line_count = 6 midx-list
623 test_expect_success 'expire removes repacked packs' '
626 ls -al .git/objects/pack/*pack &&
627 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
628 git multi-pack-index expire &&
629 ls -S .git/objects/pack/*pack >actual &&
630 test_cmp expect actual &&
631 test-tool read-midx .git/objects | grep idx >midx-list &&
632 test_line_count = 4 midx-list
636 test_expect_success 'expire works when adding new packs' '
639 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
643 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
647 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
651 git multi-pack-index write &&
652 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
656 git multi-pack-index write &&
657 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
660 git multi-pack-index expire &&
661 ls .git/objects/pack/ | grep idx >expect &&
662 test-tool read-midx .git/objects | grep idx >actual &&
663 test_cmp expect actual &&
664 git multi-pack-index verify
668 test_expect_success 'expire respects .keep files' '
671 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
674 git multi-pack-index write &&
675 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
677 git multi-pack-index expire &&
678 ls -S .git/objects/pack/a-pack* | grep $PACKA >a-pack-files &&
679 test_line_count = 3 a-pack-files &&
680 test-tool read-midx .git/objects | grep idx >midx-list &&
681 test_line_count = 2 midx-list
685 test_expect_success 'repack --batch-size=0 repacks everything' '
688 rm .git/objects/pack/*.keep &&
689 ls .git/objects/pack/*idx >idx-list &&
690 test_line_count = 2 idx-list &&
691 git multi-pack-index repack --batch-size=0 &&
692 ls .git/objects/pack/*idx >idx-list &&
693 test_line_count = 3 idx-list &&
694 test-tool read-midx .git/objects | grep idx >midx-list &&
695 test_line_count = 3 midx-list &&
696 git multi-pack-index expire &&
697 ls -al .git/objects/pack/*idx >idx-list &&
698 test_line_count = 1 idx-list &&
699 git multi-pack-index repack --batch-size=0 &&
700 ls -al .git/objects/pack/*idx >new-idx-list &&
701 test_cmp idx-list new-idx-list