Merge branch 'master' of git://repo.or.cz/alt-git
[git/dscho.git] / t / t5302-pack-index.sh
blob5da7d639ddf2bd04eaf35a1341f90a21fc0f76cf
1 #!/bin/sh
3 # Copyright (c) 2007 Nicolas Pitre
6 test_description='pack index with 64-bit offsets and object CRC'
7 . ./test-lib.sh
9 zeros () {
10 while :; do
11 echo -n xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
12 done | tr x '\0'
15 test_expect_success \
16 'setup' \
17 'rm -rf .git
18 git init &&
19 git config pack.threads 1 &&
20 i=1 &&
21 while test $i -le 100
23 iii=`printf '%03i' $i`
24 test-genrandom "bar" 200 > wide_delta_$iii &&
25 test-genrandom "baz $iii" 50 >> wide_delta_$iii &&
26 test-genrandom "foo"$i 100 > deep_delta_$iii &&
27 test-genrandom "foo"`expr $i + 1` 100 >> deep_delta_$iii &&
28 test-genrandom "foo"`expr $i + 2` 100 >> deep_delta_$iii &&
29 echo $iii >file_$iii &&
30 test-genrandom "$iii" 8192 >>file_$iii &&
31 git update-index --add file_$iii deep_delta_$iii wide_delta_$iii &&
32 i=`expr $i + 1` || return 1
33 done &&
34 { echo 101 && test-genrandom 100 8192; } >file_101 &&
35 git update-index --add file_101 &&
36 tree=`git write-tree` &&
37 commit=`git commit-tree $tree </dev/null` && {
38 echo $tree &&
39 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
40 } >obj-list &&
41 git update-ref HEAD $commit'
43 test_expect_success \
44 'pack-objects with index version 1' \
45 'pack1=$(git pack-objects --index-version=1 test-1 <obj-list) &&
46 git verify-pack -v "test-1-${pack1}.pack"'
48 test_expect_success \
49 'pack-objects with index version 2' \
50 'pack2=$(git pack-objects --index-version=2 test-2 <obj-list) &&
51 git verify-pack -v "test-2-${pack2}.pack"'
53 test_expect_success \
54 'both packs should be identical' \
55 'cmp "test-1-${pack1}.pack" "test-2-${pack2}.pack"'
57 test_expect_success \
58 'index v1 and index v2 should be different' \
59 '! cmp "test-1-${pack1}.idx" "test-2-${pack2}.idx"'
61 test_expect_success \
62 'index-pack with index version 1' \
63 'git index-pack --index-version=1 -o 1.idx "test-1-${pack1}.pack"'
65 test_expect_success \
66 'index-pack with index version 2' \
67 'git index-pack --index-version=2 -o 2.idx "test-1-${pack1}.pack"'
69 test_expect_success \
70 'index-pack results should match pack-objects ones' \
71 'cmp "test-1-${pack1}.idx" "1.idx" &&
72 cmp "test-2-${pack2}.idx" "2.idx"'
74 test_expect_success \
75 'index v2: force some 64-bit offsets with pack-objects' \
76 'pack3=$(git pack-objects --index-version=2,0x40000 test-3 <obj-list)'
78 have_64bits=
79 if msg=$(git verify-pack -v "test-3-${pack3}.pack" 2>&1) ||
80 ! (echo "$msg" | grep "pack too large .* off_t")
81 then
82 have_64bits=t
83 else
84 say "skipping tests concerning 64-bit offsets"
87 test "$have_64bits" &&
88 test_expect_success \
89 'index v2: verify a pack with some 64-bit offsets' \
90 'git verify-pack -v "test-3-${pack3}.pack"'
92 test "$have_64bits" &&
93 test_expect_success \
94 '64-bit offsets: should be different from previous index v2 results' \
95 '! cmp "test-2-${pack2}.idx" "test-3-${pack3}.idx"'
97 test "$have_64bits" &&
98 test_expect_success \
99 'index v2: force some 64-bit offsets with index-pack' \
100 'git index-pack --index-version=2,0x40000 -o 3.idx "test-1-${pack1}.pack"'
102 test "$have_64bits" &&
103 test_expect_success \
104 '64-bit offsets: index-pack result should match pack-objects one' \
105 'cmp "test-3-${pack3}.idx" "3.idx"'
107 # returns the object number for given object in given pack index
108 index_obj_nr()
110 idx_file=$1
111 object_sha1=$2
112 nr=0
113 git show-index < $idx_file |
114 while read offs sha1 extra
116 nr=$(($nr + 1))
117 test "$sha1" = "$object_sha1" || continue
118 echo "$(($nr - 1))"
119 break
120 done
123 # returns the pack offset for given object as found in given pack index
124 index_obj_offset()
126 idx_file=$1
127 object_sha1=$2
128 git show-index < $idx_file | grep $object_sha1 |
129 ( read offs extra && echo "$offs" )
132 test_expect_success \
133 '[index v1] 1) stream pack to repository' \
134 'git index-pack --index-version=1 --stdin < "test-1-${pack1}.pack" &&
135 git prune-packed &&
136 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
137 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
138 cmp "test-1-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
140 test_expect_success \
141 '[index v1] 2) create a stealth corruption in a delta base reference' \
142 '# This test assumes file_101 is a delta smaller than 16 bytes.
143 # It should be against file_100 but we substitute its base for file_099
144 sha1_101=`git hash-object file_101` &&
145 sha1_099=`git hash-object file_099` &&
146 offs_101=`index_obj_offset 1.idx $sha1_101` &&
147 nr_099=`index_obj_nr 1.idx $sha1_099` &&
148 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
149 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
150 if=".git/objects/pack/pack-${pack1}.idx" \
151 skip=$((4 + 256 * 4 + $nr_099 * 24)) \
152 bs=1 count=20 conv=notrunc &&
153 git cat-file blob $sha1_101 > file_101_foo1'
155 test_expect_success \
156 '[index v1] 3) corrupted delta happily returned wrong data' \
157 'test -f file_101_foo1 && ! cmp file_101 file_101_foo1'
159 test_expect_success \
160 '[index v1] 4) confirm that the pack is actually corrupted' \
161 'test_must_fail git fsck --full $commit'
163 test_expect_success \
164 '[index v1] 5) pack-objects happily reuses corrupted data' \
165 'pack4=$(git pack-objects test-4 <obj-list) &&
166 test -f "test-4-${pack1}.pack"'
168 test_expect_success \
169 '[index v1] 6) newly created pack is BAD !' \
170 'test_must_fail git verify-pack -v "test-4-${pack1}.pack"'
172 test_expect_success \
173 '[index v2] 1) stream pack to repository' \
174 'rm -f .git/objects/pack/* &&
175 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
176 git prune-packed &&
177 git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
178 cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
179 cmp "test-2-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"'
181 test_expect_success \
182 '[index v2] 2) create a stealth corruption in a delta base reference' \
183 '# This test assumes file_101 is a delta smaller than 16 bytes.
184 # It should be against file_100 but we substitute its base for file_099
185 sha1_101=`git hash-object file_101` &&
186 sha1_099=`git hash-object file_099` &&
187 offs_101=`index_obj_offset 1.idx $sha1_101` &&
188 nr_099=`index_obj_nr 1.idx $sha1_099` &&
189 chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
190 dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
191 if=".git/objects/pack/pack-${pack1}.idx" \
192 skip=$((8 + 256 * 4 + $nr_099 * 20)) \
193 bs=1 count=20 conv=notrunc &&
194 git cat-file blob $sha1_101 > file_101_foo2'
196 test_expect_success \
197 '[index v2] 3) corrupted delta happily returned wrong data' \
198 'test -f file_101_foo2 && ! cmp file_101 file_101_foo2'
200 test_expect_success \
201 '[index v2] 4) confirm that the pack is actually corrupted' \
202 'test_must_fail git fsck --full $commit'
204 test_expect_success \
205 '[index v2] 5) pack-objects refuses to reuse corrupted data' \
206 'test_must_fail git pack-objects test-5 <obj-list &&
207 test_must_fail git pack-objects --no-reuse-object test-6 <obj-list'
209 test_expect_success \
210 '[index v2] 6) verify-pack detects CRC mismatch' \
211 'rm -f .git/objects/pack/* &&
212 git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
213 git verify-pack ".git/objects/pack/pack-${pack1}.pack" &&
214 obj=`git hash-object file_001` &&
215 nr=`index_obj_nr ".git/objects/pack/pack-${pack1}.idx" $obj` &&
216 chmod +w ".git/objects/pack/pack-${pack1}.idx" &&
217 zeros | dd of=".git/objects/pack/pack-${pack1}.idx" conv=notrunc \
218 bs=1 count=4 seek=$((8 + 256 * 4 + `wc -l <obj-list` * 20 + $nr * 4)) &&
219 ( while read obj
220 do git cat-file -p $obj >/dev/null || exit 1
221 done <obj-list ) &&
222 err=$(test_must_fail git verify-pack \
223 ".git/objects/pack/pack-${pack1}.pack" 2>&1) &&
224 echo "$err" | grep "CRC mismatch"'
226 test_expect_success 'running index-pack in the object store' '
227 rm -f .git/objects/pack/* &&
228 cp test-1-${pack1}.pack .git/objects/pack/pack-${pack1}.pack &&
230 cd .git/objects/pack
231 git index-pack pack-${pack1}.pack
232 ) &&
233 test -f .git/objects/pack/pack-${pack1}.idx
236 test_done