t/helper: stop using `the_index`
[git.git] / t / t5310-pack-bitmaps.sh
blobd7fd71360e17bd2368562dd7c35644442475bb60
1 #!/bin/sh
3 test_description='exercise basic bitmap functionality'
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-bitmap.sh
8 # t5310 deals only with single-pack bitmaps, so don't write MIDX bitmaps in
9 # their place.
10 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
12 # Likewise, allow individual tests to control whether or not they use
13 # the boundary-based traversal.
14 sane_unset GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL
16 objpath () {
17 echo ".git/objects/$(echo "$1" | sed -e 's|\(..\)|\1/|')"
20 # show objects present in pack ($1 should be associated *.idx)
21 list_packed_objects () {
22 git show-index <"$1" >object-list &&
23 cut -d' ' -f2 object-list
26 # has_any pattern-file content-file
27 # tests whether content-file has any entry from pattern-file with entries being
28 # whole lines.
29 has_any () {
30 grep -Ff "$1" "$2"
33 test_bitmap_cases () {
34 writeLookupTable=false
35 for i in "$@"
37 case "$i" in
38 "pack.writeBitmapLookupTable") writeLookupTable=true;;
39 esac
40 done
42 test_expect_success 'setup test repository' '
43 rm -fr * .git &&
44 git init &&
45 git config pack.writeBitmapLookupTable '"$writeLookupTable"'
47 setup_bitmap_history
49 test_expect_success 'setup writing bitmaps during repack' '
50 git config repack.writeBitmaps true
53 test_expect_success 'full repack creates bitmaps' '
54 GIT_TRACE2_EVENT="$(pwd)/trace" \
55 git repack -ad &&
56 ls .git/objects/pack/ | grep bitmap >output &&
57 test_line_count = 1 output &&
58 grep "\"key\":\"num_selected_commits\",\"value\":\"106\"" trace &&
59 grep "\"key\":\"num_maximal_commits\",\"value\":\"107\"" trace
62 basic_bitmap_tests
64 test_expect_success 'pack-objects respects --local (non-local loose)' '
65 git init --bare alt.git &&
66 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
67 echo content1 >file1 &&
68 # non-local loose object which is not present in bitmapped pack
69 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
70 # non-local loose object which is also present in bitmapped pack
71 git cat-file blob $blob | GIT_DIR=alt.git git hash-object -w --stdin &&
72 git add file1 &&
73 test_tick &&
74 git commit -m commit_file1 &&
75 echo HEAD | git pack-objects --local --stdout --revs >1.pack &&
76 git index-pack 1.pack &&
77 list_packed_objects 1.idx >1.objects &&
78 printf "%s\n" "$altblob" "$blob" >nonlocal-loose &&
79 ! has_any nonlocal-loose 1.objects
82 test_expect_success 'pack-objects respects --honor-pack-keep (local non-bitmapped pack)' '
83 echo content2 >file2 &&
84 blob2=$(git hash-object -w file2) &&
85 git add file2 &&
86 test_tick &&
87 git commit -m commit_file2 &&
88 printf "%s\n" "$blob2" "$bitmaptip" >keepobjects &&
89 pack2=$(git pack-objects pack2 <keepobjects) &&
90 mv pack2-$pack2.* .git/objects/pack/ &&
91 >.git/objects/pack/pack2-$pack2.keep &&
92 rm $(objpath $blob2) &&
93 echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >2a.pack &&
94 git index-pack 2a.pack &&
95 list_packed_objects 2a.idx >2a.objects &&
96 ! has_any keepobjects 2a.objects
99 test_expect_success 'pack-objects respects --local (non-local pack)' '
100 mv .git/objects/pack/pack2-$pack2.* alt.git/objects/pack/ &&
101 echo HEAD | git pack-objects --local --stdout --revs >2b.pack &&
102 git index-pack 2b.pack &&
103 list_packed_objects 2b.idx >2b.objects &&
104 ! has_any keepobjects 2b.objects
107 test_expect_success 'pack-objects respects --honor-pack-keep (local bitmapped pack)' '
108 ls .git/objects/pack/ | grep bitmap >output &&
109 test_line_count = 1 output &&
110 packbitmap=$(basename $(cat output) .bitmap) &&
111 list_packed_objects .git/objects/pack/$packbitmap.idx >packbitmap.objects &&
112 test_when_finished "rm -f .git/objects/pack/$packbitmap.keep" &&
113 >.git/objects/pack/$packbitmap.keep &&
114 echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >3a.pack &&
115 git index-pack 3a.pack &&
116 list_packed_objects 3a.idx >3a.objects &&
117 ! has_any packbitmap.objects 3a.objects
120 test_expect_success 'pack-objects respects --local (non-local bitmapped pack)' '
121 mv .git/objects/pack/$packbitmap.* alt.git/objects/pack/ &&
122 rm -f .git/objects/pack/multi-pack-index &&
123 test_when_finished "mv alt.git/objects/pack/$packbitmap.* .git/objects/pack/" &&
124 echo HEAD | git pack-objects --local --stdout --revs >3b.pack &&
125 git index-pack 3b.pack &&
126 list_packed_objects 3b.idx >3b.objects &&
127 ! has_any packbitmap.objects 3b.objects
130 test_expect_success 'pack-objects to file can use bitmap' '
131 # make sure we still have 1 bitmap index from previous tests
132 ls .git/objects/pack/ | grep bitmap >output &&
133 test_line_count = 1 output &&
134 # verify equivalent packs are generated with/without using bitmap index
135 packasha1=$(git pack-objects --no-use-bitmap-index --all packa </dev/null) &&
136 packbsha1=$(git pack-objects --use-bitmap-index --all packb </dev/null) &&
137 list_packed_objects packa-$packasha1.idx >packa.objects &&
138 list_packed_objects packb-$packbsha1.idx >packb.objects &&
139 test_cmp packa.objects packb.objects
142 test_expect_success 'full repack, reusing previous bitmaps' '
143 git repack -ad &&
144 ls .git/objects/pack/ | grep bitmap >output &&
145 test_line_count = 1 output
148 test_expect_success 'fetch (full bitmap)' '
149 git --git-dir=clone.git fetch origin second:second &&
150 git rev-parse HEAD >expect &&
151 git --git-dir=clone.git rev-parse HEAD >actual &&
152 test_cmp expect actual
155 test_expect_success 'create objects for missing-HAVE tests' '
156 blob=$(echo "missing have" | git hash-object -w --stdin) &&
157 tree=$(printf "100644 blob $blob\tfile\n" | git mktree) &&
158 parent=$(echo parent | git commit-tree $tree) &&
159 commit=$(echo commit | git commit-tree $tree -p $parent) &&
160 cat >revs <<-EOF
161 HEAD
162 ^HEAD^
163 ^$commit
167 test_expect_success 'pack-objects respects --incremental' '
168 cat >revs2 <<-EOF &&
169 HEAD
170 $commit
172 git pack-objects --incremental --stdout --revs <revs2 >4.pack &&
173 git index-pack 4.pack &&
174 list_packed_objects 4.idx >4.objects &&
175 test_line_count = 4 4.objects &&
176 git rev-list --objects $commit >revlist &&
177 cut -d" " -f1 revlist |sort >objects &&
178 test_cmp 4.objects objects
181 test_expect_success 'pack with missing blob' '
182 rm $(objpath $blob) &&
183 git pack-objects --stdout --revs <revs >/dev/null
186 test_expect_success 'pack with missing tree' '
187 rm $(objpath $tree) &&
188 git pack-objects --stdout --revs <revs >/dev/null
191 test_expect_success 'pack with missing parent' '
192 rm $(objpath $parent) &&
193 git pack-objects --stdout --revs <revs >/dev/null
196 test_expect_success JGIT,SHA1 'we can read jgit bitmaps' '
197 git clone --bare . compat-jgit.git &&
199 cd compat-jgit.git &&
200 rm -f objects/pack/*.bitmap &&
201 jgit gc &&
202 git rev-list --test-bitmap HEAD
206 test_expect_success JGIT,SHA1 'jgit can read our bitmaps' '
207 git clone --bare . compat-us.git &&
209 cd compat-us.git &&
210 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
211 git repack -adb &&
212 # jgit gc will barf if it does not like our bitmaps
213 jgit gc
217 test_expect_success 'splitting packs does not generate bogus bitmaps' '
218 test-tool genrandom foo $((1024 * 1024)) >rand &&
219 git add rand &&
220 git commit -m "commit with big file" &&
221 git -c pack.packSizeLimit=500k repack -adb &&
222 git init --bare no-bitmaps.git &&
223 git -C no-bitmaps.git fetch .. HEAD
226 test_expect_success 'set up reusable pack' '
227 rm -f .git/objects/pack/*.keep &&
228 git repack -adb &&
229 reusable_pack () {
230 git for-each-ref --format="%(objectname)" |
231 git pack-objects --delta-base-offset --revs --stdout "$@"
235 test_expect_success 'pack reuse respects --honor-pack-keep' '
236 test_when_finished "rm -f .git/objects/pack/*.keep" &&
237 for i in .git/objects/pack/*.pack
239 >${i%.pack}.keep || return 1
240 done &&
241 reusable_pack --honor-pack-keep >empty.pack &&
242 git index-pack empty.pack &&
243 git show-index <empty.idx >actual &&
244 test_must_be_empty actual
247 test_expect_success 'pack reuse respects --local' '
248 mv .git/objects/pack/* alt.git/objects/pack/ &&
249 test_when_finished "mv alt.git/objects/pack/* .git/objects/pack/" &&
250 reusable_pack --local >empty.pack &&
251 git index-pack empty.pack &&
252 git show-index <empty.idx >actual &&
253 test_must_be_empty actual
256 test_expect_success 'pack reuse respects --incremental' '
257 reusable_pack --incremental >empty.pack &&
258 git index-pack empty.pack &&
259 git show-index <empty.idx >actual &&
260 test_must_be_empty actual
263 test_expect_success 'truncated bitmap fails gracefully (ewah)' '
264 test_config pack.writebitmaphashcache false &&
265 test_config pack.writebitmaplookuptable false &&
266 git repack -ad &&
267 git rev-list --use-bitmap-index --count --all >expect &&
268 bitmap=$(ls .git/objects/pack/*.bitmap) &&
269 test_when_finished "rm -f $bitmap" &&
270 test_copy_bytes 256 <$bitmap >$bitmap.tmp &&
271 mv -f $bitmap.tmp $bitmap &&
272 git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
273 test_cmp expect actual &&
274 test_grep corrupt.ewah.bitmap stderr
277 test_expect_success 'truncated bitmap fails gracefully (cache)' '
278 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
279 git repack -ad &&
280 git rev-list --use-bitmap-index --count --all >expect &&
281 bitmap=$(ls .git/objects/pack/*.bitmap) &&
282 test_when_finished "rm -f $bitmap" &&
283 test_copy_bytes 512 <$bitmap >$bitmap.tmp &&
284 mv -f $bitmap.tmp $bitmap &&
285 git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
286 test_cmp expect actual &&
287 test_grep corrupted.bitmap.index stderr
290 # Create a state of history with these properties:
292 # - refs that allow a client to fetch some new history, while sharing some old
293 # history with the server; we use branches delta-reuse-old and
294 # delta-reuse-new here
296 # - the new history contains an object that is stored on the server as a delta
297 # against a base that is in the old history
299 # - the base object is not immediately reachable from the tip of the old
300 # history; finding it would involve digging down through history we know the
301 # other side has
303 # This should result in a state where fetching from old->new would not
304 # traditionally reuse the on-disk delta (because we'd have to dig to realize
305 # that the client has it), but we will do so if bitmaps can tell us cheaply
306 # that the other side has it.
307 test_expect_success 'set up thin delta-reuse parent' '
308 # This first commit contains the buried base object.
309 test-tool genrandom delta 16384 >file &&
310 git add file &&
311 git commit -m "delta base" &&
312 base=$(git rev-parse --verify HEAD:file) &&
314 # These intermediate commits bury the base back in history.
315 # This becomes the "old" state.
316 for i in 1 2 3 4 5
318 echo $i >file &&
319 git commit -am "intermediate $i" || return 1
320 done &&
321 git branch delta-reuse-old &&
323 # And now our new history has a delta against the buried base. Note
324 # that this must be smaller than the original file, since pack-objects
325 # prefers to create deltas from smaller objects to larger.
326 test-tool genrandom delta 16300 >file &&
327 git commit -am "delta result" &&
328 delta=$(git rev-parse --verify HEAD:file) &&
329 git branch delta-reuse-new &&
331 # Repack with bitmaps and double check that we have the expected delta
332 # relationship.
333 git repack -adb &&
334 have_delta $delta $base
337 # Now we can sanity-check the non-bitmap behavior (that the server is not able
338 # to reuse the delta). This isn't strictly something we care about, so this
339 # test could be scrapped in the future. But it makes sure that the next test is
340 # actually triggering the feature we want.
342 # Note that our tools for working with on-the-wire "thin" packs are limited. So
343 # we actually perform the fetch, retain the resulting pack, and inspect the
344 # result.
345 test_expect_success 'fetch without bitmaps ignores delta against old base' '
346 test_config pack.usebitmaps false &&
347 test_when_finished "rm -rf client.git" &&
348 git init --bare client.git &&
350 cd client.git &&
351 git config transfer.unpackLimit 1 &&
352 git fetch .. delta-reuse-old:delta-reuse-old &&
353 git fetch .. delta-reuse-new:delta-reuse-new &&
354 have_delta $delta $ZERO_OID
358 # And do the same for the bitmap case, where we do expect to find the delta.
359 test_expect_success 'fetch with bitmaps can reuse old base' '
360 test_config pack.usebitmaps true &&
361 test_when_finished "rm -rf client.git" &&
362 git init --bare client.git &&
364 cd client.git &&
365 git config transfer.unpackLimit 1 &&
366 git fetch .. delta-reuse-old:delta-reuse-old &&
367 git fetch .. delta-reuse-new:delta-reuse-new &&
368 have_delta $delta $base
372 test_expect_success 'pack.preferBitmapTips' '
373 git init repo &&
374 test_when_finished "rm -fr repo" &&
376 cd repo &&
377 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
379 # create enough commits that not all are receive bitmap
380 # coverage even if they are all at the tip of some reference.
381 test_commit_bulk --message="%s" 103 &&
383 git rev-list HEAD >commits.raw &&
384 sort <commits.raw >commits &&
386 git log --format="create refs/tags/%s %H" HEAD >refs &&
387 git update-ref --stdin <refs &&
389 git repack -adb &&
390 test-tool bitmap list-commits | sort >bitmaps &&
392 # remember which commits did not receive bitmaps
393 comm -13 bitmaps commits >before &&
394 test_file_not_empty before &&
396 # mark the commits which did not receive bitmaps as preferred,
397 # and generate the bitmap again
398 perl -pe "s{^}{create refs/tags/include/$. }" <before |
399 git update-ref --stdin &&
400 git -c pack.preferBitmapTips=refs/tags/include repack -adb &&
402 # finally, check that the commit(s) without bitmap coverage
403 # are not the same ones as before
404 test-tool bitmap list-commits | sort >bitmaps &&
405 comm -13 bitmaps commits >after &&
407 ! test_cmp before after
411 test_expect_success 'pack.preferBitmapTips' '
412 git init repo &&
413 test_when_finished "rm -rf repo" &&
415 cd repo &&
416 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
417 test_commit_bulk --message="%s" 103 &&
419 cat >>.git/config <<-\EOF &&
420 [pack]
421 preferBitmapTips
423 cat >expect <<-\EOF &&
424 error: missing value for '\''pack.preferbitmaptips'\''
426 git repack -adb 2>actual &&
427 test_cmp expect actual
431 test_expect_success 'complains about multiple pack bitmaps' '
432 rm -fr repo &&
433 git init repo &&
434 test_when_finished "rm -fr repo" &&
436 cd repo &&
437 git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
439 test_commit base &&
441 git repack -adb &&
442 bitmap="$(ls .git/objects/pack/pack-*.bitmap)" &&
443 mv "$bitmap" "$bitmap.bak" &&
445 test_commit other &&
446 git repack -ab &&
448 mv "$bitmap.bak" "$bitmap" &&
450 find .git/objects/pack -type f -name "*.pack" >packs &&
451 find .git/objects/pack -type f -name "*.bitmap" >bitmaps &&
452 test_line_count = 2 packs &&
453 test_line_count = 2 bitmaps &&
455 GIT_TRACE2_EVENT=$(pwd)/trace2.txt git rev-list --use-bitmap-index HEAD &&
456 grep "opened bitmap" trace2.txt &&
457 grep "ignoring extra bitmap" trace2.txt
462 test_bitmap_cases
464 GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL=1
465 export GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL
467 test_bitmap_cases
469 sane_unset GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL
471 test_expect_success 'incremental repack fails when bitmaps are requested' '
472 test_commit more-1 &&
473 test_must_fail git repack -d 2>err &&
474 test_grep "Incremental repacks are incompatible with bitmap" err
477 test_expect_success 'incremental repack can disable bitmaps' '
478 test_commit more-2 &&
479 git repack -d --no-write-bitmap-index
482 test_expect_success 'boundary-based traversal is used when requested' '
483 git repack -a -d --write-bitmap-index &&
485 for argv in \
486 "git -c pack.useBitmapBoundaryTraversal=true" \
487 "git -c feature.experimental=true" \
488 "GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL=1 git"
490 eval "GIT_TRACE2_EVENT=1 $argv rev-list --objects \
491 --use-bitmap-index second..other 2>perf" &&
492 grep "\"region_enter\".*\"label\":\"haves/boundary\"" perf ||
493 return 1
494 done &&
496 for argv in \
497 "git -c pack.useBitmapBoundaryTraversal=false" \
498 "git -c feature.experimental=true -c pack.useBitmapBoundaryTraversal=false" \
499 "GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL=0 git -c pack.useBitmapBoundaryTraversal=true" \
500 "GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL=0 git -c feature.experimental=true"
502 eval "GIT_TRACE2_EVENT=1 $argv rev-list --objects \
503 --use-bitmap-index second..other 2>perf" &&
504 grep "\"region_enter\".*\"label\":\"haves/classic\"" perf ||
505 return 1
506 done
509 test_bitmap_cases "pack.writeBitmapLookupTable"
511 test_expect_success 'verify writing bitmap lookup table when enabled' '
512 GIT_TRACE2_EVENT="$(pwd)/trace2" \
513 git repack -ad &&
514 grep "\"label\":\"writing_lookup_table\"" trace2
517 test_expect_success 'truncated bitmap fails gracefully (lookup table)' '
518 test_config pack.writebitmaphashcache false &&
519 git repack -adb &&
520 git rev-list --use-bitmap-index --count --all >expect &&
521 bitmap=$(ls .git/objects/pack/*.bitmap) &&
522 test_when_finished "rm -f $bitmap" &&
523 test_copy_bytes 512 <$bitmap >$bitmap.tmp &&
524 mv -f $bitmap.tmp $bitmap &&
525 git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
526 test_cmp expect actual &&
527 test_grep corrupted.bitmap.index stderr
530 test_done