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' '
352 test_path_is_file $objdir/pack/multi-pack-index &&
353 GIT_TEST_MULTI_PACK_INDEX=0 git repack -adf &&
354 test_path_is_missing $objdir/pack/multi-pack-index
357 compare_results_with_midx "after repack"
359 test_expect_success 'multi-pack-index and pack-bitmap' '
360 git -c repack.writeBitmaps=true repack -ad &&
361 git multi-pack-index write &&
362 git rev-list --test-bitmap HEAD
365 test_expect_success 'multi-pack-index and alternates' '
366 git init --bare alt.git &&
367 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
368 echo content1 >file1 &&
369 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
370 git cat-file blob $altblob &&
374 compare_results_with_midx "with alternate (local midx)"
376 test_expect_success 'multi-pack-index in an alternate' '
377 mv .git/objects/pack/* alt.git/objects/pack &&
378 test_commit add_local_objects &&
379 git repack --local &&
380 git multi-pack-index write &&
381 midx_read_expect 1 3 4 $objdir &&
382 git reset --hard HEAD~1 &&
383 rm -f .git/objects/pack/*
386 compare_results_with_midx "with alternate (remote midx)"
388 # usage: corrupt_data <file> <pos> [<data>]
393 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
396 # Force 64-bit offsets by manipulating the idx file.
397 # This makes the IDX file _incorrect_ so be careful to clean up after!
398 test_expect_success 'force some 64-bit offsets with pack-objects' '
400 mkdir objects64/pack &&
401 for i in $(test_seq 1 11)
405 commit_and_list_objects &&
406 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
407 idx64=objects64/pack/test-64-$pack64.idx &&
409 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
410 midx64=$(git multi-pack-index --object-dir=objects64 write) &&
411 midx_read_expect 1 63 5 objects64 " large-offsets"
414 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
415 git multi-pack-index verify --object-dir=objects64
419 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
420 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
421 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
422 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
423 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
425 test_expect_success 'verify incorrect 64-bit offset' '
426 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
427 "incorrect object offset"
430 test_expect_success 'setup expire tests' '
435 test-tool genrandom "data" 4096 >large_file.txt &&
436 git update-index --add large_file.txt &&
437 for i in $(test_seq 1 20)
442 git branch B HEAD~8 &&
443 git branch C HEAD~13 &&
444 git branch D HEAD~16 &&
445 git branch E HEAD~18 &&
446 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
450 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
454 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
458 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
462 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
465 git multi-pack-index write &&
466 cp -r .git/objects/pack .git/objects/pack-backup
470 test_expect_success 'expire does not remove any packs' '
473 ls .git/objects/pack >expect &&
474 git multi-pack-index expire &&
475 ls .git/objects/pack >actual &&
476 test_cmp expect actual
480 test_expect_success 'expire progress off for redirected stderr' '
483 git multi-pack-index expire 2>err &&
484 test_line_count = 0 err
488 test_expect_success 'expire force progress on for stderr' '
491 git multi-pack-index --progress expire 2>err &&
492 test_file_not_empty err
496 test_expect_success 'expire with the --no-progress option' '
499 git multi-pack-index --no-progress expire 2>err &&
500 test_line_count = 0 err
504 test_expect_success 'expire removes unreferenced packs' '
507 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
511 git multi-pack-index write &&
512 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
513 git multi-pack-index expire &&
514 ls .git/objects/pack >actual &&
515 test_cmp expect actual &&
516 ls .git/objects/pack/ | grep idx >expect-idx &&
517 test-tool read-midx .git/objects | grep idx >actual-midx &&
518 test_cmp expect-idx actual-midx &&
519 git multi-pack-index verify &&
524 test_expect_success 'repack with minimum size does not alter existing packs' '
527 rm -rf .git/objects/pack &&
528 mv .git/objects/pack-backup .git/objects/pack &&
529 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
530 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
531 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
532 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
533 ls .git/objects/pack >expect &&
534 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
535 git multi-pack-index repack --batch-size=$MINSIZE &&
536 ls .git/objects/pack >actual &&
537 test_cmp expect actual
541 test_expect_success 'repack creates a new pack' '
544 ls .git/objects/pack/*idx >idx-list &&
545 test_line_count = 5 idx-list &&
546 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
547 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
548 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
549 ls .git/objects/pack/*idx >idx-list &&
550 test_line_count = 6 idx-list &&
551 test-tool read-midx .git/objects | grep idx >midx-list &&
552 test_line_count = 6 midx-list
556 test_expect_success 'expire removes repacked packs' '
559 ls -al .git/objects/pack/*pack &&
560 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
561 git multi-pack-index expire &&
562 ls -S .git/objects/pack/*pack >actual &&
563 test_cmp expect actual &&
564 test-tool read-midx .git/objects | grep idx >midx-list &&
565 test_line_count = 4 midx-list
569 test_expect_success 'expire works when adding new packs' '
572 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
576 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
580 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
584 git multi-pack-index write &&
585 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
589 git multi-pack-index write &&
590 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
593 git multi-pack-index expire &&
594 ls .git/objects/pack/ | grep idx >expect &&
595 test-tool read-midx .git/objects | grep idx >actual &&
596 test_cmp expect actual &&
597 git multi-pack-index verify
601 test_expect_success 'expire respects .keep files' '
604 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
607 git multi-pack-index write &&
608 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
610 git multi-pack-index expire &&
611 ls -S .git/objects/pack/a-pack* | grep $PACKA >a-pack-files &&
612 test_line_count = 3 a-pack-files &&
613 test-tool read-midx .git/objects | grep idx >midx-list &&
614 test_line_count = 2 midx-list
618 test_expect_success 'repack --batch-size=0 repacks everything' '
621 rm .git/objects/pack/*.keep &&
622 ls .git/objects/pack/*idx >idx-list &&
623 test_line_count = 2 idx-list &&
624 git multi-pack-index repack --batch-size=0 &&
625 ls .git/objects/pack/*idx >idx-list &&
626 test_line_count = 3 idx-list &&
627 test-tool read-midx .git/objects | grep idx >midx-list &&
628 test_line_count = 3 midx-list &&
629 git multi-pack-index expire &&
630 ls -al .git/objects/pack/*idx >idx-list &&
631 test_line_count = 1 idx-list &&
632 git multi-pack-index repack --batch-size=0 &&
633 ls -al .git/objects/pack/*idx >new-idx-list &&
634 test_cmp idx-list new-idx-list