3 test_description
='git cat-file'
7 test_cmdmode_usage
() {
8 test_expect_code
129 "$@" 2>err
&&
9 grep "^error: .* cannot be used together" err
17 '--textconv --filters' \
18 '--batch-all-objects -e'
20 test_expect_success
"usage: cmdmode $switches" '
21 test_cmdmode_usage git cat-file $switches
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
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"
47 test_expect_success
"usage: $opt requires another option" '
48 test_missing_usage git cat-file $opt
52 for opt
in $short_modes
54 test_expect_success
"usage: $opt requires another option" '
55 test_missing_usage git cat-file $opt
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
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
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
95 test_expect_success
"usage: bad option combination: $opt without batch mode" '
96 test_incompatible_usage git cat-file $opt &&
97 test_incompatible_usage git cat-file $opt commit HEAD
101 echo_without_newline
() {
105 echo_without_newline_nul
() {
106 echo_without_newline
"$@" |
tr '\n' '\0'
110 echo_without_newline
"$1" |
wc -c |
sed -e 's/^ *//'
120 batch_output
="$sha1 $type $size
123 test_expect_success
"$type exists" '
124 git cat-file -e $sha1
127 test_expect_success
"Type of $type is correct" '
128 echo $type >expect &&
129 git cat-file -t $sha1 >actual &&
130 test_cmp expect actual
133 test_expect_success
"Size of $type is correct" '
134 echo $size >expect &&
135 git cat-file -s $sha1 >actual &&
136 test_cmp expect actual
139 test_expect_success
"Type of $type is correct using --allow-unknown-type" '
140 echo $type >expect &&
141 git cat-file -t --allow-unknown-type $sha1 >actual &&
142 test_cmp expect actual
145 test_expect_success
"Size of $type is correct using --allow-unknown-type" '
146 echo $size >expect &&
147 git cat-file -s --allow-unknown-type $sha1 >actual &&
148 test_cmp expect actual
151 test -z "$content" ||
152 test_expect_success
"Content of $type is correct" '
153 echo_without_newline "$content" >expect &&
154 git cat-file $type $sha1 >actual &&
155 test_cmp expect actual
158 test_expect_success
"Pretty content of $type is correct" '
159 echo_without_newline "$pretty_content" >expect &&
160 git cat-file -p $sha1 >actual &&
161 test_cmp expect actual
164 test -z "$content" ||
165 test_expect_success
"--batch output of $type is correct" '
166 echo "$batch_output" >expect &&
167 echo $sha1 | git cat-file --batch >actual &&
168 test_cmp expect actual
171 test_expect_success
"--batch-check output of $type is correct" '
172 echo "$sha1 $type $size" >expect &&
173 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
174 test_cmp expect actual
177 for opt
in --buffer --no-buffer
179 test -z "$content" ||
180 test_expect_success
"--batch-command $opt output of $type content is correct" '
181 echo "$batch_output" >expect &&
182 test_write_lines "contents $sha1" | git cat-file --batch-command $opt >actual &&
183 test_cmp expect actual
186 test_expect_success
"--batch-command $opt output of $type info is correct" '
187 echo "$sha1 $type $size" >expect &&
188 test_write_lines "info $sha1" |
189 git cat-file --batch-command $opt >actual &&
190 test_cmp expect actual
194 test_expect_success
"custom --batch-check format" '
195 echo "$type $sha1" >expect &&
196 echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
197 test_cmp expect actual
200 test_expect_success
"custom --batch-command format" '
201 echo "$type $sha1" >expect &&
202 echo "info $sha1" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
203 test_cmp expect actual
206 test_expect_success
'--batch-check with %(rest)' '
207 echo "$type this is some extra content" >expect &&
208 echo "$sha1 this is some extra content" |
209 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
210 test_cmp expect actual
213 test -z "$content" ||
214 test_expect_success
"--batch without type ($type)" '
219 echo $sha1 | git cat-file --batch="%(objectsize)" >actual &&
220 test_cmp expect actual
223 test -z "$content" ||
224 test_expect_success
"--batch without size ($type)" '
229 echo $sha1 | git cat-file --batch="%(objecttype)" >actual &&
230 test_cmp expect actual
234 hello_content
="Hello World"
235 hello_size
=$
(strlen
"$hello_content")
236 hello_sha1
=$
(echo_without_newline
"$hello_content" | git hash-object
--stdin)
238 test_expect_success
"setup" '
239 echo_without_newline "$hello_content" > hello &&
240 git update-index --add hello
243 run_tests
'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
245 test_expect_success
'--batch-command --buffer with flush for blob info' '
246 echo "$hello_sha1 blob $hello_size" >expect &&
247 test_write_lines "info $hello_sha1" "flush" |
248 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
249 git cat-file --batch-command --buffer >actual &&
250 test_cmp expect actual
253 test_expect_success
'--batch-command --buffer without flush for blob info' '
255 test_write_lines "info $hello_sha1" |
256 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
257 git cat-file --batch-command --buffer >>output &&
258 test_must_be_empty output
261 test_expect_success
'--batch-check without %(rest) considers whole line' '
262 echo "$hello_sha1 blob $hello_size" >expect &&
263 git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
264 test_when_finished "git update-index --remove \"white space\"" &&
265 echo ":white space" | git cat-file --batch-check >actual &&
266 test_cmp expect actual
269 tree_sha1
=$
(git write-tree
)
270 tree_size
=$
(($
(test_oid rawsz
) + 13))
271 tree_pretty_content
="100644 blob $hello_sha1 hello${LF}"
273 run_tests
'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
275 commit_message
="Initial commit"
276 commit_sha1
=$
(echo_without_newline
"$commit_message" | git commit-tree
$tree_sha1)
277 commit_size
=$
(($
(test_oid hexsz
) + 137))
278 commit_content
="tree $tree_sha1
279 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE
280 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
284 run_tests
'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content"
286 tag_header_without_timestamp
="object $hello_sha1
289 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
290 tag_description
="This is a tag"
291 tag_content
="$tag_header_without_timestamp 0 +0000
295 tag_sha1
=$
(echo_without_newline
"$tag_content" | git hash-object
-t tag
--stdin -w)
296 tag_size
=$
(strlen
"$tag_content")
298 run_tests
'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content"
300 test_expect_success
"Reach a blob from a tag pointing to it" '
301 echo_without_newline "$hello_content" >expect &&
302 git cat-file blob $tag_sha1 >actual &&
303 test_cmp expect actual
306 for batch in batch batch-check batch-command
310 test_expect_success
"Passing -$opt with --$batch fails" '
311 test_must_fail git cat-file --$batch -$opt $hello_sha1
314 test_expect_success
"Passing --$batch with -$opt fails" '
315 test_must_fail git cat-file -$opt --$batch $hello_sha1
319 test_expect_success
"Passing <type> with --$batch fails" '
320 test_must_fail git cat-file --$batch blob $hello_sha1
323 test_expect_success
"Passing --$batch with <type> fails" '
324 test_must_fail git cat-file blob --$batch $hello_sha1
327 test_expect_success
"Passing sha1 with --$batch fails" '
328 test_must_fail git cat-file --$batch $hello_sha1
334 test_expect_success
"Passing -$opt with --follow-symlinks fails" '
335 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
339 test_expect_success
"--batch-check for a non-existent named object" '
340 cat >expect <<-EOF &&
345 printf "foobar42\nfoobar84" >in &&
346 git cat-file --batch-check <in >actual &&
347 test_cmp expect actual
350 test_expect_success
"--batch-check for a non-existent hash" '
351 cat >expect <<-EOF &&
352 0000000000000000000000000000000000000042 missing
353 0000000000000000000000000000000000000084 missing
356 printf "0000000000000000000000000000000000000042\n0000000000000000000000000000000000000084" >in &&
357 git cat-file --batch-check <in >actual &&
358 test_cmp expect actual
361 test_expect_success
"--batch for an existent and a non-existent hash" '
362 cat >expect <<-EOF &&
363 $tag_sha1 tag $tag_size
365 0000000000000000000000000000000000000000 missing
368 printf "$tag_sha1\n0000000000000000000000000000000000000000" >in &&
369 git cat-file --batch <in >actual &&
370 test_cmp expect actual
373 test_expect_success
"--batch-check for an empty line" '
374 cat >expect <<-EOF &&
379 git cat-file --batch-check <in >actual &&
380 test_cmp expect actual
383 test_expect_success
'empty --batch-check notices missing object' '
384 echo "$ZERO_OID missing" >expect &&
385 echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
386 test_cmp expect actual
389 batch_input
="$hello_sha1
397 "$hello_sha1 blob $hello_size" \
399 "$commit_sha1 commit $commit_size" \
401 "$tag_sha1 tag $tag_size" \
404 " missing" >batch_output
406 test_expect_success
'--batch with multiple sha1s gives correct format' '
407 tr "\0" "\n" <batch_output >expect &&
408 echo_without_newline "$batch_input" >in &&
409 git cat-file --batch <in >actual &&
410 test_cmp expect actual
413 test_expect_success
'--batch, -z with multiple sha1s gives correct format' '
414 echo_without_newline_nul "$batch_input" >in &&
415 tr "\0" "\n" <batch_output >expect &&
416 git cat-file --batch -z <in >actual &&
417 test_cmp expect actual
420 test_expect_success
'--batch, -Z with multiple sha1s gives correct format' '
421 echo_without_newline_nul "$batch_input" >in &&
422 git cat-file --batch -Z <in >actual &&
423 test_cmp batch_output actual
426 batch_check_input
="$hello_sha1
435 "$hello_sha1 blob $hello_size" \
436 "$tree_sha1 tree $tree_size" \
437 "$commit_sha1 commit $commit_size" \
438 "$tag_sha1 tag $tag_size" \
440 " missing" >batch_check_output
442 test_expect_success
"--batch-check with multiple sha1s gives correct format" '
443 tr "\0" "\n" <batch_check_output >expect &&
444 echo_without_newline "$batch_check_input" >in &&
445 git cat-file --batch-check <in >actual &&
446 test_cmp expect actual
449 test_expect_success
"--batch-check, -z with multiple sha1s gives correct format" '
450 tr "\0" "\n" <batch_check_output >expect &&
451 echo_without_newline_nul "$batch_check_input" >in &&
452 git cat-file --batch-check -z <in >actual &&
453 test_cmp expect actual
456 test_expect_success
"--batch-check, -Z with multiple sha1s gives correct format" '
457 echo_without_newline_nul "$batch_check_input" >in &&
458 git cat-file --batch-check -Z <in >actual &&
459 test_cmp batch_check_output actual
462 test_expect_success FUNNYNAMES
'setup with newline in input' '
463 touch -- "newline${LF}embedded" &&
464 git add -- "newline${LF}embedded" &&
465 git commit -m "file with newline embedded" &&
468 printf "HEAD:newline${LF}embedded" >in
471 test_expect_success FUNNYNAMES
'--batch-check, -z with newline in input' '
472 git cat-file --batch-check -z <in >actual &&
473 echo "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
474 test_cmp expect actual
477 test_expect_success FUNNYNAMES
'--batch-check, -Z with newline in input' '
478 git cat-file --batch-check -Z <in >actual &&
479 printf "%s\0" "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
480 test_cmp expect actual
483 batch_command_multiple_info
="info $hello_sha1
489 test_expect_success
'--batch-command with multiple info calls gives correct format' '
490 cat >expect <<-EOF &&
491 $hello_sha1 blob $hello_size
492 $tree_sha1 tree $tree_size
493 $commit_sha1 commit $commit_size
494 $tag_sha1 tag $tag_size
498 echo "$batch_command_multiple_info" >in &&
499 git cat-file --batch-command --buffer <in >actual &&
501 test_cmp expect actual &&
503 echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
504 git cat-file --batch-command --buffer -z <in >actual &&
506 test_cmp expect actual &&
508 echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
509 tr "\n" "\0" <expect >expect_nul &&
510 git cat-file --batch-command --buffer -Z <in >actual &&
512 test_cmp expect_nul actual
515 batch_command_multiple_contents
="contents $hello_sha1
516 contents $commit_sha1
521 test_expect_success
'--batch-command with multiple command calls gives correct format' '
523 "$hello_sha1 blob $hello_size" \
525 "$commit_sha1 commit $commit_size" \
527 "$tag_sha1 tag $tag_size" \
529 "deadbeef missing" >expect_nul &&
530 tr "\0" "\n" <expect_nul >expect &&
532 echo "$batch_command_multiple_contents" >in &&
533 git cat-file --batch-command --buffer <in >actual &&
535 test_cmp expect actual &&
537 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
538 git cat-file --batch-command --buffer -z <in >actual &&
540 test_cmp expect actual &&
542 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
543 git cat-file --batch-command --buffer -Z <in >actual &&
545 test_cmp expect_nul actual
548 test_expect_success
'setup blobs which are likely to delta' '
549 test-tool genrandom foo 10240 >foo &&
550 { cat foo && echo plus; } >foo-plus &&
551 git add foo foo-plus &&
559 test_expect_success
'confirm that neither loose blob is a delta' '
560 cat >expect <<-EOF &&
564 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
565 test_cmp expect actual
568 # To avoid relying too much on the current delta heuristics,
569 # we will check only that one of the two objects is a delta
570 # against the other, but not the order. We can do so by just
571 # asking for the base of both, and checking whether either
572 # sha1 appears in the output.
573 test_expect_success
'%(deltabase) reports packed delta bases' '
575 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
577 grep "$(git rev-parse HEAD:foo)" actual ||
578 grep "$(git rev-parse HEAD:foo-plus)" actual
582 test_expect_success
'setup bogus data' '
583 bogus_short_type="bogus" &&
584 bogus_short_content="bogus" &&
585 bogus_short_size=$(strlen "$bogus_short_content") &&
586 bogus_short_sha1=$(echo_without_newline "$bogus_short_content" | git hash-object -t $bogus_short_type --literally -w --stdin) &&
588 bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" &&
589 bogus_long_content="bogus" &&
590 bogus_long_size=$(strlen "$bogus_long_content") &&
591 bogus_long_sha1=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin)
594 for arg1
in '' --allow-unknown-type
598 if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p"
604 test_expect_success
"cat-file $arg1 $arg2 error on bogus short OID" '
605 cat >expect <<-\EOF &&
606 fatal: invalid object type
609 if test "$arg1" = "--allow-unknown-type"
611 git cat-file $arg1 $arg2 $bogus_short_sha1
613 test_must_fail git cat-file $arg1 $arg2 $bogus_short_sha1 >out 2>actual &&
614 test_must_be_empty out &&
615 test_cmp expect actual
619 test_expect_success
"cat-file $arg1 $arg2 error on bogus full OID" '
620 if test "$arg2" = "-p"
623 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
624 fatal: Not a valid object name $bogus_long_sha1
628 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
629 fatal: git cat-file: could not get object info
633 if test "$arg1" = "--allow-unknown-type"
635 git cat-file $arg1 $arg2 $bogus_short_sha1
637 test_must_fail git cat-file $arg1 $arg2 $bogus_long_sha1 >out 2>actual &&
638 test_must_be_empty out &&
639 test_cmp expect actual
643 test_expect_success
"cat-file $arg1 $arg2 error on missing short OID" '
644 cat >expect.err <<-EOF &&
645 fatal: Not a valid object name $(test_oid deadbeef_short)
647 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef_short) >out 2>err.actual &&
648 test_must_be_empty out &&
649 test_cmp expect.err err.actual
652 test_expect_success
"cat-file $arg1 $arg2 error on missing full OID" '
653 if test "$arg2" = "-p"
655 cat >expect.err <<-EOF
656 fatal: Not a valid object name $(test_oid deadbeef)
659 cat >expect.err <<-\EOF
660 fatal: git cat-file: could not get object info
663 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
664 test_must_be_empty out &&
665 test_cmp expect.err err.actual
670 test_expect_success
'-e is OK with a broken object without --allow-unknown-type' '
671 git cat-file -e $bogus_short_sha1
674 test_expect_success
'-e can not be combined with --allow-unknown-type' '
675 test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_sha1
678 test_expect_success
'-p cannot print a broken object even with --allow-unknown-type' '
679 test_must_fail git cat-file -p $bogus_short_sha1 &&
680 test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_sha1
683 test_expect_success
'<type> <hash> does not work with objects of broken types' '
684 cat >err.expect <<-\EOF &&
685 fatal: invalid object type "bogus"
687 test_must_fail git cat-file $bogus_short_type $bogus_short_sha1 2>err.actual &&
688 test_cmp err.expect err.actual
691 test_expect_success
'broken types combined with --batch and --batch-check' '
692 echo $bogus_short_sha1 >bogus-oid &&
694 cat >err.expect <<-\EOF &&
695 fatal: invalid object type
698 test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
699 test_cmp err.expect err.actual &&
701 test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
702 test_cmp err.expect err.actual
705 test_expect_success
'the --batch and --batch-check options do not combine with --allow-unknown-type' '
706 test_expect_code 128 git cat-file --batch --allow-unknown-type <bogus-oid &&
707 test_expect_code 128 git cat-file --batch-check --allow-unknown-type <bogus-oid
710 test_expect_success
'the --allow-unknown-type option does not consider replacement refs' '
711 cat >expect <<-EOF &&
714 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
715 test_cmp expect actual &&
717 # Create it manually, as "git replace" will die on bogus
719 head=$(git rev-parse --verify HEAD) &&
720 test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_sha1" &&
721 test-tool ref-store main update-ref msg "refs/replace/$bogus_short_sha1" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
723 cat >expect <<-EOF &&
726 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
727 test_cmp expect actual
730 test_expect_success
"Type of broken object is correct" '
731 echo $bogus_short_type >expect &&
732 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
733 test_cmp expect actual
736 test_expect_success
"Size of broken object is correct" '
737 echo $bogus_short_size >expect &&
738 git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual &&
739 test_cmp expect actual
742 test_expect_success
'clean up broken object' '
743 rm .git/objects/$(test_oid_to_path $bogus_short_sha1)
746 test_expect_success
"Type of broken object is correct when type is large" '
747 echo $bogus_long_type >expect &&
748 git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual &&
749 test_cmp expect actual
752 test_expect_success
"Size of large broken object is correct when type is large" '
753 echo $bogus_long_size >expect &&
754 git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual &&
755 test_cmp expect actual
758 test_expect_success
'clean up broken object' '
759 rm .git/objects/$(test_oid_to_path $bogus_long_sha1)
762 test_expect_success
'cat-file -t and -s on corrupt loose object' '
763 git init --bare corrupt-loose.git &&
765 cd corrupt-loose.git &&
767 # Setup and create the empty blob and its path
768 empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
769 empty_blob=$(git hash-object -w --stdin </dev/null) &&
771 # Create another blob and its path
772 echo other >other.blob &&
773 other_blob=$(git hash-object -w --stdin <other.blob) &&
774 other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
776 # Before the swap the size is 0
777 cat >out.expect <<-EOF &&
780 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
781 test_must_be_empty err.actual &&
782 test_cmp out.expect out.actual &&
784 # Swap the two to corrupt the repository
785 mv -f "$other_path" "$empty_path" &&
786 test_must_fail git fsck 2>err.fsck &&
787 grep "hash-path mismatch" err.fsck &&
789 # confirm that cat-file is reading the new swapped-in
791 cat >out.expect <<-EOF &&
794 git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
795 test_must_be_empty err.actual &&
796 test_cmp out.expect out.actual &&
798 # ... since it has a different size now.
799 cat >out.expect <<-EOF &&
802 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
803 test_must_be_empty err.actual &&
804 test_cmp out.expect out.actual &&
806 # So far "cat-file" has been happy to spew the found
807 # content out as-is. Try to make it zlib-invalid.
808 mv -f other.blob "$empty_path" &&
809 test_must_fail git fsck 2>err.fsck &&
810 cat >expect <<-EOF &&
811 error: inflate: data stream error (incorrect header check)
812 error: unable to unpack header of ./$empty_path
813 error: $empty_blob: object corrupt or missing: ./$empty_path
815 grep "^error: " err.fsck >actual &&
816 test_cmp expect actual
820 # Tests for git cat-file --follow-symlinks
821 test_expect_success
'prep for symlink tests' '
822 echo_without_newline "$hello_content" >morx &&
823 test_ln_s_add morx same-dir-link &&
824 test_ln_s_add dir link-to-dir &&
825 test_ln_s_add ../fleem out-of-repo-link &&
826 test_ln_s_add .. out-of-repo-link-dir &&
827 test_ln_s_add same-dir-link link-to-link &&
828 test_ln_s_add nope broken-same-dir-link &&
830 test_ln_s_add ../morx dir/parent-dir-link &&
831 test_ln_s_add .. dir/link-dir &&
832 test_ln_s_add ../../escape dir/out-of-repo-link &&
833 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
834 test_ln_s_add nope dir/broken-link-in-dir &&
836 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
837 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
838 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
839 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
840 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
841 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
842 echo_without_newline "$hello_content" >dir/ind1 &&
843 test_ln_s_add dir dirlink &&
844 test_ln_s_add dir/subdir subdirlink &&
845 test_ln_s_add subdir/ind2 dir/link-to-child &&
846 test_ln_s_add dir/link-to-child link-to-down-link &&
847 test_ln_s_add dir/.. up-down &&
848 test_ln_s_add dir/../ up-down-trailing &&
849 test_ln_s_add dir/../morx up-down-file &&
850 test_ln_s_add dir/../../morx up-up-down-file &&
851 test_ln_s_add subdirlink/../../morx up-two-down-file &&
852 test_ln_s_add loop1 loop2 &&
853 test_ln_s_add loop2 loop1 &&
854 git add morx dir/subdir/ind2 dir/ind1 &&
855 git commit -am "test" &&
856 echo $hello_sha1 blob $hello_size >found
859 test_expect_success
'git cat-file --batch-check --follow-symlinks works for non-links' '
860 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
861 test_cmp found actual &&
862 echo HEAD:nope missing >expect &&
863 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
864 test_cmp expect actual
867 test_expect_success
'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
868 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
869 test_cmp found actual
872 test_expect_success
'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
873 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
874 test_cmp found actual
878 test_expect_success
'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
879 echo dangling 25 >expect &&
880 echo HEAD:broken-same-dir-link >>expect &&
881 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
882 test_cmp expect actual
885 test_expect_success
'git cat-file --batch-check --follow-symlinks -Z works for broken in-repo, same-dir links' '
886 printf "HEAD:broken-same-dir-link\0" >in &&
887 printf "dangling 25\0HEAD:broken-same-dir-link\0" >expect &&
888 git cat-file --batch-check --follow-symlinks -Z <in >actual &&
889 test_cmp expect actual
892 test_expect_success
'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
893 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
894 test_cmp found actual
897 test_expect_success
'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
898 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
899 test_cmp found actual &&
900 echo notdir 29 >expect &&
901 echo HEAD:dir/parent-dir-link/nope >>expect &&
902 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
903 test_cmp expect actual
906 test_expect_success
'git cat-file --batch-check --follow-symlinks -Z works for parent-dir links' '
907 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
908 test_cmp found actual &&
909 printf "notdir 29\0HEAD:dir/parent-dir-link/nope\0" >expect &&
910 printf "HEAD:dir/parent-dir-link/nope\0" >in &&
911 git cat-file --batch-check --follow-symlinks -Z <in >actual &&
912 test_cmp expect actual
915 test_expect_success
'git cat-file --batch-check --follow-symlinks works for .. links' '
916 echo dangling 22 >expect &&
917 echo HEAD:dir/link-dir/nope >>expect &&
918 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
919 test_cmp expect actual &&
920 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
921 test_cmp found actual &&
922 echo dangling 27 >expect &&
923 echo HEAD:dir/broken-link-in-dir >>expect &&
924 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
925 test_cmp expect actual
928 test_expect_success
'git cat-file --batch-check --follow-symlinks works for ../.. links' '
929 echo notdir 41 >expect &&
930 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
931 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
932 test_cmp expect actual &&
933 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
934 test_cmp found actual &&
935 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
936 test_cmp found actual
939 test_expect_success
'git cat-file --batch-check --follow-symlinks works for dir/ links' '
940 echo dangling 17 >expect &&
941 echo HEAD:dirlink/morx >>expect &&
942 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
943 test_cmp expect actual &&
944 echo $hello_sha1 blob $hello_size >expect &&
945 echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
946 test_cmp expect actual
949 test_expect_success
'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
950 echo dangling 20 >expect &&
951 echo HEAD:subdirlink/morx >>expect &&
952 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
953 test_cmp expect actual &&
954 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
955 test_cmp found actual
958 test_expect_success
'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
959 echo notdir 27 >expect &&
960 echo HEAD:dir/link-to-child/morx >>expect &&
961 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
962 test_cmp expect actual &&
963 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
964 test_cmp found actual &&
965 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
966 test_cmp found actual
969 test_expect_success
'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
970 echo symlink 8 >expect &&
971 echo ../fleem >>expect &&
972 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
973 test_cmp expect actual &&
974 echo symlink 2 >expect &&
976 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
977 test_cmp expect actual
980 test_expect_success
'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
981 echo symlink 9 >expect &&
982 echo ../escape >>expect &&
983 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
984 test_cmp expect actual &&
985 echo symlink 2 >expect &&
987 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
988 test_cmp expect actual
991 test_expect_success
'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
992 echo symlink 15 >expect &&
993 echo ../great-escape >>expect &&
994 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
995 test_cmp expect actual &&
996 echo symlink 2 >expect &&
998 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
999 test_cmp expect actual &&
1000 echo symlink 3 >expect &&
1001 echo ../ >>expect &&
1002 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
1003 test_cmp expect actual
1006 test_expect_success
'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
1007 echo HEAD: | git cat-file --batch-check >expect &&
1008 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
1009 test_cmp expect actual &&
1010 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
1011 test_cmp expect actual &&
1012 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
1013 test_cmp found actual &&
1014 echo symlink 7 >expect &&
1015 echo ../morx >>expect &&
1016 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
1017 test_cmp expect actual &&
1018 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
1019 test_cmp found actual
1022 test_expect_success
'git cat-file --batch-check --follow-symlink breaks loops' '
1023 echo loop 10 >expect &&
1024 echo HEAD:loop1 >>expect &&
1025 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
1026 test_cmp expect actual
1029 test_expect_success
'git cat-file --batch-check --follow-symlink -Z breaks loops' '
1030 printf "loop 10\0HEAD:loop1\0" >expect &&
1031 printf "HEAD:loop1\0" >in &&
1032 git cat-file --batch-check --follow-symlinks -Z <in >actual &&
1033 test_cmp expect actual
1036 test_expect_success
'git cat-file --batch --follow-symlink returns correct sha and mode' '
1037 echo HEAD:morx | git cat-file --batch >expect &&
1038 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
1039 test_cmp expect actual
1042 test_expect_success
'cat-file --batch-all-objects shows all objects' '
1043 # make new repos so we know the full set of objects; we will
1044 # also make sure that there are some packed and some loose
1045 # objects, some referenced and some not, some duplicates, and that
1046 # there are some available only via alternates.
1050 echo content >file &&
1052 git commit -qm base &&
1053 git rev-parse HEAD HEAD^{tree} HEAD:file &&
1055 echo not-cloned | git hash-object -w --stdin
1056 ) >expect.unsorted &&
1057 git clone -s all-one all-two &&
1060 echo local-unref | git hash-object -w --stdin
1061 ) >>expect.unsorted &&
1062 git -C all-two rev-parse HEAD:file |
1063 git -C all-two pack-objects .git/objects/pack/pack &&
1064 sort <expect.unsorted >expect &&
1065 git -C all-two cat-file --batch-all-objects \
1066 --batch-check="%(objectname)" >actual &&
1067 test_cmp expect actual
1070 # The only user-visible difference is that the objects are no longer sorted,
1071 # and the resulting sort order is undefined. So we can only check that it
1072 # produces the same objects as the ordered case, but that at least exercises
1074 test_expect_success
'cat-file --unordered works' '
1075 git -C all-two cat-file --batch-all-objects --unordered \
1076 --batch-check="%(objectname)" >actual.unsorted &&
1077 sort <actual.unsorted >actual &&
1078 test_cmp expect actual
1081 test_expect_success
'set up object list for --batch-all-objects tests' '
1082 git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects
1085 test_expect_success
'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
1086 git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
1087 git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
1091 test_expect_success
'cat-file --batch="%(rest)" with --batch-all-objects will work' '
1092 git -C all-two cat-file --batch="%(rest)" <objects >expect &&
1093 git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
1097 test_expect_success
'cat-file --batch="batman" with --batch-all-objects will work' '
1098 git -C all-two cat-file --batch="batman" <objects >expect &&
1099 git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
1103 test_expect_success
'cat-file %(objectsize:disk) with --batch-all-objects' '
1104 # our state has both loose and packed objects,
1105 # so find both for our expected output
1107 find .git/objects/?? -type f |
1108 awk -F/ "{ print \$0, \$3\$4 }" |
1111 size=$(test_file_size "$path") &&
1112 echo "$oid $size" ||
1115 rawsz=$(test_oid rawsz) &&
1116 find .git/objects/pack -name "*.idx" |
1119 git show-index <"$idx" >idx.raw &&
1120 sort -nr <idx.raw >idx.sorted &&
1121 packsz=$(test_file_size "${idx%.idx}.pack") &&
1122 end=$((packsz - rawsz)) &&
1123 while read start oid rest
1125 size=$((end - start)) &&
1127 echo "$oid $size" ||
1133 sort <expect.raw >expect &&
1134 git cat-file --batch-all-objects \
1135 --batch-check="%(objectname) %(objectsize:disk)" >actual &&
1136 test_cmp expect actual
1139 test_expect_success
'set up replacement object' '
1140 orig=$(git rev-parse HEAD) &&
1141 git cat-file commit $orig >orig &&
1146 fake=$(git hash-object -t commit -w fake) &&
1147 orig_size=$(git cat-file -s $orig) &&
1148 fake_size=$(git cat-file -s $fake) &&
1149 git replace $orig $fake
1152 test_expect_success
'cat-file --batch respects replace objects' '
1153 git cat-file --batch >actual <<-EOF &&
1157 echo "$orig commit $fake_size" &&
1161 test_cmp expect actual
1164 test_expect_success
'cat-file --batch-check respects replace objects' '
1165 git cat-file --batch-check >actual <<-EOF &&
1168 echo "$orig commit $fake_size" >expect &&
1169 test_cmp expect actual
1172 # Pull the entry for object with oid "$1" out of the output of
1173 # "cat-file --batch", including its object content (which requires
1174 # parsing and reading a set amount of bytes, hence perl).
1175 extract_batch_output
() {
1177 BEGIN { $oid = shift }
1178 if (/^$oid \S+ (\d+)$/) {
1180 read STDIN, my $buf, $1;
1187 test_expect_success
'cat-file --batch-all-objects --batch ignores replace' '
1188 git cat-file --batch-all-objects --batch >actual.raw &&
1189 extract_batch_output $orig <actual.raw >actual &&
1191 echo "$orig commit $orig_size" &&
1195 test_cmp expect actual
1198 test_expect_success
'cat-file --batch-all-objects --batch-check ignores replace' '
1199 git cat-file --batch-all-objects --batch-check >actual.raw &&
1200 grep ^$orig actual.raw >actual &&
1201 echo "$orig commit $orig_size" >expect &&
1202 test_cmp expect actual
1204 test_expect_success
'batch-command empty command' '
1206 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1207 grep "^fatal:.*empty command in input.*" err
1210 test_expect_success
'batch-command whitespace before command' '
1211 echo " info deadbeef" >cmd &&
1212 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1213 grep "^fatal:.*whitespace before command.*" err
1216 test_expect_success
'batch-command unknown command' '
1217 echo unknown_command >cmd &&
1218 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1219 grep "^fatal:.*unknown command.*" err
1222 test_expect_success
'batch-command missing arguments' '
1224 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1225 grep "^fatal:.*info requires arguments.*" err
1228 test_expect_success
'batch-command flush with arguments' '
1229 echo "flush arg" >cmd &&
1230 test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
1231 grep "^fatal:.*flush takes no arguments.*" err
1234 test_expect_success
'batch-command flush without --buffer' '
1235 echo "flush" >cmd &&
1236 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1237 grep "^fatal:.*flush is only for --buffer mode.*" err