]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1400-update-ref.sh
Merge branch 'vd/fsck-submodule-url-test'
[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 -d $m &&
361 test-tool ref-store main delete-reflog $m &&
362
363 GIT_COMMITTER_DATE="1117150320 -0500" git update-ref $m $C &&
364 GIT_COMMITTER_DATE="1117150350 -0500" git update-ref $m $A &&
365 GIT_COMMITTER_DATE="1117150380 -0500" git update-ref $m $B &&
366 GIT_COMMITTER_DATE="1117150680 -0500" git update-ref $m $F &&
367 GIT_COMMITTER_DATE="1117150980 -0500" git update-ref $m $E &&
368 git update-ref $m $D &&
369 # Delete the last reflog entry so that the tip of m and the reflog for
370 # it disagree.
371 git reflog delete $m@{0} &&
372
373 cat >expect <<-EOF &&
374 $Z $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
375 $C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
376 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
377 $B $F $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
378 $F $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
379 EOF
380 test-tool ref-store main for-each-reflog-ent $m >actual &&
381 test_cmp expect actual
382 '
383
384 ed="Thu, 26 May 2005 18:32:00 -0500"
385 gd="Thu, 26 May 2005 18:33:00 -0500"
386 ld="Thu, 26 May 2005 18:43:00 -0500"
387 test_expect_success 'Query "main@{May 25 2005}" (before history)' '
388 test_when_finished "rm -f o e" &&
389 git rev-parse --verify "main@{May 25 2005}" >o 2>e &&
390 echo "$C" >expect &&
391 test_cmp expect o &&
392 echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
393 test_cmp expect e
394 '
395 test_expect_success 'Query main@{2005-05-25} (before history)' '
396 test_when_finished "rm -f o e" &&
397 git rev-parse --verify main@{2005-05-25} >o 2>e &&
398 echo "$C" >expect &&
399 test_cmp expect o &&
400 echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
401 test_cmp expect e
402 '
403 test_expect_success 'Query "main@{May 26 2005 23:31:59}" (1 second before history)' '
404 test_when_finished "rm -f o e" &&
405 git rev-parse --verify "main@{May 26 2005 23:31:59}" >o 2>e &&
406 echo "$C" >expect &&
407 test_cmp expect o &&
408 echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
409 test_cmp expect e
410 '
411 test_expect_success 'Query "main@{May 26 2005 23:32:00}" (exactly history start)' '
412 test_when_finished "rm -f o e" &&
413 git rev-parse --verify "main@{May 26 2005 23:32:00}" >o 2>e &&
414 echo "$C" >expect &&
415 test_cmp expect o &&
416 test_must_be_empty e
417 '
418 test_expect_success 'Query "main@{May 26 2005 23:32:30}" (first non-creation change)' '
419 test_when_finished "rm -f o e" &&
420 git rev-parse --verify "main@{May 26 2005 23:32:30}" >o 2>e &&
421 echo "$A" >expect &&
422 test_cmp expect o &&
423 test_must_be_empty e
424 '
425 test_expect_success 'Query "main@{2005-05-26 23:33:01}" (middle of history with gap)' '
426 test_when_finished "rm -f o e" &&
427 git rev-parse --verify "main@{2005-05-26 23:33:01}" >o 2>e &&
428 echo "$B" >expect &&
429 test_cmp expect o
430 '
431 test_expect_success 'Query "main@{2005-05-26 23:38:00}" (middle of history)' '
432 test_when_finished "rm -f o e" &&
433 git rev-parse --verify "main@{2005-05-26 23:38:00}" >o 2>e &&
434 echo "$F" >expect &&
435 test_cmp expect o &&
436 test_must_be_empty e
437 '
438 test_expect_success 'Query "main@{2005-05-26 23:43:00}" (exact end of history)' '
439 test_when_finished "rm -f o e" &&
440 git rev-parse --verify "main@{2005-05-26 23:43:00}" >o 2>e &&
441 echo "$E" >expect &&
442 test_cmp expect o &&
443 test_must_be_empty e
444 '
445 test_expect_success 'Query "main@{2005-05-28}" (past end of history)' '
446 test_when_finished "rm -f o e" &&
447 git rev-parse --verify "main@{2005-05-28}" >o 2>e &&
448 echo "$D" >expect &&
449 test_cmp expect o &&
450 test_grep -F "warning: log for ref $m unexpectedly ended on $ld" e
451 '
452
453 rm -f expect
454 git update-ref -d $m
455
456 test_expect_success REFFILES 'query reflog with gap' '
457 test_when_finished "git update-ref -d $m" &&
458
459 git update-ref $m $F &&
460 cat >.git/logs/$m <<-EOF &&
461 $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
462 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
463 $D $F $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
464 EOF
465
466 git rev-parse --verify "main@{2005-05-26 23:33:01}" >actual 2>stderr &&
467 echo "$B" >expect &&
468 test_cmp expect actual &&
469 test_grep -F "warning: log for ref $m has gap after $gd" stderr
470 '
471
472 test_expect_success 'creating initial files' '
473 test_when_finished rm -f M &&
474 echo TEST >F &&
475 git add F &&
476 GIT_AUTHOR_DATE="2005-05-26 23:30" \
477 GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
478 h_TEST=$(git rev-parse --verify HEAD) &&
479 echo The other day this did not work. >M &&
480 echo And then Bob told me how to fix it. >>M &&
481 echo OTHER >F &&
482 GIT_AUTHOR_DATE="2005-05-26 23:41" \
483 GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a &&
484 h_OTHER=$(git rev-parse --verify HEAD) &&
485 GIT_AUTHOR_DATE="2005-05-26 23:44" \
486 GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend &&
487 h_FIXED=$(git rev-parse --verify HEAD) &&
488 echo Merged initial commit and a later commit. >M &&
489 echo $h_TEST >.git/MERGE_HEAD &&
490 GIT_AUTHOR_DATE="2005-05-26 23:45" \
491 GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
492 h_MERGED=$(git rev-parse --verify HEAD)
493 '
494
495 cat >expect <<EOF
496 $Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add
497 $h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work.
498 $h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work.
499 $h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit.
500 EOF
501 test_expect_success 'git commit logged updates' '
502 test-tool ref-store main for-each-reflog-ent $m >actual &&
503 test_cmp expect actual
504 '
505 unset h_TEST h_OTHER h_FIXED h_MERGED
506
507 test_expect_success 'git cat-file blob main:F (expect OTHER)' '
508 test OTHER = $(git cat-file blob main:F)
509 '
510 test_expect_success 'git cat-file blob main@{2005-05-26 23:30}:F (expect TEST)' '
511 test TEST = $(git cat-file blob "main@{2005-05-26 23:30}:F")
512 '
513 test_expect_success 'git cat-file blob main@{2005-05-26 23:42}:F (expect OTHER)' '
514 test OTHER = $(git cat-file blob "main@{2005-05-26 23:42}:F")
515 '
516
517 # Test adding and deleting pseudorefs
518
519 test_expect_success 'given old value for missing pseudoref, do not create' '
520 test_must_fail git update-ref PSEUDOREF $A $B 2>err &&
521 test_must_fail git rev-parse PSEUDOREF &&
522 test_grep "unable to resolve reference" err
523 '
524
525 test_expect_success 'create pseudoref' '
526 git update-ref PSEUDOREF $A &&
527 test $A = $(git rev-parse PSEUDOREF)
528 '
529
530 test_expect_success 'overwrite pseudoref with no old value given' '
531 git update-ref PSEUDOREF $B &&
532 test $B = $(git rev-parse PSEUDOREF)
533 '
534
535 test_expect_success 'overwrite pseudoref with correct old value' '
536 git update-ref PSEUDOREF $C $B &&
537 test $C = $(git rev-parse PSEUDOREF)
538 '
539
540 test_expect_success 'do not overwrite pseudoref with wrong old value' '
541 test_must_fail git update-ref PSEUDOREF $D $E 2>err &&
542 test $C = $(git rev-parse PSEUDOREF) &&
543 test_grep "cannot lock ref.*expected" err
544 '
545
546 test_expect_success 'delete pseudoref' '
547 git update-ref -d PSEUDOREF &&
548 test_must_fail git rev-parse PSEUDOREF
549 '
550
551 test_expect_success 'do not delete pseudoref with wrong old value' '
552 git update-ref PSEUDOREF $A &&
553 test_must_fail git update-ref -d PSEUDOREF $B 2>err &&
554 test $A = $(git rev-parse PSEUDOREF) &&
555 test_grep "cannot lock ref.*expected" err
556 '
557
558 test_expect_success 'delete pseudoref with correct old value' '
559 git update-ref -d PSEUDOREF $A &&
560 test_must_fail git rev-parse PSEUDOREF
561 '
562
563 test_expect_success 'create pseudoref with old OID zero' '
564 git update-ref PSEUDOREF $A $Z &&
565 test $A = $(git rev-parse PSEUDOREF)
566 '
567
568 test_expect_success 'do not overwrite pseudoref with old OID zero' '
569 test_when_finished git update-ref -d PSEUDOREF &&
570 test_must_fail git update-ref PSEUDOREF $B $Z 2>err &&
571 test $A = $(git rev-parse PSEUDOREF) &&
572 test_grep "already exists" err
573 '
574
575 # Test --stdin
576
577 a=refs/heads/a
578 b=refs/heads/b
579 c=refs/heads/c
580 E='""'
581 F='%s\0'
582 pws='path with space'
583
584 test_expect_success 'stdin test setup' '
585 echo "$pws" >"$pws" &&
586 git add -- "$pws" &&
587 git commit -m "$pws"
588 '
589
590 test_expect_success '-z fails without --stdin' '
591 test_must_fail git update-ref -z $m $m $m 2>err &&
592 test_grep "usage: git update-ref" err
593 '
594
595 test_expect_success 'stdin works with no input' '
596 >stdin &&
597 git update-ref --stdin <stdin &&
598 git rev-parse --verify -q $m
599 '
600
601 test_expect_success 'stdin fails on empty line' '
602 echo "" >stdin &&
603 test_must_fail git update-ref --stdin <stdin 2>err &&
604 grep "fatal: empty command in input" err
605 '
606
607 test_expect_success 'stdin fails on only whitespace' '
608 echo " " >stdin &&
609 test_must_fail git update-ref --stdin <stdin 2>err &&
610 grep "fatal: whitespace before command: " err
611 '
612
613 test_expect_success 'stdin fails on leading whitespace' '
614 echo " create $a $m" >stdin &&
615 test_must_fail git update-ref --stdin <stdin 2>err &&
616 grep "fatal: whitespace before command: create $a $m" err
617 '
618
619 test_expect_success 'stdin fails on unknown command' '
620 echo "unknown $a" >stdin &&
621 test_must_fail git update-ref --stdin <stdin 2>err &&
622 grep "fatal: unknown command: unknown $a" err
623 '
624
625 test_expect_success 'stdin fails on unbalanced quotes' '
626 echo "create $a \"main" >stdin &&
627 test_must_fail git update-ref --stdin <stdin 2>err &&
628 grep "fatal: badly quoted argument: \\\"main" err
629 '
630
631 test_expect_success 'stdin fails on invalid escape' '
632 echo "create $a \"ma\zn\"" >stdin &&
633 test_must_fail git update-ref --stdin <stdin 2>err &&
634 grep "fatal: badly quoted argument: \\\"ma\\\\zn\\\"" err
635 '
636
637 test_expect_success 'stdin fails on junk after quoted argument' '
638 echo "create \"$a\"main" >stdin &&
639 test_must_fail git update-ref --stdin <stdin 2>err &&
640 grep "fatal: unexpected character after quoted argument: \\\"$a\\\"main" err
641 '
642
643 test_expect_success 'stdin fails create with no ref' '
644 echo "create " >stdin &&
645 test_must_fail git update-ref --stdin <stdin 2>err &&
646 grep "fatal: create: missing <ref>" err
647 '
648
649 test_expect_success 'stdin fails create with no new value' '
650 echo "create $a" >stdin &&
651 test_must_fail git update-ref --stdin <stdin 2>err &&
652 grep "fatal: create $a: missing <newvalue>" err
653 '
654
655 test_expect_success 'stdin fails create with too many arguments' '
656 echo "create $a $m $m" >stdin &&
657 test_must_fail git update-ref --stdin <stdin 2>err &&
658 grep "fatal: create $a: extra input: $m" err
659 '
660
661 test_expect_success 'stdin fails update with no ref' '
662 echo "update " >stdin &&
663 test_must_fail git update-ref --stdin <stdin 2>err &&
664 grep "fatal: update: missing <ref>" err
665 '
666
667 test_expect_success 'stdin fails update with no new value' '
668 echo "update $a" >stdin &&
669 test_must_fail git update-ref --stdin <stdin 2>err &&
670 grep "fatal: update $a: missing <newvalue>" err
671 '
672
673 test_expect_success 'stdin fails update with too many arguments' '
674 echo "update $a $m $m $m" >stdin &&
675 test_must_fail git update-ref --stdin <stdin 2>err &&
676 grep "fatal: update $a: extra input: $m" err
677 '
678
679 test_expect_success 'stdin fails delete with no ref' '
680 echo "delete " >stdin &&
681 test_must_fail git update-ref --stdin <stdin 2>err &&
682 grep "fatal: delete: missing <ref>" err
683 '
684
685 test_expect_success 'stdin fails delete with too many arguments' '
686 echo "delete $a $m $m" >stdin &&
687 test_must_fail git update-ref --stdin <stdin 2>err &&
688 grep "fatal: delete $a: extra input: $m" err
689 '
690
691 test_expect_success 'stdin fails verify with too many arguments' '
692 echo "verify $a $m $m" >stdin &&
693 test_must_fail git update-ref --stdin <stdin 2>err &&
694 grep "fatal: verify $a: extra input: $m" err
695 '
696
697 test_expect_success 'stdin fails option with unknown name' '
698 echo "option unknown" >stdin &&
699 test_must_fail git update-ref --stdin <stdin 2>err &&
700 grep "fatal: option unknown: unknown" err
701 '
702
703 test_expect_success 'stdin fails with duplicate refs' '
704 cat >stdin <<-EOF &&
705 create $a $m
706 create $b $m
707 create $a $m
708 EOF
709 test_must_fail git update-ref --stdin <stdin 2>err &&
710 test_grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
711 '
712
713 test_expect_success 'stdin create ref works' '
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 does not create reflogs by default' '
722 test_when_finished "git update-ref -d $outside" &&
723 echo "create $outside $m" >stdin &&
724 git update-ref --stdin <stdin &&
725 git rev-parse $m >expect &&
726 git rev-parse $outside >actual &&
727 test_cmp expect actual &&
728 test_must_fail git reflog exists $outside
729 '
730
731 test_expect_success 'stdin creates reflogs with --create-reflog' '
732 test_when_finished "git update-ref -d $outside" &&
733 echo "create $outside $m" >stdin &&
734 git update-ref --create-reflog --stdin <stdin &&
735 git rev-parse $m >expect &&
736 git rev-parse $outside >actual &&
737 test_cmp expect actual &&
738 git reflog exists $outside
739 '
740
741 test_expect_success 'stdin succeeds with quoted argument' '
742 git update-ref -d $a &&
743 echo "create $a \"$m\"" >stdin &&
744 git update-ref --stdin <stdin &&
745 git rev-parse $m >expect &&
746 git rev-parse $a >actual &&
747 test_cmp expect actual
748 '
749
750 test_expect_success 'stdin succeeds with escaped character' '
751 git update-ref -d $a &&
752 echo "create $a \"ma\\151n\"" >stdin &&
753 git update-ref --stdin <stdin &&
754 git rev-parse $m >expect &&
755 git rev-parse $a >actual &&
756 test_cmp expect actual
757 '
758
759 test_expect_success 'stdin update ref creates with zero old value' '
760 echo "update $b $m $Z" >stdin &&
761 git update-ref --stdin <stdin &&
762 git rev-parse $m >expect &&
763 git rev-parse $b >actual &&
764 test_cmp expect actual &&
765 git update-ref -d $b
766 '
767
768 test_expect_success 'stdin update ref creates with empty old value' '
769 echo "update $b $m $E" >stdin &&
770 git update-ref --stdin <stdin &&
771 git rev-parse $m >expect &&
772 git rev-parse $b >actual &&
773 test_cmp expect actual
774 '
775
776 test_expect_success 'stdin create ref works with path with space to blob' '
777 echo "create refs/blobs/pws \"$m:$pws\"" >stdin &&
778 git update-ref --stdin <stdin &&
779 git rev-parse "$m:$pws" >expect &&
780 git rev-parse refs/blobs/pws >actual &&
781 test_cmp expect actual &&
782 git update-ref -d refs/blobs/pws
783 '
784
785 test_expect_success 'stdin update ref fails with wrong old value' '
786 echo "update $c $m $m~1" >stdin &&
787 test_must_fail git update-ref --stdin <stdin 2>err &&
788 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
789 test_must_fail git rev-parse --verify -q $c
790 '
791
792 test_expect_success 'stdin update ref fails with bad old value' '
793 echo "update $c $m does-not-exist" >stdin &&
794 test_must_fail git update-ref --stdin <stdin 2>err &&
795 grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
796 test_must_fail git rev-parse --verify -q $c
797 '
798
799 test_expect_success 'stdin create ref fails with bad new value' '
800 echo "create $c does-not-exist" >stdin &&
801 test_must_fail git update-ref --stdin <stdin 2>err &&
802 grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
803 test_must_fail git rev-parse --verify -q $c
804 '
805
806 test_expect_success 'stdin create ref fails with zero new value' '
807 echo "create $c " >stdin &&
808 test_must_fail git update-ref --stdin <stdin 2>err &&
809 grep "fatal: create $c: zero <newvalue>" err &&
810 test_must_fail git rev-parse --verify -q $c
811 '
812
813 test_expect_success 'stdin update ref works with right old value' '
814 echo "update $b $m~1 $m" >stdin &&
815 git update-ref --stdin <stdin &&
816 git rev-parse $m~1 >expect &&
817 git rev-parse $b >actual &&
818 test_cmp expect actual
819 '
820
821 test_expect_success 'stdin delete ref fails with wrong old value' '
822 echo "delete $a $m~1" >stdin &&
823 test_must_fail git update-ref --stdin <stdin 2>err &&
824 grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
825 git rev-parse $m >expect &&
826 git rev-parse $a >actual &&
827 test_cmp expect actual
828 '
829
830 test_expect_success 'stdin delete ref fails with zero old value' '
831 echo "delete $a " >stdin &&
832 test_must_fail git update-ref --stdin <stdin 2>err &&
833 grep "fatal: delete $a: zero <oldvalue>" err &&
834 git rev-parse $m >expect &&
835 git rev-parse $a >actual &&
836 test_cmp expect actual
837 '
838
839 test_expect_success 'stdin update symref works option no-deref' '
840 git symbolic-ref TESTSYMREF $b &&
841 cat >stdin <<-EOF &&
842 option no-deref
843 update TESTSYMREF $a $b
844 EOF
845 git update-ref --stdin <stdin &&
846 git rev-parse TESTSYMREF >expect &&
847 git rev-parse $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 option no-deref' '
855 git symbolic-ref TESTSYMREF $b &&
856 cat >stdin <<-EOF &&
857 option no-deref
858 delete TESTSYMREF $b
859 EOF
860 git update-ref --stdin <stdin &&
861 test_must_fail git rev-parse --verify -q TESTSYMREF &&
862 git rev-parse $m~1 >expect &&
863 git rev-parse $b >actual &&
864 test_cmp expect actual
865 '
866
867 test_expect_success 'stdin update symref works flag --no-deref' '
868 git symbolic-ref TESTSYMREFONE $b &&
869 git symbolic-ref TESTSYMREFTWO $b &&
870 cat >stdin <<-EOF &&
871 update TESTSYMREFONE $a $b
872 update TESTSYMREFTWO $a $b
873 EOF
874 git update-ref --no-deref --stdin <stdin &&
875 git rev-parse TESTSYMREFONE TESTSYMREFTWO >expect &&
876 git rev-parse $a $a >actual &&
877 test_cmp expect actual &&
878 git rev-parse $m~1 >expect &&
879 git rev-parse $b >actual &&
880 test_cmp expect actual
881 '
882
883 test_expect_success 'stdin delete symref works flag --no-deref' '
884 git symbolic-ref TESTSYMREFONE $b &&
885 git symbolic-ref TESTSYMREFTWO $b &&
886 cat >stdin <<-EOF &&
887 delete TESTSYMREFONE $b
888 delete TESTSYMREFTWO $b
889 EOF
890 git update-ref --no-deref --stdin <stdin &&
891 test_must_fail git rev-parse --verify -q TESTSYMREFONE &&
892 test_must_fail git rev-parse --verify -q TESTSYMREFTWO &&
893 git rev-parse $m~1 >expect &&
894 git rev-parse $b >actual &&
895 test_cmp expect actual
896 '
897
898 test_expect_success 'stdin delete ref works with right old value' '
899 echo "delete $b $m~1" >stdin &&
900 git update-ref --stdin <stdin &&
901 test_must_fail git rev-parse --verify -q $b
902 '
903
904 test_expect_success 'stdin update/create/verify combination works' '
905 cat >stdin <<-EOF &&
906 update $a $m
907 create $b $m
908 verify $c
909 EOF
910 git update-ref --stdin <stdin &&
911 git rev-parse $m >expect &&
912 git rev-parse $a >actual &&
913 test_cmp expect actual &&
914 git rev-parse $b >actual &&
915 test_cmp expect actual &&
916 test_must_fail git rev-parse --verify -q $c
917 '
918
919 test_expect_success 'stdin verify succeeds for correct value' '
920 git rev-parse $m >expect &&
921 echo "verify $m $m" >stdin &&
922 git update-ref --stdin <stdin &&
923 git rev-parse $m >actual &&
924 test_cmp expect actual
925 '
926
927 test_expect_success 'stdin verify succeeds for missing reference' '
928 echo "verify refs/heads/missing $Z" >stdin &&
929 git update-ref --stdin <stdin &&
930 test_must_fail git rev-parse --verify -q refs/heads/missing
931 '
932
933 test_expect_success 'stdin verify treats no value as missing' '
934 echo "verify refs/heads/missing" >stdin &&
935 git update-ref --stdin <stdin &&
936 test_must_fail git rev-parse --verify -q refs/heads/missing
937 '
938
939 test_expect_success 'stdin verify fails for wrong value' '
940 git rev-parse $m >expect &&
941 echo "verify $m $m~1" >stdin &&
942 test_must_fail git update-ref --stdin <stdin &&
943 git rev-parse $m >actual &&
944 test_cmp expect actual
945 '
946
947 test_expect_success 'stdin verify fails for mistaken null value' '
948 git rev-parse $m >expect &&
949 echo "verify $m $Z" >stdin &&
950 test_must_fail git update-ref --stdin <stdin &&
951 git rev-parse $m >actual &&
952 test_cmp expect actual
953 '
954
955 test_expect_success 'stdin verify fails for mistaken empty value' '
956 M=$(git rev-parse $m) &&
957 test_when_finished "git update-ref $m $M" &&
958 git rev-parse $m >expect &&
959 echo "verify $m" >stdin &&
960 test_must_fail git update-ref --stdin <stdin &&
961 git rev-parse $m >actual &&
962 test_cmp expect actual
963 '
964
965 test_expect_success 'stdin update refs works with identity updates' '
966 cat >stdin <<-EOF &&
967 update $a $m $m
968 update $b $m $m
969 update $c $Z $E
970 EOF
971 git update-ref --stdin <stdin &&
972 git rev-parse $m >expect &&
973 git rev-parse $a >actual &&
974 test_cmp expect actual &&
975 git rev-parse $b >actual &&
976 test_cmp expect actual &&
977 test_must_fail git rev-parse --verify -q $c
978 '
979
980 test_expect_success 'stdin update refs fails with wrong old value' '
981 git update-ref $c $m &&
982 cat >stdin <<-EOF &&
983 update $a $m $m
984 update $b $m $m
985 update $c ''
986 EOF
987 test_must_fail git update-ref --stdin <stdin 2>err &&
988 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
989 git rev-parse $m >expect &&
990 git rev-parse $a >actual &&
991 test_cmp expect actual &&
992 git rev-parse $b >actual &&
993 test_cmp expect actual &&
994 git rev-parse $c >actual &&
995 test_cmp expect actual
996 '
997
998 test_expect_success 'stdin delete refs works with packed and loose refs' '
999 git pack-refs --all &&
1000 git update-ref $c $m~1 &&
1001 cat >stdin <<-EOF &&
1002 delete $a $m
1003 update $b $Z $m
1004 update $c $E $m~1
1005 EOF
1006 git update-ref --stdin <stdin &&
1007 test_must_fail git rev-parse --verify -q $a &&
1008 test_must_fail git rev-parse --verify -q $b &&
1009 test_must_fail git rev-parse --verify -q $c
1010 '
1011
1012 test_expect_success 'stdin -z works on empty input' '
1013 >stdin &&
1014 git update-ref -z --stdin <stdin &&
1015 git rev-parse --verify -q $m
1016 '
1017
1018 test_expect_success 'stdin -z fails on empty line' '
1019 echo "" >stdin &&
1020 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1021 grep "fatal: whitespace before command: " err
1022 '
1023
1024 test_expect_success 'stdin -z fails on empty command' '
1025 printf $F "" >stdin &&
1026 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1027 grep "fatal: empty command in input" err
1028 '
1029
1030 test_expect_success 'stdin -z fails on only whitespace' '
1031 printf $F " " >stdin &&
1032 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1033 grep "fatal: whitespace before command: " err
1034 '
1035
1036 test_expect_success 'stdin -z fails on leading whitespace' '
1037 printf $F " create $a" "$m" >stdin &&
1038 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1039 grep "fatal: whitespace before command: create $a" err
1040 '
1041
1042 test_expect_success 'stdin -z fails on unknown command' '
1043 printf $F "unknown $a" >stdin &&
1044 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1045 grep "fatal: unknown command: unknown $a" err
1046 '
1047
1048 test_expect_success 'stdin -z fails create with no ref' '
1049 printf $F "create " >stdin &&
1050 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1051 grep "fatal: create: missing <ref>" err
1052 '
1053
1054 test_expect_success 'stdin -z fails create with no new value' '
1055 printf $F "create $a" >stdin &&
1056 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1057 grep "fatal: create $a: unexpected end of input when reading <newvalue>" err
1058 '
1059
1060 test_expect_success 'stdin -z fails create with too many arguments' '
1061 printf $F "create $a" "$m" "$m" >stdin &&
1062 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1063 grep "fatal: unknown command: $m" err
1064 '
1065
1066 test_expect_success 'stdin -z fails update with no ref' '
1067 printf $F "update " >stdin &&
1068 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1069 grep "fatal: update: missing <ref>" err
1070 '
1071
1072 test_expect_success 'stdin -z fails update with too few args' '
1073 printf $F "update $a" "$m" >stdin &&
1074 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1075 grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
1076 '
1077
1078 test_expect_success 'stdin -z emits warning with empty new value' '
1079 git update-ref $a $m &&
1080 printf $F "update $a" "" "" >stdin &&
1081 git update-ref -z --stdin <stdin 2>err &&
1082 grep "warning: update $a: missing <newvalue>, treating as zero" err &&
1083 test_must_fail git rev-parse --verify -q $a
1084 '
1085
1086 test_expect_success 'stdin -z fails update with no new value' '
1087 printf $F "update $a" >stdin &&
1088 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1089 grep "fatal: update $a: unexpected end of input when reading <newvalue>" err
1090 '
1091
1092 test_expect_success 'stdin -z fails update with no old value' '
1093 printf $F "update $a" "$m" >stdin &&
1094 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1095 grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
1096 '
1097
1098 test_expect_success 'stdin -z fails update with too many arguments' '
1099 printf $F "update $a" "$m" "$m" "$m" >stdin &&
1100 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1101 grep "fatal: unknown command: $m" err
1102 '
1103
1104 test_expect_success 'stdin -z fails delete with no ref' '
1105 printf $F "delete " >stdin &&
1106 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1107 grep "fatal: delete: missing <ref>" err
1108 '
1109
1110 test_expect_success 'stdin -z fails delete with no old value' '
1111 printf $F "delete $a" >stdin &&
1112 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1113 grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err
1114 '
1115
1116 test_expect_success 'stdin -z fails delete with too many arguments' '
1117 printf $F "delete $a" "$m" "$m" >stdin &&
1118 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1119 grep "fatal: unknown command: $m" err
1120 '
1121
1122 test_expect_success 'stdin -z fails verify with too many arguments' '
1123 printf $F "verify $a" "$m" "$m" >stdin &&
1124 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1125 grep "fatal: unknown command: $m" err
1126 '
1127
1128 test_expect_success 'stdin -z fails verify with no old value' '
1129 printf $F "verify $a" >stdin &&
1130 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1131 grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err
1132 '
1133
1134 test_expect_success 'stdin -z fails option with unknown name' '
1135 printf $F "option unknown" >stdin &&
1136 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1137 grep "fatal: option unknown: unknown" err
1138 '
1139
1140 test_expect_success 'stdin -z fails with duplicate refs' '
1141 printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
1142 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1143 test_grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
1144 '
1145
1146 test_expect_success 'stdin -z create ref works' '
1147 printf $F "create $a" "$m" >stdin &&
1148 git update-ref -z --stdin <stdin &&
1149 git rev-parse $m >expect &&
1150 git rev-parse $a >actual &&
1151 test_cmp expect actual
1152 '
1153
1154 test_expect_success 'stdin -z update ref creates with zero old value' '
1155 printf $F "update $b" "$m" "$Z" >stdin &&
1156 git update-ref -z --stdin <stdin &&
1157 git rev-parse $m >expect &&
1158 git rev-parse $b >actual &&
1159 test_cmp expect actual &&
1160 git update-ref -d $b
1161 '
1162
1163 test_expect_success 'stdin -z update ref creates with empty old value' '
1164 printf $F "update $b" "$m" "" >stdin &&
1165 git update-ref -z --stdin <stdin &&
1166 git rev-parse $m >expect &&
1167 git rev-parse $b >actual &&
1168 test_cmp expect actual
1169 '
1170
1171 test_expect_success 'stdin -z create ref works with path with space to blob' '
1172 printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
1173 git update-ref -z --stdin <stdin &&
1174 git rev-parse "$m:$pws" >expect &&
1175 git rev-parse refs/blobs/pws >actual &&
1176 test_cmp expect actual &&
1177 git update-ref -d refs/blobs/pws
1178 '
1179
1180 test_expect_success 'stdin -z update ref fails with wrong old value' '
1181 printf $F "update $c" "$m" "$m~1" >stdin &&
1182 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1183 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1184 test_must_fail git rev-parse --verify -q $c
1185 '
1186
1187 test_expect_success 'stdin -z update ref fails with bad old value' '
1188 printf $F "update $c" "$m" "does-not-exist" >stdin &&
1189 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1190 grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
1191 test_must_fail git rev-parse --verify -q $c
1192 '
1193
1194 test_expect_success 'stdin -z create ref fails when ref exists' '
1195 git update-ref $c $m &&
1196 git rev-parse "$c" >expect &&
1197 printf $F "create $c" "$m~1" >stdin &&
1198 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1199 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1200 git rev-parse "$c" >actual &&
1201 test_cmp expect actual
1202 '
1203
1204 test_expect_success 'stdin -z create ref fails with bad new value' '
1205 git update-ref -d "$c" &&
1206 printf $F "create $c" "does-not-exist" >stdin &&
1207 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1208 grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
1209 test_must_fail git rev-parse --verify -q $c
1210 '
1211
1212 test_expect_success 'stdin -z create ref fails with empty new value' '
1213 printf $F "create $c" "" >stdin &&
1214 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1215 grep "fatal: create $c: missing <newvalue>" err &&
1216 test_must_fail git rev-parse --verify -q $c
1217 '
1218
1219 test_expect_success 'stdin -z update ref works with right old value' '
1220 printf $F "update $b" "$m~1" "$m" >stdin &&
1221 git update-ref -z --stdin <stdin &&
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 fails with wrong old value' '
1228 printf $F "delete $a" "$m~1" >stdin &&
1229 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1230 grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
1231 git rev-parse $m >expect &&
1232 git rev-parse $a >actual &&
1233 test_cmp expect actual
1234 '
1235
1236 test_expect_success 'stdin -z delete ref fails with zero old value' '
1237 printf $F "delete $a" "$Z" >stdin &&
1238 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1239 grep "fatal: delete $a: zero <oldvalue>" err &&
1240 git rev-parse $m >expect &&
1241 git rev-parse $a >actual &&
1242 test_cmp expect actual
1243 '
1244
1245 test_expect_success 'stdin -z update symref works option no-deref' '
1246 git symbolic-ref TESTSYMREF $b &&
1247 printf $F "option no-deref" "update TESTSYMREF" "$a" "$b" >stdin &&
1248 git update-ref -z --stdin <stdin &&
1249 git rev-parse TESTSYMREF >expect &&
1250 git rev-parse $a >actual &&
1251 test_cmp expect actual &&
1252 git rev-parse $m~1 >expect &&
1253 git rev-parse $b >actual &&
1254 test_cmp expect actual
1255 '
1256
1257 test_expect_success 'stdin -z delete symref works option no-deref' '
1258 git symbolic-ref TESTSYMREF $b &&
1259 printf $F "option no-deref" "delete TESTSYMREF" "$b" >stdin &&
1260 git update-ref -z --stdin <stdin &&
1261 test_must_fail git rev-parse --verify -q TESTSYMREF &&
1262 git rev-parse $m~1 >expect &&
1263 git rev-parse $b >actual &&
1264 test_cmp expect actual
1265 '
1266
1267 test_expect_success 'stdin -z delete ref works with right old value' '
1268 printf $F "delete $b" "$m~1" >stdin &&
1269 git update-ref -z --stdin <stdin &&
1270 test_must_fail git rev-parse --verify -q $b
1271 '
1272
1273 test_expect_success 'stdin -z update/create/verify combination works' '
1274 printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin &&
1275 git update-ref -z --stdin <stdin &&
1276 git rev-parse $m >expect &&
1277 git rev-parse $a >actual &&
1278 test_cmp expect actual &&
1279 git rev-parse $b >actual &&
1280 test_cmp expect actual &&
1281 test_must_fail git rev-parse --verify -q $c
1282 '
1283
1284 test_expect_success 'stdin -z verify succeeds for correct value' '
1285 git rev-parse $m >expect &&
1286 printf $F "verify $m" "$m" >stdin &&
1287 git update-ref -z --stdin <stdin &&
1288 git rev-parse $m >actual &&
1289 test_cmp expect actual
1290 '
1291
1292 test_expect_success 'stdin -z verify succeeds for missing reference' '
1293 printf $F "verify refs/heads/missing" "$Z" >stdin &&
1294 git update-ref -z --stdin <stdin &&
1295 test_must_fail git rev-parse --verify -q refs/heads/missing
1296 '
1297
1298 test_expect_success 'stdin -z verify treats no value as missing' '
1299 printf $F "verify refs/heads/missing" "" >stdin &&
1300 git update-ref -z --stdin <stdin &&
1301 test_must_fail git rev-parse --verify -q refs/heads/missing
1302 '
1303
1304 test_expect_success 'stdin -z verify fails for wrong value' '
1305 git rev-parse $m >expect &&
1306 printf $F "verify $m" "$m~1" >stdin &&
1307 test_must_fail git update-ref -z --stdin <stdin &&
1308 git rev-parse $m >actual &&
1309 test_cmp expect actual
1310 '
1311
1312 test_expect_success 'stdin -z verify fails for mistaken null value' '
1313 git rev-parse $m >expect &&
1314 printf $F "verify $m" "$Z" >stdin &&
1315 test_must_fail git update-ref -z --stdin <stdin &&
1316 git rev-parse $m >actual &&
1317 test_cmp expect actual
1318 '
1319
1320 test_expect_success 'stdin -z verify fails for mistaken empty value' '
1321 M=$(git rev-parse $m) &&
1322 test_when_finished "git update-ref $m $M" &&
1323 git rev-parse $m >expect &&
1324 printf $F "verify $m" "" >stdin &&
1325 test_must_fail git update-ref -z --stdin <stdin &&
1326 git rev-parse $m >actual &&
1327 test_cmp expect actual
1328 '
1329
1330 test_expect_success 'stdin -z update refs works with identity updates' '
1331 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
1332 git update-ref -z --stdin <stdin &&
1333 git rev-parse $m >expect &&
1334 git rev-parse $a >actual &&
1335 test_cmp expect actual &&
1336 git rev-parse $b >actual &&
1337 test_cmp expect actual &&
1338 test_must_fail git rev-parse --verify -q $c
1339 '
1340
1341 test_expect_success 'stdin -z update refs fails with wrong old value' '
1342 git update-ref $c $m &&
1343 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
1344 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1345 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
1346 git rev-parse $m >expect &&
1347 git rev-parse $a >actual &&
1348 test_cmp expect actual &&
1349 git rev-parse $b >actual &&
1350 test_cmp expect actual &&
1351 git rev-parse $c >actual &&
1352 test_cmp expect actual
1353 '
1354
1355 test_expect_success 'stdin -z delete refs works with packed and loose refs' '
1356 git pack-refs --all &&
1357 git update-ref $c $m~1 &&
1358 printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
1359 git update-ref -z --stdin <stdin &&
1360 test_must_fail git rev-parse --verify -q $a &&
1361 test_must_fail git rev-parse --verify -q $b &&
1362 test_must_fail git rev-parse --verify -q $c
1363 '
1364
1365 test_expect_success 'fails with duplicate HEAD update' '
1366 git branch target1 $A &&
1367 git checkout target1 &&
1368 cat >stdin <<-EOF &&
1369 update refs/heads/target1 $C
1370 option no-deref
1371 update HEAD $B
1372 EOF
1373 test_must_fail git update-ref --stdin <stdin 2>err &&
1374 test_grep "fatal: multiple updates for '\''HEAD'\'' (including one via its referent .refs/heads/target1.) are not allowed" err &&
1375 echo "refs/heads/target1" >expect &&
1376 git symbolic-ref HEAD >actual &&
1377 test_cmp expect actual &&
1378 echo "$A" >expect &&
1379 git rev-parse refs/heads/target1 >actual &&
1380 test_cmp expect actual
1381 '
1382
1383 test_expect_success 'fails with duplicate ref update via symref' '
1384 git branch target2 $A &&
1385 git symbolic-ref refs/heads/symref2 refs/heads/target2 &&
1386 cat >stdin <<-EOF &&
1387 update refs/heads/target2 $C
1388 update refs/heads/symref2 $B
1389 EOF
1390 test_must_fail git update-ref --stdin <stdin 2>err &&
1391 test_grep "fatal: multiple updates for '\''refs/heads/target2'\'' (including one via symref .refs/heads/symref2.) are not allowed" err &&
1392 echo "refs/heads/target2" >expect &&
1393 git symbolic-ref refs/heads/symref2 >actual &&
1394 test_cmp expect actual &&
1395 echo "$A" >expect &&
1396 git rev-parse refs/heads/target2 >actual &&
1397 test_cmp expect actual
1398 '
1399
1400 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
1401 (
1402 for i in $(test_seq 33)
1403 do
1404 echo "create refs/heads/$i HEAD" || exit 1
1405 done >large_input &&
1406 run_with_limited_open_files git update-ref --stdin <large_input &&
1407 git rev-parse --verify -q refs/heads/33
1408 )
1409 '
1410
1411 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
1412 (
1413 for i in $(test_seq 33)
1414 do
1415 echo "delete refs/heads/$i HEAD" || exit 1
1416 done >large_input &&
1417 run_with_limited_open_files git update-ref --stdin <large_input &&
1418 test_must_fail git rev-parse --verify -q refs/heads/33
1419 )
1420 '
1421
1422 test_expect_success 'handle per-worktree refs in refs/bisect' '
1423 git commit --allow-empty -m "initial commit" &&
1424 git worktree add -b branch worktree &&
1425 (
1426 cd worktree &&
1427 git commit --allow-empty -m "test commit" &&
1428 git for-each-ref >for-each-ref.out &&
1429 ! grep refs/bisect for-each-ref.out &&
1430 git update-ref refs/bisect/something HEAD &&
1431 git rev-parse refs/bisect/something >../worktree-head &&
1432 git for-each-ref | grep refs/bisect/something
1433 ) &&
1434 git show-ref >actual &&
1435 ! grep 'refs/bisect' actual &&
1436 test_must_fail git rev-parse refs/bisect/something &&
1437 git update-ref refs/bisect/something HEAD &&
1438 git rev-parse refs/bisect/something >main-head &&
1439 ! test_cmp main-head worktree-head
1440 '
1441
1442 test_expect_success 'transaction handles empty commit' '
1443 cat >stdin <<-EOF &&
1444 start
1445 prepare
1446 commit
1447 EOF
1448 git update-ref --stdin <stdin >actual &&
1449 printf "%s: ok\n" start prepare commit >expect &&
1450 test_cmp expect actual
1451 '
1452
1453 test_expect_success 'transaction handles empty commit with missing prepare' '
1454 cat >stdin <<-EOF &&
1455 start
1456 commit
1457 EOF
1458 git update-ref --stdin <stdin >actual &&
1459 printf "%s: ok\n" start commit >expect &&
1460 test_cmp expect actual
1461 '
1462
1463 test_expect_success 'transaction handles sole commit' '
1464 cat >stdin <<-EOF &&
1465 commit
1466 EOF
1467 git update-ref --stdin <stdin >actual &&
1468 printf "%s: ok\n" commit >expect &&
1469 test_cmp expect actual
1470 '
1471
1472 test_expect_success 'transaction handles empty abort' '
1473 cat >stdin <<-EOF &&
1474 start
1475 prepare
1476 abort
1477 EOF
1478 git update-ref --stdin <stdin >actual &&
1479 printf "%s: ok\n" start prepare abort >expect &&
1480 test_cmp expect actual
1481 '
1482
1483 test_expect_success 'transaction exits on multiple aborts' '
1484 cat >stdin <<-EOF &&
1485 abort
1486 abort
1487 EOF
1488 test_must_fail git update-ref --stdin <stdin >actual 2>err &&
1489 printf "%s: ok\n" abort >expect &&
1490 test_cmp expect actual &&
1491 grep "fatal: transaction is closed" err
1492 '
1493
1494 test_expect_success 'transaction exits on start after prepare' '
1495 cat >stdin <<-EOF &&
1496 prepare
1497 start
1498 EOF
1499 test_must_fail git update-ref --stdin <stdin 2>err >actual &&
1500 printf "%s: ok\n" prepare >expect &&
1501 test_cmp expect actual &&
1502 grep "fatal: prepared transactions can only be closed" err
1503 '
1504
1505 test_expect_success 'transaction handles empty abort with missing prepare' '
1506 cat >stdin <<-EOF &&
1507 start
1508 abort
1509 EOF
1510 git update-ref --stdin <stdin >actual &&
1511 printf "%s: ok\n" start abort >expect &&
1512 test_cmp expect actual
1513 '
1514
1515 test_expect_success 'transaction handles sole abort' '
1516 cat >stdin <<-EOF &&
1517 abort
1518 EOF
1519 git update-ref --stdin <stdin >actual &&
1520 printf "%s: ok\n" abort >expect &&
1521 test_cmp expect actual
1522 '
1523
1524 test_expect_success 'transaction can handle commit' '
1525 cat >stdin <<-EOF &&
1526 start
1527 create $a HEAD
1528 commit
1529 EOF
1530 git update-ref --stdin <stdin >actual &&
1531 printf "%s: ok\n" start commit >expect &&
1532 test_cmp expect actual &&
1533 git rev-parse HEAD >expect &&
1534 git rev-parse $a >actual &&
1535 test_cmp expect actual
1536 '
1537
1538 test_expect_success 'transaction can handle abort' '
1539 cat >stdin <<-EOF &&
1540 start
1541 create $b HEAD
1542 abort
1543 EOF
1544 git update-ref --stdin <stdin >actual &&
1545 printf "%s: ok\n" start abort >expect &&
1546 test_cmp expect actual &&
1547 test_must_fail git show-ref --verify -q $b
1548 '
1549
1550 test_expect_success 'transaction aborts by default' '
1551 cat >stdin <<-EOF &&
1552 start
1553 create $b HEAD
1554 EOF
1555 git update-ref --stdin <stdin >actual &&
1556 printf "%s: ok\n" start >expect &&
1557 test_cmp expect actual &&
1558 test_must_fail git show-ref --verify -q $b
1559 '
1560
1561 test_expect_success 'transaction with prepare aborts by default' '
1562 cat >stdin <<-EOF &&
1563 start
1564 create $b HEAD
1565 prepare
1566 EOF
1567 git update-ref --stdin <stdin >actual &&
1568 printf "%s: ok\n" start prepare >expect &&
1569 test_cmp expect actual &&
1570 test_must_fail git show-ref --verify -q $b
1571 '
1572
1573 test_expect_success 'transaction can commit multiple times' '
1574 cat >stdin <<-EOF &&
1575 start
1576 create refs/heads/branch-1 $A
1577 commit
1578 start
1579 create refs/heads/branch-2 $B
1580 commit
1581 EOF
1582 git update-ref --stdin <stdin >actual &&
1583 printf "%s: ok\n" start commit start commit >expect &&
1584 test_cmp expect actual &&
1585 echo "$A" >expect &&
1586 git rev-parse refs/heads/branch-1 >actual &&
1587 test_cmp expect actual &&
1588 echo "$B" >expect &&
1589 git rev-parse refs/heads/branch-2 >actual &&
1590 test_cmp expect actual
1591 '
1592
1593 test_expect_success 'transaction can create and delete' '
1594 cat >stdin <<-EOF &&
1595 start
1596 create refs/heads/create-and-delete $A
1597 commit
1598 start
1599 delete refs/heads/create-and-delete $A
1600 commit
1601 EOF
1602 git update-ref --stdin <stdin >actual &&
1603 printf "%s: ok\n" start commit start commit >expect &&
1604 test_cmp expect actual &&
1605 test_must_fail git show-ref --verify refs/heads/create-and-delete
1606 '
1607
1608 test_expect_success 'transaction can commit after abort' '
1609 cat >stdin <<-EOF &&
1610 start
1611 create refs/heads/abort $A
1612 abort
1613 start
1614 create refs/heads/abort $A
1615 commit
1616 EOF
1617 git update-ref --stdin <stdin >actual &&
1618 printf "%s: ok\n" start abort start commit >expect &&
1619 echo "$A" >expect &&
1620 git rev-parse refs/heads/abort >actual &&
1621 test_cmp expect actual
1622 '
1623
1624 test_expect_success 'transaction cannot restart ongoing transaction' '
1625 cat >stdin <<-EOF &&
1626 start
1627 create refs/heads/restart $A
1628 start
1629 commit
1630 EOF
1631 test_must_fail git update-ref --stdin <stdin >actual &&
1632 printf "%s: ok\n" start >expect &&
1633 test_cmp expect actual &&
1634 test_must_fail git show-ref --verify refs/heads/restart
1635 '
1636
1637 test_expect_success PIPE 'transaction flushes status updates' '
1638 mkfifo in out &&
1639 (git update-ref --stdin <in >out &) &&
1640
1641 exec 9>in &&
1642 exec 8<out &&
1643 test_when_finished "exec 9>&-" &&
1644 test_when_finished "exec 8<&-" &&
1645
1646 echo "start" >&9 &&
1647 echo "start: ok" >expected &&
1648 read line <&8 &&
1649 echo "$line" >actual &&
1650 test_cmp expected actual &&
1651
1652 echo "create refs/heads/flush $A" >&9 &&
1653
1654 echo prepare >&9 &&
1655 echo "prepare: ok" >expected &&
1656 read line <&8 &&
1657 echo "$line" >actual &&
1658 test_cmp expected actual &&
1659
1660 # This must now fail given that we have locked the ref.
1661 test_must_fail git update-ref refs/heads/flush $B 2>stderr &&
1662 grep "fatal: update_ref failed for ref ${SQ}refs/heads/flush${SQ}: cannot lock ref" stderr &&
1663
1664 echo commit >&9 &&
1665 echo "commit: ok" >expected &&
1666 read line <&8 &&
1667 echo "$line" >actual &&
1668 test_cmp expected actual
1669 '
1670
1671 test_expect_success REFFILES 'directory not created deleting packed ref' '
1672 git branch d1/d2/r1 HEAD &&
1673 git pack-refs --all &&
1674 test_path_is_missing .git/refs/heads/d1/d2 &&
1675 git update-ref -d refs/heads/d1/d2/r1 &&
1676 test_path_is_missing .git/refs/heads/d1/d2 &&
1677 test_path_is_missing .git/refs/heads/d1
1678 '
1679
1680 test_done