var: avoid a segmentation fault when `HOME` is unset
[git.git] / t / t1006-cat-file.sh
blob8eac74b59c2faa9c6e9f2396214264861c8416c1
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> 0 +0000
296 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0 +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 0 +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 &&
607 test_cmp expect.err err.actual
610 test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
611 if test "$arg2" = "-p"
612 then
613 cat >expect.err <<-EOF
614 fatal: Not a valid object name $(test_oid deadbeef)
616 else
617 cat >expect.err <<-\EOF
618 fatal: git cat-file: could not get object info
620 fi &&
621 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
622 test_must_be_empty out &&
623 test_cmp expect.err err.actual
625 done
626 done
628 test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
629 git cat-file -e $bogus_short_sha1
632 test_expect_success '-e can not be combined with --allow-unknown-type' '
633 test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_sha1
636 test_expect_success '-p cannot print a broken object even with --allow-unknown-type' '
637 test_must_fail git cat-file -p $bogus_short_sha1 &&
638 test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_sha1
641 test_expect_success '<type> <hash> does not work with objects of broken types' '
642 cat >err.expect <<-\EOF &&
643 fatal: invalid object type "bogus"
645 test_must_fail git cat-file $bogus_short_type $bogus_short_sha1 2>err.actual &&
646 test_cmp err.expect err.actual
649 test_expect_success 'broken types combined with --batch and --batch-check' '
650 echo $bogus_short_sha1 >bogus-oid &&
652 cat >err.expect <<-\EOF &&
653 fatal: invalid object type
656 test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
657 test_cmp err.expect err.actual &&
659 test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
660 test_cmp err.expect err.actual
663 test_expect_success 'the --batch and --batch-check options do not combine with --allow-unknown-type' '
664 test_expect_code 128 git cat-file --batch --allow-unknown-type <bogus-oid &&
665 test_expect_code 128 git cat-file --batch-check --allow-unknown-type <bogus-oid
668 test_expect_success 'the --allow-unknown-type option does not consider replacement refs' '
669 cat >expect <<-EOF &&
670 $bogus_short_type
672 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
673 test_cmp expect actual &&
675 # Create it manually, as "git replace" will die on bogus
676 # types.
677 head=$(git rev-parse --verify HEAD) &&
678 test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_sha1" &&
679 test-tool ref-store main update-ref msg "refs/replace/$bogus_short_sha1" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
681 cat >expect <<-EOF &&
682 commit
684 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
685 test_cmp expect actual
688 test_expect_success "Type of broken object is correct" '
689 echo $bogus_short_type >expect &&
690 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
691 test_cmp expect actual
694 test_expect_success "Size of broken object is correct" '
695 echo $bogus_short_size >expect &&
696 git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual &&
697 test_cmp expect actual
700 test_expect_success 'clean up broken object' '
701 rm .git/objects/$(test_oid_to_path $bogus_short_sha1)
704 test_expect_success "Type of broken object is correct when type is large" '
705 echo $bogus_long_type >expect &&
706 git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual &&
707 test_cmp expect actual
710 test_expect_success "Size of large broken object is correct when type is large" '
711 echo $bogus_long_size >expect &&
712 git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual &&
713 test_cmp expect actual
716 test_expect_success 'clean up broken object' '
717 rm .git/objects/$(test_oid_to_path $bogus_long_sha1)
720 test_expect_success 'cat-file -t and -s on corrupt loose object' '
721 git init --bare corrupt-loose.git &&
723 cd corrupt-loose.git &&
725 # Setup and create the empty blob and its path
726 empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
727 empty_blob=$(git hash-object -w --stdin </dev/null) &&
729 # Create another blob and its path
730 echo other >other.blob &&
731 other_blob=$(git hash-object -w --stdin <other.blob) &&
732 other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
734 # Before the swap the size is 0
735 cat >out.expect <<-EOF &&
738 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
739 test_must_be_empty err.actual &&
740 test_cmp out.expect out.actual &&
742 # Swap the two to corrupt the repository
743 mv -f "$other_path" "$empty_path" &&
744 test_must_fail git fsck 2>err.fsck &&
745 grep "hash-path mismatch" err.fsck &&
747 # confirm that cat-file is reading the new swapped-in
748 # blob...
749 cat >out.expect <<-EOF &&
750 blob
752 git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
753 test_must_be_empty err.actual &&
754 test_cmp out.expect out.actual &&
756 # ... since it has a different size now.
757 cat >out.expect <<-EOF &&
760 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
761 test_must_be_empty err.actual &&
762 test_cmp out.expect out.actual &&
764 # So far "cat-file" has been happy to spew the found
765 # content out as-is. Try to make it zlib-invalid.
766 mv -f other.blob "$empty_path" &&
767 test_must_fail git fsck 2>err.fsck &&
768 cat >expect <<-EOF &&
769 error: inflate: data stream error (incorrect header check)
770 error: unable to unpack header of ./$empty_path
771 error: $empty_blob: object corrupt or missing: ./$empty_path
773 grep "^error: " err.fsck >actual &&
774 test_cmp expect actual
778 # Tests for git cat-file --follow-symlinks
779 test_expect_success 'prep for symlink tests' '
780 echo_without_newline "$hello_content" >morx &&
781 test_ln_s_add morx same-dir-link &&
782 test_ln_s_add dir link-to-dir &&
783 test_ln_s_add ../fleem out-of-repo-link &&
784 test_ln_s_add .. out-of-repo-link-dir &&
785 test_ln_s_add same-dir-link link-to-link &&
786 test_ln_s_add nope broken-same-dir-link &&
787 mkdir dir &&
788 test_ln_s_add ../morx dir/parent-dir-link &&
789 test_ln_s_add .. dir/link-dir &&
790 test_ln_s_add ../../escape dir/out-of-repo-link &&
791 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
792 test_ln_s_add nope dir/broken-link-in-dir &&
793 mkdir dir/subdir &&
794 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
795 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
796 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
797 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
798 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
799 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
800 echo_without_newline "$hello_content" >dir/ind1 &&
801 test_ln_s_add dir dirlink &&
802 test_ln_s_add dir/subdir subdirlink &&
803 test_ln_s_add subdir/ind2 dir/link-to-child &&
804 test_ln_s_add dir/link-to-child link-to-down-link &&
805 test_ln_s_add dir/.. up-down &&
806 test_ln_s_add dir/../ up-down-trailing &&
807 test_ln_s_add dir/../morx up-down-file &&
808 test_ln_s_add dir/../../morx up-up-down-file &&
809 test_ln_s_add subdirlink/../../morx up-two-down-file &&
810 test_ln_s_add loop1 loop2 &&
811 test_ln_s_add loop2 loop1 &&
812 git add morx dir/subdir/ind2 dir/ind1 &&
813 git commit -am "test" &&
814 echo $hello_sha1 blob $hello_size >found
817 test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
818 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
819 test_cmp found actual &&
820 echo HEAD:nope missing >expect &&
821 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
822 test_cmp expect actual
825 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
826 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
827 test_cmp found actual
830 test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
831 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
832 test_cmp found actual
836 test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
837 echo dangling 25 >expect &&
838 echo HEAD:broken-same-dir-link >>expect &&
839 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
840 test_cmp expect actual
843 test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
844 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
845 test_cmp found actual
848 test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
849 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
850 test_cmp found actual &&
851 echo notdir 29 >expect &&
852 echo HEAD:dir/parent-dir-link/nope >>expect &&
853 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
854 test_cmp expect actual
857 test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
858 echo dangling 22 >expect &&
859 echo HEAD:dir/link-dir/nope >>expect &&
860 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
861 test_cmp expect actual &&
862 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
863 test_cmp found actual &&
864 echo dangling 27 >expect &&
865 echo HEAD:dir/broken-link-in-dir >>expect &&
866 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
867 test_cmp expect actual
870 test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
871 echo notdir 41 >expect &&
872 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
873 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
874 test_cmp expect actual &&
875 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
876 test_cmp found actual &&
877 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
878 test_cmp found actual
881 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
882 echo dangling 17 >expect &&
883 echo HEAD:dirlink/morx >>expect &&
884 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
885 test_cmp expect actual &&
886 echo $hello_sha1 blob $hello_size >expect &&
887 echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
888 test_cmp expect actual
891 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
892 echo dangling 20 >expect &&
893 echo HEAD:subdirlink/morx >>expect &&
894 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
895 test_cmp expect actual &&
896 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
897 test_cmp found actual
900 test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
901 echo notdir 27 >expect &&
902 echo HEAD:dir/link-to-child/morx >>expect &&
903 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
904 test_cmp expect actual &&
905 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
906 test_cmp found actual &&
907 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
908 test_cmp found actual
911 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
912 echo symlink 8 >expect &&
913 echo ../fleem >>expect &&
914 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
915 test_cmp expect actual &&
916 echo symlink 2 >expect &&
917 echo .. >>expect &&
918 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
919 test_cmp expect actual
922 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
923 echo symlink 9 >expect &&
924 echo ../escape >>expect &&
925 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
926 test_cmp expect actual &&
927 echo symlink 2 >expect &&
928 echo .. >>expect &&
929 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
930 test_cmp expect actual
933 test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
934 echo symlink 15 >expect &&
935 echo ../great-escape >>expect &&
936 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
937 test_cmp expect actual &&
938 echo symlink 2 >expect &&
939 echo .. >>expect &&
940 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
941 test_cmp expect actual &&
942 echo symlink 3 >expect &&
943 echo ../ >>expect &&
944 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
945 test_cmp expect actual
948 test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
949 echo HEAD: | git cat-file --batch-check >expect &&
950 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
951 test_cmp expect actual &&
952 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
953 test_cmp expect actual &&
954 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
955 test_cmp found actual &&
956 echo symlink 7 >expect &&
957 echo ../morx >>expect &&
958 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
959 test_cmp expect actual &&
960 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
961 test_cmp found actual
964 test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
965 echo loop 10 >expect &&
966 echo HEAD:loop1 >>expect &&
967 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
968 test_cmp expect actual
971 test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
972 echo HEAD:morx | git cat-file --batch >expect &&
973 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
974 test_cmp expect actual
977 test_expect_success 'cat-file --batch-all-objects shows all objects' '
978 # make new repos so we know the full set of objects; we will
979 # also make sure that there are some packed and some loose
980 # objects, some referenced and some not, some duplicates, and that
981 # there are some available only via alternates.
982 git init all-one &&
984 cd all-one &&
985 echo content >file &&
986 git add file &&
987 git commit -qm base &&
988 git rev-parse HEAD HEAD^{tree} HEAD:file &&
989 git repack -ad &&
990 echo not-cloned | git hash-object -w --stdin
991 ) >expect.unsorted &&
992 git clone -s all-one all-two &&
994 cd all-two &&
995 echo local-unref | git hash-object -w --stdin
996 ) >>expect.unsorted &&
997 git -C all-two rev-parse HEAD:file |
998 git -C all-two pack-objects .git/objects/pack/pack &&
999 sort <expect.unsorted >expect &&
1000 git -C all-two cat-file --batch-all-objects \
1001 --batch-check="%(objectname)" >actual &&
1002 test_cmp expect actual
1005 # The only user-visible difference is that the objects are no longer sorted,
1006 # and the resulting sort order is undefined. So we can only check that it
1007 # produces the same objects as the ordered case, but that at least exercises
1008 # the code.
1009 test_expect_success 'cat-file --unordered works' '
1010 git -C all-two cat-file --batch-all-objects --unordered \
1011 --batch-check="%(objectname)" >actual.unsorted &&
1012 sort <actual.unsorted >actual &&
1013 test_cmp expect actual
1016 test_expect_success 'set up object list for --batch-all-objects tests' '
1017 git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects
1020 test_expect_success 'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
1021 git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
1022 git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
1023 cmp expect actual
1026 test_expect_success 'cat-file --batch="%(rest)" with --batch-all-objects will work' '
1027 git -C all-two cat-file --batch="%(rest)" <objects >expect &&
1028 git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
1029 cmp expect actual
1032 test_expect_success 'cat-file --batch="batman" with --batch-all-objects will work' '
1033 git -C all-two cat-file --batch="batman" <objects >expect &&
1034 git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
1035 cmp expect actual
1038 test_expect_success 'set up replacement object' '
1039 orig=$(git rev-parse HEAD) &&
1040 git cat-file commit $orig >orig &&
1042 cat orig &&
1043 echo extra
1044 } >fake &&
1045 fake=$(git hash-object -t commit -w fake) &&
1046 orig_size=$(git cat-file -s $orig) &&
1047 fake_size=$(git cat-file -s $fake) &&
1048 git replace $orig $fake
1051 test_expect_success 'cat-file --batch respects replace objects' '
1052 git cat-file --batch >actual <<-EOF &&
1053 $orig
1056 echo "$orig commit $fake_size" &&
1057 cat fake &&
1058 echo
1059 } >expect &&
1060 test_cmp expect actual
1063 test_expect_success 'cat-file --batch-check respects replace objects' '
1064 git cat-file --batch-check >actual <<-EOF &&
1065 $orig
1067 echo "$orig commit $fake_size" >expect &&
1068 test_cmp expect actual
1071 # Pull the entry for object with oid "$1" out of the output of
1072 # "cat-file --batch", including its object content (which requires
1073 # parsing and reading a set amount of bytes, hence perl).
1074 extract_batch_output () {
1075 perl -ne '
1076 BEGIN { $oid = shift }
1077 if (/^$oid \S+ (\d+)$/) {
1078 print;
1079 read STDIN, my $buf, $1;
1080 print $buf;
1081 print "\n";
1083 ' "$@"
1086 test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
1087 git cat-file --batch-all-objects --batch >actual.raw &&
1088 extract_batch_output $orig <actual.raw >actual &&
1090 echo "$orig commit $orig_size" &&
1091 cat orig &&
1092 echo
1093 } >expect &&
1094 test_cmp expect actual
1097 test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
1098 git cat-file --batch-all-objects --batch-check >actual.raw &&
1099 grep ^$orig actual.raw >actual &&
1100 echo "$orig commit $orig_size" >expect &&
1101 test_cmp expect actual
1103 test_expect_success 'batch-command empty command' '
1104 echo "" >cmd &&
1105 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1106 grep "^fatal:.*empty command in input.*" err
1109 test_expect_success 'batch-command whitespace before command' '
1110 echo " info deadbeef" >cmd &&
1111 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1112 grep "^fatal:.*whitespace before command.*" err
1115 test_expect_success 'batch-command unknown command' '
1116 echo unknown_command >cmd &&
1117 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1118 grep "^fatal:.*unknown command.*" err
1121 test_expect_success 'batch-command missing arguments' '
1122 echo "info" >cmd &&
1123 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1124 grep "^fatal:.*info requires arguments.*" err
1127 test_expect_success 'batch-command flush with arguments' '
1128 echo "flush arg" >cmd &&
1129 test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
1130 grep "^fatal:.*flush takes no arguments.*" err
1133 test_expect_success 'batch-command flush without --buffer' '
1134 echo "flush" >cmd &&
1135 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1136 grep "^fatal:.*flush is only for --buffer mode.*" err
1139 test_done