]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1007-hash-object.sh
Merge branch 'ps/pack-refs-auto'
[thirdparty/git.git] / t / t1007-hash-object.sh
1 #!/bin/sh
2
3 test_description="git hash-object"
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 echo_without_newline() {
9 printf '%s' "$*"
10 }
11
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
15 "
16 }
17
18 test_blob_exists() {
19 test_expect_success 'blob exists in database' "
20 git cat-file blob $1
21 "
22 }
23
24 hello_content="Hello World"
25 example_content="This is an example"
26
27 setup_repo() {
28 echo_without_newline "$hello_content" > hello
29 echo_without_newline "$example_content" > example
30 }
31
32 test_repo=test
33 push_repo() {
34 test_create_repo $test_repo
35 cd $test_repo
36
37 setup_repo
38 }
39
40 pop_repo() {
41 cd ..
42 rm -rf $test_repo
43 }
44
45 test_expect_success 'setup' '
46 setup_repo &&
47 test_oid_cache <<-EOF
48 hello sha1:5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
49 hello sha256:1e3b6c04d2eeb2b3e45c8a330445404c0b7cc7b257e2b097167d26f5230090c4
50
51 example sha1:ddd3f836d3e3fbb7ae289aa9ae83536f76956399
52 example sha256:b44fe1fe65589848253737db859bd490453510719d7424daab03daf0767b85ae
53 EOF
54 '
55
56 # Argument checking
57
58 test_expect_success "multiple '--stdin's are rejected" '
59 echo example | test_must_fail git hash-object --stdin --stdin
60 '
61
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
65 '
66
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
69 '
70
71 test_expect_success "Can't use --path with --stdin-paths" '
72 echo example | test_must_fail git hash-object --stdin-paths --path=foo
73 '
74
75 test_expect_success "Can't use --path with --no-filters" '
76 test_must_fail git hash-object --no-filters --path=foo
77 '
78
79 # Behavior
80
81 push_repo
82
83 test_expect_success 'hash a file' '
84 test "$(test_oid hello)" = $(git hash-object hello)
85 '
86
87 test_blob_does_not_exist "$(test_oid hello)"
88
89 test_expect_success 'hash from stdin' '
90 test "$(test_oid example)" = $(git hash-object --stdin < example)
91 '
92
93 test_blob_does_not_exist "$(test_oid example)"
94
95 test_expect_success 'hash a file and write to database' '
96 test "$(test_oid hello)" = $(git hash-object -w hello)
97 '
98
99 test_blob_exists "$(test_oid hello)"
100
101 test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
102 echo foo > 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"
109 '
110
111 test_expect_success 'set up crlf tests' '
112 echo fooQ | tr Q "\\015" >file0 &&
113 cp file0 file1 &&
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"
120 '
121
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"
131 '
132
133 test_expect_success 'gitattributes also work in a subdirectory' '
134 mkdir subdir &&
135 (
136 cd subdir &&
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"
141 )
142 '
143
144 test_expect_success '--path works in a subdirectory' '
145 (
146 cd subdir &&
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"
151 )
152 '
153
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"
159 '
160
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"
164 '
165
166 pop_repo
167
168 for args in "-w --stdin" "--stdin -w"; do
169 push_repo
170
171 test_expect_success "hash from stdin and write to database ($args)" '
172 test "$(test_oid example)" = $(git hash-object $args < example)
173 '
174
175 test_blob_exists "$(test_oid example)"
176
177 pop_repo
178 done
179
180 filenames="hello
181 example"
182
183 oids="$(test_oid hello)
184 $(test_oid example)"
185
186 test_expect_success "hash two files with names on stdin" '
187 test "$oids" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
188 '
189
190 for args in "-w --stdin-paths" "--stdin-paths -w"; do
191 push_repo
192
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)"
195 '
196
197 test_blob_exists "$(test_oid hello)"
198 test_blob_exists "$(test_oid example)"
199
200 pop_repo
201 done
202
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
207 '
208
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
215 '
216
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
223 '
224
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) &&
228 {
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
234 '
235
236 test_expect_success 'corrupt commit' '
237 test_must_fail git hash-object -t commit --stdin </dev/null
238 '
239
240 test_expect_success 'corrupt tag' '
241 test_must_fail git hash-object -t tag --stdin </dev/null
242 '
243
244 test_expect_success 'hash-object complains about bogus type name' '
245 test_must_fail git hash-object -t bogus --stdin </dev/null
246 '
247
248 test_expect_success 'hash-object complains about truncated type name' '
249 test_must_fail git hash-object -t bl --stdin </dev/null
250 '
251
252 test_expect_success '--literally' '
253 t=1234567890 &&
254 echo example | git hash-object -t $t --literally --stdin
255 '
256
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
261 '
262
263 test_done