3 test_description
="git hash-object"
5 TEST_PASSES_SANITIZE_LEAK
=true
8 echo_without_newline
() {
12 test_blob_does_not_exist
() {
13 test_expect_success
'blob does not exist in database' "
14 test_must_fail git cat-file blob $1
19 test_expect_success
'blob exists in database' "
24 hello_content
="Hello World"
25 example_content
="This is an example"
28 echo_without_newline
"$hello_content" > hello
29 echo_without_newline
"$example_content" > example
34 test_create_repo
$test_repo
45 test_expect_success
'setup' '
48 hello sha1:5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
49 hello sha256:1e3b6c04d2eeb2b3e45c8a330445404c0b7cc7b257e2b097167d26f5230090c4
51 example sha1:ddd3f836d3e3fbb7ae289aa9ae83536f76956399
52 example sha256:b44fe1fe65589848253737db859bd490453510719d7424daab03daf0767b85ae
58 test_expect_success
"multiple '--stdin's are rejected" '
59 echo example | test_must_fail git hash-object --stdin --stdin
62 test_expect_success
"Can't use --stdin and --stdin-paths together" '
63 echo example | test_must_fail git hash-object --stdin --stdin-paths &&
64 echo example | test_must_fail git hash-object --stdin-paths --stdin
67 test_expect_success
"Can't pass filenames as arguments with --stdin-paths" '
68 echo example | test_must_fail git hash-object --stdin-paths hello
71 test_expect_success
"Can't use --path with --stdin-paths" '
72 echo example | test_must_fail git hash-object --stdin-paths --path=foo
75 test_expect_success
"Can't use --path with --no-filters" '
76 test_must_fail git hash-object --no-filters --path=foo
83 test_expect_success
'hash a file' '
84 test "$(test_oid hello)" = $(git hash-object hello)
87 test_blob_does_not_exist
"$(test_oid hello)"
89 test_expect_success
'hash from stdin' '
90 test "$(test_oid example)" = $(git hash-object --stdin < example)
93 test_blob_does_not_exist
"$(test_oid example)"
95 test_expect_success
'hash a file and write to database' '
96 test "$(test_oid hello)" = $(git hash-object -w hello)
99 test_blob_exists
"$(test_oid hello)"
101 test_expect_success
'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
103 obname0=$(echo bar | git hash-object --stdin) &&
104 obname1=$(git hash-object file1) &&
105 obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
106 obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
107 test "$obname0" = "$obname0new" &&
108 test "$obname1" = "$obname1new"
111 test_expect_success
'set up crlf tests' '
112 echo fooQ | tr Q "\\015" >file0 &&
114 echo "file0 -crlf" >.gitattributes &&
115 echo "file1 crlf" >>.gitattributes &&
116 git config core.autocrlf true &&
117 file0_sha=$(git hash-object file0) &&
118 file1_sha=$(git hash-object file1) &&
119 test "$file0_sha" != "$file1_sha"
122 test_expect_success
'check that appropriate filter is invoke when --path is used' '
123 path1_sha=$(git hash-object --path=file1 file0) &&
124 path0_sha=$(git hash-object --path=file0 file1) &&
125 test "$file0_sha" = "$path0_sha" &&
126 test "$file1_sha" = "$path1_sha" &&
127 path1_sha=$(git hash-object --path=file1 --stdin <file0) &&
128 path0_sha=$(git hash-object --path=file0 --stdin <file1) &&
129 test "$file0_sha" = "$path0_sha" &&
130 test "$file1_sha" = "$path1_sha"
133 test_expect_success
'gitattributes also work in a subdirectory' '
137 subdir_sha0=$(git hash-object ../file0) &&
138 subdir_sha1=$(git hash-object ../file1) &&
139 test "$file0_sha" = "$subdir_sha0" &&
140 test "$file1_sha" = "$subdir_sha1"
144 test_expect_success
'--path works in a subdirectory' '
147 path1_sha=$(git hash-object --path=../file1 ../file0) &&
148 path0_sha=$(git hash-object --path=../file0 ../file1) &&
149 test "$file0_sha" = "$path0_sha" &&
150 test "$file1_sha" = "$path1_sha"
154 test_expect_success
'check that --no-filters option works' '
155 nofilters_file1=$(git hash-object --no-filters file1) &&
156 test "$file0_sha" = "$nofilters_file1" &&
157 nofilters_file1=$(git hash-object --stdin <file1) &&
158 test "$file0_sha" = "$nofilters_file1"
161 test_expect_success
'check that --no-filters option works with --stdin-paths' '
162 nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
163 test "$file0_sha" = "$nofilters_file1"
168 for args
in "-w --stdin" "--stdin -w"; do
171 test_expect_success
"hash from stdin and write to database ($args)" '
172 test "$(test_oid example)" = $(git hash-object $args < example)
175 test_blob_exists
"$(test_oid example)"
183 oids
="$(test_oid hello)
186 test_expect_success
"hash two files with names on stdin" '
187 test "$oids" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
190 for args
in "-w --stdin-paths" "--stdin-paths -w"; do
193 test_expect_success
"hash two files with names on stdin and write to database ($args)" '
194 test "$oids" = "$(echo_without_newline "$filenames" | git hash-object $args)"
197 test_blob_exists
"$(test_oid hello)"
198 test_blob_exists
"$(test_oid example)"
203 test_expect_success
'too-short tree' '
204 echo abc >malformed-tree &&
205 test_must_fail git hash-object -t tree malformed-tree 2>err &&
206 grep "too-short tree object" err
209 test_expect_success
'malformed mode in tree' '
210 hex_oid=$(echo foo | git hash-object --stdin -w) &&
211 bin_oid=$(echo $hex_oid | hex2oct) &&
212 printf "9100644 \0$bin_oid" >tree-with-malformed-mode &&
213 test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err &&
214 grep "malformed mode in tree entry" err
217 test_expect_success
'empty filename in tree' '
218 hex_oid=$(echo foo | git hash-object --stdin -w) &&
219 bin_oid=$(echo $hex_oid | hex2oct) &&
220 printf "100644 \0$bin_oid" >tree-with-empty-filename &&
221 test_must_fail git hash-object -t tree tree-with-empty-filename 2>err &&
222 grep "empty filename in tree entry" err
225 test_expect_success
'duplicate filename in tree' '
226 hex_oid=$(echo foo | git hash-object --stdin -w) &&
227 bin_oid=$(echo $hex_oid | hex2oct) &&
229 printf "100644 file\0$bin_oid" &&
230 printf "100644 file\0$bin_oid"
231 } >tree-with-duplicate-filename &&
232 test_must_fail git hash-object -t tree tree-with-duplicate-filename 2>err &&
233 grep "duplicateEntries" err
236 test_expect_success
'corrupt commit' '
237 test_must_fail git hash-object -t commit --stdin </dev/null
240 test_expect_success
'corrupt tag' '
241 test_must_fail git hash-object -t tag --stdin </dev/null
244 test_expect_success
'hash-object complains about bogus type name' '
245 test_must_fail git hash-object -t bogus --stdin </dev/null
248 test_expect_success
'hash-object complains about truncated type name' '
249 test_must_fail git hash-object -t bl --stdin </dev/null
252 test_expect_success
'--literally' '
254 echo example | git hash-object -t $t --literally --stdin
257 test_expect_success
'--literally with extra-long type' '
258 t=12345678901234567890123456789012345678901234567890 &&
259 t="$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t" &&
260 echo example | git hash-object -t $t --literally --stdin
263 test_expect_success
'--stdin outside of repository (uses SHA-1)' '
264 nongit git hash-object --stdin <hello >actual &&
265 echo "$(test_oid --hash=sha1 hello)" >expect &&
266 test_cmp expect actual