]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7700-repack.sh
Merge branch 'tb/midx-bitmap-corruption-fix'
[thirdparty/git.git] / t / t7700-repack.sh
1 #!/bin/sh
2
3 test_description='git repack works correctly'
4
5 . ./test-lib.sh
6 . "${TEST_DIRECTORY}/lib-bitmap.sh"
7 . "${TEST_DIRECTORY}/lib-midx.sh"
8 . "${TEST_DIRECTORY}/lib-terminal.sh"
9
10 commit_and_pack () {
11 test_commit "$@" 1>&2 &&
12 incrpackid=$(git pack-objects --all --unpacked --incremental .git/objects/pack/pack </dev/null) &&
13 echo pack-${incrpackid}.pack
14 }
15
16 test_no_missing_in_packs () {
17 myidx=$(ls -1 .git/objects/pack/*.idx) &&
18 test_path_is_file "$myidx" &&
19 git verify-pack -v alt_objects/pack/*.idx >orig.raw &&
20 sed -n -e "s/^\($OID_REGEX\).*/\1/p" orig.raw | sort >orig &&
21 git verify-pack -v $myidx >dest.raw &&
22 cut -d" " -f1 dest.raw | sort >dest &&
23 comm -23 orig dest >missing &&
24 test_must_be_empty missing
25 }
26
27 # we expect $packid and $oid to be defined
28 test_has_duplicate_object () {
29 want_duplicate_object="$1"
30 found_duplicate_object=false
31 for p in .git/objects/pack/*.idx
32 do
33 idx=$(basename $p)
34 test "pack-$packid.idx" = "$idx" && continue
35 git verify-pack -v $p >packlist || return $?
36 if grep "^$oid" packlist
37 then
38 found_duplicate_object=true
39 echo "DUPLICATE OBJECT FOUND"
40 break
41 fi
42 done &&
43 test "$want_duplicate_object" = "$found_duplicate_object"
44 }
45
46 test_expect_success 'objects in packs marked .keep are not repacked' '
47 echo content1 >file1 &&
48 echo content2 >file2 &&
49 git add . &&
50 test_tick &&
51 git commit -m initial_commit &&
52 # Create two packs
53 # The first pack will contain all of the objects except one
54 git rev-list --objects --all >objs &&
55 grep -v file2 objs | git pack-objects pack &&
56 # The second pack will contain the excluded object
57 packid=$(grep file2 objs | git pack-objects pack) &&
58 >pack-$packid.keep &&
59 git verify-pack -v pack-$packid.idx >packlist &&
60 oid=$(head -n 1 packlist | sed -e "s/^\($OID_REGEX\).*/\1/") &&
61 mv pack-* .git/objects/pack/ &&
62 git repack -A -d -l &&
63 git prune-packed &&
64 test_has_duplicate_object false
65 '
66
67 test_expect_success 'writing bitmaps via command-line can duplicate .keep objects' '
68 # build on $oid, $packid, and .keep state from previous
69 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 git repack -Adbl &&
70 test_has_duplicate_object true
71 '
72
73 test_expect_success 'writing bitmaps via config can duplicate .keep objects' '
74 # build on $oid, $packid, and .keep state from previous
75 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
76 git -c repack.writebitmaps=true repack -Adl &&
77 test_has_duplicate_object true
78 '
79
80 test_expect_success 'loose objects in alternate ODB are not repacked' '
81 mkdir alt_objects &&
82 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
83 echo content3 >file3 &&
84 oid=$(GIT_OBJECT_DIRECTORY=alt_objects git hash-object -w file3) &&
85 git add file3 &&
86 test_tick &&
87 git commit -m commit_file3 &&
88 git repack -a -d -l &&
89 git prune-packed &&
90 test_has_duplicate_object false
91 '
92
93 test_expect_success 'packed obs in alt ODB are repacked even when local repo is packless' '
94 mkdir alt_objects/pack &&
95 mv .git/objects/pack/* alt_objects/pack &&
96 git repack -a &&
97 test_no_missing_in_packs
98 '
99
100 test_expect_success 'packed obs in alt ODB are repacked when local repo has packs' '
101 rm -f .git/objects/pack/* &&
102 echo new_content >>file1 &&
103 git add file1 &&
104 test_tick &&
105 git commit -m more_content &&
106 git repack &&
107 git repack -a -d &&
108 test_no_missing_in_packs
109 '
110
111 test_expect_success 'packed obs in alternate ODB kept pack are repacked' '
112 # swap the .keep so the commit object is in the pack with .keep
113 for p in alt_objects/pack/*.pack
114 do
115 base_name=$(basename $p .pack) &&
116 if test_path_is_file alt_objects/pack/$base_name.keep
117 then
118 rm alt_objects/pack/$base_name.keep
119 else
120 touch alt_objects/pack/$base_name.keep
121 fi || return 1
122 done &&
123 git repack -a -d &&
124 test_no_missing_in_packs
125 '
126
127 test_expect_success 'packed unreachable obs in alternate ODB are not loosened' '
128 rm -f alt_objects/pack/*.keep &&
129 mv .git/objects/pack/* alt_objects/pack/ &&
130 coid=$(git rev-parse HEAD^{commit}) &&
131 git reset --hard HEAD^ &&
132 test_tick &&
133 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
134 # The pack-objects call on the next line is equivalent to
135 # git repack -A -d without the call to prune-packed
136 git pack-objects --honor-pack-keep --non-empty --all --reflog \
137 --unpack-unreachable </dev/null pack &&
138 rm -f .git/objects/pack/* &&
139 mv pack-* .git/objects/pack/ &&
140 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
141 ! grep "^$coid " packlist &&
142 echo >.git/objects/info/alternates &&
143 test_must_fail git show $coid
144 '
145
146 test_expect_success 'local packed unreachable obs that exist in alternate ODB are not loosened' '
147 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
148 echo "$coid" | git pack-objects --non-empty --all --reflog pack &&
149 rm -f .git/objects/pack/* &&
150 mv pack-* .git/objects/pack/ &&
151 # The pack-objects call on the next line is equivalent to
152 # git repack -A -d without the call to prune-packed
153 git pack-objects --honor-pack-keep --non-empty --all --reflog \
154 --unpack-unreachable </dev/null pack &&
155 rm -f .git/objects/pack/* &&
156 mv pack-* .git/objects/pack/ &&
157 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
158 ! grep "^$coid " &&
159 echo >.git/objects/info/alternates &&
160 test_must_fail git show $coid
161 '
162
163 test_expect_success 'objects made unreachable by grafts only are kept' '
164 test_tick &&
165 git commit --allow-empty -m "commit 4" &&
166 H0=$(git rev-parse HEAD) &&
167 H1=$(git rev-parse HEAD^) &&
168 H2=$(git rev-parse HEAD^^) &&
169 echo "$H0 $H2" >.git/info/grafts &&
170 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
171 git repack -a -d &&
172 git cat-file -t $H1
173 '
174
175 test_expect_success 'repack --keep-pack' '
176 test_create_repo keep-pack &&
177 (
178 cd keep-pack &&
179 P1=$(commit_and_pack 1) &&
180 P2=$(commit_and_pack 2) &&
181 P3=$(commit_and_pack 3) &&
182 P4=$(commit_and_pack 4) &&
183 ls .git/objects/pack/*.pack >old-counts &&
184 test_line_count = 4 old-counts &&
185 git repack -a -d --keep-pack $P1 --keep-pack $P4 &&
186 ls .git/objects/pack/*.pack >new-counts &&
187 grep -q $P1 new-counts &&
188 grep -q $P4 new-counts &&
189 test_line_count = 3 new-counts &&
190 git fsck
191 )
192 '
193
194 test_expect_success 'bitmaps are created by default in bare repos' '
195 git clone --bare .git bare.git &&
196 rm -f bare.git/objects/pack/*.bitmap &&
197 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
198 git -C bare.git repack -ad &&
199 bitmap=$(ls bare.git/objects/pack/*.bitmap) &&
200 test_path_is_file "$bitmap"
201 '
202
203 test_expect_success 'incremental repack does not complain' '
204 git -C bare.git repack -q 2>repack.err &&
205 test_must_be_empty repack.err
206 '
207
208 test_expect_success 'bitmaps can be disabled on bare repos' '
209 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
210 git -c repack.writeBitmaps=false -C bare.git repack -ad &&
211 bitmap=$(ls bare.git/objects/pack/*.bitmap || :) &&
212 test -z "$bitmap"
213 '
214
215 test_expect_success 'no bitmaps created if .keep files present' '
216 pack=$(ls bare.git/objects/pack/*.pack) &&
217 test_path_is_file "$pack" &&
218 keep=${pack%.pack}.keep &&
219 test_when_finished "rm -f \"\$keep\"" &&
220 >"$keep" &&
221 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
222 git -C bare.git repack -ad 2>stderr &&
223 test_must_be_empty stderr &&
224 find bare.git/objects/pack/ -type f -name "*.bitmap" >actual &&
225 test_must_be_empty actual
226 '
227
228 test_expect_success 'auto-bitmaps do not complain if unavailable' '
229 test_config -C bare.git pack.packSizeLimit 1M &&
230 blob=$(test-tool genrandom big $((1024*1024)) |
231 git -C bare.git hash-object -w --stdin) &&
232 git -C bare.git update-ref refs/tags/big $blob &&
233 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
234 git -C bare.git repack -ad 2>stderr &&
235 test_must_be_empty stderr &&
236 find bare.git/objects/pack -type f -name "*.bitmap" >actual &&
237 test_must_be_empty actual
238 '
239
240 objdir=.git/objects
241 midx=$objdir/pack/multi-pack-index
242
243 test_expect_success 'setup for --write-midx tests' '
244 git init midx &&
245 (
246 cd midx &&
247 git config core.multiPackIndex true &&
248
249 test_commit base
250 )
251 '
252
253 test_expect_success '--write-midx unchanged' '
254 (
255 cd midx &&
256 GIT_TEST_MULTI_PACK_INDEX=0 git repack &&
257 test_path_is_missing $midx &&
258 test_path_is_missing $midx-*.bitmap &&
259
260 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
261
262 test_path_is_file $midx &&
263 test_path_is_missing $midx-*.bitmap &&
264 test_midx_consistent $objdir
265 )
266 '
267
268 test_expect_success '--write-midx with a new pack' '
269 (
270 cd midx &&
271 test_commit loose &&
272
273 GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
274
275 test_path_is_file $midx &&
276 test_path_is_missing $midx-*.bitmap &&
277 test_midx_consistent $objdir
278 )
279 '
280
281 test_expect_success '--write-midx with -b' '
282 (
283 cd midx &&
284 GIT_TEST_MULTI_PACK_INDEX=0 git repack -mb &&
285
286 test_path_is_file $midx &&
287 test_path_is_file $midx-*.bitmap &&
288 test_midx_consistent $objdir
289 )
290 '
291
292 test_expect_success '--write-midx with -d' '
293 (
294 cd midx &&
295 test_commit repack &&
296
297 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ad --write-midx &&
298
299 test_path_is_file $midx &&
300 test_path_is_missing $midx-*.bitmap &&
301 test_midx_consistent $objdir
302 )
303 '
304
305 test_expect_success 'cleans up MIDX when appropriate' '
306 (
307 cd midx &&
308
309 test_commit repack-2 &&
310 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
311
312 checksum=$(midx_checksum $objdir) &&
313 test_path_is_file $midx &&
314 test_path_is_file $midx-$checksum.bitmap &&
315
316 test_commit repack-3 &&
317 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
318
319 test_path_is_file $midx &&
320 test_path_is_missing $midx-$checksum.bitmap &&
321 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
322
323 test_commit repack-4 &&
324 GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb &&
325
326 find $objdir/pack -type f -name "multi-pack-index*" >files &&
327 test_must_be_empty files
328 )
329 '
330
331 test_expect_success '--write-midx with preferred bitmap tips' '
332 git init midx-preferred-tips &&
333 test_when_finished "rm -fr midx-preferred-tips" &&
334 (
335 cd midx-preferred-tips &&
336
337 test_commit_bulk --message="%s" 103 &&
338
339 git log --format="%H" >commits.raw &&
340 sort <commits.raw >commits &&
341
342 git log --format="create refs/tags/%s/%s %H" HEAD >refs &&
343 git update-ref --stdin <refs &&
344
345 git repack --write-midx --write-bitmap-index &&
346 test_path_is_file $midx &&
347 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
348
349 test-tool bitmap list-commits | sort >bitmaps &&
350 comm -13 bitmaps commits >before &&
351 test_line_count = 1 before &&
352
353 rm -fr $midx-$(midx_checksum $objdir).bitmap &&
354 rm -fr $midx &&
355
356 # instead of constructing the snapshot ourselves (c.f., the test
357 # "write a bitmap with --refs-snapshot (preferred tips)" in
358 # t5326), mark the missing commit as preferred by adding it to
359 # the pack.preferBitmapTips configuration.
360 git for-each-ref --format="%(refname:rstrip=1)" \
361 --points-at="$(cat before)" >missing &&
362 git config pack.preferBitmapTips "$(cat missing)" &&
363 git repack --write-midx --write-bitmap-index &&
364
365 test-tool bitmap list-commits | sort >bitmaps &&
366 comm -13 bitmaps commits >after &&
367
368 ! test_cmp before after
369 )
370 '
371
372 test_expect_success '--write-midx -b packs non-kept objects' '
373 GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
374 git repack --write-midx -a -b &&
375 test_subcommand_inexact git pack-objects --honor-pack-keep <trace.txt
376 '
377
378 test_expect_success TTY '--quiet disables progress' '
379 test_terminal env GIT_PROGRESS_DELAY=0 \
380 git -C midx repack -ad --quiet --write-midx 2>stderr &&
381 test_must_be_empty stderr
382 '
383
384 test_done