Merge branch 'db/show-ref-head'
[git/mingw.git] / t / t1006-cat-file.sh
blobd499d02a29f3ec5fb15d86835d24ccabd322df56
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 -z "$content" ||
51 test_expect_success "Content of $type is correct" '
52 maybe_remove_timestamp "$content" $no_ts >expect &&
53 maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts >actual &&
54 test_cmp expect actual
57 test_expect_success "Pretty content of $type is correct" '
58 maybe_remove_timestamp "$pretty_content" $no_ts >expect &&
59 maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts >actual &&
60 test_cmp expect actual
63 test -z "$content" ||
64 test_expect_success "--batch output of $type is correct" '
65 maybe_remove_timestamp "$batch_output" $no_ts >expect &&
66 maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" $no_ts >actual &&
67 test_cmp expect actual
70 test_expect_success "--batch-check output of $type is correct" '
71 echo "$sha1 $type $size" >expect &&
72 echo_without_newline $sha1 | git cat-file --batch-check >actual &&
73 test_cmp expect actual
76 test_expect_success "custom --batch-check format" '
77 echo "$type $sha1" >expect &&
78 echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
79 test_cmp expect actual
82 test_expect_success '--batch-check with %(rest)' '
83 echo "$type this is some extra content" >expect &&
84 echo "$sha1 this is some extra content" |
85 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
86 test_cmp expect actual
90 hello_content="Hello World"
91 hello_size=$(strlen "$hello_content")
92 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
94 test_expect_success "setup" '
95 echo_without_newline "$hello_content" > hello &&
96 git update-index --add hello
99 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
101 tree_sha1=$(git write-tree)
102 tree_size=33
103 tree_pretty_content="100644 blob $hello_sha1 hello"
105 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
107 commit_message="Initial commit"
108 commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
109 commit_size=177
110 commit_content="tree $tree_sha1
111 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
112 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
114 $commit_message"
116 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
118 tag_header_without_timestamp="object $hello_sha1
119 type blob
120 tag hellotag
121 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
122 tag_description="This is a tag"
123 tag_content="$tag_header_without_timestamp 0000000000 +0000
125 $tag_description"
127 tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
128 tag_size=$(strlen "$tag_content")
130 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
132 test_expect_success \
133 "Reach a blob from a tag pointing to it" \
134 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
136 for batch in batch batch-check
138 for opt in t s e p
140 test_expect_success "Passing -$opt with --$batch fails" '
141 test_must_fail git cat-file --$batch -$opt $hello_sha1
144 test_expect_success "Passing --$batch with -$opt fails" '
145 test_must_fail git cat-file -$opt --$batch $hello_sha1
147 done
149 test_expect_success "Passing <type> with --$batch fails" '
150 test_must_fail git cat-file --$batch blob $hello_sha1
153 test_expect_success "Passing --$batch with <type> fails" '
154 test_must_fail git cat-file blob --$batch $hello_sha1
157 test_expect_success "Passing sha1 with --$batch fails" '
158 test_must_fail git cat-file --$batch $hello_sha1
160 done
162 test_expect_success "--batch-check for a non-existent named object" '
163 test "foobar42 missing
164 foobar84 missing" = \
165 "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
168 test_expect_success "--batch-check for a non-existent hash" '
169 test "0000000000000000000000000000000000000042 missing
170 0000000000000000000000000000000000000084 missing" = \
171 "$( ( echo 0000000000000000000000000000000000000042;
172 echo_without_newline 0000000000000000000000000000000000000084; ) \
173 | git cat-file --batch-check)"
176 test_expect_success "--batch for an existent and a non-existent hash" '
177 test "$tag_sha1 tag $tag_size
178 $tag_content
179 0000000000000000000000000000000000000000 missing" = \
180 "$( ( echo $tag_sha1;
181 echo_without_newline 0000000000000000000000000000000000000000; ) \
182 | git cat-file --batch)"
185 test_expect_success "--batch-check for an emtpy line" '
186 test " missing" = "$(echo | git cat-file --batch-check)"
189 batch_input="$hello_sha1
190 $commit_sha1
191 $tag_sha1
192 deadbeef
196 batch_output="$hello_sha1 blob $hello_size
197 $hello_content
198 $commit_sha1 commit $commit_size
199 $commit_content
200 $tag_sha1 tag $tag_size
201 $tag_content
202 deadbeef missing
203 missing"
205 test_expect_success '--batch with multiple sha1s gives correct format' '
206 test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
209 batch_check_input="$hello_sha1
210 $tree_sha1
211 $commit_sha1
212 $tag_sha1
213 deadbeef
217 batch_check_output="$hello_sha1 blob $hello_size
218 $tree_sha1 tree $tree_size
219 $commit_sha1 commit $commit_size
220 $tag_sha1 tag $tag_size
221 deadbeef missing
222 missing"
224 test_expect_success "--batch-check with multiple sha1s gives correct format" '
225 test "$batch_check_output" = \
226 "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
229 test_done