Sync with 2.38.4
[git.git] / t / t1006-cat-file.sh
blob23b8942edba45c32822886ee93acef9b5474ea02
1 #!/bin/sh
3 test_description='git cat-file'
5 . ./test-lib.sh
7 test_cmdmode_usage () {
8 test_expect_code 129 "$@" 2>err &&
9 grep "^error:.*is incompatible with" err
12 for switches in \
13 '-e -p' \
14 '-p -t' \
15 '-t -s' \
16 '-s --textconv' \
17 '--textconv --filters' \
18 '--batch-all-objects -e'
20 test_expect_success "usage: cmdmode $switches" '
21 test_cmdmode_usage git cat-file $switches
23 done
25 test_incompatible_usage () {
26 test_expect_code 129 "$@" 2>err &&
27 grep -E "^(fatal|error):.*(requires|incompatible with|needs)" err
30 for opt in --batch --batch-check
32 test_expect_success "usage: incompatible options: --path with $opt" '
33 test_incompatible_usage git cat-file --path=foo $opt
35 done
37 test_missing_usage () {
38 test_expect_code 129 "$@" 2>err &&
39 grep -E "^fatal:.*required" err
42 short_modes="-e -p -t -s"
43 cw_modes="--textconv --filters"
45 for opt in $cw_modes
47 test_expect_success "usage: $opt requires another option" '
48 test_missing_usage git cat-file $opt
50 done
52 for opt in $short_modes
54 test_expect_success "usage: $opt requires another option" '
55 test_missing_usage git cat-file $opt
58 for opt2 in --batch \
59 --batch-check \
60 --follow-symlinks \
61 "--path=foo HEAD:some-path.txt"
63 test_expect_success "usage: incompatible options: $opt and $opt2" '
64 test_incompatible_usage git cat-file $opt $opt2
66 done
67 done
69 test_too_many_arguments () {
70 test_expect_code 129 "$@" 2>err &&
71 grep -E "^fatal: too many arguments$" err
74 for opt in $short_modes $cw_modes
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
81 for opt2 in --buffer --follow-symlinks
83 test_expect_success "usage: incompatible arguments: $opt with batch option $opt2" '
84 test_incompatible_usage git cat-file $opt $opt2
86 done
87 done
89 for opt in --buffer \
90 --follow-symlinks \
91 --batch-all-objects \
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
98 done
100 echo_without_newline () {
101 printf '%s' "$*"
104 echo_without_newline_nul () {
105 echo_without_newline "$@" | tr '\n' '\0'
108 strlen () {
109 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
112 maybe_remove_timestamp () {
113 if test -z "$2"; then
114 echo_without_newline "$1"
115 else
116 echo_without_newline "$(printf '%s\n' "$1" | remove_timestamp)"
120 remove_timestamp () {
121 sed -e 's/ [0-9][0-9]* [-+][0-9][0-9][0-9][0-9]$//'
125 run_tests () {
126 type=$1
127 sha1=$2
128 size=$3
129 content=$4
130 pretty_content=$5
131 no_ts=$6
133 batch_output="$sha1 $type $size
134 $content"
136 test_expect_success "$type exists" '
137 git cat-file -e $sha1
140 test_expect_success "Type of $type is correct" '
141 echo $type >expect &&
142 git cat-file -t $sha1 >actual &&
143 test_cmp expect actual
146 test_expect_success "Size of $type is correct" '
147 echo $size >expect &&
148 git cat-file -s $sha1 >actual &&
149 test_cmp expect actual
152 test_expect_success "Type of $type is correct using --allow-unknown-type" '
153 echo $type >expect &&
154 git cat-file -t --allow-unknown-type $sha1 >actual &&
155 test_cmp expect actual
158 test_expect_success "Size of $type is correct using --allow-unknown-type" '
159 echo $size >expect &&
160 git cat-file -s --allow-unknown-type $sha1 >actual &&
161 test_cmp expect actual
164 test -z "$content" ||
165 test_expect_success "Content of $type is correct" '
166 maybe_remove_timestamp "$content" $no_ts >expect &&
167 maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts >actual &&
168 test_cmp expect actual
171 test_expect_success "Pretty content of $type is correct" '
172 maybe_remove_timestamp "$pretty_content" $no_ts >expect &&
173 maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts >actual &&
174 test_cmp expect actual
177 test -z "$content" ||
178 test_expect_success "--batch output of $type is correct" '
179 maybe_remove_timestamp "$batch_output" $no_ts >expect &&
180 maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" $no_ts >actual &&
181 test_cmp expect actual
184 test_expect_success "--batch-check output of $type is correct" '
185 echo "$sha1 $type $size" >expect &&
186 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
187 test_cmp expect actual
190 for opt in --buffer --no-buffer
192 test -z "$content" ||
193 test_expect_success "--batch-command $opt output of $type content is correct" '
194 maybe_remove_timestamp "$batch_output" $no_ts >expect &&
195 maybe_remove_timestamp "$(test_write_lines "contents $sha1" |
196 git cat-file --batch-command $opt)" $no_ts >actual &&
197 test_cmp expect actual
200 test_expect_success "--batch-command $opt output of $type info is correct" '
201 echo "$sha1 $type $size" >expect &&
202 test_write_lines "info $sha1" |
203 git cat-file --batch-command $opt >actual &&
204 test_cmp expect actual
206 done
208 test_expect_success "custom --batch-check format" '
209 echo "$type $sha1" >expect &&
210 echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
211 test_cmp expect actual
214 test_expect_success "custom --batch-command format" '
215 echo "$type $sha1" >expect &&
216 echo "info $sha1" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
217 test_cmp expect actual
220 test_expect_success '--batch-check with %(rest)' '
221 echo "$type this is some extra content" >expect &&
222 echo "$sha1 this is some extra content" |
223 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
224 test_cmp expect actual
227 test -z "$content" ||
228 test_expect_success "--batch without type ($type)" '
230 echo "$size" &&
231 maybe_remove_timestamp "$content" $no_ts
232 } >expect &&
233 echo $sha1 | git cat-file --batch="%(objectsize)" >actual.full &&
234 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
235 test_cmp expect actual
238 test -z "$content" ||
239 test_expect_success "--batch without size ($type)" '
241 echo "$type" &&
242 maybe_remove_timestamp "$content" $no_ts
243 } >expect &&
244 echo $sha1 | git cat-file --batch="%(objecttype)" >actual.full &&
245 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
246 test_cmp expect actual
250 hello_content="Hello World"
251 hello_size=$(strlen "$hello_content")
252 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
254 test_expect_success "setup" '
255 echo_without_newline "$hello_content" > hello &&
256 git update-index --add hello
259 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
261 test_expect_success '--batch-command --buffer with flush for blob info' '
262 echo "$hello_sha1 blob $hello_size" >expect &&
263 test_write_lines "info $hello_sha1" "flush" |
264 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
265 git cat-file --batch-command --buffer >actual &&
266 test_cmp expect actual
269 test_expect_success '--batch-command --buffer without flush for blob info' '
270 touch output &&
271 test_write_lines "info $hello_sha1" |
272 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
273 git cat-file --batch-command --buffer >>output &&
274 test_must_be_empty output
277 test_expect_success '--batch-check without %(rest) considers whole line' '
278 echo "$hello_sha1 blob $hello_size" >expect &&
279 git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
280 test_when_finished "git update-index --remove \"white space\"" &&
281 echo ":white space" | git cat-file --batch-check >actual &&
282 test_cmp expect actual
285 tree_sha1=$(git write-tree)
286 tree_size=$(($(test_oid rawsz) + 13))
287 tree_pretty_content="100644 blob $hello_sha1 hello"
289 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
291 commit_message="Initial commit"
292 commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
293 commit_size=$(($(test_oid hexsz) + 137))
294 commit_content="tree $tree_sha1
295 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
296 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
298 $commit_message"
300 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
302 tag_header_without_timestamp="object $hello_sha1
303 type blob
304 tag hellotag
305 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
306 tag_description="This is a tag"
307 tag_content="$tag_header_without_timestamp 0000000000 +0000
309 $tag_description"
311 tag_sha1=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
312 tag_size=$(strlen "$tag_content")
314 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
316 test_expect_success \
317 "Reach a blob from a tag pointing to it" \
318 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
320 for batch in batch batch-check batch-command
322 for opt in t s e p
324 test_expect_success "Passing -$opt with --$batch fails" '
325 test_must_fail git cat-file --$batch -$opt $hello_sha1
328 test_expect_success "Passing --$batch with -$opt fails" '
329 test_must_fail git cat-file -$opt --$batch $hello_sha1
331 done
333 test_expect_success "Passing <type> with --$batch fails" '
334 test_must_fail git cat-file --$batch blob $hello_sha1
337 test_expect_success "Passing --$batch with <type> fails" '
338 test_must_fail git cat-file blob --$batch $hello_sha1
341 test_expect_success "Passing sha1 with --$batch fails" '
342 test_must_fail git cat-file --$batch $hello_sha1
344 done
346 for opt in t s e p
348 test_expect_success "Passing -$opt with --follow-symlinks fails" '
349 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
351 done
353 test_expect_success "--batch-check for a non-existent named object" '
354 test "foobar42 missing
355 foobar84 missing" = \
356 "$( ( echo foobar42 && echo_without_newline foobar84 ) | git cat-file --batch-check)"
359 test_expect_success "--batch-check for a non-existent hash" '
360 test "0000000000000000000000000000000000000042 missing
361 0000000000000000000000000000000000000084 missing" = \
362 "$( ( echo 0000000000000000000000000000000000000042 &&
363 echo_without_newline 0000000000000000000000000000000000000084 ) |
364 git cat-file --batch-check)"
367 test_expect_success "--batch for an existent and a non-existent hash" '
368 test "$tag_sha1 tag $tag_size
369 $tag_content
370 0000000000000000000000000000000000000000 missing" = \
371 "$( ( echo $tag_sha1 &&
372 echo_without_newline 0000000000000000000000000000000000000000 ) |
373 git cat-file --batch)"
376 test_expect_success "--batch-check for an empty line" '
377 test " missing" = "$(echo | git cat-file --batch-check)"
380 test_expect_success 'empty --batch-check notices missing object' '
381 echo "$ZERO_OID missing" >expect &&
382 echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
383 test_cmp expect actual
386 batch_input="$hello_sha1
387 $commit_sha1
388 $tag_sha1
389 deadbeef
393 batch_output="$hello_sha1 blob $hello_size
394 $hello_content
395 $commit_sha1 commit $commit_size
396 $commit_content
397 $tag_sha1 tag $tag_size
398 $tag_content
399 deadbeef missing
400 missing"
402 test_expect_success '--batch with multiple sha1s gives correct format' '
403 test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
406 test_expect_success '--batch, -z with multiple sha1s gives correct format' '
407 echo_without_newline_nul "$batch_input" >in &&
408 test "$(maybe_remove_timestamp "$batch_output" 1)" = \
409 "$(maybe_remove_timestamp "$(git cat-file --batch -z <in)" 1)"
412 batch_check_input="$hello_sha1
413 $tree_sha1
414 $commit_sha1
415 $tag_sha1
416 deadbeef
420 batch_check_output="$hello_sha1 blob $hello_size
421 $tree_sha1 tree $tree_size
422 $commit_sha1 commit $commit_size
423 $tag_sha1 tag $tag_size
424 deadbeef missing
425 missing"
427 test_expect_success "--batch-check with multiple sha1s gives correct format" '
428 test "$batch_check_output" = \
429 "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
432 test_expect_success "--batch-check, -z with multiple sha1s gives correct format" '
433 echo_without_newline_nul "$batch_check_input" >in &&
434 test "$batch_check_output" = "$(git cat-file --batch-check -z <in)"
437 test_expect_success FUNNYNAMES '--batch-check, -z with newline in input' '
438 touch -- "newline${LF}embedded" &&
439 git add -- "newline${LF}embedded" &&
440 git commit -m "file with newline embedded" &&
441 test_tick &&
443 printf "HEAD:newline${LF}embedded" >in &&
444 git cat-file --batch-check -z <in >actual &&
446 echo "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
447 test_cmp expect actual
450 batch_command_multiple_info="info $hello_sha1
451 info $tree_sha1
452 info $commit_sha1
453 info $tag_sha1
454 info deadbeef"
456 test_expect_success '--batch-command with multiple info calls gives correct format' '
457 cat >expect <<-EOF &&
458 $hello_sha1 blob $hello_size
459 $tree_sha1 tree $tree_size
460 $commit_sha1 commit $commit_size
461 $tag_sha1 tag $tag_size
462 deadbeef missing
465 echo "$batch_command_multiple_info" >in &&
466 git cat-file --batch-command --buffer <in >actual &&
468 test_cmp expect actual &&
470 echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
471 git cat-file --batch-command --buffer -z <in >actual &&
473 test_cmp expect actual
476 batch_command_multiple_contents="contents $hello_sha1
477 contents $commit_sha1
478 contents $tag_sha1
479 contents deadbeef
480 flush"
482 test_expect_success '--batch-command with multiple command calls gives correct format' '
483 remove_timestamp >expect <<-EOF &&
484 $hello_sha1 blob $hello_size
485 $hello_content
486 $commit_sha1 commit $commit_size
487 $commit_content
488 $tag_sha1 tag $tag_size
489 $tag_content
490 deadbeef missing
493 echo "$batch_command_multiple_contents" >in &&
494 git cat-file --batch-command --buffer <in >actual_raw &&
496 remove_timestamp <actual_raw >actual &&
497 test_cmp expect actual &&
499 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
500 git cat-file --batch-command --buffer -z <in >actual_raw &&
502 remove_timestamp <actual_raw >actual &&
503 test_cmp expect actual
506 test_expect_success 'setup blobs which are likely to delta' '
507 test-tool genrandom foo 10240 >foo &&
508 { cat foo && echo plus; } >foo-plus &&
509 git add foo foo-plus &&
510 git commit -m foo &&
511 cat >blobs <<-\EOF
512 HEAD:foo
513 HEAD:foo-plus
517 test_expect_success 'confirm that neither loose blob is a delta' '
518 cat >expect <<-EOF &&
519 $ZERO_OID
520 $ZERO_OID
522 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
523 test_cmp expect actual
526 # To avoid relying too much on the current delta heuristics,
527 # we will check only that one of the two objects is a delta
528 # against the other, but not the order. We can do so by just
529 # asking for the base of both, and checking whether either
530 # sha1 appears in the output.
531 test_expect_success '%(deltabase) reports packed delta bases' '
532 git repack -ad &&
533 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
535 grep "$(git rev-parse HEAD:foo)" actual ||
536 grep "$(git rev-parse HEAD:foo-plus)" actual
540 test_expect_success 'setup bogus data' '
541 bogus_short_type="bogus" &&
542 bogus_short_content="bogus" &&
543 bogus_short_size=$(strlen "$bogus_short_content") &&
544 bogus_short_sha1=$(echo_without_newline "$bogus_short_content" | git hash-object -t $bogus_short_type --literally -w --stdin) &&
546 bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" &&
547 bogus_long_content="bogus" &&
548 bogus_long_size=$(strlen "$bogus_long_content") &&
549 bogus_long_sha1=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin)
552 for arg1 in '' --allow-unknown-type
554 for arg2 in -s -t -p
556 if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p"
557 then
558 continue
562 test_expect_success "cat-file $arg1 $arg2 error on bogus short OID" '
563 cat >expect <<-\EOF &&
564 fatal: invalid object type
567 if test "$arg1" = "--allow-unknown-type"
568 then
569 git cat-file $arg1 $arg2 $bogus_short_sha1
570 else
571 test_must_fail git cat-file $arg1 $arg2 $bogus_short_sha1 >out 2>actual &&
572 test_must_be_empty out &&
573 test_cmp expect actual
577 test_expect_success "cat-file $arg1 $arg2 error on bogus full OID" '
578 if test "$arg2" = "-p"
579 then
580 cat >expect <<-EOF
581 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
582 fatal: Not a valid object name $bogus_long_sha1
584 else
585 cat >expect <<-EOF
586 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
587 fatal: git cat-file: could not get object info
589 fi &&
591 if test "$arg1" = "--allow-unknown-type"
592 then
593 git cat-file $arg1 $arg2 $bogus_short_sha1
594 else
595 test_must_fail git cat-file $arg1 $arg2 $bogus_long_sha1 >out 2>actual &&
596 test_must_be_empty out &&
597 test_cmp expect actual
601 test_expect_success "cat-file $arg1 $arg2 error on missing short OID" '
602 cat >expect.err <<-EOF &&
603 fatal: Not a valid object name $(test_oid deadbeef_short)
605 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef_short) >out 2>err.actual &&
606 test_must_be_empty out
609 test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
610 if test "$arg2" = "-p"
611 then
612 cat >expect.err <<-EOF
613 fatal: Not a valid object name $(test_oid deadbeef)
615 else
616 cat >expect.err <<-\EOF
617 fatal: git cat-file: could not get object info
619 fi &&
620 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
621 test_must_be_empty out &&
622 test_cmp expect.err err.actual
624 done
625 done
627 test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
628 git cat-file -e $bogus_short_sha1
631 test_expect_success '-e can not be combined with --allow-unknown-type' '
632 test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_sha1
635 test_expect_success '-p cannot print a broken object even with --allow-unknown-type' '
636 test_must_fail git cat-file -p $bogus_short_sha1 &&
637 test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_sha1
640 test_expect_success '<type> <hash> does not work with objects of broken types' '
641 cat >err.expect <<-\EOF &&
642 fatal: invalid object type "bogus"
644 test_must_fail git cat-file $bogus_short_type $bogus_short_sha1 2>err.actual &&
645 test_cmp err.expect err.actual
648 test_expect_success 'broken types combined with --batch and --batch-check' '
649 echo $bogus_short_sha1 >bogus-oid &&
651 cat >err.expect <<-\EOF &&
652 fatal: invalid object type
655 test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
656 test_cmp err.expect err.actual &&
658 test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
659 test_cmp err.expect err.actual
662 test_expect_success 'the --batch and --batch-check options do not combine with --allow-unknown-type' '
663 test_expect_code 128 git cat-file --batch --allow-unknown-type <bogus-oid &&
664 test_expect_code 128 git cat-file --batch-check --allow-unknown-type <bogus-oid
667 test_expect_success 'the --allow-unknown-type option does not consider replacement refs' '
668 cat >expect <<-EOF &&
669 $bogus_short_type
671 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
672 test_cmp expect actual &&
674 # Create it manually, as "git replace" will die on bogus
675 # types.
676 head=$(git rev-parse --verify HEAD) &&
677 test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_sha1" &&
678 test-tool ref-store main update-ref msg "refs/replace/$bogus_short_sha1" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
680 cat >expect <<-EOF &&
681 commit
683 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
684 test_cmp expect actual
687 test_expect_success "Type of broken object is correct" '
688 echo $bogus_short_type >expect &&
689 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
690 test_cmp expect actual
693 test_expect_success "Size of broken object is correct" '
694 echo $bogus_short_size >expect &&
695 git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual &&
696 test_cmp expect actual
699 test_expect_success 'clean up broken object' '
700 rm .git/objects/$(test_oid_to_path $bogus_short_sha1)
703 test_expect_success "Type of broken object is correct when type is large" '
704 echo $bogus_long_type >expect &&
705 git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual &&
706 test_cmp expect actual
709 test_expect_success "Size of large broken object is correct when type is large" '
710 echo $bogus_long_size >expect &&
711 git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual &&
712 test_cmp expect actual
715 test_expect_success 'clean up broken object' '
716 rm .git/objects/$(test_oid_to_path $bogus_long_sha1)
719 test_expect_success 'cat-file -t and -s on corrupt loose object' '
720 git init --bare corrupt-loose.git &&
722 cd corrupt-loose.git &&
724 # Setup and create the empty blob and its path
725 empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
726 empty_blob=$(git hash-object -w --stdin </dev/null) &&
728 # Create another blob and its path
729 echo other >other.blob &&
730 other_blob=$(git hash-object -w --stdin <other.blob) &&
731 other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
733 # Before the swap the size is 0
734 cat >out.expect <<-EOF &&
737 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
738 test_must_be_empty err.actual &&
739 test_cmp out.expect out.actual &&
741 # Swap the two to corrupt the repository
742 mv -f "$other_path" "$empty_path" &&
743 test_must_fail git fsck 2>err.fsck &&
744 grep "hash-path mismatch" err.fsck &&
746 # confirm that cat-file is reading the new swapped-in
747 # blob...
748 cat >out.expect <<-EOF &&
749 blob
751 git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
752 test_must_be_empty err.actual &&
753 test_cmp out.expect out.actual &&
755 # ... since it has a different size now.
756 cat >out.expect <<-EOF &&
759 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
760 test_must_be_empty err.actual &&
761 test_cmp out.expect out.actual &&
763 # So far "cat-file" has been happy to spew the found
764 # content out as-is. Try to make it zlib-invalid.
765 mv -f other.blob "$empty_path" &&
766 test_must_fail git fsck 2>err.fsck &&
767 cat >expect <<-EOF &&
768 error: inflate: data stream error (incorrect header check)
769 error: unable to unpack header of ./$empty_path
770 error: $empty_blob: object corrupt or missing: ./$empty_path
772 grep "^error: " err.fsck >actual &&
773 test_cmp expect actual
777 # Tests for git cat-file --follow-symlinks
778 test_expect_success 'prep for symlink tests' '
779 echo_without_newline "$hello_content" >morx &&
780 test_ln_s_add morx same-dir-link &&
781 test_ln_s_add dir link-to-dir &&
782 test_ln_s_add ../fleem out-of-repo-link &&
783 test_ln_s_add .. out-of-repo-link-dir &&
784 test_ln_s_add same-dir-link link-to-link &&
785 test_ln_s_add nope broken-same-dir-link &&
786 mkdir dir &&
787 test_ln_s_add ../morx dir/parent-dir-link &&
788 test_ln_s_add .. dir/link-dir &&
789 test_ln_s_add ../../escape dir/out-of-repo-link &&
790 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
791 test_ln_s_add nope dir/broken-link-in-dir &&
792 mkdir dir/subdir &&
793 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
794 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
795 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
796 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
797 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
798 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
799 echo_without_newline "$hello_content" >dir/ind1 &&
800 test_ln_s_add dir dirlink &&
801 test_ln_s_add dir/subdir subdirlink &&
802 test_ln_s_add subdir/ind2 dir/link-to-child &&
803 test_ln_s_add dir/link-to-child link-to-down-link &&
804 test_ln_s_add dir/.. up-down &&
805 test_ln_s_add dir/../ up-down-trailing &&
806 test_ln_s_add dir/../morx up-down-file &&
807 test_ln_s_add dir/../../morx up-up-down-file &&
808 test_ln_s_add subdirlink/../../morx up-two-down-file &&
809 test_ln_s_add loop1 loop2 &&
810 test_ln_s_add loop2 loop1 &&
811 git add morx dir/subdir/ind2 dir/ind1 &&
812 git commit -am "test" &&
813 echo $hello_sha1 blob $hello_size >found
816 test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
817 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
818 test_cmp found actual &&
819 echo HEAD:nope missing >expect &&
820 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
821 test_cmp expect actual
824 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
825 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
826 test_cmp found actual
829 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
830 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
831 test_cmp found actual
835 test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
836 echo dangling 25 >expect &&
837 echo HEAD:broken-same-dir-link >>expect &&
838 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
839 test_cmp expect actual
842 test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
843 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
844 test_cmp found actual
847 test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
848 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
849 test_cmp found actual &&
850 echo notdir 29 >expect &&
851 echo HEAD:dir/parent-dir-link/nope >>expect &&
852 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
853 test_cmp expect actual
856 test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
857 echo dangling 22 >expect &&
858 echo HEAD:dir/link-dir/nope >>expect &&
859 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
860 test_cmp expect actual &&
861 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
862 test_cmp found actual &&
863 echo dangling 27 >expect &&
864 echo HEAD:dir/broken-link-in-dir >>expect &&
865 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
866 test_cmp expect actual
869 test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
870 echo notdir 41 >expect &&
871 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
872 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
873 test_cmp expect actual &&
874 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
875 test_cmp found actual &&
876 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
877 test_cmp found actual
880 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
881 echo dangling 17 >expect &&
882 echo HEAD:dirlink/morx >>expect &&
883 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
884 test_cmp expect actual &&
885 echo $hello_sha1 blob $hello_size >expect &&
886 echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
887 test_cmp expect actual
890 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
891 echo dangling 20 >expect &&
892 echo HEAD:subdirlink/morx >>expect &&
893 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
894 test_cmp expect actual &&
895 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
896 test_cmp found actual
899 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
900 echo notdir 27 >expect &&
901 echo HEAD:dir/link-to-child/morx >>expect &&
902 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
903 test_cmp expect actual &&
904 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
905 test_cmp found actual &&
906 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
907 test_cmp found actual
910 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
911 echo symlink 8 >expect &&
912 echo ../fleem >>expect &&
913 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
914 test_cmp expect actual &&
915 echo symlink 2 >expect &&
916 echo .. >>expect &&
917 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
918 test_cmp expect actual
921 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
922 echo symlink 9 >expect &&
923 echo ../escape >>expect &&
924 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
925 test_cmp expect actual &&
926 echo symlink 2 >expect &&
927 echo .. >>expect &&
928 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
929 test_cmp expect actual
932 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
933 echo symlink 15 >expect &&
934 echo ../great-escape >>expect &&
935 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
936 test_cmp expect actual &&
937 echo symlink 2 >expect &&
938 echo .. >>expect &&
939 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
940 test_cmp expect actual &&
941 echo symlink 3 >expect &&
942 echo ../ >>expect &&
943 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
944 test_cmp expect actual
947 test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
948 echo HEAD: | git cat-file --batch-check >expect &&
949 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
950 test_cmp expect actual &&
951 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
952 test_cmp expect actual &&
953 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
954 test_cmp found actual &&
955 echo symlink 7 >expect &&
956 echo ../morx >>expect &&
957 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
958 test_cmp expect actual &&
959 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
960 test_cmp found actual
963 test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
964 echo loop 10 >expect &&
965 echo HEAD:loop1 >>expect &&
966 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
967 test_cmp expect actual
970 test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
971 echo HEAD:morx | git cat-file --batch >expect &&
972 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
973 test_cmp expect actual
976 test_expect_success 'cat-file --batch-all-objects shows all objects' '
977 # make new repos so we know the full set of objects; we will
978 # also make sure that there are some packed and some loose
979 # objects, some referenced and some not, some duplicates, and that
980 # there are some available only via alternates.
981 git init all-one &&
983 cd all-one &&
984 echo content >file &&
985 git add file &&
986 git commit -qm base &&
987 git rev-parse HEAD HEAD^{tree} HEAD:file &&
988 git repack -ad &&
989 echo not-cloned | git hash-object -w --stdin
990 ) >expect.unsorted &&
991 git clone -s all-one all-two &&
993 cd all-two &&
994 echo local-unref | git hash-object -w --stdin
995 ) >>expect.unsorted &&
996 git -C all-two rev-parse HEAD:file |
997 git -C all-two pack-objects .git/objects/pack/pack &&
998 sort <expect.unsorted >expect &&
999 git -C all-two cat-file --batch-all-objects \
1000 --batch-check="%(objectname)" >actual &&
1001 test_cmp expect actual
1004 # The only user-visible difference is that the objects are no longer sorted,
1005 # and the resulting sort order is undefined. So we can only check that it
1006 # produces the same objects as the ordered case, but that at least exercises
1007 # the code.
1008 test_expect_success 'cat-file --unordered works' '
1009 git -C all-two cat-file --batch-all-objects --unordered \
1010 --batch-check="%(objectname)" >actual.unsorted &&
1011 sort <actual.unsorted >actual &&
1012 test_cmp expect actual
1015 test_expect_success 'set up object list for --batch-all-objects tests' '
1016 git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects
1019 test_expect_success 'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
1020 git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
1021 git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
1022 cmp expect actual
1025 test_expect_success 'cat-file --batch="%(rest)" with --batch-all-objects will work' '
1026 git -C all-two cat-file --batch="%(rest)" <objects >expect &&
1027 git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
1028 cmp expect actual
1031 test_expect_success 'cat-file --batch="batman" with --batch-all-objects will work' '
1032 git -C all-two cat-file --batch="batman" <objects >expect &&
1033 git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
1034 cmp expect actual
1037 test_expect_success 'set up replacement object' '
1038 orig=$(git rev-parse HEAD) &&
1039 git cat-file commit $orig >orig &&
1041 cat orig &&
1042 echo extra
1043 } >fake &&
1044 fake=$(git hash-object -t commit -w fake) &&
1045 orig_size=$(git cat-file -s $orig) &&
1046 fake_size=$(git cat-file -s $fake) &&
1047 git replace $orig $fake
1050 test_expect_success 'cat-file --batch respects replace objects' '
1051 git cat-file --batch >actual <<-EOF &&
1052 $orig
1055 echo "$orig commit $fake_size" &&
1056 cat fake &&
1057 echo
1058 } >expect &&
1059 test_cmp expect actual
1062 test_expect_success 'cat-file --batch-check respects replace objects' '
1063 git cat-file --batch-check >actual <<-EOF &&
1064 $orig
1066 echo "$orig commit $fake_size" >expect &&
1067 test_cmp expect actual
1070 # Pull the entry for object with oid "$1" out of the output of
1071 # "cat-file --batch", including its object content (which requires
1072 # parsing and reading a set amount of bytes, hence perl).
1073 extract_batch_output () {
1074 perl -ne '
1075 BEGIN { $oid = shift }
1076 if (/^$oid \S+ (\d+)$/) {
1077 print;
1078 read STDIN, my $buf, $1;
1079 print $buf;
1080 print "\n";
1082 ' "$@"
1085 test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
1086 git cat-file --batch-all-objects --batch >actual.raw &&
1087 extract_batch_output $orig <actual.raw >actual &&
1089 echo "$orig commit $orig_size" &&
1090 cat orig &&
1091 echo
1092 } >expect &&
1093 test_cmp expect actual
1096 test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
1097 git cat-file --batch-all-objects --batch-check >actual.raw &&
1098 grep ^$orig actual.raw >actual &&
1099 echo "$orig commit $orig_size" >expect &&
1100 test_cmp expect actual
1102 test_expect_success 'batch-command empty command' '
1103 echo "" >cmd &&
1104 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1105 grep "^fatal:.*empty command in input.*" err
1108 test_expect_success 'batch-command whitespace before command' '
1109 echo " info deadbeef" >cmd &&
1110 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1111 grep "^fatal:.*whitespace before command.*" err
1114 test_expect_success 'batch-command unknown command' '
1115 echo unknown_command >cmd &&
1116 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1117 grep "^fatal:.*unknown command.*" err
1120 test_expect_success 'batch-command missing arguments' '
1121 echo "info" >cmd &&
1122 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1123 grep "^fatal:.*info requires arguments.*" err
1126 test_expect_success 'batch-command flush with arguments' '
1127 echo "flush arg" >cmd &&
1128 test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
1129 grep "^fatal:.*flush takes no arguments.*" err
1132 test_expect_success 'batch-command flush without --buffer' '
1133 echo "flush" >cmd &&
1134 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1135 grep "^fatal:.*flush is only for --buffer mode.*" err
1138 test_done