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