A few more topics before -rc1
[alt-git.git] / t / t5319-multi-pack-index.sh
blob1bcc02004d7d1acbce8306bdeb5439f4da689ccb
1 #!/bin/sh
3 test_description='multi-pack-indexes'
4 . ./test-lib.sh
6 GIT_TEST_MULTI_PACK_INDEX=0
7 objdir=.git/objects
9 HASH_LEN=$(test_oid rawsz)
11 midx_read_expect () {
12 NUM_PACKS=$1
13 NUM_OBJECTS=$2
14 NUM_CHUNKS=$3
15 OBJECT_DIR=$4
16 EXTRA_CHUNKS="$5"
18 cat <<-EOF &&
19 header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
20 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
21 num_objects: $NUM_OBJECTS
22 packs:
23 EOF
24 if test $NUM_PACKS -ge 1
25 then
26 ls $OBJECT_DIR/pack/ | grep idx | sort
27 fi &&
28 printf "object-dir: $OBJECT_DIR\n"
29 } >expect &&
30 test-tool read-midx $OBJECT_DIR >actual &&
31 test_cmp expect actual
34 test_expect_success 'setup' '
35 test_oid_cache <<-EOF
36 idxoff sha1:2999
37 idxoff sha256:3739
39 packnameoff sha1:652
40 packnameoff sha256:940
42 fanoutoff sha1:1
43 fanoutoff sha256:3
44 EOF
47 test_expect_success "don't write midx with no packs" '
48 test_must_fail git multi-pack-index --object-dir=. write &&
49 test_path_is_missing pack/multi-pack-index
52 test_expect_success SHA1 'warn if a midx contains no oid' '
53 cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
54 test_must_fail git multi-pack-index verify &&
55 rm $objdir/pack/multi-pack-index
58 generate_objects () {
59 i=$1
60 iii=$(printf '%03i' $i)
62 test-tool genrandom "bar" 200 &&
63 test-tool genrandom "baz $iii" 50
64 } >wide_delta_$iii &&
66 test-tool genrandom "foo"$i 100 &&
67 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
68 test-tool genrandom "foo"$(( $i + 2 )) 100
69 } >deep_delta_$iii &&
71 echo $iii &&
72 test-tool genrandom "$iii" 8192
73 } >file_$iii &&
74 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
77 commit_and_list_objects () {
79 echo 101 &&
80 test-tool genrandom 100 8192;
81 } >file_101 &&
82 git update-index --add file_101 &&
83 tree=$(git write-tree) &&
84 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
86 echo $tree &&
87 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
88 } >obj-list &&
89 git reset --hard $commit
92 test_expect_success 'create objects' '
93 test_commit initial &&
94 for i in $(test_seq 1 5)
96 generate_objects $i || return 1
97 done &&
98 commit_and_list_objects
101 test_expect_success 'write midx with one v1 pack' '
102 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
103 test_when_finished rm $objdir/pack/test-$pack.pack \
104 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
105 git multi-pack-index --object-dir=$objdir write &&
106 midx_read_expect 1 18 4 $objdir
109 midx_git_two_modes () {
110 git -c core.multiPackIndex=false $1 >expect &&
111 git -c core.multiPackIndex=true $1 >actual &&
112 if [ "$2" = "sorted" ]
113 then
114 sort <expect >expect.sorted &&
115 mv expect.sorted expect &&
116 sort <actual >actual.sorted &&
117 mv actual.sorted actual
118 fi &&
119 test_cmp expect actual
122 compare_results_with_midx () {
123 MSG=$1
124 test_expect_success "check normal git operations: $MSG" '
125 midx_git_two_modes "rev-list --objects --all" &&
126 midx_git_two_modes "log --raw" &&
127 midx_git_two_modes "count-objects --verbose" &&
128 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
129 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
133 test_expect_success 'write midx with one v2 pack' '
134 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
135 git multi-pack-index --object-dir=$objdir write &&
136 midx_read_expect 1 18 4 $objdir
139 compare_results_with_midx "one v2 pack"
141 test_expect_success 'corrupt idx reports errors' '
142 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
143 mv $objdir/pack/$idx backup-$idx &&
144 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
146 # This is the minimum size for a sha-1 based .idx; this lets
147 # us pass perfunctory tests, but anything that actually opens and reads
148 # the idx file will complain.
149 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
151 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
152 grep "index unavailable" err
155 test_expect_success 'add more objects' '
156 for i in $(test_seq 6 10)
158 generate_objects $i || return 1
159 done &&
160 commit_and_list_objects
163 test_expect_success 'write midx with two packs' '
164 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
165 git multi-pack-index --object-dir=$objdir write &&
166 midx_read_expect 2 34 4 $objdir
169 compare_results_with_midx "two packs"
171 test_expect_success 'write midx with --stdin-packs' '
172 rm -fr $objdir/pack/multi-pack-index &&
174 idx="$(find $objdir/pack -name "test-2-*.idx")" &&
175 basename "$idx" >in &&
177 git multi-pack-index write --stdin-packs <in &&
179 test-tool read-midx $objdir | grep "\.idx$" >packs &&
181 test_cmp packs in
184 compare_results_with_midx "mixed mode (one pack + extra)"
186 test_expect_success 'write with no objects and preferred pack' '
187 test_when_finished "rm -rf empty" &&
188 git init empty &&
189 test_must_fail git -C empty multi-pack-index write \
190 --stdin-packs --preferred-pack=does-not-exist </dev/null 2>err &&
191 cat >expect <<-EOF &&
192 warning: unknown preferred pack: ${SQ}does-not-exist${SQ}
193 error: no pack files to index.
195 test_cmp expect err
198 test_expect_success 'write progress off for redirected stderr' '
199 git multi-pack-index --object-dir=$objdir write 2>err &&
200 test_line_count = 0 err
203 test_expect_success 'write force progress on for stderr' '
204 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --progress 2>err &&
205 test_file_not_empty err
208 test_expect_success 'write with the --no-progress option' '
209 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --no-progress 2>err &&
210 test_line_count = 0 err
213 test_expect_success 'add more packs' '
214 for j in $(test_seq 11 20)
216 generate_objects $j &&
217 commit_and_list_objects &&
218 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list || return 1
219 done
222 compare_results_with_midx "mixed mode (two packs + extra)"
224 test_expect_success 'write midx with twelve packs' '
225 git multi-pack-index --object-dir=$objdir write &&
226 midx_read_expect 12 74 4 $objdir
229 compare_results_with_midx "twelve packs"
231 test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
232 git init repo &&
233 git clone -s repo alternate &&
235 test_when_finished "rm -rf repo alternate" &&
238 cd repo &&
239 test_commit base &&
240 git repack -d
241 ) &&
243 ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
244 theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
245 touch "$ours" "$theirs" &&
248 cd alternate &&
249 git multi-pack-index --object-dir ../repo/.git/objects write
250 ) &&
252 # writing a midx in "repo" should not remove the .rev file in the
253 # alternate
254 test_path_is_file repo/.git/objects/pack/multi-pack-index &&
255 test_path_is_file $ours &&
256 test_path_is_missing $theirs
259 test_expect_success 'warn on improper hash version' '
260 git init --object-format=sha1 sha1 &&
262 cd sha1 &&
263 git config core.multiPackIndex true &&
264 test_commit 1 &&
265 git repack -a &&
266 git multi-pack-index write &&
267 mv .git/objects/pack/multi-pack-index ../mpi-sha1
268 ) &&
269 git init --object-format=sha256 sha256 &&
271 cd sha256 &&
272 git config core.multiPackIndex true &&
273 test_commit 1 &&
274 git repack -a &&
275 git multi-pack-index write &&
276 mv .git/objects/pack/multi-pack-index ../mpi-sha256
277 ) &&
279 cd sha1 &&
280 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
281 git log -1 2>err &&
282 test_i18ngrep "multi-pack-index hash version 2 does not match version 1" err
283 ) &&
285 cd sha256 &&
286 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
287 git log -1 2>err &&
288 test_i18ngrep "multi-pack-index hash version 1 does not match version 2" err
292 test_expect_success 'midx picks objects from preferred pack' '
293 test_when_finished rm -rf preferred.git &&
294 git init --bare preferred.git &&
296 cd preferred.git &&
298 a=$(echo "a" | git hash-object -w --stdin) &&
299 b=$(echo "b" | git hash-object -w --stdin) &&
300 c=$(echo "c" | git hash-object -w --stdin) &&
302 # Set up two packs, duplicating the object "B" at different
303 # offsets.
305 # Note that the "BC" pack (the one we choose as preferred) sorts
306 # lexically after the "AB" pack, meaning that omitting the
307 # --preferred-pack argument would cause this test to fail (since
308 # the MIDX code would select the copy of "b" in the "AB" pack).
309 git pack-objects objects/pack/test-AB <<-EOF &&
313 bc=$(git pack-objects objects/pack/test-BC <<-EOF
317 ) &&
319 git multi-pack-index --object-dir=objects \
320 write --preferred-pack=test-BC-$bc.idx 2>err &&
321 test_must_be_empty err &&
323 test-tool read-midx --show-objects objects >out &&
325 ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
326 cut -d" " -f1) &&
327 printf "%s %s\tobjects/pack/test-BC-%s.pack\n" \
328 "$b" "$ofs" "$bc" >expect &&
329 grep ^$b out >actual &&
331 test_cmp expect actual
335 test_expect_success 'preferred packs must be non-empty' '
336 test_when_finished rm -rf preferred.git &&
337 git init preferred.git &&
339 cd preferred.git &&
341 test_commit base &&
342 git repack -ad &&
344 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
346 test_must_fail git multi-pack-index write \
347 --preferred-pack=pack-$empty.pack 2>err &&
348 grep "with no objects" err
352 test_expect_success 'verify multi-pack-index success' '
353 git multi-pack-index verify --object-dir=$objdir
356 test_expect_success 'verify progress off for redirected stderr' '
357 git multi-pack-index verify --object-dir=$objdir 2>err &&
358 test_line_count = 0 err
361 test_expect_success 'verify force progress on for stderr' '
362 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
363 test_file_not_empty err
366 test_expect_success 'verify with the --no-progress option' '
367 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
368 test_line_count = 0 err
371 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
372 corrupt_midx_and_verify() {
373 POS=$1 &&
374 DATA="${2:-\0}" &&
375 OBJDIR=$3 &&
376 GREPSTR="$4" &&
377 COMMAND="$5" &&
378 if test -z "$COMMAND"
379 then
380 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
381 fi &&
382 FILE=$OBJDIR/pack/multi-pack-index &&
383 chmod a+w $FILE &&
384 test_when_finished mv midx-backup $FILE &&
385 cp $FILE midx-backup &&
386 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
387 test_must_fail $COMMAND 2>test_err &&
388 grep -v "^+" test_err >err &&
389 test_i18ngrep "$GREPSTR" err
392 test_expect_success 'verify bad signature' '
393 corrupt_midx_and_verify 0 "\00" $objdir \
394 "multi-pack-index signature"
397 NUM_OBJECTS=74
398 MIDX_BYTE_VERSION=4
399 MIDX_BYTE_OID_VERSION=5
400 MIDX_BYTE_CHUNK_COUNT=6
401 MIDX_HEADER_SIZE=12
402 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
403 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
404 MIDX_NUM_CHUNKS=5
405 MIDX_CHUNK_LOOKUP_WIDTH=12
406 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
407 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
408 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
409 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
410 MIDX_OID_FANOUT_WIDTH=4
411 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
412 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
413 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
414 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
415 MIDX_OFFSET_WIDTH=8
416 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
417 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
419 test_expect_success 'verify bad version' '
420 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
421 "multi-pack-index version"
424 test_expect_success 'verify bad OID version' '
425 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
426 "hash version"
429 test_expect_success 'verify truncated chunk count' '
430 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
431 "final chunk has non-zero id"
434 test_expect_success 'verify extended chunk count' '
435 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
436 "terminating chunk id appears earlier than expected"
439 test_expect_success 'verify missing required chunk' '
440 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
441 "missing required"
444 test_expect_success 'verify invalid chunk offset' '
445 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
446 "improper chunk offset(s)"
449 test_expect_success 'verify packnames out of order' '
450 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
451 "pack names out of order"
454 test_expect_success 'verify packnames out of order' '
455 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
456 "failed to load pack"
459 test_expect_success 'verify oid fanout out of order' '
460 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
461 "oid fanout out of order"
464 test_expect_success 'verify oid lookup out of order' '
465 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
466 "oid lookup out of order"
469 test_expect_success 'verify incorrect pack-int-id' '
470 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
471 "bad pack-int-id"
474 test_expect_success 'verify incorrect offset' '
475 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
476 "incorrect object offset"
479 test_expect_success 'git-fsck incorrect offset' '
480 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
481 "incorrect object offset" \
482 "git -c core.multiPackIndex=true fsck" &&
483 test_unconfig core.multiPackIndex &&
484 test_must_fail git fsck &&
485 git -c core.multiPackIndex=false fsck
488 test_expect_success 'git fsck shows MIDX output with --progress' '
489 git fsck --progress 2>err &&
490 grep "Verifying OID order in multi-pack-index" err &&
491 grep "Verifying object offsets" err
494 test_expect_success 'git fsck suppresses MIDX output with --no-progress' '
495 git fsck --no-progress 2>err &&
496 ! grep "Verifying OID order in multi-pack-index" err &&
497 ! grep "Verifying object offsets" err
500 test_expect_success 'corrupt MIDX is not reused' '
501 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
502 "incorrect object offset" &&
503 git multi-pack-index write 2>err &&
504 test_i18ngrep checksum.mismatch err &&
505 git multi-pack-index verify
508 test_expect_success 'verify incorrect checksum' '
509 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
510 corrupt_midx_and_verify $pos \
511 "\377\377\377\377\377\377\377\377\377\377" \
512 $objdir "incorrect checksum"
515 test_expect_success 'repack progress off for redirected stderr' '
516 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
517 test_line_count = 0 err
520 test_expect_success 'repack force progress on for stderr' '
521 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
522 test_file_not_empty err
525 test_expect_success 'repack with the --no-progress option' '
526 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
527 test_line_count = 0 err
530 test_expect_success 'repack removes multi-pack-index when deleting packs' '
531 test_path_is_file $objdir/pack/multi-pack-index &&
532 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
533 # multi-pack-index after repacking, but set "core.multiPackIndex" to
534 # true so that "git repack" can read the existing MIDX.
535 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
536 test_path_is_missing $objdir/pack/multi-pack-index
539 test_expect_success 'repack preserves multi-pack-index when creating packs' '
540 git init preserve &&
541 test_when_finished "rm -fr preserve" &&
543 cd preserve &&
544 packdir=.git/objects/pack &&
545 midx=$packdir/multi-pack-index &&
547 test_commit 1 &&
548 pack1=$(git pack-objects --all $packdir/pack) &&
549 touch $packdir/pack-$pack1.keep &&
550 test_commit 2 &&
551 pack2=$(git pack-objects --revs $packdir/pack) &&
552 touch $packdir/pack-$pack2.keep &&
554 git multi-pack-index write &&
555 cp $midx $midx.bak &&
557 cat >pack-input <<-EOF &&
558 HEAD
559 ^HEAD~1
561 test_commit 3 &&
562 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
563 test_commit 4 &&
564 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
566 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
567 ls -la $packdir &&
568 test_path_is_file $packdir/pack-$pack1.pack &&
569 test_path_is_file $packdir/pack-$pack2.pack &&
570 test_path_is_missing $packdir/pack-$pack3.pack &&
571 test_path_is_missing $packdir/pack-$pack4.pack &&
572 test_cmp_bin $midx.bak $midx
576 compare_results_with_midx "after repack"
578 test_expect_success 'multi-pack-index and pack-bitmap' '
579 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
580 git -c repack.writeBitmaps=true repack -ad &&
581 git multi-pack-index write &&
582 git rev-list --test-bitmap HEAD
585 test_expect_success 'multi-pack-index and alternates' '
586 git init --bare alt.git &&
587 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
588 echo content1 >file1 &&
589 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
590 git cat-file blob $altblob &&
591 git rev-list --all
594 compare_results_with_midx "with alternate (local midx)"
596 test_expect_success 'multi-pack-index in an alternate' '
597 mv .git/objects/pack/* alt.git/objects/pack &&
598 test_commit add_local_objects &&
599 git repack --local &&
600 git multi-pack-index write &&
601 midx_read_expect 1 3 4 $objdir &&
602 git reset --hard HEAD~1 &&
603 rm -f .git/objects/pack/*
606 compare_results_with_midx "with alternate (remote midx)"
608 # usage: corrupt_data <file> <pos> [<data>]
609 corrupt_data () {
610 file=$1
611 pos=$2
612 data="${3:-\0}"
613 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
616 # Force 64-bit offsets by manipulating the idx file.
617 # This makes the IDX file _incorrect_ so be careful to clean up after!
618 test_expect_success 'force some 64-bit offsets with pack-objects' '
619 mkdir objects64 &&
620 mkdir objects64/pack &&
621 for i in $(test_seq 1 11)
623 generate_objects 11 || return 1
624 done &&
625 commit_and_list_objects &&
626 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
627 idx64=objects64/pack/test-64-$pack64.idx &&
628 chmod u+w $idx64 &&
629 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
630 # objects64 is not a real repository, but can serve as an alternate
631 # anyway so we can write a MIDX into it
632 git init repo &&
633 test_when_finished "rm -fr repo" &&
635 cd repo &&
636 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
637 midx64=$(git multi-pack-index --object-dir=../objects64 write)
638 ) &&
639 midx_read_expect 1 63 5 objects64 " large-offsets"
642 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
643 git multi-pack-index verify --object-dir=objects64
646 NUM_OBJECTS=63
647 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
648 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
649 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
650 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
651 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
653 test_expect_success 'verify incorrect 64-bit offset' '
654 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
655 "incorrect object offset"
658 test_expect_success 'setup expire tests' '
659 mkdir dup &&
661 cd dup &&
662 git init &&
663 test-tool genrandom "data" 4096 >large_file.txt &&
664 git update-index --add large_file.txt &&
665 for i in $(test_seq 1 20)
667 test_commit $i || exit 1
668 done &&
669 git branch A HEAD &&
670 git branch B HEAD~8 &&
671 git branch C HEAD~13 &&
672 git branch D HEAD~16 &&
673 git branch E HEAD~18 &&
674 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
675 refs/heads/A
676 ^refs/heads/B
678 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
679 refs/heads/B
680 ^refs/heads/C
682 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
683 refs/heads/C
684 ^refs/heads/D
686 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
687 refs/heads/D
688 ^refs/heads/E
690 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
691 refs/heads/E
693 git multi-pack-index write &&
694 cp -r .git/objects/pack .git/objects/pack-backup
698 test_expect_success 'expire does not remove any packs' '
700 cd dup &&
701 ls .git/objects/pack >expect &&
702 git multi-pack-index expire &&
703 ls .git/objects/pack >actual &&
704 test_cmp expect actual
708 test_expect_success 'expire progress off for redirected stderr' '
710 cd dup &&
711 git multi-pack-index expire 2>err &&
712 test_line_count = 0 err
716 test_expect_success 'expire force progress on for stderr' '
718 cd dup &&
719 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
720 test_file_not_empty err
724 test_expect_success 'expire with the --no-progress option' '
726 cd dup &&
727 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
728 test_line_count = 0 err
732 test_expect_success 'expire removes unreferenced packs' '
734 cd dup &&
735 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
736 refs/heads/A
737 ^refs/heads/C
739 git multi-pack-index write &&
740 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
741 git multi-pack-index expire &&
742 ls .git/objects/pack >actual &&
743 test_cmp expect actual &&
744 ls .git/objects/pack/ | grep idx >expect-idx &&
745 test-tool read-midx .git/objects | grep idx >actual-midx &&
746 test_cmp expect-idx actual-midx &&
747 git multi-pack-index verify &&
748 git fsck
752 test_expect_success 'repack with minimum size does not alter existing packs' '
754 cd dup &&
755 rm -rf .git/objects/pack &&
756 mv .git/objects/pack-backup .git/objects/pack &&
757 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
758 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
759 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
760 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
761 ls .git/objects/pack >expect &&
762 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
763 git multi-pack-index repack --batch-size=$MINSIZE &&
764 ls .git/objects/pack >actual &&
765 test_cmp expect actual
769 test_expect_success 'repack respects repack.packKeptObjects=false' '
770 test_when_finished rm -f dup/.git/objects/pack/*keep &&
772 cd dup &&
773 ls .git/objects/pack/*idx >idx-list &&
774 test_line_count = 5 idx-list &&
775 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
776 test_line_count = 5 keep-list &&
777 for keep in $(cat keep-list)
779 touch $keep || return 1
780 done &&
781 git multi-pack-index repack --batch-size=0 &&
782 ls .git/objects/pack/*idx >idx-list &&
783 test_line_count = 5 idx-list &&
784 test-tool read-midx .git/objects | grep idx >midx-list &&
785 test_line_count = 5 midx-list &&
786 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
787 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
788 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
789 ls .git/objects/pack/*idx >idx-list &&
790 test_line_count = 5 idx-list &&
791 test-tool read-midx .git/objects | grep idx >midx-list &&
792 test_line_count = 5 midx-list
796 test_expect_success 'repack creates a new pack' '
798 cd dup &&
799 ls .git/objects/pack/*idx >idx-list &&
800 test_line_count = 5 idx-list &&
801 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
802 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
803 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
804 ls .git/objects/pack/*idx >idx-list &&
805 test_line_count = 6 idx-list &&
806 test-tool read-midx .git/objects | grep idx >midx-list &&
807 test_line_count = 6 midx-list
811 test_expect_success 'repack (all) ignores cruft pack' '
812 git init repo &&
813 test_when_finished "rm -fr repo" &&
815 cd repo &&
817 test_commit base &&
818 test_commit --no-tag unreachable &&
820 git reset --hard base &&
821 git reflog expire --all --expire=all &&
822 git repack --cruft -d &&
824 git multi-pack-index write &&
826 find $objdir/pack | sort >before &&
827 git multi-pack-index repack --batch-size=0 &&
828 find $objdir/pack | sort >after &&
830 test_cmp before after
834 test_expect_success 'repack (--batch-size) ignores cruft pack' '
835 git init repo &&
836 test_when_finished "rm -fr repo" &&
838 cd repo &&
840 test_commit_bulk 5 &&
841 test_commit --no-tag unreachable &&
843 git reset --hard HEAD^ &&
844 git reflog expire --all --expire=all &&
845 git repack --cruft -d &&
847 test_commit four &&
849 find $objdir/pack -type f -name "*.pack" | sort >before &&
850 git repack -d &&
851 find $objdir/pack -type f -name "*.pack" | sort >after &&
853 pack="$(comm -13 before after)" &&
854 test_file_size "$pack" >sz &&
855 # Set --batch-size to twice the size of the pack created
856 # in the previous step, since this is enough to
857 # accommodate it and the cruft pack.
859 # This means that the MIDX machinery *could* combine the
860 # new and cruft packs together.
862 # We ensure that it does not below.
863 batch="$((($(cat sz) * 2)))" &&
865 git multi-pack-index write &&
867 find $objdir/pack | sort >before &&
868 git multi-pack-index repack --batch-size=$batch &&
869 find $objdir/pack | sort >after &&
871 test_cmp before after
875 test_expect_success 'expire removes repacked packs' '
877 cd dup &&
878 ls -al .git/objects/pack/*pack &&
879 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
880 git multi-pack-index expire &&
881 ls -S .git/objects/pack/*pack >actual &&
882 test_cmp expect actual &&
883 test-tool read-midx .git/objects | grep idx >midx-list &&
884 test_line_count = 4 midx-list
888 test_expect_success 'expire works when adding new packs' '
890 cd dup &&
891 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
892 refs/heads/A
893 ^refs/heads/B
895 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
896 refs/heads/B
897 ^refs/heads/C
899 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
900 refs/heads/C
901 ^refs/heads/D
903 git multi-pack-index write &&
904 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
905 refs/heads/D
906 ^refs/heads/E
908 git multi-pack-index write &&
909 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
910 refs/heads/E
912 git multi-pack-index expire &&
913 ls .git/objects/pack/ | grep idx >expect &&
914 test-tool read-midx .git/objects | grep idx >actual &&
915 test_cmp expect actual &&
916 git multi-pack-index verify
920 test_expect_success 'expire respects .keep files' '
922 cd dup &&
923 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
924 refs/heads/A
926 git multi-pack-index write &&
927 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
928 touch $PACKA.keep &&
929 git multi-pack-index expire &&
930 test_path_is_file $PACKA.idx &&
931 test_path_is_file $PACKA.keep &&
932 test_path_is_file $PACKA.pack &&
933 test-tool read-midx .git/objects | grep idx >midx-list &&
934 test_line_count = 2 midx-list
938 test_expect_success 'expiring unreferenced cruft pack retains pack' '
939 git init repo &&
940 test_when_finished "rm -fr repo" &&
942 cd repo &&
944 test_commit base &&
945 test_commit --no-tag unreachable &&
946 unreachable=$(git rev-parse HEAD) &&
948 git reset --hard base &&
949 git reflog expire --all --expire=all &&
950 git repack --cruft -d &&
951 mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
953 echo "base..$unreachable" >in &&
954 pack="$(git pack-objects --revs --delta-base-offset \
955 $objdir/pack/pack <in)" &&
957 # Preferring the contents of "$pack" will leave the
958 # cruft pack unreferenced (ie., none of the objects
959 # contained in the cruft pack will have their MIDX copy
960 # selected from the cruft pack).
961 git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
962 git multi-pack-index expire &&
964 test_path_is_file "$mtimes"
968 test_expect_success 'repack --batch-size=0 repacks everything' '
969 cp -r dup dup2 &&
971 cd dup &&
972 rm .git/objects/pack/*.keep &&
973 ls .git/objects/pack/*idx >idx-list &&
974 test_line_count = 2 idx-list &&
975 git multi-pack-index repack --batch-size=0 &&
976 ls .git/objects/pack/*idx >idx-list &&
977 test_line_count = 3 idx-list &&
978 test-tool read-midx .git/objects | grep idx >midx-list &&
979 test_line_count = 3 midx-list &&
980 git multi-pack-index expire &&
981 ls -al .git/objects/pack/*idx >idx-list &&
982 test_line_count = 1 idx-list &&
983 git multi-pack-index repack --batch-size=0 &&
984 ls -al .git/objects/pack/*idx >new-idx-list &&
985 test_cmp idx-list new-idx-list
989 test_expect_success 'repack --batch-size=<large> repacks everything' '
991 cd dup2 &&
992 rm .git/objects/pack/*.keep &&
993 ls .git/objects/pack/*idx >idx-list &&
994 test_line_count = 2 idx-list &&
995 git multi-pack-index repack --batch-size=2000000 &&
996 ls .git/objects/pack/*idx >idx-list &&
997 test_line_count = 3 idx-list &&
998 test-tool read-midx .git/objects | grep idx >midx-list &&
999 test_line_count = 3 midx-list &&
1000 git multi-pack-index expire &&
1001 ls -al .git/objects/pack/*idx >idx-list &&
1002 test_line_count = 1 idx-list
1006 test_expect_success 'load reverse index when missing .idx, .pack' '
1007 git init repo &&
1008 test_when_finished "rm -fr repo" &&
1010 cd repo &&
1012 git config core.multiPackIndex true &&
1014 test_commit base &&
1015 git repack -ad &&
1016 git multi-pack-index write &&
1018 git rev-parse HEAD >tip &&
1019 pack=$(ls .git/objects/pack/pack-*.pack) &&
1020 idx=$(ls .git/objects/pack/pack-*.idx) &&
1022 mv $idx $idx.bak &&
1023 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1025 mv $idx.bak $idx &&
1027 mv $pack $pack.bak &&
1028 git cat-file --batch-check="%(objectsize:disk)" <tip
1032 test_expect_success 'usage shown without sub-command' '
1033 test_expect_code 129 git multi-pack-index 2>err &&
1034 ! test_i18ngrep "unrecognized subcommand" err
1037 test_expect_success 'complains when run outside of a repository' '
1038 nongit test_must_fail git multi-pack-index write 2>err &&
1039 grep "not a git repository" err
1042 test_expect_success 'repack with delta islands' '
1043 git init repo &&
1044 test_when_finished "rm -fr repo" &&
1046 cd repo &&
1048 test_commit first &&
1049 git repack &&
1050 test_commit second &&
1051 git repack &&
1053 git multi-pack-index write &&
1054 git -c repack.useDeltaIslands=true multi-pack-index repack
1058 test_done