1#!/bin/sh
2
3test_description='git cat-file'
4
5. ./test-lib.sh
6
7echo_without_newline () {
8 printf '%s' "$*"
9}
10
11strlen () {
12 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
13}
14
15maybe_remove_timestamp () {
16 if test -z "$2"; then
17 echo_without_newline "$1"
18 else
19 echo_without_newline "$(printf '%s\n' "$1" | sed -e 's/ [0-9][0-9]* [-+][0-9][0-9][0-9][0-9]$//')"
20 fi
21}
22
23run_tests () {
24 type=$1
25 sha1=$2
26 size=$3
27 content=$4
28 pretty_content=$5
29 no_ts=$6
30
31 batch_output="$sha1 $type $size
32$content"
33
34 test_expect_success "$type exists" '
35 git cat-file -e $sha1
36 '
37
38 test_expect_success "Type of $type is correct" '
39 echo $type >expect &&
40 git cat-file -t $sha1 >actual &&
41 test_cmp expect actual
42 '
43
44 test_expect_success "Size of $type is correct" '
45 echo $size >expect &&
46 git cat-file -s $sha1 >actual &&
47 test_cmp expect actual
48 '
49
50 test_expect_success "Type of $type is correct using --allow-unknown-type" '
51 echo $type >expect &&
52 git cat-file -t --allow-unknown-type $sha1 >actual &&
53 test_cmp expect actual
54 '
55
56 test_expect_success "Size of $type is correct using --allow-unknown-type" '
57 echo $size >expect &&
58 git cat-file -s --allow-unknown-type $sha1 >actual &&
59 test_cmp expect actual
60 '
61
62 test -z "$content" ||
63 test_expect_success "Content of $type is correct" '
64 maybe_remove_timestamp "$content" $no_ts >expect &&
65 maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts >actual &&
66 test_cmp expect actual
67 '
68
69 test_expect_success "Pretty content of $type is correct" '
70 maybe_remove_timestamp "$pretty_content" $no_ts >expect &&
71 maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts >actual &&
72 test_cmp expect actual
73 '
74
75 test -z "$content" ||
76 test_expect_success "--batch output of $type is correct" '
77 maybe_remove_timestamp "$batch_output" $no_ts >expect &&
78 maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" $no_ts >actual &&
79 test_cmp expect actual
80 '
81
82 test_expect_success "--batch-check output of $type is correct" '
83 echo "$sha1 $type $size" >expect &&
84 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
85 test_cmp expect actual
86 '
87
88 test_expect_success "custom --batch-check format" '
89 echo "$type $sha1" >expect &&
90 echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
91 test_cmp expect actual
92 '
93
94 test_expect_success '--batch-check with %(rest)' '
95 echo "$type this is some extra content" >expect &&
96 echo "$sha1 this is some extra content" |
97 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
98 test_cmp expect actual
99 '
100
101 test -z "$content" ||
102 test_expect_success "--batch without type ($type)" '
103 {
104 echo "$size" &&
105 maybe_remove_timestamp "$content" $no_ts
106 } >expect &&
107 echo $sha1 | git cat-file --batch="%(objectsize)" >actual.full &&
108 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
109 test_cmp expect actual
110 '
111
112 test -z "$content" ||
113 test_expect_success "--batch without size ($type)" '
114 {
115 echo "$type" &&
116 maybe_remove_timestamp "$content" $no_ts
117 } >expect &&
118 echo $sha1 | git cat-file --batch="%(objecttype)" >actual.full &&
119 maybe_remove_timestamp "$(cat actual.full)" $no_ts >actual &&
120 test_cmp expect actual
121 '
122}
123
124hello_content="Hello World"
125hello_size=$(strlen "$hello_content")
126hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
127
128test_expect_success "setup" '
129 echo_without_newline "$hello_content" > hello &&
130 git update-index --add hello
131'
132
133run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
134
135test_expect_success '--batch-check without %(rest) considers whole line' '
136 echo "$hello_sha1 blob $hello_size" >expect &&
137 git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
138 test_when_finished "git update-index --remove \"white space\"" &&
139 echo ":white space" | git cat-file --batch-check >actual &&
140 test_cmp expect actual
141'
142
143test_oid_init
144
145tree_sha1=$(git write-tree)
146tree_size=$(($(test_oid rawsz) + 13))
147tree_pretty_content="100644 blob $hello_sha1 hello"
148
149run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
150
151commit_message="Initial commit"
152commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
153commit_size=$(($(test_oid hexsz) + 137))
154commit_content="tree $tree_sha1
155author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
156committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
157
158$commit_message"
159
160run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
161
162tag_header_without_timestamp="object $hello_sha1
163type blob
164tag hellotag
165tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
166tag_description="This is a tag"
167tag_content="$tag_header_without_timestamp 0000000000 +0000
168
169$tag_description"
170
171tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
172tag_size=$(strlen "$tag_content")
173
174run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
175
176test_expect_success \
177 "Reach a blob from a tag pointing to it" \
178 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
179
180for batch in batch batch-check
181do
182 for opt in t s e p
183 do
184 test_expect_success "Passing -$opt with --$batch fails" '
185 test_must_fail git cat-file --$batch -$opt $hello_sha1
186 '
187
188 test_expect_success "Passing --$batch with -$opt fails" '
189 test_must_fail git cat-file -$opt --$batch $hello_sha1
190 '
191 done
192
193 test_expect_success "Passing <type> with --$batch fails" '
194 test_must_fail git cat-file --$batch blob $hello_sha1
195 '
196
197 test_expect_success "Passing --$batch with <type> fails" '
198 test_must_fail git cat-file blob --$batch $hello_sha1
199 '
200
201 test_expect_success "Passing sha1 with --$batch fails" '
202 test_must_fail git cat-file --$batch $hello_sha1
203 '
204done
205
206for opt in t s e p
207do
208 test_expect_success "Passing -$opt with --follow-symlinks fails" '
209 test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
210 '
211done
212
213test_expect_success "--batch-check for a non-existent named object" '
214 test "foobar42 missing
215foobar84 missing" = \
216 "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
217'
218
219test_expect_success "--batch-check for a non-existent hash" '
220 test "0000000000000000000000000000000000000042 missing
2210000000000000000000000000000000000000084 missing" = \
222 "$( ( echo 0000000000000000000000000000000000000042;
223 echo_without_newline 0000000000000000000000000000000000000084; ) |
224 git cat-file --batch-check)"
225'
226
227test_expect_success "--batch for an existent and a non-existent hash" '
228 test "$tag_sha1 tag $tag_size
229$tag_content
2300000000000000000000000000000000000000000 missing" = \
231 "$( ( echo $tag_sha1;
232 echo_without_newline 0000000000000000000000000000000000000000; ) |
233 git cat-file --batch)"
234'
235
236test_expect_success "--batch-check for an empty line" '
237 test " missing" = "$(echo | git cat-file --batch-check)"
238'
239
240test_expect_success 'empty --batch-check notices missing object' '
241 echo "$ZERO_OID missing" >expect &&
242 echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
243 test_cmp expect actual
244'
245
246batch_input="$hello_sha1
247$commit_sha1
248$tag_sha1
249deadbeef
250
251"
252
253batch_output="$hello_sha1 blob $hello_size
254$hello_content
255$commit_sha1 commit $commit_size
256$commit_content
257$tag_sha1 tag $tag_size
258$tag_content
259deadbeef missing
260 missing"
261
262test_expect_success '--batch with multiple sha1s gives correct format' '
263 test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
264'
265
266batch_check_input="$hello_sha1
267$tree_sha1
268$commit_sha1
269$tag_sha1
270deadbeef
271
272"
273
274batch_check_output="$hello_sha1 blob $hello_size
275$tree_sha1 tree $tree_size
276$commit_sha1 commit $commit_size
277$tag_sha1 tag $tag_size
278deadbeef missing
279 missing"
280
281test_expect_success "--batch-check with multiple sha1s gives correct format" '
282 test "$batch_check_output" = \
283 "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
284'
285
286test_expect_success 'setup blobs which are likely to delta' '
287 test-tool genrandom foo 10240 >foo &&
288 { cat foo; echo plus; } >foo-plus &&
289 git add foo foo-plus &&
290 git commit -m foo &&
291 cat >blobs <<-\EOF
292 HEAD:foo
293 HEAD:foo-plus
294 EOF
295'
296
297test_expect_success 'confirm that neither loose blob is a delta' '
298 cat >expect <<-EOF &&
299 $ZERO_OID
300 $ZERO_OID
301 EOF
302 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
303 test_cmp expect actual
304'
305
306# To avoid relying too much on the current delta heuristics,
307# we will check only that one of the two objects is a delta
308# against the other, but not the order. We can do so by just
309# asking for the base of both, and checking whether either
310# sha1 appears in the output.
311test_expect_success '%(deltabase) reports packed delta bases' '
312 git repack -ad &&
313 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
314 {
315 grep "$(git rev-parse HEAD:foo)" actual ||
316 grep "$(git rev-parse HEAD:foo-plus)" actual
317 }
318'
319
320bogus_type="bogus"
321bogus_content="bogus"
322bogus_size=$(strlen "$bogus_content")
323bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
324
325test_expect_success "Type of broken object is correct" '
326 echo $bogus_type >expect &&
327 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
328 test_cmp expect actual
329'
330
331test_expect_success "Size of broken object is correct" '
332 echo $bogus_size >expect &&
333 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
334 test_cmp expect actual
335'
336bogus_type="abcdefghijklmnopqrstuvwxyz1234679"
337bogus_content="bogus"
338bogus_size=$(strlen "$bogus_content")
339bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
340
341test_expect_success "Type of broken object is correct when type is large" '
342 echo $bogus_type >expect &&
343 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
344 test_cmp expect actual
345'
346
347test_expect_success "Size of large broken object is correct when type is large" '
348 echo $bogus_size >expect &&
349 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
350 test_cmp expect actual
351'
352
353# Tests for git cat-file --follow-symlinks
354test_expect_success 'prep for symlink tests' '
355 echo_without_newline "$hello_content" >morx &&
356 test_ln_s_add morx same-dir-link &&
357 test_ln_s_add dir link-to-dir &&
358 test_ln_s_add ../fleem out-of-repo-link &&
359 test_ln_s_add .. out-of-repo-link-dir &&
360 test_ln_s_add same-dir-link link-to-link &&
361 test_ln_s_add nope broken-same-dir-link &&
362 mkdir dir &&
363 test_ln_s_add ../morx dir/parent-dir-link &&
364 test_ln_s_add .. dir/link-dir &&
365 test_ln_s_add ../../escape dir/out-of-repo-link &&
366 test_ln_s_add ../.. dir/out-of-repo-link-dir &&
367 test_ln_s_add nope dir/broken-link-in-dir &&
368 mkdir dir/subdir &&
369 test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
370 test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
371 test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
372 test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
373 test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
374 echo_without_newline "$hello_content" >dir/subdir/ind2 &&
375 echo_without_newline "$hello_content" >dir/ind1 &&
376 test_ln_s_add dir dirlink &&
377 test_ln_s_add dir/subdir subdirlink &&
378 test_ln_s_add subdir/ind2 dir/link-to-child &&
379 test_ln_s_add dir/link-to-child link-to-down-link &&
380 test_ln_s_add dir/.. up-down &&
381 test_ln_s_add dir/../ up-down-trailing &&
382 test_ln_s_add dir/../morx up-down-file &&
383 test_ln_s_add dir/../../morx up-up-down-file &&
384 test_ln_s_add subdirlink/../../morx up-two-down-file &&
385 test_ln_s_add loop1 loop2 &&
386 test_ln_s_add loop2 loop1 &&
387 git add morx dir/subdir/ind2 dir/ind1 &&
388 git commit -am "test" &&
389 echo $hello_sha1 blob $hello_size >found
390'
391
392test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
393 echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
394 test_cmp found actual &&
395 echo HEAD:nope missing >expect &&
396 echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
397 test_cmp expect actual
398'
399
400test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
401 echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
402 test_cmp found actual
403'
404
405test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
406 echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
407 test_cmp found actual
408'
409
410
411test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
412 echo dangling 25 >expect &&
413 echo HEAD:broken-same-dir-link >>expect &&
414 echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
415 test_cmp expect actual
416'
417
418test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
419 echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
420 test_cmp found actual
421'
422
423test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
424 echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
425 test_cmp found actual &&
426 echo notdir 29 >expect &&
427 echo HEAD:dir/parent-dir-link/nope >>expect &&
428 echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
429 test_cmp expect actual
430'
431
432test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
433 echo dangling 22 >expect &&
434 echo HEAD:dir/link-dir/nope >>expect &&
435 echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
436 test_cmp expect actual &&
437 echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
438 test_cmp found actual &&
439 echo dangling 27 >expect &&
440 echo HEAD:dir/broken-link-in-dir >>expect &&
441 echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
442 test_cmp expect actual
443'
444
445test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
446 echo notdir 41 >expect &&
447 echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
448 echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
449 test_cmp expect actual &&
450 echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
451 test_cmp found actual &&
452 echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
453 test_cmp found actual
454'
455
456test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
457 echo dangling 17 >expect &&
458 echo HEAD:dirlink/morx >>expect &&
459 echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
460 test_cmp expect actual &&
461 echo $hello_sha1 blob $hello_size >expect &&
462 echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
463 test_cmp expect actual
464'
465
466test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
467 echo dangling 20 >expect &&
468 echo HEAD:subdirlink/morx >>expect &&
469 echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
470 test_cmp expect actual &&
471 echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
472 test_cmp found actual
473'
474
475test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
476 echo notdir 27 >expect &&
477 echo HEAD:dir/link-to-child/morx >>expect &&
478 echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
479 test_cmp expect actual &&
480 echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
481 test_cmp found actual &&
482 echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
483 test_cmp found actual
484'
485
486test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
487 echo symlink 8 >expect &&
488 echo ../fleem >>expect &&
489 echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
490 test_cmp expect actual &&
491 echo symlink 2 >expect &&
492 echo .. >>expect &&
493 echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
494 test_cmp expect actual
495'
496
497test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
498 echo symlink 9 >expect &&
499 echo ../escape >>expect &&
500 echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
501 test_cmp expect actual &&
502 echo symlink 2 >expect &&
503 echo .. >>expect &&
504 echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
505 test_cmp expect actual
506'
507
508test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
509 echo symlink 15 >expect &&
510 echo ../great-escape >>expect &&
511 echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
512 test_cmp expect actual &&
513 echo symlink 2 >expect &&
514 echo .. >>expect &&
515 echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
516 test_cmp expect actual &&
517 echo symlink 3 >expect &&
518 echo ../ >>expect &&
519 echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
520 test_cmp expect actual
521'
522
523test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
524 echo HEAD: | git cat-file --batch-check >expect &&
525 echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
526 test_cmp expect actual &&
527 echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
528 test_cmp expect actual &&
529 echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
530 test_cmp found actual &&
531 echo symlink 7 >expect &&
532 echo ../morx >>expect &&
533 echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
534 test_cmp expect actual &&
535 echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
536 test_cmp found actual
537'
538
539test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
540 echo loop 10 >expect &&
541 echo HEAD:loop1 >>expect &&
542 echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
543 test_cmp expect actual
544'
545
546test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
547 echo HEAD:morx | git cat-file --batch >expect &&
548 echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
549 test_cmp expect actual
550'
551
552test_expect_success 'cat-file --batch-all-objects shows all objects' '
553 # make new repos so we know the full set of objects; we will
554 # also make sure that there are some packed and some loose
555 # objects, some referenced and some not, some duplicates, and that
556 # there are some available only via alternates.
557 git init all-one &&
558 (
559 cd all-one &&
560 echo content >file &&
561 git add file &&
562 git commit -qm base &&
563 git rev-parse HEAD HEAD^{tree} HEAD:file &&
564 git repack -ad &&
565 echo not-cloned | git hash-object -w --stdin
566 ) >expect.unsorted &&
567 git clone -s all-one all-two &&
568 (
569 cd all-two &&
570 echo local-unref | git hash-object -w --stdin
571 ) >>expect.unsorted &&
572 git -C all-two rev-parse HEAD:file |
573 git -C all-two pack-objects .git/objects/pack/pack &&
574 sort <expect.unsorted >expect &&
575 git -C all-two cat-file --batch-all-objects \
576 --batch-check="%(objectname)" >actual &&
577 test_cmp expect actual
578'
579
580# The only user-visible difference is that the objects are no longer sorted,
581# and the resulting sort order is undefined. So we can only check that it
582# produces the same objects as the ordered case, but that at least exercises
583# the code.
584test_expect_success 'cat-file --unordered works' '
585 git -C all-two cat-file --batch-all-objects --unordered \
586 --batch-check="%(objectname)" >actual.unsorted &&
587 sort <actual.unsorted >actual &&
588 test_cmp expect actual
589'
590
591test_done