Merge branch 'ma/sha1-is-a-hash'
[git] / t / t5310-pack-bitmaps.sh
1 #!/bin/sh
2
3 test_description='exercise basic bitmap functionality'
4 . ./test-lib.sh
5
6 objpath () {
7         echo ".git/objects/$(echo "$1" | sed -e 's|\(..\)|\1/|')"
8 }
9
10 # show objects present in pack ($1 should be associated *.idx)
11 list_packed_objects () {
12         git show-index <"$1" >object-list &&
13         cut -d' ' -f2 object-list
14 }
15
16 # has_any pattern-file content-file
17 # tests whether content-file has any entry from pattern-file with entries being
18 # whole lines.
19 has_any () {
20         grep -Ff "$1" "$2"
21 }
22
23 # To ensure the logic for "maximal commits" is exercised, make
24 # the repository a bit more complicated.
25 #
26 #    other                         second
27 #      *                             *
28 # (99 commits)                  (99 commits)
29 #      *                             *
30 #      |\                           /|
31 #      | * octo-other  octo-second * |
32 #      |/|\_________  ____________/|\|
33 #      | \          \/  __________/  |
34 #      |  | ________/\ /             |
35 #      *  |/          * merge-right  *
36 #      | _|__________/ \____________ |
37 #      |/ |                         \|
38 # (l1) *  * merge-left               * (r1)
39 #      | / \________________________ |
40 #      |/                           \|
41 # (l2) *                             * (r2)
42 #       \___________________________ |
43 #                                   \|
44 #                                    * (base)
45 #
46 # We only push bits down the first-parent history, which
47 # makes some of these commits unimportant!
48 #
49 # The important part for the maximal commit algorithm is how
50 # the bitmasks are extended. Assuming starting bit positions
51 # for second (bit 0) and other (bit 1), the bitmasks at the
52 # end should be:
53 #
54 #      second: 1       (maximal, selected)
55 #       other: 01      (maximal, selected)
56 #      (base): 11 (maximal)
57 #
58 # This complicated history was important for a previous
59 # version of the walk that guarantees never walking a
60 # commit multiple times. That goal might be important
61 # again, so preserve this complicated case. For now, this
62 # test will guarantee that the bitmaps are computed
63 # correctly, even with the repeat calculations.
64
65 test_expect_success 'setup repo with moderate-sized history' '
66         test_commit_bulk --id=file 10 &&
67         git branch -M second &&
68         git checkout -b other HEAD~5 &&
69         test_commit_bulk --id=side 10 &&
70
71         # add complicated history setup, including merges and
72         # ambiguous merge-bases
73
74         git checkout -b merge-left other~2 &&
75         git merge second~2 -m "merge-left" &&
76
77         git checkout -b merge-right second~1 &&
78         git merge other~1 -m "merge-right" &&
79
80         git checkout -b octo-second second &&
81         git merge merge-left merge-right -m "octopus-second" &&
82
83         git checkout -b octo-other other &&
84         git merge merge-left merge-right -m "octopus-other" &&
85
86         git checkout other &&
87         git merge octo-other -m "pull octopus" &&
88
89         git checkout second &&
90         git merge octo-second -m "pull octopus" &&
91
92         # Remove these branches so they are not selected
93         # as bitmap tips
94         git branch -D merge-left &&
95         git branch -D merge-right &&
96         git branch -D octo-other &&
97         git branch -D octo-second &&
98
99         # add padding to make these merges less interesting
100         # and avoid having them selected for bitmaps
101         test_commit_bulk --id=file 100 &&
102         git checkout other &&
103         test_commit_bulk --id=side 100 &&
104         git checkout second &&
105
106         bitmaptip=$(git rev-parse second) &&
107         blob=$(echo tagged-blob | git hash-object -w --stdin) &&
108         git tag tagged-blob $blob &&
109         git config repack.writebitmaps true
110 '
111
112 test_expect_success 'full repack creates bitmaps' '
113         GIT_TRACE2_EVENT_NESTING=4 GIT_TRACE2_EVENT="$(pwd)/trace" \
114                 git repack -ad &&
115         ls .git/objects/pack/ | grep bitmap >output &&
116         test_line_count = 1 output &&
117         grep "\"key\":\"num_selected_commits\",\"value\":\"106\"" trace &&
118         grep "\"key\":\"num_maximal_commits\",\"value\":\"107\"" trace
119 '
120
121 test_expect_success 'rev-list --test-bitmap verifies bitmaps' '
122         git rev-list --test-bitmap HEAD
123 '
124
125 rev_list_tests_head () {
126         test_expect_success "counting commits via bitmap ($state, $branch)" '
127                 git rev-list --count $branch >expect &&
128                 git rev-list --use-bitmap-index --count $branch >actual &&
129                 test_cmp expect actual
130         '
131
132         test_expect_success "counting partial commits via bitmap ($state, $branch)" '
133                 git rev-list --count $branch~5..$branch >expect &&
134                 git rev-list --use-bitmap-index --count $branch~5..$branch >actual &&
135                 test_cmp expect actual
136         '
137
138         test_expect_success "counting commits with limit ($state, $branch)" '
139                 git rev-list --count -n 1 $branch >expect &&
140                 git rev-list --use-bitmap-index --count -n 1 $branch >actual &&
141                 test_cmp expect actual
142         '
143
144         test_expect_success "counting non-linear history ($state, $branch)" '
145                 git rev-list --count other...second >expect &&
146                 git rev-list --use-bitmap-index --count other...second >actual &&
147                 test_cmp expect actual
148         '
149
150         test_expect_success "counting commits with limiting ($state, $branch)" '
151                 git rev-list --count $branch -- 1.t >expect &&
152                 git rev-list --use-bitmap-index --count $branch -- 1.t >actual &&
153                 test_cmp expect actual
154         '
155
156         test_expect_success "counting objects via bitmap ($state, $branch)" '
157                 git rev-list --count --objects $branch >expect &&
158                 git rev-list --use-bitmap-index --count --objects $branch >actual &&
159                 test_cmp expect actual
160         '
161
162         test_expect_success "enumerate commits ($state, $branch)" '
163                 git rev-list --use-bitmap-index $branch >actual &&
164                 git rev-list $branch >expect &&
165                 test_bitmap_traversal --no-confirm-bitmaps expect actual
166         '
167
168         test_expect_success "enumerate --objects ($state, $branch)" '
169                 git rev-list --objects --use-bitmap-index $branch >actual &&
170                 git rev-list --objects $branch >expect &&
171                 test_bitmap_traversal expect actual
172         '
173
174         test_expect_success "bitmap --objects handles non-commit objects ($state, $branch)" '
175                 git rev-list --objects --use-bitmap-index $branch tagged-blob >actual &&
176                 grep $blob actual
177         '
178 }
179
180 rev_list_tests () {
181         state=$1
182
183         for branch in "second" "other"
184         do
185                 rev_list_tests_head
186         done
187 }
188
189 rev_list_tests 'full bitmap'
190
191 test_expect_success 'clone from bitmapped repository' '
192         git clone --no-local --bare . clone.git &&
193         git rev-parse HEAD >expect &&
194         git --git-dir=clone.git rev-parse HEAD >actual &&
195         test_cmp expect actual
196 '
197
198 test_expect_success 'partial clone from bitmapped repository' '
199         test_config uploadpack.allowfilter true &&
200         git clone --no-local --bare --filter=blob:none . partial-clone.git &&
201         (
202                 cd partial-clone.git &&
203                 pack=$(echo objects/pack/*.pack) &&
204                 git verify-pack -v "$pack" >have &&
205                 awk "/blob/ { print \$1 }" <have >blobs &&
206                 # we expect this single blob because of the direct ref
207                 git rev-parse refs/tags/tagged-blob >expect &&
208                 test_cmp expect blobs
209         )
210 '
211
212 test_expect_success 'setup further non-bitmapped commits' '
213         test_commit_bulk --id=further 10
214 '
215
216 rev_list_tests 'partial bitmap'
217
218 test_expect_success 'fetch (partial bitmap)' '
219         git --git-dir=clone.git fetch origin second:second &&
220         git rev-parse HEAD >expect &&
221         git --git-dir=clone.git rev-parse HEAD >actual &&
222         test_cmp expect actual
223 '
224
225 test_expect_success 'incremental repack fails when bitmaps are requested' '
226         test_commit more-1 &&
227         test_must_fail git repack -d 2>err &&
228         test_i18ngrep "Incremental repacks are incompatible with bitmap" err
229 '
230
231 test_expect_success 'incremental repack can disable bitmaps' '
232         test_commit more-2 &&
233         git repack -d --no-write-bitmap-index
234 '
235
236 test_expect_success 'pack-objects respects --local (non-local loose)' '
237         git init --bare alt.git &&
238         echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
239         echo content1 >file1 &&
240         # non-local loose object which is not present in bitmapped pack
241         altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
242         # non-local loose object which is also present in bitmapped pack
243         git cat-file blob $blob | GIT_DIR=alt.git git hash-object -w --stdin &&
244         git add file1 &&
245         test_tick &&
246         git commit -m commit_file1 &&
247         echo HEAD | git pack-objects --local --stdout --revs >1.pack &&
248         git index-pack 1.pack &&
249         list_packed_objects 1.idx >1.objects &&
250         printf "%s\n" "$altblob" "$blob" >nonlocal-loose &&
251         ! has_any nonlocal-loose 1.objects
252 '
253
254 test_expect_success 'pack-objects respects --honor-pack-keep (local non-bitmapped pack)' '
255         echo content2 >file2 &&
256         blob2=$(git hash-object -w file2) &&
257         git add file2 &&
258         test_tick &&
259         git commit -m commit_file2 &&
260         printf "%s\n" "$blob2" "$bitmaptip" >keepobjects &&
261         pack2=$(git pack-objects pack2 <keepobjects) &&
262         mv pack2-$pack2.* .git/objects/pack/ &&
263         >.git/objects/pack/pack2-$pack2.keep &&
264         rm $(objpath $blob2) &&
265         echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >2a.pack &&
266         git index-pack 2a.pack &&
267         list_packed_objects 2a.idx >2a.objects &&
268         ! has_any keepobjects 2a.objects
269 '
270
271 test_expect_success 'pack-objects respects --local (non-local pack)' '
272         mv .git/objects/pack/pack2-$pack2.* alt.git/objects/pack/ &&
273         echo HEAD | git pack-objects --local --stdout --revs >2b.pack &&
274         git index-pack 2b.pack &&
275         list_packed_objects 2b.idx >2b.objects &&
276         ! has_any keepobjects 2b.objects
277 '
278
279 test_expect_success 'pack-objects respects --honor-pack-keep (local bitmapped pack)' '
280         ls .git/objects/pack/ | grep bitmap >output &&
281         test_line_count = 1 output &&
282         packbitmap=$(basename $(cat output) .bitmap) &&
283         list_packed_objects .git/objects/pack/$packbitmap.idx >packbitmap.objects &&
284         test_when_finished "rm -f .git/objects/pack/$packbitmap.keep" &&
285         >.git/objects/pack/$packbitmap.keep &&
286         echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >3a.pack &&
287         git index-pack 3a.pack &&
288         list_packed_objects 3a.idx >3a.objects &&
289         ! has_any packbitmap.objects 3a.objects
290 '
291
292 test_expect_success 'pack-objects respects --local (non-local bitmapped pack)' '
293         mv .git/objects/pack/$packbitmap.* alt.git/objects/pack/ &&
294         rm -f .git/objects/pack/multi-pack-index &&
295         test_when_finished "mv alt.git/objects/pack/$packbitmap.* .git/objects/pack/" &&
296         echo HEAD | git pack-objects --local --stdout --revs >3b.pack &&
297         git index-pack 3b.pack &&
298         list_packed_objects 3b.idx >3b.objects &&
299         ! has_any packbitmap.objects 3b.objects
300 '
301
302 test_expect_success 'pack-objects to file can use bitmap' '
303         # make sure we still have 1 bitmap index from previous tests
304         ls .git/objects/pack/ | grep bitmap >output &&
305         test_line_count = 1 output &&
306         # verify equivalent packs are generated with/without using bitmap index
307         packasha1=$(git pack-objects --no-use-bitmap-index --all packa </dev/null) &&
308         packbsha1=$(git pack-objects --use-bitmap-index --all packb </dev/null) &&
309         list_packed_objects packa-$packasha1.idx >packa.objects &&
310         list_packed_objects packb-$packbsha1.idx >packb.objects &&
311         test_cmp packa.objects packb.objects
312 '
313
314 test_expect_success 'full repack, reusing previous bitmaps' '
315         git repack -ad &&
316         ls .git/objects/pack/ | grep bitmap >output &&
317         test_line_count = 1 output
318 '
319
320 test_expect_success 'fetch (full bitmap)' '
321         git --git-dir=clone.git fetch origin second:second &&
322         git rev-parse HEAD >expect &&
323         git --git-dir=clone.git rev-parse HEAD >actual &&
324         test_cmp expect actual
325 '
326
327 test_expect_success 'create objects for missing-HAVE tests' '
328         blob=$(echo "missing have" | git hash-object -w --stdin) &&
329         tree=$(printf "100644 blob $blob\tfile\n" | git mktree) &&
330         parent=$(echo parent | git commit-tree $tree) &&
331         commit=$(echo commit | git commit-tree $tree -p $parent) &&
332         cat >revs <<-EOF
333         HEAD
334         ^HEAD^
335         ^$commit
336         EOF
337 '
338
339 test_expect_success 'pack-objects respects --incremental' '
340         cat >revs2 <<-EOF &&
341         HEAD
342         $commit
343         EOF
344         git pack-objects --incremental --stdout --revs <revs2 >4.pack &&
345         git index-pack 4.pack &&
346         list_packed_objects 4.idx >4.objects &&
347         test_line_count = 4 4.objects &&
348         git rev-list --objects $commit >revlist &&
349         cut -d" " -f1 revlist |sort >objects &&
350         test_cmp 4.objects objects
351 '
352
353 test_expect_success 'pack with missing blob' '
354         rm $(objpath $blob) &&
355         git pack-objects --stdout --revs <revs >/dev/null
356 '
357
358 test_expect_success 'pack with missing tree' '
359         rm $(objpath $tree) &&
360         git pack-objects --stdout --revs <revs >/dev/null
361 '
362
363 test_expect_success 'pack with missing parent' '
364         rm $(objpath $parent) &&
365         git pack-objects --stdout --revs <revs >/dev/null
366 '
367
368 test_expect_success JGIT,SHA1 'we can read jgit bitmaps' '
369         git clone --bare . compat-jgit.git &&
370         (
371                 cd compat-jgit.git &&
372                 rm -f objects/pack/*.bitmap &&
373                 jgit gc &&
374                 git rev-list --test-bitmap HEAD
375         )
376 '
377
378 test_expect_success JGIT,SHA1 'jgit can read our bitmaps' '
379         git clone --bare . compat-us.git &&
380         (
381                 cd compat-us.git &&
382                 git repack -adb &&
383                 # jgit gc will barf if it does not like our bitmaps
384                 jgit gc
385         )
386 '
387
388 test_expect_success 'splitting packs does not generate bogus bitmaps' '
389         test-tool genrandom foo $((1024 * 1024)) >rand &&
390         git add rand &&
391         git commit -m "commit with big file" &&
392         git -c pack.packSizeLimit=500k repack -adb &&
393         git init --bare no-bitmaps.git &&
394         git -C no-bitmaps.git fetch .. HEAD
395 '
396
397 test_expect_success 'set up reusable pack' '
398         rm -f .git/objects/pack/*.keep &&
399         git repack -adb &&
400         reusable_pack () {
401                 git for-each-ref --format="%(objectname)" |
402                 git pack-objects --delta-base-offset --revs --stdout "$@"
403         }
404 '
405
406 test_expect_success 'pack reuse respects --honor-pack-keep' '
407         test_when_finished "rm -f .git/objects/pack/*.keep" &&
408         for i in .git/objects/pack/*.pack
409         do
410                 >${i%.pack}.keep
411         done &&
412         reusable_pack --honor-pack-keep >empty.pack &&
413         git index-pack empty.pack &&
414         git show-index <empty.idx >actual &&
415         test_must_be_empty actual
416 '
417
418 test_expect_success 'pack reuse respects --local' '
419         mv .git/objects/pack/* alt.git/objects/pack/ &&
420         test_when_finished "mv alt.git/objects/pack/* .git/objects/pack/" &&
421         reusable_pack --local >empty.pack &&
422         git index-pack empty.pack &&
423         git show-index <empty.idx >actual &&
424         test_must_be_empty actual
425 '
426
427 test_expect_success 'pack reuse respects --incremental' '
428         reusable_pack --incremental >empty.pack &&
429         git index-pack empty.pack &&
430         git show-index <empty.idx >actual &&
431         test_must_be_empty actual
432 '
433
434 test_expect_success 'truncated bitmap fails gracefully (ewah)' '
435         test_config pack.writebitmaphashcache false &&
436         git repack -ad &&
437         git rev-list --use-bitmap-index --count --all >expect &&
438         bitmap=$(ls .git/objects/pack/*.bitmap) &&
439         test_when_finished "rm -f $bitmap" &&
440         test_copy_bytes 256 <$bitmap >$bitmap.tmp &&
441         mv -f $bitmap.tmp $bitmap &&
442         git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
443         test_cmp expect actual &&
444         test_i18ngrep corrupt.ewah.bitmap stderr
445 '
446
447 test_expect_success 'truncated bitmap fails gracefully (cache)' '
448         git repack -ad &&
449         git rev-list --use-bitmap-index --count --all >expect &&
450         bitmap=$(ls .git/objects/pack/*.bitmap) &&
451         test_when_finished "rm -f $bitmap" &&
452         test_copy_bytes 512 <$bitmap >$bitmap.tmp &&
453         mv -f $bitmap.tmp $bitmap &&
454         git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
455         test_cmp expect actual &&
456         test_i18ngrep corrupted.bitmap.index stderr
457 '
458
459 # have_delta <obj> <expected_base>
460 #
461 # Note that because this relies on cat-file, it might find _any_ copy of an
462 # object in the repository. The caller is responsible for making sure
463 # there's only one (e.g., via "repack -ad", or having just fetched a copy).
464 have_delta () {
465         echo $2 >expect &&
466         echo $1 | git cat-file --batch-check="%(deltabase)" >actual &&
467         test_cmp expect actual
468 }
469
470 # Create a state of history with these properties:
471 #
472 #  - refs that allow a client to fetch some new history, while sharing some old
473 #    history with the server; we use branches delta-reuse-old and
474 #    delta-reuse-new here
475 #
476 #  - the new history contains an object that is stored on the server as a delta
477 #    against a base that is in the old history
478 #
479 #  - the base object is not immediately reachable from the tip of the old
480 #    history; finding it would involve digging down through history we know the
481 #    other side has
482 #
483 # This should result in a state where fetching from old->new would not
484 # traditionally reuse the on-disk delta (because we'd have to dig to realize
485 # that the client has it), but we will do so if bitmaps can tell us cheaply
486 # that the other side has it.
487 test_expect_success 'set up thin delta-reuse parent' '
488         # This first commit contains the buried base object.
489         test-tool genrandom delta 16384 >file &&
490         git add file &&
491         git commit -m "delta base" &&
492         base=$(git rev-parse --verify HEAD:file) &&
493
494         # These intermediate commits bury the base back in history.
495         # This becomes the "old" state.
496         for i in 1 2 3 4 5
497         do
498                 echo $i >file &&
499                 git commit -am "intermediate $i" || return 1
500         done &&
501         git branch delta-reuse-old &&
502
503         # And now our new history has a delta against the buried base. Note
504         # that this must be smaller than the original file, since pack-objects
505         # prefers to create deltas from smaller objects to larger.
506         test-tool genrandom delta 16300 >file &&
507         git commit -am "delta result" &&
508         delta=$(git rev-parse --verify HEAD:file) &&
509         git branch delta-reuse-new &&
510
511         # Repack with bitmaps and double check that we have the expected delta
512         # relationship.
513         git repack -adb &&
514         have_delta $delta $base
515 '
516
517 # Now we can sanity-check the non-bitmap behavior (that the server is not able
518 # to reuse the delta). This isn't strictly something we care about, so this
519 # test could be scrapped in the future. But it makes sure that the next test is
520 # actually triggering the feature we want.
521 #
522 # Note that our tools for working with on-the-wire "thin" packs are limited. So
523 # we actually perform the fetch, retain the resulting pack, and inspect the
524 # result.
525 test_expect_success 'fetch without bitmaps ignores delta against old base' '
526         test_config pack.usebitmaps false &&
527         test_when_finished "rm -rf client.git" &&
528         git init --bare client.git &&
529         (
530                 cd client.git &&
531                 git config transfer.unpackLimit 1 &&
532                 git fetch .. delta-reuse-old:delta-reuse-old &&
533                 git fetch .. delta-reuse-new:delta-reuse-new &&
534                 have_delta $delta $ZERO_OID
535         )
536 '
537
538 # And do the same for the bitmap case, where we do expect to find the delta.
539 test_expect_success 'fetch with bitmaps can reuse old base' '
540         test_config pack.usebitmaps true &&
541         test_when_finished "rm -rf client.git" &&
542         git init --bare client.git &&
543         (
544                 cd client.git &&
545                 git config transfer.unpackLimit 1 &&
546                 git fetch .. delta-reuse-old:delta-reuse-old &&
547                 git fetch .. delta-reuse-new:delta-reuse-new &&
548                 have_delta $delta $base
549         )
550 '
551
552 test_done