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