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