Raw
1 #!/bin/sh
2
3 test_description="git hash-object"
4
5 . ./test-lib.sh
6
7 echo_without_newline() {
8 printf '%s' "$*"
9 }
10
11 test_blob_does_not_exist() {
12 test_expect_success 'blob does not exist in database' "
13 test_must_fail git cat-file blob $1
14 "
15 }
16
17 test_blob_exists() {
18 test_expect_success 'blob exists in database' "
19 git cat-file blob $1
20 "
21 }
22
23 hello_content="Hello World"
24 example_content="This is an example"
25
26 setup_repo() {
27 echo_without_newline "$hello_content" > hello
28 echo_without_newline "$example_content" > example
29 }
30
31 test_repo=test
32 push_repo() {
33 git init --quiet $test_repo
34 cd $test_repo
35
36 setup_repo
37 }
38
39 pop_repo() {
40 cd ..
41 rm -rf $test_repo
42 }
43
44 test_expect_success 'setup' '
45 setup_repo &&
46 test_oid_cache <<-EOF
47 hello sha1:5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
48 hello sha256:1e3b6c04d2eeb2b3e45c8a330445404c0b7cc7b257e2b097167d26f5230090c4
49
50 example sha1:ddd3f836d3e3fbb7ae289aa9ae83536f76956399
51 example sha256:b44fe1fe65589848253737db859bd490453510719d7424daab03daf0767b85ae
52
53 large5GB sha1:0be2be10a4c8764f32c4bf372a98edc731a4b204
54 large5GB sha256:dc18ca621300c8d3cfa505a275641ebab00de189859e022a975056882d313e64
55 EOF
56 '
57
58 # Argument checking
59
60 test_expect_success "multiple '--stdin's are rejected" '
61 echo example | test_must_fail git hash-object --stdin --stdin
62 '
63
64 test_expect_success "Can't use --stdin and --stdin-paths together" '
65 echo example | test_must_fail git hash-object --stdin --stdin-paths &&
66 echo example | test_must_fail git hash-object --stdin-paths --stdin
67 '
68
69 test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
70 echo example | test_must_fail git hash-object --stdin-paths hello
71 '
72
73 test_expect_success "Can't use --path with --stdin-paths" '
74 echo example | test_must_fail git hash-object --stdin-paths --path=foo
75 '
76
77 test_expect_success "Can't use --path with --no-filters" '
78 test_must_fail git hash-object --no-filters --path=foo
79 '
80
81 # Behavior
82
83 push_repo
84
85 test_expect_success 'hash a file' '
86 test "$(test_oid hello)" = $(git hash-object hello)
87 '
88
89 test_blob_does_not_exist "$(test_oid hello)"
90
91 test_expect_success 'hash from stdin' '
92 test "$(test_oid example)" = $(git hash-object --stdin < example)
93 '
94
95 test_blob_does_not_exist "$(test_oid example)"
96
97 test_expect_success 'hash a file and write to database' '
98 test "$(test_oid hello)" = $(git hash-object -w hello)
99 '
100
101 test_blob_exists "$(test_oid hello)"
102
103 test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
104 echo foo > file1 &&
105 obname0=$(echo bar | git hash-object --stdin) &&
106 obname1=$(git hash-object file1) &&
107 obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
108 obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
109 test "$obname0" = "$obname0new" &&
110 test "$obname1" = "$obname1new"
111 '
112
113 test_expect_success 'set up crlf tests' '
114 echo fooQ | tr Q "\\015" >file0 &&
115 cp file0 file1 &&
116 echo "file0 -crlf" >.gitattributes &&
117 echo "file1 crlf" >>.gitattributes &&
118 git config core.autocrlf true &&
119 file0_sha=$(git hash-object file0) &&
120 file1_sha=$(git hash-object file1) &&
121 test "$file0_sha" != "$file1_sha"
122 '
123
124 test_expect_success 'check that appropriate filter is invoke when --path is used' '
125 path1_sha=$(git hash-object --path=file1 file0) &&
126 path0_sha=$(git hash-object --path=file0 file1) &&
127 test "$file0_sha" = "$path0_sha" &&
128 test "$file1_sha" = "$path1_sha" &&
129 path1_sha=$(git hash-object --path=file1 --stdin <file0) &&
130 path0_sha=$(git hash-object --path=file0 --stdin <file1) &&
131 test "$file0_sha" = "$path0_sha" &&
132 test "$file1_sha" = "$path1_sha"
133 '
134
135 test_expect_success 'gitattributes also work in a subdirectory' '
136 mkdir subdir &&
137 (
138 cd subdir &&
139 subdir_sha0=$(git hash-object ../file0) &&
140 subdir_sha1=$(git hash-object ../file1) &&
141 test "$file0_sha" = "$subdir_sha0" &&
142 test "$file1_sha" = "$subdir_sha1"
143 )
144 '
145
146 test_expect_success '--path works in a subdirectory' '
147 (
148 cd subdir &&
149 path1_sha=$(git hash-object --path=../file1 ../file0) &&
150 path0_sha=$(git hash-object --path=../file0 ../file1) &&
151 test "$file0_sha" = "$path0_sha" &&
152 test "$file1_sha" = "$path1_sha"
153 )
154 '
155
156 test_expect_success 'check that --no-filters option works' '
157 nofilters_file1=$(git hash-object --no-filters file1) &&
158 test "$file0_sha" = "$nofilters_file1" &&
159 nofilters_file1=$(git hash-object --stdin <file1) &&
160 test "$file0_sha" = "$nofilters_file1"
161 '
162
163 test_expect_success 'check that --no-filters option works with --stdin-paths' '
164 nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
165 test "$file0_sha" = "$nofilters_file1"
166 '
167
168 pop_repo
169
170 for args in "-w --stdin" "--stdin -w"; do
171 push_repo
172
173 test_expect_success "hash from stdin and write to database ($args)" '
174 test "$(test_oid example)" = $(git hash-object $args < example)
175 '
176
177 test_blob_exists "$(test_oid example)"
178
179 pop_repo
180 done
181
182 filenames="hello
183 example"
184
185 oids="$(test_oid hello)
186 $(test_oid example)"
187
188 test_expect_success "hash two files with names on stdin" '
189 test "$oids" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
190 '
191
192 for args in "-w --stdin-paths" "--stdin-paths -w"; do
193 push_repo
194
195 test_expect_success "hash two files with names on stdin and write to database ($args)" '
196 test "$oids" = "$(echo_without_newline "$filenames" | git hash-object $args)"
197 '
198
199 test_blob_exists "$(test_oid hello)"
200 test_blob_exists "$(test_oid example)"
201
202 pop_repo
203 done
204
205 test_expect_success 'too-short tree' '
206 echo abc >malformed-tree &&
207 test_must_fail git hash-object -t tree malformed-tree 2>err &&
208 test_grep "too-short tree object" err
209 '
210
211 test_expect_success PERL_TEST_HELPERS 'malformed mode in tree' '
212 hex_oid=$(echo foo | git hash-object --stdin -w) &&
213 bin_oid=$(echo $hex_oid | hex2oct) &&
214 printf "9100644 \0$bin_oid" >tree-with-malformed-mode &&
215 test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err &&
216 test_grep "malformed mode in tree entry" err
217 '
218
219 test_expect_success PERL_TEST_HELPERS 'empty filename in tree' '
220 hex_oid=$(echo foo | git hash-object --stdin -w) &&
221 bin_oid=$(echo $hex_oid | hex2oct) &&
222 printf "100644 \0$bin_oid" >tree-with-empty-filename &&
223 test_must_fail git hash-object -t tree tree-with-empty-filename 2>err &&
224 test_grep "empty filename in tree entry" err
225 '
226
227 test_expect_success PERL_TEST_HELPERS 'duplicate filename in tree' '
228 hex_oid=$(echo foo | git hash-object --stdin -w) &&
229 bin_oid=$(echo $hex_oid | hex2oct) &&
230 {
231 printf "100644 file\0$bin_oid" &&
232 printf "100644 file\0$bin_oid"
233 } >tree-with-duplicate-filename &&
234 test_must_fail git hash-object -t tree tree-with-duplicate-filename 2>err &&
235 test_grep "duplicateEntries" err
236 '
237
238 test_expect_success 'corrupt commit' '
239 test_must_fail git hash-object -t commit --stdin </dev/null
240 '
241
242 test_expect_success 'corrupt tag' '
243 test_must_fail git hash-object -t tag --stdin </dev/null
244 '
245
246 test_expect_success 'hash-object complains about bogus type name' '
247 test_must_fail git hash-object -t bogus --stdin </dev/null
248 '
249
250 test_expect_success 'hash-object complains about truncated type name' '
251 test_must_fail git hash-object -t bl --stdin </dev/null
252 '
253
254 test_expect_success '--literally complains about non-standard types' '
255 test_must_fail git hash-object -t bogus --literally --stdin
256 '
257
258 test_expect_success '--stdin outside of repository (uses default hash)' '
259 nongit git hash-object --stdin <hello >actual &&
260 echo "$(test_oid --hash=builtin hello)" >expect &&
261 test_cmp expect actual
262 '
263
264 test_expect_success EXPENSIVE,SIZE_T_IS_64BIT \
265 'files over 4GB hash literally' '
266 test-tool genzeros $((5*1024*1024*1024)) >big &&
267 test_oid large5GB >expect &&
268 git hash-object --stdin --literally <big >actual &&
269 test_cmp expect actual
270 '
271
272 test_expect_success EXPENSIVE,SIZE_T_IS_64BIT \
273 'files over 4GB hash correctly via --stdin' '
274 { test -f big || test-tool genzeros $((5*1024*1024*1024)) >big; } &&
275 test_oid large5GB >expect &&
276 git hash-object --stdin <big >actual &&
277 test_cmp expect actual
278 '
279
280 test_expect_success EXPENSIVE,SIZE_T_IS_64BIT \
281 'files over 4GB hash correctly' '
282 { test -f big || test-tool genzeros $((5*1024*1024*1024)) >big; } &&
283 test_oid large5GB >expect &&
284 git hash-object -- big >actual &&
285 test_cmp expect actual
286 '
287
288 # This clean filter does nothing, other than excercising the interface.
289 # We ensure that cleaning doesn't mangle large files on 64-bit Windows.
290 test_expect_success EXPENSIVE,SIZE_T_IS_64BIT \
291 'hash filtered files over 4GB correctly' '
292 { test -f big || test-tool genzeros $((5*1024*1024*1024)) >big; } &&
293 test_oid large5GB >expect &&
294 test_config filter.null-filter.clean "cat" &&
295 echo "big filter=null-filter" >.gitattributes &&
296 git hash-object -- big >actual &&
297 test_cmp expect actual
298 '
299
300 test_done