]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5302-pack-index.sh
The sixth batch for 2.26
[thirdparty/git.git] / t / t5302-pack-index.sh
CommitLineData
6e541776
NP
1#!/bin/sh
2#
3# Copyright (c) 2007 Nicolas Pitre
4#
5
6test_description='pack index with 64-bit offsets and object CRC'
7. ./test-lib.sh
8
796d1383 9test_expect_success 'setup' '
10 test_oid_init &&
11 rawsz=$(test_oid rawsz) &&
12 rm -rf .git &&
5be60078 13 git init &&
1415be8f 14 git config pack.threads 1 &&
b3431bc6 15 i=1 &&
2b5c208f 16 while test $i -le 100
6e541776 17 do
20cffb72 18 iii=$(printf '%03i' $i)
c680668d
NTND
19 test-tool genrandom "bar" 200 > wide_delta_$iii &&
20 test-tool genrandom "baz $iii" 50 >> wide_delta_$iii &&
21 test-tool genrandom "foo"$i 100 > deep_delta_$iii &&
22 test-tool genrandom "foo"$(expr $i + 1) 100 >> deep_delta_$iii &&
23 test-tool genrandom "foo"$(expr $i + 2) 100 >> deep_delta_$iii &&
2b5c208f 24 echo $iii >file_$iii &&
c680668d 25 test-tool genrandom "$iii" 8192 >>file_$iii &&
2b5c208f 26 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii &&
20cffb72 27 i=$(expr $i + 1) || return 1
6e541776 28 done &&
c680668d 29 { echo 101 && test-tool genrandom 100 8192; } >file_101 &&
5be60078 30 git update-index --add file_101 &&
20cffb72
EP
31 tree=$(git write-tree) &&
32 commit=$(git commit-tree $tree </dev/null) && {
6e541776 33 echo $tree &&
5be60078 34 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
6e541776 35 } >obj-list &&
796d1383 36 git update-ref HEAD $commit
37'
6e541776
NP
38
39test_expect_success \
40 'pack-objects with index version 1' \
5be60078
JH
41 'pack1=$(git pack-objects --index-version=1 test-1 <obj-list) &&
42 git verify-pack -v "test-1-${pack1}.pack"'
6e541776
NP
43
44test_expect_success \
45 'pack-objects with index version 2' \
5be60078
JH
46 'pack2=$(git pack-objects --index-version=2 test-2 <obj-list) &&
47 git verify-pack -v "test-2-${pack2}.pack"'
6e541776
NP
48
49test_expect_success \
50 'both packs should be identical' \
51 'cmp "test-1-${pack1}.pack" "test-2-${pack2}.pack"'
52
41ac414e 53test_expect_success \
6e541776 54 'index v1 and index v2 should be different' \
41ac414e 55 '! cmp "test-1-${pack1}.idx" "test-2-${pack2}.idx"'
6e541776
NP
56
57test_expect_success \
58 'index-pack with index version 1' \
3604e7c5 59 'git index-pack --index-version=1 -o 1.idx "test-1-${pack1}.pack"'
6e541776
NP
60
61test_expect_success \
62 'index-pack with index version 2' \
3604e7c5 63 'git index-pack --index-version=2 -o 2.idx "test-1-${pack1}.pack"'
6e541776
NP
64
65test_expect_success \
66 'index-pack results should match pack-objects ones' \
67 'cmp "test-1-${pack1}.idx" "1.idx" &&
68 cmp "test-2-${pack2}.idx" "2.idx"'
69
e337a04d
JH
70test_expect_success 'index-pack --verify on index version 1' '
71 git index-pack --verify "test-1-${pack1}.pack"
72'
73
74test_expect_success 'index-pack --verify on index version 2' '
75 git index-pack --verify "test-2-${pack2}.pack"
76'
77
6a301345
NTND
78test_expect_success \
79 'pack-objects --index-version=2, is not accepted' \
80 'test_must_fail git pack-objects --index-version=2, test-3 <obj-list'
81
6e541776
NP
82test_expect_success \
83 'index v2: force some 64-bit offsets with pack-objects' \
8ed2fca4
JS
84 'pack3=$(git pack-objects --index-version=2,0x40000 test-3 <obj-list)'
85
8ed2fca4 86if msg=$(git verify-pack -v "test-3-${pack3}.pack" 2>&1) ||
bbf08124 87 ! (echo "$msg" | grep "pack too large .* off_t")
8ed2fca4 88then
18bf8798 89 test_set_prereq OFF64_T
8ed2fca4 90else
fadb5156 91 say "# skipping tests concerning 64-bit offsets"
8ed2fca4
JS
92fi
93
18bf8798 94test_expect_success OFF64_T \
8ed2fca4
JS
95 'index v2: verify a pack with some 64-bit offsets' \
96 'git verify-pack -v "test-3-${pack3}.pack"'
6e541776 97
18bf8798 98test_expect_success OFF64_T \
6e541776 99 '64-bit offsets: should be different from previous index v2 results' \
41ac414e 100 '! cmp "test-2-${pack2}.idx" "test-3-${pack3}.idx"'
6e541776 101
18bf8798 102test_expect_success OFF64_T \
6e541776 103 'index v2: force some 64-bit offsets with index-pack' \
3604e7c5 104 'git index-pack --index-version=2,0x40000 -o 3.idx "test-1-${pack1}.pack"'
6e541776 105
18bf8798 106test_expect_success OFF64_T \
6e541776
NP
107 '64-bit offsets: index-pack result should match pack-objects one' \
108 'cmp "test-3-${pack3}.idx" "3.idx"'
109
e337a04d
JH
110test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2 (cheat)' '
111 # This cheats by knowing which lower offset should still be encoded
112 # in 64-bit representation.
113 git index-pack --verify --index-version=2,0x40000 "test-3-${pack3}.pack"
114'
115
3c9fc074 116test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2' '
e337a04d
JH
117 git index-pack --verify "test-3-${pack3}.pack"
118'
119
2b5c208f
NP
120# returns the object number for given object in given pack index
121index_obj_nr()
122{
123 idx_file=$1
124 object_sha1=$2
125 nr=0
126 git show-index < $idx_file |
127 while read offs sha1 extra
128 do
129 nr=$(($nr + 1))
130 test "$sha1" = "$object_sha1" || continue
131 echo "$(($nr - 1))"
132 break
133 done
134}
135
136# returns the pack offset for given object as found in given pack index
137index_obj_offset()
138{
139 idx_file=$1
140 object_sha1=$2
141 git show-index < $idx_file | grep $object_sha1 |
142 ( read offs extra && echo "$offs" )
143}
144
6e541776
NP
145test_expect_success \
146 '[index v1] 1) stream pack to repository' \
3604e7c5 147 'git index-pack --index-version=1 --stdin < "test-1-${pack1}.pack" &&
5be60078
JH
148 git prune-packed &&
149 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
6e541776
NP
150 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
151 cmp "test-1-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
152
153test_expect_success \
154 '[index v1] 2) create a stealth corruption in a delta base reference' \
2b5c208f
NP
155 '# This test assumes file_101 is a delta smaller than 16 bytes.
156 # It should be against file_100 but we substitute its base for file_099
20cffb72
EP
157 sha1_101=$(git hash-object file_101) &&
158 sha1_099=$(git hash-object file_099) &&
159 offs_101=$(index_obj_offset 1.idx $sha1_101) &&
160 nr_099=$(index_obj_nr 1.idx $sha1_099) &&
2b5c208f 161 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
796d1383 162 recordsz=$((rawsz + 4)) &&
2b5c208f
NP
163 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
164 if=".git/objects/pack/pack-${pack1}.idx" \
796d1383 165 skip=$((4 + 256 * 4 + $nr_099 * recordsz)) \
166 bs=1 count=$rawsz conv=notrunc &&
2b5c208f 167 git cat-file blob $sha1_101 > file_101_foo1'
6e541776 168
41ac414e 169test_expect_success \
6e541776 170 '[index v1] 3) corrupted delta happily returned wrong data' \
2b5c208f 171 'test -f file_101_foo1 && ! cmp file_101 file_101_foo1'
6e541776 172
41ac414e 173test_expect_success \
6e541776 174 '[index v1] 4) confirm that the pack is actually corrupted' \
d492b31c 175 'test_must_fail git fsck --full $commit'
6e541776
NP
176
177test_expect_success \
178 '[index v1] 5) pack-objects happily reuses corrupted data' \
5be60078 179 'pack4=$(git pack-objects test-4 <obj-list) &&
1190a1ac 180 test -f "test-4-${pack4}.pack"'
6e541776 181
41ac414e 182test_expect_success \
6e541776 183 '[index v1] 6) newly created pack is BAD !' \
1190a1ac 184 'test_must_fail git verify-pack -v "test-4-${pack4}.pack"'
6e541776
NP
185
186test_expect_success \
187 '[index v2] 1) stream pack to repository' \
188 'rm -f .git/objects/pack/* &&
3604e7c5 189 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
5be60078
JH
190 git prune-packed &&
191 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
6e541776 192 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
5f9ffff3 193 cmp "test-2-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
6e541776
NP
194
195test_expect_success \
196 '[index v2] 2) create a stealth corruption in a delta base reference' \
2b5c208f
NP
197 '# This test assumes file_101 is a delta smaller than 16 bytes.
198 # It should be against file_100 but we substitute its base for file_099
20cffb72
EP
199 sha1_101=$(git hash-object file_101) &&
200 sha1_099=$(git hash-object file_099) &&
201 offs_101=$(index_obj_offset 1.idx $sha1_101) &&
202 nr_099=$(index_obj_nr 1.idx $sha1_099) &&
2b5c208f
NP
203 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
204 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
205 if=".git/objects/pack/pack-${pack1}.idx" \
796d1383 206 skip=$((8 + 256 * 4 + $nr_099 * rawsz)) \
207 bs=1 count=$rawsz conv=notrunc &&
2b5c208f 208 git cat-file blob $sha1_101 > file_101_foo2'
6e541776 209
41ac414e 210test_expect_success \
6e541776 211 '[index v2] 3) corrupted delta happily returned wrong data' \
2b5c208f 212 'test -f file_101_foo2 && ! cmp file_101 file_101_foo2'
6e541776 213
41ac414e 214test_expect_success \
6e541776 215 '[index v2] 4) confirm that the pack is actually corrupted' \
d492b31c 216 'test_must_fail git fsck --full $commit'
6e541776 217
41ac414e 218test_expect_success \
6e541776 219 '[index v2] 5) pack-objects refuses to reuse corrupted data' \
0e8189e2
NP
220 'test_must_fail git pack-objects test-5 <obj-list &&
221 test_must_fail git pack-objects --no-reuse-object test-6 <obj-list'
6e541776 222
85fe23ed
NP
223test_expect_success \
224 '[index v2] 6) verify-pack detects CRC mismatch' \
225 'rm -f .git/objects/pack/* &&
3604e7c5 226 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
85fe23ed 227 git verify-pack ".git/objects/pack/pack-${pack1}.pack" &&
20cffb72
EP
228 obj=$(git hash-object file_001) &&
229 nr=$(index_obj_nr ".git/objects/pack/pack-${pack1}.idx" $obj) &&
85fe23ed 230 chmod +w ".git/objects/pack/pack-${pack1}.idx" &&
b689ccf6 231 printf xxxx | dd of=".git/objects/pack/pack-${pack1}.idx" conv=notrunc \
796d1383 232 bs=1 count=4 seek=$((8 + 256 * 4 + $(wc -l <obj-list) * rawsz + $nr * 4)) &&
85fe23ed
NP
233 ( while read obj
234 do git cat-file -p $obj >/dev/null || exit 1
235 done <obj-list ) &&
3de89c9d
JH
236 test_must_fail git verify-pack ".git/objects/pack/pack-${pack1}.pack"
237'
85fe23ed 238
a672ea6a
NP
239test_expect_success 'running index-pack in the object store' '
240 rm -f .git/objects/pack/* &&
241 cp test-1-${pack1}.pack .git/objects/pack/pack-${pack1}.pack &&
242 (
51b85471 243 cd .git/objects/pack &&
a672ea6a
NP
244 git index-pack pack-${pack1}.pack
245 ) &&
246 test -f .git/objects/pack/pack-${pack1}.idx
247'
248
f99b7af6
JS
249test_expect_success 'index-pack --strict warns upon missing tagger in tag' '
250 sha=$(git rev-parse HEAD) &&
251 cat >wrong-tag <<EOF &&
252object $sha
253type commit
254tag guten tag
255
256This is an invalid tag.
257EOF
258
259 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
260 pack1=$(echo $tag $sha | git pack-objects tag-test) &&
261 echo remove tag object &&
262 thirtyeight=${tag#??} &&
263 rm -f .git/objects/${tag%$thirtyeight}/$thirtyeight &&
264 git index-pack --strict tag-test-${pack1}.pack 2>err &&
f27d05b1 265 grep "^warning:.* expected .tagger. line" err
f99b7af6
JS
266'
267
ffb2c0fe
JT
268test_expect_success 'index-pack --fsck-objects also warns upon missing tagger in tag' '
269 git index-pack --fsck-objects tag-test-${pack1}.pack 2>err &&
270 grep "^warning:.* expected .tagger. line" err
271'
272
6e541776 273test_done