t5[6-9]*: adjust the references to the default branch name "main"
[git] / t / t1400-update-ref.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Shawn Pearce
4 #
5
6 test_description='Test git update-ref and basic ref logging'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
10 . ./test-lib.sh
11
12 Z=$ZERO_OID
13
14 m=refs/heads/main
15 n_dir=refs/heads/gu
16 n=$n_dir/fixes
17 outside=refs/foo
18 bare=bare-repo
19
20 create_test_commits ()
21 {
22         prfx="$1"
23         for name in A B C D E F
24         do
25                 test_tick &&
26                 T=$(git write-tree) &&
27                 sha1=$(echo $name | git commit-tree $T) &&
28                 eval $prfx$name=$sha1
29         done
30 }
31
32 test_expect_success setup '
33         create_test_commits "" &&
34         mkdir $bare &&
35         cd $bare &&
36         git init --bare &&
37         create_test_commits "bare" &&
38         cd -
39 '
40
41 test_expect_success "create $m" '
42         git update-ref $m $A &&
43         test $A = $(git show-ref -s --verify $m)
44 '
45 test_expect_success "create $m with oldvalue verification" '
46         git update-ref $m $B $A &&
47         test $B = $(git show-ref -s --verify $m)
48 '
49 test_expect_success "fail to delete $m with stale ref" '
50         test_must_fail git update-ref -d $m $A &&
51         test $B = "$(git show-ref -s --verify $m)"
52 '
53 test_expect_success "delete $m" '
54         test_when_finished "rm -f .git/$m" &&
55         git update-ref -d $m $B &&
56         test_path_is_missing .git/$m
57 '
58
59 test_expect_success "delete $m without oldvalue verification" '
60         test_when_finished "rm -f .git/$m" &&
61         git update-ref $m $A &&
62         test $A = $(git show-ref -s --verify $m) &&
63         git update-ref -d $m &&
64         test_path_is_missing .git/$m
65 '
66
67 test_expect_success "fail to create $n" '
68         test_when_finished "rm -f .git/$n_dir" &&
69         touch .git/$n_dir &&
70         test_must_fail git update-ref $n $A
71 '
72
73 test_expect_success "create $m (by HEAD)" '
74         git update-ref HEAD $A &&
75         test $A = $(git show-ref -s --verify $m)
76 '
77 test_expect_success "create $m (by HEAD) with oldvalue verification" '
78         git update-ref HEAD $B $A &&
79         test $B = $(git show-ref -s --verify $m)
80 '
81 test_expect_success "fail to delete $m (by HEAD) with stale ref" '
82         test_must_fail git update-ref -d HEAD $A &&
83         test $B = $(git show-ref -s --verify $m)
84 '
85 test_expect_success "delete $m (by HEAD)" '
86         test_when_finished "rm -f .git/$m" &&
87         git update-ref -d HEAD $B &&
88         test_path_is_missing .git/$m
89 '
90
91 test_expect_success "deleting current branch adds message to HEAD's log" '
92         test_when_finished "rm -f .git/$m" &&
93         git update-ref $m $A &&
94         git symbolic-ref HEAD $m &&
95         git update-ref -m delete-$m -d $m &&
96         test_path_is_missing .git/$m &&
97         grep "delete-$m$" .git/logs/HEAD
98 '
99
100 test_expect_success "deleting by HEAD adds message to HEAD's log" '
101         test_when_finished "rm -f .git/$m" &&
102         git update-ref $m $A &&
103         git symbolic-ref HEAD $m &&
104         git update-ref -m delete-by-head -d HEAD &&
105         test_path_is_missing .git/$m &&
106         grep "delete-by-head$" .git/logs/HEAD
107 '
108
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
116 '
117
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
125 '
126
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
133 '
134
135 test_expect_success 'core.logAllRefUpdates=true creates reflog in bare repository' '
136         test_when_finished "git -C $bare config --unset core.logAllRefUpdates && \
137                 rm $bare/logs/$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
144 '
145
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
154 '
155
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
164 '
165
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
170 '
171
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
180 '
181
182 test_expect_success "create $m (by HEAD)" '
183         git update-ref HEAD $A &&
184         test $A = $(git show-ref -s --verify $m)
185 '
186 test_expect_success 'pack refs' '
187         git pack-refs --all
188 '
189 test_expect_success "move $m (by HEAD)" '
190         git update-ref HEAD $B $A &&
191         test $B = $(git show-ref -s --verify $m)
192 '
193 test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" '
194         test_when_finished "rm -f .git/$m" &&
195         git update-ref -d HEAD $B &&
196         ! grep "$m" .git/packed-refs &&
197         test_path_is_missing .git/$m
198 '
199
200 cp -f .git/HEAD .git/HEAD.orig
201 test_expect_success 'delete symref without dereference' '
202         test_when_finished "cp -f .git/HEAD.orig .git/HEAD" &&
203         git update-ref --no-deref -d HEAD &&
204         test_path_is_missing .git/HEAD
205 '
206
207 test_expect_success 'delete symref without dereference when the referred ref is packed' '
208         test_when_finished "cp -f .git/HEAD.orig .git/HEAD" &&
209         echo foo >foo.c &&
210         git add foo.c &&
211         git commit -m foo &&
212         git pack-refs --all &&
213         git update-ref --no-deref -d HEAD &&
214         test_path_is_missing .git/HEAD
215 '
216
217 git update-ref -d $m
218
219 test_expect_success 'update-ref -d is not confused by self-reference' '
220         git symbolic-ref refs/heads/self refs/heads/self &&
221         test_when_finished "rm -f .git/refs/heads/self" &&
222         test_path_is_file .git/refs/heads/self &&
223         test_must_fail git update-ref -d refs/heads/self &&
224         test_path_is_file .git/refs/heads/self
225 '
226
227 test_expect_success 'update-ref --no-deref -d can delete self-reference' '
228         git symbolic-ref refs/heads/self refs/heads/self &&
229         test_when_finished "rm -f .git/refs/heads/self" &&
230         test_path_is_file .git/refs/heads/self &&
231         git update-ref --no-deref -d refs/heads/self &&
232         test_path_is_missing .git/refs/heads/self
233 '
234
235 test_expect_success 'update-ref --no-deref -d can delete reference to bad ref' '
236         >.git/refs/heads/bad &&
237         test_when_finished "rm -f .git/refs/heads/bad" &&
238         git symbolic-ref refs/heads/ref-to-bad refs/heads/bad &&
239         test_when_finished "rm -f .git/refs/heads/ref-to-bad" &&
240         test_path_is_file .git/refs/heads/ref-to-bad &&
241         git update-ref --no-deref -d refs/heads/ref-to-bad &&
242         test_path_is_missing .git/refs/heads/ref-to-bad
243 '
244
245 test_expect_success '(not) create HEAD with old sha1' '
246         test_must_fail git update-ref HEAD $A $B
247 '
248 test_expect_success "(not) prior created .git/$m" '
249         test_when_finished "rm -f .git/$m" &&
250         test_path_is_missing .git/$m
251 '
252
253 test_expect_success 'create HEAD' '
254         git update-ref HEAD $A
255 '
256 test_expect_success '(not) change HEAD with wrong SHA1' '
257         test_must_fail git update-ref HEAD $B $Z
258 '
259 test_expect_success "(not) changed .git/$m" '
260         test_when_finished "rm -f .git/$m" &&
261         ! test $B = $(git show-ref -s --verify $m)
262 '
263
264 rm -f .git/logs/refs/heads/main
265 test_expect_success "create $m (logged by touch)" '
266         test_config core.logAllRefUpdates false &&
267         GIT_COMMITTER_DATE="2005-05-26 23:30" \
268         git update-ref --create-reflog HEAD $A -m "Initial Creation" &&
269         test $A = $(git show-ref -s --verify $m)
270 '
271 test_expect_success "update $m (logged by touch)" '
272         test_config core.logAllRefUpdates false &&
273         GIT_COMMITTER_DATE="2005-05-26 23:31" \
274         git update-ref HEAD $B $A -m "Switch" &&
275         test $B = $(git show-ref -s --verify $m)
276 '
277 test_expect_success "set $m (logged by touch)" '
278         test_config core.logAllRefUpdates false &&
279         GIT_COMMITTER_DATE="2005-05-26 23:41" \
280         git update-ref HEAD $A &&
281         test $A = $(git show-ref -s --verify $m)
282 '
283
284 test_expect_success 'empty directory removal' '
285         git branch d1/d2/r1 HEAD &&
286         git branch d1/r2 HEAD &&
287         test_path_is_file .git/refs/heads/d1/d2/r1 &&
288         test_path_is_file .git/logs/refs/heads/d1/d2/r1 &&
289         git branch -d d1/d2/r1 &&
290         test_path_is_missing .git/refs/heads/d1/d2 &&
291         test_path_is_missing .git/logs/refs/heads/d1/d2 &&
292         test_path_is_file .git/refs/heads/d1/r2 &&
293         test_path_is_file .git/logs/refs/heads/d1/r2
294 '
295
296 test_expect_success 'symref empty directory removal' '
297         git branch e1/e2/r1 HEAD &&
298         git branch e1/r2 HEAD &&
299         git checkout e1/e2/r1 &&
300         test_when_finished "git checkout main" &&
301         test_path_is_file .git/refs/heads/e1/e2/r1 &&
302         test_path_is_file .git/logs/refs/heads/e1/e2/r1 &&
303         git update-ref -d HEAD &&
304         test_path_is_missing .git/refs/heads/e1/e2 &&
305         test_path_is_missing .git/logs/refs/heads/e1/e2 &&
306         test_path_is_file .git/refs/heads/e1/r2 &&
307         test_path_is_file .git/logs/refs/heads/e1/r2 &&
308         test_path_is_file .git/logs/HEAD
309 '
310
311 cat >expect <<EOF
312 $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000       Initial Creation
313 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000       Switch
314 $B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
315 EOF
316 test_expect_success "verifying $m's log (logged by touch)" '
317         test_when_finished "rm -rf .git/$m .git/logs expect" &&
318         test_cmp expect .git/logs/$m
319 '
320
321 test_expect_success "create $m (logged by config)" '
322         test_config core.logAllRefUpdates true &&
323         GIT_COMMITTER_DATE="2005-05-26 23:32" \
324         git update-ref HEAD $A -m "Initial Creation" &&
325         test $A = $(git show-ref -s --verify $m)
326 '
327 test_expect_success "update $m (logged by config)" '
328         test_config core.logAllRefUpdates true &&
329         GIT_COMMITTER_DATE="2005-05-26 23:33" \
330         git update-ref HEAD $B $A -m "Switch" &&
331         test $B = $(git show-ref -s --verify $m)
332 '
333 test_expect_success "set $m (logged by config)" '
334         test_config core.logAllRefUpdates true &&
335         GIT_COMMITTER_DATE="2005-05-26 23:43" \
336         git update-ref HEAD $A &&
337         test $A = $(git show-ref -s --verify $m)
338 '
339
340 cat >expect <<EOF
341 $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000       Initial Creation
342 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000       Switch
343 $B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
344 EOF
345 test_expect_success "verifying $m's log (logged by config)" '
346         test_when_finished "rm -f .git/$m .git/logs/$m expect" &&
347         test_cmp expect .git/logs/$m
348 '
349
350 test_expect_success 'set up for querying the reflog' '
351         git update-ref $m $D &&
352         cat >.git/logs/$m <<-EOF
353         $Z $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
354         $C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
355         $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
356         $F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
357         $Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
358         EOF
359 '
360
361 ed="Thu, 26 May 2005 18:32:00 -0500"
362 gd="Thu, 26 May 2005 18:33:00 -0500"
363 ld="Thu, 26 May 2005 18:43:00 -0500"
364 test_expect_success 'Query "main@{May 25 2005}" (before history)' '
365         test_when_finished "rm -f o e" &&
366         git rev-parse --verify "main@{May 25 2005}" >o 2>e &&
367         echo "$C" >expect &&
368         test_cmp expect o &&
369         echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
370         test_i18ncmp expect e
371 '
372 test_expect_success 'Query main@{2005-05-25} (before history)' '
373         test_when_finished "rm -f o e" &&
374         git rev-parse --verify main@{2005-05-25} >o 2>e &&
375         echo "$C" >expect &&
376         test_cmp expect o &&
377         echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
378         test_i18ncmp expect e
379 '
380 test_expect_success 'Query "main@{May 26 2005 23:31:59}" (1 second before history)' '
381         test_when_finished "rm -f o e" &&
382         git rev-parse --verify "main@{May 26 2005 23:31:59}" >o 2>e &&
383         echo "$C" >expect &&
384         test_cmp expect o &&
385         echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
386         test_i18ncmp expect e
387 '
388 test_expect_success 'Query "main@{May 26 2005 23:32:00}" (exactly history start)' '
389         test_when_finished "rm -f o e" &&
390         git rev-parse --verify "main@{May 26 2005 23:32:00}" >o 2>e &&
391         echo "$C" >expect &&
392         test_cmp expect o &&
393         test_must_be_empty e
394 '
395 test_expect_success 'Query "main@{May 26 2005 23:32:30}" (first non-creation change)' '
396         test_when_finished "rm -f o e" &&
397         git rev-parse --verify "main@{May 26 2005 23:32:30}" >o 2>e &&
398         echo "$A" >expect &&
399         test_cmp expect o &&
400         test_must_be_empty e
401 '
402 test_expect_success 'Query "main@{2005-05-26 23:33:01}" (middle of history with gap)' '
403         test_when_finished "rm -f o e" &&
404         git rev-parse --verify "main@{2005-05-26 23:33:01}" >o 2>e &&
405         echo "$B" >expect &&
406         test_cmp expect o &&
407         test_i18ngrep -F "warning: log for ref $m has gap after $gd" e
408 '
409 test_expect_success 'Query "main@{2005-05-26 23:38:00}" (middle of history)' '
410         test_when_finished "rm -f o e" &&
411         git rev-parse --verify "main@{2005-05-26 23:38:00}" >o 2>e &&
412         echo "$Z" >expect &&
413         test_cmp expect o &&
414         test_must_be_empty e
415 '
416 test_expect_success 'Query "main@{2005-05-26 23:43:00}" (exact end of history)' '
417         test_when_finished "rm -f o e" &&
418         git rev-parse --verify "main@{2005-05-26 23:43:00}" >o 2>e &&
419         echo "$E" >expect &&
420         test_cmp expect o &&
421         test_must_be_empty e
422 '
423 test_expect_success 'Query "main@{2005-05-28}" (past end of history)' '
424         test_when_finished "rm -f o e" &&
425         git rev-parse --verify "main@{2005-05-28}" >o 2>e &&
426         echo "$D" >expect &&
427         test_cmp expect o &&
428         test_i18ngrep -F "warning: log for ref $m unexpectedly ended on $ld" e
429 '
430
431 rm -f .git/$m .git/logs/$m expect
432
433 test_expect_success 'creating initial files' '
434         test_when_finished rm -f M &&
435         echo TEST >F &&
436         git add F &&
437         GIT_AUTHOR_DATE="2005-05-26 23:30" \
438         GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
439         h_TEST=$(git rev-parse --verify HEAD) &&
440         echo The other day this did not work. >M &&
441         echo And then Bob told me how to fix it. >>M &&
442         echo OTHER >F &&
443         GIT_AUTHOR_DATE="2005-05-26 23:41" \
444         GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a &&
445         h_OTHER=$(git rev-parse --verify HEAD) &&
446         GIT_AUTHOR_DATE="2005-05-26 23:44" \
447         GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend &&
448         h_FIXED=$(git rev-parse --verify HEAD) &&
449         echo Merged initial commit and a later commit. >M &&
450         echo $h_TEST >.git/MERGE_HEAD &&
451         GIT_AUTHOR_DATE="2005-05-26 23:45" \
452         GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
453         h_MERGED=$(git rev-parse --verify HEAD)
454 '
455
456 cat >expect <<EOF
457 $Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000  commit (initial): add
458 $h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000    commit: The other day this did not work.
459 $h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000   commit (amend): The other day this did not work.
460 $h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000  commit (merge): Merged initial commit and a later commit.
461 EOF
462 test_expect_success 'git commit logged updates' '
463         test_cmp expect .git/logs/$m
464 '
465 unset h_TEST h_OTHER h_FIXED h_MERGED
466
467 test_expect_success 'git cat-file blob main:F (expect OTHER)' '
468         test OTHER = $(git cat-file blob main:F)
469 '
470 test_expect_success 'git cat-file blob main@{2005-05-26 23:30}:F (expect TEST)' '
471         test TEST = $(git cat-file blob "main@{2005-05-26 23:30}:F")
472 '
473 test_expect_success 'git cat-file blob main@{2005-05-26 23:42}:F (expect OTHER)' '
474         test OTHER = $(git cat-file blob "main@{2005-05-26 23:42}:F")
475 '
476
477 # Test adding and deleting pseudorefs
478
479 test_expect_success 'given old value for missing pseudoref, do not create' '
480         test_must_fail git update-ref PSEUDOREF $A $B 2>err &&
481         test_must_fail git rev-parse PSEUDOREF &&
482         test_i18ngrep "unable to resolve reference" err
483 '
484
485 test_expect_success 'create pseudoref' '
486         git update-ref PSEUDOREF $A &&
487         test $A = $(git rev-parse PSEUDOREF)
488 '
489
490 test_expect_success 'overwrite pseudoref with no old value given' '
491         git update-ref PSEUDOREF $B &&
492         test $B = $(git rev-parse PSEUDOREF)
493 '
494
495 test_expect_success 'overwrite pseudoref with correct old value' '
496         git update-ref PSEUDOREF $C $B &&
497         test $C = $(git rev-parse PSEUDOREF)
498 '
499
500 test_expect_success 'do not overwrite pseudoref with wrong old value' '
501         test_must_fail git update-ref PSEUDOREF $D $E 2>err &&
502         test $C = $(git rev-parse PSEUDOREF) &&
503         test_i18ngrep "cannot lock ref.*expected" err
504 '
505
506 test_expect_success 'delete pseudoref' '
507         git update-ref -d PSEUDOREF &&
508         test_must_fail git rev-parse PSEUDOREF
509 '
510
511 test_expect_success 'do not delete pseudoref with wrong old value' '
512         git update-ref PSEUDOREF $A &&
513         test_must_fail git update-ref -d PSEUDOREF $B 2>err &&
514         test $A = $(git rev-parse PSEUDOREF) &&
515         test_i18ngrep "cannot lock ref.*expected" err
516 '
517
518 test_expect_success 'delete pseudoref with correct old value' '
519         git update-ref -d PSEUDOREF $A &&
520         test_must_fail git rev-parse PSEUDOREF
521 '
522
523 test_expect_success 'create pseudoref with old OID zero' '
524         git update-ref PSEUDOREF $A $Z &&
525         test $A = $(git rev-parse PSEUDOREF)
526 '
527
528 test_expect_success 'do not overwrite pseudoref with old OID zero' '
529         test_when_finished git update-ref -d PSEUDOREF &&
530         test_must_fail git update-ref PSEUDOREF $B $Z 2>err &&
531         test $A = $(git rev-parse PSEUDOREF) &&
532         test_i18ngrep "already exists" err
533 '
534
535 # Test --stdin
536
537 a=refs/heads/a
538 b=refs/heads/b
539 c=refs/heads/c
540 E='""'
541 F='%s\0'
542 pws='path with space'
543
544 test_expect_success 'stdin test setup' '
545         echo "$pws" >"$pws" &&
546         git add -- "$pws" &&
547         git commit -m "$pws"
548 '
549
550 test_expect_success '-z fails without --stdin' '
551         test_must_fail git update-ref -z $m $m $m 2>err &&
552         test_i18ngrep "usage: git update-ref" err
553 '
554
555 test_expect_success 'stdin works with no input' '
556         >stdin &&
557         git update-ref --stdin <stdin &&
558         git rev-parse --verify -q $m
559 '
560
561 test_expect_success 'stdin fails on empty line' '
562         echo "" >stdin &&
563         test_must_fail git update-ref --stdin <stdin 2>err &&
564         grep "fatal: empty command in input" err
565 '
566
567 test_expect_success 'stdin fails on only whitespace' '
568         echo " " >stdin &&
569         test_must_fail git update-ref --stdin <stdin 2>err &&
570         grep "fatal: whitespace before command:  " err
571 '
572
573 test_expect_success 'stdin fails on leading whitespace' '
574         echo " create $a $m" >stdin &&
575         test_must_fail git update-ref --stdin <stdin 2>err &&
576         grep "fatal: whitespace before command:  create $a $m" err
577 '
578
579 test_expect_success 'stdin fails on unknown command' '
580         echo "unknown $a" >stdin &&
581         test_must_fail git update-ref --stdin <stdin 2>err &&
582         grep "fatal: unknown command: unknown $a" err
583 '
584
585 test_expect_success 'stdin fails on unbalanced quotes' '
586         echo "create $a \"main" >stdin &&
587         test_must_fail git update-ref --stdin <stdin 2>err &&
588         grep "fatal: badly quoted argument: \\\"main" err
589 '
590
591 test_expect_success PREPARE_FOR_MAIN_BRANCH 'stdin fails on invalid escape' '
592         echo "create $a \"ma\zn\"" >stdin &&
593         test_must_fail git update-ref --stdin <stdin 2>err &&
594         grep "fatal: badly quoted argument: \\\"ma\\\\zn\\\"" err
595 '
596
597 test_expect_success 'stdin fails on junk after quoted argument' '
598         echo "create \"$a\"main" >stdin &&
599         test_must_fail git update-ref --stdin <stdin 2>err &&
600         grep "fatal: unexpected character after quoted argument: \\\"$a\\\"main" err
601 '
602
603 test_expect_success 'stdin fails create with no ref' '
604         echo "create " >stdin &&
605         test_must_fail git update-ref --stdin <stdin 2>err &&
606         grep "fatal: create: missing <ref>" err
607 '
608
609 test_expect_success 'stdin fails create with no new value' '
610         echo "create $a" >stdin &&
611         test_must_fail git update-ref --stdin <stdin 2>err &&
612         grep "fatal: create $a: missing <newvalue>" err
613 '
614
615 test_expect_success 'stdin fails create with too many arguments' '
616         echo "create $a $m $m" >stdin &&
617         test_must_fail git update-ref --stdin <stdin 2>err &&
618         grep "fatal: create $a: extra input:  $m" err
619 '
620
621 test_expect_success 'stdin fails update with no ref' '
622         echo "update " >stdin &&
623         test_must_fail git update-ref --stdin <stdin 2>err &&
624         grep "fatal: update: missing <ref>" err
625 '
626
627 test_expect_success 'stdin fails update with no new value' '
628         echo "update $a" >stdin &&
629         test_must_fail git update-ref --stdin <stdin 2>err &&
630         grep "fatal: update $a: missing <newvalue>" err
631 '
632
633 test_expect_success 'stdin fails update with too many arguments' '
634         echo "update $a $m $m $m" >stdin &&
635         test_must_fail git update-ref --stdin <stdin 2>err &&
636         grep "fatal: update $a: extra input:  $m" err
637 '
638
639 test_expect_success 'stdin fails delete with no ref' '
640         echo "delete " >stdin &&
641         test_must_fail git update-ref --stdin <stdin 2>err &&
642         grep "fatal: delete: missing <ref>" err
643 '
644
645 test_expect_success 'stdin fails delete with too many arguments' '
646         echo "delete $a $m $m" >stdin &&
647         test_must_fail git update-ref --stdin <stdin 2>err &&
648         grep "fatal: delete $a: extra input:  $m" err
649 '
650
651 test_expect_success 'stdin fails verify with too many arguments' '
652         echo "verify $a $m $m" >stdin &&
653         test_must_fail git update-ref --stdin <stdin 2>err &&
654         grep "fatal: verify $a: extra input:  $m" err
655 '
656
657 test_expect_success 'stdin fails option with unknown name' '
658         echo "option unknown" >stdin &&
659         test_must_fail git update-ref --stdin <stdin 2>err &&
660         grep "fatal: option unknown: unknown" err
661 '
662
663 test_expect_success 'stdin fails with duplicate refs' '
664         cat >stdin <<-EOF &&
665         create $a $m
666         create $b $m
667         create $a $m
668         EOF
669         test_must_fail git update-ref --stdin <stdin 2>err &&
670         test_i18ngrep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
671 '
672
673 test_expect_success 'stdin create ref works' '
674         echo "create $a $m" >stdin &&
675         git update-ref --stdin <stdin &&
676         git rev-parse $m >expect &&
677         git rev-parse $a >actual &&
678         test_cmp expect actual
679 '
680
681 test_expect_success 'stdin does not create reflogs by default' '
682         test_when_finished "git update-ref -d $outside" &&
683         echo "create $outside $m" >stdin &&
684         git update-ref --stdin <stdin &&
685         git rev-parse $m >expect &&
686         git rev-parse $outside >actual &&
687         test_cmp expect actual &&
688         test_must_fail git reflog exists $outside
689 '
690
691 test_expect_success 'stdin creates reflogs with --create-reflog' '
692         test_when_finished "git update-ref -d $outside" &&
693         echo "create $outside $m" >stdin &&
694         git update-ref --create-reflog --stdin <stdin &&
695         git rev-parse $m >expect &&
696         git rev-parse $outside >actual &&
697         test_cmp expect actual &&
698         git reflog exists $outside
699 '
700
701 test_expect_success 'stdin succeeds with quoted argument' '
702         git update-ref -d $a &&
703         echo "create $a \"$m\"" >stdin &&
704         git update-ref --stdin <stdin &&
705         git rev-parse $m >expect &&
706         git rev-parse $a >actual &&
707         test_cmp expect actual
708 '
709
710 test_expect_success PREPARE_FOR_MAIN_BRANCH 'stdin succeeds with escaped character' '
711         git update-ref -d $a &&
712         echo "create $a \"ma\\151n\"" >stdin &&
713         git update-ref --stdin <stdin &&
714         git rev-parse $m >expect &&
715         git rev-parse $a >actual &&
716         test_cmp expect actual
717 '
718
719 test_expect_success 'stdin update ref creates with zero old value' '
720         echo "update $b $m $Z" >stdin &&
721         git update-ref --stdin <stdin &&
722         git rev-parse $m >expect &&
723         git rev-parse $b >actual &&
724         test_cmp expect actual &&
725         git update-ref -d $b
726 '
727
728 test_expect_success 'stdin update ref creates with empty old value' '
729         echo "update $b $m $E" >stdin &&
730         git update-ref --stdin <stdin &&
731         git rev-parse $m >expect &&
732         git rev-parse $b >actual &&
733         test_cmp expect actual
734 '
735
736 test_expect_success 'stdin create ref works with path with space to blob' '
737         echo "create refs/blobs/pws \"$m:$pws\"" >stdin &&
738         git update-ref --stdin <stdin &&
739         git rev-parse "$m:$pws" >expect &&
740         git rev-parse refs/blobs/pws >actual &&
741         test_cmp expect actual &&
742         git update-ref -d refs/blobs/pws
743 '
744
745 test_expect_success 'stdin update ref fails with wrong old value' '
746         echo "update $c $m $m~1" >stdin &&
747         test_must_fail git update-ref --stdin <stdin 2>err &&
748         grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
749         test_must_fail git rev-parse --verify -q $c
750 '
751
752 test_expect_success 'stdin update ref fails with bad old value' '
753         echo "update $c $m does-not-exist" >stdin &&
754         test_must_fail git update-ref --stdin <stdin 2>err &&
755         grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
756         test_must_fail git rev-parse --verify -q $c
757 '
758
759 test_expect_success 'stdin create ref fails with bad new value' '
760         echo "create $c does-not-exist" >stdin &&
761         test_must_fail git update-ref --stdin <stdin 2>err &&
762         grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
763         test_must_fail git rev-parse --verify -q $c
764 '
765
766 test_expect_success 'stdin create ref fails with zero new value' '
767         echo "create $c " >stdin &&
768         test_must_fail git update-ref --stdin <stdin 2>err &&
769         grep "fatal: create $c: zero <newvalue>" err &&
770         test_must_fail git rev-parse --verify -q $c
771 '
772
773 test_expect_success 'stdin update ref works with right old value' '
774         echo "update $b $m~1 $m" >stdin &&
775         git update-ref --stdin <stdin &&
776         git rev-parse $m~1 >expect &&
777         git rev-parse $b >actual &&
778         test_cmp expect actual
779 '
780
781 test_expect_success 'stdin delete ref fails with wrong old value' '
782         echo "delete $a $m~1" >stdin &&
783         test_must_fail git update-ref --stdin <stdin 2>err &&
784         grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
785         git rev-parse $m >expect &&
786         git rev-parse $a >actual &&
787         test_cmp expect actual
788 '
789
790 test_expect_success 'stdin delete ref fails with zero old value' '
791         echo "delete $a " >stdin &&
792         test_must_fail git update-ref --stdin <stdin 2>err &&
793         grep "fatal: delete $a: zero <oldvalue>" err &&
794         git rev-parse $m >expect &&
795         git rev-parse $a >actual &&
796         test_cmp expect actual
797 '
798
799 test_expect_success 'stdin update symref works option no-deref' '
800         git symbolic-ref TESTSYMREF $b &&
801         cat >stdin <<-EOF &&
802         option no-deref
803         update TESTSYMREF $a $b
804         EOF
805         git update-ref --stdin <stdin &&
806         git rev-parse TESTSYMREF >expect &&
807         git rev-parse $a >actual &&
808         test_cmp expect actual &&
809         git rev-parse $m~1 >expect &&
810         git rev-parse $b >actual &&
811         test_cmp expect actual
812 '
813
814 test_expect_success 'stdin delete symref works option no-deref' '
815         git symbolic-ref TESTSYMREF $b &&
816         cat >stdin <<-EOF &&
817         option no-deref
818         delete TESTSYMREF $b
819         EOF
820         git update-ref --stdin <stdin &&
821         test_must_fail git rev-parse --verify -q TESTSYMREF &&
822         git rev-parse $m~1 >expect &&
823         git rev-parse $b >actual &&
824         test_cmp expect actual
825 '
826
827 test_expect_success 'stdin update symref works flag --no-deref' '
828         git symbolic-ref TESTSYMREFONE $b &&
829         git symbolic-ref TESTSYMREFTWO $b &&
830         cat >stdin <<-EOF &&
831         update TESTSYMREFONE $a $b
832         update TESTSYMREFTWO $a $b
833         EOF
834         git update-ref --no-deref --stdin <stdin &&
835         git rev-parse TESTSYMREFONE TESTSYMREFTWO >expect &&
836         git rev-parse $a $a >actual &&
837         test_cmp expect actual &&
838         git rev-parse $m~1 >expect &&
839         git rev-parse $b >actual &&
840         test_cmp expect actual
841 '
842
843 test_expect_success 'stdin delete symref works flag --no-deref' '
844         git symbolic-ref TESTSYMREFONE $b &&
845         git symbolic-ref TESTSYMREFTWO $b &&
846         cat >stdin <<-EOF &&
847         delete TESTSYMREFONE $b
848         delete TESTSYMREFTWO $b
849         EOF
850         git update-ref --no-deref --stdin <stdin &&
851         test_must_fail git rev-parse --verify -q TESTSYMREFONE &&
852         test_must_fail git rev-parse --verify -q TESTSYMREFTWO &&
853         git rev-parse $m~1 >expect &&
854         git rev-parse $b >actual &&
855         test_cmp expect actual
856 '
857
858 test_expect_success 'stdin delete ref works with right old value' '
859         echo "delete $b $m~1" >stdin &&
860         git update-ref --stdin <stdin &&
861         test_must_fail git rev-parse --verify -q $b
862 '
863
864 test_expect_success 'stdin update/create/verify combination works' '
865         cat >stdin <<-EOF &&
866         update $a $m
867         create $b $m
868         verify $c
869         EOF
870         git update-ref --stdin <stdin &&
871         git rev-parse $m >expect &&
872         git rev-parse $a >actual &&
873         test_cmp expect actual &&
874         git rev-parse $b >actual &&
875         test_cmp expect actual &&
876         test_must_fail git rev-parse --verify -q $c
877 '
878
879 test_expect_success 'stdin verify succeeds for correct value' '
880         git rev-parse $m >expect &&
881         echo "verify $m $m" >stdin &&
882         git update-ref --stdin <stdin &&
883         git rev-parse $m >actual &&
884         test_cmp expect actual
885 '
886
887 test_expect_success 'stdin verify succeeds for missing reference' '
888         echo "verify refs/heads/missing $Z" >stdin &&
889         git update-ref --stdin <stdin &&
890         test_must_fail git rev-parse --verify -q refs/heads/missing
891 '
892
893 test_expect_success 'stdin verify treats no value as missing' '
894         echo "verify refs/heads/missing" >stdin &&
895         git update-ref --stdin <stdin &&
896         test_must_fail git rev-parse --verify -q refs/heads/missing
897 '
898
899 test_expect_success 'stdin verify fails for wrong value' '
900         git rev-parse $m >expect &&
901         echo "verify $m $m~1" >stdin &&
902         test_must_fail git update-ref --stdin <stdin &&
903         git rev-parse $m >actual &&
904         test_cmp expect actual
905 '
906
907 test_expect_success 'stdin verify fails for mistaken null value' '
908         git rev-parse $m >expect &&
909         echo "verify $m $Z" >stdin &&
910         test_must_fail git update-ref --stdin <stdin &&
911         git rev-parse $m >actual &&
912         test_cmp expect actual
913 '
914
915 test_expect_success 'stdin verify fails for mistaken empty value' '
916         M=$(git rev-parse $m) &&
917         test_when_finished "git update-ref $m $M" &&
918         git rev-parse $m >expect &&
919         echo "verify $m" >stdin &&
920         test_must_fail git update-ref --stdin <stdin &&
921         git rev-parse $m >actual &&
922         test_cmp expect actual
923 '
924
925 test_expect_success 'stdin update refs works with identity updates' '
926         cat >stdin <<-EOF &&
927         update $a $m $m
928         update $b $m $m
929         update $c $Z $E
930         EOF
931         git update-ref --stdin <stdin &&
932         git rev-parse $m >expect &&
933         git rev-parse $a >actual &&
934         test_cmp expect actual &&
935         git rev-parse $b >actual &&
936         test_cmp expect actual &&
937         test_must_fail git rev-parse --verify -q $c
938 '
939
940 test_expect_success 'stdin update refs fails with wrong old value' '
941         git update-ref $c $m &&
942         cat >stdin <<-EOF &&
943         update $a $m $m
944         update $b $m $m
945         update $c  ''
946         EOF
947         test_must_fail git update-ref --stdin <stdin 2>err &&
948         grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
949         git rev-parse $m >expect &&
950         git rev-parse $a >actual &&
951         test_cmp expect actual &&
952         git rev-parse $b >actual &&
953         test_cmp expect actual &&
954         git rev-parse $c >actual &&
955         test_cmp expect actual
956 '
957
958 test_expect_success 'stdin delete refs works with packed and loose refs' '
959         git pack-refs --all &&
960         git update-ref $c $m~1 &&
961         cat >stdin <<-EOF &&
962         delete $a $m
963         update $b $Z $m
964         update $c $E $m~1
965         EOF
966         git update-ref --stdin <stdin &&
967         test_must_fail git rev-parse --verify -q $a &&
968         test_must_fail git rev-parse --verify -q $b &&
969         test_must_fail git rev-parse --verify -q $c
970 '
971
972 test_expect_success 'stdin -z works on empty input' '
973         >stdin &&
974         git update-ref -z --stdin <stdin &&
975         git rev-parse --verify -q $m
976 '
977
978 test_expect_success 'stdin -z fails on empty line' '
979         echo "" >stdin &&
980         test_must_fail git update-ref -z --stdin <stdin 2>err &&
981         grep "fatal: whitespace before command: " err
982 '
983
984 test_expect_success 'stdin -z fails on empty command' '
985         printf $F "" >stdin &&
986         test_must_fail git update-ref -z --stdin <stdin 2>err &&
987         grep "fatal: empty command in input" err
988 '
989
990 test_expect_success 'stdin -z fails on only whitespace' '
991         printf $F " " >stdin &&
992         test_must_fail git update-ref -z --stdin <stdin 2>err &&
993         grep "fatal: whitespace before command:  " err
994 '
995
996 test_expect_success 'stdin -z fails on leading whitespace' '
997         printf $F " create $a" "$m" >stdin &&
998         test_must_fail git update-ref -z --stdin <stdin 2>err &&
999         grep "fatal: whitespace before command:  create $a" err
1000 '
1001
1002 test_expect_success 'stdin -z fails on unknown command' '
1003         printf $F "unknown $a" >stdin &&
1004         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1005         grep "fatal: unknown command: unknown $a" err
1006 '
1007
1008 test_expect_success 'stdin -z fails create with no ref' '
1009         printf $F "create " >stdin &&
1010         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1011         grep "fatal: create: missing <ref>" err
1012 '
1013
1014 test_expect_success 'stdin -z fails create with no new value' '
1015         printf $F "create $a" >stdin &&
1016         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1017         grep "fatal: create $a: unexpected end of input when reading <newvalue>" err
1018 '
1019
1020 test_expect_success 'stdin -z fails create with too many arguments' '
1021         printf $F "create $a" "$m" "$m" >stdin &&
1022         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1023         grep "fatal: unknown command: $m" err
1024 '
1025
1026 test_expect_success 'stdin -z fails update with no ref' '
1027         printf $F "update " >stdin &&
1028         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1029         grep "fatal: update: missing <ref>" err
1030 '
1031
1032 test_expect_success 'stdin -z fails update with too few args' '
1033         printf $F "update $a" "$m" >stdin &&
1034         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1035         grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
1036 '
1037
1038 test_expect_success 'stdin -z emits warning with empty new value' '
1039         git update-ref $a $m &&
1040         printf $F "update $a" "" "" >stdin &&
1041         git update-ref -z --stdin <stdin 2>err &&
1042         grep "warning: update $a: missing <newvalue>, treating as zero" err &&
1043         test_must_fail git rev-parse --verify -q $a
1044 '
1045
1046 test_expect_success 'stdin -z fails update with no new value' '
1047         printf $F "update $a" >stdin &&
1048         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1049         grep "fatal: update $a: unexpected end of input when reading <newvalue>" err
1050 '
1051
1052 test_expect_success 'stdin -z fails update with no old value' '
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 <oldvalue>" err
1056 '
1057
1058 test_expect_success 'stdin -z fails update with too many arguments' '
1059         printf $F "update $a" "$m" "$m" "$m" >stdin &&
1060         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1061         grep "fatal: unknown command: $m" err
1062 '
1063
1064 test_expect_success 'stdin -z fails delete with no ref' '
1065         printf $F "delete " >stdin &&
1066         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1067         grep "fatal: delete: missing <ref>" err
1068 '
1069
1070 test_expect_success 'stdin -z fails delete with no old value' '
1071         printf $F "delete $a" >stdin &&
1072         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1073         grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err
1074 '
1075
1076 test_expect_success 'stdin -z fails delete with too many arguments' '
1077         printf $F "delete $a" "$m" "$m" >stdin &&
1078         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1079         grep "fatal: unknown command: $m" err
1080 '
1081
1082 test_expect_success 'stdin -z fails verify with too many arguments' '
1083         printf $F "verify $a" "$m" "$m" >stdin &&
1084         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1085         grep "fatal: unknown command: $m" err
1086 '
1087
1088 test_expect_success 'stdin -z fails verify with no old value' '
1089         printf $F "verify $a" >stdin &&
1090         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1091         grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err
1092 '
1093
1094 test_expect_success 'stdin -z fails option with unknown name' '
1095         printf $F "option unknown" >stdin &&
1096         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1097         grep "fatal: option unknown: unknown" err
1098 '
1099
1100 test_expect_success 'stdin -z fails with duplicate refs' '
1101         printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
1102         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1103         test_i18ngrep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
1104 '
1105
1106 test_expect_success 'stdin -z create ref works' '
1107         printf $F "create $a" "$m" >stdin &&
1108         git update-ref -z --stdin <stdin &&
1109         git rev-parse $m >expect &&
1110         git rev-parse $a >actual &&
1111         test_cmp expect actual
1112 '
1113
1114 test_expect_success 'stdin -z update ref creates with zero old value' '
1115         printf $F "update $b" "$m" "$Z" >stdin &&
1116         git update-ref -z --stdin <stdin &&
1117         git rev-parse $m >expect &&
1118         git rev-parse $b >actual &&
1119         test_cmp expect actual &&
1120         git update-ref -d $b
1121 '
1122
1123 test_expect_success 'stdin -z update ref creates with empty old value' '
1124         printf $F "update $b" "$m" "" >stdin &&
1125         git update-ref -z --stdin <stdin &&
1126         git rev-parse $m >expect &&
1127         git rev-parse $b >actual &&
1128         test_cmp expect actual
1129 '
1130
1131 test_expect_success 'stdin -z create ref works with path with space to blob' '
1132         printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
1133         git update-ref -z --stdin <stdin &&
1134         git rev-parse "$m:$pws" >expect &&
1135         git rev-parse refs/blobs/pws >actual &&
1136         test_cmp expect actual &&
1137         git update-ref -d refs/blobs/pws
1138 '
1139
1140 test_expect_success 'stdin -z update ref fails with wrong old value' '
1141         printf $F "update $c" "$m" "$m~1" >stdin &&
1142         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1143         grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1144         test_must_fail git rev-parse --verify -q $c
1145 '
1146
1147 test_expect_success 'stdin -z update ref fails with bad old value' '
1148         printf $F "update $c" "$m" "does-not-exist" >stdin &&
1149         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1150         grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
1151         test_must_fail git rev-parse --verify -q $c
1152 '
1153
1154 test_expect_success 'stdin -z create ref fails when ref exists' '
1155         git update-ref $c $m &&
1156         git rev-parse "$c" >expect &&
1157         printf $F "create $c" "$m~1" >stdin &&
1158         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1159         grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1160         git rev-parse "$c" >actual &&
1161         test_cmp expect actual
1162 '
1163
1164 test_expect_success 'stdin -z create ref fails with bad new value' '
1165         git update-ref -d "$c" &&
1166         printf $F "create $c" "does-not-exist" >stdin &&
1167         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1168         grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
1169         test_must_fail git rev-parse --verify -q $c
1170 '
1171
1172 test_expect_success 'stdin -z create ref fails with empty new value' '
1173         printf $F "create $c" "" >stdin &&
1174         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1175         grep "fatal: create $c: missing <newvalue>" err &&
1176         test_must_fail git rev-parse --verify -q $c
1177 '
1178
1179 test_expect_success 'stdin -z update ref works with right old value' '
1180         printf $F "update $b" "$m~1" "$m" >stdin &&
1181         git update-ref -z --stdin <stdin &&
1182         git rev-parse $m~1 >expect &&
1183         git rev-parse $b >actual &&
1184         test_cmp expect actual
1185 '
1186
1187 test_expect_success 'stdin -z delete ref fails with wrong old value' '
1188         printf $F "delete $a" "$m~1" >stdin &&
1189         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1190         grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
1191         git rev-parse $m >expect &&
1192         git rev-parse $a >actual &&
1193         test_cmp expect actual
1194 '
1195
1196 test_expect_success 'stdin -z delete ref fails with zero old value' '
1197         printf $F "delete $a" "$Z" >stdin &&
1198         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1199         grep "fatal: delete $a: zero <oldvalue>" err &&
1200         git rev-parse $m >expect &&
1201         git rev-parse $a >actual &&
1202         test_cmp expect actual
1203 '
1204
1205 test_expect_success 'stdin -z update symref works option no-deref' '
1206         git symbolic-ref TESTSYMREF $b &&
1207         printf $F "option no-deref" "update TESTSYMREF" "$a" "$b" >stdin &&
1208         git update-ref -z --stdin <stdin &&
1209         git rev-parse TESTSYMREF >expect &&
1210         git rev-parse $a >actual &&
1211         test_cmp expect actual &&
1212         git rev-parse $m~1 >expect &&
1213         git rev-parse $b >actual &&
1214         test_cmp expect actual
1215 '
1216
1217 test_expect_success 'stdin -z delete symref works option no-deref' '
1218         git symbolic-ref TESTSYMREF $b &&
1219         printf $F "option no-deref" "delete TESTSYMREF" "$b" >stdin &&
1220         git update-ref -z --stdin <stdin &&
1221         test_must_fail git rev-parse --verify -q TESTSYMREF &&
1222         git rev-parse $m~1 >expect &&
1223         git rev-parse $b >actual &&
1224         test_cmp expect actual
1225 '
1226
1227 test_expect_success 'stdin -z delete ref works with right old value' '
1228         printf $F "delete $b" "$m~1" >stdin &&
1229         git update-ref -z --stdin <stdin &&
1230         test_must_fail git rev-parse --verify -q $b
1231 '
1232
1233 test_expect_success 'stdin -z update/create/verify combination works' '
1234         printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin &&
1235         git update-ref -z --stdin <stdin &&
1236         git rev-parse $m >expect &&
1237         git rev-parse $a >actual &&
1238         test_cmp expect actual &&
1239         git rev-parse $b >actual &&
1240         test_cmp expect actual &&
1241         test_must_fail git rev-parse --verify -q $c
1242 '
1243
1244 test_expect_success 'stdin -z verify succeeds for correct value' '
1245         git rev-parse $m >expect &&
1246         printf $F "verify $m" "$m" >stdin &&
1247         git update-ref -z --stdin <stdin &&
1248         git rev-parse $m >actual &&
1249         test_cmp expect actual
1250 '
1251
1252 test_expect_success 'stdin -z verify succeeds for missing reference' '
1253         printf $F "verify refs/heads/missing" "$Z" >stdin &&
1254         git update-ref -z --stdin <stdin &&
1255         test_must_fail git rev-parse --verify -q refs/heads/missing
1256 '
1257
1258 test_expect_success 'stdin -z verify treats no value as missing' '
1259         printf $F "verify refs/heads/missing" "" >stdin &&
1260         git update-ref -z --stdin <stdin &&
1261         test_must_fail git rev-parse --verify -q refs/heads/missing
1262 '
1263
1264 test_expect_success 'stdin -z verify fails for wrong value' '
1265         git rev-parse $m >expect &&
1266         printf $F "verify $m" "$m~1" >stdin &&
1267         test_must_fail git update-ref -z --stdin <stdin &&
1268         git rev-parse $m >actual &&
1269         test_cmp expect actual
1270 '
1271
1272 test_expect_success 'stdin -z verify fails for mistaken null value' '
1273         git rev-parse $m >expect &&
1274         printf $F "verify $m" "$Z" >stdin &&
1275         test_must_fail git update-ref -z --stdin <stdin &&
1276         git rev-parse $m >actual &&
1277         test_cmp expect actual
1278 '
1279
1280 test_expect_success 'stdin -z verify fails for mistaken empty value' '
1281         M=$(git rev-parse $m) &&
1282         test_when_finished "git update-ref $m $M" &&
1283         git rev-parse $m >expect &&
1284         printf $F "verify $m" "" >stdin &&
1285         test_must_fail git update-ref -z --stdin <stdin &&
1286         git rev-parse $m >actual &&
1287         test_cmp expect actual
1288 '
1289
1290 test_expect_success 'stdin -z update refs works with identity updates' '
1291         printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
1292         git update-ref -z --stdin <stdin &&
1293         git rev-parse $m >expect &&
1294         git rev-parse $a >actual &&
1295         test_cmp expect actual &&
1296         git rev-parse $b >actual &&
1297         test_cmp expect actual &&
1298         test_must_fail git rev-parse --verify -q $c
1299 '
1300
1301 test_expect_success 'stdin -z update refs fails with wrong old value' '
1302         git update-ref $c $m &&
1303         printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
1304         test_must_fail git update-ref -z --stdin <stdin 2>err &&
1305         grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1306         git rev-parse $m >expect &&
1307         git rev-parse $a >actual &&
1308         test_cmp expect actual &&
1309         git rev-parse $b >actual &&
1310         test_cmp expect actual &&
1311         git rev-parse $c >actual &&
1312         test_cmp expect actual
1313 '
1314
1315 test_expect_success 'stdin -z delete refs works with packed and loose refs' '
1316         git pack-refs --all &&
1317         git update-ref $c $m~1 &&
1318         printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
1319         git update-ref -z --stdin <stdin &&
1320         test_must_fail git rev-parse --verify -q $a &&
1321         test_must_fail git rev-parse --verify -q $b &&
1322         test_must_fail git rev-parse --verify -q $c
1323 '
1324
1325 test_expect_success 'fails with duplicate HEAD update' '
1326         git branch target1 $A &&
1327         git checkout target1 &&
1328         cat >stdin <<-EOF &&
1329         update refs/heads/target1 $C
1330         option no-deref
1331         update HEAD $B
1332         EOF
1333         test_must_fail git update-ref --stdin <stdin 2>err &&
1334         test_i18ngrep "fatal: multiple updates for '\''HEAD'\'' (including one via its referent .refs/heads/target1.) are not allowed" err &&
1335         echo "refs/heads/target1" >expect &&
1336         git symbolic-ref HEAD >actual &&
1337         test_cmp expect actual &&
1338         echo "$A" >expect &&
1339         git rev-parse refs/heads/target1 >actual &&
1340         test_cmp expect actual
1341 '
1342
1343 test_expect_success 'fails with duplicate ref update via symref' '
1344         git branch target2 $A &&
1345         git symbolic-ref refs/heads/symref2 refs/heads/target2 &&
1346         cat >stdin <<-EOF &&
1347         update refs/heads/target2 $C
1348         update refs/heads/symref2 $B
1349         EOF
1350         test_must_fail git update-ref --stdin <stdin 2>err &&
1351         test_i18ngrep "fatal: multiple updates for '\''refs/heads/target2'\'' (including one via symref .refs/heads/symref2.) are not allowed" err &&
1352         echo "refs/heads/target2" >expect &&
1353         git symbolic-ref refs/heads/symref2 >actual &&
1354         test_cmp expect actual &&
1355         echo "$A" >expect &&
1356         git rev-parse refs/heads/target2 >actual &&
1357         test_cmp expect actual
1358 '
1359
1360 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
1361 (
1362         for i in $(test_seq 33)
1363         do
1364                 echo "create refs/heads/$i HEAD"
1365         done >large_input &&
1366         run_with_limited_open_files git update-ref --stdin <large_input &&
1367         git rev-parse --verify -q refs/heads/33
1368 )
1369 '
1370
1371 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
1372 (
1373         for i in $(test_seq 33)
1374         do
1375                 echo "delete refs/heads/$i HEAD"
1376         done >large_input &&
1377         run_with_limited_open_files git update-ref --stdin <large_input &&
1378         test_must_fail git rev-parse --verify -q refs/heads/33
1379 )
1380 '
1381
1382 test_expect_success 'handle per-worktree refs in refs/bisect' '
1383         git commit --allow-empty -m "initial commit" &&
1384         git worktree add -b branch worktree &&
1385         (
1386                 cd worktree &&
1387                 git commit --allow-empty -m "test commit"  &&
1388                 git for-each-ref >for-each-ref.out &&
1389                 ! grep refs/bisect for-each-ref.out &&
1390                 git update-ref refs/bisect/something HEAD &&
1391                 git rev-parse refs/bisect/something >../worktree-head &&
1392                 git for-each-ref | grep refs/bisect/something
1393         ) &&
1394         test_path_is_missing .git/refs/bisect &&
1395         test_must_fail git rev-parse refs/bisect/something &&
1396         git update-ref refs/bisect/something HEAD &&
1397         git rev-parse refs/bisect/something >main-head &&
1398         ! test_cmp main-head worktree-head
1399 '
1400
1401 test_expect_success 'transaction handles empty commit' '
1402         cat >stdin <<-EOF &&
1403         start
1404         prepare
1405         commit
1406         EOF
1407         git update-ref --stdin <stdin >actual &&
1408         printf "%s: ok\n" start prepare commit >expect &&
1409         test_cmp expect actual
1410 '
1411
1412 test_expect_success 'transaction handles empty commit with missing prepare' '
1413         cat >stdin <<-EOF &&
1414         start
1415         commit
1416         EOF
1417         git update-ref --stdin <stdin >actual &&
1418         printf "%s: ok\n" start commit >expect &&
1419         test_cmp expect actual
1420 '
1421
1422 test_expect_success 'transaction handles sole commit' '
1423         cat >stdin <<-EOF &&
1424         commit
1425         EOF
1426         git update-ref --stdin <stdin >actual &&
1427         printf "%s: ok\n" commit >expect &&
1428         test_cmp expect actual
1429 '
1430
1431 test_expect_success 'transaction handles empty abort' '
1432         cat >stdin <<-EOF &&
1433         start
1434         prepare
1435         abort
1436         EOF
1437         git update-ref --stdin <stdin >actual &&
1438         printf "%s: ok\n" start prepare abort >expect &&
1439         test_cmp expect actual
1440 '
1441
1442 test_expect_success 'transaction exits on multiple aborts' '
1443         cat >stdin <<-EOF &&
1444         abort
1445         abort
1446         EOF
1447         test_must_fail git update-ref --stdin <stdin >actual 2>err &&
1448         printf "%s: ok\n" abort >expect &&
1449         test_cmp expect actual &&
1450         grep "fatal: transaction is closed" err
1451 '
1452
1453 test_expect_success 'transaction exits on start after prepare' '
1454         cat >stdin <<-EOF &&
1455         prepare
1456         start
1457         EOF
1458         test_must_fail git update-ref --stdin <stdin 2>err >actual &&
1459         printf "%s: ok\n" prepare >expect &&
1460         test_cmp expect actual &&
1461         grep "fatal: prepared transactions can only be closed" err
1462 '
1463
1464 test_expect_success 'transaction handles empty abort with missing prepare' '
1465         cat >stdin <<-EOF &&
1466         start
1467         abort
1468         EOF
1469         git update-ref --stdin <stdin >actual &&
1470         printf "%s: ok\n" start abort >expect &&
1471         test_cmp expect actual
1472 '
1473
1474 test_expect_success 'transaction handles sole abort' '
1475         cat >stdin <<-EOF &&
1476         abort
1477         EOF
1478         git update-ref --stdin <stdin >actual &&
1479         printf "%s: ok\n" abort >expect &&
1480         test_cmp expect actual
1481 '
1482
1483 test_expect_success 'transaction can handle commit' '
1484         cat >stdin <<-EOF &&
1485         start
1486         create $a HEAD
1487         commit
1488         EOF
1489         git update-ref --stdin <stdin >actual &&
1490         printf "%s: ok\n" start commit >expect &&
1491         test_cmp expect actual &&
1492         git rev-parse HEAD >expect &&
1493         git rev-parse $a >actual &&
1494         test_cmp expect actual
1495 '
1496
1497 test_expect_success 'transaction can handle abort' '
1498         cat >stdin <<-EOF &&
1499         start
1500         create $b HEAD
1501         abort
1502         EOF
1503         git update-ref --stdin <stdin >actual &&
1504         printf "%s: ok\n" start abort >expect &&
1505         test_cmp expect actual &&
1506         test_path_is_missing .git/$b
1507 '
1508
1509 test_expect_success 'transaction aborts by default' '
1510         cat >stdin <<-EOF &&
1511         start
1512         create $b HEAD
1513         EOF
1514         git update-ref --stdin <stdin >actual &&
1515         printf "%s: ok\n" start >expect &&
1516         test_cmp expect actual &&
1517         test_path_is_missing .git/$b
1518 '
1519
1520 test_expect_success 'transaction with prepare aborts by default' '
1521         cat >stdin <<-EOF &&
1522         start
1523         create $b HEAD
1524         prepare
1525         EOF
1526         git update-ref --stdin <stdin >actual &&
1527         printf "%s: ok\n" start prepare >expect &&
1528         test_cmp expect actual &&
1529         test_path_is_missing .git/$b
1530 '
1531
1532 test_done