3 test_description
='exercise basic multi-pack bitmap functionality'
5 TEST_PASSES_SANITIZE_LEAK
=true
7 .
"${TEST_DIRECTORY}/lib-bitmap.sh"
9 # We'll be writing our own MIDX, so avoid getting confused by the
11 GIT_TEST_MULTI_PACK_INDEX
=0
12 GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL
=0
14 # This test exercise multi-pack bitmap functionality where the object order is
15 # stored and read from a special chunk within the MIDX, so use the default
17 sane_unset GIT_TEST_MIDX_WRITE_REV
18 sane_unset GIT_TEST_MIDX_READ_RIDX
20 bitmap_reuse_tests
() {
23 writeLookupTable
=false
28 "pack.writeBitmapLookupTable") writeLookupTable
=true
;;
32 test_expect_success
"setup pack reuse tests ($from -> $to)" '
37 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
38 test_commit_bulk 16 &&
41 git config core.multiPackIndex true &&
42 if test "MIDX" = "$from"
45 git multi-pack-index write --bitmap
52 test_expect_success
"build bitmap from existing ($from -> $to)" '
55 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
56 test_commit_bulk --id=further 16 &&
59 if test "MIDX" = "$to"
62 git multi-pack-index write --bitmap
69 test_expect_success
"verify resulting bitmaps ($from -> $to)" '
72 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
74 git rev-list --test-bitmap refs/tags/old-tip &&
75 git rev-list --test-bitmap refs/tags/new-tip
80 test_midx_bitmap_cases
() {
81 writeLookupTable
=false
82 writeBitmapLookupTable
=
87 "pack.writeBitmapLookupTable")
89 writeBitmapLookupTable
="$i"
94 test_expect_success
'setup test_repository' '
97 git config pack.writeBitmapLookupTable '"$writeLookupTable"'
102 bitmap_reuse_tests
'pack' 'MIDX' "$writeBitmapLookupTable"
103 bitmap_reuse_tests
'MIDX' 'pack' "$writeBitmapLookupTable"
104 bitmap_reuse_tests
'MIDX' 'MIDX' "$writeBitmapLookupTable"
106 test_expect_success
'missing object closure fails gracefully' '
109 test_when_finished "rm -fr repo" &&
112 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
115 test_commit packed &&
117 # Do not pass "--revs"; we want a pack without the "loose"
119 git pack-objects $objdir/pack/pack <<-EOF &&
120 $(git rev-parse packed)
123 test_must_fail git multi-pack-index write --bitmap 2>err &&
124 grep "doesn.t have full closure" err &&
125 test_path_is_missing $midx
129 midx_bitmap_partial_tests
131 test_expect_success
'removing a MIDX clears stale bitmaps' '
134 test_when_finished "rm -fr repo" &&
137 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
140 git multi-pack-index write --bitmap &&
142 # Write a MIDX and bitmap; remove the MIDX but leave the bitmap.
143 stale_bitmap=$midx-$(midx_checksum $objdir).bitmap &&
146 # Then write a new MIDX.
149 git multi-pack-index write --bitmap &&
151 test_path_is_file $midx &&
152 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
153 test_path_is_missing $stale_bitmap
157 test_expect_success
'pack.preferBitmapTips' '
159 test_when_finished "rm -fr repo" &&
162 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
164 test_commit_bulk --message="%s" 103 &&
166 git log --format="%H" >commits.raw &&
167 sort <commits.raw >commits &&
169 git log --format="create refs/tags/%s %H" HEAD >refs &&
170 git update-ref --stdin <refs &&
172 git multi-pack-index write --bitmap &&
173 test_path_is_file $midx &&
174 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
176 test-tool bitmap list-commits | sort >bitmaps &&
177 comm -13 bitmaps commits >before &&
178 test_line_count = 1 before &&
180 perl -ne "printf(\"create refs/tags/include/%d \", $.); print" \
181 <before | git update-ref --stdin &&
183 rm -fr $midx-$(midx_checksum $objdir).bitmap &&
186 git -c pack.preferBitmapTips=refs/tags/include \
187 multi-pack-index write --bitmap &&
188 test-tool bitmap list-commits | sort >bitmaps &&
189 comm -13 bitmaps commits >after &&
191 ! test_cmp before after
195 test_expect_success
'writing a bitmap with --refs-snapshot' '
197 test_when_finished "rm -fr repo" &&
200 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
205 git rev-parse one >snapshot &&
209 # First, write a MIDX which see both refs/tags/one and
210 # refs/tags/two (causing both of those commits to receive
212 git multi-pack-index write --bitmap &&
214 test_path_is_file $midx &&
215 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
217 test-tool bitmap list-commits | sort >bitmaps &&
218 grep "$(git rev-parse one)" bitmaps &&
219 grep "$(git rev-parse two)" bitmaps &&
221 rm -fr $midx-$(midx_checksum $objdir).bitmap &&
224 # Then again, but with a refs snapshot which only sees
226 git multi-pack-index write --bitmap --refs-snapshot=snapshot &&
228 test_path_is_file $midx &&
229 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
231 test-tool bitmap list-commits | sort >bitmaps &&
232 grep "$(git rev-parse one)" bitmaps &&
233 ! grep "$(git rev-parse two)" bitmaps
237 test_expect_success
'write a bitmap with --refs-snapshot (preferred tips)' '
239 test_when_finished "rm -fr repo" &&
242 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
244 test_commit_bulk --message="%s" 103 &&
246 git log --format="%H" >commits.raw &&
247 sort <commits.raw >commits &&
249 git log --format="create refs/tags/%s %H" HEAD >refs &&
250 git update-ref --stdin <refs &&
252 git multi-pack-index write --bitmap &&
253 test_path_is_file $midx &&
254 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
256 test-tool bitmap list-commits | sort >bitmaps &&
257 comm -13 bitmaps commits >before &&
258 test_line_count = 1 before &&
261 grep -vf before commits.raw &&
262 # mark missing commits as preferred
266 rm -fr $midx-$(midx_checksum $objdir).bitmap &&
269 git multi-pack-index write --bitmap --refs-snapshot=snapshot &&
270 test-tool bitmap list-commits | sort >bitmaps &&
271 comm -13 bitmaps commits >after &&
273 ! test_cmp before after
277 test_expect_success
'hash-cache values are propagated from pack bitmaps' '
280 test_when_finished "rm -fr repo" &&
283 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
289 test-tool bitmap dump-hashes >pack.raw &&
290 test_file_not_empty pack.raw &&
291 sort pack.raw >pack.hashes &&
295 git multi-pack-index write --bitmap &&
297 test-tool bitmap dump-hashes >midx.raw &&
298 sort midx.raw >midx.hashes &&
300 # ensure that every namehash in the pack bitmap can be found in
301 # the midx bitmap (i.e., that there are no oid-namehash pairs
302 # unique to the pack bitmap).
303 comm -23 pack.hashes midx.hashes >dropped.hashes &&
304 test_must_be_empty dropped.hashes
308 test_expect_success
'no .bitmap is written without any objects' '
311 test_when_finished "rm -fr repo" &&
314 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
316 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
321 git multi-pack-index write --bitmap --stdin-packs \
324 grep "bitmap without any objects" err &&
326 test_path_is_file $midx &&
327 test_path_is_missing $midx-$(midx_checksum $objdir).bitmap
331 test_expect_success
'graceful fallback when missing reverse index' '
334 test_when_finished "rm -fr repo" &&
337 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
341 # write a pack and MIDX bitmap containing base
343 git multi-pack-index write --bitmap &&
345 GIT_TEST_MIDX_READ_RIDX=0 \
346 git rev-list --use-bitmap-index HEAD 2>err &&
347 ! grep "ignoring extra bitmap file" err
352 test_midx_bitmap_cases
354 test_midx_bitmap_cases
"pack.writeBitmapLookupTable"
356 test_expect_success
'multi-pack-index write writes lookup table if enabled' '
359 test_when_finished "rm -fr repo" &&
363 git config pack.writeBitmapLookupTable true &&
365 GIT_TRACE2_EVENT="$(pwd)/trace" \
366 git multi-pack-index write --bitmap &&
367 grep "\"label\":\"writing_lookup_table\"" trace
371 test_expect_success
'preferred pack change with existing MIDX bitmap' '
372 git init preferred-pack-with-existing &&
374 cd preferred-pack-with-existing &&
379 git rev-list --objects --no-object-names base >p1.objects &&
380 git rev-list --objects --no-object-names other >p2.objects &&
382 p1="$(git pack-objects "$objdir/pack/pack" \
383 --delta-base-offset <p1.objects)" &&
384 p2="$(git pack-objects "$objdir/pack/pack" \
385 --delta-base-offset <p2.objects)" &&
387 # Generate a MIDX containing the first two packs,
388 # marking p1 as preferred, and ensure that it can be
389 # successfully cloned.
390 git multi-pack-index write --bitmap \
391 --preferred-pack="pack-$p1.pack" &&
392 test_path_is_file $midx &&
393 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
394 git clone --no-local . clone1 &&
396 # Then generate a new pack which sorts ahead of any
397 # existing pack (by tweaking the pack prefix).
399 git pack-objects --all --unpacked $objdir/pack/pack0 &&
401 # Generate a new MIDX which changes the preferred pack
402 # to a pack contained in the existing MIDX.
403 git multi-pack-index write --bitmap \
404 --preferred-pack="pack-$p2.pack" &&
405 test_path_is_file $midx &&
406 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
408 # When the above circumstances are met, the preferred
409 # pack should change appropriately and clones should
411 git clone --no-local . clone2
415 test_expect_success
'tagged commits are selected for bitmapping' '
418 test_when_finished "rm -fr repo" &&
422 test_commit --annotate base &&
425 # Remove refs/heads/main which points at the commit directly,
426 # leaving only a reference to the annotated tag.
427 git branch -M main &&
429 git branch -d main &&
431 git multi-pack-index write --bitmap &&
433 git rev-parse HEAD >want &&
434 test-tool bitmap list-commits >actual &&
435 grep $(cat want) actual
439 test_expect_success
'do not follow replace objects for MIDX bitmap' '
442 test_when_finished "rm -fr repo" &&
448 git checkout --orphan=orphan A &&
449 test_commit orphan &&
451 git replace A HEAD &&
452 git repack -ad --write-midx --write-bitmap-index &&
454 # generating reachability bitmaps with replace refs
455 # enabled will result in broken clones
456 git clone --no-local --bare . clone.git
462 printf "bogus" |
dd of
="$1" bs
=1 seek
="12" conv
=notrunc
465 test_expect_success
'git fsck correctly identifies good and bad bitmaps' '
467 test_when_finished rm -rf valid &&
469 test_commit_bulk 20 &&
472 # Move pack-bitmap aside so it is not deleted
474 packbitmap=$(ls .git/objects/pack/pack-*.bitmap) &&
475 mv "$packbitmap" "$packbitmap.bak" &&
477 test_commit_bulk 10 &&
478 git repack -b --write-midx &&
479 midxbitmap=$(ls .git/objects/pack/multi-pack-index-*.bitmap) &&
481 # Copy MIDX bitmap to backup. Copy pack bitmap from backup.
482 cp "$midxbitmap" "$midxbitmap.bak" &&
483 cp "$packbitmap.bak" "$packbitmap" &&
485 # fsck works at first
487 test_must_be_empty err &&
489 corrupt_file "$packbitmap" &&
490 test_must_fail git fsck 2>err &&
491 grep "bitmap file '\''$packbitmap'\'' has invalid checksum" err &&
493 cp "$packbitmap.bak" "$packbitmap" &&
494 corrupt_file "$midxbitmap" &&
495 test_must_fail git fsck 2>err &&
496 grep "bitmap file '\''$midxbitmap'\'' has invalid checksum" err &&
498 corrupt_file "$packbitmap" &&
499 test_must_fail git fsck 2>err &&
500 grep "bitmap file '\''$midxbitmap'\'' has invalid checksum" err &&
501 grep "bitmap file '\''$packbitmap'\'' has invalid checksum" err
504 test_expect_success
'corrupt MIDX with bitmap causes fallback' '
505 git init corrupt-midx-bitmap &&
507 cd corrupt-midx-bitmap &&
511 test_commit second &&
514 git multi-pack-index write --bitmap &&
515 checksum=$(midx_checksum $objdir) &&
516 for f in $midx $midx-$checksum.bitmap
518 mv $f $f.bak || return 1
521 # pack everything together, invalidating the MIDX
523 # then restore the now-stale MIDX
524 for f in $midx $midx-$checksum.bitmap
526 mv $f.bak $f || return 1
529 git rev-list --count --objects --use-bitmap-index HEAD >out 2>err &&
530 # should attempt opening the broken pack twice (once
531 # from the attempt to load it via the stale bitmap, and
532 # again when attempting to load it from the stale MIDX)
533 # before falling back to the non-MIDX case
534 test 2 -eq $(grep -c "could not open pack" err) &&
535 test 6 -eq $(cat out)
539 for allow_pack_reuse
in single multi
541 test_expect_success
"reading MIDX without BTMP chunk does not complain with $allow_pack_reuse pack reuse" '
542 test_when_finished "rm -rf midx-without-btmp" &&
543 git init midx-without-btmp &&
545 cd midx-without-btmp &&
546 test_commit initial &&
548 git repack -Adbl --write-bitmap-index --write-midx &&
549 GIT_TEST_MIDX_READ_BTMP=false git -c pack.allowPackReuse=$allow_pack_reuse \
550 pack-objects --all --use-bitmap-index --stdout </dev/null >/dev/null 2>err &&
551 test_must_be_empty err
556 test_expect_success
'remove one packfile between MIDX bitmap writes' '
557 git init remove-pack-between-writes &&
559 cd remove-pack-between-writes &&
565 # Create packs with the prefix "pack-A", "pack-B",
566 # "pack-C" to impose a lexicographic order on these
567 # packs so the pack being removed is always from the
569 packdir=.git/objects/pack &&
570 A="$(echo A | git pack-objects $packdir/pack-A --revs)" &&
571 B="$(echo B | git pack-objects $packdir/pack-B --revs)" &&
572 C="$(echo C | git pack-objects $packdir/pack-C --revs)" &&
574 git multi-pack-index write --bitmap &&
580 git multi-pack-index write --bitmap --stdin-packs <in &&
582 git rev-list --test-bitmap HEAD