3 test_description
='Test git update-ref error handling'
5 TEST_PASSES_SANITIZE_LEAK
=true
8 # Create some references, perhaps run pack-refs --all, then try to
9 # create some more references. Ensure that the second creation fails
10 # with the correct error message.
11 # Usage: test_update_rejected <before> <pack> <create> <error>
12 # <before> is a ws-separated list of refs to create before the test
13 # <pack> (true or false) tells whether to pack the refs before the test
14 # <create> is a list of variables to attempt creating
15 # <error> is a string to look for in the stderr of update-ref.
16 # All references are created in the namespace specified by the current
18 test_update_rejected
() {
23 printf "create $prefix/%s $C\n" $before |
24 git update-ref
--stdin &&
25 git for-each-ref
$prefix >unchanged
&&
30 printf "create $prefix/%s $C\n" $create >input
&&
31 test_must_fail git update-ref
--stdin <input
2>output.err
&&
32 test_grep
-F "$error" output.err
&&
33 git for-each-ref
$prefix >actual
&&
34 test_cmp unchanged actual
37 # Test adding and deleting D/F-conflicting references in a single
41 pack
=: symadd
=false symdel
=false add_del
=false addref
= delref
=
47 pack
="git pack-refs --all"
51 # Perform the add via a symbolic reference
56 # Perform the del via a symbolic reference
61 # Delete first reference then add second
68 # Add first reference then delete second
75 echo 1>&2 "Extra args to df_test: $*"
80 git update-ref
"$delref" $C &&
83 addname
="$prefix/s/symadd" &&
84 git symbolic-ref
"$addname" "$addref"
90 delname
="$prefix/s/symdel" &&
91 git symbolic-ref
"$delname" "$delref"
95 cat >expected-err
<<-EOF &&
96 fatal: cannot lock ref $SQ$addname$SQ: $SQ$delref$SQ exists; cannot create $SQ$addref$SQ
101 printf "%s\n" "create $addname $D" "delete $delname"
103 printf "%s\n" "delete $delname" "create $addname $D"
105 test_must_fail git update-ref
--stdin <commands
2>output.err
&&
106 test_cmp expected-err output.err
&&
107 printf "%s\n" "$C $delref" >expected-refs
&&
108 git for-each-ref
--format="%(objectname) %(refname)" $prefix/r
>actual-refs
&&
109 test_cmp expected-refs actual-refs
112 test_expect_success
'setup' '
114 git commit --allow-empty -m Initial &&
115 C=$(git rev-parse HEAD) &&
116 git commit --allow-empty -m Second &&
117 D=$(git rev-parse HEAD) &&
118 git commit --allow-empty -m Third &&
119 E=$(git rev-parse HEAD)
122 test_expect_success
'existing loose ref is a simple prefix of new' '
125 test_update_rejected "a c e" false "b c/x d" \
126 "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x$SQ"
130 test_expect_success
'existing packed ref is a simple prefix of new' '
133 test_update_rejected "a c e" true "b c/x d" \
134 "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x$SQ"
138 test_expect_success
'existing loose ref is a deeper prefix of new' '
141 test_update_rejected "a c e" false "b c/x/y d" \
142 "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x/y$SQ"
146 test_expect_success
'existing packed ref is a deeper prefix of new' '
149 test_update_rejected "a c e" true "b c/x/y d" \
150 "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x/y$SQ"
154 test_expect_success
'new ref is a simple prefix of existing loose' '
157 test_update_rejected "a c/x e" false "b c d" \
158 "$SQ$prefix/c/x$SQ exists; cannot create $SQ$prefix/c$SQ"
162 test_expect_success
'new ref is a simple prefix of existing packed' '
165 test_update_rejected "a c/x e" true "b c d" \
166 "$SQ$prefix/c/x$SQ exists; cannot create $SQ$prefix/c$SQ"
170 test_expect_success
'new ref is a deeper prefix of existing loose' '
173 test_update_rejected "a c/x/y e" false "b c d" \
174 "$SQ$prefix/c/x/y$SQ exists; cannot create $SQ$prefix/c$SQ"
178 test_expect_success
'new ref is a deeper prefix of existing packed' '
181 test_update_rejected "a c/x/y e" true "b c d" \
182 "$SQ$prefix/c/x/y$SQ exists; cannot create $SQ$prefix/c$SQ"
186 test_expect_success
'one new ref is a simple prefix of another' '
189 test_update_rejected "a e" false "b c c/x d" \
190 "cannot process $SQ$prefix/c$SQ and $SQ$prefix/c/x$SQ at the same time"
194 test_expect_success REFFILES
'empty directory should not fool rev-parse' '
195 prefix=refs/e-rev-parse &&
196 git update-ref $prefix/foo $C &&
197 git pack-refs --all &&
198 mkdir -p .git/$prefix/foo/bar/baz &&
199 echo "$C" >expected &&
200 git rev-parse $prefix/foo >actual &&
201 test_cmp expected actual
204 test_expect_success REFFILES
'empty directory should not fool for-each-ref' '
205 prefix=refs/e-for-each-ref &&
206 git update-ref $prefix/foo $C &&
207 git for-each-ref $prefix >expected &&
208 git pack-refs --all &&
209 mkdir -p .git/$prefix/foo/bar/baz &&
210 git for-each-ref $prefix >actual &&
211 test_cmp expected actual
214 test_expect_success REFFILES
'empty directory should not fool create' '
215 prefix=refs/e-create &&
216 mkdir -p .git/$prefix/foo/bar/baz &&
217 printf "create %s $C\n" $prefix/foo |
218 git update-ref --stdin
221 test_expect_success REFFILES
'empty directory should not fool verify' '
222 prefix=refs/e-verify &&
223 git update-ref $prefix/foo $C &&
224 git pack-refs --all &&
225 mkdir -p .git/$prefix/foo/bar/baz &&
226 printf "verify %s $C\n" $prefix/foo |
227 git update-ref --stdin
230 test_expect_success REFFILES
'empty directory should not fool 1-arg update' '
231 prefix=refs/e-update-1 &&
232 git update-ref $prefix/foo $C &&
233 git pack-refs --all &&
234 mkdir -p .git/$prefix/foo/bar/baz &&
235 printf "update %s $D\n" $prefix/foo |
236 git update-ref --stdin
239 test_expect_success REFFILES
'empty directory should not fool 2-arg update' '
240 prefix=refs/e-update-2 &&
241 git update-ref $prefix/foo $C &&
242 git pack-refs --all &&
243 mkdir -p .git/$prefix/foo/bar/baz &&
244 printf "update %s $D $C\n" $prefix/foo |
245 git update-ref --stdin
248 test_expect_success REFFILES
'empty directory should not fool 0-arg delete' '
249 prefix=refs/e-delete-0 &&
250 git update-ref $prefix/foo $C &&
251 git pack-refs --all &&
252 mkdir -p .git/$prefix/foo/bar/baz &&
253 printf "delete %s\n" $prefix/foo |
254 git update-ref --stdin
257 test_expect_success REFFILES
'empty directory should not fool 1-arg delete' '
258 prefix=refs/e-delete-1 &&
259 git update-ref $prefix/foo $C &&
260 git pack-refs --all &&
261 mkdir -p .git/$prefix/foo/bar/baz &&
262 printf "delete %s $C\n" $prefix/foo |
263 git update-ref --stdin
266 test_expect_success REFFILES
'D/F conflict prevents add long + delete short' '
267 df_test refs/df-al-ds --add-del foo/bar foo
270 test_expect_success REFFILES
'D/F conflict prevents add short + delete long' '
271 df_test refs/df-as-dl --add-del foo foo/bar
274 test_expect_success REFFILES
'D/F conflict prevents delete long + add short' '
275 df_test refs/df-dl-as --del-add foo/bar foo
278 test_expect_success REFFILES
'D/F conflict prevents delete short + add long' '
279 df_test refs/df-ds-al --del-add foo foo/bar
282 test_expect_success REFFILES
'D/F conflict prevents add long + delete short packed' '
283 df_test refs/df-al-dsp --pack --add-del foo/bar foo
286 test_expect_success REFFILES
'D/F conflict prevents add short + delete long packed' '
287 df_test refs/df-as-dlp --pack --add-del foo foo/bar
290 test_expect_success REFFILES
'D/F conflict prevents delete long packed + add short' '
291 df_test refs/df-dlp-as --pack --del-add foo/bar foo
294 test_expect_success REFFILES
'D/F conflict prevents delete short packed + add long' '
295 df_test refs/df-dsp-al --pack --del-add foo foo/bar
298 # Try some combinations involving symbolic refs...
300 test_expect_success REFFILES
'D/F conflict prevents indirect add long + delete short' '
301 df_test refs/df-ial-ds --sym-add --add-del foo/bar foo
304 test_expect_success REFFILES
'D/F conflict prevents indirect add long + indirect delete short' '
305 df_test refs/df-ial-ids --sym-add --sym-del --add-del foo/bar foo
308 test_expect_success REFFILES
'D/F conflict prevents indirect add short + indirect delete long' '
309 df_test refs/df-ias-idl --sym-add --sym-del --add-del foo foo/bar
312 test_expect_success REFFILES
'D/F conflict prevents indirect delete long + indirect add short' '
313 df_test refs/df-idl-ias --sym-add --sym-del --del-add foo/bar foo
316 test_expect_success REFFILES
'D/F conflict prevents indirect add long + delete short packed' '
317 df_test refs/df-ial-dsp --sym-add --pack --add-del foo/bar foo
320 test_expect_success REFFILES
'D/F conflict prevents indirect add long + indirect delete short packed' '
321 df_test refs/df-ial-idsp --sym-add --sym-del --pack --add-del foo/bar foo
324 test_expect_success REFFILES
'D/F conflict prevents add long + indirect delete short packed' '
325 df_test refs/df-al-idsp --sym-del --pack --add-del foo/bar foo
328 test_expect_success REFFILES
'D/F conflict prevents indirect delete long packed + indirect add short' '
329 df_test refs/df-idlp-ias --sym-add --sym-del --pack --del-add foo/bar foo
332 # Test various errors when reading the old values of references...
334 test_expect_success
'missing old value blocks update' '
335 prefix=refs/missing-update &&
336 cat >expected <<-EOF &&
337 fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ
339 printf "%s\n" "update $prefix/foo $E $D" |
340 test_must_fail git update-ref --stdin 2>output.err &&
341 test_cmp expected output.err
344 test_expect_success
'incorrect old value blocks update' '
345 prefix=refs/incorrect-update &&
346 git update-ref $prefix/foo $C &&
347 cat >expected <<-EOF &&
348 fatal: cannot lock ref $SQ$prefix/foo$SQ: is at $C but expected $D
350 printf "%s\n" "update $prefix/foo $E $D" |
351 test_must_fail git update-ref --stdin 2>output.err &&
352 test_cmp expected output.err
355 test_expect_success
'existing old value blocks create' '
356 prefix=refs/existing-create &&
357 git update-ref $prefix/foo $C &&
358 cat >expected <<-EOF &&
359 fatal: cannot lock ref $SQ$prefix/foo$SQ: reference already exists
361 printf "%s\n" "create $prefix/foo $E" |
362 test_must_fail git update-ref --stdin 2>output.err &&
363 test_cmp expected output.err
366 test_expect_success
'incorrect old value blocks delete' '
367 prefix=refs/incorrect-delete &&
368 git update-ref $prefix/foo $C &&
369 cat >expected <<-EOF &&
370 fatal: cannot lock ref $SQ$prefix/foo$SQ: is at $C but expected $D
372 printf "%s\n" "delete $prefix/foo $D" |
373 test_must_fail git update-ref --stdin 2>output.err &&
374 test_cmp expected output.err
377 test_expect_success
'missing old value blocks indirect update' '
378 prefix=refs/missing-indirect-update &&
379 git symbolic-ref $prefix/symref $prefix/foo &&
380 cat >expected <<-EOF &&
381 fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ
383 printf "%s\n" "update $prefix/symref $E $D" |
384 test_must_fail git update-ref --stdin 2>output.err &&
385 test_cmp expected output.err
388 test_expect_success
'incorrect old value blocks indirect update' '
389 prefix=refs/incorrect-indirect-update &&
390 git symbolic-ref $prefix/symref $prefix/foo &&
391 git update-ref $prefix/foo $C &&
392 cat >expected <<-EOF &&
393 fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
395 printf "%s\n" "update $prefix/symref $E $D" |
396 test_must_fail git update-ref --stdin 2>output.err &&
397 test_cmp expected output.err
400 test_expect_success
'existing old value blocks indirect create' '
401 prefix=refs/existing-indirect-create &&
402 git symbolic-ref $prefix/symref $prefix/foo &&
403 git update-ref $prefix/foo $C &&
404 cat >expected <<-EOF &&
405 fatal: cannot lock ref $SQ$prefix/symref$SQ: reference already exists
407 printf "%s\n" "create $prefix/symref $E" |
408 test_must_fail git update-ref --stdin 2>output.err &&
409 test_cmp expected output.err
412 test_expect_success
'incorrect old value blocks indirect delete' '
413 prefix=refs/incorrect-indirect-delete &&
414 git symbolic-ref $prefix/symref $prefix/foo &&
415 git update-ref $prefix/foo $C &&
416 cat >expected <<-EOF &&
417 fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
419 printf "%s\n" "delete $prefix/symref $D" |
420 test_must_fail git update-ref --stdin 2>output.err &&
421 test_cmp expected output.err
424 test_expect_success
'missing old value blocks indirect no-deref update' '
425 prefix=refs/missing-noderef-update &&
426 git symbolic-ref $prefix/symref $prefix/foo &&
427 cat >expected <<-EOF &&
428 fatal: cannot lock ref $SQ$prefix/symref$SQ: reference is missing but expected $D
430 printf "%s\n" "option no-deref" "update $prefix/symref $E $D" |
431 test_must_fail git update-ref --stdin 2>output.err &&
432 test_cmp expected output.err
435 test_expect_success
'incorrect old value blocks indirect no-deref update' '
436 prefix=refs/incorrect-noderef-update &&
437 git symbolic-ref $prefix/symref $prefix/foo &&
438 git update-ref $prefix/foo $C &&
439 cat >expected <<-EOF &&
440 fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
442 printf "%s\n" "option no-deref" "update $prefix/symref $E $D" |
443 test_must_fail git update-ref --stdin 2>output.err &&
444 test_cmp expected output.err
447 test_expect_success
'existing old value blocks indirect no-deref create' '
448 prefix=refs/existing-noderef-create &&
449 git symbolic-ref $prefix/symref $prefix/foo &&
450 git update-ref $prefix/foo $C &&
451 cat >expected <<-EOF &&
452 fatal: cannot lock ref $SQ$prefix/symref$SQ: reference already exists
454 printf "%s\n" "option no-deref" "create $prefix/symref $E" |
455 test_must_fail git update-ref --stdin 2>output.err &&
456 test_cmp expected output.err
459 test_expect_success
'incorrect old value blocks indirect no-deref delete' '
460 prefix=refs/incorrect-noderef-delete &&
461 git symbolic-ref $prefix/symref $prefix/foo &&
462 git update-ref $prefix/foo $C &&
463 cat >expected <<-EOF &&
464 fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
466 printf "%s\n" "option no-deref" "delete $prefix/symref $D" |
467 test_must_fail git update-ref --stdin 2>output.err &&
468 test_cmp expected output.err
471 test_expect_success REFFILES
'non-empty directory blocks create' '
472 prefix=refs/ne-create &&
473 mkdir -p .git/$prefix/foo/bar &&
474 : >.git/$prefix/foo/bar/baz.lock &&
475 test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
476 cat >expected <<-EOF &&
477 fatal: cannot lock ref $SQ$prefix/foo$SQ: there is a non-empty directory $SQ.git/$prefix/foo$SQ blocking reference $SQ$prefix/foo$SQ
479 printf "%s\n" "update $prefix/foo $C" |
480 test_must_fail git update-ref --stdin 2>output.err &&
481 test_cmp expected output.err &&
482 cat >expected <<-EOF &&
483 fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ
485 printf "%s\n" "update $prefix/foo $D $C" |
486 test_must_fail git update-ref --stdin 2>output.err &&
487 test_cmp expected output.err
490 test_expect_success REFFILES
'broken reference blocks create' '
491 prefix=refs/broken-create &&
492 mkdir -p .git/$prefix &&
493 echo "gobbledigook" >.git/$prefix/foo &&
494 test_when_finished "rm -f .git/$prefix/foo" &&
495 cat >expected <<-EOF &&
496 fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
498 printf "%s\n" "update $prefix/foo $C" |
499 test_must_fail git update-ref --stdin 2>output.err &&
500 test_cmp expected output.err &&
501 cat >expected <<-EOF &&
502 fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
504 printf "%s\n" "update $prefix/foo $D $C" |
505 test_must_fail git update-ref --stdin 2>output.err &&
506 test_cmp expected output.err
509 test_expect_success REFFILES
'non-empty directory blocks indirect create' '
510 prefix=refs/ne-indirect-create &&
511 git symbolic-ref $prefix/symref $prefix/foo &&
512 mkdir -p .git/$prefix/foo/bar &&
513 : >.git/$prefix/foo/bar/baz.lock &&
514 test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
515 cat >expected <<-EOF &&
516 fatal: cannot lock ref $SQ$prefix/symref$SQ: there is a non-empty directory $SQ.git/$prefix/foo$SQ blocking reference $SQ$prefix/foo$SQ
518 printf "%s\n" "update $prefix/symref $C" |
519 test_must_fail git update-ref --stdin 2>output.err &&
520 test_cmp expected output.err &&
521 cat >expected <<-EOF &&
522 fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ
524 printf "%s\n" "update $prefix/symref $D $C" |
525 test_must_fail git update-ref --stdin 2>output.err &&
526 test_cmp expected output.err
529 test_expect_success REFFILES
'broken reference blocks indirect create' '
530 prefix=refs/broken-indirect-create &&
531 git symbolic-ref $prefix/symref $prefix/foo &&
532 echo "gobbledigook" >.git/$prefix/foo &&
533 test_when_finished "rm -f .git/$prefix/foo" &&
534 cat >expected <<-EOF &&
535 fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
537 printf "%s\n" "update $prefix/symref $C" |
538 test_must_fail git update-ref --stdin 2>output.err &&
539 test_cmp expected output.err &&
540 cat >expected <<-EOF &&
541 fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
543 printf "%s\n" "update $prefix/symref $D $C" |
544 test_must_fail git update-ref --stdin 2>output.err &&
545 test_cmp expected output.err
548 test_expect_success REFFILES
'no bogus intermediate values during delete' '
549 prefix=refs/slow-transaction &&
550 # Set up a reference with differing loose and packed versions:
551 git update-ref $prefix/foo $C &&
552 git pack-refs --all &&
553 git update-ref $prefix/foo $D &&
554 # Now try to update the reference, but hold the `packed-refs` lock
555 # for a while to see what happens while the process is blocked:
556 : >.git/packed-refs.lock &&
557 test_when_finished "rm -f .git/packed-refs.lock" &&
559 # Note: the following command is intentionally run in the
560 # background. We increase the timeout so that `update-ref`
561 # attempts to acquire the `packed-refs` lock for much longer
562 # than it takes for us to do the check then delete it:
563 git -c core.packedrefstimeout=30000 update-ref -d $prefix/foo &
566 # Give update-ref plenty of time to get to the point where it tries
567 # to lock packed-refs:
569 # Make sure that update-ref did not complete despite the lock:
571 # Verify that the reference still has its old value:
572 sha1=$(git rev-parse --verify --quiet $prefix/foo || echo undefined) &&
575 # This is what we hope for; it means that nothing
576 # user-visible has changed yet.
579 # This is not correct; it means the deletion has happened
580 # already even though update-ref should not have been
581 # able to acquire the lock yet.
582 echo "$prefix/foo deleted prematurely" &&
586 # This value should never be seen. Probably the loose
587 # reference has been deleted but the packed reference
589 echo "$prefix/foo incorrectly observed to be C" &&
594 echo "unexpected value observed for $prefix/foo: $sha1" &&
598 rm -f .git/packed-refs.lock &&
600 test_must_be_empty out &&
601 test_must_fail git rev-parse --verify --quiet $prefix/foo
604 test_expect_success REFFILES
'delete fails cleanly if packed-refs file is locked' '
605 prefix=refs/locked-packed-refs &&
606 # Set up a reference with differing loose and packed versions:
607 git update-ref $prefix/foo $C &&
608 git pack-refs --all &&
609 git update-ref $prefix/foo $D &&
610 git for-each-ref $prefix >unchanged &&
611 # Now try to delete it while the `packed-refs` lock is held:
612 : >.git/packed-refs.lock &&
613 test_when_finished "rm -f .git/packed-refs.lock" &&
614 test_must_fail git update-ref -d $prefix/foo >out 2>err &&
615 git for-each-ref $prefix >actual &&
616 test_grep "Unable to create $SQ.*packed-refs.lock$SQ: " err &&
617 test_cmp unchanged actual
620 test_expect_success REFFILES
'delete fails cleanly if packed-refs.new write fails' '
621 # Setup and expectations are similar to the test above.
622 prefix=refs/failed-packed-refs &&
623 git update-ref $prefix/foo $C &&
624 git pack-refs --all &&
625 git update-ref $prefix/foo $D &&
626 git for-each-ref $prefix >unchanged &&
627 # This should not happen in practice, but it is an easy way to get a
628 # reliable error (we open with create_tempfile(), which uses O_EXCL).
629 : >.git/packed-refs.new &&
630 test_when_finished "rm -f .git/packed-refs.new" &&
631 test_must_fail git update-ref -d $prefix/foo &&
632 git for-each-ref $prefix >actual &&
633 test_cmp unchanged actual