]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1007-hash-object.sh
Merge branch 'jk/bundle-use-dash-for-stdfiles'
[thirdparty/git.git] / t / t1007-hash-object.sh
CommitLineData
8a2f5e5b
GP
1#!/bin/sh
2
0cb0e143 3test_description="git hash-object"
8a2f5e5b 4
d17294a0 5TEST_PASSES_SANITIZE_LEAK=true
8a2f5e5b
GP
6. ./test-lib.sh
7
3ea5a1b3
AR
8echo_without_newline() {
9 printf '%s' "$*"
10}
11
12test_blob_does_not_exist() {
2e306f6c 13 test_expect_success 'blob does not exist in database' "
3ea5a1b3
AR
14 test_must_fail git cat-file blob $1
15 "
16}
17
18test_blob_exists() {
2e306f6c 19 test_expect_success 'blob exists in database' "
3ea5a1b3
AR
20 git cat-file blob $1
21 "
22}
23
24hello_content="Hello World"
3ea5a1b3 25example_content="This is an example"
3ea5a1b3
AR
26
27setup_repo() {
28 echo_without_newline "$hello_content" > hello
29 echo_without_newline "$example_content" > example
30}
31
32test_repo=test
33push_repo() {
34 test_create_repo $test_repo
35 cd $test_repo
36
37 setup_repo
38}
39
40pop_repo() {
41 cd ..
42 rm -rf $test_repo
43}
44
2e306f6c 45test_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'
3ea5a1b3
AR
55
56# Argument checking
57
58test_expect_success "multiple '--stdin's are rejected" '
81014073 59 echo example | test_must_fail git hash-object --stdin --stdin
3ea5a1b3
AR
60'
61
d8ee4832 62test_expect_success "Can't use --stdin and --stdin-paths together" '
81014073
DP
63 echo example | test_must_fail git hash-object --stdin --stdin-paths &&
64 echo example | test_must_fail git hash-object --stdin-paths --stdin
d8ee4832
AR
65'
66
67test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
81014073 68 echo example | test_must_fail git hash-object --stdin-paths hello
d8ee4832
AR
69'
70
39702431
DP
71test_expect_success "Can't use --path with --stdin-paths" '
72 echo example | test_must_fail git hash-object --stdin-paths --path=foo
73'
74
4a3d85dc
DP
75test_expect_success "Can't use --path with --no-filters" '
76 test_must_fail git hash-object --no-filters --path=foo
77'
78
3ea5a1b3
AR
79# Behavior
80
81push_repo
82
2e306f6c 83test_expect_success 'hash a file' '
84 test "$(test_oid hello)" = $(git hash-object hello)
3ea5a1b3
AR
85'
86
2e306f6c 87test_blob_does_not_exist "$(test_oid hello)"
3ea5a1b3 88
2e306f6c 89test_expect_success 'hash from stdin' '
90 test "$(test_oid example)" = $(git hash-object --stdin < example)
3ea5a1b3
AR
91'
92
2e306f6c 93test_blob_does_not_exist "$(test_oid example)"
3ea5a1b3 94
2e306f6c 95test_expect_success 'hash a file and write to database' '
96 test "$(test_oid hello)" = $(git hash-object -w hello)
3ea5a1b3
AR
97'
98
2e306f6c 99test_blob_exists "$(test_oid hello)"
3ea5a1b3
AR
100
101test_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
4d0efa10 111test_expect_success 'set up crlf tests' '
39702431
DP
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) &&
4d0efa10
JK
119 test "$file0_sha" != "$file1_sha"
120'
121
122test_expect_success 'check that appropriate filter is invoke when --path is used' '
39702431
DP
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=$(cat file0 | git hash-object --path=file1 --stdin) &&
128 path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&
129 test "$file0_sha" = "$path0_sha" &&
4d0efa10 130 test "$file1_sha" = "$path1_sha"
39702431
DP
131'
132
0e94ee94
JK
133test_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
a1be47e4
JK
144test_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
4a3d85dc 154test_expect_success 'check that --no-filters option works' '
4a3d85dc
DP
155 nofilters_file1=$(git hash-object --no-filters file1) &&
156 test "$file0_sha" = "$nofilters_file1" &&
157 nofilters_file1=$(cat file1 | git hash-object --stdin) &&
4d0efa10 158 test "$file0_sha" = "$nofilters_file1"
4a3d85dc
DP
159'
160
a9f97909 161test_expect_success 'check that --no-filters option works with --stdin-paths' '
a9f97909 162 nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
4d0efa10 163 test "$file0_sha" = "$nofilters_file1"
a9f97909
EFL
164'
165
3ea5a1b3
AR
166pop_repo
167
168for args in "-w --stdin" "--stdin -w"; do
169 push_repo
170
2e306f6c 171 test_expect_success "hash from stdin and write to database ($args)" '
172 test "$(test_oid example)" = $(git hash-object $args < example)
3ea5a1b3
AR
173 '
174
2e306f6c 175 test_blob_exists "$(test_oid example)"
3ea5a1b3
AR
176
177 pop_repo
178done
8a2f5e5b 179
d8ee4832
AR
180filenames="hello
181example"
182
2e306f6c 183oids="$(test_oid hello)
184$(test_oid example)"
d8ee4832 185
2e306f6c 186test_expect_success "hash two files with names on stdin" '
187 test "$oids" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
d8ee4832
AR
188'
189
190for args in "-w --stdin-paths" "--stdin-paths -w"; do
191 push_repo
192
2e306f6c 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)"
d8ee4832
AR
195 '
196
2e306f6c 197 test_blob_exists "$(test_oid hello)"
198 test_blob_exists "$(test_oid example)"
d8ee4832
AR
199
200 pop_repo
201done
202
2edffef2 203test_expect_success 'too-short tree' '
02380389 204 echo abc >malformed-tree &&
2edffef2 205 test_must_fail git hash-object -t tree malformed-tree 2>err &&
6e264607 206 grep "too-short tree object" err
2edffef2
JK
207'
208
2edffef2 209test_expect_success 'malformed mode in tree' '
6e264607
JK
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 &&
2edffef2 213 test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err &&
6e264607 214 grep "malformed mode in tree entry" err
2edffef2
JK
215'
216
217test_expect_success 'empty filename in tree' '
6e264607
JK
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 &&
2edffef2 221 test_must_fail git hash-object -t tree tree-with-empty-filename 2>err &&
6e264607 222 grep "empty filename in tree entry" err
c879daa2
NTND
223'
224
69bbbe48
JK
225test_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
c879daa2
NTND
236test_expect_success 'corrupt commit' '
237 test_must_fail git hash-object -t commit --stdin </dev/null
238'
239
240test_expect_success 'corrupt tag' '
241 test_must_fail git hash-object -t tag --stdin </dev/null
242'
243
b7994af0
JK
244test_expect_success 'hash-object complains about bogus type name' '
245 test_must_fail git hash-object -t bogus --stdin </dev/null
246'
247
248test_expect_success 'hash-object complains about truncated type name' '
249 test_must_fail git hash-object -t bl --stdin </dev/null
250'
251
383c3427
ES
252test_expect_success '--literally' '
253 t=1234567890 &&
254 echo example | git hash-object -t $t --literally --stdin
255'
256
257test_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
8a2f5e5b 263test_done