]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1006-cat-file.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[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
AR
294commit_content="tree $tree_sha1
295author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
296committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
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"
307tag_content="$tag_header_without_timestamp 0000000000 +0000
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 &&
606 test_must_be_empty out
607 '
608
609 test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
610 if test "$arg2" = "-p"
611 then
612 cat >expect.err <<-EOF
613 fatal: Not a valid object name $(test_oid deadbeef)
614 EOF
615 else
616 cat >expect.err <<-\EOF
617 fatal: git cat-file: could not get object info
618 EOF
619 fi &&
620 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
621 test_must_be_empty out &&
622 test_cmp expect.err err.actual
623 '
624 done
625done
626
dd45a562
ÆAB
627test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
628 git cat-file -e $bogus_short_sha1
629'
630
631test_expect_success '-e can not be combined with --allow-unknown-type' '
632 test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_sha1
633'
634
635test_expect_success '-p cannot print a broken object even with --allow-unknown-type' '
636 test_must_fail git cat-file -p $bogus_short_sha1 &&
637 test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_sha1
638'
639
640test_expect_success '<type> <hash> does not work with objects of broken types' '
641 cat >err.expect <<-\EOF &&
642 fatal: invalid object type "bogus"
643 EOF
644 test_must_fail git cat-file $bogus_short_type $bogus_short_sha1 2>err.actual &&
645 test_cmp err.expect err.actual
646'
647
648test_expect_success 'broken types combined with --batch and --batch-check' '
649 echo $bogus_short_sha1 >bogus-oid &&
650
651 cat >err.expect <<-\EOF &&
652 fatal: invalid object type
653 EOF
654
655 test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
656 test_cmp err.expect err.actual &&
657
658 test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
659 test_cmp err.expect err.actual
660'
661
662test_expect_success 'the --batch and --batch-check options do not combine with --allow-unknown-type' '
663 test_expect_code 128 git cat-file --batch --allow-unknown-type <bogus-oid &&
664 test_expect_code 128 git cat-file --batch-check --allow-unknown-type <bogus-oid
665'
666
667test_expect_success 'the --allow-unknown-type option does not consider replacement refs' '
668 cat >expect <<-EOF &&
669 $bogus_short_type
670 EOF
671 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
672 test_cmp expect actual &&
673
674 # Create it manually, as "git replace" will die on bogus
675 # types.
676 head=$(git rev-parse --verify HEAD) &&
e9706a18
HWN
677 test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_sha1" &&
678 test-tool ref-store main update-ref msg "refs/replace/$bogus_short_sha1" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
dd45a562
ÆAB
679
680 cat >expect <<-EOF &&
681 commit
682 EOF
683 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
684 test_cmp expect actual
685'
3e370f9f
KN
686
687test_expect_success "Type of broken object is correct" '
70e4a577
ÆAB
688 echo $bogus_short_type >expect &&
689 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
3e370f9f
KN
690 test_cmp expect actual
691'
692
693test_expect_success "Size of broken object is correct" '
70e4a577
ÆAB
694 echo $bogus_short_size >expect &&
695 git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual &&
3e370f9f
KN
696 test_cmp expect actual
697'
e879295b
JK
698
699test_expect_success 'clean up broken object' '
061a21d3 700 rm .git/objects/$(test_oid_to_path $bogus_short_sha1)
e879295b
JK
701'
702
3e370f9f 703test_expect_success "Type of broken object is correct when type is large" '
70e4a577
ÆAB
704 echo $bogus_long_type >expect &&
705 git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual &&
3e370f9f
KN
706 test_cmp expect actual
707'
708
709test_expect_success "Size of large broken object is correct when type is large" '
70e4a577
ÆAB
710 echo $bogus_long_size >expect &&
711 git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual &&
3e370f9f
KN
712 test_cmp expect actual
713'
714
e879295b 715test_expect_success 'clean up broken object' '
061a21d3
JH
716 rm .git/objects/$(test_oid_to_path $bogus_long_sha1)
717'
718
7e7d220d
ÆAB
719test_expect_success 'cat-file -t and -s on corrupt loose object' '
720 git init --bare corrupt-loose.git &&
721 (
722 cd corrupt-loose.git &&
723
724 # Setup and create the empty blob and its path
725 empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
4627c67f 726 empty_blob=$(git hash-object -w --stdin </dev/null) &&
7e7d220d
ÆAB
727
728 # Create another blob and its path
729 echo other >other.blob &&
730 other_blob=$(git hash-object -w --stdin <other.blob) &&
731 other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
732
733 # Before the swap the size is 0
734 cat >out.expect <<-EOF &&
735 0
736 EOF
737 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
738 test_must_be_empty err.actual &&
739 test_cmp out.expect out.actual &&
740
741 # Swap the two to corrupt the repository
742 mv -f "$other_path" "$empty_path" &&
743 test_must_fail git fsck 2>err.fsck &&
96e41f58 744 grep "hash-path mismatch" err.fsck &&
7e7d220d
ÆAB
745
746 # confirm that cat-file is reading the new swapped-in
747 # blob...
748 cat >out.expect <<-EOF &&
749 blob
750 EOF
751 git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
752 test_must_be_empty err.actual &&
753 test_cmp out.expect out.actual &&
754
755 # ... since it has a different size now.
756 cat >out.expect <<-EOF &&
757 6
758 EOF
759 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
760 test_must_be_empty err.actual &&
761 test_cmp out.expect out.actual &&
762
763 # So far "cat-file" has been happy to spew the found
764 # content out as-is. Try to make it zlib-invalid.
765 mv -f other.blob "$empty_path" &&
766 test_must_fail git fsck 2>err.fsck &&
4627c67f
ÆAB
767 cat >expect <<-EOF &&
768 error: inflate: data stream error (incorrect header check)
769 error: unable to unpack header of ./$empty_path
770 error: $empty_blob: object corrupt or missing: ./$empty_path
771 EOF
772 grep "^error: " err.fsck >actual &&
773 test_cmp expect actual
7e7d220d 774 )
e879295b
JK
775'
776
122d5346
DT
777# Tests for git cat-file --follow-symlinks
778test_expect_success 'prep for symlink tests' '
779 echo_without_newline "$hello_content" >morx &&
780 test_ln_s_add morx same-dir-link &&
781 test_ln_s_add dir link-to-dir &&
782 test_ln_s_add ../fleem out-of-repo-link &&
783 test_ln_s_add .. out-of-repo-link-dir &&
784 test_ln_s_add same-dir-link link-to-link &&
785 test_ln_s_add nope broken-same-dir-link &&
786 mkdir dir &&
787 test_ln_s_add ../morx dir/parent-dir-link &&
788 test_ln_s_add .. dir/link-dir &&
789 test_ln_s_add ../../escape dir/out-of-repo-link &&
790 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
791 test_ln_s_add nope dir/broken-link-in-dir &&
792 mkdir dir/subdir &&
793 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
794 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
795 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
796 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
797 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
798 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
799 echo_without_newline "$hello_content" >dir/ind1 &&
800 test_ln_s_add dir dirlink &&
801 test_ln_s_add dir/subdir subdirlink &&
802 test_ln_s_add subdir/ind2 dir/link-to-child &&
803 test_ln_s_add dir/link-to-child link-to-down-link &&
804 test_ln_s_add dir/.. up-down &&
805 test_ln_s_add dir/../ up-down-trailing &&
806 test_ln_s_add dir/../morx up-down-file &&
807 test_ln_s_add dir/../../morx up-up-down-file &&
808 test_ln_s_add subdirlink/../../morx up-two-down-file &&
809 test_ln_s_add loop1 loop2 &&
810 test_ln_s_add loop2 loop1 &&
811 git add morx dir/subdir/ind2 dir/ind1 &&
812 git commit -am "test" &&
813 echo $hello_sha1 blob $hello_size >found
814'
815
816test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
817 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
818 test_cmp found actual &&
819 echo HEAD:nope missing >expect &&
820 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
821 test_cmp expect actual
822'
823
824test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
825 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
826 test_cmp found actual
827'
828
829test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
830 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
831 test_cmp found actual
832'
833
834
835test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
836 echo dangling 25 >expect &&
837 echo HEAD:broken-same-dir-link >>expect &&
838 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
839 test_cmp expect actual
840'
841
842test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
843 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
844 test_cmp found actual
845'
846
847test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
848 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
849 test_cmp found actual &&
850 echo notdir 29 >expect &&
851 echo HEAD:dir/parent-dir-link/nope >>expect &&
852 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
853 test_cmp expect actual
854'
855
856test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
857 echo dangling 22 >expect &&
858 echo HEAD:dir/link-dir/nope >>expect &&
859 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
860 test_cmp expect actual &&
861 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
862 test_cmp found actual &&
863 echo dangling 27 >expect &&
864 echo HEAD:dir/broken-link-in-dir >>expect &&
865 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
866 test_cmp expect actual
867'
868
869test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
870 echo notdir 41 >expect &&
871 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
872 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
873 test_cmp expect actual &&
874 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
875 test_cmp found actual &&
876 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
877 test_cmp found actual
878'
879
880test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
881 echo dangling 17 >expect &&
882 echo HEAD:dirlink/morx >>expect &&
883 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
884 test_cmp expect actual &&
885 echo $hello_sha1 blob $hello_size >expect &&
886 echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
887 test_cmp expect actual
888'
889
890test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
891 echo dangling 20 >expect &&
892 echo HEAD:subdirlink/morx >>expect &&
893 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
894 test_cmp expect actual &&
895 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
896 test_cmp found actual
897'
898
899test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
900 echo notdir 27 >expect &&
901 echo HEAD:dir/link-to-child/morx >>expect &&
902 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
903 test_cmp expect actual &&
904 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
905 test_cmp found actual &&
906 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
907 test_cmp found actual
908'
909
910test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
911 echo symlink 8 >expect &&
912 echo ../fleem >>expect &&
913 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
914 test_cmp expect actual &&
915 echo symlink 2 >expect &&
916 echo .. >>expect &&
917 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
918 test_cmp expect actual
919'
920
921test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
922 echo symlink 9 >expect &&
923 echo ../escape >>expect &&
924 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
925 test_cmp expect actual &&
926 echo symlink 2 >expect &&
927 echo .. >>expect &&
928 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
929 test_cmp expect actual
930'
931
932test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
933 echo symlink 15 >expect &&
934 echo ../great-escape >>expect &&
935 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
936 test_cmp expect actual &&
937 echo symlink 2 >expect &&
938 echo .. >>expect &&
939 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
940 test_cmp expect actual &&
941 echo symlink 3 >expect &&
942 echo ../ >>expect &&
943 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
944 test_cmp expect actual
945'
946
947test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
948 echo HEAD: | git cat-file --batch-check >expect &&
949 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
950 test_cmp expect actual &&
951 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
952 test_cmp expect actual &&
953 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
954 test_cmp found actual &&
955 echo symlink 7 >expect &&
956 echo ../morx >>expect &&
957 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
958 test_cmp expect actual &&
959 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
960 test_cmp found actual
961'
962
963test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
964 echo loop 10 >expect &&
965 echo HEAD:loop1 >>expect &&
966 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
967 test_cmp expect actual
968'
969
970test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
971 echo HEAD:morx | git cat-file --batch >expect &&
972 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
973 test_cmp expect actual
974'
67f0b6f3 975
6a951937 976test_expect_success 'cat-file --batch-all-objects shows all objects' '
3115ee45 977 # make new repos so we know the full set of objects; we will
6a951937 978 # also make sure that there are some packed and some loose
aa2f5ef5
JK
979 # objects, some referenced and some not, some duplicates, and that
980 # there are some available only via alternates.
6a951937
JK
981 git init all-one &&
982 (
983 cd all-one &&
984 echo content >file &&
985 git add file &&
986 git commit -qm base &&
987 git rev-parse HEAD HEAD^{tree} HEAD:file &&
988 git repack -ad &&
989 echo not-cloned | git hash-object -w --stdin
990 ) >expect.unsorted &&
991 git clone -s all-one all-two &&
992 (
993 cd all-two &&
994 echo local-unref | git hash-object -w --stdin
995 ) >>expect.unsorted &&
aa2f5ef5
JK
996 git -C all-two rev-parse HEAD:file |
997 git -C all-two pack-objects .git/objects/pack/pack &&
6a951937
JK
998 sort <expect.unsorted >expect &&
999 git -C all-two cat-file --batch-all-objects \
3115ee45 1000 --batch-check="%(objectname)" >actual &&
6a951937
JK
1001 test_cmp expect actual
1002'
1003
0750bb5b
JK
1004# The only user-visible difference is that the objects are no longer sorted,
1005# and the resulting sort order is undefined. So we can only check that it
1006# produces the same objects as the ordered case, but that at least exercises
1007# the code.
1008test_expect_success 'cat-file --unordered works' '
1009 git -C all-two cat-file --batch-all-objects --unordered \
1010 --batch-check="%(objectname)" >actual.unsorted &&
1011 sort <actual.unsorted >actual &&
1012 test_cmp expect actual
1013'
1014
e16acc80
ZH
1015test_expect_success 'set up object list for --batch-all-objects tests' '
1016 git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects
1017'
1018
1019test_expect_success 'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
1020 git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
1021 git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
1022 cmp expect actual
1023'
1024
1025test_expect_success 'cat-file --batch="%(rest)" with --batch-all-objects will work' '
1026 git -C all-two cat-file --batch="%(rest)" <objects >expect &&
1027 git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
1028 cmp expect actual
1029'
1030
1031test_expect_success 'cat-file --batch="batman" with --batch-all-objects will work' '
1032 git -C all-two cat-file --batch="batman" <objects >expect &&
1033 git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
1034 cmp expect actual
1035'
1036
5c5b29b4
JK
1037test_expect_success 'set up replacement object' '
1038 orig=$(git rev-parse HEAD) &&
1039 git cat-file commit $orig >orig &&
1040 {
1041 cat orig &&
1042 echo extra
1043 } >fake &&
1044 fake=$(git hash-object -t commit -w fake) &&
1045 orig_size=$(git cat-file -s $orig) &&
1046 fake_size=$(git cat-file -s $fake) &&
1047 git replace $orig $fake
1048'
1049
1050test_expect_success 'cat-file --batch respects replace objects' '
1051 git cat-file --batch >actual <<-EOF &&
1052 $orig
1053 EOF
1054 {
1055 echo "$orig commit $fake_size" &&
1056 cat fake &&
1057 echo
1058 } >expect &&
1059 test_cmp expect actual
1060'
1061
1062test_expect_success 'cat-file --batch-check respects replace objects' '
1063 git cat-file --batch-check >actual <<-EOF &&
1064 $orig
1065 EOF
1066 echo "$orig commit $fake_size" >expect &&
1067 test_cmp expect actual
1068'
1069
1070# Pull the entry for object with oid "$1" out of the output of
1071# "cat-file --batch", including its object content (which requires
1072# parsing and reading a set amount of bytes, hence perl).
1073extract_batch_output () {
1074 perl -ne '
1075 BEGIN { $oid = shift }
1076 if (/^$oid \S+ (\d+)$/) {
1077 print;
1078 read STDIN, my $buf, $1;
1079 print $buf;
1080 print "\n";
1081 }
1082 ' "$@"
1083}
1084
1085test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
1086 git cat-file --batch-all-objects --batch >actual.raw &&
1087 extract_batch_output $orig <actual.raw >actual &&
1088 {
1089 echo "$orig commit $orig_size" &&
1090 cat orig &&
1091 echo
1092 } >expect &&
1093 test_cmp expect actual
1094'
1095
1096test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
1097 git cat-file --batch-all-objects --batch-check >actual.raw &&
1098 grep ^$orig actual.raw >actual &&
1099 echo "$orig commit $orig_size" >expect &&
1100 test_cmp expect actual
1101'
440c705e
JC
1102test_expect_success 'batch-command empty command' '
1103 echo "" >cmd &&
1104 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1105 grep "^fatal:.*empty command in input.*" err
1106'
1107
1108test_expect_success 'batch-command whitespace before command' '
1109 echo " info deadbeef" >cmd &&
1110 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1111 grep "^fatal:.*whitespace before command.*" err
1112'
1113
1114test_expect_success 'batch-command unknown command' '
1115 echo unknown_command >cmd &&
1116 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1117 grep "^fatal:.*unknown command.*" err
1118'
1119
1120test_expect_success 'batch-command missing arguments' '
1121 echo "info" >cmd &&
1122 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1123 grep "^fatal:.*info requires arguments.*" err
1124'
1125
1126test_expect_success 'batch-command flush with arguments' '
1127 echo "flush arg" >cmd &&
1128 test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
1129 grep "^fatal:.*flush takes no arguments.*" err
1130'
1131
1132test_expect_success 'batch-command flush without --buffer' '
1133 echo "flush" >cmd &&
1134 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1135 grep "^fatal:.*flush is only for --buffer mode.*" err
1136'
5c5b29b4 1137
b335d3f1 1138test_done