builtin/show: do not prune by pathspec
[git/mjg.git] / t / t1006-cat-file.sh
blobd36cd7c0863591d197cfc3ef340972943ff40e6f
1 #!/bin/sh
3 test_description='git cat-file'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 test_cmdmode_usage () {
9 test_expect_code 129 "$@" 2>err &&
10 grep "^error: .* cannot be used together" err
13 for switches in \
14 '-e -p' \
15 '-p -t' \
16 '-t -s' \
17 '-s --textconv' \
18 '--textconv --filters' \
19 '--batch-all-objects -e'
21 test_expect_success "usage: cmdmode $switches" '
22 test_cmdmode_usage git cat-file $switches
24 done
26 test_incompatible_usage () {
27 test_expect_code 129 "$@" 2>err &&
28 grep -E "^(fatal|error):.*(requires|incompatible with|needs)" err
31 for opt in --batch --batch-check
33 test_expect_success "usage: incompatible options: --path with $opt" '
34 test_incompatible_usage git cat-file --path=foo $opt
36 done
38 test_missing_usage () {
39 test_expect_code 129 "$@" 2>err &&
40 grep -E "^fatal:.*required" err
43 short_modes="-e -p -t -s"
44 cw_modes="--textconv --filters"
46 for opt in $cw_modes
48 test_expect_success "usage: $opt requires another option" '
49 test_missing_usage git cat-file $opt
51 done
53 for opt in $short_modes
55 test_expect_success "usage: $opt requires another option" '
56 test_missing_usage git cat-file $opt
59 for opt2 in --batch \
60 --batch-check \
61 --follow-symlinks \
62 "--path=foo HEAD:some-path.txt"
64 test_expect_success "usage: incompatible options: $opt and $opt2" '
65 test_incompatible_usage git cat-file $opt $opt2
67 done
68 done
70 test_too_many_arguments () {
71 test_expect_code 129 "$@" 2>err &&
72 grep -E "^fatal: too many arguments$" err
75 for opt in $short_modes $cw_modes
77 args="one two three"
78 test_expect_success "usage: too many arguments: $opt $args" '
79 test_too_many_arguments git cat-file $opt $args
82 for opt2 in --buffer --follow-symlinks
84 test_expect_success "usage: incompatible arguments: $opt with batch option $opt2" '
85 test_incompatible_usage git cat-file $opt $opt2
87 done
88 done
90 for opt in --buffer \
91 --follow-symlinks \
92 --batch-all-objects \
93 -z \
96 test_expect_success "usage: bad option combination: $opt without batch mode" '
97 test_incompatible_usage git cat-file $opt &&
98 test_incompatible_usage git cat-file $opt commit HEAD
100 done
102 echo_without_newline () {
103 printf '%s' "$*"
106 echo_without_newline_nul () {
107 echo_without_newline "$@" | tr '\n' '\0'
110 strlen () {
111 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
114 run_tests () {
115 type=$1
116 oid=$2
117 size=$3
118 content=$4
119 pretty_content=$5
121 batch_output="$oid $type $size
122 $content"
124 test_expect_success "$type exists" '
125 git cat-file -e $oid
128 test_expect_success "Type of $type is correct" '
129 echo $type >expect &&
130 git cat-file -t $oid >actual &&
131 test_cmp expect actual
134 test_expect_success "Size of $type is correct" '
135 echo $size >expect &&
136 git cat-file -s $oid >actual &&
137 test_cmp expect actual
140 test_expect_success "Type of $type is correct using --allow-unknown-type" '
141 echo $type >expect &&
142 git cat-file -t --allow-unknown-type $oid >actual &&
143 test_cmp expect actual
146 test_expect_success "Size of $type is correct using --allow-unknown-type" '
147 echo $size >expect &&
148 git cat-file -s --allow-unknown-type $oid >actual &&
149 test_cmp expect actual
152 test -z "$content" ||
153 test_expect_success "Content of $type is correct" '
154 echo_without_newline "$content" >expect &&
155 git cat-file $type $oid >actual &&
156 test_cmp expect actual
159 test_expect_success "Pretty content of $type is correct" '
160 echo_without_newline "$pretty_content" >expect &&
161 git cat-file -p $oid >actual &&
162 test_cmp expect actual
165 test -z "$content" ||
166 test_expect_success "--batch output of $type is correct" '
167 echo "$batch_output" >expect &&
168 echo $oid | git cat-file --batch >actual &&
169 test_cmp expect actual
172 test_expect_success "--batch-check output of $type is correct" '
173 echo "$oid $type $size" >expect &&
174 echo_without_newline $oid | git cat-file --batch-check >actual &&
175 test_cmp expect actual
178 for opt in --buffer --no-buffer
180 test -z "$content" ||
181 test_expect_success "--batch-command $opt output of $type content is correct" '
182 echo "$batch_output" >expect &&
183 test_write_lines "contents $oid" | git cat-file --batch-command $opt >actual &&
184 test_cmp expect actual
187 test_expect_success "--batch-command $opt output of $type info is correct" '
188 echo "$oid $type $size" >expect &&
189 test_write_lines "info $oid" |
190 git cat-file --batch-command $opt >actual &&
191 test_cmp expect actual
193 done
195 test_expect_success "custom --batch-check format" '
196 echo "$type $oid" >expect &&
197 echo $oid | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
198 test_cmp expect actual
201 test_expect_success "custom --batch-command format" '
202 echo "$type $oid" >expect &&
203 echo "info $oid" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
204 test_cmp expect actual
207 test_expect_success '--batch-check with %(rest)' '
208 echo "$type this is some extra content" >expect &&
209 echo "$oid this is some extra content" |
210 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
211 test_cmp expect actual
214 test -z "$content" ||
215 test_expect_success "--batch without type ($type)" '
217 echo "$size" &&
218 echo "$content"
219 } >expect &&
220 echo $oid | git cat-file --batch="%(objectsize)" >actual &&
221 test_cmp expect actual
224 test -z "$content" ||
225 test_expect_success "--batch without size ($type)" '
227 echo "$type" &&
228 echo "$content"
229 } >expect &&
230 echo $oid | git cat-file --batch="%(objecttype)" >actual &&
231 test_cmp expect actual
235 hello_content="Hello World"
236 hello_size=$(strlen "$hello_content")
237 hello_oid=$(echo_without_newline "$hello_content" | git hash-object --stdin)
239 test_expect_success "setup" '
240 git config core.repositoryformatversion 1 &&
241 git config extensions.objectformat $test_hash_algo &&
242 git config extensions.compatobjectformat $test_compat_hash_algo &&
243 echo_without_newline "$hello_content" > hello &&
244 git update-index --add hello
247 run_blob_tests () {
248 oid=$1
250 run_tests 'blob' $oid $hello_size "$hello_content" "$hello_content"
252 test_expect_success '--batch-command --buffer with flush for blob info' '
253 echo "$oid blob $hello_size" >expect &&
254 test_write_lines "info $oid" "flush" |
255 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
256 git cat-file --batch-command --buffer >actual &&
257 test_cmp expect actual
260 test_expect_success '--batch-command --buffer without flush for blob info' '
261 touch output &&
262 test_write_lines "info $oid" |
263 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
264 git cat-file --batch-command --buffer >>output &&
265 test_must_be_empty output
269 hello_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $hello_oid)
270 run_blob_tests $hello_oid
271 run_blob_tests $hello_compat_oid
273 test_expect_success '--batch-check without %(rest) considers whole line' '
274 echo "$hello_oid blob $hello_size" >expect &&
275 git update-index --add --cacheinfo 100644 $hello_oid "white space" &&
276 test_when_finished "git update-index --remove \"white space\"" &&
277 echo ":white space" | git cat-file --batch-check >actual &&
278 test_cmp expect actual
281 tree_oid=$(git write-tree)
282 tree_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $tree_oid)
283 tree_size=$(($(test_oid rawsz) + 13))
284 tree_compat_size=$(($(test_oid --hash=compat rawsz) + 13))
285 tree_pretty_content="100644 blob $hello_oid hello${LF}"
286 tree_compat_pretty_content="100644 blob $hello_compat_oid hello${LF}"
288 run_tests 'tree' $tree_oid $tree_size "" "$tree_pretty_content"
289 run_tests 'tree' $tree_compat_oid $tree_compat_size "" "$tree_compat_pretty_content"
291 commit_message="Initial commit"
292 commit_oid=$(echo_without_newline "$commit_message" | git commit-tree $tree_oid)
293 commit_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $commit_oid)
294 commit_size=$(($(test_oid hexsz) + 137))
295 commit_compat_size=$(($(test_oid --hash=compat hexsz) + 137))
296 commit_content="tree $tree_oid
297 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE
298 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
300 $commit_message"
302 commit_compat_content="tree $tree_compat_oid
303 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE
304 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
306 $commit_message"
308 run_tests 'commit' $commit_oid $commit_size "$commit_content" "$commit_content"
309 run_tests 'commit' $commit_compat_oid $commit_compat_size "$commit_compat_content" "$commit_compat_content"
311 tag_header_without_oid="type blob
312 tag hellotag
313 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
314 tag_header_without_timestamp="object $hello_oid
315 $tag_header_without_oid"
316 tag_compat_header_without_timestamp="object $hello_compat_oid
317 $tag_header_without_oid"
318 tag_description="This is a tag"
319 tag_content="$tag_header_without_timestamp 0 +0000
321 $tag_description"
322 tag_compat_content="$tag_compat_header_without_timestamp 0 +0000
324 $tag_description"
326 tag_oid=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
327 tag_size=$(strlen "$tag_content")
329 tag_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $tag_oid)
330 tag_compat_size=$(strlen "$tag_compat_content")
332 run_tests 'tag' $tag_oid $tag_size "$tag_content" "$tag_content"
333 run_tests 'tag' $tag_compat_oid $tag_compat_size "$tag_compat_content" "$tag_compat_content"
335 test_expect_success "Reach a blob from a tag pointing to it" '
336 echo_without_newline "$hello_content" >expect &&
337 git cat-file blob $tag_oid >actual &&
338 test_cmp expect actual
341 for oid in $hello_oid $hello_compat_oid
343 for batch in batch batch-check batch-command
345 for opt in t s e p
347 test_expect_success "Passing -$opt with --$batch fails" '
348 test_must_fail git cat-file --$batch -$opt $oid
351 test_expect_success "Passing --$batch with -$opt fails" '
352 test_must_fail git cat-file -$opt --$batch $oid
354 done
356 test_expect_success "Passing <type> with --$batch fails" '
357 test_must_fail git cat-file --$batch blob $oid
360 test_expect_success "Passing --$batch with <type> fails" '
361 test_must_fail git cat-file blob --$batch $oid
364 test_expect_success "Passing oid with --$batch fails" '
365 test_must_fail git cat-file --$batch $oid
367 done
368 done
370 for oid in $hello_oid $hello_compat_oid
372 for opt in t s e p
374 test_expect_success "Passing -$opt with --follow-symlinks fails" '
375 test_must_fail git cat-file --follow-symlinks -$opt $oid
377 done
378 done
380 test_expect_success "--batch-check for a non-existent named object" '
381 cat >expect <<-EOF &&
382 foobar42 missing
383 foobar84 missing
386 printf "foobar42\nfoobar84" >in &&
387 git cat-file --batch-check <in >actual &&
388 test_cmp expect actual
391 test_expect_success "--batch-check for a non-existent hash" '
392 cat >expect <<-EOF &&
393 0000000000000000000000000000000000000042 missing
394 0000000000000000000000000000000000000084 missing
397 printf "0000000000000000000000000000000000000042\n0000000000000000000000000000000000000084" >in &&
398 git cat-file --batch-check <in >actual &&
399 test_cmp expect actual
402 test_expect_success "--batch for an existent and a non-existent hash" '
403 cat >expect <<-EOF &&
404 $tag_oid tag $tag_size
405 $tag_content
406 0000000000000000000000000000000000000000 missing
409 printf "$tag_oid\n0000000000000000000000000000000000000000" >in &&
410 git cat-file --batch <in >actual &&
411 test_cmp expect actual
414 test_expect_success "--batch-check for an empty line" '
415 cat >expect <<-EOF &&
416 missing
419 echo >in &&
420 git cat-file --batch-check <in >actual &&
421 test_cmp expect actual
424 test_expect_success 'empty --batch-check notices missing object' '
425 echo "$ZERO_OID missing" >expect &&
426 echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
427 test_cmp expect actual
430 batch_tests () {
431 boid=$1
432 loid=$2
433 lsize=$3
434 coid=$4
435 csize=$5
436 ccontent=$6
437 toid=$7
438 tsize=$8
439 tcontent=$9
441 batch_input="$boid
442 $coid
443 $toid
444 deadbeef
448 printf "%s\0" \
449 "$boid blob $hello_size" \
450 "$hello_content" \
451 "$coid commit $csize" \
452 "$ccontent" \
453 "$toid tag $tsize" \
454 "$tcontent" \
455 "deadbeef missing" \
456 " missing" >batch_output
458 test_expect_success '--batch with multiple oids gives correct format' '
459 tr "\0" "\n" <batch_output >expect &&
460 echo_without_newline "$batch_input" >in &&
461 git cat-file --batch <in >actual &&
462 test_cmp expect actual
465 test_expect_success '--batch, -z with multiple oids gives correct format' '
466 echo_without_newline_nul "$batch_input" >in &&
467 tr "\0" "\n" <batch_output >expect &&
468 git cat-file --batch -z <in >actual &&
469 test_cmp expect actual
472 test_expect_success '--batch, -Z with multiple oids gives correct format' '
473 echo_without_newline_nul "$batch_input" >in &&
474 git cat-file --batch -Z <in >actual &&
475 test_cmp batch_output actual
478 batch_check_input="$boid
479 $loid
480 $coid
481 $toid
482 deadbeef
486 printf "%s\0" \
487 "$boid blob $hello_size" \
488 "$loid tree $lsize" \
489 "$coid commit $csize" \
490 "$toid tag $tsize" \
491 "deadbeef missing" \
492 " missing" >batch_check_output
494 test_expect_success "--batch-check with multiple oids gives correct format" '
495 tr "\0" "\n" <batch_check_output >expect &&
496 echo_without_newline "$batch_check_input" >in &&
497 git cat-file --batch-check <in >actual &&
498 test_cmp expect actual
501 test_expect_success "--batch-check, -z with multiple oids gives correct format" '
502 tr "\0" "\n" <batch_check_output >expect &&
503 echo_without_newline_nul "$batch_check_input" >in &&
504 git cat-file --batch-check -z <in >actual &&
505 test_cmp expect actual
508 test_expect_success "--batch-check, -Z with multiple oids gives correct format" '
509 echo_without_newline_nul "$batch_check_input" >in &&
510 git cat-file --batch-check -Z <in >actual &&
511 test_cmp batch_check_output actual
514 batch_command_multiple_info="info $boid
515 info $loid
516 info $coid
517 info $toid
518 info deadbeef"
520 test_expect_success '--batch-command with multiple info calls gives correct format' '
521 cat >expect <<-EOF &&
522 $boid blob $hello_size
523 $loid tree $lsize
524 $coid commit $csize
525 $toid tag $tsize
526 deadbeef missing
529 echo "$batch_command_multiple_info" >in &&
530 git cat-file --batch-command --buffer <in >actual &&
532 test_cmp expect actual &&
534 echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
535 git cat-file --batch-command --buffer -z <in >actual &&
537 test_cmp expect actual &&
539 echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
540 tr "\n" "\0" <expect >expect_nul &&
541 git cat-file --batch-command --buffer -Z <in >actual &&
543 test_cmp expect_nul actual
546 batch_command_multiple_contents="contents $boid
547 contents $coid
548 contents $toid
549 contents deadbeef
550 flush"
552 test_expect_success '--batch-command with multiple command calls gives correct format' '
553 printf "%s\0" \
554 "$boid blob $hello_size" \
555 "$hello_content" \
556 "$coid commit $csize" \
557 "$ccontent" \
558 "$toid tag $tsize" \
559 "$tcontent" \
560 "deadbeef missing" >expect_nul &&
561 tr "\0" "\n" <expect_nul >expect &&
563 echo "$batch_command_multiple_contents" >in &&
564 git cat-file --batch-command --buffer <in >actual &&
566 test_cmp expect actual &&
568 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
569 git cat-file --batch-command --buffer -z <in >actual &&
571 test_cmp expect actual &&
573 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
574 git cat-file --batch-command --buffer -Z <in >actual &&
576 test_cmp expect_nul actual
581 batch_tests $hello_oid $tree_oid $tree_size $commit_oid $commit_size "$commit_content" $tag_oid $tag_size "$tag_content"
582 batch_tests $hello_compat_oid $tree_compat_oid $tree_compat_size $commit_compat_oid $commit_compat_size "$commit_compat_content" $tag_compat_oid $tag_compat_size "$tag_compat_content"
585 test_expect_success FUNNYNAMES 'setup with newline in input' '
586 touch -- "newline${LF}embedded" &&
587 git add -- "newline${LF}embedded" &&
588 git commit -m "file with newline embedded" &&
589 test_tick &&
591 printf "HEAD:newline${LF}embedded" >in
594 test_expect_success FUNNYNAMES '--batch-check, -z with newline in input' '
595 git cat-file --batch-check -z <in >actual &&
596 echo "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
597 test_cmp expect actual
600 test_expect_success FUNNYNAMES '--batch-check, -Z with newline in input' '
601 git cat-file --batch-check -Z <in >actual &&
602 printf "%s\0" "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
603 test_cmp expect actual
606 test_expect_success 'setup blobs which are likely to delta' '
607 test-tool genrandom foo 10240 >foo &&
608 { cat foo && echo plus; } >foo-plus &&
609 git add foo foo-plus &&
610 git commit -m foo &&
611 cat >blobs <<-\EOF
612 HEAD:foo
613 HEAD:foo-plus
617 test_expect_success 'confirm that neither loose blob is a delta' '
618 cat >expect <<-EOF &&
619 $ZERO_OID
620 $ZERO_OID
622 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
623 test_cmp expect actual
626 # To avoid relying too much on the current delta heuristics,
627 # we will check only that one of the two objects is a delta
628 # against the other, but not the order. We can do so by just
629 # asking for the base of both, and checking whether either
630 # oid appears in the output.
631 test_expect_success '%(deltabase) reports packed delta bases' '
632 git repack -ad &&
633 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
635 grep "$(git rev-parse HEAD:foo)" actual ||
636 grep "$(git rev-parse HEAD:foo-plus)" actual
640 test_expect_success 'setup bogus data' '
641 bogus_short_type="bogus" &&
642 bogus_short_content="bogus" &&
643 bogus_short_size=$(strlen "$bogus_short_content") &&
644 bogus_short_oid=$(echo_without_newline "$bogus_short_content" | git hash-object -t $bogus_short_type --literally -w --stdin) &&
646 bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" &&
647 bogus_long_content="bogus" &&
648 bogus_long_size=$(strlen "$bogus_long_content") &&
649 bogus_long_oid=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin)
652 for arg1 in '' --allow-unknown-type
654 for arg2 in -s -t -p
656 if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p"
657 then
658 continue
662 test_expect_success "cat-file $arg1 $arg2 error on bogus short OID" '
663 cat >expect <<-\EOF &&
664 fatal: invalid object type
667 if test "$arg1" = "--allow-unknown-type"
668 then
669 git cat-file $arg1 $arg2 $bogus_short_oid
670 else
671 test_must_fail git cat-file $arg1 $arg2 $bogus_short_oid >out 2>actual &&
672 test_must_be_empty out &&
673 test_cmp expect actual
677 test_expect_success "cat-file $arg1 $arg2 error on bogus full OID" '
678 if test "$arg2" = "-p"
679 then
680 cat >expect <<-EOF
681 error: header for $bogus_long_oid too long, exceeds 32 bytes
682 fatal: Not a valid object name $bogus_long_oid
684 else
685 cat >expect <<-EOF
686 error: header for $bogus_long_oid too long, exceeds 32 bytes
687 fatal: git cat-file: could not get object info
689 fi &&
691 if test "$arg1" = "--allow-unknown-type"
692 then
693 git cat-file $arg1 $arg2 $bogus_short_oid
694 else
695 test_must_fail git cat-file $arg1 $arg2 $bogus_long_oid >out 2>actual &&
696 test_must_be_empty out &&
697 test_cmp expect actual
701 test_expect_success "cat-file $arg1 $arg2 error on missing short OID" '
702 cat >expect.err <<-EOF &&
703 fatal: Not a valid object name $(test_oid deadbeef_short)
705 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef_short) >out 2>err.actual &&
706 test_must_be_empty out &&
707 test_cmp expect.err err.actual
710 test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
711 if test "$arg2" = "-p"
712 then
713 cat >expect.err <<-EOF
714 fatal: Not a valid object name $(test_oid deadbeef)
716 else
717 cat >expect.err <<-\EOF
718 fatal: git cat-file: could not get object info
720 fi &&
721 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
722 test_must_be_empty out &&
723 test_cmp expect.err err.actual
725 done
726 done
728 test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
729 git cat-file -e $bogus_short_oid
732 test_expect_success '-e can not be combined with --allow-unknown-type' '
733 test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_oid
736 test_expect_success '-p cannot print a broken object even with --allow-unknown-type' '
737 test_must_fail git cat-file -p $bogus_short_oid &&
738 test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_oid
741 test_expect_success '<type> <hash> does not work with objects of broken types' '
742 cat >err.expect <<-\EOF &&
743 fatal: invalid object type "bogus"
745 test_must_fail git cat-file $bogus_short_type $bogus_short_oid 2>err.actual &&
746 test_cmp err.expect err.actual
749 test_expect_success 'broken types combined with --batch and --batch-check' '
750 echo $bogus_short_oid >bogus-oid &&
752 cat >err.expect <<-\EOF &&
753 fatal: invalid object type
756 test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
757 test_cmp err.expect err.actual &&
759 test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
760 test_cmp err.expect err.actual
763 test_expect_success 'the --batch and --batch-check options do not combine with --allow-unknown-type' '
764 test_expect_code 128 git cat-file --batch --allow-unknown-type <bogus-oid &&
765 test_expect_code 128 git cat-file --batch-check --allow-unknown-type <bogus-oid
768 test_expect_success 'the --allow-unknown-type option does not consider replacement refs' '
769 cat >expect <<-EOF &&
770 $bogus_short_type
772 git cat-file -t --allow-unknown-type $bogus_short_oid >actual &&
773 test_cmp expect actual &&
775 # Create it manually, as "git replace" will die on bogus
776 # types.
777 head=$(git rev-parse --verify HEAD) &&
778 test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_oid" &&
779 test-tool ref-store main update-ref msg "refs/replace/$bogus_short_oid" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
781 cat >expect <<-EOF &&
782 commit
784 git cat-file -t --allow-unknown-type $bogus_short_oid >actual &&
785 test_cmp expect actual
788 test_expect_success "Type of broken object is correct" '
789 echo $bogus_short_type >expect &&
790 git cat-file -t --allow-unknown-type $bogus_short_oid >actual &&
791 test_cmp expect actual
794 test_expect_success "Size of broken object is correct" '
795 echo $bogus_short_size >expect &&
796 git cat-file -s --allow-unknown-type $bogus_short_oid >actual &&
797 test_cmp expect actual
800 test_expect_success 'clean up broken object' '
801 rm .git/objects/$(test_oid_to_path $bogus_short_oid)
804 test_expect_success "Type of broken object is correct when type is large" '
805 echo $bogus_long_type >expect &&
806 git cat-file -t --allow-unknown-type $bogus_long_oid >actual &&
807 test_cmp expect actual
810 test_expect_success "Size of large broken object is correct when type is large" '
811 echo $bogus_long_size >expect &&
812 git cat-file -s --allow-unknown-type $bogus_long_oid >actual &&
813 test_cmp expect actual
816 test_expect_success 'clean up broken object' '
817 rm .git/objects/$(test_oid_to_path $bogus_long_oid)
820 test_expect_success 'cat-file -t and -s on corrupt loose object' '
821 git init --bare corrupt-loose.git &&
823 cd corrupt-loose.git &&
825 # Setup and create the empty blob and its path
826 empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
827 empty_blob=$(git hash-object -w --stdin </dev/null) &&
829 # Create another blob and its path
830 echo other >other.blob &&
831 other_blob=$(git hash-object -w --stdin <other.blob) &&
832 other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
834 # Before the swap the size is 0
835 cat >out.expect <<-EOF &&
838 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
839 test_must_be_empty err.actual &&
840 test_cmp out.expect out.actual &&
842 # Swap the two to corrupt the repository
843 mv -f "$other_path" "$empty_path" &&
844 test_must_fail git fsck 2>err.fsck &&
845 grep "hash-path mismatch" err.fsck &&
847 # confirm that cat-file is reading the new swapped-in
848 # blob...
849 cat >out.expect <<-EOF &&
850 blob
852 git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
853 test_must_be_empty err.actual &&
854 test_cmp out.expect out.actual &&
856 # ... since it has a different size now.
857 cat >out.expect <<-EOF &&
860 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
861 test_must_be_empty err.actual &&
862 test_cmp out.expect out.actual &&
864 # So far "cat-file" has been happy to spew the found
865 # content out as-is. Try to make it zlib-invalid.
866 mv -f other.blob "$empty_path" &&
867 test_must_fail git fsck 2>err.fsck &&
868 cat >expect <<-EOF &&
869 error: inflate: data stream error (incorrect header check)
870 error: unable to unpack header of ./$empty_path
871 error: $empty_blob: object corrupt or missing: ./$empty_path
873 grep "^error: " err.fsck >actual &&
874 test_cmp expect actual
878 # Tests for git cat-file --follow-symlinks
879 test_expect_success 'prep for symlink tests' '
880 echo_without_newline "$hello_content" >morx &&
881 test_ln_s_add morx same-dir-link &&
882 test_ln_s_add dir link-to-dir &&
883 test_ln_s_add ../fleem out-of-repo-link &&
884 test_ln_s_add .. out-of-repo-link-dir &&
885 test_ln_s_add same-dir-link link-to-link &&
886 test_ln_s_add nope broken-same-dir-link &&
887 mkdir dir &&
888 test_ln_s_add ../morx dir/parent-dir-link &&
889 test_ln_s_add .. dir/link-dir &&
890 test_ln_s_add ../../escape dir/out-of-repo-link &&
891 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
892 test_ln_s_add nope dir/broken-link-in-dir &&
893 mkdir dir/subdir &&
894 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
895 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
896 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
897 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
898 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
899 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
900 echo_without_newline "$hello_content" >dir/ind1 &&
901 test_ln_s_add dir dirlink &&
902 test_ln_s_add dir/subdir subdirlink &&
903 test_ln_s_add subdir/ind2 dir/link-to-child &&
904 test_ln_s_add dir/link-to-child link-to-down-link &&
905 test_ln_s_add dir/.. up-down &&
906 test_ln_s_add dir/../ up-down-trailing &&
907 test_ln_s_add dir/../morx up-down-file &&
908 test_ln_s_add dir/../../morx up-up-down-file &&
909 test_ln_s_add subdirlink/../../morx up-two-down-file &&
910 test_ln_s_add loop1 loop2 &&
911 test_ln_s_add loop2 loop1 &&
912 git add morx dir/subdir/ind2 dir/ind1 &&
913 git commit -am "test" &&
914 echo $hello_oid blob $hello_size >found
917 test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
918 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
919 test_cmp found actual &&
920 echo HEAD:nope missing >expect &&
921 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
922 test_cmp expect actual
925 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
926 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
927 test_cmp found actual
930 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
931 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
932 test_cmp found actual
936 test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
937 echo dangling 25 >expect &&
938 echo HEAD:broken-same-dir-link >>expect &&
939 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
940 test_cmp expect actual
943 test_expect_success 'git cat-file --batch-check --follow-symlinks -Z works for broken in-repo, same-dir links' '
944 printf "HEAD:broken-same-dir-link\0" >in &&
945 printf "dangling 25\0HEAD:broken-same-dir-link\0" >expect &&
946 git cat-file --batch-check --follow-symlinks -Z <in >actual &&
947 test_cmp expect actual
950 test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
951 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
952 test_cmp found actual
955 test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
956 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
957 test_cmp found actual &&
958 echo notdir 29 >expect &&
959 echo HEAD:dir/parent-dir-link/nope >>expect &&
960 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
961 test_cmp expect actual
964 test_expect_success 'git cat-file --batch-check --follow-symlinks -Z works for parent-dir links' '
965 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
966 test_cmp found actual &&
967 printf "notdir 29\0HEAD:dir/parent-dir-link/nope\0" >expect &&
968 printf "HEAD:dir/parent-dir-link/nope\0" >in &&
969 git cat-file --batch-check --follow-symlinks -Z <in >actual &&
970 test_cmp expect actual
973 test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
974 echo dangling 22 >expect &&
975 echo HEAD:dir/link-dir/nope >>expect &&
976 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
977 test_cmp expect actual &&
978 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
979 test_cmp found actual &&
980 echo dangling 27 >expect &&
981 echo HEAD:dir/broken-link-in-dir >>expect &&
982 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
983 test_cmp expect actual
986 test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
987 echo notdir 41 >expect &&
988 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
989 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
990 test_cmp expect actual &&
991 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
992 test_cmp found actual &&
993 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
994 test_cmp found actual
997 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
998 echo dangling 17 >expect &&
999 echo HEAD:dirlink/morx >>expect &&
1000 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
1001 test_cmp expect actual &&
1002 echo $hello_oid blob $hello_size >expect &&
1003 echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
1004 test_cmp expect actual
1007 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
1008 echo dangling 20 >expect &&
1009 echo HEAD:subdirlink/morx >>expect &&
1010 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
1011 test_cmp expect actual &&
1012 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
1013 test_cmp found actual
1016 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
1017 echo notdir 27 >expect &&
1018 echo HEAD:dir/link-to-child/morx >>expect &&
1019 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
1020 test_cmp expect actual &&
1021 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
1022 test_cmp found actual &&
1023 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
1024 test_cmp found actual
1027 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
1028 echo symlink 8 >expect &&
1029 echo ../fleem >>expect &&
1030 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
1031 test_cmp expect actual &&
1032 echo symlink 2 >expect &&
1033 echo .. >>expect &&
1034 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
1035 test_cmp expect actual
1038 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
1039 echo symlink 9 >expect &&
1040 echo ../escape >>expect &&
1041 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
1042 test_cmp expect actual &&
1043 echo symlink 2 >expect &&
1044 echo .. >>expect &&
1045 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
1046 test_cmp expect actual
1049 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
1050 echo symlink 15 >expect &&
1051 echo ../great-escape >>expect &&
1052 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
1053 test_cmp expect actual &&
1054 echo symlink 2 >expect &&
1055 echo .. >>expect &&
1056 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
1057 test_cmp expect actual &&
1058 echo symlink 3 >expect &&
1059 echo ../ >>expect &&
1060 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
1061 test_cmp expect actual
1064 test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
1065 echo HEAD: | git cat-file --batch-check >expect &&
1066 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
1067 test_cmp expect actual &&
1068 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
1069 test_cmp expect actual &&
1070 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
1071 test_cmp found actual &&
1072 echo symlink 7 >expect &&
1073 echo ../morx >>expect &&
1074 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
1075 test_cmp expect actual &&
1076 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
1077 test_cmp found actual
1080 test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
1081 echo loop 10 >expect &&
1082 echo HEAD:loop1 >>expect &&
1083 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
1084 test_cmp expect actual
1087 test_expect_success 'git cat-file --batch-check --follow-symlink -Z breaks loops' '
1088 printf "loop 10\0HEAD:loop1\0" >expect &&
1089 printf "HEAD:loop1\0" >in &&
1090 git cat-file --batch-check --follow-symlinks -Z <in >actual &&
1091 test_cmp expect actual
1094 test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
1095 echo HEAD:morx | git cat-file --batch >expect &&
1096 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
1097 test_cmp expect actual
1100 test_expect_success 'cat-file --batch-all-objects shows all objects' '
1101 # make new repos so we know the full set of objects; we will
1102 # also make sure that there are some packed and some loose
1103 # objects, some referenced and some not, some duplicates, and that
1104 # there are some available only via alternates.
1105 git init all-one &&
1107 cd all-one &&
1108 echo content >file &&
1109 git add file &&
1110 git commit -qm base &&
1111 git rev-parse HEAD HEAD^{tree} HEAD:file &&
1112 git repack -ad &&
1113 echo not-cloned | git hash-object -w --stdin
1114 ) >expect.unsorted &&
1115 git clone -s all-one all-two &&
1117 cd all-two &&
1118 echo local-unref | git hash-object -w --stdin
1119 ) >>expect.unsorted &&
1120 git -C all-two rev-parse HEAD:file |
1121 git -C all-two pack-objects .git/objects/pack/pack &&
1122 sort <expect.unsorted >expect &&
1123 git -C all-two cat-file --batch-all-objects \
1124 --batch-check="%(objectname)" >actual &&
1125 test_cmp expect actual
1128 # The only user-visible difference is that the objects are no longer sorted,
1129 # and the resulting sort order is undefined. So we can only check that it
1130 # produces the same objects as the ordered case, but that at least exercises
1131 # the code.
1132 test_expect_success 'cat-file --unordered works' '
1133 git -C all-two cat-file --batch-all-objects --unordered \
1134 --batch-check="%(objectname)" >actual.unsorted &&
1135 sort <actual.unsorted >actual &&
1136 test_cmp expect actual
1139 test_expect_success 'set up object list for --batch-all-objects tests' '
1140 git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects
1143 test_expect_success 'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
1144 git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
1145 git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
1146 cmp expect actual
1149 test_expect_success 'cat-file --batch="%(rest)" with --batch-all-objects will work' '
1150 git -C all-two cat-file --batch="%(rest)" <objects >expect &&
1151 git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
1152 cmp expect actual
1155 test_expect_success 'cat-file --batch="batman" with --batch-all-objects will work' '
1156 git -C all-two cat-file --batch="batman" <objects >expect &&
1157 git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
1158 cmp expect actual
1161 test_expect_success 'cat-file %(objectsize:disk) with --batch-all-objects' '
1162 # our state has both loose and packed objects,
1163 # so find both for our expected output
1165 find .git/objects/?? -type f |
1166 awk -F/ "{ print \$0, \$3\$4 }" |
1167 while read path oid
1169 size=$(test_file_size "$path") &&
1170 echo "$oid $size" ||
1171 return 1
1172 done &&
1173 rawsz=$(test_oid rawsz) &&
1174 find .git/objects/pack -name "*.idx" |
1175 while read idx
1177 git show-index <"$idx" >idx.raw &&
1178 sort -nr <idx.raw >idx.sorted &&
1179 packsz=$(test_file_size "${idx%.idx}.pack") &&
1180 end=$((packsz - rawsz)) &&
1181 while read start oid rest
1183 size=$((end - start)) &&
1184 end=$start &&
1185 echo "$oid $size" ||
1186 return 1
1187 done <idx.sorted ||
1188 return 1
1189 done
1190 } >expect.raw &&
1191 sort <expect.raw >expect &&
1192 git cat-file --batch-all-objects \
1193 --batch-check="%(objectname) %(objectsize:disk)" >actual &&
1194 test_cmp expect actual
1197 test_expect_success 'set up replacement object' '
1198 orig=$(git rev-parse HEAD) &&
1199 git cat-file commit $orig >orig &&
1201 cat orig &&
1202 echo extra
1203 } >fake &&
1204 fake=$(git hash-object -t commit -w fake) &&
1205 orig_size=$(git cat-file -s $orig) &&
1206 fake_size=$(git cat-file -s $fake) &&
1207 git replace $orig $fake
1210 test_expect_success 'cat-file --batch respects replace objects' '
1211 git cat-file --batch >actual <<-EOF &&
1212 $orig
1215 echo "$orig commit $fake_size" &&
1216 cat fake &&
1217 echo
1218 } >expect &&
1219 test_cmp expect actual
1222 test_expect_success 'cat-file --batch-check respects replace objects' '
1223 git cat-file --batch-check >actual <<-EOF &&
1224 $orig
1226 echo "$orig commit $fake_size" >expect &&
1227 test_cmp expect actual
1230 # Pull the entry for object with oid "$1" out of the output of
1231 # "cat-file --batch", including its object content (which requires
1232 # parsing and reading a set amount of bytes, hence perl).
1233 extract_batch_output () {
1234 perl -ne '
1235 BEGIN { $oid = shift }
1236 if (/^$oid \S+ (\d+)$/) {
1237 print;
1238 read STDIN, my $buf, $1;
1239 print $buf;
1240 print "\n";
1242 ' "$@"
1245 test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
1246 git cat-file --batch-all-objects --batch >actual.raw &&
1247 extract_batch_output $orig <actual.raw >actual &&
1249 echo "$orig commit $orig_size" &&
1250 cat orig &&
1251 echo
1252 } >expect &&
1253 test_cmp expect actual
1256 test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
1257 git cat-file --batch-all-objects --batch-check >actual.raw &&
1258 grep ^$orig actual.raw >actual &&
1259 echo "$orig commit $orig_size" >expect &&
1260 test_cmp expect actual
1262 test_expect_success 'batch-command empty command' '
1263 echo "" >cmd &&
1264 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1265 grep "^fatal:.*empty command in input.*" err
1268 test_expect_success 'batch-command whitespace before command' '
1269 echo " info deadbeef" >cmd &&
1270 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1271 grep "^fatal:.*whitespace before command.*" err
1274 test_expect_success 'batch-command unknown command' '
1275 echo unknown_command >cmd &&
1276 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1277 grep "^fatal:.*unknown command.*" err
1280 test_expect_success 'batch-command missing arguments' '
1281 echo "info" >cmd &&
1282 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1283 grep "^fatal:.*info requires arguments.*" err
1286 test_expect_success 'batch-command flush with arguments' '
1287 echo "flush arg" >cmd &&
1288 test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
1289 grep "^fatal:.*flush takes no arguments.*" err
1292 test_expect_success 'batch-command flush without --buffer' '
1293 echo "flush" >cmd &&
1294 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1295 grep "^fatal:.*flush is only for --buffer mode.*" err
1298 script='
1299 use warnings;
1300 use strict;
1301 use IPC::Open2;
1302 my ($opt, $oid, $expect, @pfx) = @ARGV;
1303 my @cmd = (qw(git cat-file), $opt);
1304 my $pid = open2(my $out, my $in, @cmd) or die "open2: @cmd";
1305 print $in @pfx, $oid, "\n" or die "print $!";
1306 my $rvec = "";
1307 vec($rvec, fileno($out), 1) = 1;
1308 select($rvec, undef, undef, 30) or die "no response to `@pfx $oid` from @cmd";
1309 my $info = <$out>;
1310 chop($info) eq "\n" or die "no LF";
1311 $info eq $expect or die "`$info` != `$expect`";
1312 close $in or die "close in $!";
1313 close $out or die "close out $!";
1314 waitpid $pid, 0;
1315 $? == 0 or die "\$?=$?";
1318 expect="$hello_oid blob $hello_size"
1320 test_expect_success PERL '--batch-check is unbuffered by default' '
1321 perl -e "$script" -- --batch-check $hello_oid "$expect"
1324 test_expect_success PERL '--batch-command info is unbuffered by default' '
1325 perl -e "$script" -- --batch-command $hello_oid "$expect" "info "
1328 test_done