blob: c34ece48f5ad77a6d53b5f0a1dcf7d53d7413837 [file] [log] [blame]
Michael Haggerty433efca2015-05-11 17:25:03 +02001#!/bin/sh
2
Michael Haggertybf0c6602016-06-07 10:13:04 +02003test_description='Test git update-ref error handling'
Michael Haggerty433efca2015-05-11 17:25:03 +02004. ./test-lib.sh
5
Michael Haggerty017f7222016-06-10 08:55:40 +02006# Create some references, perhaps run pack-refs --all, then try to
7# create some more references. Ensure that the second creation fails
8# with the correct error message.
9# Usage: test_update_rejected <before> <pack> <create> <error>
10# <before> is a ws-separated list of refs to create before the test
11# <pack> (true or false) tells whether to pack the refs before the test
12# <create> is a list of variables to attempt creating
13# <error> is a string to look for in the stderr of update-ref.
14# All references are created in the namespace specified by the current
15# value of $prefix.
Michael Haggerty433efca2015-05-11 17:25:03 +020016test_update_rejected () {
Michael Haggerty0e4b63b2016-06-10 08:50:53 +020017 before="$1" &&
18 pack="$2" &&
19 create="$3" &&
20 error="$4" &&
Michael Haggerty433efca2015-05-11 17:25:03 +020021 printf "create $prefix/%s $C\n" $before |
22 git update-ref --stdin &&
23 git for-each-ref $prefix >unchanged &&
24 if $pack
25 then
26 git pack-refs --all
27 fi &&
28 printf "create $prefix/%s $C\n" $create >input &&
29 test_must_fail git update-ref --stdin <input 2>output.err &&
30 grep -F "$error" output.err &&
31 git for-each-ref $prefix >actual &&
32 test_cmp unchanged actual
33}
34
35Q="'"
36
37test_expect_success 'setup' '
38
39 git commit --allow-empty -m Initial &&
Michael Haggerty19dd7d02016-05-05 13:22:23 +020040 C=$(git rev-parse HEAD) &&
41 git commit --allow-empty -m Second &&
Michael Haggertyc5119dc2016-06-07 12:29:02 +020042 D=$(git rev-parse HEAD) &&
43 git commit --allow-empty -m Third &&
44 E=$(git rev-parse HEAD)
Michael Haggerty433efca2015-05-11 17:25:03 +020045'
46
47test_expect_success 'existing loose ref is a simple prefix of new' '
48
49 prefix=refs/1l &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +020050 test_update_rejected "a c e" false "b c/x d" \
Michael Haggerty5b2d8d62015-05-11 17:25:16 +020051 "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x$Q"
Michael Haggerty433efca2015-05-11 17:25:03 +020052
53'
54
55test_expect_success 'existing packed ref is a simple prefix of new' '
56
57 prefix=refs/1p &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +020058 test_update_rejected "a c e" true "b c/x d" \
Michael Haggerty433efca2015-05-11 17:25:03 +020059 "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x$Q"
60
61'
62
63test_expect_success 'existing loose ref is a deeper prefix of new' '
64
65 prefix=refs/2l &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +020066 test_update_rejected "a c e" false "b c/x/y d" \
Michael Haggerty5b2d8d62015-05-11 17:25:16 +020067 "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x/y$Q"
Michael Haggerty433efca2015-05-11 17:25:03 +020068
69'
70
71test_expect_success 'existing packed ref is a deeper prefix of new' '
72
73 prefix=refs/2p &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +020074 test_update_rejected "a c e" true "b c/x/y d" \
Michael Haggerty433efca2015-05-11 17:25:03 +020075 "$Q$prefix/c$Q exists; cannot create $Q$prefix/c/x/y$Q"
76
77'
78
79test_expect_success 'new ref is a simple prefix of existing loose' '
80
81 prefix=refs/3l &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +020082 test_update_rejected "a c/x e" false "b c d" \
Michael Haggerty5b2d8d62015-05-11 17:25:16 +020083 "$Q$prefix/c/x$Q exists; cannot create $Q$prefix/c$Q"
Michael Haggerty433efca2015-05-11 17:25:03 +020084
85'
86
87test_expect_success 'new ref is a simple prefix of existing packed' '
88
89 prefix=refs/3p &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +020090 test_update_rejected "a c/x e" true "b c d" \
Michael Haggerty433efca2015-05-11 17:25:03 +020091 "$Q$prefix/c/x$Q exists; cannot create $Q$prefix/c$Q"
92
93'
94
95test_expect_success 'new ref is a deeper prefix of existing loose' '
96
97 prefix=refs/4l &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +020098 test_update_rejected "a c/x/y e" false "b c d" \
Michael Haggerty5b2d8d62015-05-11 17:25:16 +020099 "$Q$prefix/c/x/y$Q exists; cannot create $Q$prefix/c$Q"
Michael Haggerty433efca2015-05-11 17:25:03 +0200100
101'
102
103test_expect_success 'new ref is a deeper prefix of existing packed' '
104
105 prefix=refs/4p &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200106 test_update_rejected "a c/x/y e" true "b c d" \
Michael Haggerty433efca2015-05-11 17:25:03 +0200107 "$Q$prefix/c/x/y$Q exists; cannot create $Q$prefix/c$Q"
108
109'
110
Michael Haggertye9111042015-05-11 17:25:12 +0200111test_expect_success 'one new ref is a simple prefix of another' '
Michael Haggerty433efca2015-05-11 17:25:03 +0200112
113 prefix=refs/5 &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200114 test_update_rejected "a e" false "b c c/x d" \
Michael Haggerty433efca2015-05-11 17:25:03 +0200115 "cannot process $Q$prefix/c$Q and $Q$prefix/c/x$Q at the same time"
116
117'
118
Michael Haggertye167a562016-05-05 14:09:41 +0200119test_expect_success 'empty directory should not fool rev-parse' '
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200120 prefix=refs/e-rev-parse &&
121 git update-ref $prefix/foo $C &&
122 git pack-refs --all &&
123 mkdir -p .git/$prefix/foo/bar/baz &&
124 echo "$C" >expected &&
125 git rev-parse $prefix/foo >actual &&
126 test_cmp expected actual
127'
128
129test_expect_success 'empty directory should not fool for-each-ref' '
130 prefix=refs/e-for-each-ref &&
131 git update-ref $prefix/foo $C &&
132 git for-each-ref $prefix >expected &&
133 git pack-refs --all &&
134 mkdir -p .git/$prefix/foo/bar/baz &&
135 git for-each-ref $prefix >actual &&
136 test_cmp expected actual
137'
138
139test_expect_success 'empty directory should not fool create' '
140 prefix=refs/e-create &&
141 mkdir -p .git/$prefix/foo/bar/baz &&
142 printf "create %s $C\n" $prefix/foo |
143 git update-ref --stdin
144'
145
146test_expect_success 'empty directory should not fool verify' '
147 prefix=refs/e-verify &&
148 git update-ref $prefix/foo $C &&
149 git pack-refs --all &&
150 mkdir -p .git/$prefix/foo/bar/baz &&
151 printf "verify %s $C\n" $prefix/foo |
152 git update-ref --stdin
153'
154
155test_expect_success 'empty directory should not fool 1-arg update' '
156 prefix=refs/e-update-1 &&
157 git update-ref $prefix/foo $C &&
158 git pack-refs --all &&
159 mkdir -p .git/$prefix/foo/bar/baz &&
160 printf "update %s $D\n" $prefix/foo |
161 git update-ref --stdin
162'
163
164test_expect_success 'empty directory should not fool 2-arg update' '
165 prefix=refs/e-update-2 &&
166 git update-ref $prefix/foo $C &&
167 git pack-refs --all &&
168 mkdir -p .git/$prefix/foo/bar/baz &&
169 printf "update %s $D $C\n" $prefix/foo |
170 git update-ref --stdin
171'
172
173test_expect_success 'empty directory should not fool 0-arg delete' '
174 prefix=refs/e-delete-0 &&
175 git update-ref $prefix/foo $C &&
176 git pack-refs --all &&
177 mkdir -p .git/$prefix/foo/bar/baz &&
178 printf "delete %s\n" $prefix/foo |
179 git update-ref --stdin
180'
181
182test_expect_success 'empty directory should not fool 1-arg delete' '
183 prefix=refs/e-delete-1 &&
184 git update-ref $prefix/foo $C &&
185 git pack-refs --all &&
186 mkdir -p .git/$prefix/foo/bar/baz &&
187 printf "delete %s $C\n" $prefix/foo |
188 git update-ref --stdin
189'
190
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200191# Test various errors when reading the old values of references...
192
193test_expect_success 'missing old value blocks update' '
194 prefix=refs/missing-update &&
195 cat >expected <<-EOF &&
196 fatal: cannot lock ref $Q$prefix/foo$Q: unable to resolve reference $Q$prefix/foo$Q
197 EOF
198 printf "%s\n" "update $prefix/foo $E $D" |
199 test_must_fail git update-ref --stdin 2>output.err &&
200 test_cmp expected output.err
201'
202
203test_expect_success 'incorrect old value blocks update' '
204 prefix=refs/incorrect-update &&
205 git update-ref $prefix/foo $C &&
206 cat >expected <<-EOF &&
207 fatal: cannot lock ref $Q$prefix/foo$Q: is at $C but expected $D
208 EOF
209 printf "%s\n" "update $prefix/foo $E $D" |
210 test_must_fail git update-ref --stdin 2>output.err &&
211 test_cmp expected output.err
212'
213
214test_expect_success 'existing old value blocks create' '
215 prefix=refs/existing-create &&
216 git update-ref $prefix/foo $C &&
217 cat >expected <<-EOF &&
218 fatal: cannot lock ref $Q$prefix/foo$Q: reference already exists
219 EOF
220 printf "%s\n" "create $prefix/foo $E" |
221 test_must_fail git update-ref --stdin 2>output.err &&
222 test_cmp expected output.err
223'
224
225test_expect_success 'incorrect old value blocks delete' '
226 prefix=refs/incorrect-delete &&
227 git update-ref $prefix/foo $C &&
228 cat >expected <<-EOF &&
229 fatal: cannot lock ref $Q$prefix/foo$Q: is at $C but expected $D
230 EOF
231 printf "%s\n" "delete $prefix/foo $D" |
232 test_must_fail git update-ref --stdin 2>output.err &&
233 test_cmp expected output.err
234'
235
236test_expect_success 'missing old value blocks indirect update' '
237 prefix=refs/missing-indirect-update &&
238 git symbolic-ref $prefix/symref $prefix/foo &&
239 cat >expected <<-EOF &&
Michael Haggertye3f51032016-06-07 09:29:23 +0200240 fatal: cannot lock ref $Q$prefix/symref$Q: unable to resolve reference $Q$prefix/foo$Q
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200241 EOF
242 printf "%s\n" "update $prefix/symref $E $D" |
243 test_must_fail git update-ref --stdin 2>output.err &&
244 test_cmp expected output.err
245'
246
247test_expect_success 'incorrect old value blocks indirect update' '
248 prefix=refs/incorrect-indirect-update &&
249 git symbolic-ref $prefix/symref $prefix/foo &&
250 git update-ref $prefix/foo $C &&
251 cat >expected <<-EOF &&
252 fatal: cannot lock ref $Q$prefix/symref$Q: is at $C but expected $D
253 EOF
254 printf "%s\n" "update $prefix/symref $E $D" |
255 test_must_fail git update-ref --stdin 2>output.err &&
256 test_cmp expected output.err
257'
258
259test_expect_success 'existing old value blocks indirect create' '
260 prefix=refs/existing-indirect-create &&
261 git symbolic-ref $prefix/symref $prefix/foo &&
262 git update-ref $prefix/foo $C &&
263 cat >expected <<-EOF &&
264 fatal: cannot lock ref $Q$prefix/symref$Q: reference already exists
265 EOF
266 printf "%s\n" "create $prefix/symref $E" |
267 test_must_fail git update-ref --stdin 2>output.err &&
268 test_cmp expected output.err
269'
270
271test_expect_success 'incorrect old value blocks indirect delete' '
272 prefix=refs/incorrect-indirect-delete &&
273 git symbolic-ref $prefix/symref $prefix/foo &&
274 git update-ref $prefix/foo $C &&
275 cat >expected <<-EOF &&
276 fatal: cannot lock ref $Q$prefix/symref$Q: is at $C but expected $D
277 EOF
278 printf "%s\n" "delete $prefix/symref $D" |
279 test_must_fail git update-ref --stdin 2>output.err &&
280 test_cmp expected output.err
281'
282
283test_expect_success 'missing old value blocks indirect no-deref update' '
284 prefix=refs/missing-noderef-update &&
285 git symbolic-ref $prefix/symref $prefix/foo &&
286 cat >expected <<-EOF &&
Michael Haggertye3f51032016-06-07 09:29:23 +0200287 fatal: cannot lock ref $Q$prefix/symref$Q: reference is missing but expected $D
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200288 EOF
289 printf "%s\n" "option no-deref" "update $prefix/symref $E $D" |
290 test_must_fail git update-ref --stdin 2>output.err &&
291 test_cmp expected output.err
292'
293
294test_expect_success 'incorrect old value blocks indirect no-deref update' '
295 prefix=refs/incorrect-noderef-update &&
296 git symbolic-ref $prefix/symref $prefix/foo &&
297 git update-ref $prefix/foo $C &&
298 cat >expected <<-EOF &&
299 fatal: cannot lock ref $Q$prefix/symref$Q: is at $C but expected $D
300 EOF
301 printf "%s\n" "option no-deref" "update $prefix/symref $E $D" |
302 test_must_fail git update-ref --stdin 2>output.err &&
303 test_cmp expected output.err
304'
305
Michael Haggertye3f51032016-06-07 09:29:23 +0200306test_expect_success 'existing old value blocks indirect no-deref create' '
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200307 prefix=refs/existing-noderef-create &&
308 git symbolic-ref $prefix/symref $prefix/foo &&
309 git update-ref $prefix/foo $C &&
310 cat >expected <<-EOF &&
311 fatal: cannot lock ref $Q$prefix/symref$Q: reference already exists
312 EOF
313 printf "%s\n" "option no-deref" "create $prefix/symref $E" |
314 test_must_fail git update-ref --stdin 2>output.err &&
315 test_cmp expected output.err
316'
317
318test_expect_success 'incorrect old value blocks indirect no-deref delete' '
319 prefix=refs/incorrect-noderef-delete &&
320 git symbolic-ref $prefix/symref $prefix/foo &&
321 git update-ref $prefix/foo $C &&
322 cat >expected <<-EOF &&
323 fatal: cannot lock ref $Q$prefix/symref$Q: is at $C but expected $D
324 EOF
325 printf "%s\n" "option no-deref" "delete $prefix/symref $D" |
326 test_must_fail git update-ref --stdin 2>output.err &&
327 test_cmp expected output.err
328'
329
330test_expect_success 'non-empty directory blocks create' '
331 prefix=refs/ne-create &&
332 mkdir -p .git/$prefix/foo/bar &&
333 : >.git/$prefix/foo/bar/baz.lock &&
334 test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
335 cat >expected <<-EOF &&
336 fatal: cannot lock ref $Q$prefix/foo$Q: there is a non-empty directory $Q.git/$prefix/foo$Q blocking reference $Q$prefix/foo$Q
337 EOF
338 printf "%s\n" "update $prefix/foo $C" |
339 test_must_fail git update-ref --stdin 2>output.err &&
340 test_cmp expected output.err &&
341 cat >expected <<-EOF &&
342 fatal: cannot lock ref $Q$prefix/foo$Q: unable to resolve reference $Q$prefix/foo$Q
343 EOF
344 printf "%s\n" "update $prefix/foo $D $C" |
345 test_must_fail git update-ref --stdin 2>output.err &&
346 test_cmp expected output.err
347'
348
349test_expect_success 'broken reference blocks create' '
350 prefix=refs/broken-create &&
351 mkdir -p .git/$prefix &&
352 echo "gobbledigook" >.git/$prefix/foo &&
353 test_when_finished "rm -f .git/$prefix/foo" &&
354 cat >expected <<-EOF &&
355 fatal: cannot lock ref $Q$prefix/foo$Q: unable to resolve reference $Q$prefix/foo$Q: reference broken
356 EOF
357 printf "%s\n" "update $prefix/foo $C" |
358 test_must_fail git update-ref --stdin 2>output.err &&
359 test_cmp expected output.err &&
360 cat >expected <<-EOF &&
361 fatal: cannot lock ref $Q$prefix/foo$Q: unable to resolve reference $Q$prefix/foo$Q: reference broken
362 EOF
363 printf "%s\n" "update $prefix/foo $D $C" |
364 test_must_fail git update-ref --stdin 2>output.err &&
365 test_cmp expected output.err
366'
367
368test_expect_success 'non-empty directory blocks indirect create' '
369 prefix=refs/ne-indirect-create &&
370 git symbolic-ref $prefix/symref $prefix/foo &&
371 mkdir -p .git/$prefix/foo/bar &&
372 : >.git/$prefix/foo/bar/baz.lock &&
373 test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
374 cat >expected <<-EOF &&
Michael Haggertye3f51032016-06-07 09:29:23 +0200375 fatal: cannot lock ref $Q$prefix/symref$Q: there is a non-empty directory $Q.git/$prefix/foo$Q blocking reference $Q$prefix/foo$Q
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200376 EOF
377 printf "%s\n" "update $prefix/symref $C" |
378 test_must_fail git update-ref --stdin 2>output.err &&
379 test_cmp expected output.err &&
380 cat >expected <<-EOF &&
Michael Haggertye3f51032016-06-07 09:29:23 +0200381 fatal: cannot lock ref $Q$prefix/symref$Q: unable to resolve reference $Q$prefix/foo$Q
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200382 EOF
383 printf "%s\n" "update $prefix/symref $D $C" |
384 test_must_fail git update-ref --stdin 2>output.err &&
385 test_cmp expected output.err
386'
387
388test_expect_success 'broken reference blocks indirect create' '
389 prefix=refs/broken-indirect-create &&
390 git symbolic-ref $prefix/symref $prefix/foo &&
391 echo "gobbledigook" >.git/$prefix/foo &&
392 test_when_finished "rm -f .git/$prefix/foo" &&
393 cat >expected <<-EOF &&
Michael Haggertye3f51032016-06-07 09:29:23 +0200394 fatal: cannot lock ref $Q$prefix/symref$Q: unable to resolve reference $Q$prefix/foo$Q: reference broken
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200395 EOF
396 printf "%s\n" "update $prefix/symref $C" |
397 test_must_fail git update-ref --stdin 2>output.err &&
398 test_cmp expected output.err &&
399 cat >expected <<-EOF &&
Michael Haggertye3f51032016-06-07 09:29:23 +0200400 fatal: cannot lock ref $Q$prefix/symref$Q: unable to resolve reference $Q$prefix/foo$Q: reference broken
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200401 EOF
402 printf "%s\n" "update $prefix/symref $D $C" |
403 test_must_fail git update-ref --stdin 2>output.err &&
404 test_cmp expected output.err
405'
406
Michael Haggerty433efca2015-05-11 17:25:03 +0200407test_done