]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5319-multi-pack-index.sh
midx: avoid opening multiple MIDXs when writing
[thirdparty/git.git] / t / t5319-multi-pack-index.sh
CommitLineData
a3407730
DS
1#!/bin/sh
2
3test_description='multi-pack-indexes'
4. ./test-lib.sh
5
52fe41ff 6GIT_TEST_MULTI_PACK_INDEX=0
c4d25228
DS
7objdir=.git/objects
8
d9607542
DS
9HASH_LEN=$(test_oid rawsz)
10
4d80560c 11midx_read_expect () {
396f2570 12 NUM_PACKS=$1
d7cacf29 13 NUM_OBJECTS=$2
662148c4
DS
14 NUM_CHUNKS=$3
15 OBJECT_DIR=$4
16 EXTRA_CHUNKS="$5"
3227565c
DS
17 {
18 cat <<-EOF &&
d9607542 19 header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
662148c4 20 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
d7cacf29 21 num_objects: $NUM_OBJECTS
3227565c
DS
22 packs:
23 EOF
24 if test $NUM_PACKS -ge 1
25 then
662148c4 26 ls $OBJECT_DIR/pack/ | grep idx | sort
3227565c 27 fi &&
662148c4 28 printf "object-dir: $OBJECT_DIR\n"
3227565c 29 } >expect &&
662148c4 30 test-tool read-midx $OBJECT_DIR >actual &&
4d80560c
DS
31 test_cmp expect actual
32}
33
3c5e65ca 34test_expect_success 'setup' '
3c5e65ca 35 test_oid_cache <<-EOF
36 idxoff sha1:2999
37 idxoff sha256:3739
38
39 packnameoff sha1:652
40 packnameoff sha256:940
41
42 fanoutoff sha1:1
43 fanoutoff sha256:3
44 EOF
45'
46
796d61cd
DR
47test_expect_success "don't write midx with no packs" '
48 test_must_fail git multi-pack-index --object-dir=. write &&
49 test_path_is_missing pack/multi-pack-index
50'
51
d9607542 52test_expect_success SHA1 'warn if a midx contains no oid' '
796d61cd
DR
53 cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
54 test_must_fail git multi-pack-index verify &&
55 rm $objdir/pack/multi-pack-index
a3407730
DS
56'
57
2c381335
DS
58generate_objects () {
59 i=$1
60 iii=$(printf '%03i' $i)
61 {
62 test-tool genrandom "bar" 200 &&
63 test-tool genrandom "baz $iii" 50
64 } >wide_delta_$iii &&
65 {
66 test-tool genrandom "foo"$i 100 &&
67 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
68 test-tool genrandom "foo"$(( $i + 2 )) 100
69 } >deep_delta_$iii &&
70 {
71 echo $iii &&
72 test-tool genrandom "$iii" 8192
73 } >file_$iii &&
74 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
75}
76
77commit_and_list_objects () {
78 {
79 echo 101 &&
80 test-tool genrandom 100 8192;
81 } >file_101 &&
82 git update-index --add file_101 &&
83 tree=$(git write-tree) &&
84 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
85 {
86 echo $tree &&
87 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
88 } >obj-list &&
89 git reset --hard $commit
90}
91
92test_expect_success 'create objects' '
93 test_commit initial &&
94 for i in $(test_seq 1 5)
95 do
96 generate_objects $i
97 done &&
98 commit_and_list_objects
99'
100
101test_expect_success 'write midx with one v1 pack' '
c4d25228
DS
102 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
103 test_when_finished rm $objdir/pack/test-$pack.pack \
104 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
105 git multi-pack-index --object-dir=$objdir write &&
106 midx_read_expect 1 18 4 $objdir
2c381335
DS
107'
108
c4d25228 109midx_git_two_modes () {
b4a14394
JK
110 git -c core.multiPackIndex=false $1 >expect &&
111 git -c core.multiPackIndex=true $1 >actual &&
e9ab2ed7
DS
112 if [ "$2" = "sorted" ]
113 then
b4a14394
JK
114 sort <expect >expect.sorted &&
115 mv expect.sorted expect &&
116 sort <actual >actual.sorted &&
117 mv actual.sorted actual
e9ab2ed7 118 fi &&
c4d25228
DS
119 test_cmp expect actual
120}
121
122compare_results_with_midx () {
123 MSG=$1
124 test_expect_success "check normal git operations: $MSG" '
125 midx_git_two_modes "rev-list --objects --all" &&
e9ab2ed7
DS
126 midx_git_two_modes "log --raw" &&
127 midx_git_two_modes "count-objects --verbose" &&
5670ad98
JK
128 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
129 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
c4d25228
DS
130 '
131}
132
2c381335 133test_expect_success 'write midx with one v2 pack' '
c4d25228
DS
134 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
135 git multi-pack-index --object-dir=$objdir write &&
136 midx_read_expect 1 18 4 $objdir
2c381335
DS
137'
138
c4d25228
DS
139compare_results_with_midx "one v2 pack"
140
c8a45eb6 141test_expect_success 'corrupt idx reports errors' '
fc789156
JK
142 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
143 mv $objdir/pack/$idx backup-$idx &&
144 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
145
146 # This is the minimum size for a sha-1 based .idx; this lets
147 # us pass perfunctory tests, but anything that actually opens and reads
148 # the idx file will complain.
149 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
150
151 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
c8a45eb6 152 grep "index unavailable" err
fc789156
JK
153'
154
2c381335
DS
155test_expect_success 'add more objects' '
156 for i in $(test_seq 6 10)
157 do
158 generate_objects $i
159 done &&
160 commit_and_list_objects
161'
162
163test_expect_success 'write midx with two packs' '
c4d25228
DS
164 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
165 git multi-pack-index --object-dir=$objdir write &&
166 midx_read_expect 2 34 4 $objdir
2c381335
DS
167'
168
c4d25228
DS
169compare_results_with_midx "two packs"
170
680cba2c
WB
171test_expect_success 'write progress off for redirected stderr' '
172 git multi-pack-index --object-dir=$objdir write 2>err &&
173 test_line_count = 0 err
174'
175
176test_expect_success 'write force progress on for stderr' '
efdd2f0d 177 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress write 2>err &&
680cba2c
WB
178 test_file_not_empty err
179'
180
181test_expect_success 'write with the --no-progress option' '
efdd2f0d 182 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress write 2>err &&
680cba2c
WB
183 test_line_count = 0 err
184'
185
2c381335
DS
186test_expect_success 'add more packs' '
187 for j in $(test_seq 11 20)
188 do
189 generate_objects $j &&
190 commit_and_list_objects &&
c4d25228 191 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list
2c381335
DS
192 done
193'
194
c4d25228
DS
195compare_results_with_midx "mixed mode (two packs + extra)"
196
2c381335 197test_expect_success 'write midx with twelve packs' '
c4d25228
DS
198 git multi-pack-index --object-dir=$objdir write &&
199 midx_read_expect 12 74 4 $objdir
662148c4
DS
200'
201
c4d25228
DS
202compare_results_with_midx "twelve packs"
203
426c00e4
TB
204test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
205 git init repo &&
206 git clone -s repo alternate &&
207
208 test_when_finished "rm -rf repo alternate" &&
209
210 (
211 cd repo &&
212 test_commit base &&
213 git repack -d
214 ) &&
215
216 ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
217 theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
218 touch "$ours" "$theirs" &&
219
220 (
221 cd alternate &&
222 git multi-pack-index --object-dir ../repo/.git/objects write
223 ) &&
224
225 # writing a midx in "repo" should not remove the .rev file in the
226 # alternate
227 test_path_is_file repo/.git/objects/pack/multi-pack-index &&
228 test_path_is_file $ours &&
229 test_path_is_missing $theirs
230'
231
d9607542
DS
232test_expect_success 'warn on improper hash version' '
233 git init --object-format=sha1 sha1 &&
234 (
235 cd sha1 &&
236 git config core.multiPackIndex true &&
237 test_commit 1 &&
238 git repack -a &&
239 git multi-pack-index write &&
240 mv .git/objects/pack/multi-pack-index ../mpi-sha1
241 ) &&
242 git init --object-format=sha256 sha256 &&
243 (
244 cd sha256 &&
245 git config core.multiPackIndex true &&
246 test_commit 1 &&
247 git repack -a &&
248 git multi-pack-index write &&
249 mv .git/objects/pack/multi-pack-index ../mpi-sha256
250 ) &&
251 (
252 cd sha1 &&
253 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
254 git log -1 2>err &&
255 test_i18ngrep "multi-pack-index hash version 2 does not match version 1" err
256 ) &&
257 (
258 cd sha256 &&
259 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
260 git log -1 2>err &&
261 test_i18ngrep "multi-pack-index hash version 1 does not match version 2" err
262 )
263'
264
9218c6a4
TB
265test_expect_success 'midx picks objects from preferred pack' '
266 test_when_finished rm -rf preferred.git &&
267 git init --bare preferred.git &&
268 (
269 cd preferred.git &&
270
271 a=$(echo "a" | git hash-object -w --stdin) &&
272 b=$(echo "b" | git hash-object -w --stdin) &&
273 c=$(echo "c" | git hash-object -w --stdin) &&
274
275 # Set up two packs, duplicating the object "B" at different
276 # offsets.
277 #
278 # Note that the "BC" pack (the one we choose as preferred) sorts
279 # lexically after the "AB" pack, meaning that omitting the
280 # --preferred-pack argument would cause this test to fail (since
281 # the MIDX code would select the copy of "b" in the "AB" pack).
282 git pack-objects objects/pack/test-AB <<-EOF &&
283 $a
284 $b
285 EOF
286 bc=$(git pack-objects objects/pack/test-BC <<-EOF
287 $b
288 $c
289 EOF
290 ) &&
291
292 git multi-pack-index --object-dir=objects \
293 write --preferred-pack=test-BC-$bc.idx 2>err &&
294 test_must_be_empty err &&
295
296 test-tool read-midx --show-objects objects >out &&
297
298 ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
299 cut -d" " -f1) &&
300 printf "%s %s\tobjects/pack/test-BC-%s.pack\n" \
301 "$b" "$ofs" "$bc" >expect &&
302 grep ^$b out >actual &&
303
304 test_cmp expect actual
305 )
306'
d9607542 307
5d3cd09a
TB
308test_expect_success 'preferred packs must be non-empty' '
309 test_when_finished rm -rf preferred.git &&
310 git init preferred.git &&
311 (
312 cd preferred.git &&
313
314 test_commit base &&
315 git repack -ad &&
316
317 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
318
319 test_must_fail git multi-pack-index write \
320 --preferred-pack=pack-$empty.pack 2>err &&
321 grep "with no objects" err
322 )
323'
324
56ee7ff1
DS
325test_expect_success 'verify multi-pack-index success' '
326 git multi-pack-index verify --object-dir=$objdir
327'
328
680cba2c
WB
329test_expect_success 'verify progress off for redirected stderr' '
330 git multi-pack-index verify --object-dir=$objdir 2>err &&
331 test_line_count = 0 err
332'
333
334test_expect_success 'verify force progress on for stderr' '
335 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
336 test_file_not_empty err
337'
338
339test_expect_success 'verify with the --no-progress option' '
340 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
341 test_line_count = 0 err
342'
343
53ad0407
DS
344# usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
345corrupt_midx_and_verify() {
346 POS=$1 &&
347 DATA="${2:-\0}" &&
348 OBJDIR=$3 &&
349 GREPSTR="$4" &&
66ec0390
DS
350 COMMAND="$5" &&
351 if test -z "$COMMAND"
352 then
353 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
354 fi &&
53ad0407
DS
355 FILE=$OBJDIR/pack/multi-pack-index &&
356 chmod a+w $FILE &&
357 test_when_finished mv midx-backup $FILE &&
358 cp $FILE midx-backup &&
359 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
66ec0390 360 test_must_fail $COMMAND 2>test_err &&
53ad0407
DS
361 grep -v "^+" test_err >err &&
362 test_i18ngrep "$GREPSTR" err
363}
364
365test_expect_success 'verify bad signature' '
366 corrupt_midx_and_verify 0 "\00" $objdir \
367 "multi-pack-index signature"
368'
369
cc6af73c 370NUM_OBJECTS=74
53ad0407
DS
371MIDX_BYTE_VERSION=4
372MIDX_BYTE_OID_VERSION=5
373MIDX_BYTE_CHUNK_COUNT=6
d3f8e211
DS
374MIDX_HEADER_SIZE=12
375MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
376MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
8e72a3c3
DS
377MIDX_NUM_CHUNKS=5
378MIDX_CHUNK_LOOKUP_WIDTH=12
379MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
380 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
381MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
3c5e65ca 382MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
2f23d3f3 383MIDX_OID_FANOUT_WIDTH=4
3c5e65ca 384MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
55c5648d
DS
385MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
386MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
cc6af73c
DS
387MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
388MIDX_OFFSET_WIDTH=8
389MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
390MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
53ad0407
DS
391
392test_expect_success 'verify bad version' '
393 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
394 "multi-pack-index version"
395'
396
397test_expect_success 'verify bad OID version' '
d9607542 398 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
53ad0407
DS
399 "hash version"
400'
401
402test_expect_success 'verify truncated chunk count' '
403 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
6ab3b8b8 404 "final chunk has non-zero id"
53ad0407
DS
405'
406
407test_expect_success 'verify extended chunk count' '
408 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
6ab3b8b8 409 "terminating chunk id appears earlier than expected"
53ad0407
DS
410'
411
d3f8e211
DS
412test_expect_success 'verify missing required chunk' '
413 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
414 "missing required"
415'
416
417test_expect_success 'verify invalid chunk offset' '
418 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
6ab3b8b8 419 "improper chunk offset(s)"
d3f8e211
DS
420'
421
8e72a3c3
DS
422test_expect_success 'verify packnames out of order' '
423 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
424 "pack names out of order"
425'
426
d4bf1d88
DS
427test_expect_success 'verify packnames out of order' '
428 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
429 "failed to load pack"
430'
431
2f23d3f3
DS
432test_expect_success 'verify oid fanout out of order' '
433 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
434 "oid fanout out of order"
435'
436
55c5648d
DS
437test_expect_success 'verify oid lookup out of order' '
438 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
439 "oid lookup out of order"
440'
441
cc6af73c
DS
442test_expect_success 'verify incorrect pack-int-id' '
443 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
444 "bad pack-int-id"
445'
446
447test_expect_success 'verify incorrect offset' '
235d3cdd 448 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
cc6af73c
DS
449 "incorrect object offset"
450'
451
66ec0390 452test_expect_success 'git-fsck incorrect offset' '
235d3cdd 453 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
66ec0390
DS
454 "incorrect object offset" \
455 "git -c core.multipackindex=true fsck"
456'
457
ec1e28ef
TB
458test_expect_success 'corrupt MIDX is not reused' '
459 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
460 "incorrect object offset" &&
461 git multi-pack-index write 2>err &&
462 test_i18ngrep checksum.mismatch err &&
463 git multi-pack-index verify
464'
465
f89ecf79
TB
466test_expect_success 'verify incorrect checksum' '
467 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 1)) &&
468 corrupt_midx_and_verify $pos "\377" $objdir "incorrect checksum"
469'
470
680cba2c 471test_expect_success 'repack progress off for redirected stderr' '
efdd2f0d 472 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
680cba2c
WB
473 test_line_count = 0 err
474'
475
476test_expect_success 'repack force progress on for stderr' '
efdd2f0d 477 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --progress repack 2>err &&
680cba2c
WB
478 test_file_not_empty err
479'
480
481test_expect_success 'repack with the --no-progress option' '
efdd2f0d 482 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir --no-progress repack 2>err &&
680cba2c
WB
483 test_line_count = 0 err
484'
485
e08f7bb0 486test_expect_success 'repack removes multi-pack-index when deleting packs' '
525e18c0 487 test_path_is_file $objdir/pack/multi-pack-index &&
e08f7bb0
TB
488 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
489 # multi-pack-index after repacking, but set "core.multiPackIndex" to
490 # true so that "git repack" can read the existing MIDX.
491 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
525e18c0
DS
492 test_path_is_missing $objdir/pack/multi-pack-index
493'
494
e08f7bb0
TB
495test_expect_success 'repack preserves multi-pack-index when creating packs' '
496 git init preserve &&
497 test_when_finished "rm -fr preserve" &&
498 (
499 cd preserve &&
500 packdir=.git/objects/pack &&
501 midx=$packdir/multi-pack-index &&
502
503 test_commit 1 &&
504 pack1=$(git pack-objects --all $packdir/pack) &&
505 touch $packdir/pack-$pack1.keep &&
506 test_commit 2 &&
507 pack2=$(git pack-objects --revs $packdir/pack) &&
508 touch $packdir/pack-$pack2.keep &&
509
510 git multi-pack-index write &&
511 cp $midx $midx.bak &&
512
513 cat >pack-input <<-EOF &&
514 HEAD
515 ^HEAD~1
516 EOF
517 test_commit 3 &&
518 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
519 test_commit 4 &&
520 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
521
522 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
523 ls -la $packdir &&
524 test_path_is_file $packdir/pack-$pack1.pack &&
525 test_path_is_file $packdir/pack-$pack2.pack &&
526 test_path_is_missing $packdir/pack-$pack3.pack &&
527 test_path_is_missing $packdir/pack-$pack4.pack &&
528 test_cmp_bin $midx.bak $midx
529 )
530'
531
525e18c0
DS
532compare_results_with_midx "after repack"
533
e9ab2ed7
DS
534test_expect_success 'multi-pack-index and pack-bitmap' '
535 git -c repack.writeBitmaps=true repack -ad &&
536 git multi-pack-index write &&
537 git rev-list --test-bitmap HEAD
538'
539
29e2016b
DS
540test_expect_success 'multi-pack-index and alternates' '
541 git init --bare alt.git &&
542 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
543 echo content1 >file1 &&
544 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
545 git cat-file blob $altblob &&
546 git rev-list --all
547'
548
549compare_results_with_midx "with alternate (local midx)"
550
551test_expect_success 'multi-pack-index in an alternate' '
6a22d521
DS
552 mv .git/objects/pack/* alt.git/objects/pack &&
553 test_commit add_local_objects &&
554 git repack --local &&
555 git multi-pack-index write &&
556 midx_read_expect 1 3 4 $objdir &&
557 git reset --hard HEAD~1 &&
558 rm -f .git/objects/pack/*
29e2016b
DS
559'
560
561compare_results_with_midx "with alternate (remote midx)"
562
662148c4
DS
563# usage: corrupt_data <file> <pos> [<data>]
564corrupt_data () {
565 file=$1
566 pos=$2
567 data="${3:-\0}"
568 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
569}
570
571# Force 64-bit offsets by manipulating the idx file.
572# This makes the IDX file _incorrect_ so be careful to clean up after!
573test_expect_success 'force some 64-bit offsets with pack-objects' '
574 mkdir objects64 &&
575 mkdir objects64/pack &&
576 for i in $(test_seq 1 11)
577 do
578 generate_objects 11
579 done &&
580 commit_and_list_objects &&
581 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
582 idx64=objects64/pack/test-64-$pack64.idx &&
583 chmod u+w $idx64 &&
3c5e65ca 584 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
f57a7396
TB
585 # objects64 is not a real repository, but can serve as an alternate
586 # anyway so we can write a MIDX into it
587 git init repo &&
588 test_when_finished "rm -fr repo" &&
589 (
590 cd repo &&
591 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
592 midx64=$(git multi-pack-index --object-dir=../objects64 write)
593 ) &&
662148c4 594 midx_read_expect 1 63 5 objects64 " large-offsets"
2c381335
DS
595'
596
56ee7ff1
DS
597test_expect_success 'verify multi-pack-index with 64-bit offsets' '
598 git multi-pack-index verify --object-dir=objects64
599'
600
cc6af73c
DS
601NUM_OBJECTS=63
602MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
603MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
604MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
605MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
606MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
607
608test_expect_success 'verify incorrect 64-bit offset' '
609 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
610 "incorrect object offset"
611'
612
cff97116
DS
613test_expect_success 'setup expire tests' '
614 mkdir dup &&
615 (
616 cd dup &&
617 git init &&
618 test-tool genrandom "data" 4096 >large_file.txt &&
619 git update-index --add large_file.txt &&
620 for i in $(test_seq 1 20)
621 do
622 test_commit $i
623 done &&
624 git branch A HEAD &&
625 git branch B HEAD~8 &&
626 git branch C HEAD~13 &&
627 git branch D HEAD~16 &&
628 git branch E HEAD~18 &&
629 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
630 refs/heads/A
631 ^refs/heads/B
632 EOF
633 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
634 refs/heads/B
635 ^refs/heads/C
636 EOF
637 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
638 refs/heads/C
639 ^refs/heads/D
640 EOF
641 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
642 refs/heads/D
643 ^refs/heads/E
644 EOF
645 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
646 refs/heads/E
647 EOF
2af890bb
DS
648 git multi-pack-index write &&
649 cp -r .git/objects/pack .git/objects/pack-backup
cff97116
DS
650 )
651'
652
653test_expect_success 'expire does not remove any packs' '
654 (
655 cd dup &&
656 ls .git/objects/pack >expect &&
657 git multi-pack-index expire &&
658 ls .git/objects/pack >actual &&
659 test_cmp expect actual
660 )
661'
662
680cba2c
WB
663test_expect_success 'expire progress off for redirected stderr' '
664 (
665 cd dup &&
666 git multi-pack-index expire 2>err &&
667 test_line_count = 0 err
668 )
669'
670
671test_expect_success 'expire force progress on for stderr' '
672 (
673 cd dup &&
efdd2f0d 674 GIT_PROGRESS_DELAY=0 git multi-pack-index --progress expire 2>err &&
680cba2c
WB
675 test_file_not_empty err
676 )
677'
678
679test_expect_success 'expire with the --no-progress option' '
680 (
681 cd dup &&
efdd2f0d 682 GIT_PROGRESS_DELAY=0 git multi-pack-index --no-progress expire 2>err &&
680cba2c
WB
683 test_line_count = 0 err
684 )
685'
686
19575c7c
DS
687test_expect_success 'expire removes unreferenced packs' '
688 (
689 cd dup &&
690 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
691 refs/heads/A
692 ^refs/heads/C
693 EOF
694 git multi-pack-index write &&
695 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
696 git multi-pack-index expire &&
697 ls .git/objects/pack >actual &&
698 test_cmp expect actual &&
699 ls .git/objects/pack/ | grep idx >expect-idx &&
700 test-tool read-midx .git/objects | grep idx >actual-midx &&
701 test_cmp expect-idx actual-midx &&
702 git multi-pack-index verify &&
703 git fsck
704 )
705'
706
2af890bb
DS
707test_expect_success 'repack with minimum size does not alter existing packs' '
708 (
709 cd dup &&
710 rm -rf .git/objects/pack &&
711 mv .git/objects/pack-backup .git/objects/pack &&
e892a568
DS
712 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
713 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
714 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
715 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
2af890bb 716 ls .git/objects/pack >expect &&
3612c233 717 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
2af890bb
DS
718 git multi-pack-index repack --batch-size=$MINSIZE &&
719 ls .git/objects/pack >actual &&
720 test_cmp expect actual
721 )
722'
723
3ce4ca0a
DS
724test_expect_success 'repack respects repack.packKeptObjects=false' '
725 test_when_finished rm -f dup/.git/objects/pack/*keep &&
726 (
727 cd dup &&
728 ls .git/objects/pack/*idx >idx-list &&
729 test_line_count = 5 idx-list &&
730 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
731 test_line_count = 5 keep-list &&
732 for keep in $(cat keep-list)
733 do
734 touch $keep || return 1
735 done &&
736 git multi-pack-index repack --batch-size=0 &&
737 ls .git/objects/pack/*idx >idx-list &&
738 test_line_count = 5 idx-list &&
739 test-tool read-midx .git/objects | grep idx >midx-list &&
740 test_line_count = 5 midx-list &&
741 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
742 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
743 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
744 ls .git/objects/pack/*idx >idx-list &&
745 test_line_count = 5 idx-list &&
746 test-tool read-midx .git/objects | grep idx >midx-list &&
747 test_line_count = 5 midx-list
748 )
749'
750
ce1e4a10
DS
751test_expect_success 'repack creates a new pack' '
752 (
753 cd dup &&
754 ls .git/objects/pack/*idx >idx-list &&
755 test_line_count = 5 idx-list &&
3612c233 756 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
ce1e4a10
DS
757 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
758 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
759 ls .git/objects/pack/*idx >idx-list &&
760 test_line_count = 6 idx-list &&
761 test-tool read-midx .git/objects | grep idx >midx-list &&
762 test_line_count = 6 midx-list
763 )
764'
765
766test_expect_success 'expire removes repacked packs' '
767 (
768 cd dup &&
769 ls -al .git/objects/pack/*pack &&
770 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
771 git multi-pack-index expire &&
772 ls -S .git/objects/pack/*pack >actual &&
773 test_cmp expect actual &&
774 test-tool read-midx .git/objects | grep idx >midx-list &&
775 test_line_count = 4 midx-list
776 )
777'
778
d2743315
DS
779test_expect_success 'expire works when adding new packs' '
780 (
781 cd dup &&
782 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
783 refs/heads/A
784 ^refs/heads/B
785 EOF
786 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
787 refs/heads/B
788 ^refs/heads/C
789 EOF
790 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
791 refs/heads/C
792 ^refs/heads/D
793 EOF
794 git multi-pack-index write &&
795 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
796 refs/heads/D
797 ^refs/heads/E
798 EOF
799 git multi-pack-index write &&
800 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
801 refs/heads/E
802 EOF
803 git multi-pack-index expire &&
804 ls .git/objects/pack/ | grep idx >expect &&
805 test-tool read-midx .git/objects | grep idx >actual &&
806 test_cmp expect actual &&
807 git multi-pack-index verify
808 )
809'
810
10bfa3f7
DS
811test_expect_success 'expire respects .keep files' '
812 (
813 cd dup &&
814 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
815 refs/heads/A
816 EOF
817 git multi-pack-index write &&
818 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
819 touch $PACKA.keep &&
820 git multi-pack-index expire &&
35a8a354
TB
821 test_path_is_file $PACKA.idx &&
822 test_path_is_file $PACKA.keep &&
823 test_path_is_file $PACKA.pack &&
10bfa3f7
DS
824 test-tool read-midx .git/objects | grep idx >midx-list &&
825 test_line_count = 2 midx-list
826 )
827'
828
b526d8cb 829test_expect_success 'repack --batch-size=0 repacks everything' '
1eb22c7d 830 cp -r dup dup2 &&
b526d8cb
DS
831 (
832 cd dup &&
833 rm .git/objects/pack/*.keep &&
834 ls .git/objects/pack/*idx >idx-list &&
835 test_line_count = 2 idx-list &&
836 git multi-pack-index repack --batch-size=0 &&
837 ls .git/objects/pack/*idx >idx-list &&
838 test_line_count = 3 idx-list &&
839 test-tool read-midx .git/objects | grep idx >midx-list &&
840 test_line_count = 3 midx-list &&
841 git multi-pack-index expire &&
842 ls -al .git/objects/pack/*idx >idx-list &&
843 test_line_count = 1 idx-list &&
844 git multi-pack-index repack --batch-size=0 &&
845 ls -al .git/objects/pack/*idx >new-idx-list &&
846 test_cmp idx-list new-idx-list
847 )
848'
10bfa3f7 849
1eb22c7d
DS
850test_expect_success 'repack --batch-size=<large> repacks everything' '
851 (
852 cd dup2 &&
853 rm .git/objects/pack/*.keep &&
854 ls .git/objects/pack/*idx >idx-list &&
855 test_line_count = 2 idx-list &&
856 git multi-pack-index repack --batch-size=2000000 &&
857 ls .git/objects/pack/*idx >idx-list &&
858 test_line_count = 3 idx-list &&
859 test-tool read-midx .git/objects | grep idx >midx-list &&
860 test_line_count = 3 midx-list &&
861 git multi-pack-index expire &&
862 ls -al .git/objects/pack/*idx >idx-list &&
863 test_line_count = 1 idx-list
864 )
865'
866
506ec2fb 867test_expect_success 'load reverse index when missing .idx, .pack' '
c8a45eb6
TB
868 git init repo &&
869 test_when_finished "rm -fr repo" &&
870 (
871 cd repo &&
872
873 git config core.multiPackIndex true &&
874
875 test_commit base &&
876 git repack -ad &&
877 git multi-pack-index write &&
878
879 git rev-parse HEAD >tip &&
506ec2fb 880 pack=$(ls .git/objects/pack/pack-*.pack) &&
c8a45eb6
TB
881 idx=$(ls .git/objects/pack/pack-*.idx) &&
882
883 mv $idx $idx.bak &&
506ec2fb
TB
884 git cat-file --batch-check="%(objectsize:disk)" <tip &&
885
886 mv $idx.bak $idx &&
887
888 mv $pack $pack.bak &&
c8a45eb6
TB
889 git cat-file --batch-check="%(objectsize:disk)" <tip
890 )
891'
892
88617d11
TB
893test_expect_success 'usage shown without sub-command' '
894 test_expect_code 129 git multi-pack-index 2>err &&
895 ! test_i18ngrep "unrecognized subcommand" err
896'
897
73ff4ad0
TB
898test_expect_success 'complains when run outside of a repository' '
899 nongit test_must_fail git multi-pack-index write 2>err &&
900 grep "not a git repository" err
901'
902
a3407730 903test_done