at commit ae7a0ffc

1 #!/bin/sh
2
3 test_description='multi-pack-indexes'
4
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-chunk.sh
7 . "$TEST_DIRECTORY"/lib-midx.sh
8
9 GIT_TEST_MULTI_PACK_INDEX=0
10 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
11 GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL=0
12 objdir=.git/objects
13
14 HASH_LEN=$(test_oid rawsz)
15
16 midx_read_expect () {
17 NUM_PACKS=$1
18 NUM_OBJECTS=$2
19 NUM_CHUNKS=$3
20 OBJECT_DIR=$4
21 EXTRA_CHUNKS="$5"
22 {
23 cat <<-EOF &&
24 header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
25 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
26 num_objects: $NUM_OBJECTS
27 packs:
28 EOF
29 if test $NUM_PACKS -ge 1
30 then
31 ls "$OBJECT_DIR"/pack/ | grep idx | sort
32 fi &&
33 printf "object-dir: $OBJECT_DIR\n"
34 } >expect &&
35 test-tool read-midx "$OBJECT_DIR" >actual &&
36 test_cmp expect actual
37 }
38
39 test_expect_success 'setup' '
40 test_oid_cache <<-EOF
41 idxoff sha1:2999
42 idxoff sha256:3739
43
44 packnameoff sha1:652
45 packnameoff sha256:940
46
47 fanoutoff sha1:1
48 fanoutoff sha256:3
49 EOF
50 '
51
52 test_expect_success "don't write midx with no packs" '
53 test_must_fail git multi-pack-index --object-dir=. write &&
54 test_path_is_missing pack/multi-pack-index
55 '
56
57 test_expect_success SHA1 'warn if a midx contains no oid' '
58 cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
59 test_must_fail git multi-pack-index verify &&
60 rm $objdir/pack/multi-pack-index
61 '
62
63 generate_objects () {
64 i=$1
65 iii=$(printf '%03i' $i)
66 {
67 test-tool genrandom "bar" 200 &&
68 test-tool genrandom "baz $iii" 50
69 } >wide_delta_$iii &&
70 {
71 test-tool genrandom "foo"$i 100 &&
72 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
73 test-tool genrandom "foo"$(( $i + 2 )) 100
74 } >deep_delta_$iii &&
75 {
76 echo $iii &&
77 test-tool genrandom "$iii" 8192
78 } >file_$iii &&
79 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
80 }
81
82 commit_and_list_objects () {
83 {
84 echo 101 &&
85 test-tool genrandom 100 8192;
86 } >file_101 &&
87 git update-index --add file_101 &&
88 tree=$(git write-tree) &&
89 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
90 {
91 echo $tree &&
92 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
93 } >obj-list &&
94 git reset --hard $commit
95 }
96
97 test_expect_success 'create objects' '
98 test_commit initial &&
99 for i in $(test_seq 1 5)
100 do
101 generate_objects $i || return 1
102 done &&
103 commit_and_list_objects
104 '
105
106 test_expect_success 'write midx with one v1 pack' '
107 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
108 test_when_finished rm $objdir/pack/test-$pack.pack \
109 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
110 git multi-pack-index --object-dir=$objdir write &&
111 midx_read_expect 1 18 4 $objdir
112 '
113
114 test_expect_success 'write midx with one v2 pack' '
115 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
116 git multi-pack-index --object-dir=$objdir write &&
117 midx_read_expect 1 18 4 $objdir
118 '
119
120 compare_results_with_midx "one v2 pack"
121
122 test_expect_success 'corrupt idx reports errors' '
123 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
124 mv $objdir/pack/$idx backup-$idx &&
125 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
126
127 # This is the minimum size for a sha-1 based .idx; this lets
128 # us pass perfunctory tests, but anything that actually opens and reads
129 # the idx file will complain.
130 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
131
132 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
133 test_grep "index unavailable" err
134 '
135
136 test_expect_success 'add more objects' '
137 for i in $(test_seq 6 10)
138 do
139 generate_objects $i || return 1
140 done &&
141 commit_and_list_objects
142 '
143
144 test_expect_success 'write midx with two packs' '
145 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
146 git multi-pack-index --object-dir=$objdir write &&
147 midx_read_expect 2 34 4 $objdir
148 '
149
150 compare_results_with_midx "two packs"
151
152 test_expect_success 'write midx with --stdin-packs' '
153 rm -fr $objdir/pack/multi-pack-index &&
154
155 idx="$(find $objdir/pack -name "test-2-*.idx")" &&
156 basename "$idx" >in &&
157
158 git multi-pack-index write --stdin-packs <in &&
159
160 test-tool read-midx $objdir | grep "\.idx$" >packs &&
161
162 test_cmp packs in
163 '
164
165 compare_results_with_midx "mixed mode (one pack + extra)"
166
167 test_expect_success 'write with no objects and preferred pack' '
168 test_when_finished "rm -rf empty" &&
169 git init empty &&
170 test_must_fail git -C empty multi-pack-index write \
171 --stdin-packs --preferred-pack=does-not-exist </dev/null 2>err &&
172 cat >expect <<-EOF &&
173 warning: unknown preferred pack: ${SQ}does-not-exist${SQ}
174 error: no pack files to index.
175 EOF
176 test_cmp expect err
177 '
178
179 test_expect_success 'write progress off for redirected stderr' '
180 git multi-pack-index --object-dir=$objdir write 2>err &&
181 test_line_count = 0 err
182 '
183
184 test_expect_success 'write force progress on for stderr' '
185 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --progress 2>err &&
186 test_file_not_empty err
187 '
188
189 test_expect_success 'write with the --no-progress option' '
190 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --no-progress 2>err &&
191 test_line_count = 0 err
192 '
193
194 test_expect_success 'add more packs' '
195 for j in $(test_seq 11 20)
196 do
197 generate_objects $j &&
198 commit_and_list_objects &&
199 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list || return 1
200 done
201 '
202
203 compare_results_with_midx "mixed mode (two packs + extra)"
204
205 test_expect_success 'write midx with twelve packs' '
206 git multi-pack-index --object-dir=$objdir write &&
207 midx_read_expect 12 74 4 $objdir
208 '
209
210 compare_results_with_midx "twelve packs"
211
212 test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
213 git init repo &&
214 git clone -s repo alternate &&
215
216 test_when_finished "rm -rf repo alternate" &&
217
218 (
219 cd repo &&
220 test_commit base &&
221 git repack -d
222 ) &&
223
224 ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
225 theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
226 touch "$ours" "$theirs" &&
227
228 (
229 cd alternate &&
230 git multi-pack-index --object-dir ../repo/.git/objects write
231 ) &&
232
233 # writing a midx in "repo" should not remove the .rev file in the
234 # alternate
235 test_path_is_file repo/.git/objects/pack/multi-pack-index &&
236 test_path_is_file $ours &&
237 test_path_is_missing $theirs
238 '
239
240 test_expect_success 'warn on improper hash version' '
241 git init --object-format=sha1 sha1 &&
242 (
243 cd sha1 &&
244 git config core.multiPackIndex true &&
245 test_commit 1 &&
246 git repack -a &&
247 git multi-pack-index write &&
248 mv .git/objects/pack/multi-pack-index ../mpi-sha1
249 ) &&
250 git init --object-format=sha256 sha256 &&
251 (
252 cd sha256 &&
253 git config core.multiPackIndex true &&
254 test_commit 1 &&
255 git repack -a &&
256 git multi-pack-index write &&
257 mv .git/objects/pack/multi-pack-index ../mpi-sha256
258 ) &&
259 (
260 cd sha1 &&
261 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
262 git log -1 2>err &&
263 test_grep "multi-pack-index hash version 2 does not match version 1" err
264 ) &&
265 (
266 cd sha256 &&
267 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
268 git log -1 2>err &&
269 test_grep "multi-pack-index hash version 1 does not match version 2" err
270 )
271 '
272
273 test_expect_success 'midx picks objects from preferred pack' '
274 test_when_finished rm -rf preferred.git &&
275 git init --bare preferred.git &&
276 (
277 cd preferred.git &&
278
279 a=$(echo "a" | git hash-object -w --stdin) &&
280 b=$(echo "b" | git hash-object -w --stdin) &&
281 c=$(echo "c" | git hash-object -w --stdin) &&
282
283 # Set up two packs, duplicating the object "B" at different
284 # offsets.
285 #
286 # Note that the "BC" pack (the one we choose as preferred) sorts
287 # lexically after the "AB" pack, meaning that omitting the
288 # --preferred-pack argument would cause this test to fail (since
289 # the MIDX code would select the copy of "b" in the "AB" pack).
290 git pack-objects objects/pack/test-AB <<-EOF &&
291 $a
292 $b
293 EOF
294 bc=$(git pack-objects objects/pack/test-BC <<-EOF
295 $b
296 $c
297 EOF
298 ) &&
299
300 git multi-pack-index --object-dir=objects \
301 write --preferred-pack=test-BC-$bc.idx 2>err &&
302 test_must_be_empty err &&
303
304 test-tool read-midx --show-objects objects >out &&
305
306 ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
307 cut -d" " -f1) &&
308 printf "%s %s\t./objects/pack/test-BC-%s.pack\n" \
309 "$b" "$ofs" "$bc" >expect &&
310 grep ^$b out >actual &&
311
312 test_cmp expect actual
313 )
314 '
315
316 test_expect_success 'preferred packs must be non-empty' '
317 test_when_finished rm -rf preferred.git &&
318 git init preferred.git &&
319 (
320 cd preferred.git &&
321
322 test_commit base &&
323 git repack -ad &&
324
325 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
326
327 test_must_fail git multi-pack-index write \
328 --preferred-pack=pack-$empty.pack 2>err &&
329 test_grep "with no objects" err
330 )
331 '
332
333 test_expect_success 'preferred pack from existing MIDX without bitmaps' '
334 git init preferred-without-bitmaps &&
335 (
336 cd preferred-without-bitmaps &&
337
338 test_commit one &&
339 pack="$(git pack-objects --all $objdir/pack/pack </dev/null)" &&
340
341 git multi-pack-index write &&
342
343 # make another pack so that the subsequent MIDX write
344 # has something to do
345 test_commit two &&
346 git repack -d &&
347
348 # write a new MIDX without bitmaps reusing the singular
349 # pack from the existing MIDX as the preferred pack in
350 # the new MIDX
351 git multi-pack-index write --preferred-pack=pack-$pack.pack
352 )
353 '
354
355 test_expect_success 'preferred pack cannot be determined without bitmap' '
356 test_when_finished "rm -fr preferred-can-be-queried" &&
357 git init preferred-can-be-queried &&
358 (
359 cd preferred-can-be-queried &&
360 test_commit initial &&
361 git repack -Adl --write-midx --no-write-bitmap-index &&
362 test_must_fail test-tool read-midx --preferred-pack .git/objects 2>err &&
363 test_grep "could not determine MIDX preferred pack" err &&
364 git repack -Adl --write-midx --write-bitmap-index &&
365 test-tool read-midx --preferred-pack .git/objects
366 )
367 '
368
369 test_midx_is_retained () {
370 test-tool chmtime =0 .git/objects/pack/multi-pack-index &&
371 ls -l .git/objects/pack/multi-pack-index >expect &&
372 git multi-pack-index write "$@" &&
373 ls -l .git/objects/pack/multi-pack-index >actual &&
374 test_cmp expect actual
375 }
376
377 test_midx_is_rewritten () {
378 test-tool chmtime =0 .git/objects/pack/multi-pack-index &&
379 ls -l .git/objects/pack/multi-pack-index >expect &&
380 git multi-pack-index write "$@" &&
381 ls -l .git/objects/pack/multi-pack-index >actual &&
382 ! test_cmp expect actual
383 }
384
385 test_expect_success 'up-to-date multi-pack-index is retained' '
386 test_when_finished "rm -fr midx-up-to-date" &&
387 git init midx-up-to-date &&
388 (
389 cd midx-up-to-date &&
390
391 # Write the initial pack that contains the most objects.
392 test_commit first &&
393 test_commit second &&
394 git repack -Ad --write-midx &&
395 test_midx_is_retained &&
396
397 # Writing a new bitmap index should cause us to regenerate the MIDX.
398 test_midx_is_rewritten --bitmap &&
399 test_midx_is_retained --bitmap &&
400
401 # Ensure that writing a new packfile causes us to rewrite the index.
402 test_commit incremental &&
403 git repack -d &&
404 test_midx_is_rewritten &&
405 test_midx_is_retained &&
406
407 for pack in .git/objects/pack/*.idx
408 do
409 basename "$pack" || exit 1
410 done >stdin &&
411 test_line_count = 2 stdin &&
412 test_midx_is_retained --stdin-packs <stdin &&
413 head -n1 stdin >stdin.trimmed &&
414 test_midx_is_rewritten --stdin-packs <stdin.trimmed
415 )
416 '
417
418 test_expect_success 'verify multi-pack-index success' '
419 git multi-pack-index verify --object-dir=$objdir
420 '
421
422 test_expect_success 'verify progress off for redirected stderr' '
423 git multi-pack-index verify --object-dir=$objdir 2>err &&
424 test_line_count = 0 err
425 '
426
427 test_expect_success 'verify force progress on for stderr' '
428 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
429 test_file_not_empty err
430 '
431
432 test_expect_success 'verify with the --no-progress option' '
433 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
434 test_line_count = 0 err
435 '
436
437 # usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
438 corrupt_midx_and_verify() {
439 POS=$1 &&
440 DATA="${2:-\0}" &&
441 OBJDIR=$3 &&
442 GREPSTR="$4" &&
443 COMMAND="$5" &&
444 if test -z "$COMMAND"
445 then
446 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
447 fi &&
448 FILE=$OBJDIR/pack/multi-pack-index &&
449 chmod a+w $FILE &&
450 test_when_finished mv midx-backup $FILE &&
451 cp $FILE midx-backup &&
452 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
453 test_must_fail $COMMAND 2>test_err &&
454 grep -v "^+" test_err >err &&
455 test_grep "$GREPSTR" err
456 }
457
458 test_expect_success 'verify bad signature' '
459 corrupt_midx_and_verify 0 "\00" $objdir \
460 "multi-pack-index signature"
461 '
462
463 NUM_OBJECTS=74
464 MIDX_BYTE_VERSION=4
465 MIDX_BYTE_OID_VERSION=5
466 MIDX_BYTE_CHUNK_COUNT=6
467 MIDX_HEADER_SIZE=12
468 MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
469 MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
470 MIDX_NUM_CHUNKS=5
471 MIDX_CHUNK_LOOKUP_WIDTH=12
472 MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
473 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
474 MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
475 MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
476 MIDX_OID_FANOUT_WIDTH=4
477 MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
478 MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
479 MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
480 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
481 MIDX_OFFSET_WIDTH=8
482 MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
483 MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
484
485 test_expect_success 'verify bad version' '
486 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
487 "multi-pack-index version"
488 '
489
490 test_expect_success 'verify bad OID version' '
491 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
492 "hash version"
493 '
494
495 test_expect_success 'verify truncated chunk count' '
496 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
497 "final chunk has non-zero id"
498 '
499
500 test_expect_success 'verify extended chunk count' '
501 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
502 "terminating chunk id appears earlier than expected"
503 '
504
505 test_expect_success 'verify missing required chunk' '
506 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
507 "required pack-name chunk missing"
508 '
509
510 test_expect_success 'verify invalid chunk offset' '
511 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
512 "improper chunk offset(s)"
513 '
514
515 test_expect_success 'verify missing pack' '
516 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
517 "failed to load pack"
518 '
519
520 test_expect_success 'verify oid fanout out of order' '
521 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
522 "oid fanout out of order"
523 '
524
525 test_expect_success 'verify oid lookup out of order' '
526 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
527 "oid lookup out of order"
528 '
529
530 test_expect_success 'verify incorrect pack-int-id' '
531 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
532 "bad pack-int-id"
533 '
534
535 test_expect_success 'verify incorrect offset' '
536 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
537 "incorrect object offset"
538 '
539
540 test_expect_success 'git-fsck incorrect offset' '
541 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
542 "incorrect object offset" \
543 "git -c core.multiPackIndex=true fsck" &&
544 test_unconfig core.multiPackIndex &&
545 test_must_fail git fsck &&
546 git -c core.multiPackIndex=false fsck
547 '
548
549 test_expect_success 'git fsck shows MIDX output with --progress' '
550 git fsck --progress 2>err &&
551 test_grep "Verifying OID order in multi-pack-index" err &&
552 test_grep "Verifying object offsets" err
553 '
554
555 test_expect_success 'git fsck suppresses MIDX output with --no-progress' '
556 git fsck --no-progress 2>err &&
557 test_grep ! "Verifying OID order in multi-pack-index" err &&
558 test_grep ! "Verifying object offsets" err
559 '
560
561 test_expect_success 'corrupt MIDX is not reused' '
562 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
563 "incorrect object offset" &&
564 git multi-pack-index write 2>err &&
565 test_grep checksum.mismatch err &&
566 git multi-pack-index verify
567 '
568
569 test_expect_success 'verify incorrect checksum' '
570 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
571 corrupt_midx_and_verify $pos \
572 "\377\377\377\377\377\377\377\377\377\377" \
573 $objdir "incorrect checksum"
574 '
575
576 test_expect_success 'git fsck --no-full checks multi-pack-index, --connectivity-only does not' '
577 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
578 corrupt_midx_and_verify $pos \
579 "\377\377\377\377\377\377\377\377\377\377" \
580 $objdir "incorrect checksum" &&
581
582 test_must_fail git fsck --no-full 2>err &&
583 test_grep "incorrect checksum" err &&
584
585 git fsck --connectivity-only 2>err &&
586 test_grep ! "incorrect checksum" err
587 '
588
589 test_expect_success 'setup for v1-specific fsck tests' '
590 git -c midx.version=1 multi-pack-index write
591 '
592
593 test_expect_success 'verify packnames out of order (v1)' '
594 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
595 "pack names out of order"
596 '
597
598 test_expect_success 'repack progress off for redirected stderr' '
599 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
600 test_line_count = 0 err
601 '
602
603 test_expect_success 'repack force progress on for stderr' '
604 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
605 test_file_not_empty err
606 '
607
608 test_expect_success 'repack with the --no-progress option' '
609 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
610 test_line_count = 0 err
611 '
612
613 test_expect_success 'repack removes multi-pack-index when deleting packs' '
614 test_path_is_file $objdir/pack/multi-pack-index &&
615 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
616 # multi-pack-index after repacking, but set "core.multiPackIndex" to
617 # true so that "git repack" can read the existing MIDX.
618 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
619 test_path_is_missing $objdir/pack/multi-pack-index
620 '
621
622 test_expect_success 'repack preserves multi-pack-index when creating packs' '
623 git init preserve &&
624 test_when_finished "rm -fr preserve" &&
625 (
626 cd preserve &&
627 packdir=.git/objects/pack &&
628 midx=$packdir/multi-pack-index &&
629
630 test_commit 1 &&
631 pack1=$(git pack-objects --all $packdir/pack) &&
632 touch $packdir/pack-$pack1.keep &&
633 test_commit 2 &&
634 pack2=$(git pack-objects --revs $packdir/pack) &&
635 touch $packdir/pack-$pack2.keep &&
636
637 git multi-pack-index write &&
638 cp $midx $midx.bak &&
639
640 cat >pack-input <<-EOF &&
641 HEAD
642 ^HEAD~1
643 EOF
644 test_commit 3 &&
645 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
646 test_commit 4 &&
647 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
648
649 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
650 ls -la $packdir &&
651 test_path_is_file $packdir/pack-$pack1.pack &&
652 test_path_is_file $packdir/pack-$pack2.pack &&
653 test_path_is_missing $packdir/pack-$pack3.pack &&
654 test_path_is_missing $packdir/pack-$pack4.pack &&
655 test_cmp_bin $midx.bak $midx
656 )
657 '
658
659 compare_results_with_midx "after repack"
660
661 test_expect_success 'multi-pack-index and pack-bitmap' '
662 git -c repack.writeBitmaps=true repack -ad &&
663 git multi-pack-index write &&
664 git rev-list --test-bitmap HEAD
665 '
666
667 test_expect_success 'multi-pack-index and alternates' '
668 git init --bare alt.git &&
669 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
670 echo content1 >file1 &&
671 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
672 git cat-file blob $altblob &&
673 git rev-list --all
674 '
675
676 compare_results_with_midx "with alternate (local midx)"
677
678 test_expect_success 'multi-pack-index in an alternate' '
679 mv .git/objects/pack/* alt.git/objects/pack &&
680 test_commit add_local_objects &&
681 git repack --local &&
682 git multi-pack-index write &&
683 midx_read_expect 1 3 4 $objdir &&
684 git reset --hard HEAD~1 &&
685 rm -f .git/objects/pack/*
686 '
687
688 compare_results_with_midx "with alternate (remote midx)"
689
690 # usage: corrupt_data <file> <pos> [<data>]
691 corrupt_data () {
692 file=$1
693 pos=$2
694 data="${3:-\0}"
695 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
696 }
697
698 # Force 64-bit offsets by manipulating the idx file.
699 # This makes the IDX file _incorrect_ so be careful to clean up after!
700 test_expect_success 'force some 64-bit offsets with pack-objects' '
701 mkdir objects64 &&
702 mkdir objects64/pack &&
703 for i in $(test_seq 1 11)
704 do
705 generate_objects 11 || return 1
706 done &&
707 commit_and_list_objects &&
708 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
709 idx64=objects64/pack/test-64-$pack64.idx &&
710 chmod u+w $idx64 &&
711 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
712 # objects64 is not a real repository, but can serve as an alternate
713 # anyway so we can write a MIDX into it
714 git init repo &&
715 test_when_finished "rm -fr repo" &&
716 (
717 cd repo &&
718 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
719 midx64=$(git multi-pack-index --object-dir=../objects64 write)
720 ) &&
721 midx_read_expect 1 63 5 "$(pwd)/objects64" " large-offsets"
722 '
723
724 test_expect_success 'verify multi-pack-index with 64-bit offsets' '
725 git multi-pack-index verify --object-dir=objects64
726 '
727
728 NUM_OBJECTS=63
729 MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
730 MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
731 MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
732 MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
733 MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
734
735 test_expect_success 'verify incorrect 64-bit offset' '
736 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
737 "incorrect object offset"
738 '
739
740 test_expect_success 'setup expire tests' '
741 mkdir dup &&
742 (
743 cd dup &&
744 git init &&
745 test-tool genrandom "data" 4096 >large_file.txt &&
746 git update-index --add large_file.txt &&
747 for i in $(test_seq 1 20)
748 do
749 test_commit $i || exit 1
750 done &&
751 git branch A HEAD &&
752 git branch B HEAD~8 &&
753 git branch C HEAD~13 &&
754 git branch D HEAD~16 &&
755 git branch E HEAD~18 &&
756 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
757 refs/heads/A
758 ^refs/heads/B
759 EOF
760 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
761 refs/heads/B
762 ^refs/heads/C
763 EOF
764 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
765 refs/heads/C
766 ^refs/heads/D
767 EOF
768 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
769 refs/heads/D
770 ^refs/heads/E
771 EOF
772 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
773 refs/heads/E
774 EOF
775 git multi-pack-index write &&
776 cp -r .git/objects/pack .git/objects/pack-backup
777 )
778 '
779
780 test_expect_success 'expire does not remove any packs' '
781 (
782 cd dup &&
783 ls .git/objects/pack >expect &&
784 git multi-pack-index expire &&
785 ls .git/objects/pack >actual &&
786 test_cmp expect actual
787 )
788 '
789
790 test_expect_success 'expire progress off for redirected stderr' '
791 (
792 cd dup &&
793 git multi-pack-index expire 2>err &&
794 test_line_count = 0 err
795 )
796 '
797
798 test_expect_success 'expire force progress on for stderr' '
799 (
800 cd dup &&
801 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
802 test_file_not_empty err
803 )
804 '
805
806 test_expect_success 'expire with the --no-progress option' '
807 (
808 cd dup &&
809 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
810 test_line_count = 0 err
811 )
812 '
813
814 test_expect_success 'expire removes unreferenced packs' '
815 (
816 cd dup &&
817 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
818 refs/heads/A
819 ^refs/heads/C
820 EOF
821 git multi-pack-index write &&
822 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
823 git multi-pack-index expire &&
824 ls .git/objects/pack >actual &&
825 test_cmp expect actual &&
826 ls .git/objects/pack/ | grep idx >expect-idx &&
827 test-tool read-midx .git/objects | grep idx >actual-midx &&
828 test_cmp expect-idx actual-midx &&
829 git multi-pack-index verify &&
830 git fsck
831 )
832 '
833
834 test_expect_success 'repack with minimum size does not alter existing packs' '
835 (
836 cd dup &&
837 rm -rf .git/objects/pack &&
838 mv .git/objects/pack-backup .git/objects/pack &&
839 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
840 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
841 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
842 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
843 ls .git/objects/pack >expect &&
844 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
845 git multi-pack-index repack --batch-size=$MINSIZE &&
846 ls .git/objects/pack >actual &&
847 test_cmp expect actual
848 )
849 '
850
851 test_expect_success 'repack respects repack.packKeptObjects=false' '
852 test_when_finished rm -f dup/.git/objects/pack/*keep &&
853 (
854 cd dup &&
855 ls .git/objects/pack/*idx >idx-list &&
856 test_line_count = 5 idx-list &&
857 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
858 test_line_count = 5 keep-list &&
859 for keep in $(cat keep-list)
860 do
861 touch $keep || return 1
862 done &&
863 git multi-pack-index repack --batch-size=0 &&
864 ls .git/objects/pack/*idx >idx-list &&
865 test_line_count = 5 idx-list &&
866 test-tool read-midx .git/objects | grep idx >midx-list &&
867 test_line_count = 5 midx-list &&
868 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
869 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
870 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
871 ls .git/objects/pack/*idx >idx-list &&
872 test_line_count = 5 idx-list &&
873 test-tool read-midx .git/objects | grep idx >midx-list &&
874 test_line_count = 5 midx-list
875 )
876 '
877
878 test_expect_success 'repack creates a new pack' '
879 (
880 cd dup &&
881 ls .git/objects/pack/*idx >idx-list &&
882 test_line_count = 5 idx-list &&
883 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
884 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
885 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
886 ls .git/objects/pack/*idx >idx-list &&
887 test_line_count = 6 idx-list &&
888 test-tool read-midx .git/objects | grep idx >midx-list &&
889 test_line_count = 6 midx-list
890 )
891 '
892
893 test_expect_success 'repack (all) ignores cruft pack' '
894 git init repo &&
895 test_when_finished "rm -fr repo" &&
896 (
897 cd repo &&
898
899 test_commit base &&
900 test_commit --no-tag unreachable &&
901
902 git reset --hard base &&
903 git reflog expire --all --expire=all &&
904 git repack --cruft -d &&
905
906 git multi-pack-index write &&
907
908 find $objdir/pack | sort >before &&
909 git multi-pack-index repack --batch-size=0 &&
910 find $objdir/pack | sort >after &&
911
912 test_cmp before after
913 )
914 '
915
916 test_expect_success 'repack (--batch-size) ignores cruft pack' '
917 git init repo &&
918 test_when_finished "rm -fr repo" &&
919 (
920 cd repo &&
921
922 test_commit_bulk 5 &&
923 test_commit --no-tag unreachable &&
924
925 git reset --hard HEAD^ &&
926 git reflog expire --all --expire=all &&
927 git repack --cruft -d &&
928
929 test_commit four &&
930
931 find $objdir/pack -type f -name "*.pack" | sort >before &&
932 git repack -d &&
933 find $objdir/pack -type f -name "*.pack" | sort >after &&
934
935 pack="$(comm -13 before after)" &&
936 test_file_size "$pack" >sz &&
937 # Set --batch-size to twice the size of the pack created
938 # in the previous step, since this is enough to
939 # accommodate it and the cruft pack.
940 #
941 # This means that the MIDX machinery *could* combine the
942 # new and cruft packs together.
943 #
944 # We ensure that it does not below.
945 batch="$((($(cat sz) * 2)))" &&
946
947 git multi-pack-index write &&
948
949 find $objdir/pack | sort >before &&
950 git multi-pack-index repack --batch-size=$batch &&
951 find $objdir/pack | sort >after &&
952
953 test_cmp before after
954 )
955 '
956
957 test_expect_success 'expire removes repacked packs' '
958 (
959 cd dup &&
960 ls -al .git/objects/pack/*pack &&
961 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
962 git multi-pack-index expire &&
963 ls -S .git/objects/pack/*pack >actual &&
964 test_cmp expect actual &&
965 test-tool read-midx .git/objects | grep idx >midx-list &&
966 test_line_count = 4 midx-list
967 )
968 '
969
970 test_expect_success 'expire works when adding new packs' '
971 (
972 cd dup &&
973 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
974 refs/heads/A
975 ^refs/heads/B
976 EOF
977 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
978 refs/heads/B
979 ^refs/heads/C
980 EOF
981 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
982 refs/heads/C
983 ^refs/heads/D
984 EOF
985 git multi-pack-index write &&
986 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
987 refs/heads/D
988 ^refs/heads/E
989 EOF
990 git multi-pack-index write &&
991 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
992 refs/heads/E
993 EOF
994 git multi-pack-index expire &&
995 ls .git/objects/pack/ | grep idx >expect &&
996 test-tool read-midx .git/objects | grep idx >actual &&
997 test_cmp expect actual &&
998 git multi-pack-index verify
999 )
1000 '
1001
1002 test_expect_success 'expire respects .keep files' '
1003 (
1004 cd dup &&
1005 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
1006 refs/heads/A
1007 EOF
1008 git multi-pack-index write &&
1009 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
1010 touch $PACKA.keep &&
1011 git multi-pack-index expire &&
1012 test_path_is_file $PACKA.idx &&
1013 test_path_is_file $PACKA.keep &&
1014 test_path_is_file $PACKA.pack &&
1015 test-tool read-midx .git/objects | grep idx >midx-list &&
1016 test_line_count = 2 midx-list
1017 )
1018 '
1019
1020 test_expect_success 'expiring unreferenced cruft pack retains pack' '
1021 git init repo &&
1022 test_when_finished "rm -fr repo" &&
1023 (
1024 cd repo &&
1025
1026 test_commit base &&
1027 test_commit --no-tag unreachable &&
1028 unreachable=$(git rev-parse HEAD) &&
1029
1030 git reset --hard base &&
1031 git reflog expire --all --expire=all &&
1032 git repack --cruft -d &&
1033 mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
1034
1035 echo "base..$unreachable" >in &&
1036 pack="$(git pack-objects --revs --delta-base-offset \
1037 $objdir/pack/pack <in)" &&
1038
1039 # Preferring the contents of "$pack" will leave the
1040 # cruft pack unreferenced (ie., none of the objects
1041 # contained in the cruft pack will have their MIDX copy
1042 # selected from the cruft pack).
1043 git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
1044 git multi-pack-index expire &&
1045
1046 test_path_is_file "$mtimes"
1047 )
1048 '
1049
1050 test_expect_success 'repack --batch-size=0 repacks everything' '
1051 cp -r dup dup2 &&
1052 (
1053 cd dup &&
1054 rm .git/objects/pack/*.keep &&
1055 ls .git/objects/pack/*idx >idx-list &&
1056 test_line_count = 2 idx-list &&
1057 git multi-pack-index repack --batch-size=0 &&
1058 ls .git/objects/pack/*idx >idx-list &&
1059 test_line_count = 3 idx-list &&
1060 test-tool read-midx .git/objects | grep idx >midx-list &&
1061 test_line_count = 3 midx-list &&
1062 git multi-pack-index expire &&
1063 ls -al .git/objects/pack/*idx >idx-list &&
1064 test_line_count = 1 idx-list &&
1065 git multi-pack-index repack --batch-size=0 &&
1066 ls -al .git/objects/pack/*idx >new-idx-list &&
1067 test_cmp idx-list new-idx-list
1068 )
1069 '
1070
1071 test_expect_success EXPENSIVE 'repack/expire with many packs' '
1072 cp -r dup many &&
1073 (
1074 cd many &&
1075
1076 for i in $(test_seq 1 100)
1077 do
1078 test_commit extra$i &&
1079 git maintenance run --task=loose-objects || return 1
1080 done &&
1081
1082 git multi-pack-index write &&
1083 git multi-pack-index repack &&
1084 git multi-pack-index expire
1085 )
1086 '
1087
1088 test_expect_success 'repack --batch-size=<large> repacks everything' '
1089 (
1090 cd dup2 &&
1091 rm .git/objects/pack/*.keep &&
1092 ls .git/objects/pack/*idx >idx-list &&
1093 test_line_count = 2 idx-list &&
1094 git multi-pack-index repack --batch-size=2000000 &&
1095 ls .git/objects/pack/*idx >idx-list &&
1096 test_line_count = 3 idx-list &&
1097 test-tool read-midx .git/objects | grep idx >midx-list &&
1098 test_line_count = 3 midx-list &&
1099 git multi-pack-index expire &&
1100 ls -al .git/objects/pack/*idx >idx-list &&
1101 test_line_count = 1 idx-list
1102 )
1103 '
1104
1105 test_expect_success 'repack/expire loop' '
1106 git init repack-expire &&
1107 test_when_finished "rm -fr repack-expire" &&
1108 (
1109 cd repack-expire &&
1110
1111 test_commit_bulk 5 &&
1112
1113 # Create three overlapping pack-files
1114 git rev-list --objects HEAD~3 >in-1 &&
1115 git rev-list --objects HEAD~4..HEAD~2 >in-2 &&
1116 git rev-list --objects HEAD~3..HEAD >in-3 &&
1117
1118 # Create disconnected blobs
1119 obj1=$(git hash-object -w in-1) &&
1120 obj2=$(git hash-object -w in-2) &&
1121 obj3=$(git hash-object -w in-3) &&
1122
1123 echo $obj2 >>in-2 &&
1124 echo $obj3 >>in-3 &&
1125
1126 for i in $(test_seq 3)
1127 do
1128 git pack-objects .git/objects/pack/test-$i <in-$i \
1129 || return 1
1130 done &&
1131
1132 rm -fr .git/objects/pack/pack-* &&
1133 git multi-pack-index write &&
1134
1135 for i in $(test_seq 3)
1136 do
1137 for file in $(ls .git/objects/pack/test-$i*)
1138 do
1139 test-tool chmtime =+$((3600*$i-25000)) $file || return 1
1140 done || return 1
1141 done &&
1142
1143 pack1=$(ls .git/objects/pack/test-1-*.pack) &&
1144 pack2=$(ls .git/objects/pack/test-2-*.pack) &&
1145 pack3=$(ls .git/objects/pack/test-3-*.pack) &&
1146
1147 # Prevent test-1 from being rewritten.
1148 touch "${pack1%.pack}.keep" &&
1149
1150 # This repack-expire loop should repack all non-kept packs
1151 # into a new pack and then delete the old packs.
1152 git multi-pack-index repack &&
1153 git multi-pack-index expire &&
1154
1155 test_path_is_missing $pack3 &&
1156 test_path_is_missing $pack2
1157 )
1158 '
1159
1160 test_expect_success 'load reverse index when missing .idx, .pack' '
1161 git init repo &&
1162 test_when_finished "rm -fr repo" &&
1163 (
1164 cd repo &&
1165
1166 git config core.multiPackIndex true &&
1167
1168 test_commit base &&
1169 git repack -ad &&
1170 git multi-pack-index write &&
1171
1172 git rev-parse HEAD >tip &&
1173 pack=$(ls .git/objects/pack/pack-*.pack) &&
1174 idx=$(ls .git/objects/pack/pack-*.idx) &&
1175
1176 mv $idx $idx.bak &&
1177 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1178
1179 mv $idx.bak $idx &&
1180
1181 mv $pack $pack.bak &&
1182 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1183
1184 test_must_fail git multi-pack-index write 2>err &&
1185 test_grep "could not load pack" err
1186 )
1187 '
1188
1189 test_expect_success 'usage shown without sub-command' '
1190 test_expect_code 129 git multi-pack-index 2>err &&
1191 test_grep ! "unrecognized subcommand" err
1192 '
1193
1194 test_expect_success 'complains when run outside of a repository' '
1195 nongit test_must_fail git multi-pack-index write 2>err &&
1196 test_grep "not a git repository" err
1197 '
1198
1199 test_expect_success 'repack with delta islands' '
1200 git init repo &&
1201 test_when_finished "rm -fr repo" &&
1202 (
1203 cd repo &&
1204
1205 test_commit first &&
1206 git repack &&
1207 test_commit second &&
1208 git repack &&
1209
1210 git multi-pack-index write &&
1211 git -c repack.useDeltaIslands=true multi-pack-index repack
1212 )
1213 '
1214
1215 corrupt_chunk () {
1216 midx=.git/objects/pack/multi-pack-index &&
1217 test_when_finished "rm -rf $midx" &&
1218 git repack -ad --write-midx &&
1219 corrupt_chunk_file $midx "$@"
1220 }
1221
1222 test_expect_success PERL_TEST_HELPERS 'reader notices too-small oid fanout chunk' '
1223 corrupt_chunk OIDF clear 00000000 &&
1224 test_must_fail git log 2>err &&
1225 cat >expect <<-\EOF &&
1226 error: multi-pack-index OID fanout is of the wrong size
1227 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1228 EOF
1229 test_cmp expect err
1230 '
1231
1232 test_expect_success PERL_TEST_HELPERS 'reader notices too-small oid lookup chunk' '
1233 corrupt_chunk OIDL clear 00000000 &&
1234 test_must_fail git log 2>err &&
1235 cat >expect <<-\EOF &&
1236 error: multi-pack-index OID lookup chunk is the wrong size
1237 fatal: multi-pack-index required OID lookup chunk missing or corrupted
1238 EOF
1239 test_cmp expect err
1240 '
1241
1242 test_expect_success PERL_TEST_HELPERS 'reader notices too-small pack names chunk' '
1243 # There is no NUL to terminate the name here, so the
1244 # chunk is too short.
1245 corrupt_chunk PNAM clear 70656666 &&
1246 test_must_fail git log 2>err &&
1247 cat >expect <<-\EOF &&
1248 fatal: multi-pack-index pack-name chunk is too short
1249 EOF
1250 test_cmp expect err
1251 '
1252
1253 test_expect_success PERL_TEST_HELPERS 'reader handles unaligned chunks' '
1254 # A 9-byte PNAM means all of the subsequent chunks
1255 # will no longer be 4-byte aligned, but it is still
1256 # a valid one-pack chunk on its own (it is "foo.pack\0").
1257 corrupt_chunk PNAM clear 666f6f2e7061636b00 &&
1258 git -c core.multipackindex=false log >expect.out &&
1259 git -c core.multipackindex=true log >out 2>err &&
1260 test_cmp expect.out out &&
1261 cat >expect.err <<-\EOF &&
1262 error: chunk id 4f494446 not 4-byte aligned
1263 EOF
1264 test_cmp expect.err err
1265 '
1266
1267 test_expect_success PERL_TEST_HELPERS 'reader notices too-small object offset chunk' '
1268 corrupt_chunk OOFF clear 00000000 &&
1269 test_must_fail git log 2>err &&
1270 cat >expect <<-\EOF &&
1271 error: multi-pack-index object offset chunk is the wrong size
1272 fatal: multi-pack-index required object offsets chunk missing or corrupted
1273 EOF
1274 test_cmp expect err
1275 '
1276
1277 test_expect_success PERL_TEST_HELPERS 'reader bounds-checks large offset table' '
1278 # re-use the objects64 dir here to cheaply get access to a midx
1279 # with large offsets.
1280 git init repo &&
1281 test_when_finished "rm -rf repo" &&
1282 (
1283 cd repo &&
1284 (cd ../objects64 && pwd) >.git/objects/info/alternates &&
1285 git multi-pack-index --object-dir=../objects64 write &&
1286 midx=../objects64/pack/multi-pack-index &&
1287 corrupt_chunk_file $midx LOFF clear &&
1288 # using only %(objectsize) is important here; see the commit
1289 # message for more details
1290 test_must_fail git cat-file --batch-all-objects \
1291 --batch-check="%(objectsize)" 2>err &&
1292 cat >expect <<-\EOF &&
1293 fatal: multi-pack-index large offset out of bounds
1294 EOF
1295 test_cmp expect err
1296 )
1297 '
1298
1299 test_expect_success PERL_TEST_HELPERS 'reader notices too-small revindex chunk' '
1300 # We only get a revindex with bitmaps (and likewise only
1301 # load it when they are asked for).
1302 test_config repack.writeBitmaps true &&
1303 corrupt_chunk RIDX clear 00000000 &&
1304 git -c core.multipackIndex=false rev-list \
1305 --all --use-bitmap-index >expect.out &&
1306 git -c core.multipackIndex=true rev-list \
1307 --all --use-bitmap-index >out 2>err &&
1308 test_cmp expect.out out &&
1309 cat >expect.err <<-\EOF &&
1310 error: multi-pack-index reverse-index chunk is the wrong size
1311 warning: multi-pack bitmap is missing required reverse index
1312 EOF
1313 test_cmp expect.err err
1314 '
1315
1316 test_expect_success PERL_TEST_HELPERS 'reader notices out-of-bounds fanout' '
1317 # This is similar to the out-of-bounds fanout test in t5318. The values
1318 # in adjacent entries should be large but not identical (they
1319 # are used as hi/lo starts for a binary search, which would then abort
1320 # immediately).
1321 corrupt_chunk OIDF 0 $(printf "%02x000000" $(test_seq 0 254)) &&
1322 test_must_fail git log 2>err &&
1323 cat >expect <<-\EOF &&
1324 error: oid fanout out of order: fanout[254] = fe000000 > 5c = fanout[255]
1325 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1326 EOF
1327 test_cmp expect err
1328 '
1329
1330 test_expect_success 'bitmapped packs are stored via the BTMP chunk' '
1331 test_when_finished "rm -fr repo" &&
1332 git init repo &&
1333 (
1334 cd repo &&
1335 git config set maintenance.auto false &&
1336
1337 for i in 1 2 3 4 5
1338 do
1339 test_commit "$i" &&
1340 git repack -d || return 1
1341 done &&
1342
1343 find $objdir/pack -type f -name "*.idx" | xargs -n 1 basename |
1344 sort >packs &&
1345
1346 git multi-pack-index write --stdin-packs <packs &&
1347 test_must_fail test-tool read-midx --bitmap $objdir 2>err &&
1348 cat >expect <<-\EOF &&
1349 error: MIDX does not contain the BTMP chunk
1350 EOF
1351 test_cmp expect err &&
1352
1353 git multi-pack-index write --stdin-packs --bitmap \
1354 --preferred-pack="$(head -n1 <packs)" <packs &&
1355 test-tool read-midx --bitmap $objdir >actual &&
1356 for i in $(test_seq $(wc -l <packs))
1357 do
1358 sed -ne "${i}s/\.idx$/\.pack/p" packs &&
1359 echo " bitmap_pos: $((($i - 1) * 3))" &&
1360 echo " bitmap_nr: 3" || return 1
1361 done >expect &&
1362 test_cmp expect actual
1363 )
1364 '
1365
1366 test_expect_success 'pack.preferBitmapTips interprets patterns as hierarchy' '
1367 git init repo &&
1368 test_when_finished "rm -fr repo" &&
1369 (
1370 cd repo &&
1371
1372 # Create enough commits that not all will receive bitmap
1373 # coverage even if they are all at the tip of some reference.
1374 test_commit_bulk --message="%s" 103 &&
1375 git log --format="create refs/tags/%s %H" HEAD >refs &&
1376 git update-ref --stdin <refs &&
1377
1378 # Create the bitmap via the MIDX.
1379 git repack -adb --write-midx &&
1380 test-tool bitmap list-commits | sort >commits-with-bitmap &&
1381
1382 # Verify that we have at least one commit that did not
1383 # receive a bitmap.
1384 git rev-list HEAD >commits.raw &&
1385 sort <commits.raw >commits &&
1386 comm -13 commits-with-bitmap commits >commits-wo-bitmap &&
1387 test_file_not_empty commits-wo-bitmap &&
1388 commit_id=$(head commits-wo-bitmap) &&
1389 ref_without_bitmap=$(git for-each-ref --points-at="$commit_id" --format="%(refname)") &&
1390
1391 # When passing the full refname we do not expect a bitmap to be
1392 # generated, as it should be interpreted as if a slash was
1393 # appended to the pattern.
1394 rm .git/objects/pack/multi-pack-index* &&
1395 git -c pack.preferBitmapTips="$ref_without_bitmap" repack -adb --write-midx &&
1396 test-tool bitmap list-commits >after &&
1397 test_grep ! "$commit_id" after &&
1398
1399 # But if we pass the parent directory of the ref we should see
1400 # a bitmap.
1401 ref_namespace=$(dirname "$ref_without_bitmap") &&
1402 rm .git/objects/pack/multi-pack-index* &&
1403 git -c pack.preferBitmapTips="$ref_namespace" repack -adb --write-midx &&
1404 test-tool bitmap list-commits >after &&
1405 test_grep "$commit_id" after
1406 )
1407 '
1408
1409 test_done