]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1006-cat-file.sh
Merge branch 'rs/janitorial'
[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
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
a8128ed6
AR
31 batch_output="$sha1 $type $size
32$content"
33
b335d3f1
AR
34 test_expect_success "$type exists" '
35 git cat-file -e $sha1
36 '
37
38 test_expect_success "Type of $type is correct" '
03c893cb
JK
39 echo $type >expect &&
40 git cat-file -t $sha1 >actual &&
41 test_cmp expect actual
b335d3f1
AR
42 '
43
44 test_expect_success "Size of $type is correct" '
03c893cb
JK
45 echo $size >expect &&
46 git cat-file -s $sha1 >actual &&
47 test_cmp expect actual
b335d3f1
AR
48 '
49
3e370f9f
KN
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
b335d3f1
AR
62 test -z "$content" ||
63 test_expect_success "Content of $type is correct" '
03c893cb
JK
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
b335d3f1
AR
67 '
68
69 test_expect_success "Pretty content of $type is correct" '
03c893cb
JK
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
b335d3f1 73 '
05d5667f 74
a8128ed6
AR
75 test -z "$content" ||
76 test_expect_success "--batch output of $type is correct" '
03c893cb
JK
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
a8128ed6
AR
80 '
81
05d5667f 82 test_expect_success "--batch-check output of $type is correct" '
03c893cb
JK
83 echo "$sha1 $type $size" >expect &&
84 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
85 test_cmp expect actual
05d5667f 86 '
93d2a607
JK
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 '
97be0407
JK
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 '
6554dfa9
JK
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 '
b335d3f1
AR
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
97be0407
JK
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
b335d3f1
AR
143tree_sha1=$(git write-tree)
144tree_size=33
145tree_pretty_content="100644 blob $hello_sha1 hello"
146
147run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
148
41ccfdd9 149commit_message="Initial commit"
b335d3f1 150commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
41ccfdd9 151commit_size=177
b335d3f1
AR
152commit_content="tree $tree_sha1
153author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
154committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
155
156$commit_message"
157
158run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
159
160tag_header_without_timestamp="object $hello_sha1
161type blob
162tag hellotag
163tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
164tag_description="This is a tag"
165tag_content="$tag_header_without_timestamp 0000000000 +0000
166
b335d3f1
AR
167$tag_description"
168
169tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
170tag_size=$(strlen "$tag_content")
171
9cfa5126 172run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
b335d3f1
AR
173
174test_expect_success \
175 "Reach a blob from a tag pointing to it" \
176 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
177
a8128ed6 178for batch in batch batch-check
05d5667f 179do
a8128ed6
AR
180 for opt in t s e p
181 do
182 test_expect_success "Passing -$opt with --$batch fails" '
183 test_must_fail git cat-file --$batch -$opt $hello_sha1
184 '
185
186 test_expect_success "Passing --$batch with -$opt fails" '
187 test_must_fail git cat-file -$opt --$batch $hello_sha1
188 '
189 done
190
191 test_expect_success "Passing <type> with --$batch fails" '
192 test_must_fail git cat-file --$batch blob $hello_sha1
05d5667f
AR
193 '
194
a8128ed6
AR
195 test_expect_success "Passing --$batch with <type> fails" '
196 test_must_fail git cat-file blob --$batch $hello_sha1
05d5667f 197 '
05d5667f 198
a8128ed6
AR
199 test_expect_success "Passing sha1 with --$batch fails" '
200 test_must_fail git cat-file --$batch $hello_sha1
201 '
202done
05d5667f 203
3c076dbe
LW
204test_expect_success "--batch-check for a non-existent named object" '
205 test "foobar42 missing
206foobar84 missing" = \
207 "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
208'
209
210test_expect_success "--batch-check for a non-existent hash" '
211 test "0000000000000000000000000000000000000042 missing
2120000000000000000000000000000000000000084 missing" = \
213 "$( ( echo 0000000000000000000000000000000000000042;
214 echo_without_newline 0000000000000000000000000000000000000084; ) \
215 | git cat-file --batch-check)"
216'
217
218test_expect_success "--batch for an existent and a non-existent hash" '
219 test "$tag_sha1 tag $tag_size
220$tag_content
2210000000000000000000000000000000000000000 missing" = \
222 "$( ( echo $tag_sha1;
223 echo_without_newline 0000000000000000000000000000000000000000; ) \
224 | git cat-file --batch)"
05d5667f
AR
225'
226
227test_expect_success "--batch-check for an emtpy line" '
228 test " missing" = "$(echo | git cat-file --batch-check)"
229'
230
4ef8d1dd
JH
231test_expect_success 'empty --batch-check notices missing object' '
232 echo "$_z40 missing" >expect &&
233 echo "$_z40" | git cat-file --batch-check="" >actual &&
234 test_cmp expect actual
235'
236
a8128ed6
AR
237batch_input="$hello_sha1
238$commit_sha1
239$tag_sha1
240deadbeef
241
242"
243
244batch_output="$hello_sha1 blob $hello_size
245$hello_content
246$commit_sha1 commit $commit_size
247$commit_content
248$tag_sha1 tag $tag_size
249$tag_content
250deadbeef missing
251 missing"
252
6c41e21d
MB
253test_expect_success '--batch with multiple sha1s gives correct format' '
254 test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
255'
a8128ed6 256
05d5667f
AR
257batch_check_input="$hello_sha1
258$tree_sha1
259$commit_sha1
260$tag_sha1
261deadbeef
262
263"
264
265batch_check_output="$hello_sha1 blob $hello_size
266$tree_sha1 tree $tree_size
267$commit_sha1 commit $commit_size
268$tag_sha1 tag $tag_size
269deadbeef missing
270 missing"
271
272test_expect_success "--batch-check with multiple sha1s gives correct format" '
273 test "$batch_check_output" = \
274 "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
275'
276
65ea9c3c
JK
277test_expect_success 'setup blobs which are likely to delta' '
278 test-genrandom foo 10240 >foo &&
279 { cat foo; echo plus; } >foo-plus &&
280 git add foo foo-plus &&
281 git commit -m foo &&
282 cat >blobs <<-\EOF
283 HEAD:foo
284 HEAD:foo-plus
285 EOF
286'
287
288test_expect_success 'confirm that neither loose blob is a delta' '
99094a7a 289 cat >expect <<-EOF &&
65ea9c3c
JK
290 $_z40
291 $_z40
292 EOF
293 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
294 test_cmp expect actual
295'
296
297# To avoid relying too much on the current delta heuristics,
298# we will check only that one of the two objects is a delta
299# against the other, but not the order. We can do so by just
300# asking for the base of both, and checking whether either
301# sha1 appears in the output.
302test_expect_success '%(deltabase) reports packed delta bases' '
303 git repack -ad &&
304 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
305 {
306 grep "$(git rev-parse HEAD:foo)" actual ||
307 grep "$(git rev-parse HEAD:foo-plus)" actual
308 }
309'
310
3e370f9f
KN
311bogus_type="bogus"
312bogus_content="bogus"
313bogus_size=$(strlen "$bogus_content")
314bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
315
316test_expect_success "Type of broken object is correct" '
317 echo $bogus_type >expect &&
318 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
319 test_cmp expect actual
320'
321
322test_expect_success "Size of broken object is correct" '
323 echo $bogus_size >expect &&
324 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
325 test_cmp expect actual
326'
327bogus_type="abcdefghijklmnopqrstuvwxyz1234679"
328bogus_content="bogus"
329bogus_size=$(strlen "$bogus_content")
330bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
331
332test_expect_success "Type of broken object is correct when type is large" '
333 echo $bogus_type >expect &&
334 git cat-file -t --allow-unknown-type $bogus_sha1 >actual &&
335 test_cmp expect actual
336'
337
338test_expect_success "Size of large broken object is correct when type is large" '
339 echo $bogus_size >expect &&
340 git cat-file -s --allow-unknown-type $bogus_sha1 >actual &&
341 test_cmp expect actual
342'
343
b335d3f1 344test_done