worktree: stop being overly intimate with run_command() internals
[git.git] / t / t5319-multi-pack-index.sh
blob3f69e43178c1d9b7eec4b3b14c8311c867328bce
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
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
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 progress off for redirected stderr' '
187 git multi-pack-index --object-dir=$objdir write 2>err &&
188 test_line_count = 0 err
191 test_expect_success 'write force progress on for stderr' '
192 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --progress 2>err &&
193 test_file_not_empty err
196 test_expect_success 'write with the --no-progress option' '
197 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --no-progress 2>err &&
198 test_line_count = 0 err
201 test_expect_success 'add more packs' '
202 for j in $(test_seq 11 20)
204 generate_objects $j &&
205 commit_and_list_objects &&
206 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
207 done
210 compare_results_with_midx "mixed mode (two packs + extra)"
212 test_expect_success 'write midx with twelve packs' '
213 git multi-pack-index --object-dir=$objdir write &&
214 midx_read_expect 12 74 4 $objdir
217 compare_results_with_midx "twelve packs"
219 test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
220 git init repo &&
221 git clone -s repo alternate &&
223 test_when_finished "rm -rf repo alternate" &&
226 cd repo &&
227 test_commit base &&
228 git repack -d
229 ) &&
231 ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
232 theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
233 touch "$ours" "$theirs" &&
236 cd alternate &&
237 git multi-pack-index --object-dir ../repo/.git/objects write
238 ) &&
240 # writing a midx in "repo" should not remove the .rev file in the
241 # alternate
242 test_path_is_file repo/.git/objects/pack/multi-pack-index &&
243 test_path_is_file $ours &&
244 test_path_is_missing $theirs
247 test_expect_success 'warn on improper hash version' '
248 git init --object-format=sha1 sha1 &&
250 cd sha1 &&
251 git config core.multiPackIndex true &&
252 test_commit 1 &&
253 git repack -a &&
254 git multi-pack-index write &&
255 mv .git/objects/pack/multi-pack-index ../mpi-sha1
256 ) &&
257 git init --object-format=sha256 sha256 &&
259 cd sha256 &&
260 git config core.multiPackIndex true &&
261 test_commit 1 &&
262 git repack -a &&
263 git multi-pack-index write &&
264 mv .git/objects/pack/multi-pack-index ../mpi-sha256
265 ) &&
267 cd sha1 &&
268 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
269 git log -1 2>err &&
270 test_i18ngrep "multi-pack-index hash version 2 does not match version 1" err
271 ) &&
273 cd sha256 &&
274 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
275 git log -1 2>err &&
276 test_i18ngrep "multi-pack-index hash version 1 does not match version 2" err
280 test_expect_success 'midx picks objects from preferred pack' '
281 test_when_finished rm -rf preferred.git &&
282 git init --bare preferred.git &&
284 cd preferred.git &&
286 a=$(echo "a" | git hash-object -w --stdin) &&
287 b=$(echo "b" | git hash-object -w --stdin) &&
288 c=$(echo "c" | git hash-object -w --stdin) &&
290 # Set up two packs, duplicating the object "B" at different
291 # offsets.
293 # Note that the "BC" pack (the one we choose as preferred) sorts
294 # lexically after the "AB" pack, meaning that omitting the
295 # --preferred-pack argument would cause this test to fail (since
296 # the MIDX code would select the copy of "b" in the "AB" pack).
297 git pack-objects objects/pack/test-AB <<-EOF &&
301 bc=$(git pack-objects objects/pack/test-BC <<-EOF
305 ) &&
307 git multi-pack-index --object-dir=objects \
308 write --preferred-pack=test-BC-$bc.idx 2>err &&
309 test_must_be_empty err &&
311 test-tool read-midx --show-objects objects >out &&
313 ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
314 cut -d" " -f1) &&
315 printf "%s %s\tobjects/pack/test-BC-%s.pack\n" \
316 "$b" "$ofs" "$bc" >expect &&
317 grep ^$b out >actual &&
319 test_cmp expect actual
323 test_expect_success 'preferred packs must be non-empty' '
324 test_when_finished rm -rf preferred.git &&
325 git init preferred.git &&
327 cd preferred.git &&
329 test_commit base &&
330 git repack -ad &&
332 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
334 test_must_fail git multi-pack-index write \
335 --preferred-pack=pack-$empty.pack 2>err &&
336 grep "with no objects" err
340 test_expect_success 'verify multi-pack-index success' '
341 git multi-pack-index verify --object-dir=$objdir
344 test_expect_success 'verify progress off for redirected stderr' '
345 git multi-pack-index verify --object-dir=$objdir 2>err &&
346 test_line_count = 0 err
349 test_expect_success 'verify force progress on for stderr' '
350 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
351 test_file_not_empty err
354 test_expect_success 'verify with the --no-progress option' '
355 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
356 test_line_count = 0 err
359 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
360 corrupt_midx_and_verify() {
361 POS=$1 &&
362 DATA="${2:-\0}" &&
363 OBJDIR=$3 &&
364 GREPSTR="$4" &&
365 COMMAND="$5" &&
366 if test -z "$COMMAND"
367 then
368 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
369 fi &&
370 FILE=$OBJDIR/pack/multi-pack-index &&
371 chmod a+w $FILE &&
372 test_when_finished mv midx-backup $FILE &&
373 cp $FILE midx-backup &&
374 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
375 test_must_fail $COMMAND 2>test_err &&
376 grep -v "^+" test_err >err &&
377 test_i18ngrep "$GREPSTR" err
380 test_expect_success 'verify bad signature' '
381 corrupt_midx_and_verify 0 "\00" $objdir \
382 "multi-pack-index signature"
385 NUM_OBJECTS=74
386 MIDX_BYTE_VERSION=4
387 MIDX_BYTE_OID_VERSION=5
388 MIDX_BYTE_CHUNK_COUNT=6
389 MIDX_HEADER_SIZE=12
390 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
391 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
392 MIDX_NUM_CHUNKS=5
393 MIDX_CHUNK_LOOKUP_WIDTH=12
394 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
395 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
396 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
397 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
398 MIDX_OID_FANOUT_WIDTH=4
399 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
400 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
401 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
402 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
403 MIDX_OFFSET_WIDTH=8
404 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
405 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
407 test_expect_success 'verify bad version' '
408 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
409 "multi-pack-index version"
412 test_expect_success 'verify bad OID version' '
413 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
414 "hash version"
417 test_expect_success 'verify truncated chunk count' '
418 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
419 "final chunk has non-zero id"
422 test_expect_success 'verify extended chunk count' '
423 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
424 "terminating chunk id appears earlier than expected"
427 test_expect_success 'verify missing required chunk' '
428 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
429 "missing required"
432 test_expect_success 'verify invalid chunk offset' '
433 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
434 "improper chunk offset(s)"
437 test_expect_success 'verify packnames out of order' '
438 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
439 "pack names out of order"
442 test_expect_success 'verify packnames out of order' '
443 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
444 "failed to load pack"
447 test_expect_success 'verify oid fanout out of order' '
448 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
449 "oid fanout out of order"
452 test_expect_success 'verify oid lookup out of order' '
453 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
454 "oid lookup out of order"
457 test_expect_success 'verify incorrect pack-int-id' '
458 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
459 "bad pack-int-id"
462 test_expect_success 'verify incorrect offset' '
463 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
464 "incorrect object offset"
467 test_expect_success 'git-fsck incorrect offset' '
468 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
469 "incorrect object offset" \
470 "git -c core.multiPackIndex=true fsck" &&
471 test_unconfig core.multiPackIndex &&
472 test_must_fail git fsck &&
473 git -c core.multiPackIndex=false fsck
476 test_expect_success 'corrupt MIDX is not reused' '
477 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
478 "incorrect object offset" &&
479 git multi-pack-index write 2>err &&
480 test_i18ngrep checksum.mismatch err &&
481 git multi-pack-index verify
484 test_expect_success 'verify incorrect checksum' '
485 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 1)) &&
486 corrupt_midx_and_verify $pos "\377" $objdir "incorrect checksum"
489 test_expect_success 'repack progress off for redirected stderr' '
490 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
491 test_line_count = 0 err
494 test_expect_success 'repack force progress on for stderr' '
495 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
496 test_file_not_empty err
499 test_expect_success 'repack with the --no-progress option' '
500 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
501 test_line_count = 0 err
504 test_expect_success 'repack removes multi-pack-index when deleting packs' '
505 test_path_is_file $objdir/pack/multi-pack-index &&
506 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
507 # multi-pack-index after repacking, but set "core.multiPackIndex" to
508 # true so that "git repack" can read the existing MIDX.
509 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
510 test_path_is_missing $objdir/pack/multi-pack-index
513 test_expect_success 'repack preserves multi-pack-index when creating packs' '
514 git init preserve &&
515 test_when_finished "rm -fr preserve" &&
517 cd preserve &&
518 packdir=.git/objects/pack &&
519 midx=$packdir/multi-pack-index &&
521 test_commit 1 &&
522 pack1=$(git pack-objects --all $packdir/pack) &&
523 touch $packdir/pack-$pack1.keep &&
524 test_commit 2 &&
525 pack2=$(git pack-objects --revs $packdir/pack) &&
526 touch $packdir/pack-$pack2.keep &&
528 git multi-pack-index write &&
529 cp $midx $midx.bak &&
531 cat >pack-input <<-EOF &&
532 HEAD
533 ^HEAD~1
535 test_commit 3 &&
536 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
537 test_commit 4 &&
538 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
540 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
541 ls -la $packdir &&
542 test_path_is_file $packdir/pack-$pack1.pack &&
543 test_path_is_file $packdir/pack-$pack2.pack &&
544 test_path_is_missing $packdir/pack-$pack3.pack &&
545 test_path_is_missing $packdir/pack-$pack4.pack &&
546 test_cmp_bin $midx.bak $midx
550 compare_results_with_midx "after repack"
552 test_expect_success 'multi-pack-index and pack-bitmap' '
553 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
554 git -c repack.writeBitmaps=true repack -ad &&
555 git multi-pack-index write &&
556 git rev-list --test-bitmap HEAD
559 test_expect_success 'multi-pack-index and alternates' '
560 git init --bare alt.git &&
561 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
562 echo content1 >file1 &&
563 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
564 git cat-file blob $altblob &&
565 git rev-list --all
568 compare_results_with_midx "with alternate (local midx)"
570 test_expect_success 'multi-pack-index in an alternate' '
571 mv .git/objects/pack/* alt.git/objects/pack &&
572 test_commit add_local_objects &&
573 git repack --local &&
574 git multi-pack-index write &&
575 midx_read_expect 1 3 4 $objdir &&
576 git reset --hard HEAD~1 &&
577 rm -f .git/objects/pack/*
580 compare_results_with_midx "with alternate (remote midx)"
582 # usage: corrupt_data <file> <pos> [<data>]
583 corrupt_data () {
584 file=$1
585 pos=$2
586 data="${3:-\0}"
587 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
590 # Force 64-bit offsets by manipulating the idx file.
591 # This makes the IDX file _incorrect_ so be careful to clean up after!
592 test_expect_success 'force some 64-bit offsets with pack-objects' '
593 mkdir objects64 &&
594 mkdir objects64/pack &&
595 for i in $(test_seq 1 11)
597 generate_objects 11
598 done &&
599 commit_and_list_objects &&
600 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
601 idx64=objects64/pack/test-64-$pack64.idx &&
602 chmod u+w $idx64 &&
603 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
604 # objects64 is not a real repository, but can serve as an alternate
605 # anyway so we can write a MIDX into it
606 git init repo &&
607 test_when_finished "rm -fr repo" &&
609 cd repo &&
610 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
611 midx64=$(git multi-pack-index --object-dir=../objects64 write)
612 ) &&
613 midx_read_expect 1 63 5 objects64 " large-offsets"
616 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
617 git multi-pack-index verify --object-dir=objects64
620 NUM_OBJECTS=63
621 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
622 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
623 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
624 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
625 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
627 test_expect_success 'verify incorrect 64-bit offset' '
628 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
629 "incorrect object offset"
632 test_expect_success 'setup expire tests' '
633 mkdir dup &&
635 cd dup &&
636 git init &&
637 test-tool genrandom "data" 4096 >large_file.txt &&
638 git update-index --add large_file.txt &&
639 for i in $(test_seq 1 20)
641 test_commit $i
642 done &&
643 git branch A HEAD &&
644 git branch B HEAD~8 &&
645 git branch C HEAD~13 &&
646 git branch D HEAD~16 &&
647 git branch E HEAD~18 &&
648 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
649 refs/heads/A
650 ^refs/heads/B
652 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
653 refs/heads/B
654 ^refs/heads/C
656 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
657 refs/heads/C
658 ^refs/heads/D
660 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
661 refs/heads/D
662 ^refs/heads/E
664 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
665 refs/heads/E
667 git multi-pack-index write &&
668 cp -r .git/objects/pack .git/objects/pack-backup
672 test_expect_success 'expire does not remove any packs' '
674 cd dup &&
675 ls .git/objects/pack >expect &&
676 git multi-pack-index expire &&
677 ls .git/objects/pack >actual &&
678 test_cmp expect actual
682 test_expect_success 'expire progress off for redirected stderr' '
684 cd dup &&
685 git multi-pack-index expire 2>err &&
686 test_line_count = 0 err
690 test_expect_success 'expire force progress on for stderr' '
692 cd dup &&
693 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
694 test_file_not_empty err
698 test_expect_success 'expire with the --no-progress option' '
700 cd dup &&
701 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
702 test_line_count = 0 err
706 test_expect_success 'expire removes unreferenced packs' '
708 cd dup &&
709 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
710 refs/heads/A
711 ^refs/heads/C
713 git multi-pack-index write &&
714 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
715 git multi-pack-index expire &&
716 ls .git/objects/pack >actual &&
717 test_cmp expect actual &&
718 ls .git/objects/pack/ | grep idx >expect-idx &&
719 test-tool read-midx .git/objects | grep idx >actual-midx &&
720 test_cmp expect-idx actual-midx &&
721 git multi-pack-index verify &&
722 git fsck
726 test_expect_success 'repack with minimum size does not alter existing packs' '
728 cd dup &&
729 rm -rf .git/objects/pack &&
730 mv .git/objects/pack-backup .git/objects/pack &&
731 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
732 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
733 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
734 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
735 ls .git/objects/pack >expect &&
736 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
737 git multi-pack-index repack --batch-size=$MINSIZE &&
738 ls .git/objects/pack >actual &&
739 test_cmp expect actual
743 test_expect_success 'repack respects repack.packKeptObjects=false' '
744 test_when_finished rm -f dup/.git/objects/pack/*keep &&
746 cd dup &&
747 ls .git/objects/pack/*idx >idx-list &&
748 test_line_count = 5 idx-list &&
749 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
750 test_line_count = 5 keep-list &&
751 for keep in $(cat keep-list)
753 touch $keep || return 1
754 done &&
755 git multi-pack-index repack --batch-size=0 &&
756 ls .git/objects/pack/*idx >idx-list &&
757 test_line_count = 5 idx-list &&
758 test-tool read-midx .git/objects | grep idx >midx-list &&
759 test_line_count = 5 midx-list &&
760 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
761 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
762 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
763 ls .git/objects/pack/*idx >idx-list &&
764 test_line_count = 5 idx-list &&
765 test-tool read-midx .git/objects | grep idx >midx-list &&
766 test_line_count = 5 midx-list
770 test_expect_success 'repack creates a new pack' '
772 cd dup &&
773 ls .git/objects/pack/*idx >idx-list &&
774 test_line_count = 5 idx-list &&
775 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
776 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
777 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
778 ls .git/objects/pack/*idx >idx-list &&
779 test_line_count = 6 idx-list &&
780 test-tool read-midx .git/objects | grep idx >midx-list &&
781 test_line_count = 6 midx-list
785 test_expect_success 'expire removes repacked packs' '
787 cd dup &&
788 ls -al .git/objects/pack/*pack &&
789 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
790 git multi-pack-index expire &&
791 ls -S .git/objects/pack/*pack >actual &&
792 test_cmp expect actual &&
793 test-tool read-midx .git/objects | grep idx >midx-list &&
794 test_line_count = 4 midx-list
798 test_expect_success 'expire works when adding new packs' '
800 cd dup &&
801 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
802 refs/heads/A
803 ^refs/heads/B
805 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
806 refs/heads/B
807 ^refs/heads/C
809 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
810 refs/heads/C
811 ^refs/heads/D
813 git multi-pack-index write &&
814 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
815 refs/heads/D
816 ^refs/heads/E
818 git multi-pack-index write &&
819 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
820 refs/heads/E
822 git multi-pack-index expire &&
823 ls .git/objects/pack/ | grep idx >expect &&
824 test-tool read-midx .git/objects | grep idx >actual &&
825 test_cmp expect actual &&
826 git multi-pack-index verify
830 test_expect_success 'expire respects .keep files' '
832 cd dup &&
833 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
834 refs/heads/A
836 git multi-pack-index write &&
837 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
838 touch $PACKA.keep &&
839 git multi-pack-index expire &&
840 test_path_is_file $PACKA.idx &&
841 test_path_is_file $PACKA.keep &&
842 test_path_is_file $PACKA.pack &&
843 test-tool read-midx .git/objects | grep idx >midx-list &&
844 test_line_count = 2 midx-list
848 test_expect_success 'repack --batch-size=0 repacks everything' '
849 cp -r dup dup2 &&
851 cd dup &&
852 rm .git/objects/pack/*.keep &&
853 ls .git/objects/pack/*idx >idx-list &&
854 test_line_count = 2 idx-list &&
855 git multi-pack-index repack --batch-size=0 &&
856 ls .git/objects/pack/*idx >idx-list &&
857 test_line_count = 3 idx-list &&
858 test-tool read-midx .git/objects | grep idx >midx-list &&
859 test_line_count = 3 midx-list &&
860 git multi-pack-index expire &&
861 ls -al .git/objects/pack/*idx >idx-list &&
862 test_line_count = 1 idx-list &&
863 git multi-pack-index repack --batch-size=0 &&
864 ls -al .git/objects/pack/*idx >new-idx-list &&
865 test_cmp idx-list new-idx-list
869 test_expect_success 'repack --batch-size=<large> repacks everything' '
871 cd dup2 &&
872 rm .git/objects/pack/*.keep &&
873 ls .git/objects/pack/*idx >idx-list &&
874 test_line_count = 2 idx-list &&
875 git multi-pack-index repack --batch-size=2000000 &&
876 ls .git/objects/pack/*idx >idx-list &&
877 test_line_count = 3 idx-list &&
878 test-tool read-midx .git/objects | grep idx >midx-list &&
879 test_line_count = 3 midx-list &&
880 git multi-pack-index expire &&
881 ls -al .git/objects/pack/*idx >idx-list &&
882 test_line_count = 1 idx-list
886 test_expect_success 'load reverse index when missing .idx, .pack' '
887 git init repo &&
888 test_when_finished "rm -fr repo" &&
890 cd repo &&
892 git config core.multiPackIndex true &&
894 test_commit base &&
895 git repack -ad &&
896 git multi-pack-index write &&
898 git rev-parse HEAD >tip &&
899 pack=$(ls .git/objects/pack/pack-*.pack) &&
900 idx=$(ls .git/objects/pack/pack-*.idx) &&
902 mv $idx $idx.bak &&
903 git cat-file --batch-check="%(objectsize:disk)" <tip &&
905 mv $idx.bak $idx &&
907 mv $pack $pack.bak &&
908 git cat-file --batch-check="%(objectsize:disk)" <tip
912 test_expect_success 'usage shown without sub-command' '
913 test_expect_code 129 git multi-pack-index 2>err &&
914 ! test_i18ngrep "unrecognized subcommand" err
917 test_expect_success 'complains when run outside of a repository' '
918 nongit test_must_fail git multi-pack-index write 2>err &&
919 grep "not a git repository" err
922 test_done