interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t5319-multi-pack-index.sh

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