Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Shawn Pearce
4 #
5
6 test_description='Test git update-ref and basic ref logging'
7
8 . ./test-lib.sh
9
10 Z=$ZERO_OID
11
12 m=refs/heads/main
13 outside=refs/foo
14 bare=bare-repo
15
16 create_test_commits ()
17 {
18 prfx="$1"
19 for name in A B C D E F
20 do
21 test_tick &&
22 T=$(git write-tree) &&
23 sha1=$(echo $name | git commit-tree $T) &&
24 eval $prfx$name=$sha1
25 done
26 }
27
28 test_expect_success setup '
29 git checkout --orphan main &&
30 create_test_commits "" &&
31 mkdir $bare &&
32 cd $bare &&
33 git init --bare -b main &&
34 create_test_commits "bare" &&
35 cd -
36 '
37
38 test_expect_success "create $m" '
39 git update-ref $m $A &&
40 test $A = $(git show-ref -s --verify $m)
41 '
42 test_expect_success "create $m with oldvalue verification" '
43 git update-ref $m $B $A &&
44 test $B = $(git show-ref -s --verify $m)
45 '
46 test_expect_success "fail to delete $m with stale ref" '
47 test_must_fail git update-ref -d $m $A &&
48 test $B = "$(git show-ref -s --verify $m)"
49 '
50 test_expect_success "delete $m" '
51 test_when_finished "git update-ref -d $m" &&
52 git update-ref -d $m $B &&
53 test_must_fail git show-ref --verify -q $m
54 '
55
56 test_expect_success "delete $m without oldvalue verification" '
57 test_when_finished "git update-ref -d $m" &&
58 git update-ref $m $A &&
59 test $A = $(git show-ref -s --verify $m) &&
60 git update-ref -d $m &&
61 test_must_fail git show-ref --verify -q $m
62 '
63
64 test_expect_success "fail to create $n due to file/directory conflict" '
65 test_when_finished "git update-ref -d refs/heads/gu" &&
66 git update-ref refs/heads/gu $A &&
67 test_must_fail git update-ref refs/heads/gu/fixes $A
68 '
69
70 test_expect_success "create $m (by HEAD)" '
71 git update-ref HEAD $A &&
72 test $A = $(git show-ref -s --verify $m)
73 '
74 test_expect_success "create $m (by HEAD) with oldvalue verification" '
75 git update-ref HEAD $B $A &&
76 test $B = $(git show-ref -s --verify $m)
77 '
78 test_expect_success "fail to delete $m (by HEAD) with stale ref" '
79 test_must_fail git update-ref -d HEAD $A &&
80 test $B = $(git show-ref -s --verify $m)
81 '
82 test_expect_success "delete $m (by HEAD)" '
83 test_when_finished "git update-ref -d $m" &&
84 git update-ref -d HEAD $B &&
85 test_must_fail git show-ref --verify -q $m
86 '
87
88 test_expect_success "deleting current branch adds message to HEAD's log" '
89 test_when_finished "git update-ref -d $m" &&
90 git update-ref $m $A &&
91 git symbolic-ref HEAD $m &&
92 git update-ref -m delete-$m -d $m &&
93 test_must_fail git show-ref --verify -q $m &&
94 test-tool ref-store main for-each-reflog-ent HEAD >actual &&
95 grep "delete-$m$" actual
96 '
97
98 test_expect_success "deleting by HEAD adds message to HEAD's log" '
99 test_when_finished "git update-ref -d $m" &&
100 git update-ref $m $A &&
101 git symbolic-ref HEAD $m &&
102 git update-ref -m delete-by-head -d HEAD &&
103 test_must_fail git show-ref --verify -q $m &&
104 test-tool ref-store main for-each-reflog-ent HEAD >actual &&
105 grep "delete-by-head$" actual
106 '
107
108 test_expect_success 'update-ref does not create reflogs by default' '
109 test_when_finished "git update-ref -d $outside" &&
110 git update-ref $outside $A &&
111 git rev-parse $A >expect &&
112 git rev-parse $outside >actual &&
113 test_cmp expect actual &&
114 test_must_fail git reflog exists $outside
115 '
116
117 test_expect_success 'update-ref creates reflogs with --create-reflog' '
118 test_when_finished "git update-ref -d $outside" &&
119 git update-ref --create-reflog $outside $A &&
120 git rev-parse $A >expect &&
121 git rev-parse $outside >actual &&
122 test_cmp expect actual &&
123 git reflog exists $outside
124 '
125
126 test_expect_success 'creates no reflog in bare repository' '
127 git -C $bare update-ref $m $bareA &&
128 git -C $bare rev-parse $bareA >expect &&
129 git -C $bare rev-parse $m >actual &&
130 test_cmp expect actual &&
131 test_must_fail git -C $bare reflog exists $m
132 '
133
134 test_expect_success 'core.logAllRefUpdates=true creates reflog in bare repository' '
135 test_when_finished "git -C $bare config --unset core.logAllRefUpdates && \
136 test-tool ref-store main delete-reflog $m" &&
137 git -C $bare config core.logAllRefUpdates true &&
138 git -C $bare update-ref $m $bareB &&
139 git -C $bare rev-parse $bareB >expect &&
140 git -C $bare rev-parse $m >actual &&
141 test_cmp expect actual &&
142 git -C $bare reflog exists $m
143 '
144
145 test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' '
146 test_config core.logAllRefUpdates true &&
147 test_when_finished "git update-ref -d $outside" &&
148 git update-ref $outside $A &&
149 git rev-parse $A >expect &&
150 git rev-parse $outside >actual &&
151 test_cmp expect actual &&
152 test_must_fail git reflog exists $outside
153 '
154
155 test_expect_success 'core.logAllRefUpdates=always creates reflog by default' '
156 test_config core.logAllRefUpdates always &&
157 test_when_finished "git update-ref -d $outside" &&
158 git update-ref $outside $A &&
159 git rev-parse $A >expect &&
160 git rev-parse $outside >actual &&
161 test_cmp expect actual &&
162 git reflog exists $outside
163 '
164
165 test_expect_success 'core.logAllRefUpdates=always creates reflog for ORIG_HEAD' '
166 test_config core.logAllRefUpdates always &&
167 git update-ref ORIG_HEAD $A &&
168 git reflog exists ORIG_HEAD
169 '
170
171 test_expect_success '--no-create-reflog overrides core.logAllRefUpdates=always' '
172 test_config core.logAllRefUpdates true &&
173 test_when_finished "git update-ref -d $outside" &&
174 git update-ref --no-create-reflog $outside $A &&
175 git rev-parse $A >expect &&
176 git rev-parse $outside >actual &&
177 test_cmp expect actual &&
178 test_must_fail git reflog exists $outside
179 '
180
181 test_expect_success "create $m (by HEAD)" '
182 git update-ref HEAD $A &&
183 test $A = $(git show-ref -s --verify $m)
184 '
185 test_expect_success 'pack refs' '
186 git pack-refs --all
187 '
188 test_expect_success "move $m (by HEAD)" '
189 git update-ref HEAD $B $A &&
190 test $B = $(git show-ref -s --verify $m)
191 '
192 test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" '
193 test_when_finished "git update-ref -d $m" &&
194 git update-ref -d HEAD $B &&
195 ! grep "$m" .git/packed-refs &&
196 test_must_fail git show-ref --verify -q $m
197 '
198
199 test_expect_success 'delete symref without dereference' '
200 test_when_finished "git update-ref -d $m" &&
201 echo foo >foo.c &&
202 git add foo.c &&
203 git commit -m foo &&
204 git symbolic-ref SYMREF $m &&
205 git update-ref --no-deref -d SYMREF &&
206 git show-ref --verify -q $m &&
207 test_must_fail git show-ref --verify -q SYMREF &&
208 test_must_fail git symbolic-ref SYMREF
209 '
210
211 test_expect_success 'delete symref without dereference when the referred ref is packed' '
212 test_when_finished "git update-ref -d $m" &&
213 echo foo >foo.c &&
214 git add foo.c &&
215 git commit -m foo &&
216 git symbolic-ref SYMREF $m &&
217 git pack-refs --all &&
218 git update-ref --no-deref -d SYMREF &&
219 git show-ref --verify -q $m &&
220 test_must_fail git show-ref --verify -q SYMREF &&
221 test_must_fail git symbolic-ref SYMREF
222 '
223
224 test_expect_success 'update-ref -d is not confused by self-reference' '
225 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF refs/heads/self" &&
226 git symbolic-ref refs/heads/self refs/heads/self &&
227 git symbolic-ref --no-recurse refs/heads/self &&
228 test_must_fail git update-ref -d refs/heads/self &&
229 git symbolic-ref --no-recurse refs/heads/self
230 '
231
232 test_expect_success 'update-ref --no-deref -d can delete self-reference' '
233 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF refs/heads/self" &&
234 git symbolic-ref refs/heads/self refs/heads/self &&
235 git symbolic-ref --no-recurse refs/heads/self &&
236 git update-ref --no-deref -d refs/heads/self &&
237 test_must_fail git show-ref --verify -q refs/heads/self
238 '
239
240 test_expect_success REFFILES 'update-ref --no-deref -d can delete reference to bad ref' '
241 >.git/refs/heads/bad &&
242 test_when_finished "rm -f .git/refs/heads/bad" &&
243 git symbolic-ref refs/heads/ref-to-bad refs/heads/bad &&
244 test_when_finished "git update-ref -d refs/heads/ref-to-bad" &&
245 git symbolic-ref --no-recurse refs/heads/ref-to-bad &&
246 git update-ref --no-deref -d refs/heads/ref-to-bad &&
247 test_must_fail git show-ref --verify -q refs/heads/ref-to-bad
248 '
249
250 test_expect_success '(not) create HEAD with old sha1' '
251 test_must_fail git update-ref HEAD $A $B
252 '
253 test_expect_success "(not) prior created .git/$m" '
254 test_when_finished "git update-ref -d $m" &&
255 test_must_fail git show-ref --verify -q $m
256 '
257
258 test_expect_success 'create HEAD' '
259 git update-ref HEAD $A
260 '
261 test_expect_success '(not) change HEAD with wrong SHA1' '
262 test_must_fail git update-ref HEAD $B $Z
263 '
264 test_expect_success "(not) changed .git/$m" '
265 test_when_finished "git update-ref -d $m" &&
266 ! test $B = $(git show-ref -s --verify $m)
267 '
268
269 test_expect_success "clean up reflog" '
270 test-tool ref-store main delete-reflog $m
271 '
272
273 test_expect_success "create $m (logged by touch)" '
274 test_config core.logAllRefUpdates false &&
275 GIT_COMMITTER_DATE="2005-05-26 23:30" \
276 git update-ref --create-reflog HEAD $A -m "Initial Creation" &&
277 test $A = $(git show-ref -s --verify $m)
278 '
279 test_expect_success "update $m (logged by touch)" '
280 test_config core.logAllRefUpdates false &&
281 GIT_COMMITTER_DATE="2005-05-26 23:31" \
282 git update-ref HEAD $B $A -m "Switch" &&
283 test $B = $(git show-ref -s --verify $m)
284 '
285 test_expect_success "set $m (logged by touch)" '
286 test_config core.logAllRefUpdates false &&
287 GIT_COMMITTER_DATE="2005-05-26 23:41" \
288 git update-ref HEAD $A &&
289 test $A = $(git show-ref -s --verify $m)
290 '
291
292 cat >expect <<EOF
293 $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation
294 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch
295 $B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
296 EOF
297 test_expect_success "verifying $m's log (logged by touch)" '
298 test_when_finished "git update-ref -d $m && git reflog expire --expire=all --all && rm -rf actual expect" &&
299 test-tool ref-store main for-each-reflog-ent $m >actual &&
300 test_cmp actual expect
301 '
302
303 test_expect_success "create $m (logged by config)" '
304 test_config core.logAllRefUpdates true &&
305 GIT_COMMITTER_DATE="2005-05-26 23:32" \
306 git update-ref HEAD $A -m "Initial Creation" &&
307 test $A = $(git show-ref -s --verify $m)
308 '
309 test_expect_success "update $m (logged by config)" '
310 test_config core.logAllRefUpdates true &&
311 GIT_COMMITTER_DATE="2005-05-26 23:33" \
312 git update-ref HEAD $B $A -m "Switch" &&
313 test $B = $(git show-ref -s --verify $m)
314 '
315 test_expect_success "set $m (logged by config)" '
316 test_config core.logAllRefUpdates true &&
317 GIT_COMMITTER_DATE="2005-05-26 23:43" \
318 git update-ref HEAD $A &&
319 test $A = $(git show-ref -s --verify $m)
320 '
321
322 cat >expect <<EOF
323 $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation
324 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch
325 $B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
326 EOF
327 test_expect_success "verifying $m's log (logged by config)" '
328 test_when_finished "git update-ref -d $m && git reflog expire --expire=all --all && rm -rf actual expect" &&
329 test-tool ref-store main for-each-reflog-ent $m >actual &&
330 test_cmp actual expect
331 '
332
333 test_expect_success 'set up for querying the reflog' '
334 git update-ref -d $m &&
335 test-tool ref-store main delete-reflog $m &&
336
337 GIT_COMMITTER_DATE="1117150320 -0500" git update-ref $m $C &&
338 GIT_COMMITTER_DATE="1117150350 -0500" git update-ref $m $A &&
339 GIT_COMMITTER_DATE="1117150380 -0500" git update-ref $m $B &&
340 GIT_COMMITTER_DATE="1117150680 -0500" git update-ref $m $F &&
341 GIT_COMMITTER_DATE="1117150980 -0500" git update-ref $m $E &&
342 git update-ref $m $D &&
343 # Delete the last reflog entry so that the tip of m and the reflog for
344 # it disagree.
345 git reflog delete $m@{0} &&
346
347 cat >expect <<-EOF &&
348 $Z $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
349 $C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
350 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
351 $B $F $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
352 $F $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
353 EOF
354 test-tool ref-store main for-each-reflog-ent $m >actual &&
355 test_cmp expect actual
356 '
357
358 ed="Thu, 26 May 2005 18:32:00 -0500"
359 gd="Thu, 26 May 2005 18:33:00 -0500"
360 ld="Thu, 26 May 2005 18:43:00 -0500"
361 test_expect_success 'Query "main@{May 25 2005}" (before history)' '
362 test_when_finished "rm -f o e" &&
363 git rev-parse --verify "main@{May 25 2005}" >o 2>e &&
364 echo "$C" >expect &&
365 test_cmp expect o &&
366 echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
367 test_cmp expect e
368 '
369 test_expect_success 'Query main@{2005-05-25} (before history)' '
370 test_when_finished "rm -f o e" &&
371 git rev-parse --verify main@{2005-05-25} >o 2>e &&
372 echo "$C" >expect &&
373 test_cmp expect o &&
374 echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
375 test_cmp expect e
376 '
377 test_expect_success 'Query "main@{May 26 2005 23:31:59}" (1 second before history)' '
378 test_when_finished "rm -f o e" &&
379 git rev-parse --verify "main@{May 26 2005 23:31:59}" >o 2>e &&
380 echo "$C" >expect &&
381 test_cmp expect o &&
382 echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
383 test_cmp expect e
384 '
385 test_expect_success 'Query "main@{May 26 2005 23:32:00}" (exactly history start)' '
386 test_when_finished "rm -f o e" &&
387 git rev-parse --verify "main@{May 26 2005 23:32:00}" >o 2>e &&
388 echo "$C" >expect &&
389 test_cmp expect o &&
390 test_must_be_empty e
391 '
392 test_expect_success 'Query "main@{May 26 2005 23:32:30}" (first non-creation change)' '
393 test_when_finished "rm -f o e" &&
394 git rev-parse --verify "main@{May 26 2005 23:32:30}" >o 2>e &&
395 echo "$A" >expect &&
396 test_cmp expect o &&
397 test_must_be_empty e
398 '
399 test_expect_success 'Query "main@{2005-05-26 23:33:01}" (middle of history with gap)' '
400 test_when_finished "rm -f o e" &&
401 git rev-parse --verify "main@{2005-05-26 23:33:01}" >o 2>e &&
402 echo "$B" >expect &&
403 test_cmp expect o
404 '
405 test_expect_success 'Query "main@{2005-05-26 23:38:00}" (middle of history)' '
406 test_when_finished "rm -f o e" &&
407 git rev-parse --verify "main@{2005-05-26 23:38:00}" >o 2>e &&
408 echo "$F" >expect &&
409 test_cmp expect o &&
410 test_must_be_empty e
411 '
412 test_expect_success 'Query "main@{2005-05-26 23:43:00}" (exact end of history)' '
413 test_when_finished "rm -f o e" &&
414 git rev-parse --verify "main@{2005-05-26 23:43:00}" >o 2>e &&
415 echo "$E" >expect &&
416 test_cmp expect o &&
417 test_must_be_empty e
418 '
419 test_expect_success 'Query "main@{2005-05-28}" (past end of history)' '
420 test_when_finished "rm -f o e" &&
421 git rev-parse --verify "main@{2005-05-28}" >o 2>e &&
422 echo "$D" >expect &&
423 test_cmp expect o &&
424 test_grep -F "warning: log for ref $m unexpectedly ended on $ld" e
425 '
426
427 rm -f expect
428 git update-ref -d $m
429
430 test_expect_success 'query reflog with gap' '
431 test_when_finished "git update-ref -d $m" &&
432
433 GIT_COMMITTER_DATE="1117150320 -0500" git update-ref $m $A &&
434 GIT_COMMITTER_DATE="1117150380 -0500" git update-ref $m $B &&
435 GIT_COMMITTER_DATE="1117150480 -0500" git update-ref $m $C &&
436 GIT_COMMITTER_DATE="1117150580 -0500" git update-ref $m $D &&
437 GIT_COMMITTER_DATE="1117150680 -0500" git update-ref $m $F &&
438 git reflog delete $m@{2} &&
439
440 git rev-parse --verify "main@{2005-05-26 23:33:01}" >actual 2>stderr &&
441 echo "$B" >expect &&
442 test_cmp expect actual &&
443 test_grep -F "warning: log for ref $m has gap after $gd" stderr
444 '
445
446 test_expect_success 'creating initial files' '
447 test_when_finished rm -f M &&
448 echo TEST >F &&
449 git add F &&
450 GIT_AUTHOR_DATE="2005-05-26 23:30" \
451 GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
452 h_TEST=$(git rev-parse --verify HEAD) &&
453 echo The other day this did not work. >M &&
454 echo And then Bob told me how to fix it. >>M &&
455 echo OTHER >F &&
456 GIT_AUTHOR_DATE="2005-05-26 23:41" \
457 GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a &&
458 h_OTHER=$(git rev-parse --verify HEAD) &&
459 GIT_AUTHOR_DATE="2005-05-26 23:44" \
460 GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend &&
461 h_FIXED=$(git rev-parse --verify HEAD) &&
462 echo Merged initial commit and a later commit. >M &&
463 echo $h_TEST >.git/MERGE_HEAD &&
464 GIT_AUTHOR_DATE="2005-05-26 23:45" \
465 GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
466 h_MERGED=$(git rev-parse --verify HEAD)
467 '
468
469 cat >expect <<EOF
470 $Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add
471 $h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work.
472 $h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work.
473 $h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit.
474 EOF
475 test_expect_success 'git commit logged updates' '
476 test-tool ref-store main for-each-reflog-ent $m >actual &&
477 test_cmp expect actual
478 '
479 unset h_TEST h_OTHER h_FIXED h_MERGED
480
481 test_expect_success 'git cat-file blob main:F (expect OTHER)' '
482 test OTHER = $(git cat-file blob main:F)
483 '
484 test_expect_success 'git cat-file blob main@{2005-05-26 23:30}:F (expect TEST)' '
485 test TEST = $(git cat-file blob "main@{2005-05-26 23:30}:F")
486 '
487 test_expect_success 'git cat-file blob main@{2005-05-26 23:42}:F (expect OTHER)' '
488 test OTHER = $(git cat-file blob "main@{2005-05-26 23:42}:F")
489 '
490
491 # Test adding and deleting pseudorefs
492
493 test_expect_success 'given old value for missing pseudoref, do not create' '
494 test_must_fail git update-ref PSEUDOREF $A $B 2>err &&
495 test_must_fail git rev-parse PSEUDOREF &&
496 test_grep "unable to resolve reference" err
497 '
498
499 test_expect_success 'create pseudoref' '
500 git update-ref PSEUDOREF $A &&
501 test $A = $(git show-ref -s --verify PSEUDOREF)
502 '
503
504 test_expect_success 'overwrite pseudoref with no old value given' '
505 git update-ref PSEUDOREF $B &&
506 test $B = $(git show-ref -s --verify PSEUDOREF)
507 '
508
509 test_expect_success 'overwrite pseudoref with correct old value' '
510 git update-ref PSEUDOREF $C $B &&
511 test $C = $(git show-ref -s --verify PSEUDOREF)
512 '
513
514 test_expect_success 'do not overwrite pseudoref with wrong old value' '
515 test_must_fail git update-ref PSEUDOREF $D $E 2>err &&
516 test $C = $(git show-ref -s --verify PSEUDOREF) &&
517 test_grep "cannot lock ref.*expected" err
518 '
519
520 test_expect_success 'delete pseudoref' '
521 git update-ref -d PSEUDOREF &&
522 test_must_fail git show-ref -s --verify PSEUDOREF
523 '
524
525 test_expect_success 'do not delete pseudoref with wrong old value' '
526 git update-ref PSEUDOREF $A &&
527 test_must_fail git update-ref -d PSEUDOREF $B 2>err &&
528 test $A = $(git show-ref -s --verify PSEUDOREF) &&
529 test_grep "cannot lock ref.*expected" err
530 '
531
532 test_expect_success 'delete pseudoref with correct old value' '
533 git update-ref -d PSEUDOREF $A &&
534 test_must_fail git show-ref -s --verify PSEUDOREF
535 '
536
537 test_expect_success 'create pseudoref with old OID zero' '
538 git update-ref PSEUDOREF $A $Z &&
539 test $A = $(git show-ref -s --verify PSEUDOREF)
540 '
541
542 test_expect_success 'do not overwrite pseudoref with old OID zero' '
543 test_when_finished git update-ref -d PSEUDOREF &&
544 test_must_fail git update-ref PSEUDOREF $B $Z 2>err &&
545 test $A = $(git show-ref -s --verify PSEUDOREF) &&
546 test_grep "already exists" err
547 '
548
549 # Test --stdin
550
551 a=refs/heads/a
552 b=refs/heads/b
553 c=refs/heads/c
554 E='""'
555 F='%s\0'
556 pws='path with space'
557
558 test_expect_success 'stdin test setup' '
559 echo "$pws" >"$pws" &&
560 git add -- "$pws" &&
561 git commit -m "$pws"
562 '
563
564 test_expect_success '-z fails without --stdin' '
565 test_must_fail git update-ref -z $m $m $m 2>err &&
566 test_grep "usage: git update-ref" err
567 '
568
569 test_expect_success 'stdin works with no input' '
570 >stdin &&
571 git update-ref --stdin <stdin &&
572 git rev-parse --verify -q $m
573 '
574
575 test_expect_success 'stdin fails on empty line' '
576 echo "" >stdin &&
577 test_must_fail git update-ref --stdin <stdin 2>err &&
578 grep "fatal: empty command in input" err
579 '
580
581 test_expect_success 'stdin fails on only whitespace' '
582 echo " " >stdin &&
583 test_must_fail git update-ref --stdin <stdin 2>err &&
584 grep "fatal: whitespace before command: " err
585 '
586
587 test_expect_success 'stdin fails on leading whitespace' '
588 echo " create $a $m" >stdin &&
589 test_must_fail git update-ref --stdin <stdin 2>err &&
590 grep "fatal: whitespace before command: create $a $m" err
591 '
592
593 test_expect_success 'stdin fails on unknown command' '
594 echo "unknown $a" >stdin &&
595 test_must_fail git update-ref --stdin <stdin 2>err &&
596 grep "fatal: unknown command: unknown $a" err
597 '
598
599 test_expect_success 'stdin fails on unbalanced quotes' '
600 echo "create $a \"main" >stdin &&
601 test_must_fail git update-ref --stdin <stdin 2>err &&
602 grep "fatal: badly quoted argument: \\\"main" err
603 '
604
605 test_expect_success 'stdin fails on invalid escape' '
606 echo "create $a \"ma\zn\"" >stdin &&
607 test_must_fail git update-ref --stdin <stdin 2>err &&
608 grep "fatal: badly quoted argument: \\\"ma\\\\zn\\\"" err
609 '
610
611 test_expect_success 'stdin fails on junk after quoted argument' '
612 echo "create \"$a\"main" >stdin &&
613 test_must_fail git update-ref --stdin <stdin 2>err &&
614 grep "fatal: unexpected character after quoted argument: \\\"$a\\\"main" err
615 '
616
617 test_expect_success 'stdin fails create with no ref' '
618 echo "create " >stdin &&
619 test_must_fail git update-ref --stdin <stdin 2>err &&
620 grep "fatal: create: missing <ref>" err
621 '
622
623 test_expect_success 'stdin fails create with no new value' '
624 echo "create $a" >stdin &&
625 test_must_fail git update-ref --stdin <stdin 2>err &&
626 grep "fatal: create $a: missing <new-oid>" err
627 '
628
629 test_expect_success 'stdin fails create with too many arguments' '
630 echo "create $a $m $m" >stdin &&
631 test_must_fail git update-ref --stdin <stdin 2>err &&
632 grep "fatal: create $a: extra input: $m" err
633 '
634
635 test_expect_success 'stdin fails update with no ref' '
636 echo "update " >stdin &&
637 test_must_fail git update-ref --stdin <stdin 2>err &&
638 grep "fatal: update: missing <ref>" err
639 '
640
641 test_expect_success 'stdin fails update with no new value' '
642 echo "update $a" >stdin &&
643 test_must_fail git update-ref --stdin <stdin 2>err &&
644 grep "fatal: update $a: missing <new-oid>" err
645 '
646
647 test_expect_success 'stdin fails update with too many arguments' '
648 echo "update $a $m $m $m" >stdin &&
649 test_must_fail git update-ref --stdin <stdin 2>err &&
650 grep "fatal: update $a: extra input: $m" err
651 '
652
653 test_expect_success 'stdin fails delete with no ref' '
654 echo "delete " >stdin &&
655 test_must_fail git update-ref --stdin <stdin 2>err &&
656 grep "fatal: delete: missing <ref>" err
657 '
658
659 test_expect_success 'stdin fails delete with too many arguments' '
660 echo "delete $a $m $m" >stdin &&
661 test_must_fail git update-ref --stdin <stdin 2>err &&
662 grep "fatal: delete $a: extra input: $m" err
663 '
664
665 test_expect_success 'stdin fails verify with too many arguments' '
666 echo "verify $a $m $m" >stdin &&
667 test_must_fail git update-ref --stdin <stdin 2>err &&
668 grep "fatal: verify $a: extra input: $m" err
669 '
670
671 test_expect_success 'stdin fails option with unknown name' '
672 echo "option unknown" >stdin &&
673 test_must_fail git update-ref --stdin <stdin 2>err &&
674 grep "fatal: option unknown: unknown" err
675 '
676
677 test_expect_success 'stdin fails with duplicate refs' '
678 cat >stdin <<-EOF &&
679 create $a $m
680 create $b $m
681 create $a $m
682 EOF
683 test_must_fail git update-ref --stdin <stdin 2>err &&
684 test_grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
685 '
686
687 test_expect_success 'stdin create ref works' '
688 echo "create $a $m" >stdin &&
689 git update-ref --stdin <stdin &&
690 git rev-parse $m >expect &&
691 git rev-parse $a >actual &&
692 test_cmp expect actual
693 '
694
695 test_expect_success 'stdin does not create reflogs by default' '
696 test_when_finished "git update-ref -d $outside" &&
697 echo "create $outside $m" >stdin &&
698 git update-ref --stdin <stdin &&
699 git rev-parse $m >expect &&
700 git rev-parse $outside >actual &&
701 test_cmp expect actual &&
702 test_must_fail git reflog exists $outside
703 '
704
705 test_expect_success 'stdin creates reflogs with --create-reflog' '
706 test_when_finished "git update-ref -d $outside" &&
707 echo "create $outside $m" >stdin &&
708 git update-ref --create-reflog --stdin <stdin &&
709 git rev-parse $m >expect &&
710 git rev-parse $outside >actual &&
711 test_cmp expect actual &&
712 git reflog exists $outside
713 '
714
715 test_expect_success 'stdin succeeds with quoted argument' '
716 git update-ref -d $a &&
717 echo "create $a \"$m\"" >stdin &&
718 git update-ref --stdin <stdin &&
719 git rev-parse $m >expect &&
720 git rev-parse $a >actual &&
721 test_cmp expect actual
722 '
723
724 test_expect_success 'stdin succeeds with escaped character' '
725 git update-ref -d $a &&
726 echo "create $a \"ma\\151n\"" >stdin &&
727 git update-ref --stdin <stdin &&
728 git rev-parse $m >expect &&
729 git rev-parse $a >actual &&
730 test_cmp expect actual
731 '
732
733 test_expect_success 'stdin update ref creates with zero old value' '
734 echo "update $b $m $Z" >stdin &&
735 git update-ref --stdin <stdin &&
736 git rev-parse $m >expect &&
737 git rev-parse $b >actual &&
738 test_cmp expect actual &&
739 git update-ref -d $b
740 '
741
742 test_expect_success 'stdin update ref creates with empty old value' '
743 echo "update $b $m $E" >stdin &&
744 git update-ref --stdin <stdin &&
745 git rev-parse $m >expect &&
746 git rev-parse $b >actual &&
747 test_cmp expect actual
748 '
749
750 test_expect_success 'stdin create ref works with path with space to blob' '
751 echo "create refs/blobs/pws \"$m:$pws\"" >stdin &&
752 git update-ref --stdin <stdin &&
753 git rev-parse "$m:$pws" >expect &&
754 git rev-parse refs/blobs/pws >actual &&
755 test_cmp expect actual &&
756 git update-ref -d refs/blobs/pws
757 '
758
759 test_expect_success 'stdin update ref fails with wrong old value' '
760 echo "update $c $m $m~1" >stdin &&
761 test_must_fail git update-ref --stdin <stdin 2>err &&
762 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
763 test_must_fail git rev-parse --verify -q $c
764 '
765
766 test_expect_success 'stdin update ref fails with bad old value' '
767 echo "update $c $m does-not-exist" >stdin &&
768 test_must_fail git update-ref --stdin <stdin 2>err &&
769 grep "fatal: update $c: invalid <old-oid>: does-not-exist" err &&
770 test_must_fail git rev-parse --verify -q $c
771 '
772
773 test_expect_success 'stdin create ref fails with bad new value' '
774 echo "create $c does-not-exist" >stdin &&
775 test_must_fail git update-ref --stdin <stdin 2>err &&
776 grep "fatal: create $c: invalid <new-oid>: does-not-exist" err &&
777 test_must_fail git rev-parse --verify -q $c
778 '
779
780 test_expect_success 'stdin create ref fails with zero new value' '
781 echo "create $c " >stdin &&
782 test_must_fail git update-ref --stdin <stdin 2>err &&
783 grep "fatal: create $c: zero <new-oid>" err &&
784 test_must_fail git rev-parse --verify -q $c
785 '
786
787 test_expect_success 'stdin update ref works with right old value' '
788 echo "update $b $m~1 $m" >stdin &&
789 git update-ref --stdin <stdin &&
790 git rev-parse $m~1 >expect &&
791 git rev-parse $b >actual &&
792 test_cmp expect actual
793 '
794
795 test_expect_success 'stdin delete ref fails with wrong old value' '
796 echo "delete $a $m~1" >stdin &&
797 test_must_fail git update-ref --stdin <stdin 2>err &&
798 grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
799 git rev-parse $m >expect &&
800 git rev-parse $a >actual &&
801 test_cmp expect actual
802 '
803
804 test_expect_success 'stdin delete ref fails with zero old value' '
805 echo "delete $a " >stdin &&
806 test_must_fail git update-ref --stdin <stdin 2>err &&
807 grep "fatal: delete $a: zero <old-oid>" err &&
808 git rev-parse $m >expect &&
809 git rev-parse $a >actual &&
810 test_cmp expect actual
811 '
812
813 test_expect_success 'stdin update symref works option no-deref' '
814 git symbolic-ref TESTSYMREF $b &&
815 cat >stdin <<-EOF &&
816 option no-deref
817 update TESTSYMREF $a $b
818 EOF
819 git update-ref --stdin <stdin &&
820 git rev-parse TESTSYMREF >expect &&
821 git rev-parse $a >actual &&
822 test_cmp expect actual &&
823 git rev-parse $m~1 >expect &&
824 git rev-parse $b >actual &&
825 test_cmp expect actual
826 '
827
828 test_expect_success 'stdin delete symref works option no-deref' '
829 git symbolic-ref TESTSYMREF $b &&
830 cat >stdin <<-EOF &&
831 option no-deref
832 delete TESTSYMREF $b
833 EOF
834 git update-ref --stdin <stdin &&
835 test_must_fail git rev-parse --verify -q TESTSYMREF &&
836 git rev-parse $m~1 >expect &&
837 git rev-parse $b >actual &&
838 test_cmp expect actual
839 '
840
841 test_expect_success 'stdin update symref works flag --no-deref' '
842 git symbolic-ref TESTSYMREFONE $b &&
843 git symbolic-ref TESTSYMREFTWO $b &&
844 cat >stdin <<-EOF &&
845 update TESTSYMREFONE $a $b
846 update TESTSYMREFTWO $a $b
847 EOF
848 git update-ref --no-deref --stdin <stdin &&
849 git rev-parse TESTSYMREFONE TESTSYMREFTWO >expect &&
850 git rev-parse $a $a >actual &&
851 test_cmp expect actual &&
852 git rev-parse $m~1 >expect &&
853 git rev-parse $b >actual &&
854 test_cmp expect actual
855 '
856
857 test_expect_success 'stdin delete symref works flag --no-deref' '
858 git symbolic-ref TESTSYMREFONE $b &&
859 git symbolic-ref TESTSYMREFTWO $b &&
860 cat >stdin <<-EOF &&
861 delete TESTSYMREFONE $b
862 delete TESTSYMREFTWO $b
863 EOF
864 git update-ref --no-deref --stdin <stdin &&
865 test_must_fail git rev-parse --verify -q TESTSYMREFONE &&
866 test_must_fail git rev-parse --verify -q TESTSYMREFTWO &&
867 git rev-parse $m~1 >expect &&
868 git rev-parse $b >actual &&
869 test_cmp expect actual
870 '
871
872 test_expect_success 'stdin delete ref works with right old value' '
873 echo "delete $b $m~1" >stdin &&
874 git update-ref --stdin <stdin &&
875 test_must_fail git rev-parse --verify -q $b
876 '
877
878 test_expect_success 'stdin update/create/verify combination works' '
879 cat >stdin <<-EOF &&
880 update $a $m
881 create $b $m
882 verify $c
883 EOF
884 git update-ref --stdin <stdin &&
885 git rev-parse $m >expect &&
886 git rev-parse $a >actual &&
887 test_cmp expect actual &&
888 git rev-parse $b >actual &&
889 test_cmp expect actual &&
890 test_must_fail git rev-parse --verify -q $c
891 '
892
893 test_expect_success 'stdin verify succeeds for correct value' '
894 test-tool ref-store main for-each-reflog-ent $m >before &&
895 git rev-parse $m >expect &&
896 echo "verify $m $m" >stdin &&
897 git update-ref --stdin <stdin &&
898 git rev-parse $m >actual &&
899 test_cmp expect actual &&
900 test-tool ref-store main for-each-reflog-ent $m >after &&
901 test_cmp before after
902 '
903
904 test_expect_success 'stdin verify succeeds for missing reference' '
905 test-tool ref-store main for-each-reflog-ent $m >before &&
906 echo "verify refs/heads/missing $Z" >stdin &&
907 git update-ref --stdin <stdin &&
908 test_must_fail git rev-parse --verify -q refs/heads/missing &&
909 test-tool ref-store main for-each-reflog-ent $m >after &&
910 test_cmp before after
911 '
912
913 test_expect_success 'stdin verify treats no value as missing' '
914 echo "verify refs/heads/missing" >stdin &&
915 git update-ref --stdin <stdin &&
916 test_must_fail git rev-parse --verify -q refs/heads/missing
917 '
918
919 test_expect_success 'stdin verify fails for wrong value' '
920 git rev-parse $m >expect &&
921 echo "verify $m $m~1" >stdin &&
922 test_must_fail git update-ref --stdin <stdin &&
923 git rev-parse $m >actual &&
924 test_cmp expect actual
925 '
926
927 test_expect_success 'stdin verify fails for mistaken null value' '
928 git rev-parse $m >expect &&
929 echo "verify $m $Z" >stdin &&
930 test_must_fail git update-ref --stdin <stdin &&
931 git rev-parse $m >actual &&
932 test_cmp expect actual
933 '
934
935 test_expect_success 'stdin verify fails for mistaken empty value' '
936 M=$(git rev-parse $m) &&
937 test_when_finished "git update-ref $m $M" &&
938 git rev-parse $m >expect &&
939 echo "verify $m" >stdin &&
940 test_must_fail git update-ref --stdin <stdin &&
941 git rev-parse $m >actual &&
942 test_cmp expect actual
943 '
944
945 test_expect_success 'stdin update refs works with identity updates' '
946 cat >stdin <<-EOF &&
947 update $a $m $m
948 update $b $m $m
949 update $c $Z $E
950 EOF
951 git update-ref --stdin <stdin &&
952 git rev-parse $m >expect &&
953 git rev-parse $a >actual &&
954 test_cmp expect actual &&
955 git rev-parse $b >actual &&
956 test_cmp expect actual &&
957 test_must_fail git rev-parse --verify -q $c
958 '
959
960 test_expect_success 'stdin update refs fails with wrong old value' '
961 git update-ref $c $m &&
962 cat >stdin <<-EOF &&
963 update $a $m $m
964 update $b $m $m
965 update $c ''
966 EOF
967 test_must_fail git update-ref --stdin <stdin 2>err &&
968 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
969 git rev-parse $m >expect &&
970 git rev-parse $a >actual &&
971 test_cmp expect actual &&
972 git rev-parse $b >actual &&
973 test_cmp expect actual &&
974 git rev-parse $c >actual &&
975 test_cmp expect actual
976 '
977
978 test_expect_success 'stdin delete refs works with packed and loose refs' '
979 git pack-refs --all &&
980 git update-ref $c $m~1 &&
981 cat >stdin <<-EOF &&
982 delete $a $m
983 update $b $Z $m
984 update $c $E $m~1
985 EOF
986 git update-ref --stdin <stdin &&
987 test_must_fail git rev-parse --verify -q $a &&
988 test_must_fail git rev-parse --verify -q $b &&
989 test_must_fail git rev-parse --verify -q $c
990 '
991
992 test_expect_success 'stdin -z works on empty input' '
993 >stdin &&
994 git update-ref -z --stdin <stdin &&
995 git rev-parse --verify -q $m
996 '
997
998 test_expect_success 'stdin -z fails on empty line' '
999 echo "" >stdin &&
1000 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1001 grep "fatal: whitespace before command: " err
1002 '
1003
1004 test_expect_success 'stdin -z fails on empty command' '
1005 printf $F "" >stdin &&
1006 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1007 grep "fatal: empty command in input" err
1008 '
1009
1010 test_expect_success 'stdin -z fails on only whitespace' '
1011 printf $F " " >stdin &&
1012 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1013 grep "fatal: whitespace before command: " err
1014 '
1015
1016 test_expect_success 'stdin -z fails on leading whitespace' '
1017 printf $F " create $a" "$m" >stdin &&
1018 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1019 grep "fatal: whitespace before command: create $a" err
1020 '
1021
1022 test_expect_success 'stdin -z fails on unknown command' '
1023 printf $F "unknown $a" >stdin &&
1024 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1025 grep "fatal: unknown command: unknown $a" err
1026 '
1027
1028 test_expect_success 'stdin -z fails create with no ref' '
1029 printf $F "create " >stdin &&
1030 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1031 grep "fatal: create: missing <ref>" err
1032 '
1033
1034 test_expect_success 'stdin -z fails create with no new value' '
1035 printf $F "create $a" >stdin &&
1036 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1037 grep "fatal: create $a: unexpected end of input when reading <new-oid>" err
1038 '
1039
1040 test_expect_success 'stdin -z fails create with too many arguments' '
1041 printf $F "create $a" "$m" "$m" >stdin &&
1042 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1043 grep "fatal: unknown command: $m" err
1044 '
1045
1046 test_expect_success 'stdin -z fails update with no ref' '
1047 printf $F "update " >stdin &&
1048 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1049 grep "fatal: update: missing <ref>" err
1050 '
1051
1052 test_expect_success 'stdin -z fails update with too few args' '
1053 printf $F "update $a" "$m" >stdin &&
1054 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1055 grep "fatal: update $a: unexpected end of input when reading <old-oid>" err
1056 '
1057
1058 test_expect_success 'stdin -z emits warning with empty new value' '
1059 git update-ref $a $m &&
1060 printf $F "update $a" "" "" >stdin &&
1061 git update-ref -z --stdin <stdin 2>err &&
1062 grep "warning: update $a: missing <new-oid>, treating as zero" err &&
1063 test_must_fail git rev-parse --verify -q $a
1064 '
1065
1066 test_expect_success 'stdin -z fails update with no new value' '
1067 printf $F "update $a" >stdin &&
1068 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1069 grep "fatal: update $a: unexpected end of input when reading <new-oid>" err
1070 '
1071
1072 test_expect_success 'stdin -z fails update with no old value' '
1073 printf $F "update $a" "$m" >stdin &&
1074 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1075 grep "fatal: update $a: unexpected end of input when reading <old-oid>" err
1076 '
1077
1078 test_expect_success 'stdin -z fails update with too many arguments' '
1079 printf $F "update $a" "$m" "$m" "$m" >stdin &&
1080 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1081 grep "fatal: unknown command: $m" err
1082 '
1083
1084 test_expect_success 'stdin -z fails delete with no ref' '
1085 printf $F "delete " >stdin &&
1086 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1087 grep "fatal: delete: missing <ref>" err
1088 '
1089
1090 test_expect_success 'stdin -z fails delete with no old value' '
1091 printf $F "delete $a" >stdin &&
1092 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1093 grep "fatal: delete $a: unexpected end of input when reading <old-oid>" err
1094 '
1095
1096 test_expect_success 'stdin -z fails delete with too many arguments' '
1097 printf $F "delete $a" "$m" "$m" >stdin &&
1098 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1099 grep "fatal: unknown command: $m" err
1100 '
1101
1102 test_expect_success 'stdin -z fails verify with too many arguments' '
1103 printf $F "verify $a" "$m" "$m" >stdin &&
1104 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1105 grep "fatal: unknown command: $m" err
1106 '
1107
1108 test_expect_success 'stdin -z fails verify with no old value' '
1109 printf $F "verify $a" >stdin &&
1110 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1111 grep "fatal: verify $a: unexpected end of input when reading <old-oid>" err
1112 '
1113
1114 test_expect_success 'stdin -z fails option with unknown name' '
1115 printf $F "option unknown" >stdin &&
1116 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1117 grep "fatal: option unknown: unknown" err
1118 '
1119
1120 test_expect_success 'stdin -z fails with duplicate refs' '
1121 printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
1122 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1123 test_grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
1124 '
1125
1126 test_expect_success 'stdin -z create ref works' '
1127 printf $F "create $a" "$m" >stdin &&
1128 git update-ref -z --stdin <stdin &&
1129 git rev-parse $m >expect &&
1130 git rev-parse $a >actual &&
1131 test_cmp expect actual
1132 '
1133
1134 test_expect_success 'stdin -z update ref creates with zero old value' '
1135 printf $F "update $b" "$m" "$Z" >stdin &&
1136 git update-ref -z --stdin <stdin &&
1137 git rev-parse $m >expect &&
1138 git rev-parse $b >actual &&
1139 test_cmp expect actual &&
1140 git update-ref -d $b
1141 '
1142
1143 test_expect_success 'stdin -z update ref creates with empty old value' '
1144 printf $F "update $b" "$m" "" >stdin &&
1145 git update-ref -z --stdin <stdin &&
1146 git rev-parse $m >expect &&
1147 git rev-parse $b >actual &&
1148 test_cmp expect actual
1149 '
1150
1151 test_expect_success 'stdin -z create ref works with path with space to blob' '
1152 printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
1153 git update-ref -z --stdin <stdin &&
1154 git rev-parse "$m:$pws" >expect &&
1155 git rev-parse refs/blobs/pws >actual &&
1156 test_cmp expect actual &&
1157 git update-ref -d refs/blobs/pws
1158 '
1159
1160 test_expect_success 'stdin -z update ref fails with wrong old value' '
1161 printf $F "update $c" "$m" "$m~1" >stdin &&
1162 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1163 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1164 test_must_fail git rev-parse --verify -q $c
1165 '
1166
1167 test_expect_success 'stdin -z update ref fails with bad old value' '
1168 printf $F "update $c" "$m" "does-not-exist" >stdin &&
1169 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1170 grep "fatal: update $c: invalid <old-oid>: does-not-exist" err &&
1171 test_must_fail git rev-parse --verify -q $c
1172 '
1173
1174 test_expect_success 'stdin -z create ref fails when ref exists' '
1175 git update-ref $c $m &&
1176 git rev-parse "$c" >expect &&
1177 printf $F "create $c" "$m~1" >stdin &&
1178 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1179 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1180 git rev-parse "$c" >actual &&
1181 test_cmp expect actual
1182 '
1183
1184 test_expect_success 'stdin -z create ref fails with bad new value' '
1185 git update-ref -d "$c" &&
1186 printf $F "create $c" "does-not-exist" >stdin &&
1187 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1188 grep "fatal: create $c: invalid <new-oid>: does-not-exist" err &&
1189 test_must_fail git rev-parse --verify -q $c
1190 '
1191
1192 test_expect_success 'stdin -z create ref fails with empty new value' '
1193 printf $F "create $c" "" >stdin &&
1194 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1195 grep "fatal: create $c: missing <new-oid>" err &&
1196 test_must_fail git rev-parse --verify -q $c
1197 '
1198
1199 test_expect_success 'stdin -z create ref fails with non commit object' '
1200 printf $F "create $c" "$(test_oid 001)" >stdin &&
1201 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1202 grep "fatal: trying to write ref ${SQ}$c${SQ} with nonexistent object" err &&
1203 test_must_fail git rev-parse --verify -q $c
1204 '
1205
1206 test_expect_success 'stdin -z update ref fails with non commit object' '
1207 printf $F "update $b" "$(test_oid 001)" "" >stdin &&
1208 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1209 grep "fatal: trying to write ref ${SQ}$b${SQ} with nonexistent object" err &&
1210 test_must_fail git rev-parse --verify -q $c
1211 '
1212
1213 test_expect_success 'stdin -z update ref works with right old value' '
1214 printf $F "update $b" "$m~1" "$m" >stdin &&
1215 git update-ref -z --stdin <stdin &&
1216 git rev-parse $m~1 >expect &&
1217 git rev-parse $b >actual &&
1218 test_cmp expect actual
1219 '
1220
1221 test_expect_success 'stdin -z delete ref fails with wrong old value' '
1222 printf $F "delete $a" "$m~1" >stdin &&
1223 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1224 grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
1225 git rev-parse $m >expect &&
1226 git rev-parse $a >actual &&
1227 test_cmp expect actual
1228 '
1229
1230 test_expect_success 'stdin -z delete ref fails with zero old value' '
1231 printf $F "delete $a" "$Z" >stdin &&
1232 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1233 grep "fatal: delete $a: zero <old-oid>" err &&
1234 git rev-parse $m >expect &&
1235 git rev-parse $a >actual &&
1236 test_cmp expect actual
1237 '
1238
1239 test_expect_success 'stdin -z update symref works option no-deref' '
1240 git symbolic-ref TESTSYMREF $b &&
1241 printf $F "option no-deref" "update TESTSYMREF" "$a" "$b" >stdin &&
1242 git update-ref -z --stdin <stdin &&
1243 git rev-parse TESTSYMREF >expect &&
1244 git rev-parse $a >actual &&
1245 test_cmp expect actual &&
1246 git rev-parse $m~1 >expect &&
1247 git rev-parse $b >actual &&
1248 test_cmp expect actual
1249 '
1250
1251 test_expect_success 'stdin -z delete symref works option no-deref' '
1252 git symbolic-ref TESTSYMREF $b &&
1253 printf $F "option no-deref" "delete TESTSYMREF" "$b" >stdin &&
1254 git update-ref -z --stdin <stdin &&
1255 test_must_fail git rev-parse --verify -q TESTSYMREF &&
1256 git rev-parse $m~1 >expect &&
1257 git rev-parse $b >actual &&
1258 test_cmp expect actual
1259 '
1260
1261 test_expect_success 'stdin -z delete ref works with right old value' '
1262 printf $F "delete $b" "$m~1" >stdin &&
1263 git update-ref -z --stdin <stdin &&
1264 test_must_fail git rev-parse --verify -q $b
1265 '
1266
1267 test_expect_success 'stdin -z update/create/verify combination works' '
1268 printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin &&
1269 git update-ref -z --stdin <stdin &&
1270 git rev-parse $m >expect &&
1271 git rev-parse $a >actual &&
1272 test_cmp expect actual &&
1273 git rev-parse $b >actual &&
1274 test_cmp expect actual &&
1275 test_must_fail git rev-parse --verify -q $c
1276 '
1277
1278 test_expect_success 'stdin -z verify succeeds for correct value' '
1279 git rev-parse $m >expect &&
1280 printf $F "verify $m" "$m" >stdin &&
1281 git update-ref -z --stdin <stdin &&
1282 git rev-parse $m >actual &&
1283 test_cmp expect actual
1284 '
1285
1286 test_expect_success 'stdin -z verify succeeds for missing reference' '
1287 printf $F "verify refs/heads/missing" "$Z" >stdin &&
1288 git update-ref -z --stdin <stdin &&
1289 test_must_fail git rev-parse --verify -q refs/heads/missing
1290 '
1291
1292 test_expect_success 'stdin -z verify treats no value as missing' '
1293 printf $F "verify refs/heads/missing" "" >stdin &&
1294 git update-ref -z --stdin <stdin &&
1295 test_must_fail git rev-parse --verify -q refs/heads/missing
1296 '
1297
1298 test_expect_success 'stdin -z verify fails for wrong value' '
1299 git rev-parse $m >expect &&
1300 printf $F "verify $m" "$m~1" >stdin &&
1301 test_must_fail git update-ref -z --stdin <stdin &&
1302 git rev-parse $m >actual &&
1303 test_cmp expect actual
1304 '
1305
1306 test_expect_success 'stdin -z verify fails for mistaken null value' '
1307 git rev-parse $m >expect &&
1308 printf $F "verify $m" "$Z" >stdin &&
1309 test_must_fail git update-ref -z --stdin <stdin &&
1310 git rev-parse $m >actual &&
1311 test_cmp expect actual
1312 '
1313
1314 test_expect_success 'stdin -z verify fails for mistaken empty value' '
1315 M=$(git rev-parse $m) &&
1316 test_when_finished "git update-ref $m $M" &&
1317 git rev-parse $m >expect &&
1318 printf $F "verify $m" "" >stdin &&
1319 test_must_fail git update-ref -z --stdin <stdin &&
1320 git rev-parse $m >actual &&
1321 test_cmp expect actual
1322 '
1323
1324 test_expect_success 'stdin -z update refs works with identity updates' '
1325 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
1326 git update-ref -z --stdin <stdin &&
1327 git rev-parse $m >expect &&
1328 git rev-parse $a >actual &&
1329 test_cmp expect actual &&
1330 git rev-parse $b >actual &&
1331 test_cmp expect actual &&
1332 test_must_fail git rev-parse --verify -q $c
1333 '
1334
1335 test_expect_success 'stdin -z update refs fails with wrong old value' '
1336 git update-ref $c $m &&
1337 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
1338 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1339 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1340 git rev-parse $m >expect &&
1341 git rev-parse $a >actual &&
1342 test_cmp expect actual &&
1343 git rev-parse $b >actual &&
1344 test_cmp expect actual &&
1345 git rev-parse $c >actual &&
1346 test_cmp expect actual
1347 '
1348
1349 test_expect_success 'stdin -z delete refs works with packed and loose refs' '
1350 git pack-refs --all &&
1351 git update-ref $c $m~1 &&
1352 printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
1353 git update-ref -z --stdin <stdin &&
1354 test_must_fail git rev-parse --verify -q $a &&
1355 test_must_fail git rev-parse --verify -q $b &&
1356 test_must_fail git rev-parse --verify -q $c
1357 '
1358
1359 test_expect_success 'fails with duplicate HEAD update' '
1360 git branch target1 $A &&
1361 git checkout target1 &&
1362 cat >stdin <<-EOF &&
1363 update refs/heads/target1 $C
1364 option no-deref
1365 update HEAD $B
1366 EOF
1367 test_must_fail git update-ref --stdin <stdin 2>err &&
1368 test_grep "fatal: multiple updates for '\''HEAD'\'' (including one via its referent .refs/heads/target1.) are not allowed" err &&
1369 echo "refs/heads/target1" >expect &&
1370 git symbolic-ref HEAD >actual &&
1371 test_cmp expect actual &&
1372 echo "$A" >expect &&
1373 git rev-parse refs/heads/target1 >actual &&
1374 test_cmp expect actual
1375 '
1376
1377 test_expect_success 'fails with duplicate ref update via symref' '
1378 test_when_finished "git symbolic-ref -d refs/heads/symref2" &&
1379 git branch target2 $A &&
1380 git symbolic-ref refs/heads/symref2 refs/heads/target2 &&
1381 cat >stdin <<-EOF &&
1382 update refs/heads/target2 $C
1383 update refs/heads/symref2 $B
1384 EOF
1385 test_must_fail git update-ref --stdin <stdin 2>err &&
1386 test_grep "fatal: multiple updates for '\''refs/heads/target2'\'' (including one via symref .refs/heads/symref2.) are not allowed" err &&
1387 echo "refs/heads/target2" >expect &&
1388 git symbolic-ref refs/heads/symref2 >actual &&
1389 test_cmp expect actual &&
1390 echo "$A" >expect &&
1391 git rev-parse refs/heads/target2 >actual &&
1392 test_cmp expect actual
1393 '
1394
1395 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
1396 (
1397 test_seq -f "create refs/heads/%d HEAD" 33 |
1398 run_with_limited_open_files git update-ref --stdin &&
1399 git rev-parse --verify -q refs/heads/33
1400 )
1401 '
1402
1403 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
1404 (
1405 test_seq -f "delete refs/heads/%d HEAD" 33 |
1406 run_with_limited_open_files git update-ref --stdin &&
1407 test_must_fail git rev-parse --verify -q refs/heads/33
1408 )
1409 '
1410
1411 test_expect_success 'handle per-worktree refs in refs/bisect' '
1412 git commit --allow-empty -m "initial commit" &&
1413 git worktree add -b branch worktree &&
1414 (
1415 cd worktree &&
1416 git commit --allow-empty -m "test commit" &&
1417 git for-each-ref >for-each-ref.out &&
1418 ! grep refs/bisect for-each-ref.out &&
1419 git update-ref refs/bisect/something HEAD &&
1420 git rev-parse refs/bisect/something >../worktree-head &&
1421 git for-each-ref | grep refs/bisect/something
1422 ) &&
1423 git show-ref >actual &&
1424 ! grep 'refs/bisect' actual &&
1425 test_must_fail git rev-parse refs/bisect/something &&
1426 git update-ref refs/bisect/something HEAD &&
1427 git rev-parse refs/bisect/something >main-head &&
1428 ! test_cmp main-head worktree-head
1429 '
1430
1431 test_expect_success 'transaction handles empty commit' '
1432 cat >stdin <<-EOF &&
1433 start
1434 prepare
1435 commit
1436 EOF
1437 git update-ref --stdin <stdin >actual &&
1438 printf "%s: ok\n" start prepare commit >expect &&
1439 test_cmp expect actual
1440 '
1441
1442 test_expect_success 'transaction handles empty commit with missing prepare' '
1443 cat >stdin <<-EOF &&
1444 start
1445 commit
1446 EOF
1447 git update-ref --stdin <stdin >actual &&
1448 printf "%s: ok\n" start commit >expect &&
1449 test_cmp expect actual
1450 '
1451
1452 test_expect_success 'transaction handles sole commit' '
1453 cat >stdin <<-EOF &&
1454 commit
1455 EOF
1456 git update-ref --stdin <stdin >actual &&
1457 printf "%s: ok\n" commit >expect &&
1458 test_cmp expect actual
1459 '
1460
1461 test_expect_success 'transaction handles empty abort' '
1462 cat >stdin <<-EOF &&
1463 start
1464 prepare
1465 abort
1466 EOF
1467 git update-ref --stdin <stdin >actual &&
1468 printf "%s: ok\n" start prepare abort >expect &&
1469 test_cmp expect actual
1470 '
1471
1472 test_expect_success 'transaction exits on multiple aborts' '
1473 cat >stdin <<-EOF &&
1474 abort
1475 abort
1476 EOF
1477 test_must_fail git update-ref --stdin <stdin >actual 2>err &&
1478 printf "%s: ok\n" abort >expect &&
1479 test_cmp expect actual &&
1480 grep "fatal: transaction is closed" err
1481 '
1482
1483 test_expect_success 'transaction exits on start after prepare' '
1484 cat >stdin <<-EOF &&
1485 prepare
1486 start
1487 EOF
1488 test_must_fail git update-ref --stdin <stdin 2>err >actual &&
1489 printf "%s: ok\n" prepare >expect &&
1490 test_cmp expect actual &&
1491 grep "fatal: prepared transactions can only be closed" err
1492 '
1493
1494 test_expect_success 'transaction handles empty abort with missing prepare' '
1495 cat >stdin <<-EOF &&
1496 start
1497 abort
1498 EOF
1499 git update-ref --stdin <stdin >actual &&
1500 printf "%s: ok\n" start abort >expect &&
1501 test_cmp expect actual
1502 '
1503
1504 test_expect_success 'transaction handles sole abort' '
1505 cat >stdin <<-EOF &&
1506 abort
1507 EOF
1508 git update-ref --stdin <stdin >actual &&
1509 printf "%s: ok\n" abort >expect &&
1510 test_cmp expect actual
1511 '
1512
1513 test_expect_success 'transaction can handle commit' '
1514 cat >stdin <<-EOF &&
1515 start
1516 create $a HEAD
1517 commit
1518 EOF
1519 git update-ref --stdin <stdin >actual &&
1520 printf "%s: ok\n" start commit >expect &&
1521 test_cmp expect actual &&
1522 git rev-parse HEAD >expect &&
1523 git rev-parse $a >actual &&
1524 test_cmp expect actual
1525 '
1526
1527 test_expect_success 'transaction can handle abort' '
1528 cat >stdin <<-EOF &&
1529 start
1530 create $b HEAD
1531 abort
1532 EOF
1533 git update-ref --stdin <stdin >actual &&
1534 printf "%s: ok\n" start abort >expect &&
1535 test_cmp expect actual &&
1536 test_must_fail git show-ref --verify -q $b
1537 '
1538
1539 test_expect_success 'transaction aborts by default' '
1540 cat >stdin <<-EOF &&
1541 start
1542 create $b HEAD
1543 EOF
1544 git update-ref --stdin <stdin >actual &&
1545 printf "%s: ok\n" start >expect &&
1546 test_cmp expect actual &&
1547 test_must_fail git show-ref --verify -q $b
1548 '
1549
1550 test_expect_success 'transaction with prepare aborts by default' '
1551 cat >stdin <<-EOF &&
1552 start
1553 create $b HEAD
1554 prepare
1555 EOF
1556 git update-ref --stdin <stdin >actual &&
1557 printf "%s: ok\n" start prepare >expect &&
1558 test_cmp expect actual &&
1559 test_must_fail git show-ref --verify -q $b
1560 '
1561
1562 test_expect_success 'transaction can commit multiple times' '
1563 cat >stdin <<-EOF &&
1564 start
1565 create refs/heads/branch-1 $A
1566 commit
1567 start
1568 create refs/heads/branch-2 $B
1569 commit
1570 EOF
1571 git update-ref --stdin <stdin >actual &&
1572 printf "%s: ok\n" start commit start commit >expect &&
1573 test_cmp expect actual &&
1574 echo "$A" >expect &&
1575 git rev-parse refs/heads/branch-1 >actual &&
1576 test_cmp expect actual &&
1577 echo "$B" >expect &&
1578 git rev-parse refs/heads/branch-2 >actual &&
1579 test_cmp expect actual
1580 '
1581
1582 test_expect_success 'transaction can create and delete' '
1583 cat >stdin <<-EOF &&
1584 start
1585 create refs/heads/create-and-delete $A
1586 commit
1587 start
1588 delete refs/heads/create-and-delete $A
1589 commit
1590 EOF
1591 git update-ref --stdin <stdin >actual &&
1592 printf "%s: ok\n" start commit start commit >expect &&
1593 test_cmp expect actual &&
1594 test_must_fail git show-ref --verify refs/heads/create-and-delete
1595 '
1596
1597 test_expect_success 'transaction can commit after abort' '
1598 cat >stdin <<-EOF &&
1599 start
1600 create refs/heads/abort $A
1601 abort
1602 start
1603 create refs/heads/abort $A
1604 commit
1605 EOF
1606 git update-ref --stdin <stdin >actual &&
1607 printf "%s: ok\n" start abort start commit >expect &&
1608 echo "$A" >expect &&
1609 git rev-parse refs/heads/abort >actual &&
1610 test_cmp expect actual
1611 '
1612
1613 test_expect_success 'transaction cannot restart ongoing transaction' '
1614 cat >stdin <<-EOF &&
1615 start
1616 create refs/heads/restart $A
1617 start
1618 commit
1619 EOF
1620 test_must_fail git update-ref --stdin <stdin >actual &&
1621 printf "%s: ok\n" start >expect &&
1622 test_cmp expect actual &&
1623 test_must_fail git show-ref --verify refs/heads/restart
1624 '
1625
1626 test_expect_success PIPE 'transaction flushes status updates' '
1627 test_when_finished "rm -f in out" &&
1628 mkfifo in out &&
1629 (git update-ref --stdin <in >out &) &&
1630
1631 exec 9>in &&
1632 exec 8<out &&
1633 test_when_finished "exec 9>&-" &&
1634 test_when_finished "exec 8<&-" &&
1635
1636 echo "start" >&9 &&
1637 echo "start: ok" >expected &&
1638 read line <&8 &&
1639 echo "$line" >actual &&
1640 test_cmp expected actual &&
1641
1642 echo "create refs/heads/flush $A" >&9 &&
1643
1644 echo prepare >&9 &&
1645 echo "prepare: ok" >expected &&
1646 read line <&8 &&
1647 echo "$line" >actual &&
1648 test_cmp expected actual &&
1649
1650 # This must now fail given that we have locked the ref.
1651 test_must_fail git update-ref refs/heads/flush $B 2>stderr &&
1652 grep "fatal: update_ref failed for ref ${SQ}refs/heads/flush${SQ}: cannot lock ref" stderr &&
1653
1654 echo commit >&9 &&
1655 echo "commit: ok" >expected &&
1656 read line <&8 &&
1657 echo "$line" >actual &&
1658 test_cmp expected actual
1659 '
1660
1661 format_command () {
1662 if test "$1" = "-z"
1663 then
1664 shift
1665 printf "$F" "$@"
1666 else
1667 echo "$@"
1668 fi
1669 }
1670
1671 for type in "" "-z"
1672 do
1673
1674 test_expect_success "stdin $type symref-verify fails without --no-deref" '
1675 git symbolic-ref refs/heads/symref $a &&
1676 format_command $type "symref-verify refs/heads/symref" "$a" >stdin &&
1677 test_must_fail git update-ref --stdin $type <stdin 2>err &&
1678 grep "fatal: symref-verify: cannot operate with deref mode" err
1679 '
1680
1681 test_expect_success "stdin $type symref-verify fails with too many arguments" '
1682 format_command $type "symref-verify refs/heads/symref" "$a" "$a" >stdin &&
1683 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1684 if test "$type" = "-z"
1685 then
1686 grep "fatal: unknown command: $a" err
1687 else
1688 grep "fatal: symref-verify refs/heads/symref: extra input: $a" err
1689 fi
1690 '
1691
1692 test_expect_success "stdin $type symref-verify succeeds for correct value" '
1693 git symbolic-ref refs/heads/symref >expect &&
1694 test-tool ref-store main for-each-reflog-ent refs/heads/symref >before &&
1695 format_command $type "symref-verify refs/heads/symref" "$a" >stdin &&
1696 git update-ref --stdin $type --no-deref <stdin &&
1697 git symbolic-ref refs/heads/symref >actual &&
1698 test_cmp expect actual &&
1699 test-tool ref-store main for-each-reflog-ent refs/heads/symref >after &&
1700 test_cmp before after
1701 '
1702
1703 test_expect_success "stdin $type symref-verify fails with no value" '
1704 git symbolic-ref refs/heads/symref >expect &&
1705 format_command $type "symref-verify refs/heads/symref" "" >stdin &&
1706 test_must_fail git update-ref --stdin $type --no-deref <stdin
1707 '
1708
1709 test_expect_success "stdin $type symref-verify succeeds for dangling reference" '
1710 test_when_finished "git symbolic-ref -d refs/heads/symref2" &&
1711 test_must_fail git symbolic-ref refs/heads/nonexistent &&
1712 git symbolic-ref refs/heads/symref2 refs/heads/nonexistent &&
1713 format_command $type "symref-verify refs/heads/symref2" "refs/heads/nonexistent" >stdin &&
1714 git update-ref --stdin $type --no-deref <stdin
1715 '
1716
1717 test_expect_success "stdin $type symref-verify fails for missing reference" '
1718 test-tool ref-store main for-each-reflog-ent refs/heads/symref >before &&
1719 format_command $type "symref-verify refs/heads/missing" "refs/heads/unknown" >stdin &&
1720 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1721 grep "fatal: cannot lock ref ${SQ}refs/heads/missing${SQ}: unable to resolve reference ${SQ}refs/heads/missing${SQ}" err &&
1722 test_must_fail git rev-parse --verify -q refs/heads/missing &&
1723 test-tool ref-store main for-each-reflog-ent refs/heads/symref >after &&
1724 test_cmp before after
1725 '
1726
1727 test_expect_success "stdin $type symref-verify fails for wrong value" '
1728 git symbolic-ref refs/heads/symref >expect &&
1729 format_command $type "symref-verify refs/heads/symref" "$b" >stdin &&
1730 test_must_fail git update-ref --stdin $type --no-deref <stdin &&
1731 git symbolic-ref refs/heads/symref >actual &&
1732 test_cmp expect actual
1733 '
1734
1735 test_expect_success "stdin $type symref-verify fails for mistaken null value" '
1736 git symbolic-ref refs/heads/symref >expect &&
1737 format_command $type "symref-verify refs/heads/symref" "$Z" >stdin &&
1738 test_must_fail git update-ref --stdin $type --no-deref <stdin &&
1739 git symbolic-ref refs/heads/symref >actual &&
1740 test_cmp expect actual
1741 '
1742
1743 test_expect_success "stdin $type symref-delete fails without --no-deref" '
1744 git symbolic-ref refs/heads/symref $a &&
1745 format_command $type "symref-delete refs/heads/symref" "$a" >stdin &&
1746 test_must_fail git update-ref --stdin $type <stdin 2>err &&
1747 grep "fatal: symref-delete: cannot operate with deref mode" err
1748 '
1749
1750 test_expect_success "stdin $type symref-delete fails with no ref" '
1751 format_command $type "symref-delete " >stdin &&
1752 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1753 grep "fatal: symref-delete: missing <ref>" err
1754 '
1755
1756 test_expect_success "stdin $type symref-delete fails deleting regular ref" '
1757 test_when_finished "git update-ref -d refs/heads/regularref" &&
1758 git update-ref refs/heads/regularref $a &&
1759 format_command $type "symref-delete refs/heads/regularref" "$a" >stdin &&
1760 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1761 grep "fatal: cannot lock ref ${SQ}refs/heads/regularref${SQ}: expected symref with target ${SQ}$a${SQ}: but is a regular ref" err
1762 '
1763
1764 test_expect_success "stdin $type symref-delete fails with too many arguments" '
1765 format_command $type "symref-delete refs/heads/symref" "$a" "$a" >stdin &&
1766 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1767 if test "$type" = "-z"
1768 then
1769 grep "fatal: unknown command: $a" err
1770 else
1771 grep "fatal: symref-delete refs/heads/symref: extra input: $a" err
1772 fi
1773 '
1774
1775 test_expect_success "stdin $type symref-delete fails with wrong old value" '
1776 format_command $type "symref-delete refs/heads/symref" "$m" >stdin &&
1777 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1778 grep "fatal: verifying symref target: ${SQ}refs/heads/symref${SQ}: is at $a but expected refs/heads/main" err &&
1779 git symbolic-ref refs/heads/symref >expect &&
1780 echo $a >actual &&
1781 test_cmp expect actual
1782 '
1783
1784 test_expect_success "stdin $type symref-delete works with right old value" '
1785 format_command $type "symref-delete refs/heads/symref" "$a" >stdin &&
1786 git update-ref --stdin $type --no-deref <stdin &&
1787 test_must_fail git rev-parse --verify -q refs/heads/symref
1788 '
1789
1790 test_expect_success "stdin $type symref-delete works with empty old value" '
1791 git symbolic-ref refs/heads/symref $a >stdin &&
1792 format_command $type "symref-delete refs/heads/symref" "" >stdin &&
1793 git update-ref --stdin $type --no-deref <stdin &&
1794 test_must_fail git rev-parse --verify -q $b
1795 '
1796
1797 test_expect_success "stdin $type symref-delete succeeds for dangling reference" '
1798 test_must_fail git symbolic-ref refs/heads/nonexistent &&
1799 git symbolic-ref refs/heads/symref2 refs/heads/nonexistent &&
1800 format_command $type "symref-delete refs/heads/symref2" "refs/heads/nonexistent" >stdin &&
1801 git update-ref --stdin $type --no-deref <stdin &&
1802 test_must_fail git symbolic-ref -d refs/heads/symref2
1803 '
1804
1805 test_expect_success "stdin $type symref-delete deletes regular ref without target" '
1806 git update-ref refs/heads/regularref $a &&
1807 format_command $type "symref-delete refs/heads/regularref" >stdin &&
1808 git update-ref --stdin $type --no-deref <stdin
1809 '
1810
1811 test_expect_success "stdin $type symref-create fails with too many arguments" '
1812 format_command $type "symref-create refs/heads/symref" "$a" "$a" >stdin &&
1813 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1814 if test "$type" = "-z"
1815 then
1816 grep "fatal: unknown command: $a" err
1817 else
1818 grep "fatal: symref-create refs/heads/symref: extra input: $a" err
1819 fi
1820 '
1821
1822 test_expect_success "stdin $type symref-create fails with no target" '
1823 format_command $type "symref-create refs/heads/symref" >stdin &&
1824 test_must_fail git update-ref --stdin $type --no-deref <stdin
1825 '
1826
1827 test_expect_success "stdin $type symref-create fails with empty target" '
1828 format_command $type "symref-create refs/heads/symref" "" >stdin &&
1829 test_must_fail git update-ref --stdin $type --no-deref <stdin
1830 '
1831
1832 test_expect_success "stdin $type symref-create works" '
1833 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1834 format_command $type "symref-create refs/heads/symref" "$a" >stdin &&
1835 git update-ref --stdin $type --no-deref <stdin &&
1836 git symbolic-ref refs/heads/symref >expect &&
1837 echo $a >actual &&
1838 test_cmp expect actual
1839 '
1840
1841 test_expect_success "stdin $type symref-create works with --no-deref" '
1842 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1843 format_command $type "symref-create refs/heads/symref" "$a" &&
1844 git update-ref --stdin $type <stdin 2>err
1845 '
1846
1847 test_expect_success "stdin $type create dangling symref ref works" '
1848 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1849 format_command $type "symref-create refs/heads/symref" "refs/heads/unknown" >stdin &&
1850 git update-ref --stdin $type --no-deref <stdin &&
1851 git symbolic-ref refs/heads/symref >expect &&
1852 echo refs/heads/unknown >actual &&
1853 test_cmp expect actual
1854 '
1855
1856 test_expect_success "stdin $type symref-create does not create reflogs by default" '
1857 test_when_finished "git symbolic-ref -d refs/symref" &&
1858 format_command $type "symref-create refs/symref" "$a" >stdin &&
1859 git update-ref --stdin $type --no-deref <stdin &&
1860 git symbolic-ref refs/symref >expect &&
1861 echo $a >actual &&
1862 test_cmp expect actual &&
1863 test_must_fail git reflog exists refs/symref
1864 '
1865
1866 test_expect_success "stdin $type symref-create reflogs with --create-reflog" '
1867 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1868 format_command $type "symref-create refs/heads/symref" "$a" >stdin &&
1869 git update-ref --create-reflog --stdin $type --no-deref <stdin &&
1870 git symbolic-ref refs/heads/symref >expect &&
1871 echo $a >actual &&
1872 test_cmp expect actual &&
1873 git reflog exists refs/heads/symref
1874 '
1875
1876 test_expect_success "stdin $type symref-update fails with too many arguments" '
1877 format_command $type "symref-update refs/heads/symref" "$a" "ref" "$a" "$a" >stdin &&
1878 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1879 if test "$type" = "-z"
1880 then
1881 grep "fatal: unknown command: $a" err
1882 else
1883 grep "fatal: symref-update refs/heads/symref: extra input: $a" err
1884 fi
1885 '
1886
1887 test_expect_success "stdin $type symref-update fails with wrong old value argument" '
1888 format_command $type "symref-update refs/heads/symref" "$a" "foo" "$a" "$a" >stdin &&
1889 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1890 grep "fatal: symref-update refs/heads/symref: invalid arg ${SQ}foo${SQ} for old value" err
1891 '
1892
1893 test_expect_success "stdin $type symref-update creates with zero old value" '
1894 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1895 format_command $type "symref-update refs/heads/symref" "$a" "oid" "$Z" >stdin &&
1896 git update-ref --stdin $type --no-deref <stdin &&
1897 echo $a >expect &&
1898 git symbolic-ref refs/heads/symref >actual &&
1899 test_cmp expect actual
1900 '
1901
1902 test_expect_success "stdin $type symref-update creates with no old value" '
1903 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1904 format_command $type "symref-update refs/heads/symref" "$a" >stdin &&
1905 git update-ref --stdin $type --no-deref <stdin &&
1906 echo $a >expect &&
1907 git symbolic-ref refs/heads/symref >actual &&
1908 test_cmp expect actual
1909 '
1910
1911 test_expect_success "stdin $type symref-update creates dangling" '
1912 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1913 test_must_fail git rev-parse refs/heads/nonexistent &&
1914 format_command $type "symref-update refs/heads/symref" "refs/heads/nonexistent" >stdin &&
1915 git update-ref --stdin $type --no-deref <stdin &&
1916 echo refs/heads/nonexistent >expect &&
1917 git symbolic-ref refs/heads/symref >actual &&
1918 test_cmp expect actual
1919 '
1920
1921 test_expect_success "stdin $type symref-update fails with wrong old value" '
1922 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1923 git symbolic-ref refs/heads/symref $a &&
1924 format_command $type "symref-update refs/heads/symref" "$m" "ref" "$b" >stdin &&
1925 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1926 grep "fatal: verifying symref target: ${SQ}refs/heads/symref${SQ}: is at $a but expected $b" err &&
1927 test_must_fail git rev-parse --verify -q $c
1928 '
1929
1930 test_expect_success "stdin $type symref-update updates dangling ref" '
1931 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1932 test_must_fail git rev-parse refs/heads/nonexistent &&
1933 git symbolic-ref refs/heads/symref refs/heads/nonexistent &&
1934 format_command $type "symref-update refs/heads/symref" "$a" >stdin &&
1935 git update-ref --stdin $type --no-deref <stdin &&
1936 echo $a >expect &&
1937 git symbolic-ref refs/heads/symref >actual &&
1938 test_cmp expect actual
1939 '
1940
1941 test_expect_success "stdin $type symref-update updates dangling ref with old value" '
1942 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1943 test_must_fail git rev-parse refs/heads/nonexistent &&
1944 git symbolic-ref refs/heads/symref refs/heads/nonexistent &&
1945 format_command $type "symref-update refs/heads/symref" "$a" "ref" "refs/heads/nonexistent" >stdin &&
1946 git update-ref --stdin $type --no-deref <stdin &&
1947 echo $a >expect &&
1948 git symbolic-ref refs/heads/symref >actual &&
1949 test_cmp expect actual
1950 '
1951
1952 test_expect_success "stdin $type symref-update fails update dangling ref with wrong old value" '
1953 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1954 test_must_fail git rev-parse refs/heads/nonexistent &&
1955 git symbolic-ref refs/heads/symref refs/heads/nonexistent &&
1956 format_command $type "symref-update refs/heads/symref" "$a" "ref" "refs/heads/wrongref" >stdin &&
1957 test_must_fail git update-ref --stdin $type --no-deref <stdin &&
1958 echo refs/heads/nonexistent >expect &&
1959 git symbolic-ref refs/heads/symref >actual &&
1960 test_cmp expect actual
1961 '
1962
1963 test_expect_success "stdin $type symref-update works with right old value" '
1964 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1965 git symbolic-ref refs/heads/symref $a &&
1966 format_command $type "symref-update refs/heads/symref" "$m" "ref" "$a" >stdin &&
1967 git update-ref --stdin $type --no-deref <stdin &&
1968 echo $m >expect &&
1969 git symbolic-ref refs/heads/symref >actual &&
1970 test_cmp expect actual
1971 '
1972
1973 test_expect_success "stdin $type symref-update works with no old value" '
1974 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1975 git symbolic-ref refs/heads/symref $a &&
1976 format_command $type "symref-update refs/heads/symref" "$m" >stdin &&
1977 git update-ref --stdin $type --no-deref <stdin &&
1978 echo $m >expect &&
1979 git symbolic-ref refs/heads/symref >actual &&
1980 test_cmp expect actual
1981 '
1982
1983 test_expect_success "stdin $type symref-update fails with empty old ref-target" '
1984 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1985 git symbolic-ref refs/heads/symref $a &&
1986 format_command $type "symref-update refs/heads/symref" "$m" "ref" "" >stdin &&
1987 test_must_fail git update-ref --stdin $type --no-deref <stdin &&
1988 echo $a >expect &&
1989 git symbolic-ref refs/heads/symref >actual &&
1990 test_cmp expect actual
1991 '
1992
1993 test_expect_success "stdin $type symref-update creates (with deref)" '
1994 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1995 format_command $type "symref-update refs/heads/symref" "$a" >stdin &&
1996 git update-ref --stdin $type <stdin &&
1997 echo $a >expect &&
1998 git symbolic-ref --no-recurse refs/heads/symref >actual &&
1999 test_cmp expect actual &&
2000 test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual &&
2001 grep "$Z $(git rev-parse $a)" actual
2002 '
2003
2004 test_expect_success "stdin $type symref-update regular ref to symref with correct old-oid" '
2005 test_when_finished "git symbolic-ref -d --no-recurse refs/heads/regularref" &&
2006 git update-ref --no-deref refs/heads/regularref $a &&
2007 format_command $type "symref-update refs/heads/regularref" "$a" "oid" "$(git rev-parse $a)" >stdin &&
2008 git update-ref --stdin $type <stdin &&
2009 echo $a >expect &&
2010 git symbolic-ref --no-recurse refs/heads/regularref >actual &&
2011 test_cmp expect actual &&
2012 test-tool ref-store main for-each-reflog-ent refs/heads/regularref >actual &&
2013 grep "$(git rev-parse $a) $(git rev-parse $a)" actual
2014 '
2015
2016 test_expect_success "stdin $type symref-update regular ref to symref fails with wrong old-oid" '
2017 test_when_finished "git update-ref -d refs/heads/regularref" &&
2018 git update-ref --no-deref refs/heads/regularref $a &&
2019 format_command $type "symref-update refs/heads/regularref" "$a" "oid" "$(git rev-parse refs/heads/target2)" >stdin &&
2020 test_must_fail git update-ref --stdin $type <stdin 2>err &&
2021 grep "fatal: cannot lock ref ${SQ}refs/heads/regularref${SQ}: is at $(git rev-parse $a) but expected $(git rev-parse refs/heads/target2)" err &&
2022 echo $(git rev-parse $a) >expect &&
2023 git rev-parse refs/heads/regularref >actual &&
2024 test_cmp expect actual
2025 '
2026
2027 test_expect_success "stdin $type symref-update regular ref to symref fails with invalid old-oid" '
2028 test_when_finished "git update-ref -d refs/heads/regularref" &&
2029 git update-ref --no-deref refs/heads/regularref $a &&
2030 format_command $type "symref-update refs/heads/regularref" "$a" "oid" "not-a-ref-oid" >stdin &&
2031 test_must_fail git update-ref --stdin $type <stdin 2>err &&
2032 grep "fatal: symref-update refs/heads/regularref: invalid oid: not-a-ref-oid" err &&
2033 echo $(git rev-parse $a) >expect &&
2034 git rev-parse refs/heads/regularref >actual &&
2035 test_cmp expect actual
2036 '
2037
2038 test_expect_success "stdin $type symref-update existing symref with zero old-oid" '
2039 test_when_finished "git symbolic-ref -d --no-recurse refs/heads/symref" &&
2040 git symbolic-ref refs/heads/symref refs/heads/target2 &&
2041 format_command $type "symref-update refs/heads/symref" "$a" "oid" "$Z" >stdin &&
2042 test_must_fail git update-ref --stdin $type <stdin 2>err &&
2043 grep "fatal: cannot lock ref ${SQ}refs/heads/symref${SQ}: reference already exists" err &&
2044 echo refs/heads/target2 >expect &&
2045 git symbolic-ref refs/heads/symref >actual &&
2046 test_cmp expect actual
2047 '
2048
2049 test_expect_success "stdin $type symref-update regular ref to symref (with deref)" '
2050 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
2051 test_when_finished "git update-ref -d --no-deref refs/heads/symref2" &&
2052 git update-ref refs/heads/symref2 $a &&
2053 git symbolic-ref --no-recurse refs/heads/symref refs/heads/symref2 &&
2054 format_command $type "symref-update refs/heads/symref" "$a" >stdin &&
2055 git update-ref $type --stdin <stdin &&
2056 echo $a >expect &&
2057 git symbolic-ref --no-recurse refs/heads/symref2 >actual &&
2058 test_cmp expect actual &&
2059 echo refs/heads/symref2 >expect &&
2060 git symbolic-ref --no-recurse refs/heads/symref >actual &&
2061 test_cmp expect actual &&
2062 test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual &&
2063 grep "$(git rev-parse $a) $(git rev-parse $a)" actual
2064 '
2065
2066 test_expect_success "stdin $type symref-update regular ref to symref" '
2067 test_when_finished "git symbolic-ref -d --no-recurse refs/heads/regularref" &&
2068 git update-ref --no-deref refs/heads/regularref $a &&
2069 format_command $type "symref-update refs/heads/regularref" "$a" >stdin &&
2070 git update-ref $type --stdin <stdin &&
2071 echo $a >expect &&
2072 git symbolic-ref --no-recurse refs/heads/regularref >actual &&
2073 test_cmp expect actual &&
2074 test-tool ref-store main for-each-reflog-ent refs/heads/regularref >actual &&
2075 grep "$(git rev-parse $a) $(git rev-parse $a)" actual
2076 '
2077
2078 test_expect_success "stdin $type batch-updates" '
2079 git init repo &&
2080 test_when_finished "rm -fr repo" &&
2081 (
2082 cd repo &&
2083 test_commit commit &&
2084 head=$(git rev-parse HEAD) &&
2085
2086 format_command $type "update refs/heads/ref1" "$head" "$Z" >stdin &&
2087 format_command $type "update refs/heads/ref2" "$head" "$Z" >>stdin &&
2088 git update-ref $type --stdin --batch-updates <stdin &&
2089 echo $head >expect &&
2090 git rev-parse refs/heads/ref1 >actual &&
2091 test_cmp expect actual &&
2092 git rev-parse refs/heads/ref2 >actual &&
2093 test_cmp expect actual
2094 )
2095 '
2096
2097 test_expect_success "stdin $type batch-updates with invalid new_oid" '
2098 git init repo &&
2099 test_when_finished "rm -fr repo" &&
2100 (
2101 cd repo &&
2102 test_commit one &&
2103 old_head=$(git rev-parse HEAD) &&
2104 test_commit two &&
2105 head=$(git rev-parse HEAD) &&
2106 git update-ref refs/heads/ref1 $head &&
2107 git update-ref refs/heads/ref2 $head &&
2108
2109 format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
2110 format_command $type "update refs/heads/ref2" "$(test_oid 001)" "$head" >>stdin &&
2111 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2112 echo $old_head >expect &&
2113 git rev-parse refs/heads/ref1 >actual &&
2114 test_cmp expect actual &&
2115 echo $head >expect &&
2116 git rev-parse refs/heads/ref2 >actual &&
2117 test_cmp expect actual &&
2118 test_grep "rejected refs/heads/ref2 $(test_oid 001) $head invalid new value provided" stdout &&
2119 test_grep "trying to write ref ${SQ}refs/heads/ref2${SQ} with nonexistent object" err
2120 )
2121 '
2122
2123 test_expect_success "stdin $type batch-updates with non-commit new_oid" '
2124 git init repo &&
2125 test_when_finished "rm -fr repo" &&
2126 (
2127 cd repo &&
2128 test_commit one &&
2129 old_head=$(git rev-parse HEAD) &&
2130 test_commit two &&
2131 head=$(git rev-parse HEAD) &&
2132 head_tree=$(git rev-parse HEAD^{tree}) &&
2133 git update-ref refs/heads/ref1 $head &&
2134 git update-ref refs/heads/ref2 $head &&
2135
2136 format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
2137 format_command $type "update refs/heads/ref2" "$head_tree" "$head" >>stdin &&
2138 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2139 echo $old_head >expect &&
2140 git rev-parse refs/heads/ref1 >actual &&
2141 test_cmp expect actual &&
2142 echo $head >expect &&
2143 git rev-parse refs/heads/ref2 >actual &&
2144 test_cmp expect actual &&
2145 test_grep "rejected refs/heads/ref2 $head_tree $head invalid new value provided" stdout &&
2146 test_grep "trying to write non-commit object $head_tree to branch ${SQ}refs/heads/ref2${SQ}" err
2147 )
2148 '
2149
2150 test_expect_success "stdin $type batch-updates with non-existent ref" '
2151 git init repo &&
2152 test_when_finished "rm -fr repo" &&
2153 (
2154 cd repo &&
2155 test_commit one &&
2156 old_head=$(git rev-parse HEAD) &&
2157 test_commit two &&
2158 head=$(git rev-parse HEAD) &&
2159 git update-ref refs/heads/ref1 $head &&
2160
2161 format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
2162 format_command $type "update refs/heads/ref2" "$old_head" "$head" >>stdin &&
2163 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2164 echo $old_head >expect &&
2165 git rev-parse refs/heads/ref1 >actual &&
2166 test_cmp expect actual &&
2167 test_must_fail git rev-parse refs/heads/ref2 &&
2168 test_grep "rejected refs/heads/ref2 $old_head $head reference does not exist" stdout &&
2169 test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: unable to resolve reference ${SQ}refs/heads/ref2${SQ}" err
2170 )
2171 '
2172
2173 test_expect_success "stdin $type batch-updates with dangling symref" '
2174 git init repo &&
2175 test_when_finished "rm -fr repo" &&
2176 (
2177 cd repo &&
2178 test_commit one &&
2179 old_head=$(git rev-parse HEAD) &&
2180 test_commit two &&
2181 head=$(git rev-parse HEAD) &&
2182 git update-ref refs/heads/ref1 $head &&
2183 git symbolic-ref refs/heads/ref2 refs/heads/nonexistent &&
2184
2185 format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
2186 format_command $type "update refs/heads/ref2" "$old_head" "$head" >>stdin &&
2187 git update-ref $type --no-deref --stdin --batch-updates <stdin >stdout 2>err &&
2188 echo $old_head >expect &&
2189 git rev-parse refs/heads/ref1 >actual &&
2190 test_cmp expect actual &&
2191 echo $head >expect &&
2192 test_must_fail git rev-parse refs/heads/ref2 &&
2193 test_grep "rejected refs/heads/ref2 $old_head $head reference does not exist" stdout &&
2194 test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: reference is missing but expected $head" err
2195 )
2196 '
2197
2198 test_expect_success "stdin $type batch-updates with regular ref as symref" '
2199 git init repo &&
2200 test_when_finished "rm -fr repo" &&
2201 (
2202 cd repo &&
2203 test_commit one &&
2204 old_head=$(git rev-parse HEAD) &&
2205 test_commit two &&
2206 head=$(git rev-parse HEAD) &&
2207 git update-ref refs/heads/ref1 $head &&
2208 git update-ref refs/heads/ref2 $head &&
2209
2210 format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
2211 format_command $type "symref-update refs/heads/ref2" "$old_head" "ref" "refs/heads/nonexistent" >>stdin &&
2212 git update-ref $type --no-deref --stdin --batch-updates <stdin >stdout 2>err &&
2213 echo $old_head >expect &&
2214 git rev-parse refs/heads/ref1 >actual &&
2215 test_cmp expect actual &&
2216 echo $head >expect &&
2217 echo $head >expect &&
2218 git rev-parse refs/heads/ref2 >actual &&
2219 test_cmp expect actual &&
2220 test_grep "rejected refs/heads/ref2 $ZERO_OID $ZERO_OID expected symref but found regular ref" stdout &&
2221 test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: expected symref with target ${SQ}refs/heads/nonexistent${SQ}: but is a regular ref" err
2222 )
2223 '
2224
2225 test_expect_success "stdin $type batch-updates with invalid old_oid" '
2226 git init repo &&
2227 test_when_finished "rm -fr repo" &&
2228 (
2229 cd repo &&
2230 test_commit one &&
2231 old_head=$(git rev-parse HEAD) &&
2232 test_commit two &&
2233 head=$(git rev-parse HEAD) &&
2234 git update-ref refs/heads/ref1 $head &&
2235 git update-ref refs/heads/ref2 $head &&
2236
2237 format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
2238 format_command $type "update refs/heads/ref2" "$old_head" "$Z" >>stdin &&
2239 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2240 echo $old_head >expect &&
2241 git rev-parse refs/heads/ref1 >actual &&
2242 test_cmp expect actual &&
2243 echo $head >expect &&
2244 git rev-parse refs/heads/ref2 >actual &&
2245 test_cmp expect actual &&
2246 test_grep "rejected refs/heads/ref2 $old_head $ZERO_OID reference already exists" stdout &&
2247 test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: reference already exists" err
2248 )
2249 '
2250
2251 test_expect_success "stdin $type batch-updates with incorrect old oid" '
2252 git init repo &&
2253 test_when_finished "rm -fr repo" &&
2254 (
2255 cd repo &&
2256 test_commit one &&
2257 old_head=$(git rev-parse HEAD) &&
2258 test_commit two &&
2259 head=$(git rev-parse HEAD) &&
2260 git update-ref refs/heads/ref1 $head &&
2261 git update-ref refs/heads/ref2 $head &&
2262
2263 format_command $type "update refs/heads/ref1" "$old_head" "$head" >stdin &&
2264 format_command $type "update refs/heads/ref2" "$head" "$old_head" >>stdin &&
2265 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2266 echo $old_head >expect &&
2267 git rev-parse refs/heads/ref1 >actual &&
2268 test_cmp expect actual &&
2269 echo $head >expect &&
2270 git rev-parse refs/heads/ref2 >actual &&
2271 test_cmp expect actual &&
2272 test_grep "rejected refs/heads/ref2 $head $old_head incorrect old value provided" stdout &&
2273 test_grep "cannot lock ref ${SQ}refs/heads/ref2${SQ}: is at $head but expected $old_head" err
2274 )
2275 '
2276
2277 test_expect_success "stdin $type batch-updates refname conflict" '
2278 git init repo &&
2279 test_when_finished "rm -fr repo" &&
2280 (
2281 cd repo &&
2282 test_commit one &&
2283 old_head=$(git rev-parse HEAD) &&
2284 test_commit two &&
2285 head=$(git rev-parse HEAD) &&
2286 git update-ref refs/heads/ref/foo $head &&
2287
2288 format_command $type "update refs/heads/ref/foo" "$old_head" "$head" >stdin &&
2289 format_command $type "update refs/heads/ref" "$old_head" "$ZERO_OID" >>stdin &&
2290 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2291 echo $old_head >expect &&
2292 git rev-parse refs/heads/ref/foo >actual &&
2293 test_cmp expect actual &&
2294 test_grep "rejected refs/heads/ref $old_head $ZERO_OID refname conflict" stdout &&
2295 test_grep "${SQ}refs/heads/ref/foo${SQ} exists; cannot create ${SQ}refs/heads/ref${SQ}" err
2296 )
2297 '
2298
2299 test_expect_success "stdin $type batch-updates refname conflict new ref" '
2300 git init repo &&
2301 test_when_finished "rm -fr repo" &&
2302 (
2303 cd repo &&
2304 test_commit one &&
2305 old_head=$(git rev-parse HEAD) &&
2306 test_commit two &&
2307 head=$(git rev-parse HEAD) &&
2308 git update-ref refs/heads/ref/foo $head &&
2309
2310 format_command $type "update refs/heads/foo" "$old_head" "$ZERO_OID" >stdin &&
2311 format_command $type "update refs/heads/ref" "$old_head" "$ZERO_OID" >>stdin &&
2312 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2313 echo $old_head >expect &&
2314 git rev-parse refs/heads/foo >actual &&
2315 test_cmp expect actual &&
2316 test_grep "rejected refs/heads/ref $old_head $ZERO_OID refname conflict" stdout &&
2317 test_grep "${SQ}refs/heads/ref/foo${SQ} exists; cannot create ${SQ}refs/heads/ref${SQ}" err
2318 )
2319 '
2320
2321 test_expect_success CASE_INSENSITIVE_FS,REFFILES "stdin $type batch-updates existing reference" '
2322 git init repo &&
2323 test_when_finished "rm -fr repo" &&
2324 (
2325 cd repo &&
2326 test_commit one &&
2327 old_head=$(git rev-parse HEAD) &&
2328 test_commit two &&
2329 head=$(git rev-parse HEAD) &&
2330
2331 {
2332 format_command $type "create refs/heads/foo" "$head" &&
2333 format_command $type "create refs/heads/ref" "$old_head" &&
2334 format_command $type "create refs/heads/Foo" "$old_head"
2335 } >stdin &&
2336 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2337
2338 echo $head >expect &&
2339 git rev-parse refs/heads/foo >actual &&
2340 echo $old_head >expect &&
2341 git rev-parse refs/heads/ref >actual &&
2342 test_cmp expect actual &&
2343 test_grep "rejected refs/heads/Foo $old_head $ZERO_OID reference conflict due to case-insensitive filesystem" stdout &&
2344 test_grep -e "cannot lock ref ${SQ}refs/heads/Foo${SQ}: Unable to create" -e "Foo.lock" err
2345 )
2346 '
2347
2348 test_expect_success CASE_INSENSITIVE_FS "stdin $type batch-updates existing reference" '
2349 git init --ref-format=reftable repo &&
2350 test_when_finished "rm -fr repo" &&
2351 (
2352 cd repo &&
2353 test_commit one &&
2354 old_head=$(git rev-parse HEAD) &&
2355 test_commit two &&
2356 head=$(git rev-parse HEAD) &&
2357
2358 {
2359 format_command $type "create refs/heads/foo" "$head" &&
2360 format_command $type "create refs/heads/ref" "$old_head" &&
2361 format_command $type "create refs/heads/Foo" "$old_head"
2362 } >stdin &&
2363 git update-ref $type --stdin --batch-updates <stdin >stdout &&
2364
2365 echo $head >expect &&
2366 git rev-parse refs/heads/foo >actual &&
2367 echo $old_head >expect &&
2368 git rev-parse refs/heads/ref >actual &&
2369 test_cmp expect actual &&
2370 git rev-parse refs/heads/Foo >actual &&
2371 test_cmp expect actual
2372 )
2373 '
2374
2375 test_expect_success "stdin $type batch-updates delete incorrect symbolic ref" '
2376 git init repo &&
2377 test_when_finished "rm -fr repo" &&
2378 (
2379 cd repo &&
2380 test_commit c1 &&
2381 head=$(git rev-parse HEAD) &&
2382 git symbolic-ref refs/heads/symbolic refs/heads/non-existent &&
2383
2384 format_command $type "delete refs/heads/symbolic" "$head" >stdin &&
2385 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2386 test_grep "rejected refs/heads/non-existent $ZERO_OID $head reference does not exist" stdout &&
2387 test_grep "cannot lock ref ${SQ}refs/heads/symbolic${SQ}: unable to resolve reference ${SQ}refs/heads/non-existent${SQ}" err
2388 )
2389 '
2390
2391 test_expect_success "stdin $type batch-updates delete with incorrect old_oid" '
2392 git init repo &&
2393 test_when_finished "rm -fr repo" &&
2394 (
2395 cd repo &&
2396 test_commit c1 &&
2397 git branch new-branch &&
2398 test_commit c2 &&
2399 head=$(git rev-parse HEAD) &&
2400
2401 format_command $type "delete refs/heads/new-branch" "$head" >stdin &&
2402 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2403 test_grep "rejected refs/heads/new-branch $ZERO_OID $head incorrect old value provided" stdout &&
2404 test_grep "cannot lock ref ${SQ}refs/heads/new-branch${SQ}: is at $(git rev-parse new-branch) but expected $head" err
2405 )
2406 '
2407
2408 test_expect_success "stdin $type batch-updates delete non-existent ref" '
2409 git init repo &&
2410 test_when_finished "rm -fr repo" &&
2411 (
2412 cd repo &&
2413 test_commit commit &&
2414 head=$(git rev-parse HEAD) &&
2415
2416 format_command $type "delete refs/heads/non-existent" "$head" >stdin &&
2417 git update-ref $type --stdin --batch-updates <stdin >stdout 2>err &&
2418 test_grep "rejected refs/heads/non-existent $ZERO_OID $head reference does not exist" stdout &&
2419 test_grep "cannot lock ref ${SQ}refs/heads/non-existent${SQ}: unable to resolve reference ${SQ}refs/heads/non-existent${SQ}" err
2420 )
2421 '
2422 done
2423
2424 test_expect_success 'update-ref should also create reflog for HEAD' '
2425 test_commit to-rewind &&
2426 git rev-parse HEAD >expect &&
2427 head=$(git symbolic-ref HEAD) &&
2428 git update-ref --create-reflog "$head" HEAD~ &&
2429 git rev-parse HEAD@{1} >actual &&
2430 test_cmp expect actual
2431 '
2432
2433 test_expect_success REFFILES 'empty directories are pruned when aborting a transaction' '
2434 test_path_is_missing .git/refs/heads/nested &&
2435 git update-ref --stdin <<-EOF &&
2436 create refs/heads/nested/something HEAD
2437 prepare
2438 abort
2439 EOF
2440 test_path_is_missing .git/refs/heads/nested
2441 '
2442
2443 test_expect_success REFFILES 'empty directories are pruned when not committing' '
2444 test_path_is_missing .git/refs/heads/nested &&
2445 git update-ref --stdin <<-EOF &&
2446 create refs/heads/nested/something HEAD
2447 prepare
2448 EOF
2449 test_path_is_missing .git/refs/heads/nested
2450 '
2451
2452 test_expect_success 'dangling symref not overwritten by creation' '
2453 test_when_finished "git update-ref -d refs/heads/dangling" &&
2454 git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
2455 test_must_fail git update-ref --no-deref --stdin 2>err <<-\EOF &&
2456 create refs/heads/dangling HEAD
2457 EOF
2458 test_grep "cannot lock.*dangling symref already exists" err &&
2459 test_must_fail git rev-parse --verify refs/heads/dangling &&
2460 test_must_fail git rev-parse --verify refs/heads/does-not-exist
2461 '
2462
2463 test_expect_success 'dangling symref overwritten without old oid' '
2464 test_when_finished "git update-ref -d refs/heads/dangling" &&
2465 git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
2466 git update-ref --no-deref --stdin <<-\EOF &&
2467 update refs/heads/dangling HEAD
2468 EOF
2469 git rev-parse --verify refs/heads/dangling &&
2470 test_must_fail git rev-parse --verify refs/heads/does-not-exist
2471 '
2472
2473 test_done