The eleventh batch
[git/gitster.git] / t / t5326-multi-pack-bitmaps.sh
blob6eaa692f33b5c42bf24459377bd31309f7b03f00
1 #!/bin/sh
3 test_description='exercise basic multi-pack bitmap functionality'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "${TEST_DIRECTORY}/lib-bitmap.sh"
9 # We'll be writing our own MIDX, so avoid getting confused by the
10 # automatic ones.
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
16 # behavior here.
17 sane_unset GIT_TEST_MIDX_WRITE_REV
18 sane_unset GIT_TEST_MIDX_READ_RIDX
20 bitmap_reuse_tests() {
21 from=$1
22 to=$2
23 writeLookupTable=false
25 for i in $3-${$#}
27 case $i in
28 "pack.writeBitmapLookupTable") writeLookupTable=true;;
29 esac
30 done
32 test_expect_success "setup pack reuse tests ($from -> $to)" '
33 rm -fr repo &&
34 git init repo &&
36 cd repo &&
37 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
38 test_commit_bulk 16 &&
39 git tag old-tip &&
41 git config core.multiPackIndex true &&
42 if test "MIDX" = "$from"
43 then
44 git repack -Ad &&
45 git multi-pack-index write --bitmap
46 else
47 git repack -Adb
52 test_expect_success "build bitmap from existing ($from -> $to)" '
54 cd repo &&
55 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
56 test_commit_bulk --id=further 16 &&
57 git tag new-tip &&
59 if test "MIDX" = "$to"
60 then
61 git repack -d &&
62 git multi-pack-index write --bitmap
63 else
64 git repack -Adb
69 test_expect_success "verify resulting bitmaps ($from -> $to)" '
71 cd repo &&
72 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
73 git for-each-ref &&
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=
84 for i in "$@"
86 case $i in
87 "pack.writeBitmapLookupTable")
88 writeLookupTable=true
89 writeBitmapLookupTable="$i"
91 esac
92 done
94 test_expect_success 'setup test_repository' '
95 rm -rf * .git &&
96 git init &&
97 git config pack.writeBitmapLookupTable '"$writeLookupTable"'
100 midx_bitmap_core
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' '
107 rm -fr repo &&
108 git init repo &&
109 test_when_finished "rm -fr repo" &&
111 cd repo &&
112 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
114 test_commit loose &&
115 test_commit packed &&
117 # Do not pass "--revs"; we want a pack without the "loose"
118 # commit.
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' '
132 rm -fr repo &&
133 git init repo &&
134 test_when_finished "rm -fr repo" &&
136 cd repo &&
137 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
138 test_commit base &&
139 git repack &&
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 &&
144 rm $midx &&
146 # Then write a new MIDX.
147 test_commit new &&
148 git repack &&
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' '
158 git init repo &&
159 test_when_finished "rm -fr repo" &&
161 cd 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 &&
184 rm -fr $midx &&
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' '
196 git init repo &&
197 test_when_finished "rm -fr repo" &&
199 cd repo &&
200 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
202 test_commit one &&
203 test_commit two &&
205 git rev-parse one >snapshot &&
207 git repack -ad &&
209 # First, write a MIDX which see both refs/tags/one and
210 # refs/tags/two (causing both of those commits to receive
211 # bitmaps).
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 &&
222 rm -fr $midx &&
224 # Then again, but with a refs snapshot which only sees
225 # refs/tags/one.
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)' '
238 git init repo &&
239 test_when_finished "rm -fr repo" &&
241 cd 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
263 sed "s/^/+/" before
264 ) >snapshot &&
266 rm -fr $midx-$(midx_checksum $objdir).bitmap &&
267 rm -fr $midx &&
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' '
278 rm -fr repo &&
279 git init repo &&
280 test_when_finished "rm -fr repo" &&
282 cd repo &&
283 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
285 test_commit base &&
286 test_commit base2 &&
287 git repack -adb &&
289 test-tool bitmap dump-hashes >pack.raw &&
290 test_file_not_empty pack.raw &&
291 sort pack.raw >pack.hashes &&
293 test_commit new &&
294 git repack &&
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' '
309 rm -fr repo &&
310 git init repo &&
311 test_when_finished "rm -fr repo" &&
313 cd repo &&
314 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
316 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
317 cat >packs <<-EOF &&
318 pack-$empty.idx
321 git multi-pack-index write --bitmap --stdin-packs \
322 <packs 2>err &&
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' '
332 rm -fr repo &&
333 git init repo &&
334 test_when_finished "rm -fr repo" &&
336 cd repo &&
337 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
339 test_commit base &&
341 # write a pack and MIDX bitmap containing base
342 git repack -adb &&
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' '
357 rm -fr repo &&
358 git init repo &&
359 test_when_finished "rm -fr repo" &&
361 cd repo &&
362 test_commit base &&
363 git config pack.writeBitmapLookupTable true &&
364 git repack -ad &&
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 &&
376 test_commit base &&
377 test_commit other &&
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).
398 test_commit foo &&
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
410 # (still) succeed.
411 git clone --no-local . clone2
415 test_expect_success 'tagged commits are selected for bitmapping' '
416 rm -fr repo &&
417 git init repo &&
418 test_when_finished "rm -fr repo" &&
420 cd repo &&
422 test_commit --annotate base &&
423 git repack -d &&
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 &&
428 git checkout base &&
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' '
440 rm -fr repo &&
441 git init repo &&
442 test_when_finished "rm -fr repo" &&
444 cd repo &&
446 test_commit A &&
447 test_commit B &&
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
460 corrupt_file () {
461 chmod a+w "$1" &&
462 printf "bogus" | dd of="$1" bs=1 seek="12" conv=notrunc
465 test_expect_success 'git fsck correctly identifies good and bad bitmaps' '
466 git init valid &&
467 test_when_finished rm -rf valid &&
469 test_commit_bulk 20 &&
470 git repack -adbf &&
472 # Move pack-bitmap aside so it is not deleted
473 # in next repack.
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
486 git fsck 2>err &&
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 &&
509 test_commit first &&
510 git repack -d &&
511 test_commit second &&
512 git repack -d &&
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
519 done &&
521 # pack everything together, invalidating the MIDX
522 git repack -ad &&
523 # then restore the now-stale MIDX
524 for f in $midx $midx-$checksum.bitmap
526 mv $f.bak $f || return 1
527 done &&
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
554 done
556 test_expect_success 'remove one packfile between MIDX bitmap writes' '
557 git init remove-pack-between-writes &&
559 cd remove-pack-between-writes &&
561 test_commit A &&
562 test_commit B &&
563 test_commit C &&
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
568 # middle.
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 &&
576 cat >in <<-EOF &&
577 pack-A-$A.idx
578 pack-C-$C.idx
580 git multi-pack-index write --bitmap --stdin-packs <in &&
582 git rev-list --test-bitmap HEAD
586 test_done