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