Merge branch 'da/mergetool-winmerge'
[git.git] / t / t1006-cat-file.sh
blob4f225db9a72729e99f5eaab0b81ec579691b34c7
1 #!/bin/sh
3 test_description='git cat-file'
5 . ./test-lib.sh
7 echo_without_newline () {
8 printf '%s' "$*"
11 strlen () {
12 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
15 maybe_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]$//')"
23 run_tests () {
24 type=$1
25 sha1=$2
26 size=$3
27 content=$4
28 pretty_content=$5
29 no_ts=$6
31 batch_output="$sha1 $type $size
32 $content"
34 test_expect_success "$type exists" '
35 git cat-file -e $sha1
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
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
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
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
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
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
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
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
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
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
101 test -z "$content" ||
102 test_expect_success "--batch without type ($type)" '
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
112 test -z "$content" ||
113 test_expect_success "--batch without size ($type)" '
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
124 hello_content="Hello World"
125 hello_size=$(strlen "$hello_content")
126 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
128 test_expect_success "setup" '
129 echo_without_newline "$hello_content" > hello &&
130 git update-index --add hello
133 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
135 test_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
143 tree_sha1=$(git write-tree)
144 tree_size=33
145 tree_pretty_content="100644 blob $hello_sha1 hello"
147 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
149 commit_message="Initial commit"
150 commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
151 commit_size=177
152 commit_content="tree $tree_sha1
153 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
154 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
156 $commit_message"
158 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
160 tag_header_without_timestamp="object $hello_sha1
161 type blob
162 tag hellotag
163 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
164 tag_description="This is a tag"
165 tag_content="$tag_header_without_timestamp 0000000000 +0000
167 $tag_description"
169 tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
170 tag_size=$(strlen "$tag_content")
172 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
174 test_expect_success \
175 "Reach a blob from a tag pointing to it" \
176 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
178 for batch in batch batch-check
180 for opt in t s e p
182 test_expect_success "Passing -$opt with --$batch fails" '
183 test_must_fail git cat-file --$batch -$opt $hello_sha1
186 test_expect_success "Passing --$batch with -$opt fails" '
187 test_must_fail git cat-file -$opt --$batch $hello_sha1
189 done
191 test_expect_success "Passing <type> with --$batch fails" '
192 test_must_fail git cat-file --$batch blob $hello_sha1
195 test_expect_success "Passing --$batch with <type> fails" '
196 test_must_fail git cat-file blob --$batch $hello_sha1
199 test_expect_success "Passing sha1 with --$batch fails" '
200 test_must_fail git cat-file --$batch $hello_sha1
202 done
204 test_expect_success "--batch-check for a non-existent named object" '
205 test "foobar42 missing
206 foobar84 missing" = \
207 "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
210 test_expect_success "--batch-check for a non-existent hash" '
211 test "0000000000000000000000000000000000000042 missing
212 0000000000000000000000000000000000000084 missing" = \
213 "$( ( echo 0000000000000000000000000000000000000042;
214 echo_without_newline 0000000000000000000000000000000000000084; ) \
215 | git cat-file --batch-check)"
218 test_expect_success "--batch for an existent and a non-existent hash" '
219 test "$tag_sha1 tag $tag_size
220 $tag_content
221 0000000000000000000000000000000000000000 missing" = \
222 "$( ( echo $tag_sha1;
223 echo_without_newline 0000000000000000000000000000000000000000; ) \
224 | git cat-file --batch)"
227 test_expect_success "--batch-check for an emtpy line" '
228 test " missing" = "$(echo | git cat-file --batch-check)"
231 test_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
237 batch_input="$hello_sha1
238 $commit_sha1
239 $tag_sha1
240 deadbeef
244 batch_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
250 deadbeef missing
251 missing"
253 test_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)"
257 batch_check_input="$hello_sha1
258 $tree_sha1
259 $commit_sha1
260 $tag_sha1
261 deadbeef
265 batch_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
269 deadbeef missing
270 missing"
272 test_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)"
277 test_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
288 test_expect_success 'confirm that neither loose blob is a delta' '
289 cat >expect <<-EOF &&
290 $_z40
291 $_z40
293 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
294 test_cmp expect actual
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.
302 test_expect_success '%(deltabase) reports packed delta bases' '
303 git repack -ad &&
304 git cat-file --batch-check="%(deltabase)" <blobs >actual &&
306 grep "$(git rev-parse HEAD:foo)" actual ||
307 grep "$(git rev-parse HEAD:foo-plus)" actual
311 bogus_type="bogus"
312 bogus_content="bogus"
313 bogus_size=$(strlen "$bogus_content")
314 bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
316 test_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
322 test_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
327 bogus_type="abcdefghijklmnopqrstuvwxyz1234679"
328 bogus_content="bogus"
329 bogus_size=$(strlen "$bogus_content")
330 bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin)
332 test_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
338 test_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
344 test_done