3 test_description='multi-pack-indexes'
8 HASH_LEN=$(test_oid rawsz)
18 header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
19 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
20 num_objects: $NUM_OBJECTS
23 if test $NUM_PACKS -ge 1
25 ls $OBJECT_DIR/pack/ | grep idx | sort
27 printf "object-dir: $OBJECT_DIR\n"
29 test-tool read-midx $OBJECT_DIR >actual &&
30 test_cmp expect actual
33 test_expect_success 'setup' '
39 packnameoff sha256:940
46 test_expect_success "don't write midx with no packs" '
47 test_must_fail git multi-pack-index --object-dir=. write &&
48 test_path_is_missing pack/multi-pack-index
51 test_expect_success SHA1 'warn if a midx contains no oid' '
52 cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
53 test_must_fail git multi-pack-index verify &&
54 rm $objdir/pack/multi-pack-index
59 iii=$(printf '%03i' $i)
61 test-tool genrandom "bar" 200 &&
62 test-tool genrandom "baz $iii" 50
65 test-tool genrandom "foo"$i 100 &&
66 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
67 test-tool genrandom "foo"$(( $i + 2 )) 100
71 test-tool genrandom "$iii" 8192
73 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
76 commit_and_list_objects () {
79 test-tool genrandom 100 8192;
81 git update-index --add file_101 &&
82 tree=$(git write-tree) &&
83 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
86 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
88 git reset --hard $commit
91 test_expect_success 'create objects' '
92 test_commit initial &&
93 for i in $(test_seq 1 5)
97 commit_and_list_objects
100 test_expect_success 'write midx with one v1 pack' '
101 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
102 test_when_finished rm $objdir/pack/test-$pack.pack \
103 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
104 git multi-pack-index --object-dir=$objdir write &&
105 midx_read_expect 1 18 4 $objdir
108 midx_git_two_modes () {
109 git -c core.multiPackIndex=false $1 >expect &&
110 git -c core.multiPackIndex=true $1 >actual &&
111 if [ "$2" = "sorted" ]
113 sort <expect >expect.sorted &&
114 mv expect.sorted expect &&
115 sort <actual >actual.sorted &&
116 mv actual.sorted actual
118 test_cmp expect actual
121 compare_results_with_midx () {
123 test_expect_success "check normal git operations: $MSG" '
124 midx_git_two_modes "rev-list --objects --all" &&
125 midx_git_two_modes "log --raw" &&
126 midx_git_two_modes "count-objects --verbose" &&
127 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
128 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
132 test_expect_success 'write midx with one v2 pack' '
133 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
134 git multi-pack-index --object-dir=$objdir write &&
135 midx_read_expect 1 18 4 $objdir
138 compare_results_with_midx "one v2 pack"
140 test_expect_success 'corrupt idx not opened' '
141 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
142 mv $objdir/pack/$idx backup-$idx &&
143 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
145 # This is the minimum size for a sha-1 based .idx; this lets
146 # us pass perfunctory tests, but anything that actually opens and reads
147 # the idx file will complain.
148 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
150 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
151 test_must_be_empty err
154 test_expect_success 'add more objects' '
155 for i in $(test_seq 6 10)
159 commit_and_list_objects
162 test_expect_success 'write midx with two packs' '
163 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
164 git multi-pack-index --object-dir=$objdir write &&
165 midx_read_expect 2 34 4 $objdir
168 compare_results_with_midx "two packs"
170 test_expect_success 'write progress off for redirected stderr' '
171 git multi-pack-index --object-dir=$objdir write 2>err &&
172 test_line_count = 0 err
175 test_expect_success 'write force progress on for stderr' '
176 git multi-pack-index --object-dir=$objdir --progress write 2>err &&
177 test_file_not_empty err
180 test_expect_success 'write with the --no-progress option' '
181 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&
182 test_line_count = 0 err
185 test_expect_success 'add more packs' '
186 for j in $(test_seq 11 20)
188 generate_objects $j &&
189 commit_and_list_objects &&
190 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
194 compare_results_with_midx "mixed mode (two packs + extra)"
196 test_expect_success 'write midx with twelve packs' '
197 git multi-pack-index --object-dir=$objdir write &&
198 midx_read_expect 12 74 4 $objdir
201 compare_results_with_midx "twelve packs"
203 test_expect_success 'warn on improper hash version' '
204 git init --object-format=sha1 sha1 &&
207 git config core.multiPackIndex true &&
210 git multi-pack-index write &&
211 mv .git/objects/pack/multi-pack-index ../mpi-sha1
213 git init --object-format=sha256 sha256 &&
216 git config core.multiPackIndex true &&
219 git multi-pack-index write &&
220 mv .git/objects/pack/multi-pack-index ../mpi-sha256
224 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
226 test_i18ngrep "multi-pack-index hash version 2 does not match version 1" err
230 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
232 test_i18ngrep "multi-pack-index hash version 1 does not match version 2" err
237 test_expect_success 'verify multi-pack-index success' '
238 git multi-pack-index verify --object-dir=$objdir
241 test_expect_success 'verify progress off for redirected stderr' '
242 git multi-pack-index verify --object-dir=$objdir 2>err &&
243 test_line_count = 0 err
246 test_expect_success 'verify force progress on for stderr' '
247 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
248 test_file_not_empty err
251 test_expect_success 'verify with the --no-progress option' '
252 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
253 test_line_count = 0 err
256 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
257 corrupt_midx_and_verify() {
263 if test -z "$COMMAND"
265 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
267 FILE=$OBJDIR/pack/multi-pack-index &&
269 test_when_finished mv midx-backup $FILE &&
270 cp $FILE midx-backup &&
271 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
272 test_must_fail $COMMAND 2>test_err &&
273 grep -v "^+" test_err >err &&
274 test_i18ngrep "$GREPSTR" err
277 test_expect_success 'verify bad signature' '
278 corrupt_midx_and_verify 0 "\00" $objdir \
279 "multi-pack-index signature"
284 MIDX_BYTE_OID_VERSION=5
285 MIDX_BYTE_CHUNK_COUNT=6
287 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
288 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
290 MIDX_CHUNK_LOOKUP_WIDTH=12
291 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
292 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
293 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
294 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
295 MIDX_OID_FANOUT_WIDTH=4
296 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
297 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
298 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
299 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
301 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
302 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
304 test_expect_success 'verify bad version' '
305 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
306 "multi-pack-index version"
309 test_expect_success 'verify bad OID version' '
310 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
314 test_expect_success 'verify truncated chunk count' '
315 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
319 test_expect_success 'verify extended chunk count' '
320 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
321 "terminating multi-pack-index chunk id appears earlier than expected"
324 test_expect_success 'verify missing required chunk' '
325 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
329 test_expect_success 'verify invalid chunk offset' '
330 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
331 "invalid chunk offset (too large)"
334 test_expect_success 'verify packnames out of order' '
335 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
336 "pack names out of order"
339 test_expect_success 'verify packnames out of order' '
340 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
341 "failed to load pack"
344 test_expect_success 'verify oid fanout out of order' '
345 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
346 "oid fanout out of order"
349 test_expect_success 'verify oid lookup out of order' '
350 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
351 "oid lookup out of order"
354 test_expect_success 'verify incorrect pack-int-id' '
355 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
359 test_expect_success 'verify incorrect offset' '
360 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
361 "incorrect object offset"
364 test_expect_success 'git-fsck incorrect offset' '
365 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
366 "incorrect object offset" \
367 "git -c core.multipackindex=true fsck"
370 test_expect_success 'repack progress off for redirected stderr' '
371 git multi-pack-index --object-dir=$objdir repack 2>err &&
372 test_line_count = 0 err
375 test_expect_success 'repack force progress on for stderr' '
376 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&
377 test_file_not_empty err
380 test_expect_success 'repack with the --no-progress option' '
381 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&
382 test_line_count = 0 err
385 test_expect_success 'repack removes multi-pack-index' '
386 test_path_is_file $objdir/pack/multi-pack-index &&
387 GIT_TEST_MULTI_PACK_INDEX=0 git repack -adf &&
388 test_path_is_missing $objdir/pack/multi-pack-index
391 compare_results_with_midx "after repack"
393 test_expect_success 'multi-pack-index and pack-bitmap' '
394 git -c repack.writeBitmaps=true repack -ad &&
395 git multi-pack-index write &&
396 git rev-list --test-bitmap HEAD
399 test_expect_success 'multi-pack-index and alternates' '
400 git init --bare alt.git &&
401 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
402 echo content1 >file1 &&
403 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
404 git cat-file blob $altblob &&
408 compare_results_with_midx "with alternate (local midx)"
410 test_expect_success 'multi-pack-index in an alternate' '
411 mv .git/objects/pack/* alt.git/objects/pack &&
412 test_commit add_local_objects &&
413 git repack --local &&
414 git multi-pack-index write &&
415 midx_read_expect 1 3 4 $objdir &&
416 git reset --hard HEAD~1 &&
417 rm -f .git/objects/pack/*
420 compare_results_with_midx "with alternate (remote midx)"
422 # usage: corrupt_data <file> <pos> [<data>]
427 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
430 # Force 64-bit offsets by manipulating the idx file.
431 # This makes the IDX file _incorrect_ so be careful to clean up after!
432 test_expect_success 'force some 64-bit offsets with pack-objects' '
434 mkdir objects64/pack &&
435 for i in $(test_seq 1 11)
439 commit_and_list_objects &&
440 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
441 idx64=objects64/pack/test-64-$pack64.idx &&
443 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
444 midx64=$(git multi-pack-index --object-dir=objects64 write) &&
445 midx_read_expect 1 63 5 objects64 " large-offsets"
448 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
449 git multi-pack-index verify --object-dir=objects64
453 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
454 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
455 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
456 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
457 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
459 test_expect_success 'verify incorrect 64-bit offset' '
460 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
461 "incorrect object offset"
464 test_expect_success 'setup expire tests' '
469 test-tool genrandom "data" 4096 >large_file.txt &&
470 git update-index --add large_file.txt &&
471 for i in $(test_seq 1 20)
476 git branch B HEAD~8 &&
477 git branch C HEAD~13 &&
478 git branch D HEAD~16 &&
479 git branch E HEAD~18 &&
480 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
484 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
488 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
492 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
496 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
499 git multi-pack-index write &&
500 cp -r .git/objects/pack .git/objects/pack-backup
504 test_expect_success 'expire does not remove any packs' '
507 ls .git/objects/pack >expect &&
508 git multi-pack-index expire &&
509 ls .git/objects/pack >actual &&
510 test_cmp expect actual
514 test_expect_success 'expire progress off for redirected stderr' '
517 git multi-pack-index expire 2>err &&
518 test_line_count = 0 err
522 test_expect_success 'expire force progress on for stderr' '
525 git multi-pack-index --progress expire 2>err &&
526 test_file_not_empty err
530 test_expect_success 'expire with the --no-progress option' '
533 git multi-pack-index --no-progress expire 2>err &&
534 test_line_count = 0 err
538 test_expect_success 'expire removes unreferenced packs' '
541 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
545 git multi-pack-index write &&
546 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
547 git multi-pack-index expire &&
548 ls .git/objects/pack >actual &&
549 test_cmp expect actual &&
550 ls .git/objects/pack/ | grep idx >expect-idx &&
551 test-tool read-midx .git/objects | grep idx >actual-midx &&
552 test_cmp expect-idx actual-midx &&
553 git multi-pack-index verify &&
558 test_expect_success 'repack with minimum size does not alter existing packs' '
561 rm -rf .git/objects/pack &&
562 mv .git/objects/pack-backup .git/objects/pack &&
563 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
564 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
565 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
566 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
567 ls .git/objects/pack >expect &&
568 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
569 git multi-pack-index repack --batch-size=$MINSIZE &&
570 ls .git/objects/pack >actual &&
571 test_cmp expect actual
575 test_expect_success 'repack respects repack.packKeptObjects=false' '
576 test_when_finished rm -f dup/.git/objects/pack/*keep &&
579 ls .git/objects/pack/*idx >idx-list &&
580 test_line_count = 5 idx-list &&
581 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
582 test_line_count = 5 keep-list &&
583 for keep in $(cat keep-list)
585 touch $keep || return 1
587 git multi-pack-index repack --batch-size=0 &&
588 ls .git/objects/pack/*idx >idx-list &&
589 test_line_count = 5 idx-list &&
590 test-tool read-midx .git/objects | grep idx >midx-list &&
591 test_line_count = 5 midx-list &&
592 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
593 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
594 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
595 ls .git/objects/pack/*idx >idx-list &&
596 test_line_count = 5 idx-list &&
597 test-tool read-midx .git/objects | grep idx >midx-list &&
598 test_line_count = 5 midx-list
602 test_expect_success 'repack creates a new pack' '
605 ls .git/objects/pack/*idx >idx-list &&
606 test_line_count = 5 idx-list &&
607 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
608 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
609 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
610 ls .git/objects/pack/*idx >idx-list &&
611 test_line_count = 6 idx-list &&
612 test-tool read-midx .git/objects | grep idx >midx-list &&
613 test_line_count = 6 midx-list
617 test_expect_success 'expire removes repacked packs' '
620 ls -al .git/objects/pack/*pack &&
621 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
622 git multi-pack-index expire &&
623 ls -S .git/objects/pack/*pack >actual &&
624 test_cmp expect actual &&
625 test-tool read-midx .git/objects | grep idx >midx-list &&
626 test_line_count = 4 midx-list
630 test_expect_success 'expire works when adding new packs' '
633 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
637 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
641 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
645 git multi-pack-index write &&
646 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
650 git multi-pack-index write &&
651 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
654 git multi-pack-index expire &&
655 ls .git/objects/pack/ | grep idx >expect &&
656 test-tool read-midx .git/objects | grep idx >actual &&
657 test_cmp expect actual &&
658 git multi-pack-index verify
662 test_expect_success 'expire respects .keep files' '
665 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
668 git multi-pack-index write &&
669 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
671 git multi-pack-index expire &&
672 ls -S .git/objects/pack/a-pack* | grep $PACKA >a-pack-files &&
673 test_line_count = 3 a-pack-files &&
674 test-tool read-midx .git/objects | grep idx >midx-list &&
675 test_line_count = 2 midx-list
679 test_expect_success 'repack --batch-size=0 repacks everything' '
683 rm .git/objects/pack/*.keep &&
684 ls .git/objects/pack/*idx >idx-list &&
685 test_line_count = 2 idx-list &&
686 git multi-pack-index repack --batch-size=0 &&
687 ls .git/objects/pack/*idx >idx-list &&
688 test_line_count = 3 idx-list &&
689 test-tool read-midx .git/objects | grep idx >midx-list &&
690 test_line_count = 3 midx-list &&
691 git multi-pack-index expire &&
692 ls -al .git/objects/pack/*idx >idx-list &&
693 test_line_count = 1 idx-list &&
694 git multi-pack-index repack --batch-size=0 &&
695 ls -al .git/objects/pack/*idx >new-idx-list &&
696 test_cmp idx-list new-idx-list
700 test_expect_success 'repack --batch-size=<large> repacks everything' '
703 rm .git/objects/pack/*.keep &&
704 ls .git/objects/pack/*idx >idx-list &&
705 test_line_count = 2 idx-list &&
706 git multi-pack-index repack --batch-size=2000000 &&
707 ls .git/objects/pack/*idx >idx-list &&
708 test_line_count = 3 idx-list &&
709 test-tool read-midx .git/objects | grep idx >midx-list &&
710 test_line_count = 3 midx-list &&
711 git multi-pack-index expire &&
712 ls -al .git/objects/pack/*idx >idx-list &&
713 test_line_count = 1 idx-list