1#!/bin/sh
2
3test_description="git hash-object"
4
5. ./test-lib.sh
6
7echo_without_newline() {
8 printf '%s' "$*"
9}
10
11test_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
17test_blob_exists() {
18 test_expect_success 'blob exists in database' "
19 git cat-file blob $1
20 "
21}
22
23hello_content="Hello World"
24hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
25
26example_content="This is an example"
27example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399
28
29setup_repo() {
30 echo_without_newline "$hello_content" > hello
31 echo_without_newline "$example_content" > example
32}
33
34test_repo=test
35push_repo() {
36 test_create_repo $test_repo
37 cd $test_repo
38
39 setup_repo
40}
41
42pop_repo() {
43 cd ..
44 rm -rf $test_repo
45}
46
47setup_repo
48
49# Argument checking
50
51test_expect_success "multiple '--stdin's are rejected" '
52 echo example | test_must_fail git hash-object --stdin --stdin
53'
54
55test_expect_success "Can't use --stdin and --stdin-paths together" '
56 echo example | test_must_fail git hash-object --stdin --stdin-paths &&
57 echo example | test_must_fail git hash-object --stdin-paths --stdin
58'
59
60test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
61 echo example | test_must_fail git hash-object --stdin-paths hello
62'
63
64test_expect_success "Can't use --path with --stdin-paths" '
65 echo example | test_must_fail git hash-object --stdin-paths --path=foo
66'
67
68test_expect_success "Can't use --path with --no-filters" '
69 test_must_fail git hash-object --no-filters --path=foo
70'
71
72# Behavior
73
74push_repo
75
76test_expect_success 'hash a file' '
77 test $hello_sha1 = $(git hash-object hello)
78'
79
80test_blob_does_not_exist $hello_sha1
81
82test_expect_success 'hash from stdin' '
83 test $example_sha1 = $(git hash-object --stdin < example)
84'
85
86test_blob_does_not_exist $example_sha1
87
88test_expect_success 'hash a file and write to database' '
89 test $hello_sha1 = $(git hash-object -w hello)
90'
91
92test_blob_exists $hello_sha1
93
94test_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
104test_expect_success 'check that appropriate filter is invoke when --path is used' '
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) &&
112 test "$file0_sha" != "$file1_sha" &&
113 path1_sha=$(git hash-object --path=file1 file0) &&
114 path0_sha=$(git hash-object --path=file0 file1) &&
115 test "$file0_sha" = "$path0_sha" &&
116 test "$file1_sha" = "$path1_sha" &&
117 path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) &&
118 path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&
119 test "$file0_sha" = "$path0_sha" &&
120 test "$file1_sha" = "$path1_sha" &&
121 git config --unset core.autocrlf
122'
123
124test_expect_success 'check that --no-filters option works' '
125 echo fooQ | tr Q "\\015" >file0 &&
126 cp file0 file1 &&
127 echo "file0 -crlf" >.gitattributes &&
128 echo "file1 crlf" >>.gitattributes &&
129 git config core.autocrlf true &&
130 file0_sha=$(git hash-object file0) &&
131 file1_sha=$(git hash-object file1) &&
132 test "$file0_sha" != "$file1_sha" &&
133 nofilters_file1=$(git hash-object --no-filters file1) &&
134 test "$file0_sha" = "$nofilters_file1" &&
135 nofilters_file1=$(cat file1 | git hash-object --stdin) &&
136 test "$file0_sha" = "$nofilters_file1" &&
137 git config --unset core.autocrlf
138'
139
140test_expect_success 'check that --no-filters option works with --stdin-paths' '
141 echo fooQ | tr Q "\\015" >file0 &&
142 cp file0 file1 &&
143 echo "file0 -crlf" >.gitattributes &&
144 echo "file1 crlf" >>.gitattributes &&
145 git config core.autocrlf true &&
146 file0_sha=$(git hash-object file0) &&
147 file1_sha=$(git hash-object file1) &&
148 test "$file0_sha" != "$file1_sha" &&
149 nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
150 test "$file0_sha" = "$nofilters_file1" &&
151 git config --unset core.autocrlf
152'
153
154pop_repo
155
156for args in "-w --stdin" "--stdin -w"; do
157 push_repo
158
159 test_expect_success "hash from stdin and write to database ($args)" '
160 test $example_sha1 = $(git hash-object $args < example)
161 '
162
163 test_blob_exists $example_sha1
164
165 pop_repo
166done
167
168filenames="hello
169example"
170
171sha1s="$hello_sha1
172$example_sha1"
173
174test_expect_success "hash two files with names on stdin" '
175 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
176'
177
178for args in "-w --stdin-paths" "--stdin-paths -w"; do
179 push_repo
180
181 test_expect_success "hash two files with names on stdin and write to database ($args)" '
182 test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)"
183 '
184
185 test_blob_exists $hello_sha1
186 test_blob_exists $example_sha1
187
188 pop_repo
189done
190
191test_expect_success 'corrupt tree' '
192 echo abc >malformed-tree &&
193 test_must_fail git hash-object -t tree malformed-tree
194'
195
196test_expect_success 'corrupt commit' '
197 test_must_fail git hash-object -t commit --stdin </dev/null
198'
199
200test_expect_success 'corrupt tag' '
201 test_must_fail git hash-object -t tag --stdin </dev/null
202'
203
204test_expect_success 'hash-object complains about bogus type name' '
205 test_must_fail git hash-object -t bogus --stdin </dev/null
206'
207
208test_expect_success 'hash-object complains about truncated type name' '
209 test_must_fail git hash-object -t bl --stdin </dev/null
210'
211
212test_expect_success '--literally' '
213 t=1234567890 &&
214 echo example | git hash-object -t $t --literally --stdin
215'
216
217test_expect_success '--literally with extra-long type' '
218 t=12345678901234567890123456789012345678901234567890 &&
219 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" &&
220 echo example | git hash-object -t $t --literally --stdin
221'
222
223test_done