]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1006-cat-file.sh
Start the 2.46 cycle
[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
baab175c 115 oid=$2
b335d3f1
AR
116 size=$3
117 content=$4
118 pretty_content=$5
b335d3f1 119
baab175c 120 batch_output="$oid $type $size
a8128ed6
AR
121$content"
122
b335d3f1 123 test_expect_success "$type exists" '
baab175c 124 git cat-file -e $oid
b335d3f1
AR
125 '
126
127 test_expect_success "Type of $type is correct" '
03c893cb 128 echo $type >expect &&
baab175c 129 git cat-file -t $oid >actual &&
03c893cb 130 test_cmp expect actual
b335d3f1
AR
131 '
132
133 test_expect_success "Size of $type is correct" '
03c893cb 134 echo $size >expect &&
baab175c 135 git cat-file -s $oid >actual &&
03c893cb 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 &&
baab175c 141 git cat-file -t --allow-unknown-type $oid >actual &&
3e370f9f
KN
142 test_cmp expect actual
143 '
144
145 test_expect_success "Size of $type is correct using --allow-unknown-type" '
146 echo $size >expect &&
baab175c 147 git cat-file -s --allow-unknown-type $oid >actual &&
3e370f9f
KN
148 test_cmp expect actual
149 '
150
b335d3f1
AR
151 test -z "$content" ||
152 test_expect_success "Content of $type is correct" '
c7309f63 153 echo_without_newline "$content" >expect &&
baab175c 154 git cat-file $type $oid >actual &&
03c893cb 155 test_cmp expect actual
b335d3f1
AR
156 '
157
158 test_expect_success "Pretty content of $type is correct" '
c7309f63 159 echo_without_newline "$pretty_content" >expect &&
baab175c 160 git cat-file -p $oid >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 166 echo "$batch_output" >expect &&
baab175c 167 echo $oid | 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" '
baab175c
EB
172 echo "$oid $type $size" >expect &&
173 echo_without_newline $oid | git cat-file --batch-check >actual &&
03c893cb 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 181 echo "$batch_output" >expect &&
baab175c 182 test_write_lines "contents $oid" | 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" '
baab175c
EB
187 echo "$oid $type $size" >expect &&
188 test_write_lines "info $oid" |
440c705e
JC
189 git cat-file --batch-command $opt >actual &&
190 test_cmp expect actual
191 '
192 done
193
93d2a607 194 test_expect_success "custom --batch-check format" '
baab175c
EB
195 echo "$type $oid" >expect &&
196 echo $oid | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
93d2a607
JK
197 test_cmp expect actual
198 '
97be0407 199
440c705e 200 test_expect_success "custom --batch-command format" '
baab175c
EB
201 echo "$type $oid" >expect &&
202 echo "info $oid" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
440c705e
JC
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 &&
baab175c 208 echo "$oid this is some extra content" |
97be0407
JK
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 &&
baab175c 219 echo $oid | 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 &&
baab175c 229 echo $oid | 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")
baab175c 236hello_oid=$(echo_without_newline "$hello_content" | git hash-object --stdin)
b335d3f1
AR
237
238test_expect_success "setup" '
3afa8d86
EB
239 git config core.repositoryformatversion 1 &&
240 git config extensions.objectformat $test_hash_algo &&
241 git config extensions.compatobjectformat $test_compat_hash_algo &&
b335d3f1
AR
242 echo_without_newline "$hello_content" > hello &&
243 git update-index --add hello
244'
245
3afa8d86
EB
246run_blob_tests () {
247 oid=$1
b335d3f1 248
3afa8d86
EB
249 run_tests 'blob' $oid $hello_size "$hello_content" "$hello_content"
250
251 test_expect_success '--batch-command --buffer with flush for blob info' '
252 echo "$oid blob $hello_size" >expect &&
253 test_write_lines "info $oid" "flush" |
440c705e
JC
254 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
255 git cat-file --batch-command --buffer >actual &&
256 test_cmp expect actual
3afa8d86 257 '
440c705e 258
3afa8d86 259 test_expect_success '--batch-command --buffer without flush for blob info' '
440c705e 260 touch output &&
3afa8d86 261 test_write_lines "info $oid" |
440c705e
JC
262 GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
263 git cat-file --batch-command --buffer >>output &&
264 test_must_be_empty output
3afa8d86
EB
265 '
266}
267
268hello_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $hello_oid)
269run_blob_tests $hello_oid
270run_blob_tests $hello_compat_oid
440c705e 271
97be0407 272test_expect_success '--batch-check without %(rest) considers whole line' '
baab175c
EB
273 echo "$hello_oid blob $hello_size" >expect &&
274 git update-index --add --cacheinfo 100644 $hello_oid "white space" &&
97be0407
JK
275 test_when_finished "git update-index --remove \"white space\"" &&
276 echo ":white space" | git cat-file --batch-check >actual &&
277 test_cmp expect actual
278'
279
baab175c 280tree_oid=$(git write-tree)
3afa8d86 281tree_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $tree_oid)
e95f5313 282tree_size=$(($(test_oid rawsz) + 13))
3afa8d86 283tree_compat_size=$(($(test_oid --hash=compat rawsz) + 13))
baab175c 284tree_pretty_content="100644 blob $hello_oid hello${LF}"
3afa8d86 285tree_compat_pretty_content="100644 blob $hello_compat_oid hello${LF}"
b335d3f1 286
baab175c 287run_tests 'tree' $tree_oid $tree_size "" "$tree_pretty_content"
3afa8d86 288run_tests 'tree' $tree_compat_oid $tree_compat_size "" "$tree_compat_pretty_content"
b335d3f1 289
41ccfdd9 290commit_message="Initial commit"
baab175c 291commit_oid=$(echo_without_newline "$commit_message" | git commit-tree $tree_oid)
3afa8d86 292commit_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $commit_oid)
e95f5313 293commit_size=$(($(test_oid hexsz) + 137))
3afa8d86 294commit_compat_size=$(($(test_oid --hash=compat hexsz) + 137))
baab175c 295commit_content="tree $tree_oid
c7309f63
PS
296author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE
297committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
b335d3f1
AR
298
299$commit_message"
300
3afa8d86 301commit_compat_content="tree $tree_compat_oid
c7309f63
PS
302author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE
303committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
b335d3f1
AR
304
305$commit_message"
306
baab175c 307run_tests 'commit' $commit_oid $commit_size "$commit_content" "$commit_content"
3afa8d86 308run_tests 'commit' $commit_compat_oid $commit_compat_size "$commit_compat_content" "$commit_compat_content"
b335d3f1 309
3afa8d86 310tag_header_without_oid="type blob
b335d3f1
AR
311tag hellotag
312tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
3afa8d86
EB
313tag_header_without_timestamp="object $hello_oid
314$tag_header_without_oid"
315tag_compat_header_without_timestamp="object $hello_compat_oid
316$tag_header_without_oid"
b335d3f1 317tag_description="This is a tag"
61cc4be7 318tag_content="$tag_header_without_timestamp 0 +0000
b335d3f1 319
b335d3f1 320$tag_description"
3afa8d86 321tag_compat_content="$tag_compat_header_without_timestamp 0 +0000
b335d3f1 322
b335d3f1
AR
323$tag_description"
324
baab175c 325tag_oid=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
b335d3f1
AR
326tag_size=$(strlen "$tag_content")
327
3afa8d86
EB
328tag_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $tag_oid)
329tag_compat_size=$(strlen "$tag_compat_content")
330
baab175c 331run_tests 'tag' $tag_oid $tag_size "$tag_content" "$tag_content"
3afa8d86 332run_tests 'tag' $tag_compat_oid $tag_compat_size "$tag_compat_content" "$tag_compat_content"
b335d3f1 333
b116c773
PS
334test_expect_success "Reach a blob from a tag pointing to it" '
335 echo_without_newline "$hello_content" >expect &&
baab175c 336 git cat-file blob $tag_oid >actual &&
b116c773
PS
337 test_cmp expect actual
338'
b335d3f1 339
3afa8d86 340for oid in $hello_oid $hello_compat_oid
05d5667f 341do
3afa8d86 342 for batch in batch batch-check batch-command
a8128ed6 343 do
3afa8d86
EB
344 for opt in t s e p
345 do
a8128ed6 346 test_expect_success "Passing -$opt with --$batch fails" '
3afa8d86 347 test_must_fail git cat-file --$batch -$opt $oid
a8128ed6
AR
348 '
349
350 test_expect_success "Passing --$batch with -$opt fails" '
3afa8d86 351 test_must_fail git cat-file -$opt --$batch $oid
a8128ed6 352 '
3afa8d86 353 done
a8128ed6 354
3afa8d86
EB
355 test_expect_success "Passing <type> with --$batch fails" '
356 test_must_fail git cat-file --$batch blob $oid
357 '
05d5667f 358
3afa8d86
EB
359 test_expect_success "Passing --$batch with <type> fails" '
360 test_must_fail git cat-file blob --$batch $oid
361 '
05d5667f 362
3afa8d86
EB
363 test_expect_success "Passing oid with --$batch fails" '
364 test_must_fail git cat-file --$batch $oid
365 '
366 done
a8128ed6 367done
05d5667f 368
3afa8d86 369for oid in $hello_oid $hello_compat_oid
122d5346 370do
3afa8d86
EB
371 for opt in t s e p
372 do
373 test_expect_success "Passing -$opt with --follow-symlinks fails" '
374 test_must_fail git cat-file --follow-symlinks -$opt $oid
122d5346 375 '
3afa8d86 376 done
122d5346
DT
377done
378
3c076dbe 379test_expect_success "--batch-check for a non-existent named object" '
b116c773
PS
380 cat >expect <<-EOF &&
381 foobar42 missing
382 foobar84 missing
383 EOF
384
385 printf "foobar42\nfoobar84" >in &&
386 git cat-file --batch-check <in >actual &&
387 test_cmp expect actual
3c076dbe
LW
388'
389
390test_expect_success "--batch-check for a non-existent hash" '
b116c773
PS
391 cat >expect <<-EOF &&
392 0000000000000000000000000000000000000042 missing
393 0000000000000000000000000000000000000084 missing
394 EOF
395
396 printf "0000000000000000000000000000000000000042\n0000000000000000000000000000000000000084" >in &&
397 git cat-file --batch-check <in >actual &&
398 test_cmp expect actual
3c076dbe
LW
399'
400
401test_expect_success "--batch for an existent and a non-existent hash" '
b116c773 402 cat >expect <<-EOF &&
baab175c 403 $tag_oid tag $tag_size
b116c773
PS
404 $tag_content
405 0000000000000000000000000000000000000000 missing
406 EOF
407
baab175c 408 printf "$tag_oid\n0000000000000000000000000000000000000000" >in &&
b116c773
PS
409 git cat-file --batch <in >actual &&
410 test_cmp expect actual
05d5667f
AR
411'
412
2e3a16b2 413test_expect_success "--batch-check for an empty line" '
b116c773
PS
414 cat >expect <<-EOF &&
415 missing
416 EOF
417
418 echo >in &&
419 git cat-file --batch-check <in >actual &&
420 test_cmp expect actual
05d5667f
AR
421'
422
4ef8d1dd 423test_expect_success 'empty --batch-check notices missing object' '
8125a58b 424 echo "$ZERO_OID missing" >expect &&
425 echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
4ef8d1dd
JH
426 test_cmp expect actual
427'
428
3afa8d86
EB
429batch_tests () {
430 boid=$1
431 loid=$2
432 lsize=$3
433 coid=$4
434 csize=$5
435 ccontent=$6
436 toid=$7
437 tsize=$8
438 tcontent=$9
439
440 batch_input="$boid
441$coid
442$toid
a8128ed6
AR
443deadbeef
444
445"
446
3afa8d86
EB
447 printf "%s\0" \
448 "$boid blob $hello_size" \
f79e1884 449 "$hello_content" \
3afa8d86
EB
450 "$coid commit $csize" \
451 "$ccontent" \
452 "$toid tag $tsize" \
453 "$tcontent" \
f79e1884
PS
454 "deadbeef missing" \
455 " missing" >batch_output
a8128ed6 456
3afa8d86 457 test_expect_success '--batch with multiple oids gives correct format' '
f79e1884 458 tr "\0" "\n" <batch_output >expect &&
b116c773
PS
459 echo_without_newline "$batch_input" >in &&
460 git cat-file --batch <in >actual &&
c7309f63 461 test_cmp expect actual
3afa8d86 462 '
a8128ed6 463
3afa8d86 464 test_expect_success '--batch, -z with multiple oids gives correct format' '
db9d67f2 465 echo_without_newline_nul "$batch_input" >in &&
f79e1884 466 tr "\0" "\n" <batch_output >expect &&
c7309f63
PS
467 git cat-file --batch -z <in >actual &&
468 test_cmp expect actual
3afa8d86 469 '
db9d67f2 470
3afa8d86 471 test_expect_success '--batch, -Z with multiple oids gives correct format' '
f79e1884
PS
472 echo_without_newline_nul "$batch_input" >in &&
473 git cat-file --batch -Z <in >actual &&
474 test_cmp batch_output actual
3afa8d86 475 '
f79e1884 476
3afa8d86
EB
477batch_check_input="$boid
478$loid
479$coid
480$toid
05d5667f
AR
481deadbeef
482
483"
484
3afa8d86
EB
485 printf "%s\0" \
486 "$boid blob $hello_size" \
487 "$loid tree $lsize" \
488 "$coid commit $csize" \
489 "$toid tag $tsize" \
f79e1884
PS
490 "deadbeef missing" \
491 " missing" >batch_check_output
05d5667f 492
3afa8d86 493 test_expect_success "--batch-check with multiple oids gives correct format" '
f79e1884 494 tr "\0" "\n" <batch_check_output >expect &&
b116c773
PS
495 echo_without_newline "$batch_check_input" >in &&
496 git cat-file --batch-check <in >actual &&
497 test_cmp expect actual
3afa8d86 498 '
05d5667f 499
3afa8d86 500 test_expect_success "--batch-check, -z with multiple oids gives correct format" '
f79e1884 501 tr "\0" "\n" <batch_check_output >expect &&
b116c773
PS
502 echo_without_newline_nul "$batch_check_input" >in &&
503 git cat-file --batch-check -z <in >actual &&
504 test_cmp expect actual
3afa8d86 505 '
db9d67f2 506
3afa8d86 507 test_expect_success "--batch-check, -Z with multiple oids gives correct format" '
f79e1884
PS
508 echo_without_newline_nul "$batch_check_input" >in &&
509 git cat-file --batch-check -Z <in >actual &&
510 test_cmp batch_check_output actual
3afa8d86 511 '
f79e1884 512
3afa8d86
EB
513batch_command_multiple_info="info $boid
514info $loid
515info $coid
516info $toid
3639fefe
TB
517info deadbeef"
518
3afa8d86 519 test_expect_success '--batch-command with multiple info calls gives correct format' '
440c705e 520 cat >expect <<-EOF &&
3afa8d86
EB
521 $boid blob $hello_size
522 $loid tree $lsize
523 $coid commit $csize
524 $toid tag $tsize
440c705e
JC
525 deadbeef missing
526 EOF
527
3639fefe
TB
528 echo "$batch_command_multiple_info" >in &&
529 git cat-file --batch-command --buffer <in >actual &&
440c705e 530
db9d67f2
TB
531 test_cmp expect actual &&
532
533 echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
534 git cat-file --batch-command --buffer -z <in >actual &&
535
f79e1884
PS
536 test_cmp expect actual &&
537
538 echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
539 tr "\n" "\0" <expect >expect_nul &&
540 git cat-file --batch-command --buffer -Z <in >actual &&
541
542 test_cmp expect_nul actual
3afa8d86 543 '
440c705e 544
3afa8d86
EB
545batch_command_multiple_contents="contents $boid
546contents $coid
547contents $toid
3639fefe
TB
548contents deadbeef
549flush"
550
3afa8d86 551 test_expect_success '--batch-command with multiple command calls gives correct format' '
f79e1884 552 printf "%s\0" \
3afa8d86 553 "$boid blob $hello_size" \
f79e1884 554 "$hello_content" \
3afa8d86
EB
555 "$coid commit $csize" \
556 "$ccontent" \
557 "$toid tag $tsize" \
558 "$tcontent" \
f79e1884
PS
559 "deadbeef missing" >expect_nul &&
560 tr "\0" "\n" <expect_nul >expect &&
440c705e 561
3639fefe 562 echo "$batch_command_multiple_contents" >in &&
c7309f63 563 git cat-file --batch-command --buffer <in >actual &&
440c705e 564
db9d67f2
TB
565 test_cmp expect actual &&
566
567 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
c7309f63 568 git cat-file --batch-command --buffer -z <in >actual &&
db9d67f2 569
f79e1884
PS
570 test_cmp expect actual &&
571
572 echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
573 git cat-file --batch-command --buffer -Z <in >actual &&
574
575 test_cmp expect_nul actual
3afa8d86
EB
576 '
577
578}
579
580batch_tests $hello_oid $tree_oid $tree_size $commit_oid $commit_size "$commit_content" $tag_oid $tag_size "$tag_content"
581batch_tests $hello_compat_oid $tree_compat_oid $tree_compat_size $commit_compat_oid $commit_compat_size "$commit_compat_content" $tag_compat_oid $tag_compat_size "$tag_compat_content"
582
583
584test_expect_success FUNNYNAMES 'setup with newline in input' '
585 touch -- "newline${LF}embedded" &&
586 git add -- "newline${LF}embedded" &&
587 git commit -m "file with newline embedded" &&
588 test_tick &&
589
590 printf "HEAD:newline${LF}embedded" >in
591'
592
593test_expect_success FUNNYNAMES '--batch-check, -z with newline in input' '
594 git cat-file --batch-check -z <in >actual &&
595 echo "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
596 test_cmp expect actual
597'
598
599test_expect_success FUNNYNAMES '--batch-check, -Z with newline in input' '
600 git cat-file --batch-check -Z <in >actual &&
601 printf "%s\0" "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
602 test_cmp expect actual
440c705e
JC
603'
604
65ea9c3c 605test_expect_success 'setup blobs which are likely to delta' '
c680668d 606 test-tool genrandom foo 10240 >foo &&
7abcbcb7 607 { cat foo && echo plus; } >foo-plus &&
65ea9c3c
JK
608 git add foo foo-plus &&
609 git commit -m foo &&
610 cat >blobs <<-\EOF
611 HEAD:foo
612 HEAD:foo-plus
613 EOF
614'
615
616test_expect_success 'confirm that neither loose blob is a delta' '
99094a7a 617 cat >expect <<-EOF &&
8125a58b 618 $ZERO_OID
619 $ZERO_OID
65ea9c3c
JK
620 EOF
621 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
622 test_cmp expect actual
623'
624
625# To avoid relying too much on the current delta heuristics,
626# we will check only that one of the two objects is a delta
627# against the other, but not the order. We can do so by just
628# asking for the base of both, and checking whether either
baab175c 629# oid appears in the output.
65ea9c3c
JK
630test_expect_success '%(deltabase) reports packed delta bases' '
631 git repack -ad &&
632 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
633 {
634 grep "$(git rev-parse HEAD:foo)" actual ||
635 grep "$(git rev-parse HEAD:foo-plus)" actual
636 }
637'
638
70e4a577
ÆAB
639test_expect_success 'setup bogus data' '
640 bogus_short_type="bogus" &&
641 bogus_short_content="bogus" &&
642 bogus_short_size=$(strlen "$bogus_short_content") &&
baab175c 643 bogus_short_oid=$(echo_without_newline "$bogus_short_content" | git hash-object -t $bogus_short_type --literally -w --stdin) &&
70e4a577
ÆAB
644
645 bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" &&
646 bogus_long_content="bogus" &&
647 bogus_long_size=$(strlen "$bogus_long_content") &&
baab175c 648 bogus_long_oid=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin)
70e4a577 649'
3e370f9f 650
59b8283d
ÆAB
651for arg1 in '' --allow-unknown-type
652do
653 for arg2 in -s -t -p
654 do
655 if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p"
656 then
657 continue
658 fi
659
660
661 test_expect_success "cat-file $arg1 $arg2 error on bogus short OID" '
662 cat >expect <<-\EOF &&
663 fatal: invalid object type
664 EOF
665
666 if test "$arg1" = "--allow-unknown-type"
667 then
baab175c 668 git cat-file $arg1 $arg2 $bogus_short_oid
59b8283d 669 else
baab175c 670 test_must_fail git cat-file $arg1 $arg2 $bogus_short_oid >out 2>actual &&
59b8283d
ÆAB
671 test_must_be_empty out &&
672 test_cmp expect actual
673 fi
674 '
675
676 test_expect_success "cat-file $arg1 $arg2 error on bogus full OID" '
677 if test "$arg2" = "-p"
678 then
679 cat >expect <<-EOF
baab175c
EB
680 error: header for $bogus_long_oid too long, exceeds 32 bytes
681 fatal: Not a valid object name $bogus_long_oid
59b8283d
ÆAB
682 EOF
683 else
684 cat >expect <<-EOF
baab175c 685 error: header for $bogus_long_oid too long, exceeds 32 bytes
59b8283d
ÆAB
686 fatal: git cat-file: could not get object info
687 EOF
688 fi &&
689
690 if test "$arg1" = "--allow-unknown-type"
691 then
baab175c 692 git cat-file $arg1 $arg2 $bogus_short_oid
59b8283d 693 else
baab175c 694 test_must_fail git cat-file $arg1 $arg2 $bogus_long_oid >out 2>actual &&
59b8283d
ÆAB
695 test_must_be_empty out &&
696 test_cmp expect actual
697 fi
698 '
699
700 test_expect_success "cat-file $arg1 $arg2 error on missing short OID" '
701 cat >expect.err <<-EOF &&
702 fatal: Not a valid object name $(test_oid deadbeef_short)
703 EOF
704 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef_short) >out 2>err.actual &&
4e273368
AR
705 test_must_be_empty out &&
706 test_cmp expect.err err.actual
59b8283d
ÆAB
707 '
708
709 test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
710 if test "$arg2" = "-p"
711 then
712 cat >expect.err <<-EOF
713 fatal: Not a valid object name $(test_oid deadbeef)
714 EOF
715 else
716 cat >expect.err <<-\EOF
717 fatal: git cat-file: could not get object info
718 EOF
719 fi &&
720 test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
721 test_must_be_empty out &&
722 test_cmp expect.err err.actual
723 '
724 done
725done
726
dd45a562 727test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
baab175c 728 git cat-file -e $bogus_short_oid
dd45a562
ÆAB
729'
730
731test_expect_success '-e can not be combined with --allow-unknown-type' '
baab175c 732 test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_oid
dd45a562
ÆAB
733'
734
735test_expect_success '-p cannot print a broken object even with --allow-unknown-type' '
baab175c
EB
736 test_must_fail git cat-file -p $bogus_short_oid &&
737 test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_oid
dd45a562
ÆAB
738'
739
740test_expect_success '<type> <hash> does not work with objects of broken types' '
741 cat >err.expect <<-\EOF &&
742 fatal: invalid object type "bogus"
743 EOF
baab175c 744 test_must_fail git cat-file $bogus_short_type $bogus_short_oid 2>err.actual &&
dd45a562
ÆAB
745 test_cmp err.expect err.actual
746'
747
748test_expect_success 'broken types combined with --batch and --batch-check' '
baab175c 749 echo $bogus_short_oid >bogus-oid &&
dd45a562
ÆAB
750
751 cat >err.expect <<-\EOF &&
752 fatal: invalid object type
753 EOF
754
755 test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
756 test_cmp err.expect err.actual &&
757
758 test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
759 test_cmp err.expect err.actual
760'
761
762test_expect_success 'the --batch and --batch-check options do not combine with --allow-unknown-type' '
763 test_expect_code 128 git cat-file --batch --allow-unknown-type <bogus-oid &&
764 test_expect_code 128 git cat-file --batch-check --allow-unknown-type <bogus-oid
765'
766
767test_expect_success 'the --allow-unknown-type option does not consider replacement refs' '
768 cat >expect <<-EOF &&
769 $bogus_short_type
770 EOF
baab175c 771 git cat-file -t --allow-unknown-type $bogus_short_oid >actual &&
dd45a562
ÆAB
772 test_cmp expect actual &&
773
774 # Create it manually, as "git replace" will die on bogus
775 # types.
776 head=$(git rev-parse --verify HEAD) &&
baab175c
EB
777 test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_oid" &&
778 test-tool ref-store main update-ref msg "refs/replace/$bogus_short_oid" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
dd45a562
ÆAB
779
780 cat >expect <<-EOF &&
781 commit
782 EOF
baab175c 783 git cat-file -t --allow-unknown-type $bogus_short_oid >actual &&
dd45a562
ÆAB
784 test_cmp expect actual
785'
3e370f9f
KN
786
787test_expect_success "Type of broken object is correct" '
70e4a577 788 echo $bogus_short_type >expect &&
baab175c 789 git cat-file -t --allow-unknown-type $bogus_short_oid >actual &&
3e370f9f
KN
790 test_cmp expect actual
791'
792
793test_expect_success "Size of broken object is correct" '
70e4a577 794 echo $bogus_short_size >expect &&
baab175c 795 git cat-file -s --allow-unknown-type $bogus_short_oid >actual &&
3e370f9f
KN
796 test_cmp expect actual
797'
e879295b
JK
798
799test_expect_success 'clean up broken object' '
baab175c 800 rm .git/objects/$(test_oid_to_path $bogus_short_oid)
e879295b
JK
801'
802
3e370f9f 803test_expect_success "Type of broken object is correct when type is large" '
70e4a577 804 echo $bogus_long_type >expect &&
baab175c 805 git cat-file -t --allow-unknown-type $bogus_long_oid >actual &&
3e370f9f
KN
806 test_cmp expect actual
807'
808
809test_expect_success "Size of large broken object is correct when type is large" '
70e4a577 810 echo $bogus_long_size >expect &&
baab175c 811 git cat-file -s --allow-unknown-type $bogus_long_oid >actual &&
3e370f9f
KN
812 test_cmp expect actual
813'
814
e879295b 815test_expect_success 'clean up broken object' '
baab175c 816 rm .git/objects/$(test_oid_to_path $bogus_long_oid)
061a21d3
JH
817'
818
7e7d220d
ÆAB
819test_expect_success 'cat-file -t and -s on corrupt loose object' '
820 git init --bare corrupt-loose.git &&
821 (
822 cd corrupt-loose.git &&
823
824 # Setup and create the empty blob and its path
825 empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
4627c67f 826 empty_blob=$(git hash-object -w --stdin </dev/null) &&
7e7d220d
ÆAB
827
828 # Create another blob and its path
829 echo other >other.blob &&
830 other_blob=$(git hash-object -w --stdin <other.blob) &&
831 other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
832
833 # Before the swap the size is 0
834 cat >out.expect <<-EOF &&
835 0
836 EOF
837 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
838 test_must_be_empty err.actual &&
839 test_cmp out.expect out.actual &&
840
841 # Swap the two to corrupt the repository
842 mv -f "$other_path" "$empty_path" &&
843 test_must_fail git fsck 2>err.fsck &&
96e41f58 844 grep "hash-path mismatch" err.fsck &&
7e7d220d
ÆAB
845
846 # confirm that cat-file is reading the new swapped-in
847 # blob...
848 cat >out.expect <<-EOF &&
849 blob
850 EOF
851 git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
852 test_must_be_empty err.actual &&
853 test_cmp out.expect out.actual &&
854
855 # ... since it has a different size now.
856 cat >out.expect <<-EOF &&
857 6
858 EOF
859 git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
860 test_must_be_empty err.actual &&
861 test_cmp out.expect out.actual &&
862
863 # So far "cat-file" has been happy to spew the found
864 # content out as-is. Try to make it zlib-invalid.
865 mv -f other.blob "$empty_path" &&
866 test_must_fail git fsck 2>err.fsck &&
4627c67f
ÆAB
867 cat >expect <<-EOF &&
868 error: inflate: data stream error (incorrect header check)
869 error: unable to unpack header of ./$empty_path
870 error: $empty_blob: object corrupt or missing: ./$empty_path
871 EOF
872 grep "^error: " err.fsck >actual &&
873 test_cmp expect actual
7e7d220d 874 )
e879295b
JK
875'
876
122d5346
DT
877# Tests for git cat-file --follow-symlinks
878test_expect_success 'prep for symlink tests' '
879 echo_without_newline "$hello_content" >morx &&
880 test_ln_s_add morx same-dir-link &&
881 test_ln_s_add dir link-to-dir &&
882 test_ln_s_add ../fleem out-of-repo-link &&
883 test_ln_s_add .. out-of-repo-link-dir &&
884 test_ln_s_add same-dir-link link-to-link &&
885 test_ln_s_add nope broken-same-dir-link &&
886 mkdir dir &&
887 test_ln_s_add ../morx dir/parent-dir-link &&
888 test_ln_s_add .. dir/link-dir &&
889 test_ln_s_add ../../escape dir/out-of-repo-link &&
890 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
891 test_ln_s_add nope dir/broken-link-in-dir &&
892 mkdir dir/subdir &&
893 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
894 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
895 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
896 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
897 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
898 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
899 echo_without_newline "$hello_content" >dir/ind1 &&
900 test_ln_s_add dir dirlink &&
901 test_ln_s_add dir/subdir subdirlink &&
902 test_ln_s_add subdir/ind2 dir/link-to-child &&
903 test_ln_s_add dir/link-to-child link-to-down-link &&
904 test_ln_s_add dir/.. up-down &&
905 test_ln_s_add dir/../ up-down-trailing &&
906 test_ln_s_add dir/../morx up-down-file &&
907 test_ln_s_add dir/../../morx up-up-down-file &&
908 test_ln_s_add subdirlink/../../morx up-two-down-file &&
909 test_ln_s_add loop1 loop2 &&
910 test_ln_s_add loop2 loop1 &&
911 git add morx dir/subdir/ind2 dir/ind1 &&
912 git commit -am "test" &&
baab175c 913 echo $hello_oid blob $hello_size >found
122d5346
DT
914'
915
916test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
917 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
918 test_cmp found actual &&
919 echo HEAD:nope missing >expect &&
920 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
921 test_cmp expect actual
922'
923
924test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
925 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
926 test_cmp found actual
927'
928
929test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
930 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
931 test_cmp found actual
932'
933
934
935test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
936 echo dangling 25 >expect &&
937 echo HEAD:broken-same-dir-link >>expect &&
938 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
939 test_cmp expect actual
940'
941
f79e1884
PS
942test_expect_success 'git cat-file --batch-check --follow-symlinks -Z works for broken in-repo, same-dir links' '
943 printf "HEAD:broken-same-dir-link\0" >in &&
944 printf "dangling 25\0HEAD:broken-same-dir-link\0" >expect &&
945 git cat-file --batch-check --follow-symlinks -Z <in >actual &&
946 test_cmp expect actual
947'
948
122d5346
DT
949test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
950 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
951 test_cmp found actual
952'
953
954test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
955 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
956 test_cmp found actual &&
957 echo notdir 29 >expect &&
958 echo HEAD:dir/parent-dir-link/nope >>expect &&
959 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
960 test_cmp expect actual
961'
962
f79e1884
PS
963test_expect_success 'git cat-file --batch-check --follow-symlinks -Z works for parent-dir links' '
964 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
965 test_cmp found actual &&
966 printf "notdir 29\0HEAD:dir/parent-dir-link/nope\0" >expect &&
967 printf "HEAD:dir/parent-dir-link/nope\0" >in &&
968 git cat-file --batch-check --follow-symlinks -Z <in >actual &&
969 test_cmp expect actual
970'
971
122d5346
DT
972test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
973 echo dangling 22 >expect &&
974 echo HEAD:dir/link-dir/nope >>expect &&
975 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
976 test_cmp expect actual &&
977 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
978 test_cmp found actual &&
979 echo dangling 27 >expect &&
980 echo HEAD:dir/broken-link-in-dir >>expect &&
981 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
982 test_cmp expect actual
983'
984
985test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
986 echo notdir 41 >expect &&
987 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
988 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
989 test_cmp expect actual &&
990 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
991 test_cmp found actual &&
992 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
993 test_cmp found actual
994'
995
996test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
997 echo dangling 17 >expect &&
998 echo HEAD:dirlink/morx >>expect &&
999 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
1000 test_cmp expect actual &&
baab175c 1001 echo $hello_oid blob $hello_size >expect &&
122d5346
DT
1002 echo HEAD:dirlink/ind1 | 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 dir/subdir links' '
1007 echo dangling 20 >expect &&
1008 echo HEAD:subdirlink/morx >>expect &&
1009 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
1010 test_cmp expect actual &&
1011 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
1012 test_cmp found actual
1013'
1014
1015test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
1016 echo notdir 27 >expect &&
1017 echo HEAD:dir/link-to-child/morx >>expect &&
1018 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
1019 test_cmp expect actual &&
1020 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
1021 test_cmp found actual &&
1022 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
1023 test_cmp found actual
1024'
1025
1026test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
1027 echo symlink 8 >expect &&
1028 echo ../fleem >>expect &&
1029 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
1030 test_cmp expect actual &&
1031 echo symlink 2 >expect &&
1032 echo .. >>expect &&
1033 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
1034 test_cmp expect actual
1035'
1036
1037test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
1038 echo symlink 9 >expect &&
1039 echo ../escape >>expect &&
1040 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
1041 test_cmp expect actual &&
1042 echo symlink 2 >expect &&
1043 echo .. >>expect &&
1044 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
1045 test_cmp expect actual
1046'
1047
1048test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
1049 echo symlink 15 >expect &&
1050 echo ../great-escape >>expect &&
1051 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
1052 test_cmp expect actual &&
1053 echo symlink 2 >expect &&
1054 echo .. >>expect &&
1055 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
1056 test_cmp expect actual &&
1057 echo symlink 3 >expect &&
1058 echo ../ >>expect &&
1059 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
1060 test_cmp expect actual
1061'
1062
1063test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
1064 echo HEAD: | git cat-file --batch-check >expect &&
1065 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
1066 test_cmp expect actual &&
1067 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
1068 test_cmp expect actual &&
1069 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
1070 test_cmp found actual &&
1071 echo symlink 7 >expect &&
1072 echo ../morx >>expect &&
1073 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
1074 test_cmp expect actual &&
1075 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
1076 test_cmp found actual
1077'
1078
1079test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
1080 echo loop 10 >expect &&
1081 echo HEAD:loop1 >>expect &&
1082 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
1083 test_cmp expect actual
1084'
1085
f79e1884
PS
1086test_expect_success 'git cat-file --batch-check --follow-symlink -Z breaks loops' '
1087 printf "loop 10\0HEAD:loop1\0" >expect &&
1088 printf "HEAD:loop1\0" >in &&
1089 git cat-file --batch-check --follow-symlinks -Z <in >actual &&
1090 test_cmp expect actual
1091'
1092
122d5346
DT
1093test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
1094 echo HEAD:morx | git cat-file --batch >expect &&
1095 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
1096 test_cmp expect actual
1097'
67f0b6f3 1098
6a951937 1099test_expect_success 'cat-file --batch-all-objects shows all objects' '
3115ee45 1100 # make new repos so we know the full set of objects; we will
6a951937 1101 # also make sure that there are some packed and some loose
aa2f5ef5
JK
1102 # objects, some referenced and some not, some duplicates, and that
1103 # there are some available only via alternates.
6a951937
JK
1104 git init all-one &&
1105 (
1106 cd all-one &&
1107 echo content >file &&
1108 git add file &&
1109 git commit -qm base &&
1110 git rev-parse HEAD HEAD^{tree} HEAD:file &&
1111 git repack -ad &&
1112 echo not-cloned | git hash-object -w --stdin
1113 ) >expect.unsorted &&
1114 git clone -s all-one all-two &&
1115 (
1116 cd all-two &&
1117 echo local-unref | git hash-object -w --stdin
1118 ) >>expect.unsorted &&
aa2f5ef5
JK
1119 git -C all-two rev-parse HEAD:file |
1120 git -C all-two pack-objects .git/objects/pack/pack &&
6a951937
JK
1121 sort <expect.unsorted >expect &&
1122 git -C all-two cat-file --batch-all-objects \
3115ee45 1123 --batch-check="%(objectname)" >actual &&
6a951937
JK
1124 test_cmp expect actual
1125'
1126
0750bb5b
JK
1127# The only user-visible difference is that the objects are no longer sorted,
1128# and the resulting sort order is undefined. So we can only check that it
1129# produces the same objects as the ordered case, but that at least exercises
1130# the code.
1131test_expect_success 'cat-file --unordered works' '
1132 git -C all-two cat-file --batch-all-objects --unordered \
1133 --batch-check="%(objectname)" >actual.unsorted &&
1134 sort <actual.unsorted >actual &&
1135 test_cmp expect actual
1136'
1137
e16acc80
ZH
1138test_expect_success 'set up object list for --batch-all-objects tests' '
1139 git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects
1140'
1141
1142test_expect_success 'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
1143 git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
1144 git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
1145 cmp expect actual
1146'
1147
1148test_expect_success 'cat-file --batch="%(rest)" with --batch-all-objects will work' '
1149 git -C all-two cat-file --batch="%(rest)" <objects >expect &&
1150 git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
1151 cmp expect actual
1152'
1153
1154test_expect_success 'cat-file --batch="batman" with --batch-all-objects will work' '
1155 git -C all-two cat-file --batch="batman" <objects >expect &&
1156 git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
1157 cmp expect actual
1158'
1159
f5461512
JK
1160test_expect_success 'cat-file %(objectsize:disk) with --batch-all-objects' '
1161 # our state has both loose and packed objects,
1162 # so find both for our expected output
1163 {
1164 find .git/objects/?? -type f |
1165 awk -F/ "{ print \$0, \$3\$4 }" |
1166 while read path oid
1167 do
1168 size=$(test_file_size "$path") &&
1169 echo "$oid $size" ||
1170 return 1
1171 done &&
1172 rawsz=$(test_oid rawsz) &&
1173 find .git/objects/pack -name "*.idx" |
1174 while read idx
1175 do
1176 git show-index <"$idx" >idx.raw &&
54d8a253 1177 sort -nr <idx.raw >idx.sorted &&
f5461512
JK
1178 packsz=$(test_file_size "${idx%.idx}.pack") &&
1179 end=$((packsz - rawsz)) &&
54d8a253
RS
1180 while read start oid rest
1181 do
1182 size=$((end - start)) &&
1183 end=$start &&
1184 echo "$oid $size" ||
1185 return 1
1186 done <idx.sorted ||
f5461512
JK
1187 return 1
1188 done
1189 } >expect.raw &&
1190 sort <expect.raw >expect &&
1191 git cat-file --batch-all-objects \
1192 --batch-check="%(objectname) %(objectsize:disk)" >actual &&
1193 test_cmp expect actual
1194'
1195
5c5b29b4
JK
1196test_expect_success 'set up replacement object' '
1197 orig=$(git rev-parse HEAD) &&
1198 git cat-file commit $orig >orig &&
1199 {
1200 cat orig &&
1201 echo extra
1202 } >fake &&
1203 fake=$(git hash-object -t commit -w fake) &&
1204 orig_size=$(git cat-file -s $orig) &&
1205 fake_size=$(git cat-file -s $fake) &&
1206 git replace $orig $fake
1207'
1208
1209test_expect_success 'cat-file --batch respects replace objects' '
1210 git cat-file --batch >actual <<-EOF &&
1211 $orig
1212 EOF
1213 {
1214 echo "$orig commit $fake_size" &&
1215 cat fake &&
1216 echo
1217 } >expect &&
1218 test_cmp expect actual
1219'
1220
1221test_expect_success 'cat-file --batch-check respects replace objects' '
1222 git cat-file --batch-check >actual <<-EOF &&
1223 $orig
1224 EOF
1225 echo "$orig commit $fake_size" >expect &&
1226 test_cmp expect actual
1227'
1228
1229# Pull the entry for object with oid "$1" out of the output of
1230# "cat-file --batch", including its object content (which requires
1231# parsing and reading a set amount of bytes, hence perl).
1232extract_batch_output () {
1233 perl -ne '
1234 BEGIN { $oid = shift }
1235 if (/^$oid \S+ (\d+)$/) {
1236 print;
1237 read STDIN, my $buf, $1;
1238 print $buf;
1239 print "\n";
1240 }
1241 ' "$@"
1242}
1243
1244test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
1245 git cat-file --batch-all-objects --batch >actual.raw &&
1246 extract_batch_output $orig <actual.raw >actual &&
1247 {
1248 echo "$orig commit $orig_size" &&
1249 cat orig &&
1250 echo
1251 } >expect &&
1252 test_cmp expect actual
1253'
1254
1255test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
1256 git cat-file --batch-all-objects --batch-check >actual.raw &&
1257 grep ^$orig actual.raw >actual &&
1258 echo "$orig commit $orig_size" >expect &&
1259 test_cmp expect actual
1260'
440c705e
JC
1261test_expect_success 'batch-command empty command' '
1262 echo "" >cmd &&
1263 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1264 grep "^fatal:.*empty command in input.*" err
1265'
1266
1267test_expect_success 'batch-command whitespace before command' '
1268 echo " info deadbeef" >cmd &&
1269 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1270 grep "^fatal:.*whitespace before command.*" err
1271'
1272
1273test_expect_success 'batch-command unknown command' '
1274 echo unknown_command >cmd &&
1275 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1276 grep "^fatal:.*unknown command.*" err
1277'
1278
1279test_expect_success 'batch-command missing arguments' '
1280 echo "info" >cmd &&
1281 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1282 grep "^fatal:.*info requires arguments.*" err
1283'
1284
1285test_expect_success 'batch-command flush with arguments' '
1286 echo "flush arg" >cmd &&
1287 test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
1288 grep "^fatal:.*flush takes no arguments.*" err
1289'
1290
1291test_expect_success 'batch-command flush without --buffer' '
1292 echo "flush" >cmd &&
1293 test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
1294 grep "^fatal:.*flush is only for --buffer mode.*" err
1295'
5c5b29b4 1296
b335d3f1 1297test_done