3 # Copyright (c) 2005 Junio C Hamano
6 test_description
='git pack-object
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` && {
25 git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
27 git diff-tree --root -p $commit &&
30 t=`git cat-file -t $object` &&
31 git cat-file $t $object || return 1
36 'pack without delta' \
37 'packname_1=$(git pack-objects --window=0 test-1 <obj-list)'
43 'unpack without delta' \
44 "GIT_OBJECT_DIRECTORY=.git2/objects &&
45 export GIT_OBJECT_DIRECTORY &&
47 git unpack-objects -n <test-1-${packname_1}.pack &&
48 git unpack-objects <test-1-${packname_1}.pack"
50 unset GIT_OBJECT_DIRECTORY
54 'check unpack without delta' \
55 '(cd ../.git && find objects -type f -print) |
58 cmp $path ../.git/$path || {
66 'pack with REF_DELTA' \
68 packname_2=$(git pack-objects test-2 <obj-list)'
74 'unpack with REF_DELTA' \
75 'GIT_OBJECT_DIRECTORY=.git2/objects &&
76 export GIT_OBJECT_DIRECTORY &&
78 git unpack-objects -n <test-2-${packname_2}.pack &&
79 git unpack-objects <test-2-${packname_2}.pack'
81 unset GIT_OBJECT_DIRECTORY
84 'check unpack with REF_DELTA' \
85 '(cd ../.git && find objects -type f -print) |
88 cmp $path ../.git/$path || {
96 'pack with OFS_DELTA' \
98 packname_3=$(git pack-objects --delta-base-offset test-3 <obj-list)'
103 test_expect_success \
104 'unpack with OFS_DELTA' \
105 'GIT_OBJECT_DIRECTORY=.git2/objects &&
106 export GIT_OBJECT_DIRECTORY &&
108 git unpack-objects -n <test-3-${packname_3}.pack &&
109 git unpack-objects <test-3-${packname_3}.pack'
111 unset GIT_OBJECT_DIRECTORY
113 test_expect_success \
114 'check unpack with OFS_DELTA' \
115 '(cd ../.git && find objects -type f -print) |
118 cmp $path ../.git/$path || {
125 test_expect_success
'compare delta flavors' '
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
135 test_expect_success \
136 'use packed objects' \
137 'GIT_OBJECT_DIRECTORY=.git2/objects &&
138 export GIT_OBJECT_DIRECTORY &&
140 cp test-1-${packname_1}.pack test-1-${packname_1}.idx .git2/objects/pack && {
141 git diff-tree --root -p $commit &&
144 t=`git cat-file -t $object` &&
145 git cat-file $t $object || return 1
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 &&
159 t=`git cat-file -t $object` &&
160 git cat-file $t $object || return 1
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 &&
174 t=`git cat-file -t $object` &&
175 git cat-file $t $object || return 1
180 unset GIT_OBJECT_DIRECTORY
182 test_expect_success
'survive missing objects/pack directory' '
184 rm -fr missing-pack &&
185 mkdir missing-pack &&
188 GOP=.git/objects/pack
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 \
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 \
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
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
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
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
247 test_expect_success \
248 'verify-pack catches a corrupted sum of the index file itself' \
249 'l=`wc -c <test-3.idx` &&
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
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"
294 git update-index --index-info <LIST &&
295 LIST=$(git write-tree) &&
297 head -n 10 LIST | git update-index --index-info &&
298 LI=$(git write-tree) &&
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 ) &&
308 ) | git pack-objects test-6 ) &&
309 test_create_repo test-5 &&
312 git unpack-objects --strict <../test-5-$PACK5.pack &&
313 git ls-tree -r $LIST &&
314 git ls-tree -r $LI &&
317 test_create_repo test-6 &&
319 # tree-only into empty repo -- many unreachables
321 test_must_fail git unpack-objects --strict <../test-6-$PACK6.pack
324 # already populated -- no unreachables
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"
341 git update-index --index-info <LIST &&
342 LIST=$(git write-tree) &&
344 head -n 10 LIST | git update-index --index-info &&
345 LI=$(git write-tree) &&
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 ) &&
355 ) | git pack-objects test-6 ) &&
356 test_create_repo test-7 &&
359 git index-pack --strict --stdin <../test-5-$PACK5.pack &&
360 git ls-tree -r $LIST &&
361 git ls-tree -r $LI &&
364 test_create_repo test-8 &&
366 # tree-only into empty repo -- many unreachables
368 test_must_fail git index-pack --strict --stdin <../test-6-$PACK6.pack
371 # already populated -- no unreachables
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
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'