git-compat-util: move strbuf.c funcs to its header
[git/gitster.git] / t / t5319-multi-pack-index.sh
blob0883c7c6bd965d5506ca45a10c2c406a2700707a
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 'corrupt MIDX is not reused' '
489 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
490 "incorrect object offset" &&
491 git multi-pack-index write 2>err &&
492 test_i18ngrep checksum.mismatch err &&
493 git multi-pack-index verify
496 test_expect_success 'verify incorrect checksum' '
497 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
498 corrupt_midx_and_verify $pos \
499 "\377\377\377\377\377\377\377\377\377\377" \
500 $objdir "incorrect checksum"
503 test_expect_success 'repack progress off for redirected stderr' '
504 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
505 test_line_count = 0 err
508 test_expect_success 'repack force progress on for stderr' '
509 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
510 test_file_not_empty err
513 test_expect_success 'repack with the --no-progress option' '
514 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
515 test_line_count = 0 err
518 test_expect_success 'repack removes multi-pack-index when deleting packs' '
519 test_path_is_file $objdir/pack/multi-pack-index &&
520 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
521 # multi-pack-index after repacking, but set "core.multiPackIndex" to
522 # true so that "git repack" can read the existing MIDX.
523 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
524 test_path_is_missing $objdir/pack/multi-pack-index
527 test_expect_success 'repack preserves multi-pack-index when creating packs' '
528 git init preserve &&
529 test_when_finished "rm -fr preserve" &&
531 cd preserve &&
532 packdir=.git/objects/pack &&
533 midx=$packdir/multi-pack-index &&
535 test_commit 1 &&
536 pack1=$(git pack-objects --all $packdir/pack) &&
537 touch $packdir/pack-$pack1.keep &&
538 test_commit 2 &&
539 pack2=$(git pack-objects --revs $packdir/pack) &&
540 touch $packdir/pack-$pack2.keep &&
542 git multi-pack-index write &&
543 cp $midx $midx.bak &&
545 cat >pack-input <<-EOF &&
546 HEAD
547 ^HEAD~1
549 test_commit 3 &&
550 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
551 test_commit 4 &&
552 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
554 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
555 ls -la $packdir &&
556 test_path_is_file $packdir/pack-$pack1.pack &&
557 test_path_is_file $packdir/pack-$pack2.pack &&
558 test_path_is_missing $packdir/pack-$pack3.pack &&
559 test_path_is_missing $packdir/pack-$pack4.pack &&
560 test_cmp_bin $midx.bak $midx
564 compare_results_with_midx "after repack"
566 test_expect_success 'multi-pack-index and pack-bitmap' '
567 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
568 git -c repack.writeBitmaps=true repack -ad &&
569 git multi-pack-index write &&
570 git rev-list --test-bitmap HEAD
573 test_expect_success 'multi-pack-index and alternates' '
574 git init --bare alt.git &&
575 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
576 echo content1 >file1 &&
577 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
578 git cat-file blob $altblob &&
579 git rev-list --all
582 compare_results_with_midx "with alternate (local midx)"
584 test_expect_success 'multi-pack-index in an alternate' '
585 mv .git/objects/pack/* alt.git/objects/pack &&
586 test_commit add_local_objects &&
587 git repack --local &&
588 git multi-pack-index write &&
589 midx_read_expect 1 3 4 $objdir &&
590 git reset --hard HEAD~1 &&
591 rm -f .git/objects/pack/*
594 compare_results_with_midx "with alternate (remote midx)"
596 # usage: corrupt_data <file> <pos> [<data>]
597 corrupt_data () {
598 file=$1
599 pos=$2
600 data="${3:-\0}"
601 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
604 # Force 64-bit offsets by manipulating the idx file.
605 # This makes the IDX file _incorrect_ so be careful to clean up after!
606 test_expect_success 'force some 64-bit offsets with pack-objects' '
607 mkdir objects64 &&
608 mkdir objects64/pack &&
609 for i in $(test_seq 1 11)
611 generate_objects 11 || return 1
612 done &&
613 commit_and_list_objects &&
614 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
615 idx64=objects64/pack/test-64-$pack64.idx &&
616 chmod u+w $idx64 &&
617 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
618 # objects64 is not a real repository, but can serve as an alternate
619 # anyway so we can write a MIDX into it
620 git init repo &&
621 test_when_finished "rm -fr repo" &&
623 cd repo &&
624 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
625 midx64=$(git multi-pack-index --object-dir=../objects64 write)
626 ) &&
627 midx_read_expect 1 63 5 objects64 " large-offsets"
630 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
631 git multi-pack-index verify --object-dir=objects64
634 NUM_OBJECTS=63
635 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
636 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
637 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
638 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
639 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
641 test_expect_success 'verify incorrect 64-bit offset' '
642 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
643 "incorrect object offset"
646 test_expect_success 'setup expire tests' '
647 mkdir dup &&
649 cd dup &&
650 git init &&
651 test-tool genrandom "data" 4096 >large_file.txt &&
652 git update-index --add large_file.txt &&
653 for i in $(test_seq 1 20)
655 test_commit $i || exit 1
656 done &&
657 git branch A HEAD &&
658 git branch B HEAD~8 &&
659 git branch C HEAD~13 &&
660 git branch D HEAD~16 &&
661 git branch E HEAD~18 &&
662 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
663 refs/heads/A
664 ^refs/heads/B
666 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
667 refs/heads/B
668 ^refs/heads/C
670 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
671 refs/heads/C
672 ^refs/heads/D
674 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
675 refs/heads/D
676 ^refs/heads/E
678 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
679 refs/heads/E
681 git multi-pack-index write &&
682 cp -r .git/objects/pack .git/objects/pack-backup
686 test_expect_success 'expire does not remove any packs' '
688 cd dup &&
689 ls .git/objects/pack >expect &&
690 git multi-pack-index expire &&
691 ls .git/objects/pack >actual &&
692 test_cmp expect actual
696 test_expect_success 'expire progress off for redirected stderr' '
698 cd dup &&
699 git multi-pack-index expire 2>err &&
700 test_line_count = 0 err
704 test_expect_success 'expire force progress on for stderr' '
706 cd dup &&
707 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
708 test_file_not_empty err
712 test_expect_success 'expire with the --no-progress option' '
714 cd dup &&
715 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
716 test_line_count = 0 err
720 test_expect_success 'expire removes unreferenced packs' '
722 cd dup &&
723 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
724 refs/heads/A
725 ^refs/heads/C
727 git multi-pack-index write &&
728 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
729 git multi-pack-index expire &&
730 ls .git/objects/pack >actual &&
731 test_cmp expect actual &&
732 ls .git/objects/pack/ | grep idx >expect-idx &&
733 test-tool read-midx .git/objects | grep idx >actual-midx &&
734 test_cmp expect-idx actual-midx &&
735 git multi-pack-index verify &&
736 git fsck
740 test_expect_success 'repack with minimum size does not alter existing packs' '
742 cd dup &&
743 rm -rf .git/objects/pack &&
744 mv .git/objects/pack-backup .git/objects/pack &&
745 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
746 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
747 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
748 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
749 ls .git/objects/pack >expect &&
750 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
751 git multi-pack-index repack --batch-size=$MINSIZE &&
752 ls .git/objects/pack >actual &&
753 test_cmp expect actual
757 test_expect_success 'repack respects repack.packKeptObjects=false' '
758 test_when_finished rm -f dup/.git/objects/pack/*keep &&
760 cd dup &&
761 ls .git/objects/pack/*idx >idx-list &&
762 test_line_count = 5 idx-list &&
763 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
764 test_line_count = 5 keep-list &&
765 for keep in $(cat keep-list)
767 touch $keep || return 1
768 done &&
769 git multi-pack-index repack --batch-size=0 &&
770 ls .git/objects/pack/*idx >idx-list &&
771 test_line_count = 5 idx-list &&
772 test-tool read-midx .git/objects | grep idx >midx-list &&
773 test_line_count = 5 midx-list &&
774 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
775 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
776 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
777 ls .git/objects/pack/*idx >idx-list &&
778 test_line_count = 5 idx-list &&
779 test-tool read-midx .git/objects | grep idx >midx-list &&
780 test_line_count = 5 midx-list
784 test_expect_success 'repack creates a new pack' '
786 cd dup &&
787 ls .git/objects/pack/*idx >idx-list &&
788 test_line_count = 5 idx-list &&
789 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
790 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
791 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
792 ls .git/objects/pack/*idx >idx-list &&
793 test_line_count = 6 idx-list &&
794 test-tool read-midx .git/objects | grep idx >midx-list &&
795 test_line_count = 6 midx-list
799 test_expect_success 'repack (all) ignores cruft pack' '
800 git init repo &&
801 test_when_finished "rm -fr repo" &&
803 cd repo &&
805 test_commit base &&
806 test_commit --no-tag unreachable &&
808 git reset --hard base &&
809 git reflog expire --all --expire=all &&
810 git repack --cruft -d &&
812 git multi-pack-index write &&
814 find $objdir/pack | sort >before &&
815 git multi-pack-index repack --batch-size=0 &&
816 find $objdir/pack | sort >after &&
818 test_cmp before after
822 test_expect_success 'repack (--batch-size) ignores cruft pack' '
823 git init repo &&
824 test_when_finished "rm -fr repo" &&
826 cd repo &&
828 test_commit_bulk 5 &&
829 test_commit --no-tag unreachable &&
831 git reset --hard HEAD^ &&
832 git reflog expire --all --expire=all &&
833 git repack --cruft -d &&
835 test_commit four &&
837 find $objdir/pack -type f -name "*.pack" | sort >before &&
838 git repack -d &&
839 find $objdir/pack -type f -name "*.pack" | sort >after &&
841 pack="$(comm -13 before after)" &&
842 test_file_size "$pack" >sz &&
843 # Set --batch-size to twice the size of the pack created
844 # in the previous step, since this is enough to
845 # accommodate it and the cruft pack.
847 # This means that the MIDX machinery *could* combine the
848 # new and cruft packs together.
850 # We ensure that it does not below.
851 batch="$((($(cat sz) * 2)))" &&
853 git multi-pack-index write &&
855 find $objdir/pack | sort >before &&
856 git multi-pack-index repack --batch-size=$batch &&
857 find $objdir/pack | sort >after &&
859 test_cmp before after
863 test_expect_success 'expire removes repacked packs' '
865 cd dup &&
866 ls -al .git/objects/pack/*pack &&
867 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
868 git multi-pack-index expire &&
869 ls -S .git/objects/pack/*pack >actual &&
870 test_cmp expect actual &&
871 test-tool read-midx .git/objects | grep idx >midx-list &&
872 test_line_count = 4 midx-list
876 test_expect_success 'expire works when adding new packs' '
878 cd dup &&
879 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
880 refs/heads/A
881 ^refs/heads/B
883 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
884 refs/heads/B
885 ^refs/heads/C
887 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
888 refs/heads/C
889 ^refs/heads/D
891 git multi-pack-index write &&
892 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
893 refs/heads/D
894 ^refs/heads/E
896 git multi-pack-index write &&
897 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
898 refs/heads/E
900 git multi-pack-index expire &&
901 ls .git/objects/pack/ | grep idx >expect &&
902 test-tool read-midx .git/objects | grep idx >actual &&
903 test_cmp expect actual &&
904 git multi-pack-index verify
908 test_expect_success 'expire respects .keep files' '
910 cd dup &&
911 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
912 refs/heads/A
914 git multi-pack-index write &&
915 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
916 touch $PACKA.keep &&
917 git multi-pack-index expire &&
918 test_path_is_file $PACKA.idx &&
919 test_path_is_file $PACKA.keep &&
920 test_path_is_file $PACKA.pack &&
921 test-tool read-midx .git/objects | grep idx >midx-list &&
922 test_line_count = 2 midx-list
926 test_expect_success 'expiring unreferenced cruft pack retains pack' '
927 git init repo &&
928 test_when_finished "rm -fr repo" &&
930 cd repo &&
932 test_commit base &&
933 test_commit --no-tag unreachable &&
934 unreachable=$(git rev-parse HEAD) &&
936 git reset --hard base &&
937 git reflog expire --all --expire=all &&
938 git repack --cruft -d &&
939 mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
941 echo "base..$unreachable" >in &&
942 pack="$(git pack-objects --revs --delta-base-offset \
943 $objdir/pack/pack <in)" &&
945 # Preferring the contents of "$pack" will leave the
946 # cruft pack unreferenced (ie., none of the objects
947 # contained in the cruft pack will have their MIDX copy
948 # selected from the cruft pack).
949 git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
950 git multi-pack-index expire &&
952 test_path_is_file "$mtimes"
956 test_expect_success 'repack --batch-size=0 repacks everything' '
957 cp -r dup dup2 &&
959 cd dup &&
960 rm .git/objects/pack/*.keep &&
961 ls .git/objects/pack/*idx >idx-list &&
962 test_line_count = 2 idx-list &&
963 git multi-pack-index repack --batch-size=0 &&
964 ls .git/objects/pack/*idx >idx-list &&
965 test_line_count = 3 idx-list &&
966 test-tool read-midx .git/objects | grep idx >midx-list &&
967 test_line_count = 3 midx-list &&
968 git multi-pack-index expire &&
969 ls -al .git/objects/pack/*idx >idx-list &&
970 test_line_count = 1 idx-list &&
971 git multi-pack-index repack --batch-size=0 &&
972 ls -al .git/objects/pack/*idx >new-idx-list &&
973 test_cmp idx-list new-idx-list
977 test_expect_success 'repack --batch-size=<large> repacks everything' '
979 cd dup2 &&
980 rm .git/objects/pack/*.keep &&
981 ls .git/objects/pack/*idx >idx-list &&
982 test_line_count = 2 idx-list &&
983 git multi-pack-index repack --batch-size=2000000 &&
984 ls .git/objects/pack/*idx >idx-list &&
985 test_line_count = 3 idx-list &&
986 test-tool read-midx .git/objects | grep idx >midx-list &&
987 test_line_count = 3 midx-list &&
988 git multi-pack-index expire &&
989 ls -al .git/objects/pack/*idx >idx-list &&
990 test_line_count = 1 idx-list
994 test_expect_success 'load reverse index when missing .idx, .pack' '
995 git init repo &&
996 test_when_finished "rm -fr repo" &&
998 cd repo &&
1000 git config core.multiPackIndex true &&
1002 test_commit base &&
1003 git repack -ad &&
1004 git multi-pack-index write &&
1006 git rev-parse HEAD >tip &&
1007 pack=$(ls .git/objects/pack/pack-*.pack) &&
1008 idx=$(ls .git/objects/pack/pack-*.idx) &&
1010 mv $idx $idx.bak &&
1011 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1013 mv $idx.bak $idx &&
1015 mv $pack $pack.bak &&
1016 git cat-file --batch-check="%(objectsize:disk)" <tip
1020 test_expect_success 'usage shown without sub-command' '
1021 test_expect_code 129 git multi-pack-index 2>err &&
1022 ! test_i18ngrep "unrecognized subcommand" err
1025 test_expect_success 'complains when run outside of a repository' '
1026 nongit test_must_fail git multi-pack-index write 2>err &&
1027 grep "not a git repository" err
1030 test_expect_success 'repack with delta islands' '
1031 git init repo &&
1032 test_when_finished "rm -fr repo" &&
1034 cd repo &&
1036 test_commit first &&
1037 git repack &&
1038 test_commit second &&
1039 git repack &&
1041 git multi-pack-index write &&
1042 git -c repack.useDeltaIslands=true multi-pack-index repack
1046 test_done