rev-list: use bitmap filters for traversal
[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 test_expect_success 'setup repo with moderate-sized history' '
24         test_commit_bulk --id=file 100 &&
25         git checkout -b other HEAD~5 &&
26         test_commit_bulk --id=side 10 &&
27         git checkout master &&
28         bitmaptip=$(git rev-parse master) &&
29         blob=$(echo tagged-blob | git hash-object -w --stdin) &&
30         git tag tagged-blob $blob &&
31         git config repack.writebitmaps true
32 '
33
34 test_expect_success 'full repack creates bitmaps' '
35         git repack -ad &&
36         ls .git/objects/pack/ | grep bitmap >output &&
37         test_line_count = 1 output
38 '
39
40 test_expect_success 'rev-list --test-bitmap verifies bitmaps' '
41         git rev-list --test-bitmap HEAD
42 '
43
44 rev_list_tests() {
45         state=$1
46
47         test_expect_success "counting commits via bitmap ($state)" '
48                 git rev-list --count HEAD >expect &&
49                 git rev-list --use-bitmap-index --count HEAD >actual &&
50                 test_cmp expect actual
51         '
52
53         test_expect_success "counting partial commits via bitmap ($state)" '
54                 git rev-list --count HEAD~5..HEAD >expect &&
55                 git rev-list --use-bitmap-index --count HEAD~5..HEAD >actual &&
56                 test_cmp expect actual
57         '
58
59         test_expect_success "counting commits with limit ($state)" '
60                 git rev-list --count -n 1 HEAD >expect &&
61                 git rev-list --use-bitmap-index --count -n 1 HEAD >actual &&
62                 test_cmp expect actual
63         '
64
65         test_expect_success "counting non-linear history ($state)" '
66                 git rev-list --count other...master >expect &&
67                 git rev-list --use-bitmap-index --count other...master >actual &&
68                 test_cmp expect actual
69         '
70
71         test_expect_success "counting commits with limiting ($state)" '
72                 git rev-list --count HEAD -- 1.t >expect &&
73                 git rev-list --use-bitmap-index --count HEAD -- 1.t >actual &&
74                 test_cmp expect actual
75         '
76
77         test_expect_success "counting objects via bitmap ($state)" '
78                 git rev-list --count --objects HEAD >expect &&
79                 git rev-list --use-bitmap-index --count --objects HEAD >actual &&
80                 test_cmp expect actual
81         '
82
83         test_expect_success "enumerate commits ($state)" '
84                 git rev-list --use-bitmap-index HEAD >actual &&
85                 git rev-list HEAD >expect &&
86                 test_bitmap_traversal --no-confirm-bitmaps expect actual
87         '
88
89         test_expect_success "enumerate --objects ($state)" '
90                 git rev-list --objects --use-bitmap-index HEAD >actual &&
91                 git rev-list --objects HEAD >expect &&
92                 test_bitmap_traversal expect actual
93         '
94
95         test_expect_success "bitmap --objects handles non-commit objects ($state)" '
96                 git rev-list --objects --use-bitmap-index HEAD tagged-blob >actual &&
97                 grep $blob actual
98         '
99 }
100
101 rev_list_tests 'full bitmap'
102
103 test_expect_success 'clone from bitmapped repository' '
104         git clone --no-local --bare . clone.git &&
105         git rev-parse HEAD >expect &&
106         git --git-dir=clone.git rev-parse HEAD >actual &&
107         test_cmp expect actual
108 '
109
110 test_expect_success 'setup further non-bitmapped commits' '
111         test_commit_bulk --id=further 10
112 '
113
114 rev_list_tests 'partial bitmap'
115
116 test_expect_success 'fetch (partial bitmap)' '
117         git --git-dir=clone.git fetch origin master:master &&
118         git rev-parse HEAD >expect &&
119         git --git-dir=clone.git rev-parse HEAD >actual &&
120         test_cmp expect actual
121 '
122
123 test_expect_success 'incremental repack fails when bitmaps are requested' '
124         test_commit more-1 &&
125         test_must_fail git repack -d 2>err &&
126         test_i18ngrep "Incremental repacks are incompatible with bitmap" err
127 '
128
129 test_expect_success 'incremental repack can disable bitmaps' '
130         test_commit more-2 &&
131         git repack -d --no-write-bitmap-index
132 '
133
134 test_expect_success 'pack-objects respects --local (non-local loose)' '
135         git init --bare alt.git &&
136         echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
137         echo content1 >file1 &&
138         # non-local loose object which is not present in bitmapped pack
139         altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
140         # non-local loose object which is also present in bitmapped pack
141         git cat-file blob $blob | GIT_DIR=alt.git git hash-object -w --stdin &&
142         git add file1 &&
143         test_tick &&
144         git commit -m commit_file1 &&
145         echo HEAD | git pack-objects --local --stdout --revs >1.pack &&
146         git index-pack 1.pack &&
147         list_packed_objects 1.idx >1.objects &&
148         printf "%s\n" "$altblob" "$blob" >nonlocal-loose &&
149         ! has_any nonlocal-loose 1.objects
150 '
151
152 test_expect_success 'pack-objects respects --honor-pack-keep (local non-bitmapped pack)' '
153         echo content2 >file2 &&
154         blob2=$(git hash-object -w file2) &&
155         git add file2 &&
156         test_tick &&
157         git commit -m commit_file2 &&
158         printf "%s\n" "$blob2" "$bitmaptip" >keepobjects &&
159         pack2=$(git pack-objects pack2 <keepobjects) &&
160         mv pack2-$pack2.* .git/objects/pack/ &&
161         >.git/objects/pack/pack2-$pack2.keep &&
162         rm $(objpath $blob2) &&
163         echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >2a.pack &&
164         git index-pack 2a.pack &&
165         list_packed_objects 2a.idx >2a.objects &&
166         ! has_any keepobjects 2a.objects
167 '
168
169 test_expect_success 'pack-objects respects --local (non-local pack)' '
170         mv .git/objects/pack/pack2-$pack2.* alt.git/objects/pack/ &&
171         echo HEAD | git pack-objects --local --stdout --revs >2b.pack &&
172         git index-pack 2b.pack &&
173         list_packed_objects 2b.idx >2b.objects &&
174         ! has_any keepobjects 2b.objects
175 '
176
177 test_expect_success 'pack-objects respects --honor-pack-keep (local bitmapped pack)' '
178         ls .git/objects/pack/ | grep bitmap >output &&
179         test_line_count = 1 output &&
180         packbitmap=$(basename $(cat output) .bitmap) &&
181         list_packed_objects .git/objects/pack/$packbitmap.idx >packbitmap.objects &&
182         test_when_finished "rm -f .git/objects/pack/$packbitmap.keep" &&
183         >.git/objects/pack/$packbitmap.keep &&
184         echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >3a.pack &&
185         git index-pack 3a.pack &&
186         list_packed_objects 3a.idx >3a.objects &&
187         ! has_any packbitmap.objects 3a.objects
188 '
189
190 test_expect_success 'pack-objects respects --local (non-local bitmapped pack)' '
191         mv .git/objects/pack/$packbitmap.* alt.git/objects/pack/ &&
192         rm -f .git/objects/pack/multi-pack-index &&
193         test_when_finished "mv alt.git/objects/pack/$packbitmap.* .git/objects/pack/" &&
194         echo HEAD | git pack-objects --local --stdout --revs >3b.pack &&
195         git index-pack 3b.pack &&
196         list_packed_objects 3b.idx >3b.objects &&
197         ! has_any packbitmap.objects 3b.objects
198 '
199
200 test_expect_success 'pack-objects to file can use bitmap' '
201         # make sure we still have 1 bitmap index from previous tests
202         ls .git/objects/pack/ | grep bitmap >output &&
203         test_line_count = 1 output &&
204         # verify equivalent packs are generated with/without using bitmap index
205         packasha1=$(git pack-objects --no-use-bitmap-index --all packa </dev/null) &&
206         packbsha1=$(git pack-objects --use-bitmap-index --all packb </dev/null) &&
207         list_packed_objects packa-$packasha1.idx >packa.objects &&
208         list_packed_objects packb-$packbsha1.idx >packb.objects &&
209         test_cmp packa.objects packb.objects
210 '
211
212 test_expect_success 'full repack, reusing previous bitmaps' '
213         git repack -ad &&
214         ls .git/objects/pack/ | grep bitmap >output &&
215         test_line_count = 1 output
216 '
217
218 test_expect_success 'fetch (full bitmap)' '
219         git --git-dir=clone.git fetch origin master:master &&
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 'create objects for missing-HAVE tests' '
226         blob=$(echo "missing have" | git hash-object -w --stdin) &&
227         tree=$(printf "100644 blob $blob\tfile\n" | git mktree) &&
228         parent=$(echo parent | git commit-tree $tree) &&
229         commit=$(echo commit | git commit-tree $tree -p $parent) &&
230         cat >revs <<-EOF
231         HEAD
232         ^HEAD^
233         ^$commit
234         EOF
235 '
236
237 test_expect_success 'pack-objects respects --incremental' '
238         cat >revs2 <<-EOF &&
239         HEAD
240         $commit
241         EOF
242         git pack-objects --incremental --stdout --revs <revs2 >4.pack &&
243         git index-pack 4.pack &&
244         list_packed_objects 4.idx >4.objects &&
245         test_line_count = 4 4.objects &&
246         git rev-list --objects $commit >revlist &&
247         cut -d" " -f1 revlist |sort >objects &&
248         test_cmp 4.objects objects
249 '
250
251 test_expect_success 'pack with missing blob' '
252         rm $(objpath $blob) &&
253         git pack-objects --stdout --revs <revs >/dev/null
254 '
255
256 test_expect_success 'pack with missing tree' '
257         rm $(objpath $tree) &&
258         git pack-objects --stdout --revs <revs >/dev/null
259 '
260
261 test_expect_success 'pack with missing parent' '
262         rm $(objpath $parent) &&
263         git pack-objects --stdout --revs <revs >/dev/null
264 '
265
266 test_expect_success JGIT 'we can read jgit bitmaps' '
267         git clone --bare . compat-jgit.git &&
268         (
269                 cd compat-jgit.git &&
270                 rm -f objects/pack/*.bitmap &&
271                 jgit gc &&
272                 git rev-list --test-bitmap HEAD
273         )
274 '
275
276 test_expect_success JGIT 'jgit can read our bitmaps' '
277         git clone --bare . compat-us.git &&
278         (
279                 cd compat-us.git &&
280                 git repack -adb &&
281                 # jgit gc will barf if it does not like our bitmaps
282                 jgit gc
283         )
284 '
285
286 test_expect_success 'splitting packs does not generate bogus bitmaps' '
287         test-tool genrandom foo $((1024 * 1024)) >rand &&
288         git add rand &&
289         git commit -m "commit with big file" &&
290         git -c pack.packSizeLimit=500k repack -adb &&
291         git init --bare no-bitmaps.git &&
292         git -C no-bitmaps.git fetch .. HEAD
293 '
294
295 test_expect_success 'set up reusable pack' '
296         rm -f .git/objects/pack/*.keep &&
297         git repack -adb &&
298         reusable_pack () {
299                 git for-each-ref --format="%(objectname)" |
300                 git pack-objects --delta-base-offset --revs --stdout "$@"
301         }
302 '
303
304 test_expect_success 'pack reuse respects --honor-pack-keep' '
305         test_when_finished "rm -f .git/objects/pack/*.keep" &&
306         for i in .git/objects/pack/*.pack
307         do
308                 >${i%.pack}.keep
309         done &&
310         reusable_pack --honor-pack-keep >empty.pack &&
311         git index-pack empty.pack &&
312         git show-index <empty.idx >actual &&
313         test_must_be_empty actual
314 '
315
316 test_expect_success 'pack reuse respects --local' '
317         mv .git/objects/pack/* alt.git/objects/pack/ &&
318         test_when_finished "mv alt.git/objects/pack/* .git/objects/pack/" &&
319         reusable_pack --local >empty.pack &&
320         git index-pack empty.pack &&
321         git show-index <empty.idx >actual &&
322         test_must_be_empty actual
323 '
324
325 test_expect_success 'pack reuse respects --incremental' '
326         reusable_pack --incremental >empty.pack &&
327         git index-pack empty.pack &&
328         git show-index <empty.idx >actual &&
329         test_must_be_empty actual
330 '
331
332 test_expect_success 'truncated bitmap fails gracefully' '
333         git repack -ad &&
334         git rev-list --use-bitmap-index --count --all >expect &&
335         bitmap=$(ls .git/objects/pack/*.bitmap) &&
336         test_when_finished "rm -f $bitmap" &&
337         test_copy_bytes 512 <$bitmap >$bitmap.tmp &&
338         mv -f $bitmap.tmp $bitmap &&
339         git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
340         test_cmp expect actual &&
341         test_i18ngrep corrupt stderr
342 '
343
344 # have_delta <obj> <expected_base>
345 #
346 # Note that because this relies on cat-file, it might find _any_ copy of an
347 # object in the repository. The caller is responsible for making sure
348 # there's only one (e.g., via "repack -ad", or having just fetched a copy).
349 have_delta () {
350         echo $2 >expect &&
351         echo $1 | git cat-file --batch-check="%(deltabase)" >actual &&
352         test_cmp expect actual
353 }
354
355 # Create a state of history with these properties:
356 #
357 #  - refs that allow a client to fetch some new history, while sharing some old
358 #    history with the server; we use branches delta-reuse-old and
359 #    delta-reuse-new here
360 #
361 #  - the new history contains an object that is stored on the server as a delta
362 #    against a base that is in the old history
363 #
364 #  - the base object is not immediately reachable from the tip of the old
365 #    history; finding it would involve digging down through history we know the
366 #    other side has
367 #
368 # This should result in a state where fetching from old->new would not
369 # traditionally reuse the on-disk delta (because we'd have to dig to realize
370 # that the client has it), but we will do so if bitmaps can tell us cheaply
371 # that the other side has it.
372 test_expect_success 'set up thin delta-reuse parent' '
373         # This first commit contains the buried base object.
374         test-tool genrandom delta 16384 >file &&
375         git add file &&
376         git commit -m "delta base" &&
377         base=$(git rev-parse --verify HEAD:file) &&
378
379         # These intermediate commits bury the base back in history.
380         # This becomes the "old" state.
381         for i in 1 2 3 4 5
382         do
383                 echo $i >file &&
384                 git commit -am "intermediate $i" || return 1
385         done &&
386         git branch delta-reuse-old &&
387
388         # And now our new history has a delta against the buried base. Note
389         # that this must be smaller than the original file, since pack-objects
390         # prefers to create deltas from smaller objects to larger.
391         test-tool genrandom delta 16300 >file &&
392         git commit -am "delta result" &&
393         delta=$(git rev-parse --verify HEAD:file) &&
394         git branch delta-reuse-new &&
395
396         # Repack with bitmaps and double check that we have the expected delta
397         # relationship.
398         git repack -adb &&
399         have_delta $delta $base
400 '
401
402 # Now we can sanity-check the non-bitmap behavior (that the server is not able
403 # to reuse the delta). This isn't strictly something we care about, so this
404 # test could be scrapped in the future. But it makes sure that the next test is
405 # actually triggering the feature we want.
406 #
407 # Note that our tools for working with on-the-wire "thin" packs are limited. So
408 # we actually perform the fetch, retain the resulting pack, and inspect the
409 # result.
410 test_expect_success 'fetch without bitmaps ignores delta against old base' '
411         test_config pack.usebitmaps false &&
412         test_when_finished "rm -rf client.git" &&
413         git init --bare client.git &&
414         (
415                 cd client.git &&
416                 git config transfer.unpackLimit 1 &&
417                 git fetch .. delta-reuse-old:delta-reuse-old &&
418                 git fetch .. delta-reuse-new:delta-reuse-new &&
419                 have_delta $delta $ZERO_OID
420         )
421 '
422
423 # And do the same for the bitmap case, where we do expect to find the delta.
424 test_expect_success 'fetch with bitmaps can reuse old base' '
425         test_config pack.usebitmaps true &&
426         test_when_finished "rm -rf client.git" &&
427         git init --bare client.git &&
428         (
429                 cd client.git &&
430                 git config transfer.unpackLimit 1 &&
431                 git fetch .. delta-reuse-old:delta-reuse-old &&
432                 git fetch .. delta-reuse-new:delta-reuse-new &&
433                 have_delta $delta $base
434         )
435 '
436
437 test_done