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