fix multiple issues with t5300
[git/jrn.git] / t / t5300-pack-object.sh
blob7c6231c66a8f01a56464953e8c48bce38d52aa83
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 test_description='git pack-object
9 . ./test-lib.sh
11 TRASH=`pwd`
13 test_expect_success \
14 'setup' \
15 'rm -f .git/index*
16 perl -e "print \"a\" x 4096;" > a &&
17 perl -e "print \"b\" x 4096;" > b &&
18 perl -e "print \"c\" x 4096;" > c &&
19 git update-index --add a b c &&
20 cat c >d && echo foo >>d && git update-index --add d &&
21 tree=`git write-tree` &&
22 commit=`git commit-tree $tree </dev/null` && {
23 echo $tree &&
24 echo $commit &&
25 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
26 } >obj-list && {
27 git diff-tree --root -p $commit &&
28 while read object
30 t=`git cat-file -t $object` &&
31 git cat-file $t $object || return 1
32 done <obj-list
33 } >expect'
35 test_expect_success \
36 'pack without delta' \
37 'packname_1=$(git pack-objects --window=0 test-1 <obj-list)'
39 rm -fr .git2
40 mkdir .git2
42 test_expect_success \
43 'unpack without delta' \
44 "GIT_OBJECT_DIRECTORY=.git2/objects &&
45 export GIT_OBJECT_DIRECTORY &&
46 git init &&
47 git unpack-objects -n <test-1-${packname_1}.pack &&
48 git unpack-objects <test-1-${packname_1}.pack"
50 unset GIT_OBJECT_DIRECTORY
51 cd "$TRASH/.git2"
53 test_expect_success \
54 'check unpack without delta' \
55 '(cd ../.git && find objects -type f -print) |
56 while read path
58 cmp $path ../.git/$path || {
59 echo $path differs.
60 return 1
62 done'
63 cd "$TRASH"
65 test_expect_success \
66 'pack with REF_DELTA' \
67 'pwd &&
68 packname_2=$(git pack-objects test-2 <obj-list)'
70 rm -fr .git2
71 mkdir .git2
73 test_expect_success \
74 'unpack with REF_DELTA' \
75 'GIT_OBJECT_DIRECTORY=.git2/objects &&
76 export GIT_OBJECT_DIRECTORY &&
77 git init &&
78 git unpack-objects -n <test-2-${packname_2}.pack &&
79 git unpack-objects <test-2-${packname_2}.pack'
81 unset GIT_OBJECT_DIRECTORY
82 cd "$TRASH/.git2"
83 test_expect_success \
84 'check unpack with REF_DELTA' \
85 '(cd ../.git && find objects -type f -print) |
86 while read path
88 cmp $path ../.git/$path || {
89 echo $path differs.
90 return 1
92 done'
93 cd "$TRASH"
95 test_expect_success \
96 'pack with OFS_DELTA' \
97 'pwd &&
98 packname_3=$(git pack-objects --delta-base-offset test-3 <obj-list)'
100 rm -fr .git2
101 mkdir .git2
103 test_expect_success \
104 'unpack with OFS_DELTA' \
105 'GIT_OBJECT_DIRECTORY=.git2/objects &&
106 export GIT_OBJECT_DIRECTORY &&
107 git init &&
108 git unpack-objects -n <test-3-${packname_3}.pack &&
109 git unpack-objects <test-3-${packname_3}.pack'
111 unset GIT_OBJECT_DIRECTORY
112 cd "$TRASH/.git2"
113 test_expect_success \
114 'check unpack with OFS_DELTA' \
115 '(cd ../.git && find objects -type f -print) |
116 while read path
118 cmp $path ../.git/$path || {
119 echo $path differs.
120 return 1
122 done'
123 cd "$TRASH"
125 test_expect_success 'compare delta flavors' '
126 perl -e '\''
127 defined($_ = -s $_) or die for @ARGV;
128 exit 1 if $ARGV[0] <= $ARGV[1];
129 '\'' test-2-$packname_2.pack test-3-$packname_3.pack
132 rm -fr .git2
133 mkdir .git2
135 test_expect_success \
136 'use packed objects' \
137 'GIT_OBJECT_DIRECTORY=.git2/objects &&
138 export GIT_OBJECT_DIRECTORY &&
139 git init &&
140 cp test-1-${packname_1}.pack test-1-${packname_1}.idx .git2/objects/pack && {
141 git diff-tree --root -p $commit &&
142 while read object
144 t=`git cat-file -t $object` &&
145 git cat-file $t $object || return 1
146 done <obj-list
147 } >current &&
148 diff expect current'
150 test_expect_success \
151 'use packed deltified (REF_DELTA) objects' \
152 'GIT_OBJECT_DIRECTORY=.git2/objects &&
153 export GIT_OBJECT_DIRECTORY &&
154 rm -f .git2/objects/pack/test-* &&
155 cp test-2-${packname_2}.pack test-2-${packname_2}.idx .git2/objects/pack && {
156 git diff-tree --root -p $commit &&
157 while read object
159 t=`git cat-file -t $object` &&
160 git cat-file $t $object || return 1
161 done <obj-list
162 } >current &&
163 diff expect current'
165 test_expect_success \
166 'use packed deltified (OFS_DELTA) objects' \
167 'GIT_OBJECT_DIRECTORY=.git2/objects &&
168 export GIT_OBJECT_DIRECTORY &&
169 rm -f .git2/objects/pack/test-* &&
170 cp test-3-${packname_3}.pack test-3-${packname_3}.idx .git2/objects/pack && {
171 git diff-tree --root -p $commit &&
172 while read object
174 t=`git cat-file -t $object` &&
175 git cat-file $t $object || return 1
176 done <obj-list
177 } >current &&
178 diff expect current'
180 unset GIT_OBJECT_DIRECTORY
182 test_expect_success 'survive missing objects/pack directory' '
184 rm -fr missing-pack &&
185 mkdir missing-pack &&
186 cd missing-pack &&
187 git init &&
188 GOP=.git/objects/pack
189 rm -fr $GOP &&
190 git index-pack --stdin --keep=test <../test-3-${packname_3}.pack &&
191 test -f $GOP/pack-${packname_3}.pack &&
192 test_cmp $GOP/pack-${packname_3}.pack ../test-3-${packname_3}.pack &&
193 test -f $GOP/pack-${packname_3}.idx &&
194 test_cmp $GOP/pack-${packname_3}.idx ../test-3-${packname_3}.idx &&
195 test -f $GOP/pack-${packname_3}.keep
199 test_expect_success \
200 'verify pack' \
201 'git verify-pack test-1-${packname_1}.idx \
202 test-2-${packname_2}.idx \
203 test-3-${packname_3}.idx'
205 test_expect_success \
206 'verify pack -v' \
207 'git verify-pack -v test-1-${packname_1}.idx \
208 test-2-${packname_2}.idx \
209 test-3-${packname_3}.idx'
211 test_expect_success \
212 'verify-pack catches mismatched .idx and .pack files' \
213 'cat test-1-${packname_1}.idx >test-3.idx &&
214 cat test-2-${packname_2}.pack >test-3.pack &&
215 if git verify-pack test-3.idx
216 then false
217 else :;
220 test_expect_success \
221 'verify-pack catches a corrupted pack signature' \
222 'cat test-1-${packname_1}.pack >test-3.pack &&
223 echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=2 &&
224 if git verify-pack test-3.idx
225 then false
226 else :;
229 test_expect_success \
230 'verify-pack catches a corrupted pack version' \
231 'cat test-1-${packname_1}.pack >test-3.pack &&
232 echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=7 &&
233 if git verify-pack test-3.idx
234 then false
235 else :;
238 test_expect_success \
239 'verify-pack catches a corrupted type/size of the 1st packed object data' \
240 'cat test-1-${packname_1}.pack >test-3.pack &&
241 echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=12 &&
242 if git verify-pack test-3.idx
243 then false
244 else :;
247 test_expect_success \
248 'verify-pack catches a corrupted sum of the index file itself' \
249 'l=`wc -c <test-3.idx` &&
250 l=`expr $l - 20` &&
251 cat test-1-${packname_1}.pack >test-3.pack &&
252 printf "%20s" "" | dd of=test-3.idx count=20 bs=1 conv=notrunc seek=$l &&
253 if git verify-pack test-3.pack
254 then false
255 else :;
258 test_expect_success \
259 'build pack index for an existing pack' \
260 'cat test-1-${packname_1}.pack >test-3.pack &&
261 git index-pack -o tmp.idx test-3.pack &&
262 cmp tmp.idx test-1-${packname_1}.idx &&
264 git index-pack test-3.pack &&
265 cmp test-3.idx test-1-${packname_1}.idx &&
267 cat test-2-${packname_2}.pack >test-3.pack &&
268 git index-pack -o tmp.idx test-2-${packname_2}.pack &&
269 cmp tmp.idx test-2-${packname_2}.idx &&
271 git index-pack test-3.pack &&
272 cmp test-3.idx test-2-${packname_2}.idx &&
274 cat test-3-${packname_3}.pack >test-3.pack &&
275 git index-pack -o tmp.idx test-3-${packname_3}.pack &&
276 cmp tmp.idx test-3-${packname_3}.idx &&
278 git index-pack test-3.pack &&
279 cmp test-3.idx test-3-${packname_3}.idx &&
283 test_expect_success 'unpacking with --strict' '
285 for j in a b c d e f g
287 for i in 0 1 2 3 4 5 6 7 8 9
289 o=$(echo $j$i | git hash-object -w --stdin) &&
290 echo "100644 $o 0 $j$i"
291 done
292 done >LIST &&
293 rm -f .git/index &&
294 git update-index --index-info <LIST &&
295 LIST=$(git write-tree) &&
296 rm -f .git/index &&
297 head -n 10 LIST | git update-index --index-info &&
298 LI=$(git write-tree) &&
299 rm -f .git/index &&
300 tail -n 10 LIST | git update-index --index-info &&
301 ST=$(git write-tree) &&
302 PACK5=$( git rev-list --objects "$LIST" "$LI" "$ST" | \
303 git pack-objects test-5 ) &&
304 PACK6=$( (
305 echo "$LIST"
306 echo "$LI"
307 echo "$ST"
308 ) | git pack-objects test-6 ) &&
309 test_create_repo test-5 &&
311 cd test-5 &&
312 git unpack-objects --strict <../test-5-$PACK5.pack &&
313 git ls-tree -r $LIST &&
314 git ls-tree -r $LI &&
315 git ls-tree -r $ST
316 ) &&
317 test_create_repo test-6 &&
319 # tree-only into empty repo -- many unreachables
320 cd test-6 &&
321 test_must_fail git unpack-objects --strict <../test-6-$PACK6.pack
322 ) &&
324 # already populated -- no unreachables
325 cd test-5 &&
326 git unpack-objects --strict <../test-6-$PACK6.pack
330 test_expect_success 'index-pack with --strict' '
332 for j in a b c d e f g
334 for i in 0 1 2 3 4 5 6 7 8 9
336 o=$(echo $j$i | git hash-object -w --stdin) &&
337 echo "100644 $o 0 $j$i"
338 done
339 done >LIST &&
340 rm -f .git/index &&
341 git update-index --index-info <LIST &&
342 LIST=$(git write-tree) &&
343 rm -f .git/index &&
344 head -n 10 LIST | git update-index --index-info &&
345 LI=$(git write-tree) &&
346 rm -f .git/index &&
347 tail -n 10 LIST | git update-index --index-info &&
348 ST=$(git write-tree) &&
349 PACK5=$( git rev-list --objects "$LIST" "$LI" "$ST" | \
350 git pack-objects test-5 ) &&
351 PACK6=$( (
352 echo "$LIST"
353 echo "$LI"
354 echo "$ST"
355 ) | git pack-objects test-6 ) &&
356 test_create_repo test-7 &&
358 cd test-7 &&
359 git index-pack --strict --stdin <../test-5-$PACK5.pack &&
360 git ls-tree -r $LIST &&
361 git ls-tree -r $LI &&
362 git ls-tree -r $ST
363 ) &&
364 test_create_repo test-8 &&
366 # tree-only into empty repo -- many unreachables
367 cd test-8 &&
368 test_must_fail git index-pack --strict --stdin <../test-6-$PACK6.pack
369 ) &&
371 # already populated -- no unreachables
372 cd test-7 &&
373 git index-pack --strict --stdin <../test-6-$PACK6.pack
377 test_expect_success 'honor pack.packSizeLimit' '
378 git config pack.packSizeLimit 200 &&
379 packname_10=$(git pack-objects test-10 <obj-list) &&
380 test 3 = $(ls test-10-*.pack | wc -l)
383 test_expect_success 'verify resulting packs' '
384 git verify-pack test-10-*.pack
387 test_expect_success 'tolerate absurdly small packsizelimit' '
388 git config pack.packSizeLimit 2 &&
389 packname_11=$(git pack-objects test-11 <obj-list) &&
390 test $(wc -l <obj-list) = $(ls test-11-*.pack | wc -l)
393 test_expect_success 'verify resulting packs' '
394 git verify-pack test-11-*.pack
398 # WARNING!
400 # The following test is destructive. Please keep the next
401 # two tests at the end of this file.
404 test_expect_success \
405 'fake a SHA1 hash collision' \
406 'test -f .git/objects/c8/2de19312b6c3695c0c18f70709a6c535682a67 &&
407 cp -f .git/objects/9d/235ed07cd19811a6ceb342de82f190e49c9f68 \
408 .git/objects/c8/2de19312b6c3695c0c18f70709a6c535682a67'
410 test_expect_success \
411 'make sure index-pack detects the SHA1 collision' \
412 'test_must_fail git index-pack -o bad.idx test-3.pack 2>msg &&
413 grep "SHA1 COLLISION FOUND" msg'
415 test_done