]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7700-repack.sh
Merge branch 'gc/branch-recurse-submodules-fix'
[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"
47ca93d0 8. "${TEST_DIRECTORY}/lib-terminal.sh"
9245ddd5 9
ae475afc 10commit_and_pack () {
09279086 11 test_commit "$@" 1>&2 &&
3699d69d
DL
12 incrpackid=$(git pack-objects --all --unpacked --incremental .git/objects/pack/pack </dev/null) &&
13 echo pack-${incrpackid}.pack
ed7e5fc3
NTND
14}
15
ae475afc
DL
16test_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 &&
3699d69d 20 sed -n -e "s/^\($OID_REGEX\).*/\1/p" orig.raw | sort >orig &&
ae475afc
DL
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
3699d69d 27# we expect $packid and $oid to be defined
cfe5eda0
DL
28test_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)
3699d69d 34 test "pack-$packid.idx" = "$idx" && continue
cfe5eda0 35 git verify-pack -v $p >packlist || return $?
3699d69d 36 if grep "^$oid" packlist
cfe5eda0
DL
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
dd718365 46test_expect_success 'objects in packs marked .keep are not repacked' '
7a1c8c23
DL
47 echo content1 >file1 &&
48 echo content2 >file2 &&
9245ddd5 49 git add . &&
713c79e8 50 test_tick &&
9245ddd5
BC
51 git commit -m initial_commit &&
52 # Create two packs
53 # The first pack will contain all of the objects except one
d9b31db2
DL
54 git rev-list --objects --all >objs &&
55 grep -v file2 objs | git pack-objects pack &&
9245ddd5 56 # The second pack will contain the excluded object
d9b31db2 57 packid=$(grep file2 objs | git pack-objects pack) &&
3699d69d 58 >pack-$packid.keep &&
d9b31db2
DL
59 git verify-pack -v pack-$packid.idx >packlist &&
60 oid=$(head -n 1 packlist | sed -e "s/^\($OID_REGEX\).*/\1/") &&
9245ddd5 61 mv pack-* .git/objects/pack/ &&
2d0174e3 62 git repack -A -d -l &&
9245ddd5 63 git prune-packed &&
cfe5eda0 64 test_has_duplicate_object false
9245ddd5
BC
65'
66
3198b89f 67test_expect_success 'writing bitmaps via command-line can duplicate .keep objects' '
3699d69d 68 # build on $oid, $packid, and .keep state from previous
4b58b6f7 69 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 git repack -Adbl &&
cfe5eda0 70 test_has_duplicate_object true
ee34a2be
JK
71'
72
3198b89f 73test_expect_success 'writing bitmaps via config can duplicate .keep objects' '
3699d69d 74 # build on $oid, $packid, and .keep state from previous
4b58b6f7
TB
75 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
76 git -c repack.writebitmaps=true repack -Adl &&
cfe5eda0 77 test_has_duplicate_object true
ee34a2be
JK
78'
79
daae0625 80test_expect_success 'loose objects in alternate ODB are not repacked' '
3c3df429 81 mkdir alt_objects &&
7a1c8c23
DL
82 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
83 echo content3 >file3 &&
3699d69d 84 oid=$(GIT_OBJECT_DIRECTORY=alt_objects git hash-object -w file3) &&
3c3df429 85 git add file3 &&
713c79e8 86 test_tick &&
3c3df429
BC
87 git commit -m commit_file3 &&
88 git repack -a -d -l &&
89 git prune-packed &&
cfe5eda0 90 test_has_duplicate_object false
3c3df429
BC
91'
92
3289b9de 93test_expect_success 'packed obs in alt ODB are repacked even when local repo is packless' '
2dec68cf 94 mkdir alt_objects/pack &&
3289b9de
BC
95 mv .git/objects/pack/* alt_objects/pack &&
96 git repack -a &&
ae475afc 97 test_no_missing_in_packs
3289b9de
BC
98'
99
1ef2d5a6 100test_expect_success 'packed obs in alt ODB are repacked when local repo has packs' '
a83c8852 101 rm -f .git/objects/pack/* &&
7a1c8c23 102 echo new_content >>file1 &&
a83c8852 103 git add file1 &&
713c79e8 104 test_tick &&
a83c8852
BC
105 git commit -m more_content &&
106 git repack &&
107 git repack -a -d &&
ae475afc 108 test_no_missing_in_packs
a83c8852
BC
109'
110
171110a4 111test_expect_success 'packed obs in alternate ODB kept pack are repacked' '
92cd8722
BC
112 # swap the .keep so the commit object is in the pack with .keep
113 for p in alt_objects/pack/*.pack
114 do
2dec68cf 115 base_name=$(basename $p .pack) &&
17a4ae92 116 if test_path_is_file alt_objects/pack/$base_name.keep
92cd8722
BC
117 then
118 rm alt_objects/pack/$base_name.keep
119 else
120 touch alt_objects/pack/$base_name.keep
0c51d6b4 121 fi || return 1
2dec68cf 122 done &&
92cd8722 123 git repack -a -d &&
ae475afc 124 test_no_missing_in_packs
92cd8722
BC
125'
126
79bc4c71 127test_expect_success 'packed unreachable obs in alternate ODB are not loosened' '
92cd8722
BC
128 rm -f alt_objects/pack/*.keep &&
129 mv .git/objects/pack/* alt_objects/pack/ &&
3699d69d 130 coid=$(git rev-parse HEAD^{commit}) &&
92cd8722 131 git reset --hard HEAD^ &&
713c79e8
JH
132 test_tick &&
133 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
92cd8722
BC
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/ &&
d9b31db2
DL
140 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
141 ! grep "^$coid " packlist &&
7a1c8c23 142 echo >.git/objects/info/alternates &&
3699d69d 143 test_must_fail git show $coid
92cd8722
BC
144'
145
094085e3 146test_expect_success 'local packed unreachable obs that exist in alternate ODB are not loosened' '
7a1c8c23 147 echo $(pwd)/alt_objects >.git/objects/info/alternates &&
3699d69d 148 echo "$coid" | git pack-objects --non-empty --all --reflog pack &&
869a3d34
BC
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/ &&
d9b31db2
DL
157 git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
158 ! grep "^$coid " &&
7a1c8c23 159 echo >.git/objects/info/alternates &&
3699d69d 160 test_must_fail git show $coid
869a3d34
BC
161'
162
7f3140cd 163test_expect_success 'objects made unreachable by grafts only are kept' '
1ec64827
BS
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^^) &&
7a1c8c23 169 echo "$H0 $H2" >.git/info/grafts &&
713c79e8 170 git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
1ec64827
BS
171 git repack -a -d &&
172 git cat-file -t $H1
e9e33ab0 173'
1ec64827 174
ed7e5fc3
NTND
175test_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
36eba032
EW
194test_expect_success 'bitmaps are created by default in bare repos' '
195 git clone --bare .git bare.git &&
4b58b6f7
TB
196 rm -f bare.git/objects/pack/*.bitmap &&
197 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
198 git -C bare.git repack -ad &&
36eba032
EW
199 bitmap=$(ls bare.git/objects/pack/*.bitmap) &&
200 test_path_is_file "$bitmap"
201'
202
203test_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'
9245ddd5 207
36eba032 208test_expect_success 'bitmaps can be disabled on bare repos' '
4b58b6f7
TB
209 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
210 git -c repack.writeBitmaps=false -C bare.git repack -ad &&
09279086 211 bitmap=$(ls bare.git/objects/pack/*.bitmap || :) &&
36eba032
EW
212 test -z "$bitmap"
213'
214
73284822
EW
215test_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 &&
cc2649ae 219 test_when_finished "rm -f \"\$keep\"" &&
73284822 220 >"$keep" &&
4b58b6f7
TB
221 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
222 git -C bare.git repack -ad 2>stderr &&
7ff024e7 223 test_must_be_empty stderr &&
73284822
EW
224 find bare.git/objects/pack/ -type f -name "*.bitmap" >actual &&
225 test_must_be_empty actual
226'
227
25575015
JK
228test_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 &&
4b58b6f7
TB
233 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
234 git -C bare.git repack -ad 2>stderr &&
25575015
JK
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
1d89d88d
TB
240objdir=.git/objects
241midx=$objdir/pack/multi-pack-index
242
243test_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
253test_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
268test_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
281test_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
292test_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
305test_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 &&
1d89d88d
TB
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 &&
1d89d88d 321 test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
1d89d88d
TB
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
324efc90
TB
331test_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 &&
324efc90
TB
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
f1486203
DS
372# The first argument is expected to be a filename
373# and that file should contain the name of a .idx
374# file. Send the list of objects in that .idx file
375# into stdout.
376get_sorted_objects_from_pack () {
377 git show-index <$(cat "$1") >raw &&
378 cut -d" " -f2 raw
379}
380
e4d0c11c 381test_expect_success '--write-midx -b packs non-kept objects' '
f1486203
DS
382 git init repo &&
383 test_when_finished "rm -fr repo" &&
384 (
385 cd repo &&
386
387 # Create a kept pack-file
388 test_commit base &&
389 git repack -ad &&
390 find $objdir/pack -name "*.idx" >before &&
391 test_line_count = 1 before &&
392 before_name=$(cat before) &&
393 >${before_name%.idx}.keep &&
394
395 # Create a non-kept pack-file
396 test_commit other &&
397 git repack &&
398
399 # Create loose objects
400 test_commit loose &&
401
402 # Repack everything
403 git repack --write-midx -a -b -d &&
404
405 # There should be two pack-files now, the
406 # old, kept pack and the new, non-kept pack.
407 find $objdir/pack -name "*.idx" | sort >after &&
408 test_line_count = 2 after &&
409 find $objdir/pack -name "*.keep" >kept &&
410 kept_name=$(cat kept) &&
411 echo ${kept_name%.keep}.idx >kept-idx &&
412 test_cmp before kept-idx &&
413
414 # Get object list from the kept pack.
415 get_sorted_objects_from_pack before >old.objects &&
416
417 # Get object list from the one non-kept pack-file
418 comm -13 before after >new-pack &&
419 test_line_count = 1 new-pack &&
420 get_sorted_objects_from_pack new-pack >new.objects &&
421
422 # None of the objects in the new pack should
423 # exist within the kept pack.
424 comm -12 old.objects new.objects >shared.objects &&
425 test_must_be_empty shared.objects
426 )
e4d0c11c
DS
427'
428
47ca93d0
DS
429test_expect_success TTY '--quiet disables progress' '
430 test_terminal env GIT_PROGRESS_DELAY=0 \
431 git -C midx repack -ad --quiet --write-midx 2>stderr &&
432 test_must_be_empty stderr
433'
434
64a6151d
PS
435test_expect_success 'setup for update-server-info' '
436 git init update-server-info &&
437 test_commit -C update-server-info message
438'
439
440test_server_info_present () {
441 test_path_is_file update-server-info/.git/objects/info/packs &&
442 test_path_is_file update-server-info/.git/info/refs
443}
444
445test_server_info_missing () {
446 test_path_is_missing update-server-info/.git/objects/info/packs &&
447 test_path_is_missing update-server-info/.git/info/refs
448}
449
450test_server_info_cleanup () {
451 rm -f update-server-info/.git/objects/info/packs update-server-info/.git/info/refs &&
452 test_server_info_missing
453}
454
455test_expect_success 'updates server info by default' '
456 test_server_info_cleanup &&
457 git -C update-server-info repack &&
458 test_server_info_present
459'
460
461test_expect_success '-n skips updating server info' '
462 test_server_info_cleanup &&
463 git -C update-server-info repack -n &&
464 test_server_info_missing
465'
466
a2565c48
PS
467test_expect_success 'repack.updateServerInfo=true updates server info' '
468 test_server_info_cleanup &&
469 git -C update-server-info -c repack.updateServerInfo=true repack &&
470 test_server_info_present
471'
472
473test_expect_success 'repack.updateServerInfo=false skips updating server info' '
474 test_server_info_cleanup &&
475 git -C update-server-info -c repack.updateServerInfo=false repack &&
476 test_server_info_missing
477'
478
479test_expect_success '-n overrides repack.updateServerInfo=true' '
480 test_server_info_cleanup &&
481 git -C update-server-info -c repack.updateServerInfo=true repack -n &&
482 test_server_info_missing
483'
484
36eba032 485test_done