Merge branch 'ja/doc-synopsis-markup'
[git/gitster.git] / t / t1400-update-ref.sh
blobeb1691860dabadffe60737656ba2f9221cab9322
1 #!/bin/sh
3 # Copyright (c) 2006 Shawn Pearce
6 test_description='Test git update-ref and basic ref logging'
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 Z=$ZERO_OID
13 m=refs/heads/main
14 outside=refs/foo
15 bare=bare-repo
17 create_test_commits ()
19 prfx="$1"
20 for name in A B C D E F
22 test_tick &&
23 T=$(git write-tree) &&
24 sha1=$(echo $name | git commit-tree $T) &&
25 eval $prfx$name=$sha1
26 done
29 test_expect_success setup '
30 git checkout --orphan main &&
31 create_test_commits "" &&
32 mkdir $bare &&
33 cd $bare &&
34 git init --bare -b main &&
35 create_test_commits "bare" &&
36 cd -
39 test_expect_success "create $m" '
40 git update-ref $m $A &&
41 test $A = $(git show-ref -s --verify $m)
43 test_expect_success "create $m with oldvalue verification" '
44 git update-ref $m $B $A &&
45 test $B = $(git show-ref -s --verify $m)
47 test_expect_success "fail to delete $m with stale ref" '
48 test_must_fail git update-ref -d $m $A &&
49 test $B = "$(git show-ref -s --verify $m)"
51 test_expect_success "delete $m" '
52 test_when_finished "git update-ref -d $m" &&
53 git update-ref -d $m $B &&
54 test_must_fail git show-ref --verify -q $m
57 test_expect_success "delete $m without oldvalue verification" '
58 test_when_finished "git update-ref -d $m" &&
59 git update-ref $m $A &&
60 test $A = $(git show-ref -s --verify $m) &&
61 git update-ref -d $m &&
62 test_must_fail git show-ref --verify -q $m
65 test_expect_success "fail to create $n due to file/directory conflict" '
66 test_when_finished "git update-ref -d refs/heads/gu" &&
67 git update-ref refs/heads/gu $A &&
68 test_must_fail git update-ref refs/heads/gu/fixes $A
71 test_expect_success "create $m (by HEAD)" '
72 git update-ref HEAD $A &&
73 test $A = $(git show-ref -s --verify $m)
75 test_expect_success "create $m (by HEAD) with oldvalue verification" '
76 git update-ref HEAD $B $A &&
77 test $B = $(git show-ref -s --verify $m)
79 test_expect_success "fail to delete $m (by HEAD) with stale ref" '
80 test_must_fail git update-ref -d HEAD $A &&
81 test $B = $(git show-ref -s --verify $m)
83 test_expect_success "delete $m (by HEAD)" '
84 test_when_finished "git update-ref -d $m" &&
85 git update-ref -d HEAD $B &&
86 test_must_fail git show-ref --verify -q $m
89 test_expect_success "deleting current branch adds message to HEAD's log" '
90 test_when_finished "git update-ref -d $m" &&
91 git update-ref $m $A &&
92 git symbolic-ref HEAD $m &&
93 git update-ref -m delete-$m -d $m &&
94 test_must_fail git show-ref --verify -q $m &&
95 test-tool ref-store main for-each-reflog-ent HEAD >actual &&
96 grep "delete-$m$" actual
99 test_expect_success "deleting by HEAD adds message to HEAD's log" '
100 test_when_finished "git update-ref -d $m" &&
101 git update-ref $m $A &&
102 git symbolic-ref HEAD $m &&
103 git update-ref -m delete-by-head -d HEAD &&
104 test_must_fail git show-ref --verify -q $m &&
105 test-tool ref-store main for-each-reflog-ent HEAD >actual &&
106 grep "delete-by-head$" actual
109 test_expect_success 'update-ref does not create reflogs by default' '
110 test_when_finished "git update-ref -d $outside" &&
111 git update-ref $outside $A &&
112 git rev-parse $A >expect &&
113 git rev-parse $outside >actual &&
114 test_cmp expect actual &&
115 test_must_fail git reflog exists $outside
118 test_expect_success 'update-ref creates reflogs with --create-reflog' '
119 test_when_finished "git update-ref -d $outside" &&
120 git update-ref --create-reflog $outside $A &&
121 git rev-parse $A >expect &&
122 git rev-parse $outside >actual &&
123 test_cmp expect actual &&
124 git reflog exists $outside
127 test_expect_success 'creates no reflog in bare repository' '
128 git -C $bare update-ref $m $bareA &&
129 git -C $bare rev-parse $bareA >expect &&
130 git -C $bare rev-parse $m >actual &&
131 test_cmp expect actual &&
132 test_must_fail git -C $bare reflog exists $m
135 test_expect_success 'core.logAllRefUpdates=true creates reflog in bare repository' '
136 test_when_finished "git -C $bare config --unset core.logAllRefUpdates && \
137 test-tool ref-store main delete-reflog $m" &&
138 git -C $bare config core.logAllRefUpdates true &&
139 git -C $bare update-ref $m $bareB &&
140 git -C $bare rev-parse $bareB >expect &&
141 git -C $bare rev-parse $m >actual &&
142 test_cmp expect actual &&
143 git -C $bare reflog exists $m
146 test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' '
147 test_config core.logAllRefUpdates true &&
148 test_when_finished "git update-ref -d $outside" &&
149 git update-ref $outside $A &&
150 git rev-parse $A >expect &&
151 git rev-parse $outside >actual &&
152 test_cmp expect actual &&
153 test_must_fail git reflog exists $outside
156 test_expect_success 'core.logAllRefUpdates=always creates reflog by default' '
157 test_config core.logAllRefUpdates always &&
158 test_when_finished "git update-ref -d $outside" &&
159 git update-ref $outside $A &&
160 git rev-parse $A >expect &&
161 git rev-parse $outside >actual &&
162 test_cmp expect actual &&
163 git reflog exists $outside
166 test_expect_success 'core.logAllRefUpdates=always creates reflog for ORIG_HEAD' '
167 test_config core.logAllRefUpdates always &&
168 git update-ref ORIG_HEAD $A &&
169 git reflog exists ORIG_HEAD
172 test_expect_success '--no-create-reflog overrides core.logAllRefUpdates=always' '
173 test_config core.logAllRefUpdates true &&
174 test_when_finished "git update-ref -d $outside" &&
175 git update-ref --no-create-reflog $outside $A &&
176 git rev-parse $A >expect &&
177 git rev-parse $outside >actual &&
178 test_cmp expect actual &&
179 test_must_fail git reflog exists $outside
182 test_expect_success "create $m (by HEAD)" '
183 git update-ref HEAD $A &&
184 test $A = $(git show-ref -s --verify $m)
186 test_expect_success 'pack refs' '
187 git pack-refs --all
189 test_expect_success "move $m (by HEAD)" '
190 git update-ref HEAD $B $A &&
191 test $B = $(git show-ref -s --verify $m)
193 test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" '
194 test_when_finished "git update-ref -d $m" &&
195 git update-ref -d HEAD $B &&
196 ! grep "$m" .git/packed-refs &&
197 test_must_fail git show-ref --verify -q $m
200 test_expect_success 'delete symref without dereference' '
201 test_when_finished "git update-ref -d $m" &&
202 echo foo >foo.c &&
203 git add foo.c &&
204 git commit -m foo &&
205 git symbolic-ref SYMREF $m &&
206 git update-ref --no-deref -d SYMREF &&
207 git show-ref --verify -q $m &&
208 test_must_fail git show-ref --verify -q SYMREF &&
209 test_must_fail git symbolic-ref SYMREF
212 test_expect_success 'delete symref without dereference when the referred ref is packed' '
213 test_when_finished "git update-ref -d $m" &&
214 echo foo >foo.c &&
215 git add foo.c &&
216 git commit -m foo &&
217 git symbolic-ref SYMREF $m &&
218 git pack-refs --all &&
219 git update-ref --no-deref -d SYMREF &&
220 git show-ref --verify -q $m &&
221 test_must_fail git show-ref --verify -q SYMREF &&
222 test_must_fail git symbolic-ref SYMREF
225 test_expect_success 'update-ref -d is not confused by self-reference' '
226 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF refs/heads/self" &&
227 git symbolic-ref refs/heads/self refs/heads/self &&
228 git symbolic-ref --no-recurse refs/heads/self &&
229 test_must_fail git update-ref -d refs/heads/self &&
230 git symbolic-ref --no-recurse refs/heads/self
233 test_expect_success 'update-ref --no-deref -d can delete self-reference' '
234 test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF refs/heads/self" &&
235 git symbolic-ref refs/heads/self refs/heads/self &&
236 git symbolic-ref --no-recurse refs/heads/self &&
237 git update-ref --no-deref -d refs/heads/self &&
238 test_must_fail git show-ref --verify -q refs/heads/self
241 test_expect_success REFFILES 'update-ref --no-deref -d can delete reference to bad ref' '
242 >.git/refs/heads/bad &&
243 test_when_finished "rm -f .git/refs/heads/bad" &&
244 git symbolic-ref refs/heads/ref-to-bad refs/heads/bad &&
245 test_when_finished "git update-ref -d refs/heads/ref-to-bad" &&
246 git symbolic-ref --no-recurse refs/heads/ref-to-bad &&
247 git update-ref --no-deref -d refs/heads/ref-to-bad &&
248 test_must_fail git show-ref --verify -q refs/heads/ref-to-bad
251 test_expect_success '(not) create HEAD with old sha1' '
252 test_must_fail git update-ref HEAD $A $B
254 test_expect_success "(not) prior created .git/$m" '
255 test_when_finished "git update-ref -d $m" &&
256 test_must_fail git show-ref --verify -q $m
259 test_expect_success 'create HEAD' '
260 git update-ref HEAD $A
262 test_expect_success '(not) change HEAD with wrong SHA1' '
263 test_must_fail git update-ref HEAD $B $Z
265 test_expect_success "(not) changed .git/$m" '
266 test_when_finished "git update-ref -d $m" &&
267 ! test $B = $(git show-ref -s --verify $m)
270 test_expect_success "clean up reflog" '
271 test-tool ref-store main delete-reflog $m
274 test_expect_success "create $m (logged by touch)" '
275 test_config core.logAllRefUpdates false &&
276 GIT_COMMITTER_DATE="2005-05-26 23:30" \
277 git update-ref --create-reflog HEAD $A -m "Initial Creation" &&
278 test $A = $(git show-ref -s --verify $m)
280 test_expect_success "update $m (logged by touch)" '
281 test_config core.logAllRefUpdates false &&
282 GIT_COMMITTER_DATE="2005-05-26 23:31" \
283 git update-ref HEAD $B $A -m "Switch" &&
284 test $B = $(git show-ref -s --verify $m)
286 test_expect_success "set $m (logged by touch)" '
287 test_config core.logAllRefUpdates false &&
288 GIT_COMMITTER_DATE="2005-05-26 23:41" \
289 git update-ref HEAD $A &&
290 test $A = $(git show-ref -s --verify $m)
293 cat >expect <<EOF
294 $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation
295 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch
296 $B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
298 test_expect_success "verifying $m's log (logged by touch)" '
299 test_when_finished "git update-ref -d $m && git reflog expire --expire=all --all && rm -rf actual expect" &&
300 test-tool ref-store main for-each-reflog-ent $m >actual &&
301 test_cmp actual expect
304 test_expect_success "create $m (logged by config)" '
305 test_config core.logAllRefUpdates true &&
306 GIT_COMMITTER_DATE="2005-05-26 23:32" \
307 git update-ref HEAD $A -m "Initial Creation" &&
308 test $A = $(git show-ref -s --verify $m)
310 test_expect_success "update $m (logged by config)" '
311 test_config core.logAllRefUpdates true &&
312 GIT_COMMITTER_DATE="2005-05-26 23:33" \
313 git update-ref HEAD $B $A -m "Switch" &&
314 test $B = $(git show-ref -s --verify $m)
316 test_expect_success "set $m (logged by config)" '
317 test_config core.logAllRefUpdates true &&
318 GIT_COMMITTER_DATE="2005-05-26 23:43" \
319 git update-ref HEAD $A &&
320 test $A = $(git show-ref -s --verify $m)
323 cat >expect <<EOF
324 $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation
325 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch
326 $B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
328 test_expect_success "verifying $m's log (logged by config)" '
329 test_when_finished "git update-ref -d $m && git reflog expire --expire=all --all && rm -rf actual expect" &&
330 test-tool ref-store main for-each-reflog-ent $m >actual &&
331 test_cmp actual expect
334 test_expect_success 'set up for querying the reflog' '
335 git update-ref -d $m &&
336 test-tool ref-store main delete-reflog $m &&
338 GIT_COMMITTER_DATE="1117150320 -0500" git update-ref $m $C &&
339 GIT_COMMITTER_DATE="1117150350 -0500" git update-ref $m $A &&
340 GIT_COMMITTER_DATE="1117150380 -0500" git update-ref $m $B &&
341 GIT_COMMITTER_DATE="1117150680 -0500" git update-ref $m $F &&
342 GIT_COMMITTER_DATE="1117150980 -0500" git update-ref $m $E &&
343 git update-ref $m $D &&
344 # Delete the last reflog entry so that the tip of m and the reflog for
345 # it disagree.
346 git reflog delete $m@{0} &&
348 cat >expect <<-EOF &&
349 $Z $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
350 $C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
351 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
352 $B $F $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
353 $F $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
355 test-tool ref-store main for-each-reflog-ent $m >actual &&
356 test_cmp expect actual
359 ed="Thu, 26 May 2005 18:32:00 -0500"
360 gd="Thu, 26 May 2005 18:33:00 -0500"
361 ld="Thu, 26 May 2005 18:43:00 -0500"
362 test_expect_success 'Query "main@{May 25 2005}" (before history)' '
363 test_when_finished "rm -f o e" &&
364 git rev-parse --verify "main@{May 25 2005}" >o 2>e &&
365 echo "$C" >expect &&
366 test_cmp expect o &&
367 echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
368 test_cmp expect e
370 test_expect_success 'Query main@{2005-05-25} (before history)' '
371 test_when_finished "rm -f o e" &&
372 git rev-parse --verify main@{2005-05-25} >o 2>e &&
373 echo "$C" >expect &&
374 test_cmp expect o &&
375 echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
376 test_cmp expect e
378 test_expect_success 'Query "main@{May 26 2005 23:31:59}" (1 second before history)' '
379 test_when_finished "rm -f o e" &&
380 git rev-parse --verify "main@{May 26 2005 23:31:59}" >o 2>e &&
381 echo "$C" >expect &&
382 test_cmp expect o &&
383 echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
384 test_cmp expect e
386 test_expect_success 'Query "main@{May 26 2005 23:32:00}" (exactly history start)' '
387 test_when_finished "rm -f o e" &&
388 git rev-parse --verify "main@{May 26 2005 23:32:00}" >o 2>e &&
389 echo "$C" >expect &&
390 test_cmp expect o &&
391 test_must_be_empty e
393 test_expect_success 'Query "main@{May 26 2005 23:32:30}" (first non-creation change)' '
394 test_when_finished "rm -f o e" &&
395 git rev-parse --verify "main@{May 26 2005 23:32:30}" >o 2>e &&
396 echo "$A" >expect &&
397 test_cmp expect o &&
398 test_must_be_empty e
400 test_expect_success 'Query "main@{2005-05-26 23:33:01}" (middle of history with gap)' '
401 test_when_finished "rm -f o e" &&
402 git rev-parse --verify "main@{2005-05-26 23:33:01}" >o 2>e &&
403 echo "$B" >expect &&
404 test_cmp expect o
406 test_expect_success 'Query "main@{2005-05-26 23:38:00}" (middle of history)' '
407 test_when_finished "rm -f o e" &&
408 git rev-parse --verify "main@{2005-05-26 23:38:00}" >o 2>e &&
409 echo "$F" >expect &&
410 test_cmp expect o &&
411 test_must_be_empty e
413 test_expect_success 'Query "main@{2005-05-26 23:43:00}" (exact end of history)' '
414 test_when_finished "rm -f o e" &&
415 git rev-parse --verify "main@{2005-05-26 23:43:00}" >o 2>e &&
416 echo "$E" >expect &&
417 test_cmp expect o &&
418 test_must_be_empty e
420 test_expect_success 'Query "main@{2005-05-28}" (past end of history)' '
421 test_when_finished "rm -f o e" &&
422 git rev-parse --verify "main@{2005-05-28}" >o 2>e &&
423 echo "$D" >expect &&
424 test_cmp expect o &&
425 test_grep -F "warning: log for ref $m unexpectedly ended on $ld" e
428 rm -f expect
429 git update-ref -d $m
431 test_expect_success 'query reflog with gap' '
432 test_when_finished "git update-ref -d $m" &&
434 GIT_COMMITTER_DATE="1117150320 -0500" git update-ref $m $A &&
435 GIT_COMMITTER_DATE="1117150380 -0500" git update-ref $m $B &&
436 GIT_COMMITTER_DATE="1117150480 -0500" git update-ref $m $C &&
437 GIT_COMMITTER_DATE="1117150580 -0500" git update-ref $m $D &&
438 GIT_COMMITTER_DATE="1117150680 -0500" git update-ref $m $F &&
439 git reflog delete $m@{2} &&
441 git rev-parse --verify "main@{2005-05-26 23:33:01}" >actual 2>stderr &&
442 echo "$B" >expect &&
443 test_cmp expect actual &&
444 test_grep -F "warning: log for ref $m has gap after $gd" stderr
447 test_expect_success 'creating initial files' '
448 test_when_finished rm -f M &&
449 echo TEST >F &&
450 git add F &&
451 GIT_AUTHOR_DATE="2005-05-26 23:30" \
452 GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
453 h_TEST=$(git rev-parse --verify HEAD) &&
454 echo The other day this did not work. >M &&
455 echo And then Bob told me how to fix it. >>M &&
456 echo OTHER >F &&
457 GIT_AUTHOR_DATE="2005-05-26 23:41" \
458 GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a &&
459 h_OTHER=$(git rev-parse --verify HEAD) &&
460 GIT_AUTHOR_DATE="2005-05-26 23:44" \
461 GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend &&
462 h_FIXED=$(git rev-parse --verify HEAD) &&
463 echo Merged initial commit and a later commit. >M &&
464 echo $h_TEST >.git/MERGE_HEAD &&
465 GIT_AUTHOR_DATE="2005-05-26 23:45" \
466 GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
467 h_MERGED=$(git rev-parse --verify HEAD)
470 cat >expect <<EOF
471 $Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add
472 $h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work.
473 $h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work.
474 $h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit.
476 test_expect_success 'git commit logged updates' '
477 test-tool ref-store main for-each-reflog-ent $m >actual &&
478 test_cmp expect actual
480 unset h_TEST h_OTHER h_FIXED h_MERGED
482 test_expect_success 'git cat-file blob main:F (expect OTHER)' '
483 test OTHER = $(git cat-file blob main:F)
485 test_expect_success 'git cat-file blob main@{2005-05-26 23:30}:F (expect TEST)' '
486 test TEST = $(git cat-file blob "main@{2005-05-26 23:30}:F")
488 test_expect_success 'git cat-file blob main@{2005-05-26 23:42}:F (expect OTHER)' '
489 test OTHER = $(git cat-file blob "main@{2005-05-26 23:42}:F")
492 # Test adding and deleting pseudorefs
494 test_expect_success 'given old value for missing pseudoref, do not create' '
495 test_must_fail git update-ref PSEUDOREF $A $B 2>err &&
496 test_must_fail git rev-parse PSEUDOREF &&
497 test_grep "unable to resolve reference" err
500 test_expect_success 'create pseudoref' '
501 git update-ref PSEUDOREF $A &&
502 test $A = $(git show-ref -s --verify PSEUDOREF)
505 test_expect_success 'overwrite pseudoref with no old value given' '
506 git update-ref PSEUDOREF $B &&
507 test $B = $(git show-ref -s --verify PSEUDOREF)
510 test_expect_success 'overwrite pseudoref with correct old value' '
511 git update-ref PSEUDOREF $C $B &&
512 test $C = $(git show-ref -s --verify PSEUDOREF)
515 test_expect_success 'do not overwrite pseudoref with wrong old value' '
516 test_must_fail git update-ref PSEUDOREF $D $E 2>err &&
517 test $C = $(git show-ref -s --verify PSEUDOREF) &&
518 test_grep "cannot lock ref.*expected" err
521 test_expect_success 'delete pseudoref' '
522 git update-ref -d PSEUDOREF &&
523 test_must_fail git show-ref -s --verify PSEUDOREF
526 test_expect_success 'do not delete pseudoref with wrong old value' '
527 git update-ref PSEUDOREF $A &&
528 test_must_fail git update-ref -d PSEUDOREF $B 2>err &&
529 test $A = $(git show-ref -s --verify PSEUDOREF) &&
530 test_grep "cannot lock ref.*expected" err
533 test_expect_success 'delete pseudoref with correct old value' '
534 git update-ref -d PSEUDOREF $A &&
535 test_must_fail git show-ref -s --verify PSEUDOREF
538 test_expect_success 'create pseudoref with old OID zero' '
539 git update-ref PSEUDOREF $A $Z &&
540 test $A = $(git show-ref -s --verify PSEUDOREF)
543 test_expect_success 'do not overwrite pseudoref with old OID zero' '
544 test_when_finished git update-ref -d PSEUDOREF &&
545 test_must_fail git update-ref PSEUDOREF $B $Z 2>err &&
546 test $A = $(git show-ref -s --verify PSEUDOREF) &&
547 test_grep "already exists" err
550 # Test --stdin
552 a=refs/heads/a
553 b=refs/heads/b
554 c=refs/heads/c
555 E='""'
556 F='%s\0'
557 pws='path with space'
559 test_expect_success 'stdin test setup' '
560 echo "$pws" >"$pws" &&
561 git add -- "$pws" &&
562 git commit -m "$pws"
565 test_expect_success '-z fails without --stdin' '
566 test_must_fail git update-ref -z $m $m $m 2>err &&
567 test_grep "usage: git update-ref" err
570 test_expect_success 'stdin works with no input' '
571 >stdin &&
572 git update-ref --stdin <stdin &&
573 git rev-parse --verify -q $m
576 test_expect_success 'stdin fails on empty line' '
577 echo "" >stdin &&
578 test_must_fail git update-ref --stdin <stdin 2>err &&
579 grep "fatal: empty command in input" err
582 test_expect_success 'stdin fails on only whitespace' '
583 echo " " >stdin &&
584 test_must_fail git update-ref --stdin <stdin 2>err &&
585 grep "fatal: whitespace before command: " err
588 test_expect_success 'stdin fails on leading whitespace' '
589 echo " create $a $m" >stdin &&
590 test_must_fail git update-ref --stdin <stdin 2>err &&
591 grep "fatal: whitespace before command: create $a $m" err
594 test_expect_success 'stdin fails on unknown command' '
595 echo "unknown $a" >stdin &&
596 test_must_fail git update-ref --stdin <stdin 2>err &&
597 grep "fatal: unknown command: unknown $a" err
600 test_expect_success 'stdin fails on unbalanced quotes' '
601 echo "create $a \"main" >stdin &&
602 test_must_fail git update-ref --stdin <stdin 2>err &&
603 grep "fatal: badly quoted argument: \\\"main" err
606 test_expect_success 'stdin fails on invalid escape' '
607 echo "create $a \"ma\zn\"" >stdin &&
608 test_must_fail git update-ref --stdin <stdin 2>err &&
609 grep "fatal: badly quoted argument: \\\"ma\\\\zn\\\"" err
612 test_expect_success 'stdin fails on junk after quoted argument' '
613 echo "create \"$a\"main" >stdin &&
614 test_must_fail git update-ref --stdin <stdin 2>err &&
615 grep "fatal: unexpected character after quoted argument: \\\"$a\\\"main" err
618 test_expect_success 'stdin fails create with no ref' '
619 echo "create " >stdin &&
620 test_must_fail git update-ref --stdin <stdin 2>err &&
621 grep "fatal: create: missing <ref>" err
624 test_expect_success 'stdin fails create with no new value' '
625 echo "create $a" >stdin &&
626 test_must_fail git update-ref --stdin <stdin 2>err &&
627 grep "fatal: create $a: missing <new-oid>" err
630 test_expect_success 'stdin fails create with too many arguments' '
631 echo "create $a $m $m" >stdin &&
632 test_must_fail git update-ref --stdin <stdin 2>err &&
633 grep "fatal: create $a: extra input: $m" err
636 test_expect_success 'stdin fails update with no ref' '
637 echo "update " >stdin &&
638 test_must_fail git update-ref --stdin <stdin 2>err &&
639 grep "fatal: update: missing <ref>" err
642 test_expect_success 'stdin fails update with no new value' '
643 echo "update $a" >stdin &&
644 test_must_fail git update-ref --stdin <stdin 2>err &&
645 grep "fatal: update $a: missing <new-oid>" err
648 test_expect_success 'stdin fails update with too many arguments' '
649 echo "update $a $m $m $m" >stdin &&
650 test_must_fail git update-ref --stdin <stdin 2>err &&
651 grep "fatal: update $a: extra input: $m" err
654 test_expect_success 'stdin fails delete with no ref' '
655 echo "delete " >stdin &&
656 test_must_fail git update-ref --stdin <stdin 2>err &&
657 grep "fatal: delete: missing <ref>" err
660 test_expect_success 'stdin fails delete with too many arguments' '
661 echo "delete $a $m $m" >stdin &&
662 test_must_fail git update-ref --stdin <stdin 2>err &&
663 grep "fatal: delete $a: extra input: $m" err
666 test_expect_success 'stdin fails verify with too many arguments' '
667 echo "verify $a $m $m" >stdin &&
668 test_must_fail git update-ref --stdin <stdin 2>err &&
669 grep "fatal: verify $a: extra input: $m" err
672 test_expect_success 'stdin fails option with unknown name' '
673 echo "option unknown" >stdin &&
674 test_must_fail git update-ref --stdin <stdin 2>err &&
675 grep "fatal: option unknown: unknown" err
678 test_expect_success 'stdin fails with duplicate refs' '
679 cat >stdin <<-EOF &&
680 create $a $m
681 create $b $m
682 create $a $m
684 test_must_fail git update-ref --stdin <stdin 2>err &&
685 test_grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
688 test_expect_success 'stdin create ref works' '
689 echo "create $a $m" >stdin &&
690 git update-ref --stdin <stdin &&
691 git rev-parse $m >expect &&
692 git rev-parse $a >actual &&
693 test_cmp expect actual
696 test_expect_success 'stdin does not create reflogs by default' '
697 test_when_finished "git update-ref -d $outside" &&
698 echo "create $outside $m" >stdin &&
699 git update-ref --stdin <stdin &&
700 git rev-parse $m >expect &&
701 git rev-parse $outside >actual &&
702 test_cmp expect actual &&
703 test_must_fail git reflog exists $outside
706 test_expect_success 'stdin creates reflogs with --create-reflog' '
707 test_when_finished "git update-ref -d $outside" &&
708 echo "create $outside $m" >stdin &&
709 git update-ref --create-reflog --stdin <stdin &&
710 git rev-parse $m >expect &&
711 git rev-parse $outside >actual &&
712 test_cmp expect actual &&
713 git reflog exists $outside
716 test_expect_success 'stdin succeeds with quoted argument' '
717 git update-ref -d $a &&
718 echo "create $a \"$m\"" >stdin &&
719 git update-ref --stdin <stdin &&
720 git rev-parse $m >expect &&
721 git rev-parse $a >actual &&
722 test_cmp expect actual
725 test_expect_success 'stdin succeeds with escaped character' '
726 git update-ref -d $a &&
727 echo "create $a \"ma\\151n\"" >stdin &&
728 git update-ref --stdin <stdin &&
729 git rev-parse $m >expect &&
730 git rev-parse $a >actual &&
731 test_cmp expect actual
734 test_expect_success 'stdin update ref creates with zero old value' '
735 echo "update $b $m $Z" >stdin &&
736 git update-ref --stdin <stdin &&
737 git rev-parse $m >expect &&
738 git rev-parse $b >actual &&
739 test_cmp expect actual &&
740 git update-ref -d $b
743 test_expect_success 'stdin update ref creates with empty old value' '
744 echo "update $b $m $E" >stdin &&
745 git update-ref --stdin <stdin &&
746 git rev-parse $m >expect &&
747 git rev-parse $b >actual &&
748 test_cmp expect actual
751 test_expect_success 'stdin create ref works with path with space to blob' '
752 echo "create refs/blobs/pws \"$m:$pws\"" >stdin &&
753 git update-ref --stdin <stdin &&
754 git rev-parse "$m:$pws" >expect &&
755 git rev-parse refs/blobs/pws >actual &&
756 test_cmp expect actual &&
757 git update-ref -d refs/blobs/pws
760 test_expect_success 'stdin update ref fails with wrong old value' '
761 echo "update $c $m $m~1" >stdin &&
762 test_must_fail git update-ref --stdin <stdin 2>err &&
763 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
764 test_must_fail git rev-parse --verify -q $c
767 test_expect_success 'stdin update ref fails with bad old value' '
768 echo "update $c $m does-not-exist" >stdin &&
769 test_must_fail git update-ref --stdin <stdin 2>err &&
770 grep "fatal: update $c: invalid <old-oid>: does-not-exist" err &&
771 test_must_fail git rev-parse --verify -q $c
774 test_expect_success 'stdin create ref fails with bad new value' '
775 echo "create $c does-not-exist" >stdin &&
776 test_must_fail git update-ref --stdin <stdin 2>err &&
777 grep "fatal: create $c: invalid <new-oid>: does-not-exist" err &&
778 test_must_fail git rev-parse --verify -q $c
781 test_expect_success 'stdin create ref fails with zero new value' '
782 echo "create $c " >stdin &&
783 test_must_fail git update-ref --stdin <stdin 2>err &&
784 grep "fatal: create $c: zero <new-oid>" err &&
785 test_must_fail git rev-parse --verify -q $c
788 test_expect_success 'stdin update ref works with right old value' '
789 echo "update $b $m~1 $m" >stdin &&
790 git update-ref --stdin <stdin &&
791 git rev-parse $m~1 >expect &&
792 git rev-parse $b >actual &&
793 test_cmp expect actual
796 test_expect_success 'stdin delete ref fails with wrong old value' '
797 echo "delete $a $m~1" >stdin &&
798 test_must_fail git update-ref --stdin <stdin 2>err &&
799 grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
800 git rev-parse $m >expect &&
801 git rev-parse $a >actual &&
802 test_cmp expect actual
805 test_expect_success 'stdin delete ref fails with zero old value' '
806 echo "delete $a " >stdin &&
807 test_must_fail git update-ref --stdin <stdin 2>err &&
808 grep "fatal: delete $a: zero <old-oid>" err &&
809 git rev-parse $m >expect &&
810 git rev-parse $a >actual &&
811 test_cmp expect actual
814 test_expect_success 'stdin update symref works option no-deref' '
815 git symbolic-ref TESTSYMREF $b &&
816 cat >stdin <<-EOF &&
817 option no-deref
818 update TESTSYMREF $a $b
820 git update-ref --stdin <stdin &&
821 git rev-parse TESTSYMREF >expect &&
822 git rev-parse $a >actual &&
823 test_cmp expect actual &&
824 git rev-parse $m~1 >expect &&
825 git rev-parse $b >actual &&
826 test_cmp expect actual
829 test_expect_success 'stdin delete symref works option no-deref' '
830 git symbolic-ref TESTSYMREF $b &&
831 cat >stdin <<-EOF &&
832 option no-deref
833 delete TESTSYMREF $b
835 git update-ref --stdin <stdin &&
836 test_must_fail git rev-parse --verify -q TESTSYMREF &&
837 git rev-parse $m~1 >expect &&
838 git rev-parse $b >actual &&
839 test_cmp expect actual
842 test_expect_success 'stdin update symref works flag --no-deref' '
843 git symbolic-ref TESTSYMREFONE $b &&
844 git symbolic-ref TESTSYMREFTWO $b &&
845 cat >stdin <<-EOF &&
846 update TESTSYMREFONE $a $b
847 update TESTSYMREFTWO $a $b
849 git update-ref --no-deref --stdin <stdin &&
850 git rev-parse TESTSYMREFONE TESTSYMREFTWO >expect &&
851 git rev-parse $a $a >actual &&
852 test_cmp expect actual &&
853 git rev-parse $m~1 >expect &&
854 git rev-parse $b >actual &&
855 test_cmp expect actual
858 test_expect_success 'stdin delete symref works flag --no-deref' '
859 git symbolic-ref TESTSYMREFONE $b &&
860 git symbolic-ref TESTSYMREFTWO $b &&
861 cat >stdin <<-EOF &&
862 delete TESTSYMREFONE $b
863 delete TESTSYMREFTWO $b
865 git update-ref --no-deref --stdin <stdin &&
866 test_must_fail git rev-parse --verify -q TESTSYMREFONE &&
867 test_must_fail git rev-parse --verify -q TESTSYMREFTWO &&
868 git rev-parse $m~1 >expect &&
869 git rev-parse $b >actual &&
870 test_cmp expect actual
873 test_expect_success 'stdin delete ref works with right old value' '
874 echo "delete $b $m~1" >stdin &&
875 git update-ref --stdin <stdin &&
876 test_must_fail git rev-parse --verify -q $b
879 test_expect_success 'stdin update/create/verify combination works' '
880 cat >stdin <<-EOF &&
881 update $a $m
882 create $b $m
883 verify $c
885 git update-ref --stdin <stdin &&
886 git rev-parse $m >expect &&
887 git rev-parse $a >actual &&
888 test_cmp expect actual &&
889 git rev-parse $b >actual &&
890 test_cmp expect actual &&
891 test_must_fail git rev-parse --verify -q $c
894 test_expect_success 'stdin verify succeeds for correct value' '
895 test-tool ref-store main for-each-reflog-ent $m >before &&
896 git rev-parse $m >expect &&
897 echo "verify $m $m" >stdin &&
898 git update-ref --stdin <stdin &&
899 git rev-parse $m >actual &&
900 test_cmp expect actual &&
901 test-tool ref-store main for-each-reflog-ent $m >after &&
902 test_cmp before after
905 test_expect_success 'stdin verify succeeds for missing reference' '
906 test-tool ref-store main for-each-reflog-ent $m >before &&
907 echo "verify refs/heads/missing $Z" >stdin &&
908 git update-ref --stdin <stdin &&
909 test_must_fail git rev-parse --verify -q refs/heads/missing &&
910 test-tool ref-store main for-each-reflog-ent $m >after &&
911 test_cmp before after
914 test_expect_success 'stdin verify treats no value as missing' '
915 echo "verify refs/heads/missing" >stdin &&
916 git update-ref --stdin <stdin &&
917 test_must_fail git rev-parse --verify -q refs/heads/missing
920 test_expect_success 'stdin verify fails for wrong value' '
921 git rev-parse $m >expect &&
922 echo "verify $m $m~1" >stdin &&
923 test_must_fail git update-ref --stdin <stdin &&
924 git rev-parse $m >actual &&
925 test_cmp expect actual
928 test_expect_success 'stdin verify fails for mistaken null value' '
929 git rev-parse $m >expect &&
930 echo "verify $m $Z" >stdin &&
931 test_must_fail git update-ref --stdin <stdin &&
932 git rev-parse $m >actual &&
933 test_cmp expect actual
936 test_expect_success 'stdin verify fails for mistaken empty value' '
937 M=$(git rev-parse $m) &&
938 test_when_finished "git update-ref $m $M" &&
939 git rev-parse $m >expect &&
940 echo "verify $m" >stdin &&
941 test_must_fail git update-ref --stdin <stdin &&
942 git rev-parse $m >actual &&
943 test_cmp expect actual
946 test_expect_success 'stdin update refs works with identity updates' '
947 cat >stdin <<-EOF &&
948 update $a $m $m
949 update $b $m $m
950 update $c $Z $E
952 git update-ref --stdin <stdin &&
953 git rev-parse $m >expect &&
954 git rev-parse $a >actual &&
955 test_cmp expect actual &&
956 git rev-parse $b >actual &&
957 test_cmp expect actual &&
958 test_must_fail git rev-parse --verify -q $c
961 test_expect_success 'stdin update refs fails with wrong old value' '
962 git update-ref $c $m &&
963 cat >stdin <<-EOF &&
964 update $a $m $m
965 update $b $m $m
966 update $c ''
968 test_must_fail git update-ref --stdin <stdin 2>err &&
969 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
970 git rev-parse $m >expect &&
971 git rev-parse $a >actual &&
972 test_cmp expect actual &&
973 git rev-parse $b >actual &&
974 test_cmp expect actual &&
975 git rev-parse $c >actual &&
976 test_cmp expect actual
979 test_expect_success 'stdin delete refs works with packed and loose refs' '
980 git pack-refs --all &&
981 git update-ref $c $m~1 &&
982 cat >stdin <<-EOF &&
983 delete $a $m
984 update $b $Z $m
985 update $c $E $m~1
987 git update-ref --stdin <stdin &&
988 test_must_fail git rev-parse --verify -q $a &&
989 test_must_fail git rev-parse --verify -q $b &&
990 test_must_fail git rev-parse --verify -q $c
993 test_expect_success 'stdin -z works on empty input' '
994 >stdin &&
995 git update-ref -z --stdin <stdin &&
996 git rev-parse --verify -q $m
999 test_expect_success 'stdin -z fails on empty line' '
1000 echo "" >stdin &&
1001 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1002 grep "fatal: whitespace before command: " err
1005 test_expect_success 'stdin -z fails on empty command' '
1006 printf $F "" >stdin &&
1007 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1008 grep "fatal: empty command in input" err
1011 test_expect_success 'stdin -z fails on only whitespace' '
1012 printf $F " " >stdin &&
1013 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1014 grep "fatal: whitespace before command: " err
1017 test_expect_success 'stdin -z fails on leading whitespace' '
1018 printf $F " create $a" "$m" >stdin &&
1019 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1020 grep "fatal: whitespace before command: create $a" err
1023 test_expect_success 'stdin -z fails on unknown command' '
1024 printf $F "unknown $a" >stdin &&
1025 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1026 grep "fatal: unknown command: unknown $a" err
1029 test_expect_success 'stdin -z fails create with no ref' '
1030 printf $F "create " >stdin &&
1031 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1032 grep "fatal: create: missing <ref>" err
1035 test_expect_success 'stdin -z fails create with no new value' '
1036 printf $F "create $a" >stdin &&
1037 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1038 grep "fatal: create $a: unexpected end of input when reading <new-oid>" err
1041 test_expect_success 'stdin -z fails create with too many arguments' '
1042 printf $F "create $a" "$m" "$m" >stdin &&
1043 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1044 grep "fatal: unknown command: $m" err
1047 test_expect_success 'stdin -z fails update with no ref' '
1048 printf $F "update " >stdin &&
1049 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1050 grep "fatal: update: missing <ref>" err
1053 test_expect_success 'stdin -z fails update with too few args' '
1054 printf $F "update $a" "$m" >stdin &&
1055 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1056 grep "fatal: update $a: unexpected end of input when reading <old-oid>" err
1059 test_expect_success 'stdin -z emits warning with empty new value' '
1060 git update-ref $a $m &&
1061 printf $F "update $a" "" "" >stdin &&
1062 git update-ref -z --stdin <stdin 2>err &&
1063 grep "warning: update $a: missing <new-oid>, treating as zero" err &&
1064 test_must_fail git rev-parse --verify -q $a
1067 test_expect_success 'stdin -z fails update with no new value' '
1068 printf $F "update $a" >stdin &&
1069 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1070 grep "fatal: update $a: unexpected end of input when reading <new-oid>" err
1073 test_expect_success 'stdin -z fails update with no old value' '
1074 printf $F "update $a" "$m" >stdin &&
1075 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1076 grep "fatal: update $a: unexpected end of input when reading <old-oid>" err
1079 test_expect_success 'stdin -z fails update with too many arguments' '
1080 printf $F "update $a" "$m" "$m" "$m" >stdin &&
1081 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1082 grep "fatal: unknown command: $m" err
1085 test_expect_success 'stdin -z fails delete with no ref' '
1086 printf $F "delete " >stdin &&
1087 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1088 grep "fatal: delete: missing <ref>" err
1091 test_expect_success 'stdin -z fails delete with no old value' '
1092 printf $F "delete $a" >stdin &&
1093 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1094 grep "fatal: delete $a: unexpected end of input when reading <old-oid>" err
1097 test_expect_success 'stdin -z fails delete with too many arguments' '
1098 printf $F "delete $a" "$m" "$m" >stdin &&
1099 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1100 grep "fatal: unknown command: $m" err
1103 test_expect_success 'stdin -z fails verify with too many arguments' '
1104 printf $F "verify $a" "$m" "$m" >stdin &&
1105 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1106 grep "fatal: unknown command: $m" err
1109 test_expect_success 'stdin -z fails verify with no old value' '
1110 printf $F "verify $a" >stdin &&
1111 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1112 grep "fatal: verify $a: unexpected end of input when reading <old-oid>" err
1115 test_expect_success 'stdin -z fails option with unknown name' '
1116 printf $F "option unknown" >stdin &&
1117 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1118 grep "fatal: option unknown: unknown" err
1121 test_expect_success 'stdin -z fails with duplicate refs' '
1122 printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
1123 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1124 test_grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
1127 test_expect_success 'stdin -z create ref works' '
1128 printf $F "create $a" "$m" >stdin &&
1129 git update-ref -z --stdin <stdin &&
1130 git rev-parse $m >expect &&
1131 git rev-parse $a >actual &&
1132 test_cmp expect actual
1135 test_expect_success 'stdin -z update ref creates with zero old value' '
1136 printf $F "update $b" "$m" "$Z" >stdin &&
1137 git update-ref -z --stdin <stdin &&
1138 git rev-parse $m >expect &&
1139 git rev-parse $b >actual &&
1140 test_cmp expect actual &&
1141 git update-ref -d $b
1144 test_expect_success 'stdin -z update ref creates with empty old value' '
1145 printf $F "update $b" "$m" "" >stdin &&
1146 git update-ref -z --stdin <stdin &&
1147 git rev-parse $m >expect &&
1148 git rev-parse $b >actual &&
1149 test_cmp expect actual
1152 test_expect_success 'stdin -z create ref works with path with space to blob' '
1153 printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
1154 git update-ref -z --stdin <stdin &&
1155 git rev-parse "$m:$pws" >expect &&
1156 git rev-parse refs/blobs/pws >actual &&
1157 test_cmp expect actual &&
1158 git update-ref -d refs/blobs/pws
1161 test_expect_success 'stdin -z update ref fails with wrong old value' '
1162 printf $F "update $c" "$m" "$m~1" >stdin &&
1163 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1164 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1165 test_must_fail git rev-parse --verify -q $c
1168 test_expect_success 'stdin -z update ref fails with bad old value' '
1169 printf $F "update $c" "$m" "does-not-exist" >stdin &&
1170 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1171 grep "fatal: update $c: invalid <old-oid>: does-not-exist" err &&
1172 test_must_fail git rev-parse --verify -q $c
1175 test_expect_success 'stdin -z create ref fails when ref exists' '
1176 git update-ref $c $m &&
1177 git rev-parse "$c" >expect &&
1178 printf $F "create $c" "$m~1" >stdin &&
1179 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1180 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1181 git rev-parse "$c" >actual &&
1182 test_cmp expect actual
1185 test_expect_success 'stdin -z create ref fails with bad new value' '
1186 git update-ref -d "$c" &&
1187 printf $F "create $c" "does-not-exist" >stdin &&
1188 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1189 grep "fatal: create $c: invalid <new-oid>: does-not-exist" err &&
1190 test_must_fail git rev-parse --verify -q $c
1193 test_expect_success 'stdin -z create ref fails with empty new value' '
1194 printf $F "create $c" "" >stdin &&
1195 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1196 grep "fatal: create $c: missing <new-oid>" err &&
1197 test_must_fail git rev-parse --verify -q $c
1200 test_expect_success 'stdin -z update ref works with right old value' '
1201 printf $F "update $b" "$m~1" "$m" >stdin &&
1202 git update-ref -z --stdin <stdin &&
1203 git rev-parse $m~1 >expect &&
1204 git rev-parse $b >actual &&
1205 test_cmp expect actual
1208 test_expect_success 'stdin -z delete ref fails with wrong old value' '
1209 printf $F "delete $a" "$m~1" >stdin &&
1210 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1211 grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
1212 git rev-parse $m >expect &&
1213 git rev-parse $a >actual &&
1214 test_cmp expect actual
1217 test_expect_success 'stdin -z delete ref fails with zero old value' '
1218 printf $F "delete $a" "$Z" >stdin &&
1219 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1220 grep "fatal: delete $a: zero <old-oid>" err &&
1221 git rev-parse $m >expect &&
1222 git rev-parse $a >actual &&
1223 test_cmp expect actual
1226 test_expect_success 'stdin -z update symref works option no-deref' '
1227 git symbolic-ref TESTSYMREF $b &&
1228 printf $F "option no-deref" "update TESTSYMREF" "$a" "$b" >stdin &&
1229 git update-ref -z --stdin <stdin &&
1230 git rev-parse TESTSYMREF >expect &&
1231 git rev-parse $a >actual &&
1232 test_cmp expect actual &&
1233 git rev-parse $m~1 >expect &&
1234 git rev-parse $b >actual &&
1235 test_cmp expect actual
1238 test_expect_success 'stdin -z delete symref works option no-deref' '
1239 git symbolic-ref TESTSYMREF $b &&
1240 printf $F "option no-deref" "delete TESTSYMREF" "$b" >stdin &&
1241 git update-ref -z --stdin <stdin &&
1242 test_must_fail git rev-parse --verify -q TESTSYMREF &&
1243 git rev-parse $m~1 >expect &&
1244 git rev-parse $b >actual &&
1245 test_cmp expect actual
1248 test_expect_success 'stdin -z delete ref works with right old value' '
1249 printf $F "delete $b" "$m~1" >stdin &&
1250 git update-ref -z --stdin <stdin &&
1251 test_must_fail git rev-parse --verify -q $b
1254 test_expect_success 'stdin -z update/create/verify combination works' '
1255 printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin &&
1256 git update-ref -z --stdin <stdin &&
1257 git rev-parse $m >expect &&
1258 git rev-parse $a >actual &&
1259 test_cmp expect actual &&
1260 git rev-parse $b >actual &&
1261 test_cmp expect actual &&
1262 test_must_fail git rev-parse --verify -q $c
1265 test_expect_success 'stdin -z verify succeeds for correct value' '
1266 git rev-parse $m >expect &&
1267 printf $F "verify $m" "$m" >stdin &&
1268 git update-ref -z --stdin <stdin &&
1269 git rev-parse $m >actual &&
1270 test_cmp expect actual
1273 test_expect_success 'stdin -z verify succeeds for missing reference' '
1274 printf $F "verify refs/heads/missing" "$Z" >stdin &&
1275 git update-ref -z --stdin <stdin &&
1276 test_must_fail git rev-parse --verify -q refs/heads/missing
1279 test_expect_success 'stdin -z verify treats no value as missing' '
1280 printf $F "verify refs/heads/missing" "" >stdin &&
1281 git update-ref -z --stdin <stdin &&
1282 test_must_fail git rev-parse --verify -q refs/heads/missing
1285 test_expect_success 'stdin -z verify fails for wrong value' '
1286 git rev-parse $m >expect &&
1287 printf $F "verify $m" "$m~1" >stdin &&
1288 test_must_fail git update-ref -z --stdin <stdin &&
1289 git rev-parse $m >actual &&
1290 test_cmp expect actual
1293 test_expect_success 'stdin -z verify fails for mistaken null value' '
1294 git rev-parse $m >expect &&
1295 printf $F "verify $m" "$Z" >stdin &&
1296 test_must_fail git update-ref -z --stdin <stdin &&
1297 git rev-parse $m >actual &&
1298 test_cmp expect actual
1301 test_expect_success 'stdin -z verify fails for mistaken empty value' '
1302 M=$(git rev-parse $m) &&
1303 test_when_finished "git update-ref $m $M" &&
1304 git rev-parse $m >expect &&
1305 printf $F "verify $m" "" >stdin &&
1306 test_must_fail git update-ref -z --stdin <stdin &&
1307 git rev-parse $m >actual &&
1308 test_cmp expect actual
1311 test_expect_success 'stdin -z update refs works with identity updates' '
1312 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
1313 git update-ref -z --stdin <stdin &&
1314 git rev-parse $m >expect &&
1315 git rev-parse $a >actual &&
1316 test_cmp expect actual &&
1317 git rev-parse $b >actual &&
1318 test_cmp expect actual &&
1319 test_must_fail git rev-parse --verify -q $c
1322 test_expect_success 'stdin -z update refs fails with wrong old value' '
1323 git update-ref $c $m &&
1324 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
1325 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1326 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
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 git rev-parse $c >actual &&
1333 test_cmp expect actual
1336 test_expect_success 'stdin -z delete refs works with packed and loose refs' '
1337 git pack-refs --all &&
1338 git update-ref $c $m~1 &&
1339 printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
1340 git update-ref -z --stdin <stdin &&
1341 test_must_fail git rev-parse --verify -q $a &&
1342 test_must_fail git rev-parse --verify -q $b &&
1343 test_must_fail git rev-parse --verify -q $c
1346 test_expect_success 'fails with duplicate HEAD update' '
1347 git branch target1 $A &&
1348 git checkout target1 &&
1349 cat >stdin <<-EOF &&
1350 update refs/heads/target1 $C
1351 option no-deref
1352 update HEAD $B
1354 test_must_fail git update-ref --stdin <stdin 2>err &&
1355 test_grep "fatal: multiple updates for '\''HEAD'\'' (including one via its referent .refs/heads/target1.) are not allowed" err &&
1356 echo "refs/heads/target1" >expect &&
1357 git symbolic-ref HEAD >actual &&
1358 test_cmp expect actual &&
1359 echo "$A" >expect &&
1360 git rev-parse refs/heads/target1 >actual &&
1361 test_cmp expect actual
1364 test_expect_success 'fails with duplicate ref update via symref' '
1365 test_when_finished "git symbolic-ref -d refs/heads/symref2" &&
1366 git branch target2 $A &&
1367 git symbolic-ref refs/heads/symref2 refs/heads/target2 &&
1368 cat >stdin <<-EOF &&
1369 update refs/heads/target2 $C
1370 update refs/heads/symref2 $B
1372 test_must_fail git update-ref --stdin <stdin 2>err &&
1373 test_grep "fatal: multiple updates for '\''refs/heads/target2'\'' (including one via symref .refs/heads/symref2.) are not allowed" err &&
1374 echo "refs/heads/target2" >expect &&
1375 git symbolic-ref refs/heads/symref2 >actual &&
1376 test_cmp expect actual &&
1377 echo "$A" >expect &&
1378 git rev-parse refs/heads/target2 >actual &&
1379 test_cmp expect actual
1382 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
1384 for i in $(test_seq 33)
1386 echo "create refs/heads/$i HEAD" || exit 1
1387 done >large_input &&
1388 run_with_limited_open_files git update-ref --stdin <large_input &&
1389 git rev-parse --verify -q refs/heads/33
1393 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
1395 for i in $(test_seq 33)
1397 echo "delete refs/heads/$i HEAD" || exit 1
1398 done >large_input &&
1399 run_with_limited_open_files git update-ref --stdin <large_input &&
1400 test_must_fail git rev-parse --verify -q refs/heads/33
1404 test_expect_success 'handle per-worktree refs in refs/bisect' '
1405 git commit --allow-empty -m "initial commit" &&
1406 git worktree add -b branch worktree &&
1408 cd worktree &&
1409 git commit --allow-empty -m "test commit" &&
1410 git for-each-ref >for-each-ref.out &&
1411 ! grep refs/bisect for-each-ref.out &&
1412 git update-ref refs/bisect/something HEAD &&
1413 git rev-parse refs/bisect/something >../worktree-head &&
1414 git for-each-ref | grep refs/bisect/something
1415 ) &&
1416 git show-ref >actual &&
1417 ! grep 'refs/bisect' actual &&
1418 test_must_fail git rev-parse refs/bisect/something &&
1419 git update-ref refs/bisect/something HEAD &&
1420 git rev-parse refs/bisect/something >main-head &&
1421 ! test_cmp main-head worktree-head
1424 test_expect_success 'transaction handles empty commit' '
1425 cat >stdin <<-EOF &&
1426 start
1427 prepare
1428 commit
1430 git update-ref --stdin <stdin >actual &&
1431 printf "%s: ok\n" start prepare commit >expect &&
1432 test_cmp expect actual
1435 test_expect_success 'transaction handles empty commit with missing prepare' '
1436 cat >stdin <<-EOF &&
1437 start
1438 commit
1440 git update-ref --stdin <stdin >actual &&
1441 printf "%s: ok\n" start commit >expect &&
1442 test_cmp expect actual
1445 test_expect_success 'transaction handles sole commit' '
1446 cat >stdin <<-EOF &&
1447 commit
1449 git update-ref --stdin <stdin >actual &&
1450 printf "%s: ok\n" commit >expect &&
1451 test_cmp expect actual
1454 test_expect_success 'transaction handles empty abort' '
1455 cat >stdin <<-EOF &&
1456 start
1457 prepare
1458 abort
1460 git update-ref --stdin <stdin >actual &&
1461 printf "%s: ok\n" start prepare abort >expect &&
1462 test_cmp expect actual
1465 test_expect_success 'transaction exits on multiple aborts' '
1466 cat >stdin <<-EOF &&
1467 abort
1468 abort
1470 test_must_fail git update-ref --stdin <stdin >actual 2>err &&
1471 printf "%s: ok\n" abort >expect &&
1472 test_cmp expect actual &&
1473 grep "fatal: transaction is closed" err
1476 test_expect_success 'transaction exits on start after prepare' '
1477 cat >stdin <<-EOF &&
1478 prepare
1479 start
1481 test_must_fail git update-ref --stdin <stdin 2>err >actual &&
1482 printf "%s: ok\n" prepare >expect &&
1483 test_cmp expect actual &&
1484 grep "fatal: prepared transactions can only be closed" err
1487 test_expect_success 'transaction handles empty abort with missing prepare' '
1488 cat >stdin <<-EOF &&
1489 start
1490 abort
1492 git update-ref --stdin <stdin >actual &&
1493 printf "%s: ok\n" start abort >expect &&
1494 test_cmp expect actual
1497 test_expect_success 'transaction handles sole abort' '
1498 cat >stdin <<-EOF &&
1499 abort
1501 git update-ref --stdin <stdin >actual &&
1502 printf "%s: ok\n" abort >expect &&
1503 test_cmp expect actual
1506 test_expect_success 'transaction can handle commit' '
1507 cat >stdin <<-EOF &&
1508 start
1509 create $a HEAD
1510 commit
1512 git update-ref --stdin <stdin >actual &&
1513 printf "%s: ok\n" start commit >expect &&
1514 test_cmp expect actual &&
1515 git rev-parse HEAD >expect &&
1516 git rev-parse $a >actual &&
1517 test_cmp expect actual
1520 test_expect_success 'transaction can handle abort' '
1521 cat >stdin <<-EOF &&
1522 start
1523 create $b HEAD
1524 abort
1526 git update-ref --stdin <stdin >actual &&
1527 printf "%s: ok\n" start abort >expect &&
1528 test_cmp expect actual &&
1529 test_must_fail git show-ref --verify -q $b
1532 test_expect_success 'transaction aborts by default' '
1533 cat >stdin <<-EOF &&
1534 start
1535 create $b HEAD
1537 git update-ref --stdin <stdin >actual &&
1538 printf "%s: ok\n" start >expect &&
1539 test_cmp expect actual &&
1540 test_must_fail git show-ref --verify -q $b
1543 test_expect_success 'transaction with prepare aborts by default' '
1544 cat >stdin <<-EOF &&
1545 start
1546 create $b HEAD
1547 prepare
1549 git update-ref --stdin <stdin >actual &&
1550 printf "%s: ok\n" start prepare >expect &&
1551 test_cmp expect actual &&
1552 test_must_fail git show-ref --verify -q $b
1555 test_expect_success 'transaction can commit multiple times' '
1556 cat >stdin <<-EOF &&
1557 start
1558 create refs/heads/branch-1 $A
1559 commit
1560 start
1561 create refs/heads/branch-2 $B
1562 commit
1564 git update-ref --stdin <stdin >actual &&
1565 printf "%s: ok\n" start commit start commit >expect &&
1566 test_cmp expect actual &&
1567 echo "$A" >expect &&
1568 git rev-parse refs/heads/branch-1 >actual &&
1569 test_cmp expect actual &&
1570 echo "$B" >expect &&
1571 git rev-parse refs/heads/branch-2 >actual &&
1572 test_cmp expect actual
1575 test_expect_success 'transaction can create and delete' '
1576 cat >stdin <<-EOF &&
1577 start
1578 create refs/heads/create-and-delete $A
1579 commit
1580 start
1581 delete refs/heads/create-and-delete $A
1582 commit
1584 git update-ref --stdin <stdin >actual &&
1585 printf "%s: ok\n" start commit start commit >expect &&
1586 test_cmp expect actual &&
1587 test_must_fail git show-ref --verify refs/heads/create-and-delete
1590 test_expect_success 'transaction can commit after abort' '
1591 cat >stdin <<-EOF &&
1592 start
1593 create refs/heads/abort $A
1594 abort
1595 start
1596 create refs/heads/abort $A
1597 commit
1599 git update-ref --stdin <stdin >actual &&
1600 printf "%s: ok\n" start abort start commit >expect &&
1601 echo "$A" >expect &&
1602 git rev-parse refs/heads/abort >actual &&
1603 test_cmp expect actual
1606 test_expect_success 'transaction cannot restart ongoing transaction' '
1607 cat >stdin <<-EOF &&
1608 start
1609 create refs/heads/restart $A
1610 start
1611 commit
1613 test_must_fail git update-ref --stdin <stdin >actual &&
1614 printf "%s: ok\n" start >expect &&
1615 test_cmp expect actual &&
1616 test_must_fail git show-ref --verify refs/heads/restart
1619 test_expect_success PIPE 'transaction flushes status updates' '
1620 mkfifo in out &&
1621 (git update-ref --stdin <in >out &) &&
1623 exec 9>in &&
1624 exec 8<out &&
1625 test_when_finished "exec 9>&-" &&
1626 test_when_finished "exec 8<&-" &&
1628 echo "start" >&9 &&
1629 echo "start: ok" >expected &&
1630 read line <&8 &&
1631 echo "$line" >actual &&
1632 test_cmp expected actual &&
1634 echo "create refs/heads/flush $A" >&9 &&
1636 echo prepare >&9 &&
1637 echo "prepare: ok" >expected &&
1638 read line <&8 &&
1639 echo "$line" >actual &&
1640 test_cmp expected actual &&
1642 # This must now fail given that we have locked the ref.
1643 test_must_fail git update-ref refs/heads/flush $B 2>stderr &&
1644 grep "fatal: update_ref failed for ref ${SQ}refs/heads/flush${SQ}: cannot lock ref" stderr &&
1646 echo commit >&9 &&
1647 echo "commit: ok" >expected &&
1648 read line <&8 &&
1649 echo "$line" >actual &&
1650 test_cmp expected actual
1653 format_command () {
1654 if test "$1" = "-z"
1655 then
1656 shift
1657 printf "$F" "$@"
1658 else
1659 echo "$@"
1663 for type in "" "-z"
1666 test_expect_success "stdin $type symref-verify fails without --no-deref" '
1667 git symbolic-ref refs/heads/symref $a &&
1668 format_command $type "symref-verify refs/heads/symref" "$a" >stdin &&
1669 test_must_fail git update-ref --stdin $type <stdin 2>err &&
1670 grep "fatal: symref-verify: cannot operate with deref mode" err
1673 test_expect_success "stdin $type symref-verify fails with too many arguments" '
1674 format_command $type "symref-verify refs/heads/symref" "$a" "$a" >stdin &&
1675 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1676 if test "$type" = "-z"
1677 then
1678 grep "fatal: unknown command: $a" err
1679 else
1680 grep "fatal: symref-verify refs/heads/symref: extra input: $a" err
1684 test_expect_success "stdin $type symref-verify succeeds for correct value" '
1685 git symbolic-ref refs/heads/symref >expect &&
1686 test-tool ref-store main for-each-reflog-ent refs/heads/symref >before &&
1687 format_command $type "symref-verify refs/heads/symref" "$a" >stdin &&
1688 git update-ref --stdin $type --no-deref <stdin &&
1689 git symbolic-ref refs/heads/symref >actual &&
1690 test_cmp expect actual &&
1691 test-tool ref-store main for-each-reflog-ent refs/heads/symref >after &&
1692 test_cmp before after
1695 test_expect_success "stdin $type symref-verify fails with no value" '
1696 git symbolic-ref refs/heads/symref >expect &&
1697 format_command $type "symref-verify refs/heads/symref" "" >stdin &&
1698 test_must_fail git update-ref --stdin $type --no-deref <stdin
1701 test_expect_success "stdin $type symref-verify succeeds for dangling reference" '
1702 test_when_finished "git symbolic-ref -d refs/heads/symref2" &&
1703 test_must_fail git symbolic-ref refs/heads/nonexistent &&
1704 git symbolic-ref refs/heads/symref2 refs/heads/nonexistent &&
1705 format_command $type "symref-verify refs/heads/symref2" "refs/heads/nonexistent" >stdin &&
1706 git update-ref --stdin $type --no-deref <stdin
1709 test_expect_success "stdin $type symref-verify fails for missing reference" '
1710 test-tool ref-store main for-each-reflog-ent refs/heads/symref >before &&
1711 format_command $type "symref-verify refs/heads/missing" "refs/heads/unknown" >stdin &&
1712 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1713 grep "fatal: cannot lock ref ${SQ}refs/heads/missing${SQ}: unable to resolve reference ${SQ}refs/heads/missing${SQ}" err &&
1714 test_must_fail git rev-parse --verify -q refs/heads/missing &&
1715 test-tool ref-store main for-each-reflog-ent refs/heads/symref >after &&
1716 test_cmp before after
1719 test_expect_success "stdin $type symref-verify fails for wrong value" '
1720 git symbolic-ref refs/heads/symref >expect &&
1721 format_command $type "symref-verify refs/heads/symref" "$b" >stdin &&
1722 test_must_fail git update-ref --stdin $type --no-deref <stdin &&
1723 git symbolic-ref refs/heads/symref >actual &&
1724 test_cmp expect actual
1727 test_expect_success "stdin $type symref-verify fails for mistaken null value" '
1728 git symbolic-ref refs/heads/symref >expect &&
1729 format_command $type "symref-verify refs/heads/symref" "$Z" >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
1735 test_expect_success "stdin $type symref-delete fails without --no-deref" '
1736 git symbolic-ref refs/heads/symref $a &&
1737 format_command $type "symref-delete refs/heads/symref" "$a" >stdin &&
1738 test_must_fail git update-ref --stdin $type <stdin 2>err &&
1739 grep "fatal: symref-delete: cannot operate with deref mode" err
1742 test_expect_success "stdin $type symref-delete fails with no ref" '
1743 format_command $type "symref-delete " >stdin &&
1744 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1745 grep "fatal: symref-delete: missing <ref>" err
1748 test_expect_success "stdin $type symref-delete fails deleting regular ref" '
1749 test_when_finished "git update-ref -d refs/heads/regularref" &&
1750 git update-ref refs/heads/regularref $a &&
1751 format_command $type "symref-delete refs/heads/regularref" "$a" >stdin &&
1752 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1753 grep "fatal: cannot lock ref ${SQ}refs/heads/regularref${SQ}: expected symref with target ${SQ}$a${SQ}: but is a regular ref" err
1756 test_expect_success "stdin $type symref-delete fails with too many arguments" '
1757 format_command $type "symref-delete refs/heads/symref" "$a" "$a" >stdin &&
1758 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1759 if test "$type" = "-z"
1760 then
1761 grep "fatal: unknown command: $a" err
1762 else
1763 grep "fatal: symref-delete refs/heads/symref: extra input: $a" err
1767 test_expect_success "stdin $type symref-delete fails with wrong old value" '
1768 format_command $type "symref-delete refs/heads/symref" "$m" >stdin &&
1769 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1770 grep "fatal: verifying symref target: ${SQ}refs/heads/symref${SQ}: is at $a but expected refs/heads/main" err &&
1771 git symbolic-ref refs/heads/symref >expect &&
1772 echo $a >actual &&
1773 test_cmp expect actual
1776 test_expect_success "stdin $type symref-delete works with right old value" '
1777 format_command $type "symref-delete refs/heads/symref" "$a" >stdin &&
1778 git update-ref --stdin $type --no-deref <stdin &&
1779 test_must_fail git rev-parse --verify -q refs/heads/symref
1782 test_expect_success "stdin $type symref-delete works with empty old value" '
1783 git symbolic-ref refs/heads/symref $a >stdin &&
1784 format_command $type "symref-delete refs/heads/symref" "" >stdin &&
1785 git update-ref --stdin $type --no-deref <stdin &&
1786 test_must_fail git rev-parse --verify -q $b
1789 test_expect_success "stdin $type symref-delete succeeds for dangling reference" '
1790 test_must_fail git symbolic-ref refs/heads/nonexistent &&
1791 git symbolic-ref refs/heads/symref2 refs/heads/nonexistent &&
1792 format_command $type "symref-delete refs/heads/symref2" "refs/heads/nonexistent" >stdin &&
1793 git update-ref --stdin $type --no-deref <stdin &&
1794 test_must_fail git symbolic-ref -d refs/heads/symref2
1797 test_expect_success "stdin $type symref-delete deletes regular ref without target" '
1798 git update-ref refs/heads/regularref $a &&
1799 format_command $type "symref-delete refs/heads/regularref" >stdin &&
1800 git update-ref --stdin $type --no-deref <stdin
1803 test_expect_success "stdin $type symref-create fails with too many arguments" '
1804 format_command $type "symref-create refs/heads/symref" "$a" "$a" >stdin &&
1805 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1806 if test "$type" = "-z"
1807 then
1808 grep "fatal: unknown command: $a" err
1809 else
1810 grep "fatal: symref-create refs/heads/symref: extra input: $a" err
1814 test_expect_success "stdin $type symref-create fails with no target" '
1815 format_command $type "symref-create refs/heads/symref" >stdin &&
1816 test_must_fail git update-ref --stdin $type --no-deref <stdin
1819 test_expect_success "stdin $type symref-create fails with empty target" '
1820 format_command $type "symref-create refs/heads/symref" "" >stdin &&
1821 test_must_fail git update-ref --stdin $type --no-deref <stdin
1824 test_expect_success "stdin $type symref-create works" '
1825 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1826 format_command $type "symref-create refs/heads/symref" "$a" >stdin &&
1827 git update-ref --stdin $type --no-deref <stdin &&
1828 git symbolic-ref refs/heads/symref >expect &&
1829 echo $a >actual &&
1830 test_cmp expect actual
1833 test_expect_success "stdin $type symref-create works with --no-deref" '
1834 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1835 format_command $type "symref-create refs/heads/symref" "$a" &&
1836 git update-ref --stdin $type <stdin 2>err
1839 test_expect_success "stdin $type create dangling symref ref works" '
1840 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1841 format_command $type "symref-create refs/heads/symref" "refs/heads/unkown" >stdin &&
1842 git update-ref --stdin $type --no-deref <stdin &&
1843 git symbolic-ref refs/heads/symref >expect &&
1844 echo refs/heads/unkown >actual &&
1845 test_cmp expect actual
1848 test_expect_success "stdin $type symref-create does not create reflogs by default" '
1849 test_when_finished "git symbolic-ref -d refs/symref" &&
1850 format_command $type "symref-create refs/symref" "$a" >stdin &&
1851 git update-ref --stdin $type --no-deref <stdin &&
1852 git symbolic-ref refs/symref >expect &&
1853 echo $a >actual &&
1854 test_cmp expect actual &&
1855 test_must_fail git reflog exists refs/symref
1858 test_expect_success "stdin $type symref-create reflogs with --create-reflog" '
1859 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1860 format_command $type "symref-create refs/heads/symref" "$a" >stdin &&
1861 git update-ref --create-reflog --stdin $type --no-deref <stdin &&
1862 git symbolic-ref refs/heads/symref >expect &&
1863 echo $a >actual &&
1864 test_cmp expect actual &&
1865 git reflog exists refs/heads/symref
1868 test_expect_success "stdin $type symref-update fails with too many arguments" '
1869 format_command $type "symref-update refs/heads/symref" "$a" "ref" "$a" "$a" >stdin &&
1870 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1871 if test "$type" = "-z"
1872 then
1873 grep "fatal: unknown command: $a" err
1874 else
1875 grep "fatal: symref-update refs/heads/symref: extra input: $a" err
1879 test_expect_success "stdin $type symref-update fails with wrong old value argument" '
1880 format_command $type "symref-update refs/heads/symref" "$a" "foo" "$a" "$a" >stdin &&
1881 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1882 grep "fatal: symref-update refs/heads/symref: invalid arg ${SQ}foo${SQ} for old value" err
1885 test_expect_success "stdin $type symref-update creates with zero old value" '
1886 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1887 format_command $type "symref-update refs/heads/symref" "$a" "oid" "$Z" >stdin &&
1888 git update-ref --stdin $type --no-deref <stdin &&
1889 echo $a >expect &&
1890 git symbolic-ref refs/heads/symref >actual &&
1891 test_cmp expect actual
1894 test_expect_success "stdin $type symref-update creates with no old value" '
1895 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1896 format_command $type "symref-update refs/heads/symref" "$a" >stdin &&
1897 git update-ref --stdin $type --no-deref <stdin &&
1898 echo $a >expect &&
1899 git symbolic-ref refs/heads/symref >actual &&
1900 test_cmp expect actual
1903 test_expect_success "stdin $type symref-update creates dangling" '
1904 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1905 test_must_fail git rev-parse refs/heads/nonexistent &&
1906 format_command $type "symref-update refs/heads/symref" "refs/heads/nonexistent" >stdin &&
1907 git update-ref --stdin $type --no-deref <stdin &&
1908 echo refs/heads/nonexistent >expect &&
1909 git symbolic-ref refs/heads/symref >actual &&
1910 test_cmp expect actual
1913 test_expect_success "stdin $type symref-update fails with wrong old value" '
1914 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1915 git symbolic-ref refs/heads/symref $a &&
1916 format_command $type "symref-update refs/heads/symref" "$m" "ref" "$b" >stdin &&
1917 test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1918 grep "fatal: verifying symref target: ${SQ}refs/heads/symref${SQ}: is at $a but expected $b" err &&
1919 test_must_fail git rev-parse --verify -q $c
1922 test_expect_success "stdin $type symref-update updates dangling ref" '
1923 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1924 test_must_fail git rev-parse refs/heads/nonexistent &&
1925 git symbolic-ref refs/heads/symref refs/heads/nonexistent &&
1926 format_command $type "symref-update refs/heads/symref" "$a" >stdin &&
1927 git update-ref --stdin $type --no-deref <stdin &&
1928 echo $a >expect &&
1929 git symbolic-ref refs/heads/symref >actual &&
1930 test_cmp expect actual
1933 test_expect_success "stdin $type symref-update updates dangling ref with old value" '
1934 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1935 test_must_fail git rev-parse refs/heads/nonexistent &&
1936 git symbolic-ref refs/heads/symref refs/heads/nonexistent &&
1937 format_command $type "symref-update refs/heads/symref" "$a" "ref" "refs/heads/nonexistent" >stdin &&
1938 git update-ref --stdin $type --no-deref <stdin &&
1939 echo $a >expect &&
1940 git symbolic-ref refs/heads/symref >actual &&
1941 test_cmp expect actual
1944 test_expect_success "stdin $type symref-update fails update dangling ref with wrong old value" '
1945 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1946 test_must_fail git rev-parse refs/heads/nonexistent &&
1947 git symbolic-ref refs/heads/symref refs/heads/nonexistent &&
1948 format_command $type "symref-update refs/heads/symref" "$a" "ref" "refs/heads/wrongref" >stdin &&
1949 test_must_fail git update-ref --stdin $type --no-deref <stdin &&
1950 echo refs/heads/nonexistent >expect &&
1951 git symbolic-ref refs/heads/symref >actual &&
1952 test_cmp expect actual
1955 test_expect_success "stdin $type symref-update works with right old value" '
1956 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1957 git symbolic-ref refs/heads/symref $a &&
1958 format_command $type "symref-update refs/heads/symref" "$m" "ref" "$a" >stdin &&
1959 git update-ref --stdin $type --no-deref <stdin &&
1960 echo $m >expect &&
1961 git symbolic-ref refs/heads/symref >actual &&
1962 test_cmp expect actual
1965 test_expect_success "stdin $type symref-update works with no old value" '
1966 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1967 git symbolic-ref refs/heads/symref $a &&
1968 format_command $type "symref-update refs/heads/symref" "$m" >stdin &&
1969 git update-ref --stdin $type --no-deref <stdin &&
1970 echo $m >expect &&
1971 git symbolic-ref refs/heads/symref >actual &&
1972 test_cmp expect actual
1975 test_expect_success "stdin $type symref-update fails with empty old ref-target" '
1976 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1977 git symbolic-ref refs/heads/symref $a &&
1978 format_command $type "symref-update refs/heads/symref" "$m" "ref" "" >stdin &&
1979 test_must_fail git update-ref --stdin $type --no-deref <stdin &&
1980 echo $a >expect &&
1981 git symbolic-ref refs/heads/symref >actual &&
1982 test_cmp expect actual
1985 test_expect_success "stdin $type symref-update creates (with deref)" '
1986 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1987 format_command $type "symref-update refs/heads/symref" "$a" >stdin &&
1988 git update-ref --stdin $type <stdin &&
1989 echo $a >expect &&
1990 git symbolic-ref --no-recurse refs/heads/symref >actual &&
1991 test_cmp expect actual &&
1992 test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual &&
1993 grep "$Z $(git rev-parse $a)" actual
1996 test_expect_success "stdin $type symref-update regular ref to symref with correct old-oid" '
1997 test_when_finished "git symbolic-ref -d --no-recurse refs/heads/regularref" &&
1998 git update-ref --no-deref refs/heads/regularref $a &&
1999 format_command $type "symref-update refs/heads/regularref" "$a" "oid" "$(git rev-parse $a)" >stdin &&
2000 git update-ref --stdin $type <stdin &&
2001 echo $a >expect &&
2002 git symbolic-ref --no-recurse refs/heads/regularref >actual &&
2003 test_cmp expect actual &&
2004 test-tool ref-store main for-each-reflog-ent refs/heads/regularref >actual &&
2005 grep "$(git rev-parse $a) $(git rev-parse $a)" actual
2008 test_expect_success "stdin $type symref-update regular ref to symref fails with wrong old-oid" '
2009 test_when_finished "git update-ref -d refs/heads/regularref" &&
2010 git update-ref --no-deref refs/heads/regularref $a &&
2011 format_command $type "symref-update refs/heads/regularref" "$a" "oid" "$(git rev-parse refs/heads/target2)" >stdin &&
2012 test_must_fail git update-ref --stdin $type <stdin 2>err &&
2013 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 &&
2014 echo $(git rev-parse $a) >expect &&
2015 git rev-parse refs/heads/regularref >actual &&
2016 test_cmp expect actual
2019 test_expect_success "stdin $type symref-update regular ref to symref fails with invalid old-oid" '
2020 test_when_finished "git update-ref -d refs/heads/regularref" &&
2021 git update-ref --no-deref refs/heads/regularref $a &&
2022 format_command $type "symref-update refs/heads/regularref" "$a" "oid" "not-a-ref-oid" >stdin &&
2023 test_must_fail git update-ref --stdin $type <stdin 2>err &&
2024 grep "fatal: symref-update refs/heads/regularref: invalid oid: not-a-ref-oid" err &&
2025 echo $(git rev-parse $a) >expect &&
2026 git rev-parse refs/heads/regularref >actual &&
2027 test_cmp expect actual
2030 test_expect_success "stdin $type symref-update existing symref with zero old-oid" '
2031 test_when_finished "git symbolic-ref -d --no-recurse refs/heads/symref" &&
2032 git symbolic-ref refs/heads/symref refs/heads/target2 &&
2033 format_command $type "symref-update refs/heads/symref" "$a" "oid" "$Z" >stdin &&
2034 test_must_fail git update-ref --stdin $type <stdin 2>err &&
2035 grep "fatal: cannot lock ref ${SQ}refs/heads/symref${SQ}: reference already exists" err &&
2036 echo refs/heads/target2 >expect &&
2037 git symbolic-ref refs/heads/symref >actual &&
2038 test_cmp expect actual
2041 test_expect_success "stdin $type symref-update regular ref to symref (with deref)" '
2042 test_when_finished "git symbolic-ref -d refs/heads/symref" &&
2043 test_when_finished "git update-ref -d --no-deref refs/heads/symref2" &&
2044 git update-ref refs/heads/symref2 $a &&
2045 git symbolic-ref --no-recurse refs/heads/symref refs/heads/symref2 &&
2046 format_command $type "symref-update refs/heads/symref" "$a" >stdin &&
2047 git update-ref $type --stdin <stdin &&
2048 echo $a >expect &&
2049 git symbolic-ref --no-recurse refs/heads/symref2 >actual &&
2050 test_cmp expect actual &&
2051 echo refs/heads/symref2 >expect &&
2052 git symbolic-ref --no-recurse refs/heads/symref >actual &&
2053 test_cmp expect actual &&
2054 test-tool ref-store main for-each-reflog-ent refs/heads/symref >actual &&
2055 grep "$(git rev-parse $a) $(git rev-parse $a)" actual
2058 test_expect_success "stdin $type symref-update regular ref to symref" '
2059 test_when_finished "git symbolic-ref -d --no-recurse refs/heads/regularref" &&
2060 git update-ref --no-deref refs/heads/regularref $a &&
2061 format_command $type "symref-update refs/heads/regularref" "$a" >stdin &&
2062 git update-ref $type --stdin <stdin &&
2063 echo $a >expect &&
2064 git symbolic-ref --no-recurse refs/heads/regularref >actual &&
2065 test_cmp expect actual &&
2066 test-tool ref-store main for-each-reflog-ent refs/heads/regularref >actual &&
2067 grep "$(git rev-parse $a) $(git rev-parse $a)" actual
2070 done
2072 test_done