blob: b5606d93b52bf44366a51f3d4b6dd19e7a8816ab [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'
Ævar Arnfjörð Bjarmason03267e82022-11-08 19:17:39 +01004
5TEST_PASSES_SANITIZE_LEAK=true
Michael Haggerty433efca2015-05-11 17:25:03 +02006. ./test-lib.sh
7
Michael Haggerty017f7222016-06-10 08:55:40 +02008# 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
17# value of $prefix.
Michael Haggerty433efca2015-05-11 17:25:03 +020018test_update_rejected () {
Michael Haggerty0e4b63b2016-06-10 08:50:53 +020019 before="$1" &&
20 pack="$2" &&
21 create="$3" &&
22 error="$4" &&
Michael Haggerty433efca2015-05-11 17:25:03 +020023 printf "create $prefix/%s $C\n" $before |
24 git update-ref --stdin &&
25 git for-each-ref $prefix >unchanged &&
26 if $pack
27 then
28 git pack-refs --all
29 fi &&
30 printf "create $prefix/%s $C\n" $create >input &&
31 test_must_fail git update-ref --stdin <input 2>output.err &&
Nguyễn Thái Ngọc Duy661558f2018-07-21 09:49:35 +020032 test_i18ngrep -F "$error" output.err &&
Michael Haggerty433efca2015-05-11 17:25:03 +020033 git for-each-ref $prefix >actual &&
34 test_cmp unchanged actual
35}
36
Michael Haggerty2e9de012017-10-24 17:16:24 +020037# Test adding and deleting D/F-conflicting references in a single
38# transaction.
39df_test() {
40 prefix="$1"
41 pack=: symadd=false symdel=false add_del=false addref= delref=
42 shift
43 while test $# -gt 0
44 do
45 case "$1" in
46 --pack)
47 pack="git pack-refs --all"
48 shift
49 ;;
50 --sym-add)
51 # Perform the add via a symbolic reference
52 symadd=true
53 shift
54 ;;
55 --sym-del)
56 # Perform the del via a symbolic reference
57 symdel=true
58 shift
59 ;;
60 --del-add)
61 # Delete first reference then add second
62 add_del=false
63 delref="$prefix/r/$2"
64 addref="$prefix/r/$3"
65 shift 3
66 ;;
67 --add-del)
68 # Add first reference then delete second
69 add_del=true
70 addref="$prefix/r/$2"
71 delref="$prefix/r/$3"
72 shift 3
73 ;;
74 *)
75 echo 1>&2 "Extra args to df_test: $*"
76 return 1
77 ;;
78 esac
79 done
80 git update-ref "$delref" $C &&
81 if $symadd
82 then
83 addname="$prefix/s/symadd" &&
84 git symbolic-ref "$addname" "$addref"
85 else
86 addname="$addref"
87 fi &&
88 if $symdel
89 then
90 delname="$prefix/s/symdel" &&
91 git symbolic-ref "$delname" "$delref"
92 else
93 delname="$delref"
94 fi &&
95 cat >expected-err <<-EOF &&
Denton Liubd482d62019-09-05 15:10:05 -070096 fatal: cannot lock ref $SQ$addname$SQ: $SQ$delref$SQ exists; cannot create $SQ$addref$SQ
Michael Haggerty2e9de012017-10-24 17:16:24 +020097 EOF
98 $pack &&
99 if $add_del
100 then
101 printf "%s\n" "create $addname $D" "delete $delname"
102 else
103 printf "%s\n" "delete $delname" "create $addname $D"
104 fi >commands &&
105 test_must_fail git update-ref --stdin <commands 2>output.err &&
Ævar Arnfjörð Bjarmason1108cea2021-02-11 02:53:53 +0100106 test_cmp expected-err output.err &&
Michael Haggerty2e9de012017-10-24 17:16:24 +0200107 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
110}
111
Michael Haggerty433efca2015-05-11 17:25:03 +0200112test_expect_success 'setup' '
113
114 git commit --allow-empty -m Initial &&
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200115 C=$(git rev-parse HEAD) &&
116 git commit --allow-empty -m Second &&
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200117 D=$(git rev-parse HEAD) &&
118 git commit --allow-empty -m Third &&
119 E=$(git rev-parse HEAD)
Michael Haggerty433efca2015-05-11 17:25:03 +0200120'
121
122test_expect_success 'existing loose ref is a simple prefix of new' '
123
124 prefix=refs/1l &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200125 test_update_rejected "a c e" false "b c/x d" \
Denton Liubd482d62019-09-05 15:10:05 -0700126 "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x$SQ"
Michael Haggerty433efca2015-05-11 17:25:03 +0200127
128'
129
130test_expect_success 'existing packed ref is a simple prefix of new' '
131
132 prefix=refs/1p &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200133 test_update_rejected "a c e" true "b c/x d" \
Denton Liubd482d62019-09-05 15:10:05 -0700134 "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x$SQ"
Michael Haggerty433efca2015-05-11 17:25:03 +0200135
136'
137
138test_expect_success 'existing loose ref is a deeper prefix of new' '
139
140 prefix=refs/2l &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200141 test_update_rejected "a c e" false "b c/x/y d" \
Denton Liubd482d62019-09-05 15:10:05 -0700142 "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x/y$SQ"
Michael Haggerty433efca2015-05-11 17:25:03 +0200143
144'
145
146test_expect_success 'existing packed ref is a deeper prefix of new' '
147
148 prefix=refs/2p &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200149 test_update_rejected "a c e" true "b c/x/y d" \
Denton Liubd482d62019-09-05 15:10:05 -0700150 "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x/y$SQ"
Michael Haggerty433efca2015-05-11 17:25:03 +0200151
152'
153
154test_expect_success 'new ref is a simple prefix of existing loose' '
155
156 prefix=refs/3l &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200157 test_update_rejected "a c/x e" false "b c d" \
Denton Liubd482d62019-09-05 15:10:05 -0700158 "$SQ$prefix/c/x$SQ exists; cannot create $SQ$prefix/c$SQ"
Michael Haggerty433efca2015-05-11 17:25:03 +0200159
160'
161
162test_expect_success 'new ref is a simple prefix of existing packed' '
163
164 prefix=refs/3p &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200165 test_update_rejected "a c/x e" true "b c d" \
Denton Liubd482d62019-09-05 15:10:05 -0700166 "$SQ$prefix/c/x$SQ exists; cannot create $SQ$prefix/c$SQ"
Michael Haggerty433efca2015-05-11 17:25:03 +0200167
168'
169
170test_expect_success 'new ref is a deeper prefix of existing loose' '
171
172 prefix=refs/4l &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200173 test_update_rejected "a c/x/y e" false "b c d" \
Denton Liubd482d62019-09-05 15:10:05 -0700174 "$SQ$prefix/c/x/y$SQ exists; cannot create $SQ$prefix/c$SQ"
Michael Haggerty433efca2015-05-11 17:25:03 +0200175
176'
177
178test_expect_success 'new ref is a deeper prefix of existing packed' '
179
180 prefix=refs/4p &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200181 test_update_rejected "a c/x/y e" true "b c d" \
Denton Liubd482d62019-09-05 15:10:05 -0700182 "$SQ$prefix/c/x/y$SQ exists; cannot create $SQ$prefix/c$SQ"
Michael Haggerty433efca2015-05-11 17:25:03 +0200183
184'
185
Michael Haggertye9111042015-05-11 17:25:12 +0200186test_expect_success 'one new ref is a simple prefix of another' '
Michael Haggerty433efca2015-05-11 17:25:03 +0200187
188 prefix=refs/5 &&
Michael Haggerty0e4b63b2016-06-10 08:50:53 +0200189 test_update_rejected "a e" false "b c c/x d" \
Denton Liubd482d62019-09-05 15:10:05 -0700190 "cannot process $SQ$prefix/c$SQ and $SQ$prefix/c/x$SQ at the same time"
Michael Haggerty433efca2015-05-11 17:25:03 +0200191
192'
193
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000194test_expect_success REFFILES 'empty directory should not fool rev-parse' '
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200195 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
202'
203
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000204test_expect_success REFFILES 'empty directory should not fool for-each-ref' '
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200205 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
212'
213
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000214test_expect_success REFFILES 'empty directory should not fool create' '
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200215 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
219'
220
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000221test_expect_success REFFILES 'empty directory should not fool verify' '
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200222 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
228'
229
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000230test_expect_success REFFILES 'empty directory should not fool 1-arg update' '
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200231 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
237'
238
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000239test_expect_success REFFILES 'empty directory should not fool 2-arg update' '
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200240 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
246'
247
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000248test_expect_success REFFILES 'empty directory should not fool 0-arg delete' '
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200249 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
255'
256
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000257test_expect_success REFFILES 'empty directory should not fool 1-arg delete' '
Michael Haggerty19dd7d02016-05-05 13:22:23 +0200258 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
264'
265
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000266test_expect_success REFFILES 'D/F conflict prevents add long + delete short' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200267 df_test refs/df-al-ds --add-del foo/bar foo
268'
269
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000270test_expect_success REFFILES 'D/F conflict prevents add short + delete long' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200271 df_test refs/df-as-dl --add-del foo foo/bar
272'
273
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000274test_expect_success REFFILES 'D/F conflict prevents delete long + add short' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200275 df_test refs/df-dl-as --del-add foo/bar foo
276'
277
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000278test_expect_success REFFILES 'D/F conflict prevents delete short + add long' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200279 df_test refs/df-ds-al --del-add foo foo/bar
280'
281
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000282test_expect_success REFFILES 'D/F conflict prevents add long + delete short packed' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200283 df_test refs/df-al-dsp --pack --add-del foo/bar foo
284'
285
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000286test_expect_success REFFILES 'D/F conflict prevents add short + delete long packed' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200287 df_test refs/df-as-dlp --pack --add-del foo foo/bar
288'
289
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000290test_expect_success REFFILES 'D/F conflict prevents delete long packed + add short' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200291 df_test refs/df-dlp-as --pack --del-add foo/bar foo
292'
293
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000294test_expect_success REFFILES 'D/F conflict prevents delete short packed + add long' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200295 df_test refs/df-dsp-al --pack --del-add foo foo/bar
296'
297
298# Try some combinations involving symbolic refs...
299
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000300test_expect_success REFFILES 'D/F conflict prevents indirect add long + delete short' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200301 df_test refs/df-ial-ds --sym-add --add-del foo/bar foo
302'
303
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000304test_expect_success REFFILES 'D/F conflict prevents indirect add long + indirect delete short' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200305 df_test refs/df-ial-ids --sym-add --sym-del --add-del foo/bar foo
306'
307
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000308test_expect_success REFFILES 'D/F conflict prevents indirect add short + indirect delete long' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200309 df_test refs/df-ias-idl --sym-add --sym-del --add-del foo foo/bar
310'
311
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000312test_expect_success REFFILES 'D/F conflict prevents indirect delete long + indirect add short' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200313 df_test refs/df-idl-ias --sym-add --sym-del --del-add foo/bar foo
314'
315
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000316test_expect_success REFFILES 'D/F conflict prevents indirect add long + delete short packed' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200317 df_test refs/df-ial-dsp --sym-add --pack --add-del foo/bar foo
318'
319
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000320test_expect_success REFFILES 'D/F conflict prevents indirect add long + indirect delete short packed' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200321 df_test refs/df-ial-idsp --sym-add --sym-del --pack --add-del foo/bar foo
322'
323
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000324test_expect_success REFFILES 'D/F conflict prevents add long + indirect delete short packed' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200325 df_test refs/df-al-idsp --sym-del --pack --add-del foo/bar foo
326'
327
Han-Wen Nienhuys911e9e82021-11-29 18:20:22 +0000328test_expect_success REFFILES 'D/F conflict prevents indirect delete long packed + indirect add short' '
Michael Haggerty2e9de012017-10-24 17:16:24 +0200329 df_test refs/df-idlp-ias --sym-add --sym-del --pack --del-add foo/bar foo
330'
331
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200332# Test various errors when reading the old values of references...
333
334test_expect_success 'missing old value blocks update' '
335 prefix=refs/missing-update &&
336 cat >expected <<-EOF &&
Denton Liubd482d62019-09-05 15:10:05 -0700337 fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200338 EOF
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
342'
343
344test_expect_success 'incorrect old value blocks update' '
345 prefix=refs/incorrect-update &&
346 git update-ref $prefix/foo $C &&
347 cat >expected <<-EOF &&
Denton Liubd482d62019-09-05 15:10:05 -0700348 fatal: cannot lock ref $SQ$prefix/foo$SQ: is at $C but expected $D
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200349 EOF
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
353'
354
355test_expect_success 'existing old value blocks create' '
356 prefix=refs/existing-create &&
357 git update-ref $prefix/foo $C &&
358 cat >expected <<-EOF &&
Denton Liubd482d62019-09-05 15:10:05 -0700359 fatal: cannot lock ref $SQ$prefix/foo$SQ: reference already exists
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200360 EOF
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
364'
365
366test_expect_success 'incorrect old value blocks delete' '
367 prefix=refs/incorrect-delete &&
368 git update-ref $prefix/foo $C &&
369 cat >expected <<-EOF &&
Denton Liubd482d62019-09-05 15:10:05 -0700370 fatal: cannot lock ref $SQ$prefix/foo$SQ: is at $C but expected $D
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200371 EOF
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
375'
376
377test_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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700381 fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200382 EOF
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
386'
387
388test_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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700393 fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200394 EOF
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
398'
399
400test_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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700405 fatal: cannot lock ref $SQ$prefix/symref$SQ: reference already exists
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200406 EOF
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
410'
411
412test_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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700417 fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200418 EOF
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
422'
423
424test_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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700428 fatal: cannot lock ref $SQ$prefix/symref$SQ: reference is missing but expected $D
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200429 EOF
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
433'
434
435test_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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700440 fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200441 EOF
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
445'
446
Michael Haggertye3f51032016-06-07 09:29:23 +0200447test_expect_success 'existing old value blocks indirect no-deref create' '
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200448 prefix=refs/existing-noderef-create &&
449 git symbolic-ref $prefix/symref $prefix/foo &&
450 git update-ref $prefix/foo $C &&
451 cat >expected <<-EOF &&
Denton Liubd482d62019-09-05 15:10:05 -0700452 fatal: cannot lock ref $SQ$prefix/symref$SQ: reference already exists
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200453 EOF
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
457'
458
459test_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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700464 fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200465 EOF
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
469'
470
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000471test_expect_success REFFILES 'non-empty directory blocks create' '
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200472 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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700477 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
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200478 EOF
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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700483 fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200484 EOF
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
488'
489
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000490test_expect_success REFFILES 'broken reference blocks create' '
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200491 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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700496 fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200497 EOF
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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700502 fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200503 EOF
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
507'
508
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000509test_expect_success REFFILES 'non-empty directory blocks indirect create' '
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200510 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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700516 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
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200517 EOF
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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700522 fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200523 EOF
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
527'
528
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000529test_expect_success REFFILES 'broken reference blocks indirect create' '
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200530 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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700535 fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200536 EOF
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 &&
Denton Liubd482d62019-09-05 15:10:05 -0700541 fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
Michael Haggertyc5119dc2016-06-07 12:29:02 +0200542 EOF
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
546'
547
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000548test_expect_success REFFILES 'no bogus intermediate values during delete' '
Michael Haggerty6a2a7732017-09-08 15:51:50 +0200549 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 git for-each-ref $prefix >unchanged &&
555 # Now try to update the reference, but hold the `packed-refs` lock
556 # for a while to see what happens while the process is blocked:
557 : >.git/packed-refs.lock &&
558 test_when_finished "rm -f .git/packed-refs.lock" &&
559 {
560 # Note: the following command is intentionally run in the
561 # background. We increase the timeout so that `update-ref`
SZEDER Gábor377d8452018-08-01 01:32:48 +0200562 # attempts to acquire the `packed-refs` lock for much longer
563 # than it takes for us to do the check then delete it:
564 git -c core.packedrefstimeout=30000 update-ref -d $prefix/foo &
Michael Haggerty6a2a7732017-09-08 15:51:50 +0200565 } &&
566 pid2=$! &&
567 # Give update-ref plenty of time to get to the point where it tries
568 # to lock packed-refs:
569 sleep 1 &&
570 # Make sure that update-ref did not complete despite the lock:
571 kill -0 $pid2 &&
572 # Verify that the reference still has its old value:
573 sha1=$(git rev-parse --verify --quiet $prefix/foo || echo undefined) &&
574 case "$sha1" in
575 $D)
576 # This is what we hope for; it means that nothing
577 # user-visible has changed yet.
578 : ;;
579 undefined)
580 # This is not correct; it means the deletion has happened
581 # already even though update-ref should not have been
582 # able to acquire the lock yet.
583 echo "$prefix/foo deleted prematurely" &&
584 break
585 ;;
586 $C)
587 # This value should never be seen. Probably the loose
588 # reference has been deleted but the packed reference
589 # is still there:
590 echo "$prefix/foo incorrectly observed to be C" &&
591 break
592 ;;
593 *)
594 # WTF?
595 echo "unexpected value observed for $prefix/foo: $sha1" &&
596 break
597 ;;
598 esac >out &&
599 rm -f .git/packed-refs.lock &&
600 wait $pid2 &&
601 test_must_be_empty out &&
602 test_must_fail git rev-parse --verify --quiet $prefix/foo
603'
604
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000605test_expect_success REFFILES 'delete fails cleanly if packed-refs file is locked' '
Michael Haggerty6a2a7732017-09-08 15:51:50 +0200606 prefix=refs/locked-packed-refs &&
607 # Set up a reference with differing loose and packed versions:
608 git update-ref $prefix/foo $C &&
609 git pack-refs --all &&
610 git update-ref $prefix/foo $D &&
611 git for-each-ref $prefix >unchanged &&
612 # Now try to delete it while the `packed-refs` lock is held:
613 : >.git/packed-refs.lock &&
614 test_when_finished "rm -f .git/packed-refs.lock" &&
615 test_must_fail git update-ref -d $prefix/foo >out 2>err &&
616 git for-each-ref $prefix >actual &&
Denton Liubd482d62019-09-05 15:10:05 -0700617 test_i18ngrep "Unable to create $SQ.*packed-refs.lock$SQ: " err &&
Michael Haggerty6a2a7732017-09-08 15:51:50 +0200618 test_cmp unchanged actual
619'
620
Han-Wen Nienhuysfe8fc092021-05-31 16:56:33 +0000621test_expect_success REFFILES 'delete fails cleanly if packed-refs.new write fails' '
Jeff King249e8dc2019-03-21 05:28:44 -0400622 # Setup and expectations are similar to the test above.
623 prefix=refs/failed-packed-refs &&
624 git update-ref $prefix/foo $C &&
625 git pack-refs --all &&
626 git update-ref $prefix/foo $D &&
627 git for-each-ref $prefix >unchanged &&
628 # This should not happen in practice, but it is an easy way to get a
629 # reliable error (we open with create_tempfile(), which uses O_EXCL).
630 : >.git/packed-refs.new &&
631 test_when_finished "rm -f .git/packed-refs.new" &&
632 test_must_fail git update-ref -d $prefix/foo &&
633 git for-each-ref $prefix >actual &&
634 test_cmp unchanged actual
635'
636
Michael Haggerty433efca2015-05-11 17:25:03 +0200637test_done