]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5319-multi-pack-index.sh
Merge branch 'tz/lib-gpg-prereq-fix'
[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
d0fd9931 96 generate_objects $i || return 1
2c381335
DS
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
d0fd9931 158 generate_objects $i || return 1
2c381335
DS
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
6fb22ca4
TB
171test_expect_success 'write midx with --stdin-packs' '
172 rm -fr $objdir/pack/multi-pack-index &&
173
174 idx="$(find $objdir/pack -name "test-2-*.idx")" &&
175 basename "$idx" >in &&
176
177 git multi-pack-index write --stdin-packs <in &&
178
179 test-tool read-midx $objdir | grep "\.idx$" >packs &&
180
181 test_cmp packs in
182'
183
184compare_results_with_midx "mixed mode (one pack + extra)"
185
ceb96a16
PS
186test_expect_success 'write with no objects and preferred pack' '
187 test_when_finished "rm -rf empty" &&
188 git init empty &&
189 test_must_fail git -C empty multi-pack-index write \
190 --stdin-packs --preferred-pack=does-not-exist </dev/null 2>err &&
191 cat >expect <<-EOF &&
192 warning: unknown preferred pack: ${SQ}does-not-exist${SQ}
193 error: no pack files to index.
194 EOF
195 test_cmp expect err
196'
197
680cba2c
WB
198test_expect_success 'write progress off for redirected stderr' '
199 git multi-pack-index --object-dir=$objdir write 2>err &&
200 test_line_count = 0 err
201'
202
203test_expect_success 'write force progress on for stderr' '
0394f8d0 204 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --progress 2>err &&
680cba2c
WB
205 test_file_not_empty err
206'
207
208test_expect_success 'write with the --no-progress option' '
0394f8d0 209 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --no-progress 2>err &&
680cba2c
WB
210 test_line_count = 0 err
211'
212
2c381335
DS
213test_expect_success 'add more packs' '
214 for j in $(test_seq 11 20)
215 do
216 generate_objects $j &&
217 commit_and_list_objects &&
d0fd9931 218 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list || return 1
2c381335
DS
219 done
220'
221
c4d25228
DS
222compare_results_with_midx "mixed mode (two packs + extra)"
223
2c381335 224test_expect_success 'write midx with twelve packs' '
c4d25228
DS
225 git multi-pack-index --object-dir=$objdir write &&
226 midx_read_expect 12 74 4 $objdir
662148c4
DS
227'
228
c4d25228
DS
229compare_results_with_midx "twelve packs"
230
426c00e4
TB
231test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
232 git init repo &&
233 git clone -s repo alternate &&
234
235 test_when_finished "rm -rf repo alternate" &&
236
237 (
238 cd repo &&
239 test_commit base &&
240 git repack -d
241 ) &&
242
243 ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
244 theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
245 touch "$ours" "$theirs" &&
246
247 (
248 cd alternate &&
249 git multi-pack-index --object-dir ../repo/.git/objects write
250 ) &&
251
252 # writing a midx in "repo" should not remove the .rev file in the
253 # alternate
254 test_path_is_file repo/.git/objects/pack/multi-pack-index &&
255 test_path_is_file $ours &&
256 test_path_is_missing $theirs
257'
258
d9607542
DS
259test_expect_success 'warn on improper hash version' '
260 git init --object-format=sha1 sha1 &&
261 (
262 cd sha1 &&
263 git config core.multiPackIndex true &&
264 test_commit 1 &&
265 git repack -a &&
266 git multi-pack-index write &&
267 mv .git/objects/pack/multi-pack-index ../mpi-sha1
268 ) &&
269 git init --object-format=sha256 sha256 &&
270 (
271 cd sha256 &&
272 git config core.multiPackIndex true &&
273 test_commit 1 &&
274 git repack -a &&
275 git multi-pack-index write &&
276 mv .git/objects/pack/multi-pack-index ../mpi-sha256
277 ) &&
278 (
279 cd sha1 &&
280 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
281 git log -1 2>err &&
282 test_i18ngrep "multi-pack-index hash version 2 does not match version 1" err
283 ) &&
284 (
285 cd sha256 &&
286 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
287 git log -1 2>err &&
288 test_i18ngrep "multi-pack-index hash version 1 does not match version 2" err
289 )
290'
291
9218c6a4
TB
292test_expect_success 'midx picks objects from preferred pack' '
293 test_when_finished rm -rf preferred.git &&
294 git init --bare preferred.git &&
295 (
296 cd preferred.git &&
297
298 a=$(echo "a" | git hash-object -w --stdin) &&
299 b=$(echo "b" | git hash-object -w --stdin) &&
300 c=$(echo "c" | git hash-object -w --stdin) &&
301
302 # Set up two packs, duplicating the object "B" at different
303 # offsets.
304 #
305 # Note that the "BC" pack (the one we choose as preferred) sorts
306 # lexically after the "AB" pack, meaning that omitting the
307 # --preferred-pack argument would cause this test to fail (since
308 # the MIDX code would select the copy of "b" in the "AB" pack).
309 git pack-objects objects/pack/test-AB <<-EOF &&
310 $a
311 $b
312 EOF
313 bc=$(git pack-objects objects/pack/test-BC <<-EOF
314 $b
315 $c
316 EOF
317 ) &&
318
319 git multi-pack-index --object-dir=objects \
320 write --preferred-pack=test-BC-$bc.idx 2>err &&
321 test_must_be_empty err &&
322
323 test-tool read-midx --show-objects objects >out &&
324
325 ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
326 cut -d" " -f1) &&
327 printf "%s %s\tobjects/pack/test-BC-%s.pack\n" \
328 "$b" "$ofs" "$bc" >expect &&
329 grep ^$b out >actual &&
330
331 test_cmp expect actual
332 )
333'
d9607542 334
5d3cd09a
TB
335test_expect_success 'preferred packs must be non-empty' '
336 test_when_finished rm -rf preferred.git &&
337 git init preferred.git &&
338 (
339 cd preferred.git &&
340
341 test_commit base &&
342 git repack -ad &&
343
344 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
345
346 test_must_fail git multi-pack-index write \
347 --preferred-pack=pack-$empty.pack 2>err &&
348 grep "with no objects" err
349 )
350'
351
56ee7ff1
DS
352test_expect_success 'verify multi-pack-index success' '
353 git multi-pack-index verify --object-dir=$objdir
354'
355
680cba2c
WB
356test_expect_success 'verify progress off for redirected stderr' '
357 git multi-pack-index verify --object-dir=$objdir 2>err &&
358 test_line_count = 0 err
359'
360
361test_expect_success 'verify force progress on for stderr' '
362 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
363 test_file_not_empty err
364'
365
366test_expect_success 'verify with the --no-progress option' '
367 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
368 test_line_count = 0 err
369'
370
53ad0407
DS
371# usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
372corrupt_midx_and_verify() {
373 POS=$1 &&
374 DATA="${2:-\0}" &&
375 OBJDIR=$3 &&
376 GREPSTR="$4" &&
66ec0390
DS
377 COMMAND="$5" &&
378 if test -z "$COMMAND"
379 then
380 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
381 fi &&
53ad0407
DS
382 FILE=$OBJDIR/pack/multi-pack-index &&
383 chmod a+w $FILE &&
384 test_when_finished mv midx-backup $FILE &&
385 cp $FILE midx-backup &&
386 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
66ec0390 387 test_must_fail $COMMAND 2>test_err &&
53ad0407
DS
388 grep -v "^+" test_err >err &&
389 test_i18ngrep "$GREPSTR" err
390}
391
392test_expect_success 'verify bad signature' '
393 corrupt_midx_and_verify 0 "\00" $objdir \
394 "multi-pack-index signature"
395'
396
cc6af73c 397NUM_OBJECTS=74
53ad0407
DS
398MIDX_BYTE_VERSION=4
399MIDX_BYTE_OID_VERSION=5
400MIDX_BYTE_CHUNK_COUNT=6
d3f8e211
DS
401MIDX_HEADER_SIZE=12
402MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
403MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
8e72a3c3
DS
404MIDX_NUM_CHUNKS=5
405MIDX_CHUNK_LOOKUP_WIDTH=12
406MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
407 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
408MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
3c5e65ca 409MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
2f23d3f3 410MIDX_OID_FANOUT_WIDTH=4
3c5e65ca 411MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
55c5648d
DS
412MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
413MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
cc6af73c
DS
414MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
415MIDX_OFFSET_WIDTH=8
416MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
417MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
53ad0407
DS
418
419test_expect_success 'verify bad version' '
420 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
421 "multi-pack-index version"
422'
423
424test_expect_success 'verify bad OID version' '
d9607542 425 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
53ad0407
DS
426 "hash version"
427'
428
429test_expect_success 'verify truncated chunk count' '
430 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
6ab3b8b8 431 "final chunk has non-zero id"
53ad0407
DS
432'
433
434test_expect_success 'verify extended chunk count' '
435 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
6ab3b8b8 436 "terminating chunk id appears earlier than expected"
53ad0407
DS
437'
438
d3f8e211
DS
439test_expect_success 'verify missing required chunk' '
440 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
441 "missing required"
442'
443
444test_expect_success 'verify invalid chunk offset' '
445 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
6ab3b8b8 446 "improper chunk offset(s)"
d3f8e211
DS
447'
448
8e72a3c3
DS
449test_expect_success 'verify packnames out of order' '
450 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
451 "pack names out of order"
452'
453
d4bf1d88
DS
454test_expect_success 'verify packnames out of order' '
455 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
456 "failed to load pack"
457'
458
2f23d3f3
DS
459test_expect_success 'verify oid fanout out of order' '
460 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
461 "oid fanout out of order"
462'
463
55c5648d
DS
464test_expect_success 'verify oid lookup out of order' '
465 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
466 "oid lookup out of order"
467'
468
cc6af73c
DS
469test_expect_success 'verify incorrect pack-int-id' '
470 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
471 "bad pack-int-id"
472'
473
474test_expect_success 'verify incorrect offset' '
235d3cdd 475 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
cc6af73c
DS
476 "incorrect object offset"
477'
478
66ec0390 479test_expect_success 'git-fsck incorrect offset' '
235d3cdd 480 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
66ec0390 481 "incorrect object offset" \
dc557087
GC
482 "git -c core.multiPackIndex=true fsck" &&
483 test_unconfig core.multiPackIndex &&
484 test_must_fail git fsck &&
485 git -c core.multiPackIndex=false fsck
66ec0390
DS
486'
487
ec1e28ef
TB
488test_expect_success 'corrupt MIDX is not reused' '
489 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
490 "incorrect object offset" &&
491 git multi-pack-index write 2>err &&
492 test_i18ngrep checksum.mismatch err &&
493 git multi-pack-index verify
494'
495
f89ecf79 496test_expect_success 'verify incorrect checksum' '
152923b1
JK
497 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
498 corrupt_midx_and_verify $pos \
499 "\377\377\377\377\377\377\377\377\377\377" \
500 $objdir "incorrect checksum"
f89ecf79
TB
501'
502
680cba2c 503test_expect_success 'repack progress off for redirected stderr' '
efdd2f0d 504 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
680cba2c
WB
505 test_line_count = 0 err
506'
507
508test_expect_success 'repack force progress on for stderr' '
0394f8d0 509 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
680cba2c
WB
510 test_file_not_empty err
511'
512
513test_expect_success 'repack with the --no-progress option' '
0394f8d0 514 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
680cba2c
WB
515 test_line_count = 0 err
516'
517
e08f7bb0 518test_expect_success 'repack removes multi-pack-index when deleting packs' '
525e18c0 519 test_path_is_file $objdir/pack/multi-pack-index &&
e08f7bb0
TB
520 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
521 # multi-pack-index after repacking, but set "core.multiPackIndex" to
522 # true so that "git repack" can read the existing MIDX.
523 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
525e18c0
DS
524 test_path_is_missing $objdir/pack/multi-pack-index
525'
526
e08f7bb0
TB
527test_expect_success 'repack preserves multi-pack-index when creating packs' '
528 git init preserve &&
529 test_when_finished "rm -fr preserve" &&
530 (
531 cd preserve &&
532 packdir=.git/objects/pack &&
533 midx=$packdir/multi-pack-index &&
534
535 test_commit 1 &&
536 pack1=$(git pack-objects --all $packdir/pack) &&
537 touch $packdir/pack-$pack1.keep &&
538 test_commit 2 &&
539 pack2=$(git pack-objects --revs $packdir/pack) &&
540 touch $packdir/pack-$pack2.keep &&
541
542 git multi-pack-index write &&
543 cp $midx $midx.bak &&
544
545 cat >pack-input <<-EOF &&
546 HEAD
547 ^HEAD~1
548 EOF
549 test_commit 3 &&
550 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
551 test_commit 4 &&
552 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
553
554 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
555 ls -la $packdir &&
556 test_path_is_file $packdir/pack-$pack1.pack &&
557 test_path_is_file $packdir/pack-$pack2.pack &&
558 test_path_is_missing $packdir/pack-$pack3.pack &&
559 test_path_is_missing $packdir/pack-$pack4.pack &&
560 test_cmp_bin $midx.bak $midx
561 )
562'
563
525e18c0
DS
564compare_results_with_midx "after repack"
565
e9ab2ed7 566test_expect_success 'multi-pack-index and pack-bitmap' '
e255a5e8
TB
567 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
568 git -c repack.writeBitmaps=true repack -ad &&
e9ab2ed7
DS
569 git multi-pack-index write &&
570 git rev-list --test-bitmap HEAD
571'
572
29e2016b
DS
573test_expect_success 'multi-pack-index and alternates' '
574 git init --bare alt.git &&
575 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
576 echo content1 >file1 &&
577 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
578 git cat-file blob $altblob &&
579 git rev-list --all
580'
581
582compare_results_with_midx "with alternate (local midx)"
583
584test_expect_success 'multi-pack-index in an alternate' '
6a22d521
DS
585 mv .git/objects/pack/* alt.git/objects/pack &&
586 test_commit add_local_objects &&
587 git repack --local &&
588 git multi-pack-index write &&
589 midx_read_expect 1 3 4 $objdir &&
590 git reset --hard HEAD~1 &&
591 rm -f .git/objects/pack/*
29e2016b
DS
592'
593
594compare_results_with_midx "with alternate (remote midx)"
595
662148c4
DS
596# usage: corrupt_data <file> <pos> [<data>]
597corrupt_data () {
598 file=$1
599 pos=$2
600 data="${3:-\0}"
601 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
602}
603
604# Force 64-bit offsets by manipulating the idx file.
605# This makes the IDX file _incorrect_ so be careful to clean up after!
606test_expect_success 'force some 64-bit offsets with pack-objects' '
607 mkdir objects64 &&
608 mkdir objects64/pack &&
609 for i in $(test_seq 1 11)
610 do
d0fd9931 611 generate_objects 11 || return 1
662148c4
DS
612 done &&
613 commit_and_list_objects &&
614 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
615 idx64=objects64/pack/test-64-$pack64.idx &&
616 chmod u+w $idx64 &&
3c5e65ca 617 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
f57a7396
TB
618 # objects64 is not a real repository, but can serve as an alternate
619 # anyway so we can write a MIDX into it
620 git init repo &&
621 test_when_finished "rm -fr repo" &&
622 (
623 cd repo &&
624 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
625 midx64=$(git multi-pack-index --object-dir=../objects64 write)
626 ) &&
662148c4 627 midx_read_expect 1 63 5 objects64 " large-offsets"
2c381335
DS
628'
629
56ee7ff1
DS
630test_expect_success 'verify multi-pack-index with 64-bit offsets' '
631 git multi-pack-index verify --object-dir=objects64
632'
633
cc6af73c
DS
634NUM_OBJECTS=63
635MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
636MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
637MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
638MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
639MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
640
641test_expect_success 'verify incorrect 64-bit offset' '
642 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
643 "incorrect object offset"
644'
645
cff97116
DS
646test_expect_success 'setup expire tests' '
647 mkdir dup &&
648 (
649 cd dup &&
650 git init &&
651 test-tool genrandom "data" 4096 >large_file.txt &&
652 git update-index --add large_file.txt &&
653 for i in $(test_seq 1 20)
654 do
d0fd9931 655 test_commit $i || exit 1
cff97116
DS
656 done &&
657 git branch A HEAD &&
658 git branch B HEAD~8 &&
659 git branch C HEAD~13 &&
660 git branch D HEAD~16 &&
661 git branch E HEAD~18 &&
662 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
663 refs/heads/A
664 ^refs/heads/B
665 EOF
666 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
667 refs/heads/B
668 ^refs/heads/C
669 EOF
670 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
671 refs/heads/C
672 ^refs/heads/D
673 EOF
674 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
675 refs/heads/D
676 ^refs/heads/E
677 EOF
678 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
679 refs/heads/E
680 EOF
2af890bb
DS
681 git multi-pack-index write &&
682 cp -r .git/objects/pack .git/objects/pack-backup
cff97116
DS
683 )
684'
685
686test_expect_success 'expire does not remove any packs' '
687 (
688 cd dup &&
689 ls .git/objects/pack >expect &&
690 git multi-pack-index expire &&
691 ls .git/objects/pack >actual &&
692 test_cmp expect actual
693 )
694'
695
680cba2c
WB
696test_expect_success 'expire progress off for redirected stderr' '
697 (
698 cd dup &&
699 git multi-pack-index expire 2>err &&
700 test_line_count = 0 err
701 )
702'
703
704test_expect_success 'expire force progress on for stderr' '
705 (
706 cd dup &&
0394f8d0 707 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
680cba2c
WB
708 test_file_not_empty err
709 )
710'
711
712test_expect_success 'expire with the --no-progress option' '
713 (
714 cd dup &&
0394f8d0 715 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
680cba2c
WB
716 test_line_count = 0 err
717 )
718'
719
19575c7c
DS
720test_expect_success 'expire removes unreferenced packs' '
721 (
722 cd dup &&
723 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
724 refs/heads/A
725 ^refs/heads/C
726 EOF
727 git multi-pack-index write &&
728 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
729 git multi-pack-index expire &&
730 ls .git/objects/pack >actual &&
731 test_cmp expect actual &&
732 ls .git/objects/pack/ | grep idx >expect-idx &&
733 test-tool read-midx .git/objects | grep idx >actual-midx &&
734 test_cmp expect-idx actual-midx &&
735 git multi-pack-index verify &&
736 git fsck
737 )
738'
739
2af890bb
DS
740test_expect_success 'repack with minimum size does not alter existing packs' '
741 (
742 cd dup &&
743 rm -rf .git/objects/pack &&
744 mv .git/objects/pack-backup .git/objects/pack &&
e892a568
DS
745 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
746 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
747 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
748 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
2af890bb 749 ls .git/objects/pack >expect &&
3612c233 750 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
2af890bb
DS
751 git multi-pack-index repack --batch-size=$MINSIZE &&
752 ls .git/objects/pack >actual &&
753 test_cmp expect actual
754 )
755'
756
3ce4ca0a
DS
757test_expect_success 'repack respects repack.packKeptObjects=false' '
758 test_when_finished rm -f dup/.git/objects/pack/*keep &&
759 (
760 cd dup &&
761 ls .git/objects/pack/*idx >idx-list &&
762 test_line_count = 5 idx-list &&
763 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
764 test_line_count = 5 keep-list &&
765 for keep in $(cat keep-list)
766 do
767 touch $keep || return 1
768 done &&
769 git multi-pack-index repack --batch-size=0 &&
770 ls .git/objects/pack/*idx >idx-list &&
771 test_line_count = 5 idx-list &&
772 test-tool read-midx .git/objects | grep idx >midx-list &&
773 test_line_count = 5 midx-list &&
774 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
775 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
776 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
777 ls .git/objects/pack/*idx >idx-list &&
778 test_line_count = 5 idx-list &&
779 test-tool read-midx .git/objects | grep idx >midx-list &&
780 test_line_count = 5 midx-list
781 )
782'
783
ce1e4a10
DS
784test_expect_success 'repack creates a new pack' '
785 (
786 cd dup &&
787 ls .git/objects/pack/*idx >idx-list &&
788 test_line_count = 5 idx-list &&
3612c233 789 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
ce1e4a10
DS
790 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
791 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
792 ls .git/objects/pack/*idx >idx-list &&
793 test_line_count = 6 idx-list &&
794 test-tool read-midx .git/objects | grep idx >midx-list &&
795 test_line_count = 6 midx-list
796 )
797'
798
d9f77214
TB
799test_expect_success 'repack (all) ignores cruft pack' '
800 git init repo &&
801 test_when_finished "rm -fr repo" &&
802 (
803 cd repo &&
804
805 test_commit base &&
806 test_commit --no-tag unreachable &&
807
808 git reset --hard base &&
809 git reflog expire --all --expire=all &&
810 git repack --cruft -d &&
811
812 git multi-pack-index write &&
813
814 find $objdir/pack | sort >before &&
815 git multi-pack-index repack --batch-size=0 &&
816 find $objdir/pack | sort >after &&
817
818 test_cmp before after
819 )
820'
821
b62ad568
TB
822test_expect_success 'repack (--batch-size) ignores cruft pack' '
823 git init repo &&
824 test_when_finished "rm -fr repo" &&
825 (
826 cd repo &&
827
828 test_commit_bulk 5 &&
829 test_commit --no-tag unreachable &&
830
831 git reset --hard HEAD^ &&
832 git reflog expire --all --expire=all &&
833 git repack --cruft -d &&
834
835 test_commit four &&
836
837 find $objdir/pack -type f -name "*.pack" | sort >before &&
838 git repack -d &&
839 find $objdir/pack -type f -name "*.pack" | sort >after &&
840
841 pack="$(comm -13 before after)" &&
842 test_file_size "$pack" >sz &&
843 # Set --batch-size to twice the size of the pack created
844 # in the previous step, since this is enough to
845 # accommodate it and the cruft pack.
846 #
847 # This means that the MIDX machinery *could* combine the
848 # new and cruft packs together.
849 #
850 # We ensure that it does not below.
851 batch="$((($(cat sz) * 2)))" &&
852
853 git multi-pack-index write &&
854
855 find $objdir/pack | sort >before &&
856 git multi-pack-index repack --batch-size=$batch &&
857 find $objdir/pack | sort >after &&
858
859 test_cmp before after
860 )
861'
862
ce1e4a10
DS
863test_expect_success 'expire removes repacked packs' '
864 (
865 cd dup &&
866 ls -al .git/objects/pack/*pack &&
867 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
868 git multi-pack-index expire &&
869 ls -S .git/objects/pack/*pack >actual &&
870 test_cmp expect actual &&
871 test-tool read-midx .git/objects | grep idx >midx-list &&
872 test_line_count = 4 midx-list
873 )
874'
875
d2743315
DS
876test_expect_success 'expire works when adding new packs' '
877 (
878 cd dup &&
879 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
880 refs/heads/A
881 ^refs/heads/B
882 EOF
883 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
884 refs/heads/B
885 ^refs/heads/C
886 EOF
887 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
888 refs/heads/C
889 ^refs/heads/D
890 EOF
891 git multi-pack-index write &&
892 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
893 refs/heads/D
894 ^refs/heads/E
895 EOF
896 git multi-pack-index write &&
897 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
898 refs/heads/E
899 EOF
900 git multi-pack-index expire &&
901 ls .git/objects/pack/ | grep idx >expect &&
902 test-tool read-midx .git/objects | grep idx >actual &&
903 test_cmp expect actual &&
904 git multi-pack-index verify
905 )
906'
907
10bfa3f7
DS
908test_expect_success 'expire respects .keep files' '
909 (
910 cd dup &&
911 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
912 refs/heads/A
913 EOF
914 git multi-pack-index write &&
915 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
916 touch $PACKA.keep &&
917 git multi-pack-index expire &&
35a8a354
TB
918 test_path_is_file $PACKA.idx &&
919 test_path_is_file $PACKA.keep &&
920 test_path_is_file $PACKA.pack &&
10bfa3f7
DS
921 test-tool read-midx .git/objects | grep idx >midx-list &&
922 test_line_count = 2 midx-list
923 )
924'
925
757d4579
TB
926test_expect_success 'expiring unreferenced cruft pack retains pack' '
927 git init repo &&
928 test_when_finished "rm -fr repo" &&
929 (
930 cd repo &&
931
932 test_commit base &&
933 test_commit --no-tag unreachable &&
934 unreachable=$(git rev-parse HEAD) &&
935
936 git reset --hard base &&
937 git reflog expire --all --expire=all &&
938 git repack --cruft -d &&
939 mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
940
941 echo "base..$unreachable" >in &&
942 pack="$(git pack-objects --revs --delta-base-offset \
943 $objdir/pack/pack <in)" &&
944
945 # Preferring the contents of "$pack" will leave the
946 # cruft pack unreferenced (ie., none of the objects
947 # contained in the cruft pack will have their MIDX copy
948 # selected from the cruft pack).
949 git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
950 git multi-pack-index expire &&
951
952 test_path_is_file "$mtimes"
953 )
954'
955
b526d8cb 956test_expect_success 'repack --batch-size=0 repacks everything' '
1eb22c7d 957 cp -r dup dup2 &&
b526d8cb
DS
958 (
959 cd dup &&
960 rm .git/objects/pack/*.keep &&
961 ls .git/objects/pack/*idx >idx-list &&
962 test_line_count = 2 idx-list &&
963 git multi-pack-index repack --batch-size=0 &&
964 ls .git/objects/pack/*idx >idx-list &&
965 test_line_count = 3 idx-list &&
966 test-tool read-midx .git/objects | grep idx >midx-list &&
967 test_line_count = 3 midx-list &&
968 git multi-pack-index expire &&
969 ls -al .git/objects/pack/*idx >idx-list &&
970 test_line_count = 1 idx-list &&
971 git multi-pack-index repack --batch-size=0 &&
972 ls -al .git/objects/pack/*idx >new-idx-list &&
973 test_cmp idx-list new-idx-list
974 )
975'
10bfa3f7 976
1eb22c7d
DS
977test_expect_success 'repack --batch-size=<large> repacks everything' '
978 (
979 cd dup2 &&
980 rm .git/objects/pack/*.keep &&
981 ls .git/objects/pack/*idx >idx-list &&
982 test_line_count = 2 idx-list &&
983 git multi-pack-index repack --batch-size=2000000 &&
984 ls .git/objects/pack/*idx >idx-list &&
985 test_line_count = 3 idx-list &&
986 test-tool read-midx .git/objects | grep idx >midx-list &&
987 test_line_count = 3 midx-list &&
988 git multi-pack-index expire &&
989 ls -al .git/objects/pack/*idx >idx-list &&
990 test_line_count = 1 idx-list
991 )
992'
993
506ec2fb 994test_expect_success 'load reverse index when missing .idx, .pack' '
c8a45eb6
TB
995 git init repo &&
996 test_when_finished "rm -fr repo" &&
997 (
998 cd repo &&
999
1000 git config core.multiPackIndex true &&
1001
1002 test_commit base &&
1003 git repack -ad &&
1004 git multi-pack-index write &&
1005
1006 git rev-parse HEAD >tip &&
506ec2fb 1007 pack=$(ls .git/objects/pack/pack-*.pack) &&
c8a45eb6
TB
1008 idx=$(ls .git/objects/pack/pack-*.idx) &&
1009
1010 mv $idx $idx.bak &&
506ec2fb
TB
1011 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1012
1013 mv $idx.bak $idx &&
1014
1015 mv $pack $pack.bak &&
c8a45eb6
TB
1016 git cat-file --batch-check="%(objectsize:disk)" <tip
1017 )
1018'
1019
88617d11
TB
1020test_expect_success 'usage shown without sub-command' '
1021 test_expect_code 129 git multi-pack-index 2>err &&
1022 ! test_i18ngrep "unrecognized subcommand" err
1023'
1024
73ff4ad0
TB
1025test_expect_success 'complains when run outside of a repository' '
1026 nongit test_must_fail git multi-pack-index write 2>err &&
1027 grep "not a git repository" err
1028'
1029
6eb095d7
PS
1030test_expect_success 'repack with delta islands' '
1031 git init repo &&
1032 test_when_finished "rm -fr repo" &&
1033 (
1034 cd repo &&
1035
1036 test_commit first &&
1037 git repack &&
1038 test_commit second &&
1039 git repack &&
1040
1041 git multi-pack-index write &&
1042 git -c repack.useDeltaIslands=true multi-pack-index repack
1043 )
1044'
1045
a3407730 1046test_done