]>
Commit | Line | Data |
---|---|---|
8a2f5e5b GP |
1 | #!/bin/sh |
2 | ||
0cb0e143 | 3 | test_description="git hash-object" |
8a2f5e5b GP |
4 | |
5 | . ./test-lib.sh | |
6 | ||
3ea5a1b3 AR |
7 | echo_without_newline() { |
8 | printf '%s' "$*" | |
9 | } | |
10 | ||
11 | test_blob_does_not_exist() { | |
0fbdb52f | 12 | test_expect_success SHA1 'blob does not exist in database' " |
3ea5a1b3 AR |
13 | test_must_fail git cat-file blob $1 |
14 | " | |
15 | } | |
16 | ||
17 | test_blob_exists() { | |
0fbdb52f | 18 | test_expect_success SHA1 'blob exists in database' " |
3ea5a1b3 AR |
19 | git cat-file blob $1 |
20 | " | |
21 | } | |
22 | ||
23 | hello_content="Hello World" | |
24 | hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689 | |
25 | ||
26 | example_content="This is an example" | |
27 | example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399 | |
28 | ||
29 | setup_repo() { | |
30 | echo_without_newline "$hello_content" > hello | |
31 | echo_without_newline "$example_content" > example | |
32 | } | |
33 | ||
34 | test_repo=test | |
35 | push_repo() { | |
36 | test_create_repo $test_repo | |
37 | cd $test_repo | |
38 | ||
39 | setup_repo | |
40 | } | |
41 | ||
42 | pop_repo() { | |
43 | cd .. | |
44 | rm -rf $test_repo | |
45 | } | |
46 | ||
47 | setup_repo | |
48 | ||
49 | # Argument checking | |
50 | ||
51 | test_expect_success "multiple '--stdin's are rejected" ' | |
81014073 | 52 | echo example | test_must_fail git hash-object --stdin --stdin |
3ea5a1b3 AR |
53 | ' |
54 | ||
d8ee4832 | 55 | test_expect_success "Can't use --stdin and --stdin-paths together" ' |
81014073 DP |
56 | echo example | test_must_fail git hash-object --stdin --stdin-paths && |
57 | echo example | test_must_fail git hash-object --stdin-paths --stdin | |
d8ee4832 AR |
58 | ' |
59 | ||
60 | test_expect_success "Can't pass filenames as arguments with --stdin-paths" ' | |
81014073 | 61 | echo example | test_must_fail git hash-object --stdin-paths hello |
d8ee4832 AR |
62 | ' |
63 | ||
39702431 DP |
64 | test_expect_success "Can't use --path with --stdin-paths" ' |
65 | echo example | test_must_fail git hash-object --stdin-paths --path=foo | |
66 | ' | |
67 | ||
4a3d85dc DP |
68 | test_expect_success "Can't use --path with --no-filters" ' |
69 | test_must_fail git hash-object --no-filters --path=foo | |
70 | ' | |
71 | ||
3ea5a1b3 AR |
72 | # Behavior |
73 | ||
74 | push_repo | |
75 | ||
0fbdb52f | 76 | test_expect_success SHA1 'hash a file' ' |
3ea5a1b3 AR |
77 | test $hello_sha1 = $(git hash-object hello) |
78 | ' | |
79 | ||
80 | test_blob_does_not_exist $hello_sha1 | |
81 | ||
0fbdb52f | 82 | test_expect_success SHA1 'hash from stdin' ' |
3ea5a1b3 AR |
83 | test $example_sha1 = $(git hash-object --stdin < example) |
84 | ' | |
85 | ||
86 | test_blob_does_not_exist $example_sha1 | |
87 | ||
0fbdb52f | 88 | test_expect_success SHA1 'hash a file and write to database' ' |
3ea5a1b3 AR |
89 | test $hello_sha1 = $(git hash-object -w hello) |
90 | ' | |
91 | ||
92 | test_blob_exists $hello_sha1 | |
93 | ||
94 | test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' ' | |
95 | echo foo > file1 && | |
96 | obname0=$(echo bar | git hash-object --stdin) && | |
97 | obname1=$(git hash-object file1) && | |
98 | obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) && | |
99 | obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) && | |
100 | test "$obname0" = "$obname0new" && | |
101 | test "$obname1" = "$obname1new" | |
102 | ' | |
103 | ||
4d0efa10 | 104 | test_expect_success 'set up crlf tests' ' |
39702431 DP |
105 | echo fooQ | tr Q "\\015" >file0 && |
106 | cp file0 file1 && | |
107 | echo "file0 -crlf" >.gitattributes && | |
108 | echo "file1 crlf" >>.gitattributes && | |
109 | git config core.autocrlf true && | |
110 | file0_sha=$(git hash-object file0) && | |
111 | file1_sha=$(git hash-object file1) && | |
4d0efa10 JK |
112 | test "$file0_sha" != "$file1_sha" |
113 | ' | |
114 | ||
115 | test_expect_success 'check that appropriate filter is invoke when --path is used' ' | |
39702431 DP |
116 | path1_sha=$(git hash-object --path=file1 file0) && |
117 | path0_sha=$(git hash-object --path=file0 file1) && | |
118 | test "$file0_sha" = "$path0_sha" && | |
119 | test "$file1_sha" = "$path1_sha" && | |
120 | path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) && | |
121 | path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) && | |
122 | test "$file0_sha" = "$path0_sha" && | |
4d0efa10 | 123 | test "$file1_sha" = "$path1_sha" |
39702431 DP |
124 | ' |
125 | ||
0e94ee94 JK |
126 | test_expect_success 'gitattributes also work in a subdirectory' ' |
127 | mkdir subdir && | |
128 | ( | |
129 | cd subdir && | |
130 | subdir_sha0=$(git hash-object ../file0) && | |
131 | subdir_sha1=$(git hash-object ../file1) && | |
132 | test "$file0_sha" = "$subdir_sha0" && | |
133 | test "$file1_sha" = "$subdir_sha1" | |
134 | ) | |
135 | ' | |
136 | ||
a1be47e4 JK |
137 | test_expect_success '--path works in a subdirectory' ' |
138 | ( | |
139 | cd subdir && | |
140 | path1_sha=$(git hash-object --path=../file1 ../file0) && | |
141 | path0_sha=$(git hash-object --path=../file0 ../file1) && | |
142 | test "$file0_sha" = "$path0_sha" && | |
143 | test "$file1_sha" = "$path1_sha" | |
144 | ) | |
145 | ' | |
146 | ||
4a3d85dc | 147 | test_expect_success 'check that --no-filters option works' ' |
4a3d85dc DP |
148 | nofilters_file1=$(git hash-object --no-filters file1) && |
149 | test "$file0_sha" = "$nofilters_file1" && | |
150 | nofilters_file1=$(cat file1 | git hash-object --stdin) && | |
4d0efa10 | 151 | test "$file0_sha" = "$nofilters_file1" |
4a3d85dc DP |
152 | ' |
153 | ||
a9f97909 | 154 | test_expect_success 'check that --no-filters option works with --stdin-paths' ' |
a9f97909 | 155 | nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) && |
4d0efa10 | 156 | test "$file0_sha" = "$nofilters_file1" |
a9f97909 EFL |
157 | ' |
158 | ||
3ea5a1b3 AR |
159 | pop_repo |
160 | ||
161 | for args in "-w --stdin" "--stdin -w"; do | |
162 | push_repo | |
163 | ||
0fbdb52f | 164 | test_expect_success SHA1 "hash from stdin and write to database ($args)" ' |
3ea5a1b3 AR |
165 | test $example_sha1 = $(git hash-object $args < example) |
166 | ' | |
167 | ||
168 | test_blob_exists $example_sha1 | |
169 | ||
170 | pop_repo | |
171 | done | |
8a2f5e5b | 172 | |
d8ee4832 AR |
173 | filenames="hello |
174 | example" | |
175 | ||
176 | sha1s="$hello_sha1 | |
177 | $example_sha1" | |
178 | ||
0fbdb52f | 179 | test_expect_success SHA1 "hash two files with names on stdin" ' |
d8ee4832 AR |
180 | test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)" |
181 | ' | |
182 | ||
183 | for args in "-w --stdin-paths" "--stdin-paths -w"; do | |
184 | push_repo | |
185 | ||
0fbdb52f | 186 | test_expect_success SHA1 "hash two files with names on stdin and write to database ($args)" ' |
d8ee4832 AR |
187 | test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)" |
188 | ' | |
189 | ||
190 | test_blob_exists $hello_sha1 | |
191 | test_blob_exists $example_sha1 | |
192 | ||
193 | pop_repo | |
194 | done | |
195 | ||
2edffef2 | 196 | test_expect_success 'too-short tree' ' |
02380389 | 197 | echo abc >malformed-tree && |
2edffef2 JK |
198 | test_must_fail git hash-object -t tree malformed-tree 2>err && |
199 | test_i18ngrep "too-short tree object" err | |
200 | ' | |
201 | ||
2edffef2 JK |
202 | test_expect_success 'malformed mode in tree' ' |
203 | hex_sha1=$(echo foo | git hash-object --stdin -w) && | |
204 | bin_sha1=$(echo $hex_sha1 | hex2oct) && | |
205 | printf "9100644 \0$bin_sha1" >tree-with-malformed-mode && | |
206 | test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err && | |
207 | test_i18ngrep "malformed mode in tree entry" err | |
208 | ' | |
209 | ||
210 | test_expect_success 'empty filename in tree' ' | |
211 | hex_sha1=$(echo foo | git hash-object --stdin -w) && | |
212 | bin_sha1=$(echo $hex_sha1 | hex2oct) && | |
213 | printf "100644 \0$bin_sha1" >tree-with-empty-filename && | |
214 | test_must_fail git hash-object -t tree tree-with-empty-filename 2>err && | |
215 | test_i18ngrep "empty filename in tree entry" err | |
c879daa2 NTND |
216 | ' |
217 | ||
218 | test_expect_success 'corrupt commit' ' | |
219 | test_must_fail git hash-object -t commit --stdin </dev/null | |
220 | ' | |
221 | ||
222 | test_expect_success 'corrupt tag' ' | |
223 | test_must_fail git hash-object -t tag --stdin </dev/null | |
224 | ' | |
225 | ||
b7994af0 JK |
226 | test_expect_success 'hash-object complains about bogus type name' ' |
227 | test_must_fail git hash-object -t bogus --stdin </dev/null | |
228 | ' | |
229 | ||
230 | test_expect_success 'hash-object complains about truncated type name' ' | |
231 | test_must_fail git hash-object -t bl --stdin </dev/null | |
232 | ' | |
233 | ||
383c3427 ES |
234 | test_expect_success '--literally' ' |
235 | t=1234567890 && | |
236 | echo example | git hash-object -t $t --literally --stdin | |
237 | ' | |
238 | ||
239 | test_expect_success '--literally with extra-long type' ' | |
240 | t=12345678901234567890123456789012345678901234567890 && | |
241 | 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" && | |
242 | echo example | git hash-object -t $t --literally --stdin | |
243 | ' | |
244 | ||
8a2f5e5b | 245 | test_done |