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