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