Merge branch 'jc/cvsserver-perm-bit-fix' into maint
[git] / t / t1006-cat-file.sh
1 #!/bin/sh
2
3 test_description='git cat-file'
4
5 . ./test-lib.sh
6
7 echo_without_newline () {
8     printf '%s' "$*"
9 }
10
11 strlen () {
12     echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
13 }
14
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]$//')"
20     fi
21 }
22
23 run_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 -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
55     '
56
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
61     '
62
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
68     '
69
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
74     '
75
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
80     '
81 }
82
83 hello_content="Hello World"
84 hello_size=$(strlen "$hello_content")
85 hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
86
87 test_expect_success "setup" '
88         echo_without_newline "$hello_content" > hello &&
89         git update-index --add hello
90 '
91
92 run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
93
94 tree_sha1=$(git write-tree)
95 tree_size=33
96 tree_pretty_content="100644 blob $hello_sha1    hello"
97
98 run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
99
100 commit_message="Initial commit"
101 commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
102 commit_size=177
103 commit_content="tree $tree_sha1
104 author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000
105 committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000
106
107 $commit_message"
108
109 run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1
110
111 tag_header_without_timestamp="object $hello_sha1
112 type blob
113 tag hellotag
114 tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
115 tag_description="This is a tag"
116 tag_content="$tag_header_without_timestamp 0000000000 +0000
117
118 $tag_description"
119
120 tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
121 tag_size=$(strlen "$tag_content")
122
123 run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
124
125 test_expect_success \
126     "Reach a blob from a tag pointing to it" \
127     "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
128
129 for batch in batch batch-check
130 do
131     for opt in t s e p
132     do
133         test_expect_success "Passing -$opt with --$batch fails" '
134             test_must_fail git cat-file --$batch -$opt $hello_sha1
135         '
136
137         test_expect_success "Passing --$batch with -$opt fails" '
138             test_must_fail git cat-file -$opt --$batch $hello_sha1
139         '
140     done
141
142     test_expect_success "Passing <type> with --$batch fails" '
143         test_must_fail git cat-file --$batch blob $hello_sha1
144     '
145
146     test_expect_success "Passing --$batch with <type> fails" '
147         test_must_fail git cat-file blob --$batch $hello_sha1
148     '
149
150     test_expect_success "Passing sha1 with --$batch fails" '
151         test_must_fail git cat-file --$batch $hello_sha1
152     '
153 done
154
155 test_expect_success "--batch-check for a non-existent named object" '
156     test "foobar42 missing
157 foobar84 missing" = \
158     "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
159 '
160
161 test_expect_success "--batch-check for a non-existent hash" '
162     test "0000000000000000000000000000000000000042 missing
163 0000000000000000000000000000000000000084 missing" = \
164     "$( ( echo 0000000000000000000000000000000000000042;
165          echo_without_newline 0000000000000000000000000000000000000084; ) \
166        | git cat-file --batch-check)"
167 '
168
169 test_expect_success "--batch for an existent and a non-existent hash" '
170     test "$tag_sha1 tag $tag_size
171 $tag_content
172 0000000000000000000000000000000000000000 missing" = \
173     "$( ( echo $tag_sha1;
174          echo_without_newline 0000000000000000000000000000000000000000; ) \
175        | git cat-file --batch)"
176 '
177
178 test_expect_success "--batch-check for an emtpy line" '
179     test " missing" = "$(echo | git cat-file --batch-check)"
180 '
181
182 batch_input="$hello_sha1
183 $commit_sha1
184 $tag_sha1
185 deadbeef
186
187 "
188
189 batch_output="$hello_sha1 blob $hello_size
190 $hello_content
191 $commit_sha1 commit $commit_size
192 $commit_content
193 $tag_sha1 tag $tag_size
194 $tag_content
195 deadbeef missing
196  missing"
197
198 test_expect_success '--batch with multiple sha1s gives correct format' '
199         test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
200 '
201
202 batch_check_input="$hello_sha1
203 $tree_sha1
204 $commit_sha1
205 $tag_sha1
206 deadbeef
207
208 "
209
210 batch_check_output="$hello_sha1 blob $hello_size
211 $tree_sha1 tree $tree_size
212 $commit_sha1 commit $commit_size
213 $tag_sha1 tag $tag_size
214 deadbeef missing
215  missing"
216
217 test_expect_success "--batch-check with multiple sha1s gives correct format" '
218     test "$batch_check_output" = \
219     "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
220 '
221
222 test_done