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' '
37 packnameoff sha256:940
44 test_expect_success "don't write midx with no packs" '
45 test_must_fail git multi-pack-index --object-dir=. write &&
46 test_path_is_missing pack/multi-pack-index
49 test_expect_success "Warn if a midx contains no oid" '
50 cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
51 test_must_fail git multi-pack-index verify &&
52 rm $objdir/pack/multi-pack-index
57 iii=$(printf '%03i' $i)
59 test-tool genrandom "bar" 200 &&
60 test-tool genrandom "baz $iii" 50
63 test-tool genrandom "foo"$i 100 &&
64 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
65 test-tool genrandom "foo"$(( $i + 2 )) 100
69 test-tool genrandom "$iii" 8192
71 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
74 commit_and_list_objects () {
77 test-tool genrandom 100 8192;
79 git update-index --add file_101 &&
80 tree=$(git write-tree) &&
81 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
84 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
86 git reset --hard $commit
89 test_expect_success 'create objects' '
90 test_commit initial &&
91 for i in $(test_seq 1 5)
95 commit_and_list_objects
98 test_expect_success 'write midx with one v1 pack' '
99 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
100 test_when_finished rm $objdir/pack/test-$pack.pack \
101 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
102 git multi-pack-index --object-dir=$objdir write &&
103 midx_read_expect 1 18 4 $objdir
106 midx_git_two_modes () {
107 git -c core.multiPackIndex=false $1 >expect &&
108 git -c core.multiPackIndex=true $1 >actual &&
109 if [ "$2" = "sorted" ]
111 sort <expect >expect.sorted &&
112 mv expect.sorted expect &&
113 sort <actual >actual.sorted &&
114 mv actual.sorted actual
116 test_cmp expect actual
119 compare_results_with_midx () {
121 test_expect_success "check normal git operations: $MSG" '
122 midx_git_two_modes "rev-list --objects --all" &&
123 midx_git_two_modes "log --raw" &&
124 midx_git_two_modes "count-objects --verbose" &&
125 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
126 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
130 test_expect_success 'write midx with one v2 pack' '
131 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
132 git multi-pack-index --object-dir=$objdir write &&
133 midx_read_expect 1 18 4 $objdir
136 compare_results_with_midx "one v2 pack"
138 test_expect_success 'corrupt idx not opened' '
139 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
140 mv $objdir/pack/$idx backup-$idx &&
141 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
143 # This is the minimum size for a sha-1 based .idx; this lets
144 # us pass perfunctory tests, but anything that actually opens and reads
145 # the idx file will complain.
146 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
148 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
149 test_must_be_empty err
152 test_expect_success 'add more objects' '
153 for i in $(test_seq 6 10)
157 commit_and_list_objects
160 test_expect_success 'write midx with two packs' '
161 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
162 git multi-pack-index --object-dir=$objdir write &&
163 midx_read_expect 2 34 4 $objdir
166 compare_results_with_midx "two packs"
168 test_expect_success 'write progress off for redirected stderr' '
169 git multi-pack-index --object-dir=$objdir write 2>err &&
170 test_line_count = 0 err
173 test_expect_success 'write force progress on for stderr' '
174 git multi-pack-index --object-dir=$objdir --progress write 2>err &&
175 test_file_not_empty err
178 test_expect_success 'write with the --no-progress option' '
179 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&
180 test_line_count = 0 err
183 test_expect_success 'add more packs' '
184 for j in $(test_seq 11 20)
186 generate_objects $j &&
187 commit_and_list_objects &&
188 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
192 compare_results_with_midx "mixed mode (two packs + extra)"
194 test_expect_success 'write midx with twelve packs' '
195 git multi-pack-index --object-dir=$objdir write &&
196 midx_read_expect 12 74 4 $objdir
199 compare_results_with_midx "twelve packs"
201 test_expect_success 'verify multi-pack-index success' '
202 git multi-pack-index verify --object-dir=$objdir
205 test_expect_success 'verify progress off for redirected stderr' '
206 git multi-pack-index verify --object-dir=$objdir 2>err &&
207 test_line_count = 0 err
210 test_expect_success 'verify force progress on for stderr' '
211 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
212 test_file_not_empty err
215 test_expect_success 'verify with the --no-progress option' '
216 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
217 test_line_count = 0 err
220 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
221 corrupt_midx_and_verify() {
227 if test -z "$COMMAND"
229 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
231 FILE=$OBJDIR/pack/multi-pack-index &&
233 test_when_finished mv midx-backup $FILE &&
234 cp $FILE midx-backup &&
235 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
236 test_must_fail $COMMAND 2>test_err &&
237 grep -v "^+" test_err >err &&
238 test_i18ngrep "$GREPSTR" err
241 test_expect_success 'verify bad signature' '
242 corrupt_midx_and_verify 0 "\00" $objdir \
243 "multi-pack-index signature"
246 HASH_LEN=$(test_oid rawsz)
249 MIDX_BYTE_OID_VERSION=5
250 MIDX_BYTE_CHUNK_COUNT=6
252 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
253 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
255 MIDX_CHUNK_LOOKUP_WIDTH=12
256 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
257 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
258 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
259 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
260 MIDX_OID_FANOUT_WIDTH=4
261 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
262 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
263 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
264 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
266 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
267 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
269 test_expect_success 'verify bad version' '
270 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
271 "multi-pack-index version"
274 test_expect_success 'verify bad OID version' '
275 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\02" $objdir \
279 test_expect_success 'verify truncated chunk count' '
280 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
284 test_expect_success 'verify extended chunk count' '
285 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
286 "terminating multi-pack-index chunk id appears earlier than expected"
289 test_expect_success 'verify missing required chunk' '
290 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
294 test_expect_success 'verify invalid chunk offset' '
295 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
296 "invalid chunk offset (too large)"
299 test_expect_success 'verify packnames out of order' '
300 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
301 "pack names out of order"
304 test_expect_success 'verify packnames out of order' '
305 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
306 "failed to load pack"
309 test_expect_success 'verify oid fanout out of order' '
310 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
311 "oid fanout out of order"
314 test_expect_success 'verify oid lookup out of order' '
315 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
316 "oid lookup out of order"
319 test_expect_success 'verify incorrect pack-int-id' '
320 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
324 test_expect_success 'verify incorrect offset' '
325 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
326 "incorrect object offset"
329 test_expect_success 'git-fsck incorrect offset' '
330 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
331 "incorrect object offset" \
332 "git -c core.multipackindex=true fsck"
335 test_expect_success 'repack progress off for redirected stderr' '
336 git multi-pack-index --object-dir=$objdir repack 2>err &&
337 test_line_count = 0 err
340 test_expect_success 'repack force progress on for stderr' '
341 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&
342 test_file_not_empty err
345 test_expect_success 'repack with the --no-progress option' '
346 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&
347 test_line_count = 0 err
350 test_expect_success 'repack removes multi-pack-index' '
351 test_path_is_file $objdir/pack/multi-pack-index &&
352 GIT_TEST_MULTI_PACK_INDEX=0 git repack -adf &&
353 test_path_is_missing $objdir/pack/multi-pack-index
356 compare_results_with_midx "after repack"
358 test_expect_success 'multi-pack-index and pack-bitmap' '
359 git -c repack.writeBitmaps=true repack -ad &&
360 git multi-pack-index write &&
361 git rev-list --test-bitmap HEAD
364 test_expect_success 'multi-pack-index and alternates' '
365 git init --bare alt.git &&
366 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
367 echo content1 >file1 &&
368 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
369 git cat-file blob $altblob &&
373 compare_results_with_midx "with alternate (local midx)"
375 test_expect_success 'multi-pack-index in an alternate' '
376 mv .git/objects/pack/* alt.git/objects/pack &&
377 test_commit add_local_objects &&
378 git repack --local &&
379 git multi-pack-index write &&
380 midx_read_expect 1 3 4 $objdir &&
381 git reset --hard HEAD~1 &&
382 rm -f .git/objects/pack/*
385 compare_results_with_midx "with alternate (remote midx)"
387 # usage: corrupt_data <file> <pos> [<data>]
392 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
395 # Force 64-bit offsets by manipulating the idx file.
396 # This makes the IDX file _incorrect_ so be careful to clean up after!
397 test_expect_success 'force some 64-bit offsets with pack-objects' '
399 mkdir objects64/pack &&
400 for i in $(test_seq 1 11)
404 commit_and_list_objects &&
405 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
406 idx64=objects64/pack/test-64-$pack64.idx &&
408 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
409 midx64=$(git multi-pack-index --object-dir=objects64 write) &&
410 midx_read_expect 1 63 5 objects64 " large-offsets"
413 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
414 git multi-pack-index verify --object-dir=objects64
418 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
419 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
420 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
421 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
422 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
424 test_expect_success 'verify incorrect 64-bit offset' '
425 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
426 "incorrect object offset"
429 test_expect_success 'setup expire tests' '
434 test-tool genrandom "data" 4096 >large_file.txt &&
435 git update-index --add large_file.txt &&
436 for i in $(test_seq 1 20)
441 git branch B HEAD~8 &&
442 git branch C HEAD~13 &&
443 git branch D HEAD~16 &&
444 git branch E HEAD~18 &&
445 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
449 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
453 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
457 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
461 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
464 git multi-pack-index write &&
465 cp -r .git/objects/pack .git/objects/pack-backup
469 test_expect_success 'expire does not remove any packs' '
472 ls .git/objects/pack >expect &&
473 git multi-pack-index expire &&
474 ls .git/objects/pack >actual &&
475 test_cmp expect actual
479 test_expect_success 'expire progress off for redirected stderr' '
482 git multi-pack-index expire 2>err &&
483 test_line_count = 0 err
487 test_expect_success 'expire force progress on for stderr' '
490 git multi-pack-index --progress expire 2>err &&
491 test_file_not_empty err
495 test_expect_success 'expire with the --no-progress option' '
498 git multi-pack-index --no-progress expire 2>err &&
499 test_line_count = 0 err
503 test_expect_success 'expire removes unreferenced packs' '
506 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
510 git multi-pack-index write &&
511 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
512 git multi-pack-index expire &&
513 ls .git/objects/pack >actual &&
514 test_cmp expect actual &&
515 ls .git/objects/pack/ | grep idx >expect-idx &&
516 test-tool read-midx .git/objects | grep idx >actual-midx &&
517 test_cmp expect-idx actual-midx &&
518 git multi-pack-index verify &&
523 test_expect_success 'repack with minimum size does not alter existing packs' '
526 rm -rf .git/objects/pack &&
527 mv .git/objects/pack-backup .git/objects/pack &&
528 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
529 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
530 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
531 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
532 ls .git/objects/pack >expect &&
533 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
534 git multi-pack-index repack --batch-size=$MINSIZE &&
535 ls .git/objects/pack >actual &&
536 test_cmp expect actual
540 test_expect_success 'repack respects repack.packKeptObjects=false' '
541 test_when_finished rm -f dup/.git/objects/pack/*keep &&
544 ls .git/objects/pack/*idx >idx-list &&
545 test_line_count = 5 idx-list &&
546 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
547 test_line_count = 5 keep-list &&
548 for keep in $(cat keep-list)
550 touch $keep || return 1
552 git multi-pack-index repack --batch-size=0 &&
553 ls .git/objects/pack/*idx >idx-list &&
554 test_line_count = 5 idx-list &&
555 test-tool read-midx .git/objects | grep idx >midx-list &&
556 test_line_count = 5 midx-list &&
557 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
558 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
559 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
560 ls .git/objects/pack/*idx >idx-list &&
561 test_line_count = 5 idx-list &&
562 test-tool read-midx .git/objects | grep idx >midx-list &&
563 test_line_count = 5 midx-list
567 test_expect_success 'repack creates a new pack' '
570 ls .git/objects/pack/*idx >idx-list &&
571 test_line_count = 5 idx-list &&
572 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
573 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
574 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
575 ls .git/objects/pack/*idx >idx-list &&
576 test_line_count = 6 idx-list &&
577 test-tool read-midx .git/objects | grep idx >midx-list &&
578 test_line_count = 6 midx-list
582 test_expect_success 'expire removes repacked packs' '
585 ls -al .git/objects/pack/*pack &&
586 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
587 git multi-pack-index expire &&
588 ls -S .git/objects/pack/*pack >actual &&
589 test_cmp expect actual &&
590 test-tool read-midx .git/objects | grep idx >midx-list &&
591 test_line_count = 4 midx-list
595 test_expect_success 'expire works when adding new packs' '
598 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
602 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
606 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
610 git multi-pack-index write &&
611 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
615 git multi-pack-index write &&
616 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
619 git multi-pack-index expire &&
620 ls .git/objects/pack/ | grep idx >expect &&
621 test-tool read-midx .git/objects | grep idx >actual &&
622 test_cmp expect actual &&
623 git multi-pack-index verify
627 test_expect_success 'expire respects .keep files' '
630 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
633 git multi-pack-index write &&
634 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
636 git multi-pack-index expire &&
637 ls -S .git/objects/pack/a-pack* | grep $PACKA >a-pack-files &&
638 test_line_count = 3 a-pack-files &&
639 test-tool read-midx .git/objects | grep idx >midx-list &&
640 test_line_count = 2 midx-list
644 test_expect_success 'repack --batch-size=0 repacks everything' '
647 rm .git/objects/pack/*.keep &&
648 ls .git/objects/pack/*idx >idx-list &&
649 test_line_count = 2 idx-list &&
650 git multi-pack-index repack --batch-size=0 &&
651 ls .git/objects/pack/*idx >idx-list &&
652 test_line_count = 3 idx-list &&
653 test-tool read-midx .git/objects | grep idx >midx-list &&
654 test_line_count = 3 midx-list &&
655 git multi-pack-index expire &&
656 ls -al .git/objects/pack/*idx >idx-list &&
657 test_line_count = 1 idx-list &&
658 git multi-pack-index repack --batch-size=0 &&
659 ls -al .git/objects/pack/*idx >new-idx-list &&
660 test_cmp idx-list new-idx-list