]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1006-cat-file.sh
7b985cfdedf40eb702d51cf6884db5bfda603749
[thirdparty/git.git] / t / t1006-cat-file.sh
1 #!/bin/sh
2
3 test_description='git cat-file'
4
5 . ./test-lib.sh
6
7 test_cmdmode_usage () {
8 test_expect_code 129 "$@" 2>err &&
9 grep "^error:.*is incompatible with" err
10 }
11
12 for switches in \
13 '-e -p' \
14 '-p -t' \
15 '-t -s' \
16 '-s --textconv' \
17 '--textconv --filters' \
18 '--batch-all-objects -e'
19 do
20 test_expect_success "usage: cmdmode $switches" '
21 test_cmdmode_usage git cat-file $switches
22 '
23 done
24
25 test_incompatible_usage () {
26 test_expect_code 129 "$@" 2>err &&
27 grep -E "^(fatal|error):.*(requires|incompatible with|needs)" err
28 }
29
30 for opt in --batch --batch-check
31 do
32 test_expect_success "usage: incompatible options: --path with $opt" '
33 test_incompatible_usage git cat-file --path=foo $opt
34 '
35 done
36
37 test_missing_usage () {
38 test_expect_code 129 "$@" 2>err &&
39 grep -E "^fatal:.*required" err
40 }
41
42 short_modes="-e -p -t -s"
43 cw_modes="--textconv --filters"
44
45 for opt in $cw_modes
46 do
47 test_expect_success "usage: $opt requires another option" '
48 test_missing_usage git cat-file $opt
49 '
50 done
51
52 for opt in $short_modes
53 do
54 test_expect_success "usage: $opt requires another option" '
55 test_missing_usage git cat-file $opt
56 '
57
58 for opt2 in --batch \
59 --batch-check \
60 --follow-symlinks \
61 "--path=foo HEAD:some-path.txt"
62 do
63 test_expect_success "usage: incompatible options: $opt and $opt2" '
64 test_incompatible_usage git cat-file $opt $opt2
65 '
66 done
67 done
68
69 test_too_many_arguments () {
70 test_expect_code 129 "$@" 2>err &&
71 grep -E "^fatal: too many arguments$" err
72 }
73
74 for opt in $short_modes $cw_modes
75 do
76 args="one two three"
77 test_expect_success "usage: too many arguments: $opt $args" '
78 test_too_many_arguments git cat-file $opt $args
79 '
80
81 for opt2 in --buffer --follow-symlinks
82 do
83 test_expect_success "usage: incompatible arguments: $opt with batch option $opt2" '
84 test_incompatible_usage git cat-file $opt $opt2
85 '
86 done
87 done
88
89 for opt in --buffer \
90 --follow-symlinks \
91 --batch-all-objects \
92 -z
93 do
94 test_expect_success "usage: bad option combination: $opt without batch mode" '
95 test_incompatible_usage git cat-file $opt &&
96 test_incompatible_usage git cat-file $opt commit HEAD
97 '
98 done
99
100 echo_without_newline () {
101 printf '%s' "$*"
102 }
103
104 echo_without_newline_nul () {
105 echo_without_newline "$@" | tr '\n' '\0'
106 }
107
108 strlen () {
109 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
110 }
111
112 run_tests () {
113 type=$1
114 sha1=$2
115 size=$3
116 content=$4
117 pretty_content=$5
118
119 batch_output="$sha1 $type $size
120 $content"
121
122 test_expect_success "$type exists" '
123 git cat-file -e $sha1
124 '
125
126 test_expect_success "Type of $type is correct" '
127 echo $type >expect &&
128 git cat-file -t $sha1 >actual &&
129 test_cmp expect actual
130 '
131
132 test_expect_success "Size of $type is correct" '
133 echo $size >expect &&
134 git cat-file -s $sha1 >actual &&
135 test_cmp expect actual
136 '
137
138 test_expect_success "Type of $type is correct using --allow-unknown-type" '
139 echo $type >expect &&
140 git cat-file -t --allow-unknown-type $sha1 >actual &&
141 test_cmp expect actual
142 '
143
144 test_expect_success "Size of $type is correct using --allow-unknown-type" '
145 echo $size >expect &&
146 git cat-file -s --allow-unknown-type $sha1 >actual &&
147 test_cmp expect actual
148 '
149
150 test -z "$content" ||
151 test_expect_success "Content of $type is correct" '
152 echo_without_newline "$content" >expect &&
153 git cat-file $type $sha1 >actual &&
154 test_cmp expect actual
155 '
156
157 test_expect_success "Pretty content of $type is correct" '
158 echo_without_newline "$pretty_content" >expect &&
159 git cat-file -p $sha1 >actual &&
160 test_cmp expect actual
161 '
162
163 test -z "$content" ||
164 test_expect_success "--batch output of $type is correct" '
165 echo "$batch_output" >expect &&
166 echo $sha1 | git cat-file --batch >actual &&
167 test_cmp expect actual
168 '
169
170 test_expect_success "--batch-check output of $type is correct" '
171 echo "$sha1 $type $size" >expect &&
172 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
173 test_cmp expect actual
174 '
175
176 for opt in --buffer --no-buffer
177 do
178 test -z "$content" ||
179 test_expect_success "--batch-command $opt output of $type content is correct" '
180 echo "$batch_output" >expect &&
181 test_write_lines "contents $sha1" | git cat-file --batch-command $opt >actual &&
182 test_cmp expect actual
183 '
184
185 test_expect_success "--batch-command $opt output of $type info is correct" '
186 echo "$sha1 $type $size" >expect &&
187 test_write_lines "info $sha1" |
188 git cat-file --batch-command $opt >actual &&
189 test_cmp expect actual
190 '
191 done
192
193 test_expect_success "custom --batch-check format" '
194 echo "$type $sha1" >expect &&
195 echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
196 test_cmp expect actual
197 '
198
199 test_expect_success "custom --batch-command format" '
200 echo "$type $sha1" >expect &&
201 echo "info $sha1" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
202 test_cmp expect actual
203 '
204
205 test_expect_success '--batch-check with %(rest)' '
206 echo "$type this is some extra content" >expect &&
207 echo "$sha1 this is some extra content" |
208 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
209 test_cmp expect actual
210 '
211
212 test -z "$content" ||
213 test_expect_success "--batch without type ($type)" '
214 {
215 echo "$size" &&
216 echo "$content"
217 } >expect &&
218 echo $sha1 | git cat-file --batch="%(objectsize)" >actual &&
219 test_cmp expect actual
220 '
221
222 test -z "$content" ||
223 test_expect_success "--batch without size ($type)" '
224 {
225 echo "$type" &&
226 echo "$content"
227 } >expect &&
228 echo $sha1 | git cat-file --batch="%(objecttype)" >actual &&
229 test_cmp expect actual
230 '
231 }
232
233 hello_content="Hello World"
234 hello_size=$(strlen "$hello_content")
235 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
236
237 test_expect_success "setup" '
238 echo_without_newline "$hello_content" > hello &&
239 git update-index --add hello
240 '
241
242 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
243
244 test_expect_success '--batch-command --buffer with flush for blob info' '
245 echo "$hello_sha1 blob $hello_size" >expect &&
246 test_write_lines "info $hello_sha1" "flush" |
247 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
248 git cat-file --batch-command --buffer >actual &&
249 test_cmp expect actual
250 '
251
252 test_expect_success '--batch-command --buffer without flush for blob info' '
253 touch output &&
254 test_write_lines "info $hello_sha1" |
255 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
256 git cat-file --batch-command --buffer >>output &&
257 test_must_be_empty output
258 '
259
260 test_expect_success '--batch-check without %(rest) considers whole line' '
261 echo "$hello_sha1 blob $hello_size" >expect &&
262 git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
263 test_when_finished "git update-index --remove \"white space\"" &&
264 echo ":white space" | git cat-file --batch-check >actual &&
265 test_cmp expect actual
266 '
267
268 tree_sha1=$(git write-tree)
269 tree_size=$(($(test_oid rawsz) + 13))
270 tree_pretty_content="100644 blob $hello_sha1 hello${LF}"
271
272 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
273
274 commit_message="Initial commit"
275 commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
276 commit_size=$(($(test_oid hexsz) + 137))
277 commit_content="tree $tree_sha1
278 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE
279 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
280
281 $commit_message"
282
283 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content"
284
285 tag_header_without_timestamp="object $hello_sha1
286 type blob
287 tag hellotag
288 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
289 tag_description="This is a tag"
290 tag_content="$tag_header_without_timestamp 0 +0000
291
292 $tag_description"
293
294 tag_sha1=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
295 tag_size=$(strlen "$tag_content")
296
297 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content"
298
299 test_expect_success "Reach a blob from a tag pointing to it" '
300 echo_without_newline "$hello_content" >expect &&
301 git cat-file blob $tag_sha1 >actual &&
302 test_cmp expect actual
303 '
304
305 for batch in batch batch-check batch-command
306 do
307 for opt in t s e p
308 do
309 test_expect_success "Passing -$opt with --$batch fails" '
310 test_must_fail git cat-file --$batch -$opt $hello_sha1
311 '
312
313 test_expect_success "Passing --$batch with -$opt fails" '
314 test_must_fail git cat-file -$opt --$batch $hello_sha1
315 '
316 done
317
318 test_expect_success "Passing <type> with --$batch fails" '
319 test_must_fail git cat-file --$batch blob $hello_sha1
320 '
321
322 test_expect_success "Passing --$batch with <type> fails" '
323 test_must_fail git cat-file blob --$batch $hello_sha1
324 '
325
326 test_expect_success "Passing sha1 with --$batch fails" '
327 test_must_fail git cat-file --$batch $hello_sha1
328 '
329 done
330
331 for opt in t s e p
332 do
333 test_expect_success "Passing -$opt with --follow-symlinks fails" '
334 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
335 '
336 done
337
338 test_expect_success "--batch-check for a non-existent named object" '
339 cat >expect <<-EOF &&
340 foobar42 missing
341 foobar84 missing
342 EOF
343
344 printf "foobar42\nfoobar84" >in &&
345 git cat-file --batch-check <in >actual &&
346 test_cmp expect actual
347 '
348
349 test_expect_success "--batch-check for a non-existent hash" '
350 cat >expect <<-EOF &&
351 0000000000000000000000000000000000000042 missing
352 0000000000000000000000000000000000000084 missing
353 EOF
354
355 printf "0000000000000000000000000000000000000042\n0000000000000000000000000000000000000084" >in &&
356 git cat-file --batch-check <in >actual &&
357 test_cmp expect actual
358 '
359
360 test_expect_success "--batch for an existent and a non-existent hash" '
361 cat >expect <<-EOF &&
362 $tag_sha1 tag $tag_size
363 $tag_content
364 0000000000000000000000000000000000000000 missing
365 EOF
366
367 printf "$tag_sha1\n0000000000000000000000000000000000000000" >in &&
368 git cat-file --batch <in >actual &&
369 test_cmp expect actual
370 '
371
372 test_expect_success "--batch-check for an empty line" '
373 cat >expect <<-EOF &&
374 missing
375 EOF
376
377 echo >in &&
378 git cat-file --batch-check <in >actual &&
379 test_cmp expect actual
380 '
381
382 test_expect_success 'empty --batch-check notices missing object' '
383 echo "$ZERO_OID missing" >expect &&
384 echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
385 test_cmp expect actual
386 '
387
388 batch_input="$hello_sha1
389 $commit_sha1
390 $tag_sha1
391 deadbeef
392
393 "
394
395 batch_output="$hello_sha1 blob $hello_size
396 $hello_content
397 $commit_sha1 commit $commit_size
398 $commit_content
399 $tag_sha1 tag $tag_size
400 $tag_content
401 deadbeef missing
402 missing"
403
404 test_expect_success '--batch with multiple sha1s gives correct format' '
405 echo "$batch_output" >expect &&
406 echo_without_newline "$batch_input" >in &&
407 git cat-file --batch <in >actual &&
408 test_cmp expect actual
409 '
410
411 test_expect_success '--batch, -z with multiple sha1s gives correct format' '
412 echo_without_newline_nul "$batch_input" >in &&
413 echo "$batch_output" >expect &&
414 git cat-file --batch -z <in >actual &&
415 test_cmp expect actual
416 '
417
418 batch_check_input="$hello_sha1
419 $tree_sha1
420 $commit_sha1
421 $tag_sha1
422 deadbeef
423
424 "
425
426 batch_check_output="$hello_sha1 blob $hello_size
427 $tree_sha1 tree $tree_size
428 $commit_sha1 commit $commit_size
429 $tag_sha1 tag $tag_size
430 deadbeef missing
431 missing"
432
433 test_expect_success "--batch-check with multiple sha1s gives correct format" '
434 echo "$batch_check_output" >expect &&
435 echo_without_newline "$batch_check_input" >in &&
436 git cat-file --batch-check <in >actual &&
437 test_cmp expect actual
438 '
439
440 test_expect_success "--batch-check, -z with multiple sha1s gives correct format" '
441 echo "$batch_check_output" >expect &&
442 echo_without_newline_nul "$batch_check_input" >in &&
443 git cat-file --batch-check -z <in >actual &&
444 test_cmp expect actual
445 '
446
447 test_expect_success FUNNYNAMES '--batch-check, -z with newline in input' '
448 touch -- "newline${LF}embedded" &&
449 git add -- "newline${LF}embedded" &&
450 git commit -m "file with newline embedded" &&
451 test_tick &&
452
453 printf "HEAD:newline${LF}embedded" >in &&
454 git cat-file --batch-check -z <in >actual &&
455
456 echo "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
457 test_cmp expect actual
458 '
459
460 batch_command_multiple_info="info $hello_sha1
461 info $tree_sha1
462 info $commit_sha1
463 info $tag_sha1
464 info deadbeef"
465
466 test_expect_success '--batch-command with multiple info calls gives correct format' '
467 cat >expect <<-EOF &&
468 $hello_sha1 blob $hello_size
469 $tree_sha1 tree $tree_size
470 $commit_sha1 commit $commit_size
471 $tag_sha1 tag $tag_size
472 deadbeef missing
473 EOF
474
475 echo "$batch_command_multiple_info" >in &&
476 git cat-file --batch-command --buffer <in >actual &&
477
478 test_cmp expect actual &&
479
480 echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
481 git cat-file --batch-command --buffer -z <in >actual &&
482
483 test_cmp expect actual
484 '
485
486 batch_command_multiple_contents="contents $hello_sha1
487 contents $commit_sha1
488 contents $tag_sha1
489 contents deadbeef
490 flush"
491
492 test_expect_success '--batch-command with multiple command calls gives correct format' '
493 cat >expect <<-EOF &&
494 $hello_sha1 blob $hello_size
495 $hello_content
496 $commit_sha1 commit $commit_size
497 $commit_content
498 $tag_sha1 tag $tag_size
499 $tag_content
500 deadbeef missing
501 EOF
502
503 echo "$batch_command_multiple_contents" >in &&
504 git cat-file --batch-command --buffer <in >actual &&
505
506 test_cmp expect actual &&
507
508 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
509 git cat-file --batch-command --buffer -z <in >actual &&
510
511 test_cmp expect actual
512 '
513
514 test_expect_success 'setup blobs which are likely to delta' '
515 test-tool genrandom foo 10240 >foo &&
516 { cat foo && echo plus; } >foo-plus &&
517 git add foo foo-plus &&
518 git commit -m foo &&
519 cat >blobs <<-\EOF
520 HEAD:foo
521 HEAD:foo-plus
522 EOF
523 '
524
525 test_expect_success 'confirm that neither loose blob is a delta' '
526 cat >expect <<-EOF &&
527 $ZERO_OID
528 $ZERO_OID
529 EOF
530 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
531 test_cmp expect actual
532 '
533
534 # To avoid relying too much on the current delta heuristics,
535 # we will check only that one of the two objects is a delta
536 # against the other, but not the order. We can do so by just
537 # asking for the base of both, and checking whether either
538 # sha1 appears in the output.
539 test_expect_success '%(deltabase) reports packed delta bases' '
540 git repack -ad &&
541 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
542 {
543 grep "$(git rev-parse HEAD:foo)" actual ||
544 grep "$(git rev-parse HEAD:foo-plus)" actual
545 }
546 '
547
548 test_expect_success 'setup bogus data' '
549 bogus_short_type="bogus" &&
550 bogus_short_content="bogus" &&
551 bogus_short_size=$(strlen "$bogus_short_content") &&
552 bogus_short_sha1=$(echo_without_newline "$bogus_short_content" | git hash-object -t $bogus_short_type --literally -w --stdin) &&
553
554 bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" &&
555 bogus_long_content="bogus" &&
556 bogus_long_size=$(strlen "$bogus_long_content") &&
557 bogus_long_sha1=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin)
558 '
559
560 for arg1 in '' --allow-unknown-type
561 do
562 for arg2 in -s -t -p
563 do
564 if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p"
565 then
566 continue
567 fi
568
569
570 test_expect_success "cat-file $arg1 $arg2 error on bogus short OID" '
571 cat >expect <<-\EOF &&
572 fatal: invalid object type
573 EOF
574
575 if test "$arg1" = "--allow-unknown-type"
576 then
577 git cat-file $arg1 $arg2 $bogus_short_sha1
578 else
579 test_must_fail git cat-file $arg1 $arg2 $bogus_short_sha1 >out 2>actual &&
580 test_must_be_empty out &&
581 test_cmp expect actual
582 fi
583 '
584
585 test_expect_success "cat-file $arg1 $arg2 error on bogus full OID" '
586 if test "$arg2" = "-p"
587 then
588 cat >expect <<-EOF
589 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
590 fatal: Not a valid object name $bogus_long_sha1
591 EOF
592 else
593 cat >expect <<-EOF
594 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
595 fatal: git cat-file: could not get object info
596 EOF
597 fi &&
598
599 if test "$arg1" = "--allow-unknown-type"
600 then
601 git cat-file $arg1 $arg2 $bogus_short_sha1
602 else
603 test_must_fail git cat-file $arg1 $arg2 $bogus_long_sha1 >out 2>actual &&
604 test_must_be_empty out &&
605 test_cmp expect actual
606 fi
607 '
608
609 test_expect_success "cat-file $arg1 $arg2 error on missing short OID" '
610 cat >expect.err <<-EOF &&
611 fatal: Not a valid object name $(test_oid deadbeef_short)
612 EOF
613 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef_short) >out 2>err.actual &&
614 test_must_be_empty out &&
615 test_cmp expect.err err.actual
616 '
617
618 test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
619 if test "$arg2" = "-p"
620 then
621 cat >expect.err <<-EOF
622 fatal: Not a valid object name $(test_oid deadbeef)
623 EOF
624 else
625 cat >expect.err <<-\EOF
626 fatal: git cat-file: could not get object info
627 EOF
628 fi &&
629 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
630 test_must_be_empty out &&
631 test_cmp expect.err err.actual
632 '
633 done
634 done
635
636 test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
637 git cat-file -e $bogus_short_sha1
638 '
639
640 test_expect_success '-e can not be combined with --allow-unknown-type' '
641 test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_sha1
642 '
643
644 test_expect_success '-p cannot print a broken object even with --allow-unknown-type' '
645 test_must_fail git cat-file -p $bogus_short_sha1 &&
646 test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_sha1
647 '
648
649 test_expect_success '<type> <hash> does not work with objects of broken types' '
650 cat >err.expect <<-\EOF &&
651 fatal: invalid object type "bogus"
652 EOF
653 test_must_fail git cat-file $bogus_short_type $bogus_short_sha1 2>err.actual &&
654 test_cmp err.expect err.actual
655 '
656
657 test_expect_success 'broken types combined with --batch and --batch-check' '
658 echo $bogus_short_sha1 >bogus-oid &&
659
660 cat >err.expect <<-\EOF &&
661 fatal: invalid object type
662 EOF
663
664 test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
665 test_cmp err.expect err.actual &&
666
667 test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
668 test_cmp err.expect err.actual
669 '
670
671 test_expect_success 'the --batch and --batch-check options do not combine with --allow-unknown-type' '
672 test_expect_code 128 git cat-file --batch --allow-unknown-type <bogus-oid &&
673 test_expect_code 128 git cat-file --batch-check --allow-unknown-type <bogus-oid
674 '
675
676 test_expect_success 'the --allow-unknown-type option does not consider replacement refs' '
677 cat >expect <<-EOF &&
678 $bogus_short_type
679 EOF
680 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
681 test_cmp expect actual &&
682
683 # Create it manually, as "git replace" will die on bogus
684 # types.
685 head=$(git rev-parse --verify HEAD) &&
686 test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_sha1" &&
687 test-tool ref-store main update-ref msg "refs/replace/$bogus_short_sha1" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
688
689 cat >expect <<-EOF &&
690 commit
691 EOF
692 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
693 test_cmp expect actual
694 '
695
696 test_expect_success "Type of broken object is correct" '
697 echo $bogus_short_type >expect &&
698 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
699 test_cmp expect actual
700 '
701
702 test_expect_success "Size of broken object is correct" '
703 echo $bogus_short_size >expect &&
704 git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual &&
705 test_cmp expect actual
706 '
707
708 test_expect_success 'clean up broken object' '
709 rm .git/objects/$(test_oid_to_path $bogus_short_sha1)
710 '
711
712 test_expect_success "Type of broken object is correct when type is large" '
713 echo $bogus_long_type >expect &&
714 git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual &&
715 test_cmp expect actual
716 '
717
718 test_expect_success "Size of large broken object is correct when type is large" '
719 echo $bogus_long_size >expect &&
720 git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual &&
721 test_cmp expect actual
722 '
723
724 test_expect_success 'clean up broken object' '
725 rm .git/objects/$(test_oid_to_path $bogus_long_sha1)
726 '
727
728 test_expect_success 'cat-file -t and -s on corrupt loose object' '
729 git init --bare corrupt-loose.git &&
730 (
731 cd corrupt-loose.git &&
732
733 # Setup and create the empty blob and its path
734 empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
735 empty_blob=$(git hash-object -w --stdin </dev/null) &&
736
737 # Create another blob and its path
738 echo other >other.blob &&
739 other_blob=$(git hash-object -w --stdin <other.blob) &&
740 other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
741
742 # Before the swap the size is 0
743 cat >out.expect <<-EOF &&
744 0
745 EOF
746 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
747 test_must_be_empty err.actual &&
748 test_cmp out.expect out.actual &&
749
750 # Swap the two to corrupt the repository
751 mv -f "$other_path" "$empty_path" &&
752 test_must_fail git fsck 2>err.fsck &&
753 grep "hash-path mismatch" err.fsck &&
754
755 # confirm that cat-file is reading the new swapped-in
756 # blob...
757 cat >out.expect <<-EOF &&
758 blob
759 EOF
760 git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
761 test_must_be_empty err.actual &&
762 test_cmp out.expect out.actual &&
763
764 # ... since it has a different size now.
765 cat >out.expect <<-EOF &&
766 6
767 EOF
768 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
769 test_must_be_empty err.actual &&
770 test_cmp out.expect out.actual &&
771
772 # So far "cat-file" has been happy to spew the found
773 # content out as-is. Try to make it zlib-invalid.
774 mv -f other.blob "$empty_path" &&
775 test_must_fail git fsck 2>err.fsck &&
776 cat >expect <<-EOF &&
777 error: inflate: data stream error (incorrect header check)
778 error: unable to unpack header of ./$empty_path
779 error: $empty_blob: object corrupt or missing: ./$empty_path
780 EOF
781 grep "^error: " err.fsck >actual &&
782 test_cmp expect actual
783 )
784 '
785
786 # Tests for git cat-file --follow-symlinks
787 test_expect_success 'prep for symlink tests' '
788 echo_without_newline "$hello_content" >morx &&
789 test_ln_s_add morx same-dir-link &&
790 test_ln_s_add dir link-to-dir &&
791 test_ln_s_add ../fleem out-of-repo-link &&
792 test_ln_s_add .. out-of-repo-link-dir &&
793 test_ln_s_add same-dir-link link-to-link &&
794 test_ln_s_add nope broken-same-dir-link &&
795 mkdir dir &&
796 test_ln_s_add ../morx dir/parent-dir-link &&
797 test_ln_s_add .. dir/link-dir &&
798 test_ln_s_add ../../escape dir/out-of-repo-link &&
799 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
800 test_ln_s_add nope dir/broken-link-in-dir &&
801 mkdir dir/subdir &&
802 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
803 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
804 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
805 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
806 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
807 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
808 echo_without_newline "$hello_content" >dir/ind1 &&
809 test_ln_s_add dir dirlink &&
810 test_ln_s_add dir/subdir subdirlink &&
811 test_ln_s_add subdir/ind2 dir/link-to-child &&
812 test_ln_s_add dir/link-to-child link-to-down-link &&
813 test_ln_s_add dir/.. up-down &&
814 test_ln_s_add dir/../ up-down-trailing &&
815 test_ln_s_add dir/../morx up-down-file &&
816 test_ln_s_add dir/../../morx up-up-down-file &&
817 test_ln_s_add subdirlink/../../morx up-two-down-file &&
818 test_ln_s_add loop1 loop2 &&
819 test_ln_s_add loop2 loop1 &&
820 git add morx dir/subdir/ind2 dir/ind1 &&
821 git commit -am "test" &&
822 echo $hello_sha1 blob $hello_size >found
823 '
824
825 test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
826 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
827 test_cmp found actual &&
828 echo HEAD:nope missing >expect &&
829 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
830 test_cmp expect actual
831 '
832
833 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
834 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
835 test_cmp found actual
836 '
837
838 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
839 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
840 test_cmp found actual
841 '
842
843
844 test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
845 echo dangling 25 >expect &&
846 echo HEAD:broken-same-dir-link >>expect &&
847 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
848 test_cmp expect actual
849 '
850
851 test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
852 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
853 test_cmp found actual
854 '
855
856 test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
857 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
858 test_cmp found actual &&
859 echo notdir 29 >expect &&
860 echo HEAD:dir/parent-dir-link/nope >>expect &&
861 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
862 test_cmp expect actual
863 '
864
865 test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
866 echo dangling 22 >expect &&
867 echo HEAD:dir/link-dir/nope >>expect &&
868 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
869 test_cmp expect actual &&
870 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
871 test_cmp found actual &&
872 echo dangling 27 >expect &&
873 echo HEAD:dir/broken-link-in-dir >>expect &&
874 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
875 test_cmp expect actual
876 '
877
878 test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
879 echo notdir 41 >expect &&
880 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
881 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
882 test_cmp expect actual &&
883 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
884 test_cmp found actual &&
885 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
886 test_cmp found actual
887 '
888
889 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
890 echo dangling 17 >expect &&
891 echo HEAD:dirlink/morx >>expect &&
892 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
893 test_cmp expect actual &&
894 echo $hello_sha1 blob $hello_size >expect &&
895 echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
896 test_cmp expect actual
897 '
898
899 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
900 echo dangling 20 >expect &&
901 echo HEAD:subdirlink/morx >>expect &&
902 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
903 test_cmp expect actual &&
904 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
905 test_cmp found actual
906 '
907
908 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
909 echo notdir 27 >expect &&
910 echo HEAD:dir/link-to-child/morx >>expect &&
911 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
912 test_cmp expect actual &&
913 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
914 test_cmp found actual &&
915 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
916 test_cmp found actual
917 '
918
919 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
920 echo symlink 8 >expect &&
921 echo ../fleem >>expect &&
922 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
923 test_cmp expect actual &&
924 echo symlink 2 >expect &&
925 echo .. >>expect &&
926 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
927 test_cmp expect actual
928 '
929
930 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
931 echo symlink 9 >expect &&
932 echo ../escape >>expect &&
933 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
934 test_cmp expect actual &&
935 echo symlink 2 >expect &&
936 echo .. >>expect &&
937 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
938 test_cmp expect actual
939 '
940
941 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
942 echo symlink 15 >expect &&
943 echo ../great-escape >>expect &&
944 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
945 test_cmp expect actual &&
946 echo symlink 2 >expect &&
947 echo .. >>expect &&
948 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
949 test_cmp expect actual &&
950 echo symlink 3 >expect &&
951 echo ../ >>expect &&
952 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
953 test_cmp expect actual
954 '
955
956 test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
957 echo HEAD: | git cat-file --batch-check >expect &&
958 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
959 test_cmp expect actual &&
960 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
961 test_cmp expect actual &&
962 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
963 test_cmp found actual &&
964 echo symlink 7 >expect &&
965 echo ../morx >>expect &&
966 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
967 test_cmp expect actual &&
968 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
969 test_cmp found actual
970 '
971
972 test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
973 echo loop 10 >expect &&
974 echo HEAD:loop1 >>expect &&
975 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
976 test_cmp expect actual
977 '
978
979 test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
980 echo HEAD:morx | git cat-file --batch >expect &&
981 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
982 test_cmp expect actual
983 '
984
985 test_expect_success 'cat-file --batch-all-objects shows all objects' '
986 # make new repos so we know the full set of objects; we will
987 # also make sure that there are some packed and some loose
988 # objects, some referenced and some not, some duplicates, and that
989 # there are some available only via alternates.
990 git init all-one &&
991 (
992 cd all-one &&
993 echo content >file &&
994 git add file &&
995 git commit -qm base &&
996 git rev-parse HEAD HEAD^{tree} HEAD:file &&
997 git repack -ad &&
998 echo not-cloned | git hash-object -w --stdin
999 ) >expect.unsorted &&
1000 git clone -s all-one all-two &&
1001 (
1002 cd all-two &&
1003 echo local-unref | git hash-object -w --stdin
1004 ) >>expect.unsorted &&
1005 git -C all-two rev-parse HEAD:file |
1006 git -C all-two pack-objects .git/objects/pack/pack &&
1007 sort <expect.unsorted >expect &&
1008 git -C all-two cat-file --batch-all-objects \
1009 --batch-check="%(objectname)" >actual &&
1010 test_cmp expect actual
1011 '
1012
1013 # The only user-visible difference is that the objects are no longer sorted,
1014 # and the resulting sort order is undefined. So we can only check that it
1015 # produces the same objects as the ordered case, but that at least exercises
1016 # the code.
1017 test_expect_success 'cat-file --unordered works' '
1018 git -C all-two cat-file --batch-all-objects --unordered \
1019 --batch-check="%(objectname)" >actual.unsorted &&
1020 sort <actual.unsorted >actual &&
1021 test_cmp expect actual
1022 '
1023
1024 test_expect_success 'set up object list for --batch-all-objects tests' '
1025 git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects
1026 '
1027
1028 test_expect_success 'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
1029 git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
1030 git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
1031 cmp expect actual
1032 '
1033
1034 test_expect_success 'cat-file --batch="%(rest)" with --batch-all-objects will work' '
1035 git -C all-two cat-file --batch="%(rest)" <objects >expect &&
1036 git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
1037 cmp expect actual
1038 '
1039
1040 test_expect_success 'cat-file --batch="batman" with --batch-all-objects will work' '
1041 git -C all-two cat-file --batch="batman" <objects >expect &&
1042 git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
1043 cmp expect actual
1044 '
1045
1046 test_expect_success 'set up replacement object' '
1047 orig=$(git rev-parse HEAD) &&
1048 git cat-file commit $orig >orig &&
1049 {
1050 cat orig &&
1051 echo extra
1052 } >fake &&
1053 fake=$(git hash-object -t commit -w fake) &&
1054 orig_size=$(git cat-file -s $orig) &&
1055 fake_size=$(git cat-file -s $fake) &&
1056 git replace $orig $fake
1057 '
1058
1059 test_expect_success 'cat-file --batch respects replace objects' '
1060 git cat-file --batch >actual <<-EOF &&
1061 $orig
1062 EOF
1063 {
1064 echo "$orig commit $fake_size" &&
1065 cat fake &&
1066 echo
1067 } >expect &&
1068 test_cmp expect actual
1069 '
1070
1071 test_expect_success 'cat-file --batch-check respects replace objects' '
1072 git cat-file --batch-check >actual <<-EOF &&
1073 $orig
1074 EOF
1075 echo "$orig commit $fake_size" >expect &&
1076 test_cmp expect actual
1077 '
1078
1079 # Pull the entry for object with oid "$1" out of the output of
1080 # "cat-file --batch", including its object content (which requires
1081 # parsing and reading a set amount of bytes, hence perl).
1082 extract_batch_output () {
1083 perl -ne '
1084 BEGIN { $oid = shift }
1085 if (/^$oid \S+ (\d+)$/) {
1086 print;
1087 read STDIN, my $buf, $1;
1088 print $buf;
1089 print "\n";
1090 }
1091 ' "$@"
1092 }
1093
1094 test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
1095 git cat-file --batch-all-objects --batch >actual.raw &&
1096 extract_batch_output $orig <actual.raw >actual &&
1097 {
1098 echo "$orig commit $orig_size" &&
1099 cat orig &&
1100 echo
1101 } >expect &&
1102 test_cmp expect actual
1103 '
1104
1105 test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
1106 git cat-file --batch-all-objects --batch-check >actual.raw &&
1107 grep ^$orig actual.raw >actual &&
1108 echo "$orig commit $orig_size" >expect &&
1109 test_cmp expect actual
1110 '
1111 test_expect_success 'batch-command empty command' '
1112 echo "" >cmd &&
1113 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1114 grep "^fatal:.*empty command in input.*" err
1115 '
1116
1117 test_expect_success 'batch-command whitespace before command' '
1118 echo " info deadbeef" >cmd &&
1119 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1120 grep "^fatal:.*whitespace before command.*" err
1121 '
1122
1123 test_expect_success 'batch-command unknown command' '
1124 echo unknown_command >cmd &&
1125 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1126 grep "^fatal:.*unknown command.*" err
1127 '
1128
1129 test_expect_success 'batch-command missing arguments' '
1130 echo "info" >cmd &&
1131 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1132 grep "^fatal:.*info requires arguments.*" err
1133 '
1134
1135 test_expect_success 'batch-command flush with arguments' '
1136 echo "flush arg" >cmd &&
1137 test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
1138 grep "^fatal:.*flush takes no arguments.*" err
1139 '
1140
1141 test_expect_success 'batch-command flush without --buffer' '
1142 echo "flush" >cmd &&
1143 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1144 grep "^fatal:.*flush is only for --buffer mode.*" err
1145 '
1146
1147 test_done