blob: 10d2a6bf9231d1931e7d06065c2e1b15370edfd7 [file] [log] [blame]
Derrick Stoleea3407732018-07-12 15:39:21 -04001#!/bin/sh
2
3test_description='multi-pack-indexes'
4. ./test-lib.sh
Jeff Kinge3c96002023-10-09 16:59:19 -04005. "$TEST_DIRECTORY"/lib-chunk.sh
Derrick Stoleea3407732018-07-12 15:39:21 -04006
Derrick Stolee52fe41f2020-09-25 12:33:36 +00007GIT_TEST_MULTI_PACK_INDEX=0
Derrick Stoleec4d25222018-07-12 15:39:33 -04008objdir=.git/objects
9
Derrick Stoleed9607542020-08-17 14:04:48 +000010HASH_LEN=$(test_oid rawsz)
11
Derrick Stolee4d805602018-07-12 15:39:23 -040012midx_read_expect () {
Derrick Stolee396f2572018-07-12 15:39:26 -040013 NUM_PACKS=$1
Derrick Stoleed7cacf22018-07-12 15:39:31 -040014 NUM_OBJECTS=$2
Derrick Stolee662148c2018-07-12 15:39:32 -040015 NUM_CHUNKS=$3
16 OBJECT_DIR=$4
17 EXTRA_CHUNKS="$5"
Derrick Stolee32275652018-07-12 15:39:28 -040018 {
19 cat <<-EOF &&
Derrick Stoleed9607542020-08-17 14:04:48 +000020 header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
Derrick Stolee662148c2018-07-12 15:39:32 -040021 chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
Derrick Stoleed7cacf22018-07-12 15:39:31 -040022 num_objects: $NUM_OBJECTS
Derrick Stolee32275652018-07-12 15:39:28 -040023 packs:
24 EOF
25 if test $NUM_PACKS -ge 1
26 then
Derrick Stolee662148c2018-07-12 15:39:32 -040027 ls $OBJECT_DIR/pack/ | grep idx | sort
Derrick Stolee32275652018-07-12 15:39:28 -040028 fi &&
Derrick Stolee662148c2018-07-12 15:39:32 -040029 printf "object-dir: $OBJECT_DIR\n"
Derrick Stolee32275652018-07-12 15:39:28 -040030 } >expect &&
Derrick Stolee662148c2018-07-12 15:39:32 -040031 test-tool read-midx $OBJECT_DIR >actual &&
Derrick Stolee4d805602018-07-12 15:39:23 -040032 test_cmp expect actual
33}
34
brian m. carlson3c5e65c2019-12-21 19:49:26 +000035test_expect_success 'setup' '
brian m. carlson3c5e65c2019-12-21 19:49:26 +000036 test_oid_cache <<-EOF
37 idxoff sha1:2999
38 idxoff sha256:3739
39
40 packnameoff sha1:652
41 packnameoff sha256:940
42
43 fanoutoff sha1:1
44 fanoutoff sha256:3
45 EOF
46'
47
Damien Robert796d61c2020-03-28 23:18:22 +010048test_expect_success "don't write midx with no packs" '
49 test_must_fail git multi-pack-index --object-dir=. write &&
50 test_path_is_missing pack/multi-pack-index
51'
52
Derrick Stoleed9607542020-08-17 14:04:48 +000053test_expect_success SHA1 'warn if a midx contains no oid' '
Damien Robert796d61c2020-03-28 23:18:22 +010054 cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
55 test_must_fail git multi-pack-index verify &&
56 rm $objdir/pack/multi-pack-index
Derrick Stoleea3407732018-07-12 15:39:21 -040057'
58
Derrick Stolee2c381332018-07-12 15:39:24 -040059generate_objects () {
60 i=$1
61 iii=$(printf '%03i' $i)
62 {
63 test-tool genrandom "bar" 200 &&
64 test-tool genrandom "baz $iii" 50
65 } >wide_delta_$iii &&
66 {
67 test-tool genrandom "foo"$i 100 &&
68 test-tool genrandom "foo"$(( $i + 1 )) 100 &&
69 test-tool genrandom "foo"$(( $i + 2 )) 100
70 } >deep_delta_$iii &&
71 {
72 echo $iii &&
73 test-tool genrandom "$iii" 8192
74 } >file_$iii &&
75 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
76}
77
78commit_and_list_objects () {
79 {
80 echo 101 &&
81 test-tool genrandom 100 8192;
82 } >file_101 &&
83 git update-index --add file_101 &&
84 tree=$(git write-tree) &&
85 commit=$(git commit-tree $tree -p HEAD</dev/null) &&
86 {
87 echo $tree &&
88 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
89 } >obj-list &&
90 git reset --hard $commit
91}
92
93test_expect_success 'create objects' '
94 test_commit initial &&
95 for i in $(test_seq 1 5)
96 do
Eric Sunshined0fd9932021-12-09 00:11:14 -050097 generate_objects $i || return 1
Derrick Stolee2c381332018-07-12 15:39:24 -040098 done &&
99 commit_and_list_objects
100'
101
102test_expect_success 'write midx with one v1 pack' '
Derrick Stoleec4d25222018-07-12 15:39:33 -0400103 pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
104 test_when_finished rm $objdir/pack/test-$pack.pack \
105 $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
106 git multi-pack-index --object-dir=$objdir write &&
107 midx_read_expect 1 18 4 $objdir
Derrick Stolee2c381332018-07-12 15:39:24 -0400108'
109
Derrick Stoleec4d25222018-07-12 15:39:33 -0400110midx_git_two_modes () {
Jeff Kingb4a14392019-04-05 14:04:56 -0400111 git -c core.multiPackIndex=false $1 >expect &&
112 git -c core.multiPackIndex=true $1 >actual &&
Derrick Stoleee9ab2ed2018-08-20 16:52:06 +0000113 if [ "$2" = "sorted" ]
114 then
Jeff Kingb4a14392019-04-05 14:04:56 -0400115 sort <expect >expect.sorted &&
116 mv expect.sorted expect &&
117 sort <actual >actual.sorted &&
118 mv actual.sorted actual
Derrick Stoleee9ab2ed2018-08-20 16:52:06 +0000119 fi &&
Derrick Stoleec4d25222018-07-12 15:39:33 -0400120 test_cmp expect actual
121}
122
123compare_results_with_midx () {
124 MSG=$1
125 test_expect_success "check normal git operations: $MSG" '
126 midx_git_two_modes "rev-list --objects --all" &&
Derrick Stoleee9ab2ed2018-08-20 16:52:06 +0000127 midx_git_two_modes "log --raw" &&
128 midx_git_two_modes "count-objects --verbose" &&
Jeff King5670ad92019-04-05 14:05:03 -0400129 midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
130 midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
Derrick Stoleec4d25222018-07-12 15:39:33 -0400131 '
132}
133
Derrick Stolee2c381332018-07-12 15:39:24 -0400134test_expect_success 'write midx with one v2 pack' '
Derrick Stoleec4d25222018-07-12 15:39:33 -0400135 git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
136 git multi-pack-index --object-dir=$objdir write &&
137 midx_read_expect 1 18 4 $objdir
Derrick Stolee2c381332018-07-12 15:39:24 -0400138'
139
Derrick Stoleec4d25222018-07-12 15:39:33 -0400140compare_results_with_midx "one v2 pack"
141
Taylor Blauc8a45eb2020-11-25 12:17:28 -0500142test_expect_success 'corrupt idx reports errors' '
Jeff Kingfc789152019-04-05 14:06:22 -0400143 idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
144 mv $objdir/pack/$idx backup-$idx &&
145 test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
146
147 # This is the minimum size for a sha-1 based .idx; this lets
148 # us pass perfunctory tests, but anything that actually opens and reads
149 # the idx file will complain.
150 test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
151
152 git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
Taylor Blauc8a45eb2020-11-25 12:17:28 -0500153 grep "index unavailable" err
Jeff Kingfc789152019-04-05 14:06:22 -0400154'
155
Derrick Stolee2c381332018-07-12 15:39:24 -0400156test_expect_success 'add more objects' '
157 for i in $(test_seq 6 10)
158 do
Eric Sunshined0fd9932021-12-09 00:11:14 -0500159 generate_objects $i || return 1
Derrick Stolee2c381332018-07-12 15:39:24 -0400160 done &&
161 commit_and_list_objects
162'
163
164test_expect_success 'write midx with two packs' '
Derrick Stoleec4d25222018-07-12 15:39:33 -0400165 git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
166 git multi-pack-index --object-dir=$objdir write &&
167 midx_read_expect 2 34 4 $objdir
Derrick Stolee2c381332018-07-12 15:39:24 -0400168'
169
Derrick Stoleec4d25222018-07-12 15:39:33 -0400170compare_results_with_midx "two packs"
171
Taylor Blau6fb22ca2021-09-28 21:55:04 -0400172test_expect_success 'write midx with --stdin-packs' '
173 rm -fr $objdir/pack/multi-pack-index &&
174
175 idx="$(find $objdir/pack -name "test-2-*.idx")" &&
176 basename "$idx" >in &&
177
178 git multi-pack-index write --stdin-packs <in &&
179
180 test-tool read-midx $objdir | grep "\.idx$" >packs &&
181
182 test_cmp packs in
183'
184
185compare_results_with_midx "mixed mode (one pack + extra)"
186
Patrick Steinhardtceb96a12023-04-14 08:01:31 +0200187test_expect_success 'write with no objects and preferred pack' '
188 test_when_finished "rm -rf empty" &&
189 git init empty &&
190 test_must_fail git -C empty multi-pack-index write \
191 --stdin-packs --preferred-pack=does-not-exist </dev/null 2>err &&
192 cat >expect <<-EOF &&
193 warning: unknown preferred pack: ${SQ}does-not-exist${SQ}
194 error: no pack files to index.
195 EOF
196 test_cmp expect err
197'
198
William Baker680cba22019-10-21 18:40:03 +0000199test_expect_success 'write progress off for redirected stderr' '
200 git multi-pack-index --object-dir=$objdir write 2>err &&
201 test_line_count = 0 err
202'
203
204test_expect_success 'write force progress on for stderr' '
Taylor Blau0394f8d2021-09-20 17:39:19 -0400205 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --progress 2>err &&
William Baker680cba22019-10-21 18:40:03 +0000206 test_file_not_empty err
207'
208
209test_expect_success 'write with the --no-progress option' '
Taylor Blau0394f8d2021-09-20 17:39:19 -0400210 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --no-progress 2>err &&
William Baker680cba22019-10-21 18:40:03 +0000211 test_line_count = 0 err
212'
213
Derrick Stolee2c381332018-07-12 15:39:24 -0400214test_expect_success 'add more packs' '
215 for j in $(test_seq 11 20)
216 do
217 generate_objects $j &&
218 commit_and_list_objects &&
Eric Sunshined0fd9932021-12-09 00:11:14 -0500219 git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list || return 1
Derrick Stolee2c381332018-07-12 15:39:24 -0400220 done
221'
222
Derrick Stoleec4d25222018-07-12 15:39:33 -0400223compare_results_with_midx "mixed mode (two packs + extra)"
224
Derrick Stolee2c381332018-07-12 15:39:24 -0400225test_expect_success 'write midx with twelve packs' '
Derrick Stoleec4d25222018-07-12 15:39:33 -0400226 git multi-pack-index --object-dir=$objdir write &&
227 midx_read_expect 12 74 4 $objdir
Derrick Stolee662148c2018-07-12 15:39:32 -0400228'
229
Derrick Stoleec4d25222018-07-12 15:39:33 -0400230compare_results_with_midx "twelve packs"
231
Taylor Blau426c00e2021-08-31 16:51:55 -0400232test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
233 git init repo &&
234 git clone -s repo alternate &&
235
236 test_when_finished "rm -rf repo alternate" &&
237
238 (
239 cd repo &&
240 test_commit base &&
241 git repack -d
242 ) &&
243
244 ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
245 theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
246 touch "$ours" "$theirs" &&
247
248 (
249 cd alternate &&
250 git multi-pack-index --object-dir ../repo/.git/objects write
251 ) &&
252
253 # writing a midx in "repo" should not remove the .rev file in the
254 # alternate
255 test_path_is_file repo/.git/objects/pack/multi-pack-index &&
256 test_path_is_file $ours &&
257 test_path_is_missing $theirs
258'
259
Derrick Stoleed9607542020-08-17 14:04:48 +0000260test_expect_success 'warn on improper hash version' '
261 git init --object-format=sha1 sha1 &&
262 (
263 cd sha1 &&
264 git config core.multiPackIndex true &&
265 test_commit 1 &&
266 git repack -a &&
267 git multi-pack-index write &&
268 mv .git/objects/pack/multi-pack-index ../mpi-sha1
269 ) &&
270 git init --object-format=sha256 sha256 &&
271 (
272 cd sha256 &&
273 git config core.multiPackIndex true &&
274 test_commit 1 &&
275 git repack -a &&
276 git multi-pack-index write &&
277 mv .git/objects/pack/multi-pack-index ../mpi-sha256
278 ) &&
279 (
280 cd sha1 &&
281 mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
282 git log -1 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900283 test_grep "multi-pack-index hash version 2 does not match version 1" err
Derrick Stoleed9607542020-08-17 14:04:48 +0000284 ) &&
285 (
286 cd sha256 &&
287 mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
288 git log -1 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900289 test_grep "multi-pack-index hash version 1 does not match version 2" err
Derrick Stoleed9607542020-08-17 14:04:48 +0000290 )
291'
292
Taylor Blau9218c6a2021-03-30 11:04:11 -0400293test_expect_success 'midx picks objects from preferred pack' '
294 test_when_finished rm -rf preferred.git &&
295 git init --bare preferred.git &&
296 (
297 cd preferred.git &&
298
299 a=$(echo "a" | git hash-object -w --stdin) &&
300 b=$(echo "b" | git hash-object -w --stdin) &&
301 c=$(echo "c" | git hash-object -w --stdin) &&
302
303 # Set up two packs, duplicating the object "B" at different
304 # offsets.
305 #
306 # Note that the "BC" pack (the one we choose as preferred) sorts
307 # lexically after the "AB" pack, meaning that omitting the
308 # --preferred-pack argument would cause this test to fail (since
309 # the MIDX code would select the copy of "b" in the "AB" pack).
310 git pack-objects objects/pack/test-AB <<-EOF &&
311 $a
312 $b
313 EOF
314 bc=$(git pack-objects objects/pack/test-BC <<-EOF
315 $b
316 $c
317 EOF
318 ) &&
319
320 git multi-pack-index --object-dir=objects \
321 write --preferred-pack=test-BC-$bc.idx 2>err &&
322 test_must_be_empty err &&
323
324 test-tool read-midx --show-objects objects >out &&
325
326 ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
327 cut -d" " -f1) &&
328 printf "%s %s\tobjects/pack/test-BC-%s.pack\n" \
329 "$b" "$ofs" "$bc" >expect &&
330 grep ^$b out >actual &&
331
332 test_cmp expect actual
333 )
334'
Derrick Stoleed9607542020-08-17 14:04:48 +0000335
Taylor Blau5d3cd092021-08-31 16:52:02 -0400336test_expect_success 'preferred packs must be non-empty' '
337 test_when_finished rm -rf preferred.git &&
338 git init preferred.git &&
339 (
340 cd preferred.git &&
341
342 test_commit base &&
343 git repack -ad &&
344
345 empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
346
347 test_must_fail git multi-pack-index write \
348 --preferred-pack=pack-$empty.pack 2>err &&
349 grep "with no objects" err
350 )
351'
352
Taylor Blau23532be2024-05-29 18:55:19 -0400353test_expect_success 'preferred pack from existing MIDX without bitmaps' '
354 git init preferred-without-bitmaps &&
355 (
356 cd preferred-without-bitmaps &&
357
358 test_commit one &&
359 pack="$(git pack-objects --all $objdir/pack/pack </dev/null)" &&
360
361 git multi-pack-index write &&
362
363 # make another pack so that the subsequent MIDX write
364 # has something to do
365 test_commit two &&
366 git repack -d &&
367
368 # write a new MIDX without bitmaps reusing the singular
369 # pack from the existing MIDX as the preferred pack in
370 # the new MIDX
371 git multi-pack-index write --preferred-pack=pack-$pack.pack
372 )
373
374'
375
Derrick Stolee56ee7ff2018-09-13 11:02:13 -0700376test_expect_success 'verify multi-pack-index success' '
377 git multi-pack-index verify --object-dir=$objdir
378'
379
William Baker680cba22019-10-21 18:40:03 +0000380test_expect_success 'verify progress off for redirected stderr' '
381 git multi-pack-index verify --object-dir=$objdir 2>err &&
382 test_line_count = 0 err
383'
384
385test_expect_success 'verify force progress on for stderr' '
386 git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
387 test_file_not_empty err
388'
389
390test_expect_success 'verify with the --no-progress option' '
391 git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
392 test_line_count = 0 err
393'
394
Derrick Stolee53ad0402018-09-13 11:02:15 -0700395# usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
396corrupt_midx_and_verify() {
397 POS=$1 &&
398 DATA="${2:-\0}" &&
399 OBJDIR=$3 &&
400 GREPSTR="$4" &&
Derrick Stolee66ec0392018-09-13 11:02:27 -0700401 COMMAND="$5" &&
402 if test -z "$COMMAND"
403 then
404 COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
405 fi &&
Derrick Stolee53ad0402018-09-13 11:02:15 -0700406 FILE=$OBJDIR/pack/multi-pack-index &&
407 chmod a+w $FILE &&
408 test_when_finished mv midx-backup $FILE &&
409 cp $FILE midx-backup &&
410 printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
Derrick Stolee66ec0392018-09-13 11:02:27 -0700411 test_must_fail $COMMAND 2>test_err &&
Derrick Stolee53ad0402018-09-13 11:02:15 -0700412 grep -v "^+" test_err >err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900413 test_grep "$GREPSTR" err
Derrick Stolee53ad0402018-09-13 11:02:15 -0700414}
415
416test_expect_success 'verify bad signature' '
417 corrupt_midx_and_verify 0 "\00" $objdir \
418 "multi-pack-index signature"
419'
420
Derrick Stoleecc6af732018-09-13 11:02:25 -0700421NUM_OBJECTS=74
Derrick Stolee53ad0402018-09-13 11:02:15 -0700422MIDX_BYTE_VERSION=4
423MIDX_BYTE_OID_VERSION=5
424MIDX_BYTE_CHUNK_COUNT=6
Derrick Stoleed3f8e212018-09-13 11:02:16 -0700425MIDX_HEADER_SIZE=12
426MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
427MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
Derrick Stolee8e72a3c2018-09-13 11:02:18 -0700428MIDX_NUM_CHUNKS=5
429MIDX_CHUNK_LOOKUP_WIDTH=12
430MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
431 $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
432MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
brian m. carlson3c5e65c2019-12-21 19:49:26 +0000433MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
Derrick Stolee2f23d3f2018-09-13 11:02:20 -0700434MIDX_OID_FANOUT_WIDTH=4
brian m. carlson3c5e65c2019-12-21 19:49:26 +0000435MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
Derrick Stolee55c56482018-09-13 11:02:22 -0700436MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
437MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
Derrick Stoleecc6af732018-09-13 11:02:25 -0700438MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
439MIDX_OFFSET_WIDTH=8
440MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
441MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
Derrick Stolee53ad0402018-09-13 11:02:15 -0700442
443test_expect_success 'verify bad version' '
444 corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
445 "multi-pack-index version"
446'
447
448test_expect_success 'verify bad OID version' '
Derrick Stoleed9607542020-08-17 14:04:48 +0000449 corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
Derrick Stolee53ad0402018-09-13 11:02:15 -0700450 "hash version"
451'
452
453test_expect_success 'verify truncated chunk count' '
454 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000455 "final chunk has non-zero id"
Derrick Stolee53ad0402018-09-13 11:02:15 -0700456'
457
458test_expect_success 'verify extended chunk count' '
459 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000460 "terminating chunk id appears earlier than expected"
Derrick Stolee53ad0402018-09-13 11:02:15 -0700461'
462
Derrick Stoleed3f8e212018-09-13 11:02:16 -0700463test_expect_success 'verify missing required chunk' '
464 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
Jeff Kinge3c96002023-10-09 16:59:19 -0400465 "required pack-name chunk missing"
Derrick Stoleed3f8e212018-09-13 11:02:16 -0700466'
467
468test_expect_success 'verify invalid chunk offset' '
469 corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000470 "improper chunk offset(s)"
Derrick Stoleed3f8e212018-09-13 11:02:16 -0700471'
472
Derrick Stolee8e72a3c2018-09-13 11:02:18 -0700473test_expect_success 'verify packnames out of order' '
474 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
475 "pack names out of order"
476'
477
Derrick Stoleed4bf1d82018-09-13 11:02:19 -0700478test_expect_success 'verify packnames out of order' '
479 corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
480 "failed to load pack"
481'
482
Derrick Stolee2f23d3f2018-09-13 11:02:20 -0700483test_expect_success 'verify oid fanout out of order' '
484 corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
485 "oid fanout out of order"
486'
487
Derrick Stolee55c56482018-09-13 11:02:22 -0700488test_expect_success 'verify oid lookup out of order' '
489 corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
490 "oid lookup out of order"
491'
492
Derrick Stoleecc6af732018-09-13 11:02:25 -0700493test_expect_success 'verify incorrect pack-int-id' '
494 corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
495 "bad pack-int-id"
496'
497
498test_expect_success 'verify incorrect offset' '
brian m. carlson235d3cd2019-12-21 19:49:25 +0000499 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
Derrick Stoleecc6af732018-09-13 11:02:25 -0700500 "incorrect object offset"
501'
502
Derrick Stolee66ec0392018-09-13 11:02:27 -0700503test_expect_success 'git-fsck incorrect offset' '
brian m. carlson235d3cd2019-12-21 19:49:25 +0000504 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
Derrick Stolee66ec0392018-09-13 11:02:27 -0700505 "incorrect object offset" \
Glen Choodc557082021-10-15 13:16:30 -0700506 "git -c core.multiPackIndex=true fsck" &&
507 test_unconfig core.multiPackIndex &&
508 test_must_fail git fsck &&
509 git -c core.multiPackIndex=false fsck
Derrick Stolee66ec0392018-09-13 11:02:27 -0700510'
511
Taylor Blau39bdd302023-07-07 20:31:34 -0400512test_expect_success 'git fsck shows MIDX output with --progress' '
513 git fsck --progress 2>err &&
514 grep "Verifying OID order in multi-pack-index" err &&
515 grep "Verifying object offsets" err
516'
517
518test_expect_success 'git fsck suppresses MIDX output with --no-progress' '
519 git fsck --no-progress 2>err &&
520 ! grep "Verifying OID order in multi-pack-index" err &&
521 ! grep "Verifying object offsets" err
522'
523
Taylor Blauec1e28e2021-06-23 14:39:12 -0400524test_expect_success 'corrupt MIDX is not reused' '
525 corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
526 "incorrect object offset" &&
527 git multi-pack-index write 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900528 test_grep checksum.mismatch err &&
Taylor Blauec1e28e2021-06-23 14:39:12 -0400529 git multi-pack-index verify
530'
531
Taylor Blauf89ecf72021-06-23 14:39:15 -0400532test_expect_success 'verify incorrect checksum' '
Jeff King152923b2021-11-16 16:38:50 -0500533 pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
534 corrupt_midx_and_verify $pos \
535 "\377\377\377\377\377\377\377\377\377\377" \
536 $objdir "incorrect checksum"
Taylor Blauf89ecf72021-06-23 14:39:15 -0400537'
538
William Baker680cba22019-10-21 18:40:03 +0000539test_expect_success 'repack progress off for redirected stderr' '
Derrick Stoleeefdd2f02020-09-25 12:33:35 +0000540 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
William Baker680cba22019-10-21 18:40:03 +0000541 test_line_count = 0 err
542'
543
544test_expect_success 'repack force progress on for stderr' '
Taylor Blau0394f8d2021-09-20 17:39:19 -0400545 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
William Baker680cba22019-10-21 18:40:03 +0000546 test_file_not_empty err
547'
548
549test_expect_success 'repack with the --no-progress option' '
Taylor Blau0394f8d2021-09-20 17:39:19 -0400550 GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
William Baker680cba22019-10-21 18:40:03 +0000551 test_line_count = 0 err
552'
553
Taylor Blaue08f7bb2020-08-25 12:04:36 -0400554test_expect_success 'repack removes multi-pack-index when deleting packs' '
Derrick Stolee525e18c2018-07-12 15:39:40 -0400555 test_path_is_file $objdir/pack/multi-pack-index &&
Taylor Blaue08f7bb2020-08-25 12:04:36 -0400556 # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
557 # multi-pack-index after repacking, but set "core.multiPackIndex" to
558 # true so that "git repack" can read the existing MIDX.
559 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
Derrick Stolee525e18c2018-07-12 15:39:40 -0400560 test_path_is_missing $objdir/pack/multi-pack-index
561'
562
Taylor Blaue08f7bb2020-08-25 12:04:36 -0400563test_expect_success 'repack preserves multi-pack-index when creating packs' '
564 git init preserve &&
565 test_when_finished "rm -fr preserve" &&
566 (
567 cd preserve &&
568 packdir=.git/objects/pack &&
569 midx=$packdir/multi-pack-index &&
570
571 test_commit 1 &&
572 pack1=$(git pack-objects --all $packdir/pack) &&
573 touch $packdir/pack-$pack1.keep &&
574 test_commit 2 &&
575 pack2=$(git pack-objects --revs $packdir/pack) &&
576 touch $packdir/pack-$pack2.keep &&
577
578 git multi-pack-index write &&
579 cp $midx $midx.bak &&
580
581 cat >pack-input <<-EOF &&
582 HEAD
583 ^HEAD~1
584 EOF
585 test_commit 3 &&
586 pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
587 test_commit 4 &&
588 pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
589
590 GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
591 ls -la $packdir &&
592 test_path_is_file $packdir/pack-$pack1.pack &&
593 test_path_is_file $packdir/pack-$pack2.pack &&
594 test_path_is_missing $packdir/pack-$pack3.pack &&
595 test_path_is_missing $packdir/pack-$pack4.pack &&
596 test_cmp_bin $midx.bak $midx
597 )
598'
599
Derrick Stolee525e18c2018-07-12 15:39:40 -0400600compare_results_with_midx "after repack"
601
Derrick Stoleee9ab2ed2018-08-20 16:52:06 +0000602test_expect_success 'multi-pack-index and pack-bitmap' '
Taylor Blaue255a5e2021-08-31 16:52:38 -0400603 GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
604 git -c repack.writeBitmaps=true repack -ad &&
Derrick Stoleee9ab2ed2018-08-20 16:52:06 +0000605 git multi-pack-index write &&
606 git rev-list --test-bitmap HEAD
607'
608
Derrick Stolee29e20162018-08-20 16:52:00 +0000609test_expect_success 'multi-pack-index and alternates' '
610 git init --bare alt.git &&
611 echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
612 echo content1 >file1 &&
613 altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
614 git cat-file blob $altblob &&
615 git rev-list --all
616'
617
618compare_results_with_midx "with alternate (local midx)"
619
620test_expect_success 'multi-pack-index in an alternate' '
Derrick Stolee6a22d522018-08-20 16:52:08 +0000621 mv .git/objects/pack/* alt.git/objects/pack &&
622 test_commit add_local_objects &&
623 git repack --local &&
624 git multi-pack-index write &&
625 midx_read_expect 1 3 4 $objdir &&
626 git reset --hard HEAD~1 &&
627 rm -f .git/objects/pack/*
Derrick Stolee29e20162018-08-20 16:52:00 +0000628'
629
630compare_results_with_midx "with alternate (remote midx)"
631
Derrick Stolee662148c2018-07-12 15:39:32 -0400632# usage: corrupt_data <file> <pos> [<data>]
633corrupt_data () {
634 file=$1
635 pos=$2
636 data="${3:-\0}"
637 printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
638}
639
640# Force 64-bit offsets by manipulating the idx file.
641# This makes the IDX file _incorrect_ so be careful to clean up after!
642test_expect_success 'force some 64-bit offsets with pack-objects' '
643 mkdir objects64 &&
644 mkdir objects64/pack &&
645 for i in $(test_seq 1 11)
646 do
Eric Sunshined0fd9932021-12-09 00:11:14 -0500647 generate_objects 11 || return 1
Derrick Stolee662148c2018-07-12 15:39:32 -0400648 done &&
649 commit_and_list_objects &&
650 pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
651 idx64=objects64/pack/test-64-$pack64.idx &&
652 chmod u+w $idx64 &&
brian m. carlson3c5e65c2019-12-21 19:49:26 +0000653 corrupt_data $idx64 $(test_oid idxoff) "\02" &&
Taylor Blauf57a7392021-09-01 16:34:01 -0400654 # objects64 is not a real repository, but can serve as an alternate
655 # anyway so we can write a MIDX into it
656 git init repo &&
657 test_when_finished "rm -fr repo" &&
658 (
659 cd repo &&
660 ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
661 midx64=$(git multi-pack-index --object-dir=../objects64 write)
662 ) &&
Derrick Stolee662148c2018-07-12 15:39:32 -0400663 midx_read_expect 1 63 5 objects64 " large-offsets"
Derrick Stolee2c381332018-07-12 15:39:24 -0400664'
665
Derrick Stolee56ee7ff2018-09-13 11:02:13 -0700666test_expect_success 'verify multi-pack-index with 64-bit offsets' '
667 git multi-pack-index verify --object-dir=objects64
668'
669
Derrick Stoleecc6af732018-09-13 11:02:25 -0700670NUM_OBJECTS=63
671MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
672MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
673MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
674MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
675MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
676
677test_expect_success 'verify incorrect 64-bit offset' '
678 corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
679 "incorrect object offset"
680'
681
Derrick Stoleecff97112019-06-10 16:35:23 -0700682test_expect_success 'setup expire tests' '
683 mkdir dup &&
684 (
685 cd dup &&
686 git init &&
687 test-tool genrandom "data" 4096 >large_file.txt &&
688 git update-index --add large_file.txt &&
689 for i in $(test_seq 1 20)
690 do
Eric Sunshined0fd9932021-12-09 00:11:14 -0500691 test_commit $i || exit 1
Derrick Stoleecff97112019-06-10 16:35:23 -0700692 done &&
693 git branch A HEAD &&
694 git branch B HEAD~8 &&
695 git branch C HEAD~13 &&
696 git branch D HEAD~16 &&
697 git branch E HEAD~18 &&
698 git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
699 refs/heads/A
700 ^refs/heads/B
701 EOF
702 git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
703 refs/heads/B
704 ^refs/heads/C
705 EOF
706 git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
707 refs/heads/C
708 ^refs/heads/D
709 EOF
710 git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
711 refs/heads/D
712 ^refs/heads/E
713 EOF
714 git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
715 refs/heads/E
716 EOF
Derrick Stolee2af890b2019-06-10 16:35:26 -0700717 git multi-pack-index write &&
718 cp -r .git/objects/pack .git/objects/pack-backup
Derrick Stoleecff97112019-06-10 16:35:23 -0700719 )
720'
721
722test_expect_success 'expire does not remove any packs' '
723 (
724 cd dup &&
725 ls .git/objects/pack >expect &&
726 git multi-pack-index expire &&
727 ls .git/objects/pack >actual &&
728 test_cmp expect actual
729 )
730'
731
William Baker680cba22019-10-21 18:40:03 +0000732test_expect_success 'expire progress off for redirected stderr' '
733 (
734 cd dup &&
735 git multi-pack-index expire 2>err &&
736 test_line_count = 0 err
737 )
738'
739
740test_expect_success 'expire force progress on for stderr' '
741 (
742 cd dup &&
Taylor Blau0394f8d2021-09-20 17:39:19 -0400743 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
William Baker680cba22019-10-21 18:40:03 +0000744 test_file_not_empty err
745 )
746'
747
748test_expect_success 'expire with the --no-progress option' '
749 (
750 cd dup &&
Taylor Blau0394f8d2021-09-20 17:39:19 -0400751 GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
William Baker680cba22019-10-21 18:40:03 +0000752 test_line_count = 0 err
753 )
754'
755
Derrick Stolee19575c72019-06-10 16:35:25 -0700756test_expect_success 'expire removes unreferenced packs' '
757 (
758 cd dup &&
759 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
760 refs/heads/A
761 ^refs/heads/C
762 EOF
763 git multi-pack-index write &&
764 ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
765 git multi-pack-index expire &&
766 ls .git/objects/pack >actual &&
767 test_cmp expect actual &&
768 ls .git/objects/pack/ | grep idx >expect-idx &&
769 test-tool read-midx .git/objects | grep idx >actual-midx &&
770 test_cmp expect-idx actual-midx &&
771 git multi-pack-index verify &&
772 git fsck
773 )
774'
775
Derrick Stolee2af890b2019-06-10 16:35:26 -0700776test_expect_success 'repack with minimum size does not alter existing packs' '
777 (
778 cd dup &&
779 rm -rf .git/objects/pack &&
780 mv .git/objects/pack-backup .git/objects/pack &&
Derrick Stoleee892a562020-04-01 21:00:43 +0000781 test-tool chmtime =-5 .git/objects/pack/pack-D* &&
782 test-tool chmtime =-4 .git/objects/pack/pack-C* &&
783 test-tool chmtime =-3 .git/objects/pack/pack-B* &&
784 test-tool chmtime =-2 .git/objects/pack/pack-A* &&
Derrick Stolee2af890b2019-06-10 16:35:26 -0700785 ls .git/objects/pack >expect &&
Derrick Stolee3612c232019-07-01 06:16:19 -0700786 MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
Derrick Stolee2af890b2019-06-10 16:35:26 -0700787 git multi-pack-index repack --batch-size=$MINSIZE &&
788 ls .git/objects/pack >actual &&
789 test_cmp expect actual
790 )
791'
792
Derrick Stolee3ce4ca02020-05-10 16:07:34 +0000793test_expect_success 'repack respects repack.packKeptObjects=false' '
794 test_when_finished rm -f dup/.git/objects/pack/*keep &&
795 (
796 cd dup &&
797 ls .git/objects/pack/*idx >idx-list &&
798 test_line_count = 5 idx-list &&
799 ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
800 test_line_count = 5 keep-list &&
801 for keep in $(cat keep-list)
802 do
803 touch $keep || return 1
804 done &&
805 git multi-pack-index repack --batch-size=0 &&
806 ls .git/objects/pack/*idx >idx-list &&
807 test_line_count = 5 idx-list &&
808 test-tool read-midx .git/objects | grep idx >midx-list &&
809 test_line_count = 5 midx-list &&
810 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
811 BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
812 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
813 ls .git/objects/pack/*idx >idx-list &&
814 test_line_count = 5 idx-list &&
815 test-tool read-midx .git/objects | grep idx >midx-list &&
816 test_line_count = 5 midx-list
817 )
818'
819
Derrick Stoleece1e4a12019-06-10 16:35:27 -0700820test_expect_success 'repack creates a new pack' '
821 (
822 cd dup &&
823 ls .git/objects/pack/*idx >idx-list &&
824 test_line_count = 5 idx-list &&
Derrick Stolee3612c232019-07-01 06:16:19 -0700825 THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
Derrick Stoleece1e4a12019-06-10 16:35:27 -0700826 BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
827 git multi-pack-index repack --batch-size=$BATCH_SIZE &&
828 ls .git/objects/pack/*idx >idx-list &&
829 test_line_count = 6 idx-list &&
830 test-tool read-midx .git/objects | grep idx >midx-list &&
831 test_line_count = 6 midx-list
832 )
833'
834
Taylor Blaud9f77212022-09-19 21:55:48 -0400835test_expect_success 'repack (all) ignores cruft pack' '
836 git init repo &&
837 test_when_finished "rm -fr repo" &&
838 (
839 cd repo &&
840
841 test_commit base &&
842 test_commit --no-tag unreachable &&
843
844 git reset --hard base &&
845 git reflog expire --all --expire=all &&
846 git repack --cruft -d &&
847
848 git multi-pack-index write &&
849
850 find $objdir/pack | sort >before &&
851 git multi-pack-index repack --batch-size=0 &&
852 find $objdir/pack | sort >after &&
853
854 test_cmp before after
855 )
856'
857
Taylor Blaub62ad562022-09-19 21:55:56 -0400858test_expect_success 'repack (--batch-size) ignores cruft pack' '
859 git init repo &&
860 test_when_finished "rm -fr repo" &&
861 (
862 cd repo &&
863
864 test_commit_bulk 5 &&
865 test_commit --no-tag unreachable &&
866
867 git reset --hard HEAD^ &&
868 git reflog expire --all --expire=all &&
869 git repack --cruft -d &&
870
871 test_commit four &&
872
873 find $objdir/pack -type f -name "*.pack" | sort >before &&
874 git repack -d &&
875 find $objdir/pack -type f -name "*.pack" | sort >after &&
876
877 pack="$(comm -13 before after)" &&
878 test_file_size "$pack" >sz &&
879 # Set --batch-size to twice the size of the pack created
880 # in the previous step, since this is enough to
881 # accommodate it and the cruft pack.
882 #
883 # This means that the MIDX machinery *could* combine the
884 # new and cruft packs together.
885 #
886 # We ensure that it does not below.
887 batch="$((($(cat sz) * 2)))" &&
888
889 git multi-pack-index write &&
890
891 find $objdir/pack | sort >before &&
892 git multi-pack-index repack --batch-size=$batch &&
893 find $objdir/pack | sort >after &&
894
895 test_cmp before after
896 )
897'
898
Derrick Stoleece1e4a12019-06-10 16:35:27 -0700899test_expect_success 'expire removes repacked packs' '
900 (
901 cd dup &&
902 ls -al .git/objects/pack/*pack &&
903 ls -S .git/objects/pack/*pack | head -n 4 >expect &&
904 git multi-pack-index expire &&
905 ls -S .git/objects/pack/*pack >actual &&
906 test_cmp expect actual &&
907 test-tool read-midx .git/objects | grep idx >midx-list &&
908 test_line_count = 4 midx-list
909 )
910'
911
Derrick Stoleed2743312019-06-10 16:35:27 -0700912test_expect_success 'expire works when adding new packs' '
913 (
914 cd dup &&
915 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
916 refs/heads/A
917 ^refs/heads/B
918 EOF
919 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
920 refs/heads/B
921 ^refs/heads/C
922 EOF
923 git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
924 refs/heads/C
925 ^refs/heads/D
926 EOF
927 git multi-pack-index write &&
928 git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
929 refs/heads/D
930 ^refs/heads/E
931 EOF
932 git multi-pack-index write &&
933 git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
934 refs/heads/E
935 EOF
936 git multi-pack-index expire &&
937 ls .git/objects/pack/ | grep idx >expect &&
938 test-tool read-midx .git/objects | grep idx >actual &&
939 test_cmp expect actual &&
940 git multi-pack-index verify
941 )
942'
943
Derrick Stolee10bfa3f2019-06-10 16:35:28 -0700944test_expect_success 'expire respects .keep files' '
945 (
946 cd dup &&
947 git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
948 refs/heads/A
949 EOF
950 git multi-pack-index write &&
951 PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
952 touch $PACKA.keep &&
953 git multi-pack-index expire &&
Taylor Blau35a8a352021-01-25 18:37:38 -0500954 test_path_is_file $PACKA.idx &&
955 test_path_is_file $PACKA.keep &&
956 test_path_is_file $PACKA.pack &&
Derrick Stolee10bfa3f2019-06-10 16:35:28 -0700957 test-tool read-midx .git/objects | grep idx >midx-list &&
958 test_line_count = 2 midx-list
959 )
960'
961
Taylor Blau757d4572022-09-19 21:55:45 -0400962test_expect_success 'expiring unreferenced cruft pack retains pack' '
963 git init repo &&
964 test_when_finished "rm -fr repo" &&
965 (
966 cd repo &&
967
968 test_commit base &&
969 test_commit --no-tag unreachable &&
970 unreachable=$(git rev-parse HEAD) &&
971
972 git reset --hard base &&
973 git reflog expire --all --expire=all &&
974 git repack --cruft -d &&
975 mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
976
977 echo "base..$unreachable" >in &&
978 pack="$(git pack-objects --revs --delta-base-offset \
979 $objdir/pack/pack <in)" &&
980
981 # Preferring the contents of "$pack" will leave the
982 # cruft pack unreferenced (ie., none of the objects
983 # contained in the cruft pack will have their MIDX copy
984 # selected from the cruft pack).
985 git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
986 git multi-pack-index expire &&
987
988 test_path_is_file "$mtimes"
989 )
990'
991
Derrick Stoleeb526d8c2019-06-10 16:35:29 -0700992test_expect_success 'repack --batch-size=0 repacks everything' '
Derrick Stolee1eb22c72020-08-11 15:30:18 +0000993 cp -r dup dup2 &&
Derrick Stoleeb526d8c2019-06-10 16:35:29 -0700994 (
995 cd dup &&
996 rm .git/objects/pack/*.keep &&
997 ls .git/objects/pack/*idx >idx-list &&
998 test_line_count = 2 idx-list &&
999 git multi-pack-index repack --batch-size=0 &&
1000 ls .git/objects/pack/*idx >idx-list &&
1001 test_line_count = 3 idx-list &&
1002 test-tool read-midx .git/objects | grep idx >midx-list &&
1003 test_line_count = 3 midx-list &&
1004 git multi-pack-index expire &&
1005 ls -al .git/objects/pack/*idx >idx-list &&
1006 test_line_count = 1 idx-list &&
1007 git multi-pack-index repack --batch-size=0 &&
1008 ls -al .git/objects/pack/*idx >new-idx-list &&
1009 test_cmp idx-list new-idx-list
1010 )
1011'
Derrick Stolee10bfa3f2019-06-10 16:35:28 -07001012
Derrick Stolee1eb22c72020-08-11 15:30:18 +00001013test_expect_success 'repack --batch-size=<large> repacks everything' '
1014 (
1015 cd dup2 &&
1016 rm .git/objects/pack/*.keep &&
1017 ls .git/objects/pack/*idx >idx-list &&
1018 test_line_count = 2 idx-list &&
1019 git multi-pack-index repack --batch-size=2000000 &&
1020 ls .git/objects/pack/*idx >idx-list &&
1021 test_line_count = 3 idx-list &&
1022 test-tool read-midx .git/objects | grep idx >midx-list &&
1023 test_line_count = 3 midx-list &&
1024 git multi-pack-index expire &&
1025 ls -al .git/objects/pack/*idx >idx-list &&
1026 test_line_count = 1 idx-list
1027 )
1028'
1029
Taylor Blau506ec2f2020-11-25 12:17:33 -05001030test_expect_success 'load reverse index when missing .idx, .pack' '
Taylor Blauc8a45eb2020-11-25 12:17:28 -05001031 git init repo &&
1032 test_when_finished "rm -fr repo" &&
1033 (
1034 cd repo &&
1035
1036 git config core.multiPackIndex true &&
1037
1038 test_commit base &&
1039 git repack -ad &&
1040 git multi-pack-index write &&
1041
1042 git rev-parse HEAD >tip &&
Taylor Blau506ec2f2020-11-25 12:17:33 -05001043 pack=$(ls .git/objects/pack/pack-*.pack) &&
Taylor Blauc8a45eb2020-11-25 12:17:28 -05001044 idx=$(ls .git/objects/pack/pack-*.idx) &&
1045
1046 mv $idx $idx.bak &&
Taylor Blau506ec2f2020-11-25 12:17:33 -05001047 git cat-file --batch-check="%(objectsize:disk)" <tip &&
1048
1049 mv $idx.bak $idx &&
1050
1051 mv $pack $pack.bak &&
Taylor Blauc8a45eb2020-11-25 12:17:28 -05001052 git cat-file --batch-check="%(objectsize:disk)" <tip
1053 )
1054'
1055
Taylor Blau88617d12021-07-19 13:18:49 -04001056test_expect_success 'usage shown without sub-command' '
1057 test_expect_code 129 git multi-pack-index 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +09001058 ! test_grep "unrecognized subcommand" err
Taylor Blau88617d12021-07-19 13:18:49 -04001059'
1060
Taylor Blau73ff4ad2021-08-31 16:51:53 -04001061test_expect_success 'complains when run outside of a repository' '
1062 nongit test_must_fail git multi-pack-index write 2>err &&
1063 grep "not a git repository" err
1064'
1065
Patrick Steinhardt6eb095d2023-02-16 11:29:48 +01001066test_expect_success 'repack with delta islands' '
1067 git init repo &&
1068 test_when_finished "rm -fr repo" &&
1069 (
1070 cd repo &&
1071
1072 test_commit first &&
1073 git repack &&
1074 test_commit second &&
1075 git repack &&
1076
1077 git multi-pack-index write &&
1078 git -c repack.useDeltaIslands=true multi-pack-index repack
1079 )
1080'
1081
Jeff Kinge3c96002023-10-09 16:59:19 -04001082corrupt_chunk () {
1083 midx=.git/objects/pack/multi-pack-index &&
1084 test_when_finished "rm -rf $midx" &&
1085 git repack -ad --write-midx &&
1086 corrupt_chunk_file $midx "$@"
1087}
1088
1089test_expect_success 'reader notices too-small oid fanout chunk' '
1090 corrupt_chunk OIDF clear 00000000 &&
1091 test_must_fail git log 2>err &&
1092 cat >expect <<-\EOF &&
1093 error: multi-pack-index OID fanout is of the wrong size
1094 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1095 EOF
1096 test_cmp expect err
1097'
1098
Jeff Kingfc926562023-10-09 17:02:03 -04001099test_expect_success 'reader notices too-small oid lookup chunk' '
1100 corrupt_chunk OIDL clear 00000000 &&
1101 test_must_fail git log 2>err &&
1102 cat >expect <<-\EOF &&
1103 error: multi-pack-index OID lookup chunk is the wrong size
1104 fatal: multi-pack-index required OID lookup chunk missing or corrupted
1105 EOF
1106 test_cmp expect err
1107'
1108
Jeff King72a9a082023-10-09 17:05:14 -04001109test_expect_success 'reader notices too-small pack names chunk' '
1110 # There is no NUL to terminate the name here, so the
1111 # chunk is too short.
1112 corrupt_chunk PNAM clear 70656666 &&
1113 test_must_fail git log 2>err &&
1114 cat >expect <<-\EOF &&
1115 fatal: multi-pack-index pack-name chunk is too short
1116 EOF
1117 test_cmp expect err
1118'
1119
Jeff Kingc9b9fef2023-10-09 17:05:23 -04001120test_expect_success 'reader handles unaligned chunks' '
1121 # A 9-byte PNAM means all of the subsequent chunks
1122 # will no longer be 4-byte aligned, but it is still
1123 # a valid one-pack chunk on its own (it is "foo.pack\0").
1124 corrupt_chunk PNAM clear 666f6f2e7061636b00 &&
1125 git -c core.multipackindex=false log >expect.out &&
1126 git -c core.multipackindex=true log >out 2>err &&
1127 test_cmp expect.out out &&
1128 cat >expect.err <<-\EOF &&
1129 error: chunk id 4f494446 not 4-byte aligned
1130 EOF
1131 test_cmp expect.err err
1132'
1133
Jeff King09248692023-10-09 17:05:27 -04001134test_expect_success 'reader notices too-small object offset chunk' '
1135 corrupt_chunk OOFF clear 00000000 &&
1136 test_must_fail git log 2>err &&
1137 cat >expect <<-\EOF &&
1138 error: multi-pack-index object offset chunk is the wrong size
1139 fatal: multi-pack-index required object offsets chunk missing or corrupted
1140 EOF
1141 test_cmp expect err
1142'
1143
Jeff King2abd56e2023-10-09 17:05:30 -04001144test_expect_success 'reader bounds-checks large offset table' '
1145 # re-use the objects64 dir here to cheaply get access to a midx
1146 # with large offsets.
1147 git init repo &&
1148 test_when_finished "rm -rf repo" &&
1149 (
1150 cd repo &&
1151 (cd ../objects64 && pwd) >.git/objects/info/alternates &&
1152 git multi-pack-index --object-dir=../objects64 write &&
1153 midx=../objects64/pack/multi-pack-index &&
1154 corrupt_chunk_file $midx LOFF clear &&
Jeff King7538f9d2023-10-13 20:43:48 -04001155 # using only %(objectsize) is important here; see the commit
1156 # message for more details
1157 test_must_fail git cat-file --batch-all-objects \
1158 --batch-check="%(objectsize)" 2>err &&
Jeff King2abd56e2023-10-09 17:05:30 -04001159 cat >expect <<-\EOF &&
1160 fatal: multi-pack-index large offset out of bounds
1161 EOF
1162 test_cmp expect err
1163 )
1164'
1165
Jeff Kingc0fe9b22023-10-09 17:05:33 -04001166test_expect_success 'reader notices too-small revindex chunk' '
1167 # We only get a revindex with bitmaps (and likewise only
1168 # load it when they are asked for).
1169 test_config repack.writeBitmaps true &&
1170 corrupt_chunk RIDX clear 00000000 &&
1171 git -c core.multipackIndex=false rev-list \
1172 --all --use-bitmap-index >expect.out &&
1173 git -c core.multipackIndex=true rev-list \
1174 --all --use-bitmap-index >out 2>err &&
1175 test_cmp expect.out out &&
1176 cat >expect.err <<-\EOF &&
1177 error: multi-pack-index reverse-index chunk is the wrong size
1178 warning: multi-pack bitmap is missing required reverse index
1179 EOF
1180 test_cmp expect.err err
1181'
1182
Jeff King9d78fb02023-11-09 02:12:07 -05001183test_expect_success 'reader notices out-of-bounds fanout' '
1184 # This is similar to the out-of-bounds fanout test in t5318. The values
1185 # in adjacent entries should be large but not identical (they
1186 # are used as hi/lo starts for a binary search, which would then abort
1187 # immediately).
1188 corrupt_chunk OIDF 0 $(printf "%02x000000" $(test_seq 0 254)) &&
1189 test_must_fail git log 2>err &&
1190 cat >expect <<-\EOF &&
1191 error: oid fanout out of order: fanout[254] = fe000000 > 5c = fanout[255]
1192 fatal: multi-pack-index required OID fanout chunk missing or corrupted
1193 EOF
1194 test_cmp expect err
1195'
1196
Taylor Blau5f5ccd92023-12-14 17:23:51 -05001197test_expect_success 'bitmapped packs are stored via the BTMP chunk' '
1198 test_when_finished "rm -fr repo" &&
1199 git init repo &&
1200 (
1201 cd repo &&
1202
1203 for i in 1 2 3 4 5
1204 do
1205 test_commit "$i" &&
1206 git repack -d || return 1
1207 done &&
1208
1209 find $objdir/pack -type f -name "*.idx" | xargs -n 1 basename |
1210 sort >packs &&
1211
1212 git multi-pack-index write --stdin-packs <packs &&
1213 test_must_fail test-tool read-midx --bitmap $objdir 2>err &&
1214 cat >expect <<-\EOF &&
1215 error: MIDX does not contain the BTMP chunk
1216 EOF
1217 test_cmp expect err &&
1218
1219 git multi-pack-index write --stdin-packs --bitmap \
1220 --preferred-pack="$(head -n1 <packs)" <packs &&
1221 test-tool read-midx --bitmap $objdir >actual &&
1222 for i in $(test_seq $(wc -l <packs))
1223 do
1224 sed -ne "${i}s/\.idx$/\.pack/p" packs &&
1225 echo " bitmap_pos: $((($i - 1) * 3))" &&
1226 echo " bitmap_nr: 3" || return 1
1227 done >expect &&
1228 test_cmp expect actual
1229 )
1230'
1231
Derrick Stoleea3407732018-07-12 15:39:21 -04001232test_done