]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1006-cat-file.sh
Merge branch 'jc/orphan-unborn' into maint-2.43
[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: .* cannot be used together" 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 -z \
93 -Z
94 do
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
98 '
99 done
100
101 echo_without_newline () {
102 printf '%s' "$*"
103 }
104
105 echo_without_newline_nul () {
106 echo_without_newline "$@" | tr '\n' '\0'
107 }
108
109 strlen () {
110 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
111 }
112
113 run_tests () {
114 type=$1
115 sha1=$2
116 size=$3
117 content=$4
118 pretty_content=$5
119
120 batch_output="$sha1 $type $size
121 $content"
122
123 test_expect_success "$type exists" '
124 git cat-file -e $sha1
125 '
126
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
131 '
132
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
137 '
138
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
143 '
144
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
149 '
150
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
156 '
157
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
162 '
163
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
169 '
170
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
175 '
176
177 for opt in --buffer --no-buffer
178 do
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
184 '
185
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
191 '
192 done
193
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
198 '
199
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
204 '
205
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
211 '
212
213 test -z "$content" ||
214 test_expect_success "--batch without type ($type)" '
215 {
216 echo "$size" &&
217 echo "$content"
218 } >expect &&
219 echo $sha1 | git cat-file --batch="%(objectsize)" >actual &&
220 test_cmp expect actual
221 '
222
223 test -z "$content" ||
224 test_expect_success "--batch without size ($type)" '
225 {
226 echo "$type" &&
227 echo "$content"
228 } >expect &&
229 echo $sha1 | git cat-file --batch="%(objecttype)" >actual &&
230 test_cmp expect actual
231 '
232 }
233
234 hello_content="Hello World"
235 hello_size=$(strlen "$hello_content")
236 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
237
238 test_expect_success "setup" '
239 echo_without_newline "$hello_content" > hello &&
240 git update-index --add hello
241 '
242
243 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
244
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
251 '
252
253 test_expect_success '--batch-command --buffer without flush for blob info' '
254 touch output &&
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
259 '
260
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
267 '
268
269 tree_sha1=$(git write-tree)
270 tree_size=$(($(test_oid rawsz) + 13))
271 tree_pretty_content="100644 blob $hello_sha1 hello${LF}"
272
273 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
274
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
281
282 $commit_message"
283
284 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content"
285
286 tag_header_without_timestamp="object $hello_sha1
287 type blob
288 tag hellotag
289 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
290 tag_description="This is a tag"
291 tag_content="$tag_header_without_timestamp 0 +0000
292
293 $tag_description"
294
295 tag_sha1=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
296 tag_size=$(strlen "$tag_content")
297
298 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content"
299
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
304 '
305
306 for batch in batch batch-check batch-command
307 do
308 for opt in t s e p
309 do
310 test_expect_success "Passing -$opt with --$batch fails" '
311 test_must_fail git cat-file --$batch -$opt $hello_sha1
312 '
313
314 test_expect_success "Passing --$batch with -$opt fails" '
315 test_must_fail git cat-file -$opt --$batch $hello_sha1
316 '
317 done
318
319 test_expect_success "Passing <type> with --$batch fails" '
320 test_must_fail git cat-file --$batch blob $hello_sha1
321 '
322
323 test_expect_success "Passing --$batch with <type> fails" '
324 test_must_fail git cat-file blob --$batch $hello_sha1
325 '
326
327 test_expect_success "Passing sha1 with --$batch fails" '
328 test_must_fail git cat-file --$batch $hello_sha1
329 '
330 done
331
332 for opt in t s e p
333 do
334 test_expect_success "Passing -$opt with --follow-symlinks fails" '
335 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
336 '
337 done
338
339 test_expect_success "--batch-check for a non-existent named object" '
340 cat >expect <<-EOF &&
341 foobar42 missing
342 foobar84 missing
343 EOF
344
345 printf "foobar42\nfoobar84" >in &&
346 git cat-file --batch-check <in >actual &&
347 test_cmp expect actual
348 '
349
350 test_expect_success "--batch-check for a non-existent hash" '
351 cat >expect <<-EOF &&
352 0000000000000000000000000000000000000042 missing
353 0000000000000000000000000000000000000084 missing
354 EOF
355
356 printf "0000000000000000000000000000000000000042\n0000000000000000000000000000000000000084" >in &&
357 git cat-file --batch-check <in >actual &&
358 test_cmp expect actual
359 '
360
361 test_expect_success "--batch for an existent and a non-existent hash" '
362 cat >expect <<-EOF &&
363 $tag_sha1 tag $tag_size
364 $tag_content
365 0000000000000000000000000000000000000000 missing
366 EOF
367
368 printf "$tag_sha1\n0000000000000000000000000000000000000000" >in &&
369 git cat-file --batch <in >actual &&
370 test_cmp expect actual
371 '
372
373 test_expect_success "--batch-check for an empty line" '
374 cat >expect <<-EOF &&
375 missing
376 EOF
377
378 echo >in &&
379 git cat-file --batch-check <in >actual &&
380 test_cmp expect actual
381 '
382
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
387 '
388
389 batch_input="$hello_sha1
390 $commit_sha1
391 $tag_sha1
392 deadbeef
393
394 "
395
396 printf "%s\0" \
397 "$hello_sha1 blob $hello_size" \
398 "$hello_content" \
399 "$commit_sha1 commit $commit_size" \
400 "$commit_content" \
401 "$tag_sha1 tag $tag_size" \
402 "$tag_content" \
403 "deadbeef missing" \
404 " missing" >batch_output
405
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
411 '
412
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
418 '
419
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
424 '
425
426 batch_check_input="$hello_sha1
427 $tree_sha1
428 $commit_sha1
429 $tag_sha1
430 deadbeef
431
432 "
433
434 printf "%s\0" \
435 "$hello_sha1 blob $hello_size" \
436 "$tree_sha1 tree $tree_size" \
437 "$commit_sha1 commit $commit_size" \
438 "$tag_sha1 tag $tag_size" \
439 "deadbeef missing" \
440 " missing" >batch_check_output
441
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
447 '
448
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
454 '
455
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
460 '
461
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" &&
466 test_tick &&
467
468 printf "HEAD:newline${LF}embedded" >in
469 '
470
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
475 '
476
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
481 '
482
483 batch_command_multiple_info="info $hello_sha1
484 info $tree_sha1
485 info $commit_sha1
486 info $tag_sha1
487 info deadbeef"
488
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
495 deadbeef missing
496 EOF
497
498 echo "$batch_command_multiple_info" >in &&
499 git cat-file --batch-command --buffer <in >actual &&
500
501 test_cmp expect actual &&
502
503 echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
504 git cat-file --batch-command --buffer -z <in >actual &&
505
506 test_cmp expect actual &&
507
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 &&
511
512 test_cmp expect_nul actual
513 '
514
515 batch_command_multiple_contents="contents $hello_sha1
516 contents $commit_sha1
517 contents $tag_sha1
518 contents deadbeef
519 flush"
520
521 test_expect_success '--batch-command with multiple command calls gives correct format' '
522 printf "%s\0" \
523 "$hello_sha1 blob $hello_size" \
524 "$hello_content" \
525 "$commit_sha1 commit $commit_size" \
526 "$commit_content" \
527 "$tag_sha1 tag $tag_size" \
528 "$tag_content" \
529 "deadbeef missing" >expect_nul &&
530 tr "\0" "\n" <expect_nul >expect &&
531
532 echo "$batch_command_multiple_contents" >in &&
533 git cat-file --batch-command --buffer <in >actual &&
534
535 test_cmp expect actual &&
536
537 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
538 git cat-file --batch-command --buffer -z <in >actual &&
539
540 test_cmp expect actual &&
541
542 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
543 git cat-file --batch-command --buffer -Z <in >actual &&
544
545 test_cmp expect_nul actual
546 '
547
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 &&
552 git commit -m foo &&
553 cat >blobs <<-\EOF
554 HEAD:foo
555 HEAD:foo-plus
556 EOF
557 '
558
559 test_expect_success 'confirm that neither loose blob is a delta' '
560 cat >expect <<-EOF &&
561 $ZERO_OID
562 $ZERO_OID
563 EOF
564 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
565 test_cmp expect actual
566 '
567
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' '
574 git repack -ad &&
575 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
576 {
577 grep "$(git rev-parse HEAD:foo)" actual ||
578 grep "$(git rev-parse HEAD:foo-plus)" actual
579 }
580 '
581
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) &&
587
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)
592 '
593
594 for arg1 in '' --allow-unknown-type
595 do
596 for arg2 in -s -t -p
597 do
598 if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p"
599 then
600 continue
601 fi
602
603
604 test_expect_success "cat-file $arg1 $arg2 error on bogus short OID" '
605 cat >expect <<-\EOF &&
606 fatal: invalid object type
607 EOF
608
609 if test "$arg1" = "--allow-unknown-type"
610 then
611 git cat-file $arg1 $arg2 $bogus_short_sha1
612 else
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
616 fi
617 '
618
619 test_expect_success "cat-file $arg1 $arg2 error on bogus full OID" '
620 if test "$arg2" = "-p"
621 then
622 cat >expect <<-EOF
623 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
624 fatal: Not a valid object name $bogus_long_sha1
625 EOF
626 else
627 cat >expect <<-EOF
628 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
629 fatal: git cat-file: could not get object info
630 EOF
631 fi &&
632
633 if test "$arg1" = "--allow-unknown-type"
634 then
635 git cat-file $arg1 $arg2 $bogus_short_sha1
636 else
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
640 fi
641 '
642
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)
646 EOF
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
650 '
651
652 test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
653 if test "$arg2" = "-p"
654 then
655 cat >expect.err <<-EOF
656 fatal: Not a valid object name $(test_oid deadbeef)
657 EOF
658 else
659 cat >expect.err <<-\EOF
660 fatal: git cat-file: could not get object info
661 EOF
662 fi &&
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
666 '
667 done
668 done
669
670 test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
671 git cat-file -e $bogus_short_sha1
672 '
673
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
676 '
677
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
681 '
682
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"
686 EOF
687 test_must_fail git cat-file $bogus_short_type $bogus_short_sha1 2>err.actual &&
688 test_cmp err.expect err.actual
689 '
690
691 test_expect_success 'broken types combined with --batch and --batch-check' '
692 echo $bogus_short_sha1 >bogus-oid &&
693
694 cat >err.expect <<-\EOF &&
695 fatal: invalid object type
696 EOF
697
698 test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
699 test_cmp err.expect err.actual &&
700
701 test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
702 test_cmp err.expect err.actual
703 '
704
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
708 '
709
710 test_expect_success 'the --allow-unknown-type option does not consider replacement refs' '
711 cat >expect <<-EOF &&
712 $bogus_short_type
713 EOF
714 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
715 test_cmp expect actual &&
716
717 # Create it manually, as "git replace" will die on bogus
718 # types.
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 &&
722
723 cat >expect <<-EOF &&
724 commit
725 EOF
726 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
727 test_cmp expect actual
728 '
729
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
734 '
735
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
740 '
741
742 test_expect_success 'clean up broken object' '
743 rm .git/objects/$(test_oid_to_path $bogus_short_sha1)
744 '
745
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
750 '
751
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
756 '
757
758 test_expect_success 'clean up broken object' '
759 rm .git/objects/$(test_oid_to_path $bogus_long_sha1)
760 '
761
762 test_expect_success 'cat-file -t and -s on corrupt loose object' '
763 git init --bare corrupt-loose.git &&
764 (
765 cd corrupt-loose.git &&
766
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) &&
770
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")) &&
775
776 # Before the swap the size is 0
777 cat >out.expect <<-EOF &&
778 0
779 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 &&
783
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 &&
788
789 # confirm that cat-file is reading the new swapped-in
790 # blob...
791 cat >out.expect <<-EOF &&
792 blob
793 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 &&
797
798 # ... since it has a different size now.
799 cat >out.expect <<-EOF &&
800 6
801 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 &&
805
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
814 EOF
815 grep "^error: " err.fsck >actual &&
816 test_cmp expect actual
817 )
818 '
819
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 &&
829 mkdir dir &&
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 &&
835 mkdir dir/subdir &&
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
857 '
858
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
865 '
866
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
870 '
871
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
875 '
876
877
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
883 '
884
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
890 '
891
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
895 '
896
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
904 '
905
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
913 '
914
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
926 '
927
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
937 '
938
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
947 '
948
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
956 '
957
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
967 '
968
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 &&
975 echo .. >>expect &&
976 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
977 test_cmp expect actual
978 '
979
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 &&
986 echo .. >>expect &&
987 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
988 test_cmp expect actual
989 '
990
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 &&
997 echo .. >>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
1004 '
1005
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
1020 '
1021
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
1027 '
1028
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
1034 '
1035
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
1040 '
1041
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.
1047 git init all-one &&
1048 (
1049 cd all-one &&
1050 echo content >file &&
1051 git add file &&
1052 git commit -qm base &&
1053 git rev-parse HEAD HEAD^{tree} HEAD:file &&
1054 git repack -ad &&
1055 echo not-cloned | git hash-object -w --stdin
1056 ) >expect.unsorted &&
1057 git clone -s all-one all-two &&
1058 (
1059 cd 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
1068 '
1069
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
1073 # the code.
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
1079 '
1080
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
1083 '
1084
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 &&
1088 cmp expect actual
1089 '
1090
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 &&
1094 cmp expect actual
1095 '
1096
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 &&
1100 cmp expect actual
1101 '
1102
1103 test_expect_success 'set up replacement object' '
1104 orig=$(git rev-parse HEAD) &&
1105 git cat-file commit $orig >orig &&
1106 {
1107 cat orig &&
1108 echo extra
1109 } >fake &&
1110 fake=$(git hash-object -t commit -w fake) &&
1111 orig_size=$(git cat-file -s $orig) &&
1112 fake_size=$(git cat-file -s $fake) &&
1113 git replace $orig $fake
1114 '
1115
1116 test_expect_success 'cat-file --batch respects replace objects' '
1117 git cat-file --batch >actual <<-EOF &&
1118 $orig
1119 EOF
1120 {
1121 echo "$orig commit $fake_size" &&
1122 cat fake &&
1123 echo
1124 } >expect &&
1125 test_cmp expect actual
1126 '
1127
1128 test_expect_success 'cat-file --batch-check respects replace objects' '
1129 git cat-file --batch-check >actual <<-EOF &&
1130 $orig
1131 EOF
1132 echo "$orig commit $fake_size" >expect &&
1133 test_cmp expect actual
1134 '
1135
1136 # Pull the entry for object with oid "$1" out of the output of
1137 # "cat-file --batch", including its object content (which requires
1138 # parsing and reading a set amount of bytes, hence perl).
1139 extract_batch_output () {
1140 perl -ne '
1141 BEGIN { $oid = shift }
1142 if (/^$oid \S+ (\d+)$/) {
1143 print;
1144 read STDIN, my $buf, $1;
1145 print $buf;
1146 print "\n";
1147 }
1148 ' "$@"
1149 }
1150
1151 test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
1152 git cat-file --batch-all-objects --batch >actual.raw &&
1153 extract_batch_output $orig <actual.raw >actual &&
1154 {
1155 echo "$orig commit $orig_size" &&
1156 cat orig &&
1157 echo
1158 } >expect &&
1159 test_cmp expect actual
1160 '
1161
1162 test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
1163 git cat-file --batch-all-objects --batch-check >actual.raw &&
1164 grep ^$orig actual.raw >actual &&
1165 echo "$orig commit $orig_size" >expect &&
1166 test_cmp expect actual
1167 '
1168 test_expect_success 'batch-command empty command' '
1169 echo "" >cmd &&
1170 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1171 grep "^fatal:.*empty command in input.*" err
1172 '
1173
1174 test_expect_success 'batch-command whitespace before command' '
1175 echo " info deadbeef" >cmd &&
1176 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1177 grep "^fatal:.*whitespace before command.*" err
1178 '
1179
1180 test_expect_success 'batch-command unknown command' '
1181 echo unknown_command >cmd &&
1182 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1183 grep "^fatal:.*unknown command.*" err
1184 '
1185
1186 test_expect_success 'batch-command missing arguments' '
1187 echo "info" >cmd &&
1188 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1189 grep "^fatal:.*info requires arguments.*" err
1190 '
1191
1192 test_expect_success 'batch-command flush with arguments' '
1193 echo "flush arg" >cmd &&
1194 test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
1195 grep "^fatal:.*flush takes no arguments.*" err
1196 '
1197
1198 test_expect_success 'batch-command flush without --buffer' '
1199 echo "flush" >cmd &&
1200 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1201 grep "^fatal:.*flush is only for --buffer mode.*" err
1202 '
1203
1204 test_done