packfile.c: fix a typo in `each_file_in_pack_dir_fn()`'s declaration
[alt-git.git] / t / t5302-pack-index.sh
blobf89809be53cf335295e91b91af42de0c1ccf2b5b
1 #!/bin/sh
3 # Copyright (c) 2007 Nicolas Pitre
6 test_description='pack index with 64-bit offsets and object CRC'
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 test_expect_success 'setup' '
12 rawsz=$(test_oid rawsz) &&
13 rm -rf .git &&
14 git init &&
15 git config pack.threads 1 &&
16 i=1 &&
17 while test $i -le 100
19 iii=$(printf "%03i" $i) &&
20 test-tool genrandom "bar" 200 > wide_delta_$iii &&
21 test-tool genrandom "baz $iii" 50 >> wide_delta_$iii &&
22 test-tool genrandom "foo"$i 100 > deep_delta_$iii &&
23 test-tool genrandom "foo"$(expr $i + 1) 100 >> deep_delta_$iii &&
24 test-tool genrandom "foo"$(expr $i + 2) 100 >> deep_delta_$iii &&
25 echo $iii >file_$iii &&
26 test-tool genrandom "$iii" 8192 >>file_$iii &&
27 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii &&
28 i=$(expr $i + 1) || return 1
29 done &&
30 { echo 101 && test-tool genrandom 100 8192; } >file_101 &&
31 git update-index --add file_101 &&
32 tree=$(git write-tree) &&
33 commit=$(git commit-tree $tree </dev/null) && {
34 echo $tree &&
35 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
36 } >obj-list &&
37 git update-ref HEAD $commit
40 test_expect_success 'pack-objects with index version 1' '
41 pack1=$(git pack-objects --index-version=1 test-1 <obj-list) &&
42 git verify-pack -v "test-1-${pack1}.pack"
45 test_expect_success 'pack-objects with index version 2' '
46 pack2=$(git pack-objects --index-version=2 test-2 <obj-list) &&
47 git verify-pack -v "test-2-${pack2}.pack"
50 test_expect_success 'both packs should be identical' '
51 cmp "test-1-${pack1}.pack" "test-2-${pack2}.pack"
54 test_expect_success 'index v1 and index v2 should be different' '
55 ! cmp "test-1-${pack1}.idx" "test-2-${pack2}.idx"
58 test_expect_success 'index-pack with index version 1' '
59 git index-pack --index-version=1 -o 1.idx "test-1-${pack1}.pack"
62 test_expect_success 'index-pack with index version 2' '
63 git index-pack --index-version=2 -o 2.idx "test-1-${pack1}.pack"
66 test_expect_success 'index-pack results should match pack-objects ones' '
67 cmp "test-1-${pack1}.idx" "1.idx" &&
68 cmp "test-2-${pack2}.idx" "2.idx"
71 test_expect_success 'index-pack --verify on index version 1' '
72 git index-pack --verify "test-1-${pack1}.pack"
75 test_expect_success 'index-pack --verify on index version 2' '
76 git index-pack --verify "test-2-${pack2}.pack"
79 test_expect_success 'pack-objects --index-version=2, is not accepted' '
80 test_must_fail git pack-objects --index-version=2, test-3 <obj-list
83 test_expect_success 'index v2: force some 64-bit offsets with pack-objects' '
84 pack3=$(git pack-objects --index-version=2,0x40000 test-3 <obj-list)
87 if msg=$(git verify-pack -v "test-3-${pack3}.pack" 2>&1) ||
88 ! (echo "$msg" | grep "pack too large .* off_t")
89 then
90 test_set_prereq OFF64_T
91 else
92 say "# skipping tests concerning 64-bit offsets"
95 test_expect_success OFF64_T 'index v2: verify a pack with some 64-bit offsets' '
96 git verify-pack -v "test-3-${pack3}.pack"
99 test_expect_success OFF64_T '64-bit offsets: should be different from previous index v2 results' '
100 ! cmp "test-2-${pack2}.idx" "test-3-${pack3}.idx"
103 test_expect_success OFF64_T 'index v2: force some 64-bit offsets with index-pack' '
104 git index-pack --index-version=2,0x40000 -o 3.idx "test-1-${pack1}.pack"
107 test_expect_success OFF64_T '64-bit offsets: index-pack result should match pack-objects one' '
108 cmp "test-3-${pack3}.idx" "3.idx"
111 test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2 (cheat)' '
112 # This cheats by knowing which lower offset should still be encoded
113 # in 64-bit representation.
114 git index-pack --verify --index-version=2,0x40000 "test-3-${pack3}.pack"
117 test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2' '
118 git index-pack --verify "test-3-${pack3}.pack"
121 # returns the object number for given object in given pack index
122 index_obj_nr()
124 idx_file=$1
125 object_sha1=$2
126 nr=0
127 git show-index < $idx_file |
128 while read offs sha1 extra
130 nr=$(($nr + 1))
131 test "$sha1" = "$object_sha1" || continue
132 echo "$(($nr - 1))"
133 break
134 done
137 # returns the pack offset for given object as found in given pack index
138 index_obj_offset()
140 idx_file=$1
141 object_sha1=$2
142 git show-index < $idx_file | grep $object_sha1 |
143 ( read offs extra && echo "$offs" )
146 test_expect_success '[index v1] 1) stream pack to repository' '
147 git index-pack --index-version=1 --stdin < "test-1-${pack1}.pack" &&
148 git prune-packed &&
149 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
150 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
151 cmp "test-1-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"
154 test_expect_success \
155 '[index v1] 2) create a stealth corruption in a delta base reference' '
156 # This test assumes file_101 is a delta smaller than 16 bytes.
157 # It should be against file_100 but we substitute its base for file_099
158 sha1_101=$(git hash-object file_101) &&
159 sha1_099=$(git hash-object file_099) &&
160 offs_101=$(index_obj_offset 1.idx $sha1_101) &&
161 nr_099=$(index_obj_nr 1.idx $sha1_099) &&
162 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
163 recordsz=$((rawsz + 4)) &&
164 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
165 if=".git/objects/pack/pack-${pack1}.idx" \
166 skip=$((4 + 256 * 4 + $nr_099 * recordsz)) \
167 bs=1 count=$rawsz conv=notrunc &&
168 git cat-file blob $sha1_101 > file_101_foo1
171 test_expect_success \
172 '[index v1] 3) corrupted delta happily returned wrong data' '
173 test -f file_101_foo1 && ! cmp file_101 file_101_foo1
176 test_expect_success \
177 '[index v1] 4) confirm that the pack is actually corrupted' '
178 test_must_fail git fsck --full $commit
181 test_expect_success \
182 '[index v1] 5) pack-objects happily reuses corrupted data' '
183 pack4=$(git pack-objects test-4 <obj-list) &&
184 test -f "test-4-${pack4}.pack"
187 test_expect_success '[index v1] 6) newly created pack is BAD !' '
188 test_must_fail git verify-pack -v "test-4-${pack4}.pack"
191 test_expect_success '[index v2] 1) stream pack to repository' '
192 rm -f .git/objects/pack/* &&
193 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
194 git prune-packed &&
195 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
196 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
197 cmp "test-2-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"
200 test_expect_success \
201 '[index v2] 2) create a stealth corruption in a delta base reference' '
202 # This test assumes file_101 is a delta smaller than 16 bytes.
203 # It should be against file_100 but we substitute its base for file_099
204 sha1_101=$(git hash-object file_101) &&
205 sha1_099=$(git hash-object file_099) &&
206 offs_101=$(index_obj_offset 1.idx $sha1_101) &&
207 nr_099=$(index_obj_nr 1.idx $sha1_099) &&
208 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
209 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
210 if=".git/objects/pack/pack-${pack1}.idx" \
211 skip=$((8 + 256 * 4 + $nr_099 * rawsz)) \
212 bs=1 count=$rawsz conv=notrunc &&
213 git cat-file blob $sha1_101 > file_101_foo2
216 test_expect_success \
217 '[index v2] 3) corrupted delta happily returned wrong data' '
218 test -f file_101_foo2 && ! cmp file_101 file_101_foo2
221 test_expect_success \
222 '[index v2] 4) confirm that the pack is actually corrupted' '
223 test_must_fail git fsck --full $commit
226 test_expect_success \
227 '[index v2] 5) pack-objects refuses to reuse corrupted data' '
228 test_must_fail git pack-objects test-5 <obj-list &&
229 test_must_fail git pack-objects --no-reuse-object test-6 <obj-list
232 test_expect_success \
233 '[index v2] 6) verify-pack detects CRC mismatch' '
234 rm -f .git/objects/pack/* &&
235 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
236 git verify-pack ".git/objects/pack/pack-${pack1}.pack" &&
237 obj=$(git hash-object file_001) &&
238 nr=$(index_obj_nr ".git/objects/pack/pack-${pack1}.idx" $obj) &&
239 chmod +w ".git/objects/pack/pack-${pack1}.idx" &&
240 printf xxxx | dd of=".git/objects/pack/pack-${pack1}.idx" conv=notrunc \
241 bs=1 count=4 seek=$((8 + 256 * 4 + $(wc -l <obj-list) * rawsz + $nr * 4)) &&
242 ( while read obj
243 do git cat-file -p $obj >/dev/null || exit 1
244 done <obj-list ) &&
245 test_must_fail git verify-pack ".git/objects/pack/pack-${pack1}.pack"
248 test_expect_success 'running index-pack in the object store' '
249 rm -f .git/objects/pack/* &&
250 cp test-1-${pack1}.pack .git/objects/pack/pack-${pack1}.pack &&
252 cd .git/objects/pack &&
253 git index-pack pack-${pack1}.pack
254 ) &&
255 test -f .git/objects/pack/pack-${pack1}.idx
258 test_expect_success 'index-pack --strict warns upon missing tagger in tag' '
259 sha=$(git rev-parse HEAD) &&
260 cat >wrong-tag <<EOF &&
261 object $sha
262 type commit
263 tag guten tag
265 This is an invalid tag.
268 tag=$(git hash-object -t tag -w --stdin --literally <wrong-tag) &&
269 pack1=$(echo $tag $sha | git pack-objects tag-test) &&
270 echo remove tag object &&
271 thirtyeight=${tag#??} &&
272 rm -f .git/objects/${tag%$thirtyeight}/$thirtyeight &&
273 git index-pack --strict tag-test-${pack1}.pack 2>err &&
274 grep "^warning:.* expected .tagger. line" err
277 test_expect_success 'index-pack --fsck-objects also warns upon missing tagger in tag' '
278 git index-pack --fsck-objects tag-test-${pack1}.pack 2>err &&
279 grep "^warning:.* expected .tagger. line" err
282 test_expect_success 'index-pack -v --stdin produces progress for both phases' '
283 pack=$(git pack-objects --all pack </dev/null) &&
284 GIT_PROGRESS_DELAY=0 git index-pack -v --stdin <pack-$pack.pack 2>err &&
285 test_i18ngrep "Receiving objects" err &&
286 test_i18ngrep "Resolving deltas" err
289 test_expect_success 'too-large packs report the breach' '
290 pack=$(git pack-objects --all pack </dev/null) &&
291 sz="$(test_file_size pack-$pack.pack)" &&
292 test "$sz" -gt 20 &&
293 test_must_fail git index-pack --max-input-size=20 pack-$pack.pack 2>err &&
294 grep "maximum allowed size (20 bytes)" err
297 test_done