]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1006-cat-file.sh
Merge branch 'rs/parse-options-with-keep-unknown-abbrev-fix'
[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 &&
7854bf49 9 grep "^error: .* cannot be used together" err
ddf8420b
ÆAB
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 91 --batch-all-objects \
f79e1884
PS
92 -z \
93 -Z
ddf8420b 94do
b3fe4680
ÆAB
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
ddf8420b
ÆAB
98 '
99done
100
b335d3f1
AR
101echo_without_newline () {
102 printf '%s' "$*"
103}
104
db9d67f2
TB
105echo_without_newline_nul () {
106 echo_without_newline "$@" | tr '\n' '\0'
107}
108
b335d3f1
AR
109strlen () {
110 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
111}
112
b335d3f1
AR
113run_tests () {
114 type=$1
115 sha1=$2
116 size=$3
117 content=$4
118 pretty_content=$5
b335d3f1 119
a8128ed6
AR
120 batch_output="$sha1 $type $size
121$content"
122
b335d3f1
AR
123 test_expect_success "$type exists" '
124 git cat-file -e $sha1
125 '
126
127 test_expect_success "Type of $type is correct" '
03c893cb
JK
128 echo $type >expect &&
129 git cat-file -t $sha1 >actual &&
130 test_cmp expect actual
b335d3f1
AR
131 '
132
133 test_expect_success "Size of $type is correct" '
03c893cb
JK
134 echo $size >expect &&
135 git cat-file -s $sha1 >actual &&
136 test_cmp expect actual
b335d3f1
AR
137 '
138
3e370f9f
KN
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
b335d3f1
AR
151 test -z "$content" ||
152 test_expect_success "Content of $type is correct" '
c7309f63
PS
153 echo_without_newline "$content" >expect &&
154 git cat-file $type $sha1 >actual &&
03c893cb 155 test_cmp expect actual
b335d3f1
AR
156 '
157
158 test_expect_success "Pretty content of $type is correct" '
c7309f63
PS
159 echo_without_newline "$pretty_content" >expect &&
160 git cat-file -p $sha1 >actual &&
03c893cb 161 test_cmp expect actual
b335d3f1 162 '
05d5667f 163
a8128ed6
AR
164 test -z "$content" ||
165 test_expect_success "--batch output of $type is correct" '
c7309f63
PS
166 echo "$batch_output" >expect &&
167 echo $sha1 | git cat-file --batch >actual &&
03c893cb 168 test_cmp expect actual
a8128ed6
AR
169 '
170
05d5667f 171 test_expect_success "--batch-check output of $type is correct" '
03c893cb
JK
172 echo "$sha1 $type $size" >expect &&
173 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
174 test_cmp expect actual
05d5667f 175 '
93d2a607 176
440c705e
JC
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" '
c7309f63
PS
181 echo "$batch_output" >expect &&
182 test_write_lines "contents $sha1" | git cat-file --batch-command $opt >actual &&
440c705e
JC
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
93d2a607
JK
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 '
97be0407 199
440c705e
JC
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
97be0407
JK
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 '
6554dfa9
JK
212
213 test -z "$content" ||
214 test_expect_success "--batch without type ($type)" '
215 {
216 echo "$size" &&
c7309f63 217 echo "$content"
6554dfa9 218 } >expect &&
c7309f63 219 echo $sha1 | git cat-file --batch="%(objectsize)" >actual &&
6554dfa9
JK
220 test_cmp expect actual
221 '
222
223 test -z "$content" ||
224 test_expect_success "--batch without size ($type)" '
225 {
226 echo "$type" &&
c7309f63 227 echo "$content"
6554dfa9 228 } >expect &&
c7309f63 229 echo $sha1 | git cat-file --batch="%(objecttype)" >actual &&
6554dfa9
JK
230 test_cmp expect actual
231 '
b335d3f1
AR
232}
233
234hello_content="Hello World"
235hello_size=$(strlen "$hello_content")
236hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
237
238test_expect_success "setup" '
239 echo_without_newline "$hello_content" > hello &&
240 git update-index --add hello
241'
242
243run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
244
440c705e
JC
245test_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
253test_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
97be0407
JK
261test_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
b335d3f1 269tree_sha1=$(git write-tree)
e95f5313 270tree_size=$(($(test_oid rawsz) + 13))
c7309f63 271tree_pretty_content="100644 blob $hello_sha1 hello${LF}"
b335d3f1
AR
272
273run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
274
41ccfdd9 275commit_message="Initial commit"
b335d3f1 276commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
e95f5313 277commit_size=$(($(test_oid hexsz) + 137))
b335d3f1 278commit_content="tree $tree_sha1
c7309f63
PS
279author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE
280committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
b335d3f1
AR
281
282$commit_message"
283
c7309f63 284run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content"
b335d3f1
AR
285
286tag_header_without_timestamp="object $hello_sha1
287type blob
288tag hellotag
289tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
290tag_description="This is a tag"
61cc4be7 291tag_content="$tag_header_without_timestamp 0 +0000
b335d3f1 292
b335d3f1
AR
293$tag_description"
294
acf9de4c 295tag_sha1=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
b335d3f1
AR
296tag_size=$(strlen "$tag_content")
297
c7309f63 298run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content"
b335d3f1 299
b116c773
PS
300test_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'
b335d3f1 305
440c705e 306for batch in batch batch-check batch-command
05d5667f 307do
a8128ed6
AR
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
05d5667f
AR
321 '
322
a8128ed6
AR
323 test_expect_success "Passing --$batch with <type> fails" '
324 test_must_fail git cat-file blob --$batch $hello_sha1
05d5667f 325 '
05d5667f 326
a8128ed6
AR
327 test_expect_success "Passing sha1 with --$batch fails" '
328 test_must_fail git cat-file --$batch $hello_sha1
329 '
330done
05d5667f 331
122d5346
DT
332for opt in t s e p
333do
334 test_expect_success "Passing -$opt with --follow-symlinks fails" '
335 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
336 '
337done
338
3c076dbe 339test_expect_success "--batch-check for a non-existent named object" '
b116c773
PS
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
3c076dbe
LW
348'
349
350test_expect_success "--batch-check for a non-existent hash" '
b116c773
PS
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
3c076dbe
LW
359'
360
361test_expect_success "--batch for an existent and a non-existent hash" '
b116c773
PS
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
05d5667f
AR
371'
372
2e3a16b2 373test_expect_success "--batch-check for an empty line" '
b116c773
PS
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
05d5667f
AR
381'
382
4ef8d1dd 383test_expect_success 'empty --batch-check notices missing object' '
8125a58b 384 echo "$ZERO_OID missing" >expect &&
385 echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
4ef8d1dd
JH
386 test_cmp expect actual
387'
388
a8128ed6
AR
389batch_input="$hello_sha1
390$commit_sha1
391$tag_sha1
392deadbeef
393
394"
395
f79e1884
PS
396printf "%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
a8128ed6 405
6c41e21d 406test_expect_success '--batch with multiple sha1s gives correct format' '
f79e1884 407 tr "\0" "\n" <batch_output >expect &&
b116c773
PS
408 echo_without_newline "$batch_input" >in &&
409 git cat-file --batch <in >actual &&
c7309f63 410 test_cmp expect actual
6c41e21d 411'
a8128ed6 412
db9d67f2
TB
413test_expect_success '--batch, -z with multiple sha1s gives correct format' '
414 echo_without_newline_nul "$batch_input" >in &&
f79e1884 415 tr "\0" "\n" <batch_output >expect &&
c7309f63
PS
416 git cat-file --batch -z <in >actual &&
417 test_cmp expect actual
db9d67f2
TB
418'
419
f79e1884
PS
420test_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
05d5667f
AR
426batch_check_input="$hello_sha1
427$tree_sha1
428$commit_sha1
429$tag_sha1
430deadbeef
431
432"
433
f79e1884
PS
434printf "%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
05d5667f
AR
441
442test_expect_success "--batch-check with multiple sha1s gives correct format" '
f79e1884 443 tr "\0" "\n" <batch_check_output >expect &&
b116c773
PS
444 echo_without_newline "$batch_check_input" >in &&
445 git cat-file --batch-check <in >actual &&
446 test_cmp expect actual
05d5667f
AR
447'
448
db9d67f2 449test_expect_success "--batch-check, -z with multiple sha1s gives correct format" '
f79e1884 450 tr "\0" "\n" <batch_check_output >expect &&
b116c773
PS
451 echo_without_newline_nul "$batch_check_input" >in &&
452 git cat-file --batch-check -z <in >actual &&
453 test_cmp expect actual
db9d67f2
TB
454'
455
f79e1884
PS
456test_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
462test_expect_success FUNNYNAMES 'setup with newline in input' '
db9d67f2
TB
463 touch -- "newline${LF}embedded" &&
464 git add -- "newline${LF}embedded" &&
465 git commit -m "file with newline embedded" &&
466 test_tick &&
467
f79e1884
PS
468 printf "HEAD:newline${LF}embedded" >in
469'
db9d67f2 470
f79e1884
PS
471test_expect_success FUNNYNAMES '--batch-check, -z with newline in input' '
472 git cat-file --batch-check -z <in >actual &&
db9d67f2
TB
473 echo "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
474 test_cmp expect actual
475'
476
f79e1884
PS
477test_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
3639fefe
TB
483batch_command_multiple_info="info $hello_sha1
484info $tree_sha1
485info $commit_sha1
486info $tag_sha1
487info deadbeef"
488
440c705e
JC
489test_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
3639fefe
TB
498 echo "$batch_command_multiple_info" >in &&
499 git cat-file --batch-command --buffer <in >actual &&
440c705e 500
db9d67f2
TB
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
f79e1884
PS
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
440c705e
JC
513'
514
3639fefe
TB
515batch_command_multiple_contents="contents $hello_sha1
516contents $commit_sha1
517contents $tag_sha1
518contents deadbeef
519flush"
520
440c705e 521test_expect_success '--batch-command with multiple command calls gives correct format' '
f79e1884
PS
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 &&
440c705e 531
3639fefe 532 echo "$batch_command_multiple_contents" >in &&
c7309f63 533 git cat-file --batch-command --buffer <in >actual &&
440c705e 534
db9d67f2
TB
535 test_cmp expect actual &&
536
537 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
c7309f63 538 git cat-file --batch-command --buffer -z <in >actual &&
db9d67f2 539
f79e1884
PS
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
440c705e
JC
546'
547
65ea9c3c 548test_expect_success 'setup blobs which are likely to delta' '
c680668d 549 test-tool genrandom foo 10240 >foo &&
7abcbcb7 550 { cat foo && echo plus; } >foo-plus &&
65ea9c3c
JK
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
559test_expect_success 'confirm that neither loose blob is a delta' '
99094a7a 560 cat >expect <<-EOF &&
8125a58b 561 $ZERO_OID
562 $ZERO_OID
65ea9c3c
JK
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.
573test_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
70e4a577
ÆAB
582test_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'
3e370f9f 593
59b8283d
ÆAB
594for arg1 in '' --allow-unknown-type
595do
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
5848fb11 623 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
59b8283d
ÆAB
624 fatal: Not a valid object name $bogus_long_sha1
625 EOF
626 else
627 cat >expect <<-EOF
5848fb11 628 error: header for $bogus_long_sha1 too long, exceeds 32 bytes
59b8283d
ÆAB
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 &&
4e273368
AR
648 test_must_be_empty out &&
649 test_cmp expect.err err.actual
59b8283d
ÆAB
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
668done
669
dd45a562
ÆAB
670test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
671 git cat-file -e $bogus_short_sha1
672'
673
674test_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
678test_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
683test_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
691test_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
705test_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
710test_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) &&
e9706a18
HWN
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 &&
dd45a562
ÆAB
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'
3e370f9f
KN
729
730test_expect_success "Type of broken object is correct" '
70e4a577
ÆAB
731 echo $bogus_short_type >expect &&
732 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
3e370f9f
KN
733 test_cmp expect actual
734'
735
736test_expect_success "Size of broken object is correct" '
70e4a577
ÆAB
737 echo $bogus_short_size >expect &&
738 git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual &&
3e370f9f
KN
739 test_cmp expect actual
740'
e879295b
JK
741
742test_expect_success 'clean up broken object' '
061a21d3 743 rm .git/objects/$(test_oid_to_path $bogus_short_sha1)
e879295b
JK
744'
745
3e370f9f 746test_expect_success "Type of broken object is correct when type is large" '
70e4a577
ÆAB
747 echo $bogus_long_type >expect &&
748 git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual &&
3e370f9f
KN
749 test_cmp expect actual
750'
751
752test_expect_success "Size of large broken object is correct when type is large" '
70e4a577
ÆAB
753 echo $bogus_long_size >expect &&
754 git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual &&
3e370f9f
KN
755 test_cmp expect actual
756'
757
e879295b 758test_expect_success 'clean up broken object' '
061a21d3
JH
759 rm .git/objects/$(test_oid_to_path $bogus_long_sha1)
760'
761
7e7d220d
ÆAB
762test_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")) &&
4627c67f 769 empty_blob=$(git hash-object -w --stdin </dev/null) &&
7e7d220d
ÆAB
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 &&
96e41f58 787 grep "hash-path mismatch" err.fsck &&
7e7d220d
ÆAB
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 &&
4627c67f
ÆAB
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
7e7d220d 817 )
e879295b
JK
818'
819
122d5346
DT
820# Tests for git cat-file --follow-symlinks
821test_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
859test_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
867test_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
872test_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
878test_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
f79e1884
PS
885test_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
122d5346
DT
892test_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
897test_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
f79e1884
PS
906test_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
122d5346
DT
915test_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
928test_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
939test_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
949test_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
958test_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
969test_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
980test_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
991test_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
1006test_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
1022test_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
f79e1884
PS
1029test_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
122d5346
DT
1036test_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'
67f0b6f3 1041
6a951937 1042test_expect_success 'cat-file --batch-all-objects shows all objects' '
3115ee45 1043 # make new repos so we know the full set of objects; we will
6a951937 1044 # also make sure that there are some packed and some loose
aa2f5ef5
JK
1045 # objects, some referenced and some not, some duplicates, and that
1046 # there are some available only via alternates.
6a951937
JK
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 &&
aa2f5ef5
JK
1062 git -C all-two rev-parse HEAD:file |
1063 git -C all-two pack-objects .git/objects/pack/pack &&
6a951937
JK
1064 sort <expect.unsorted >expect &&
1065 git -C all-two cat-file --batch-all-objects \
3115ee45 1066 --batch-check="%(objectname)" >actual &&
6a951937
JK
1067 test_cmp expect actual
1068'
1069
0750bb5b
JK
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.
1074test_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
e16acc80
ZH
1081test_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
1085test_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
1091test_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
1097test_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
f5461512
JK
1103test_expect_success 'cat-file %(objectsize:disk) with --batch-all-objects' '
1104 # our state has both loose and packed objects,
1105 # so find both for our expected output
1106 {
1107 find .git/objects/?? -type f |
1108 awk -F/ "{ print \$0, \$3\$4 }" |
1109 while read path oid
1110 do
1111 size=$(test_file_size "$path") &&
1112 echo "$oid $size" ||
1113 return 1
1114 done &&
1115 rawsz=$(test_oid rawsz) &&
1116 find .git/objects/pack -name "*.idx" |
1117 while read idx
1118 do
1119 git show-index <"$idx" >idx.raw &&
54d8a253 1120 sort -nr <idx.raw >idx.sorted &&
f5461512
JK
1121 packsz=$(test_file_size "${idx%.idx}.pack") &&
1122 end=$((packsz - rawsz)) &&
54d8a253
RS
1123 while read start oid rest
1124 do
1125 size=$((end - start)) &&
1126 end=$start &&
1127 echo "$oid $size" ||
1128 return 1
1129 done <idx.sorted ||
f5461512
JK
1130 return 1
1131 done
1132 } >expect.raw &&
1133 sort <expect.raw >expect &&
1134 git cat-file --batch-all-objects \
1135 --batch-check="%(objectname) %(objectsize:disk)" >actual &&
1136 test_cmp expect actual
1137'
1138
5c5b29b4
JK
1139test_expect_success 'set up replacement object' '
1140 orig=$(git rev-parse HEAD) &&
1141 git cat-file commit $orig >orig &&
1142 {
1143 cat orig &&
1144 echo extra
1145 } >fake &&
1146 fake=$(git hash-object -t commit -w fake) &&
1147 orig_size=$(git cat-file -s $orig) &&
1148 fake_size=$(git cat-file -s $fake) &&
1149 git replace $orig $fake
1150'
1151
1152test_expect_success 'cat-file --batch respects replace objects' '
1153 git cat-file --batch >actual <<-EOF &&
1154 $orig
1155 EOF
1156 {
1157 echo "$orig commit $fake_size" &&
1158 cat fake &&
1159 echo
1160 } >expect &&
1161 test_cmp expect actual
1162'
1163
1164test_expect_success 'cat-file --batch-check respects replace objects' '
1165 git cat-file --batch-check >actual <<-EOF &&
1166 $orig
1167 EOF
1168 echo "$orig commit $fake_size" >expect &&
1169 test_cmp expect actual
1170'
1171
1172# Pull the entry for object with oid "$1" out of the output of
1173# "cat-file --batch", including its object content (which requires
1174# parsing and reading a set amount of bytes, hence perl).
1175extract_batch_output () {
1176 perl -ne '
1177 BEGIN { $oid = shift }
1178 if (/^$oid \S+ (\d+)$/) {
1179 print;
1180 read STDIN, my $buf, $1;
1181 print $buf;
1182 print "\n";
1183 }
1184 ' "$@"
1185}
1186
1187test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
1188 git cat-file --batch-all-objects --batch >actual.raw &&
1189 extract_batch_output $orig <actual.raw >actual &&
1190 {
1191 echo "$orig commit $orig_size" &&
1192 cat orig &&
1193 echo
1194 } >expect &&
1195 test_cmp expect actual
1196'
1197
1198test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
1199 git cat-file --batch-all-objects --batch-check >actual.raw &&
1200 grep ^$orig actual.raw >actual &&
1201 echo "$orig commit $orig_size" >expect &&
1202 test_cmp expect actual
1203'
440c705e
JC
1204test_expect_success 'batch-command empty command' '
1205 echo "" >cmd &&
1206 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1207 grep "^fatal:.*empty command in input.*" err
1208'
1209
1210test_expect_success 'batch-command whitespace before command' '
1211 echo " info deadbeef" >cmd &&
1212 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1213 grep "^fatal:.*whitespace before command.*" err
1214'
1215
1216test_expect_success 'batch-command unknown command' '
1217 echo unknown_command >cmd &&
1218 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1219 grep "^fatal:.*unknown command.*" err
1220'
1221
1222test_expect_success 'batch-command missing arguments' '
1223 echo "info" >cmd &&
1224 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1225 grep "^fatal:.*info requires arguments.*" err
1226'
1227
1228test_expect_success 'batch-command flush with arguments' '
1229 echo "flush arg" >cmd &&
1230 test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
1231 grep "^fatal:.*flush takes no arguments.*" err
1232'
1233
1234test_expect_success 'batch-command flush without --buffer' '
1235 echo "flush" >cmd &&
1236 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1237 grep "^fatal:.*flush is only for --buffer mode.*" err
1238'
5c5b29b4 1239
b335d3f1 1240test_done