reset: make sparse-aware (except --mixed)
[git.git] / t / t5310-pack-bitmaps.sh
blob673baa5c3ccc8a44948280337d55fa67fd28f324
1 #!/bin/sh
3 test_description='exercise basic bitmap functionality'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 . ./test-lib.sh
8 . "$TEST_DIRECTORY"/lib-bundle.sh
9 . "$TEST_DIRECTORY"/lib-bitmap.sh
11 # t5310 deals only with single-pack bitmaps, so don't write MIDX bitmaps in
12 # their place.
13 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
15 objpath () {
16 echo ".git/objects/$(echo "$1" | sed -e 's|\(..\)|\1/|')"
19 # show objects present in pack ($1 should be associated *.idx)
20 list_packed_objects () {
21 git show-index <"$1" >object-list &&
22 cut -d' ' -f2 object-list
25 # has_any pattern-file content-file
26 # tests whether content-file has any entry from pattern-file with entries being
27 # whole lines.
28 has_any () {
29 grep -Ff "$1" "$2"
32 setup_bitmap_history
34 test_expect_success 'setup writing bitmaps during repack' '
35 git config repack.writeBitmaps true
38 test_expect_success 'full repack creates bitmaps' '
39 GIT_TRACE2_EVENT_NESTING=4 GIT_TRACE2_EVENT="$(pwd)/trace" \
40 git repack -ad &&
41 ls .git/objects/pack/ | grep bitmap >output &&
42 test_line_count = 1 output &&
43 grep "\"key\":\"num_selected_commits\",\"value\":\"106\"" trace &&
44 grep "\"key\":\"num_maximal_commits\",\"value\":\"107\"" trace
47 basic_bitmap_tests
49 test_expect_success 'incremental repack fails when bitmaps are requested' '
50 test_commit more-1 &&
51 test_must_fail git repack -d 2>err &&
52 test_i18ngrep "Incremental repacks are incompatible with bitmap" err
55 test_expect_success 'incremental repack can disable bitmaps' '
56 test_commit more-2 &&
57 git repack -d --no-write-bitmap-index
60 test_expect_success 'pack-objects respects --local (non-local loose)' '
61 git init --bare alt.git &&
62 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
63 echo content1 >file1 &&
64 # non-local loose object which is not present in bitmapped pack
65 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
66 # non-local loose object which is also present in bitmapped pack
67 git cat-file blob $blob | GIT_DIR=alt.git git hash-object -w --stdin &&
68 git add file1 &&
69 test_tick &&
70 git commit -m commit_file1 &&
71 echo HEAD | git pack-objects --local --stdout --revs >1.pack &&
72 git index-pack 1.pack &&
73 list_packed_objects 1.idx >1.objects &&
74 printf "%s\n" "$altblob" "$blob" >nonlocal-loose &&
75 ! has_any nonlocal-loose 1.objects
78 test_expect_success 'pack-objects respects --honor-pack-keep (local non-bitmapped pack)' '
79 echo content2 >file2 &&
80 blob2=$(git hash-object -w file2) &&
81 git add file2 &&
82 test_tick &&
83 git commit -m commit_file2 &&
84 printf "%s\n" "$blob2" "$bitmaptip" >keepobjects &&
85 pack2=$(git pack-objects pack2 <keepobjects) &&
86 mv pack2-$pack2.* .git/objects/pack/ &&
87 >.git/objects/pack/pack2-$pack2.keep &&
88 rm $(objpath $blob2) &&
89 echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >2a.pack &&
90 git index-pack 2a.pack &&
91 list_packed_objects 2a.idx >2a.objects &&
92 ! has_any keepobjects 2a.objects
95 test_expect_success 'pack-objects respects --local (non-local pack)' '
96 mv .git/objects/pack/pack2-$pack2.* alt.git/objects/pack/ &&
97 echo HEAD | git pack-objects --local --stdout --revs >2b.pack &&
98 git index-pack 2b.pack &&
99 list_packed_objects 2b.idx >2b.objects &&
100 ! has_any keepobjects 2b.objects
103 test_expect_success 'pack-objects respects --honor-pack-keep (local bitmapped pack)' '
104 ls .git/objects/pack/ | grep bitmap >output &&
105 test_line_count = 1 output &&
106 packbitmap=$(basename $(cat output) .bitmap) &&
107 list_packed_objects .git/objects/pack/$packbitmap.idx >packbitmap.objects &&
108 test_when_finished "rm -f .git/objects/pack/$packbitmap.keep" &&
109 >.git/objects/pack/$packbitmap.keep &&
110 echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >3a.pack &&
111 git index-pack 3a.pack &&
112 list_packed_objects 3a.idx >3a.objects &&
113 ! has_any packbitmap.objects 3a.objects
116 test_expect_success 'pack-objects respects --local (non-local bitmapped pack)' '
117 mv .git/objects/pack/$packbitmap.* alt.git/objects/pack/ &&
118 rm -f .git/objects/pack/multi-pack-index &&
119 test_when_finished "mv alt.git/objects/pack/$packbitmap.* .git/objects/pack/" &&
120 echo HEAD | git pack-objects --local --stdout --revs >3b.pack &&
121 git index-pack 3b.pack &&
122 list_packed_objects 3b.idx >3b.objects &&
123 ! has_any packbitmap.objects 3b.objects
126 test_expect_success 'pack-objects to file can use bitmap' '
127 # make sure we still have 1 bitmap index from previous tests
128 ls .git/objects/pack/ | grep bitmap >output &&
129 test_line_count = 1 output &&
130 # verify equivalent packs are generated with/without using bitmap index
131 packasha1=$(git pack-objects --no-use-bitmap-index --all packa </dev/null) &&
132 packbsha1=$(git pack-objects --use-bitmap-index --all packb </dev/null) &&
133 list_packed_objects packa-$packasha1.idx >packa.objects &&
134 list_packed_objects packb-$packbsha1.idx >packb.objects &&
135 test_cmp packa.objects packb.objects
138 test_expect_success 'full repack, reusing previous bitmaps' '
139 git repack -ad &&
140 ls .git/objects/pack/ | grep bitmap >output &&
141 test_line_count = 1 output
144 test_expect_success 'fetch (full bitmap)' '
145 git --git-dir=clone.git fetch origin second:second &&
146 git rev-parse HEAD >expect &&
147 git --git-dir=clone.git rev-parse HEAD >actual &&
148 test_cmp expect actual
151 test_expect_success 'create objects for missing-HAVE tests' '
152 blob=$(echo "missing have" | git hash-object -w --stdin) &&
153 tree=$(printf "100644 blob $blob\tfile\n" | git mktree) &&
154 parent=$(echo parent | git commit-tree $tree) &&
155 commit=$(echo commit | git commit-tree $tree -p $parent) &&
156 cat >revs <<-EOF
157 HEAD
158 ^HEAD^
159 ^$commit
163 test_expect_success 'pack-objects respects --incremental' '
164 cat >revs2 <<-EOF &&
165 HEAD
166 $commit
168 git pack-objects --incremental --stdout --revs <revs2 >4.pack &&
169 git index-pack 4.pack &&
170 list_packed_objects 4.idx >4.objects &&
171 test_line_count = 4 4.objects &&
172 git rev-list --objects $commit >revlist &&
173 cut -d" " -f1 revlist |sort >objects &&
174 test_cmp 4.objects objects
177 test_expect_success 'pack with missing blob' '
178 rm $(objpath $blob) &&
179 git pack-objects --stdout --revs <revs >/dev/null
182 test_expect_success 'pack with missing tree' '
183 rm $(objpath $tree) &&
184 git pack-objects --stdout --revs <revs >/dev/null
187 test_expect_success 'pack with missing parent' '
188 rm $(objpath $parent) &&
189 git pack-objects --stdout --revs <revs >/dev/null
192 test_expect_success JGIT,SHA1 'we can read jgit bitmaps' '
193 git clone --bare . compat-jgit.git &&
195 cd compat-jgit.git &&
196 rm -f objects/pack/*.bitmap &&
197 jgit gc &&
198 git rev-list --test-bitmap HEAD
202 test_expect_success JGIT,SHA1 'jgit can read our bitmaps' '
203 git clone --bare . compat-us.git &&
205 cd compat-us.git &&
206 git repack -adb &&
207 # jgit gc will barf if it does not like our bitmaps
208 jgit gc
212 test_expect_success 'splitting packs does not generate bogus bitmaps' '
213 test-tool genrandom foo $((1024 * 1024)) >rand &&
214 git add rand &&
215 git commit -m "commit with big file" &&
216 git -c pack.packSizeLimit=500k repack -adb &&
217 git init --bare no-bitmaps.git &&
218 git -C no-bitmaps.git fetch .. HEAD
221 test_expect_success 'set up reusable pack' '
222 rm -f .git/objects/pack/*.keep &&
223 git repack -adb &&
224 reusable_pack () {
225 git for-each-ref --format="%(objectname)" |
226 git pack-objects --delta-base-offset --revs --stdout "$@"
230 test_expect_success 'pack reuse respects --honor-pack-keep' '
231 test_when_finished "rm -f .git/objects/pack/*.keep" &&
232 for i in .git/objects/pack/*.pack
234 >${i%.pack}.keep
235 done &&
236 reusable_pack --honor-pack-keep >empty.pack &&
237 git index-pack empty.pack &&
238 git show-index <empty.idx >actual &&
239 test_must_be_empty actual
242 test_expect_success 'pack reuse respects --local' '
243 mv .git/objects/pack/* alt.git/objects/pack/ &&
244 test_when_finished "mv alt.git/objects/pack/* .git/objects/pack/" &&
245 reusable_pack --local >empty.pack &&
246 git index-pack empty.pack &&
247 git show-index <empty.idx >actual &&
248 test_must_be_empty actual
251 test_expect_success 'pack reuse respects --incremental' '
252 reusable_pack --incremental >empty.pack &&
253 git index-pack empty.pack &&
254 git show-index <empty.idx >actual &&
255 test_must_be_empty actual
258 test_expect_success 'truncated bitmap fails gracefully (ewah)' '
259 test_config pack.writebitmaphashcache false &&
260 git repack -ad &&
261 git rev-list --use-bitmap-index --count --all >expect &&
262 bitmap=$(ls .git/objects/pack/*.bitmap) &&
263 test_when_finished "rm -f $bitmap" &&
264 test_copy_bytes 256 <$bitmap >$bitmap.tmp &&
265 mv -f $bitmap.tmp $bitmap &&
266 git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
267 test_cmp expect actual &&
268 test_i18ngrep corrupt.ewah.bitmap stderr
271 test_expect_success 'truncated bitmap fails gracefully (cache)' '
272 git repack -ad &&
273 git rev-list --use-bitmap-index --count --all >expect &&
274 bitmap=$(ls .git/objects/pack/*.bitmap) &&
275 test_when_finished "rm -f $bitmap" &&
276 test_copy_bytes 512 <$bitmap >$bitmap.tmp &&
277 mv -f $bitmap.tmp $bitmap &&
278 git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
279 test_cmp expect actual &&
280 test_i18ngrep corrupted.bitmap.index stderr
283 # Create a state of history with these properties:
285 # - refs that allow a client to fetch some new history, while sharing some old
286 # history with the server; we use branches delta-reuse-old and
287 # delta-reuse-new here
289 # - the new history contains an object that is stored on the server as a delta
290 # against a base that is in the old history
292 # - the base object is not immediately reachable from the tip of the old
293 # history; finding it would involve digging down through history we know the
294 # other side has
296 # This should result in a state where fetching from old->new would not
297 # traditionally reuse the on-disk delta (because we'd have to dig to realize
298 # that the client has it), but we will do so if bitmaps can tell us cheaply
299 # that the other side has it.
300 test_expect_success 'set up thin delta-reuse parent' '
301 # This first commit contains the buried base object.
302 test-tool genrandom delta 16384 >file &&
303 git add file &&
304 git commit -m "delta base" &&
305 base=$(git rev-parse --verify HEAD:file) &&
307 # These intermediate commits bury the base back in history.
308 # This becomes the "old" state.
309 for i in 1 2 3 4 5
311 echo $i >file &&
312 git commit -am "intermediate $i" || return 1
313 done &&
314 git branch delta-reuse-old &&
316 # And now our new history has a delta against the buried base. Note
317 # that this must be smaller than the original file, since pack-objects
318 # prefers to create deltas from smaller objects to larger.
319 test-tool genrandom delta 16300 >file &&
320 git commit -am "delta result" &&
321 delta=$(git rev-parse --verify HEAD:file) &&
322 git branch delta-reuse-new &&
324 # Repack with bitmaps and double check that we have the expected delta
325 # relationship.
326 git repack -adb &&
327 have_delta $delta $base
330 # Now we can sanity-check the non-bitmap behavior (that the server is not able
331 # to reuse the delta). This isn't strictly something we care about, so this
332 # test could be scrapped in the future. But it makes sure that the next test is
333 # actually triggering the feature we want.
335 # Note that our tools for working with on-the-wire "thin" packs are limited. So
336 # we actually perform the fetch, retain the resulting pack, and inspect the
337 # result.
338 test_expect_success 'fetch without bitmaps ignores delta against old base' '
339 test_config pack.usebitmaps false &&
340 test_when_finished "rm -rf client.git" &&
341 git init --bare client.git &&
343 cd client.git &&
344 git config transfer.unpackLimit 1 &&
345 git fetch .. delta-reuse-old:delta-reuse-old &&
346 git fetch .. delta-reuse-new:delta-reuse-new &&
347 have_delta $delta $ZERO_OID
351 # And do the same for the bitmap case, where we do expect to find the delta.
352 test_expect_success 'fetch with bitmaps can reuse old base' '
353 test_config pack.usebitmaps true &&
354 test_when_finished "rm -rf client.git" &&
355 git init --bare client.git &&
357 cd client.git &&
358 git config transfer.unpackLimit 1 &&
359 git fetch .. delta-reuse-old:delta-reuse-old &&
360 git fetch .. delta-reuse-new:delta-reuse-new &&
361 have_delta $delta $base
365 test_expect_success 'pack.preferBitmapTips' '
366 git init repo &&
367 test_when_finished "rm -fr repo" &&
369 cd repo &&
371 # create enough commits that not all are receive bitmap
372 # coverage even if they are all at the tip of some reference.
373 test_commit_bulk --message="%s" 103 &&
375 git rev-list HEAD >commits.raw &&
376 sort <commits.raw >commits &&
378 git log --format="create refs/tags/%s %H" HEAD >refs &&
379 git update-ref --stdin <refs &&
381 git repack -adb &&
382 test-tool bitmap list-commits | sort >bitmaps &&
384 # remember which commits did not receive bitmaps
385 comm -13 bitmaps commits >before &&
386 test_file_not_empty before &&
388 # mark the commits which did not receive bitmaps as preferred,
389 # and generate the bitmap again
390 perl -pe "s{^}{create refs/tags/include/$. }" <before |
391 git update-ref --stdin &&
392 git -c pack.preferBitmapTips=refs/tags/include repack -adb &&
394 # finally, check that the commit(s) without bitmap coverage
395 # are not the same ones as before
396 test-tool bitmap list-commits | sort >bitmaps &&
397 comm -13 bitmaps commits >after &&
399 ! test_cmp before after
403 test_done