t1400: use test_when_finished for cleanup
[git/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'
6de08ae6
SP
7. ./test-lib.sh
8
3749fde5 9Z=$_z40
c3b0dec5 10
b1421a43
CW
11m=refs/heads/master
12n_dir=refs/heads/gu
13n=$n_dir/fixes
14outside=refs/foo
15bare=bare-repo
c3b0dec5 16
b1421a43
CW
17create_test_commits ()
18{
19 prfx="$1"
c3b0dec5
LT
20 for name in A B C D E F
21 do
22 test_tick &&
23 T=$(git write-tree) &&
24 sha1=$(echo $name | git commit-tree $T) &&
b1421a43 25 eval $prfx$name=$sha1
c3b0dec5 26 done
b1421a43 27}
c3b0dec5 28
b1421a43
CW
29test_expect_success setup '
30 create_test_commits "" &&
31 mkdir $bare &&
32 cd $bare &&
33 git init --bare &&
34 create_test_commits "bare" &&
35 cd -
c3b0dec5
LT
36'
37
6de08ae6
SP
38test_expect_success \
39 "create $m" \
5be60078 40 "git update-ref $m $A &&
8aac4b45 41 test $A"' = $(cat .git/'"$m"')'
6de08ae6 42test_expect_success \
f950e89f 43 "create $m with oldvalue verification" \
5be60078 44 "git update-ref $m $B $A &&
8aac4b45 45 test $B"' = $(cat .git/'"$m"')'
40672a19
KW
46test_expect_success "fail to delete $m with stale ref" '
47 test_must_fail git update-ref -d $m $A &&
48 test $B = "$(cat .git/$m)"
49'
50test_expect_success "delete $m" '
2405ed07 51 test_when_finished "rm -f .git/$m" &&
40672a19 52 git update-ref -d $m $B &&
cd81af4b 53 test_path_is_missing .git/$m
40672a19 54'
6de08ae6 55
2405ed07
KM
56test_expect_success "delete $m without oldvalue verification" '
57 test_when_finished "rm -f .git/$m" &&
3fe8dce6 58 git update-ref $m $A &&
2405ed07 59 test $A = $(cat .git/$m) &&
3fe8dce6 60 git update-ref -d $m &&
cd81af4b 61 test_path_is_missing .git/$m
2405ed07 62'
3fe8dce6 63
2405ed07
KM
64test_expect_success "fail to create $n" '
65 test_when_finished "rm -f .git/$n_dir" &&
66 touch .git/$n_dir &&
67 test_must_fail git update-ref $n $A
68'
818f477c 69
6de08ae6
SP
70test_expect_success \
71 "create $m (by HEAD)" \
5be60078 72 "git update-ref HEAD $A &&
8aac4b45 73 test $A"' = $(cat .git/'"$m"')'
6de08ae6 74test_expect_success \
f950e89f 75 "create $m (by HEAD) with oldvalue verification" \
5be60078 76 "git update-ref HEAD $B $A &&
8aac4b45 77 test $B"' = $(cat .git/'"$m"')'
40672a19
KW
78test_expect_success "fail to delete $m (by HEAD) with stale ref" '
79 test_must_fail git update-ref -d HEAD $A &&
80 test $B = $(cat .git/$m)
81'
82test_expect_success "delete $m (by HEAD)" '
2405ed07 83 test_when_finished "rm -f .git/$m" &&
40672a19 84 git update-ref -d HEAD $B &&
cd81af4b 85 test_path_is_missing .git/$m
40672a19 86'
6de08ae6 87
de922669 88test_expect_success "deleting current branch adds message to HEAD's log" '
2405ed07 89 test_when_finished "rm -f .git/$m" &&
de922669
KM
90 git update-ref $m $A &&
91 git symbolic-ref HEAD $m &&
92 git update-ref -m delete-$m -d $m &&
cd81af4b 93 test_path_is_missing .git/$m &&
de922669
KM
94 grep "delete-$m$" .git/logs/HEAD
95'
de922669
KM
96
97test_expect_success "deleting by HEAD adds message to HEAD's log" '
2405ed07 98 test_when_finished "rm -f .git/$m" &&
de922669
KM
99 git update-ref $m $A &&
100 git symbolic-ref HEAD $m &&
101 git update-ref -m delete-by-head -d HEAD &&
cd81af4b 102 test_path_is_missing .git/$m &&
de922669
KM
103 grep "delete-by-head$" .git/logs/HEAD
104'
de922669 105
144c76fa
DT
106test_expect_success 'update-ref does not create reflogs by default' '
107 test_when_finished "git update-ref -d $outside" &&
108 git update-ref $outside $A &&
109 git rev-parse $A >expect &&
110 git rev-parse $outside >actual &&
111 test_cmp expect actual &&
112 test_must_fail git reflog exists $outside
113'
114
115test_expect_success 'update-ref creates reflogs with --create-reflog' '
116 test_when_finished "git update-ref -d $outside" &&
117 git update-ref --create-reflog $outside $A &&
118 git rev-parse $A >expect &&
119 git rev-parse $outside >actual &&
120 test_cmp expect actual &&
121 git reflog exists $outside
122'
123
b1421a43
CW
124test_expect_success 'creates no reflog in bare repository' '
125 git -C $bare update-ref $m $bareA &&
126 git -C $bare rev-parse $bareA >expect &&
127 git -C $bare rev-parse $m >actual &&
128 test_cmp expect actual &&
129 test_must_fail git -C $bare reflog exists $m
130'
131
132test_expect_success 'core.logAllRefUpdates=true creates reflog in bare repository' '
133 test_when_finished "git -C $bare config --unset core.logAllRefUpdates && \
134 rm $bare/logs/$m" &&
135 git -C $bare config core.logAllRefUpdates true &&
136 git -C $bare update-ref $m $bareB &&
137 git -C $bare rev-parse $bareB >expect &&
138 git -C $bare rev-parse $m >actual &&
139 test_cmp expect actual &&
140 git -C $bare reflog exists $m
141'
142
341fb286
CW
143test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' '
144 test_config core.logAllRefUpdates true &&
145 test_when_finished "git update-ref -d $outside" &&
146 git update-ref $outside $A &&
147 git rev-parse $A >expect &&
148 git rev-parse $outside >actual &&
149 test_cmp expect actual &&
150 test_must_fail git reflog exists $outside
151'
152
153test_expect_success 'core.logAllRefUpdates=always creates reflog by default' '
154 test_config core.logAllRefUpdates always &&
155 test_when_finished "git update-ref -d $outside" &&
156 git update-ref $outside $A &&
157 git rev-parse $A >expect &&
158 git rev-parse $outside >actual &&
159 test_cmp expect actual &&
160 git reflog exists $outside
161'
162
163test_expect_success 'core.logAllRefUpdates=always creates no reflog for ORIG_HEAD' '
164 test_config core.logAllRefUpdates always &&
165 git update-ref ORIG_HEAD $A &&
166 test_must_fail git reflog exists ORIG_HEAD
167'
168
169test_expect_success '--no-create-reflog overrides core.logAllRefUpdates=always' '
170 test_config core.logAllRefUpdates true &&
171 test_when_finished "git update-ref -d $outside" &&
172 git update-ref --no-create-reflog $outside $A &&
173 git rev-parse $A >expect &&
174 git rev-parse $outside >actual &&
175 test_cmp expect actual &&
176 test_must_fail git reflog exists $outside
177'
178
75c96e05
JH
179test_expect_success \
180 "create $m (by HEAD)" \
181 "git update-ref HEAD $A &&
182 test $A"' = $(cat .git/'"$m"')'
183test_expect_success \
184 "pack refs" \
185 "git pack-refs --all"
186test_expect_success \
187 "move $m (by HEAD)" \
188 "git update-ref HEAD $B $A &&
189 test $B"' = $(cat .git/'"$m"')'
b274a714 190test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" '
2405ed07 191 test_when_finished "rm -f .git/$m" &&
75c96e05
JH
192 git update-ref -d HEAD $B &&
193 ! grep "$m" .git/packed-refs &&
cd81af4b 194 test_path_is_missing .git/$m
75c96e05 195'
75c96e05 196
569740bd
MV
197cp -f .git/HEAD .git/HEAD.orig
198test_expect_success "delete symref without dereference" '
2405ed07 199 test_when_finished "cp -f .git/HEAD.orig .git/HEAD" &&
569740bd 200 git update-ref --no-deref -d HEAD &&
cd81af4b 201 test_path_is_missing .git/HEAD
569740bd 202'
569740bd 203
045a476f 204test_expect_success "delete symref without dereference when the referred ref is packed" '
2405ed07 205 test_when_finished "cp -f .git/HEAD.orig .git/HEAD" &&
045a476f
MV
206 echo foo >foo.c &&
207 git add foo.c &&
208 git commit -m foo &&
209 git pack-refs --all &&
210 git update-ref --no-deref -d HEAD &&
cd81af4b 211 test_path_is_missing .git/HEAD
045a476f 212'
2405ed07 213
045a476f
MV
214git update-ref -d $m
215
62a2d525
JN
216test_expect_success 'update-ref -d is not confused by self-reference' '
217 git symbolic-ref refs/heads/self refs/heads/self &&
218 test_when_finished "rm -f .git/refs/heads/self" &&
219 test_path_is_file .git/refs/heads/self &&
220 test_must_fail git update-ref -d refs/heads/self &&
221 test_path_is_file .git/refs/heads/self
222'
223
224test_expect_success 'update-ref --no-deref -d can delete self-reference' '
225 git symbolic-ref refs/heads/self refs/heads/self &&
226 test_when_finished "rm -f .git/refs/heads/self" &&
227 test_path_is_file .git/refs/heads/self &&
228 git update-ref --no-deref -d refs/heads/self &&
229 test_path_is_missing .git/refs/heads/self
230'
231
232test_expect_success 'update-ref --no-deref -d can delete reference to bad ref' '
233 >.git/refs/heads/bad &&
234 test_when_finished "rm -f .git/refs/heads/bad" &&
235 git symbolic-ref refs/heads/ref-to-bad refs/heads/bad &&
236 test_when_finished "rm -f .git/refs/heads/ref-to-bad" &&
237 test_path_is_file .git/refs/heads/ref-to-bad &&
238 git update-ref --no-deref -d refs/heads/ref-to-bad &&
239 test_path_is_missing .git/refs/heads/ref-to-bad
240'
241
41ac414e 242test_expect_success '(not) create HEAD with old sha1' "
d492b31c 243 test_must_fail git update-ref HEAD $A $B
41ac414e 244"
2405ed07
KM
245test_expect_success "(not) prior created .git/$m" '
246 test_when_finished "rm -f .git/$m" &&
cd81af4b 247 test_path_is_missing .git/$m
2405ed07 248'
6de08ae6
SP
249
250test_expect_success \
251 "create HEAD" \
5be60078 252 "git update-ref HEAD $A"
41ac414e 253test_expect_success '(not) change HEAD with wrong SHA1' "
d492b31c 254 test_must_fail git update-ref HEAD $B $Z
41ac414e 255"
2405ed07
KM
256test_expect_success "(not) changed .git/$m" '
257 test_when_finished "rm -f .git/$m" &&
258 ! test $B = $(cat .git/$m)
41ac414e 259'
6de08ae6 260
d0ab0584 261rm -f .git/logs/refs/heads/master
6de08ae6
SP
262test_expect_success \
263 "create $m (logged by touch)" \
3e5584ca
KM
264 'test_config core.logAllRefUpdates false &&
265 GIT_COMMITTER_DATE="2005-05-26 23:30" \
d0ab0584 266 git update-ref --create-reflog HEAD '"$A"' -m "Initial Creation" &&
8aac4b45 267 test '"$A"' = $(cat .git/'"$m"')'
6de08ae6
SP
268test_expect_success \
269 "update $m (logged by touch)" \
3e5584ca
KM
270 'test_config core.logAllRefUpdates false &&
271 GIT_COMMITTER_DATE="2005-05-26 23:31" \
5be60078 272 git update-ref HEAD'" $B $A "'-m "Switch" &&
8aac4b45 273 test '"$B"' = $(cat .git/'"$m"')'
6de08ae6
SP
274test_expect_success \
275 "set $m (logged by touch)" \
3e5584ca
KM
276 'test_config core.logAllRefUpdates false &&
277 GIT_COMMITTER_DATE="2005-05-26 23:41" \
5be60078 278 git update-ref HEAD'" $A &&
8aac4b45 279 test $A"' = $(cat .git/'"$m"')'
6de08ae6 280
44639777
MH
281test_expect_success "empty directory removal" '
282 git branch d1/d2/r1 HEAD &&
283 git branch d1/r2 HEAD &&
cd81af4b
KM
284 test_path_is_file .git/refs/heads/d1/d2/r1 &&
285 test_path_is_file .git/logs/refs/heads/d1/d2/r1 &&
44639777 286 git branch -d d1/d2/r1 &&
cd81af4b
KM
287 test_path_is_missing .git/refs/heads/d1/d2 &&
288 test_path_is_missing .git/logs/refs/heads/d1/d2 &&
289 test_path_is_file .git/refs/heads/d1/r2 &&
290 test_path_is_file .git/logs/refs/heads/d1/r2
44639777
MH
291'
292
293test_expect_success "symref empty directory removal" '
294 git branch e1/e2/r1 HEAD &&
295 git branch e1/r2 HEAD &&
296 git checkout e1/e2/r1 &&
297 test_when_finished "git checkout master" &&
cd81af4b
KM
298 test_path_is_file .git/refs/heads/e1/e2/r1 &&
299 test_path_is_file .git/logs/refs/heads/e1/e2/r1 &&
44639777 300 git update-ref -d HEAD &&
cd81af4b
KM
301 test_path_is_missing .git/refs/heads/e1/e2 &&
302 test_path_is_missing .git/logs/refs/heads/e1/e2 &&
303 test_path_is_file .git/refs/heads/e1/r2 &&
304 test_path_is_file .git/logs/refs/heads/e1/r2 &&
305 test_path_is_file .git/logs/HEAD
44639777
MH
306'
307
6de08ae6
SP
308cat >expect <<EOF
309$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation
310$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch
311$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
312EOF
2405ed07
KM
313test_expect_success "verifying $m's log (logged by touch)" '
314 test_when_finished "rm -rf .git/$m .git/logs expect" &&
315 test_cmp expect .git/logs/$m
316'
6de08ae6 317
6de08ae6
SP
318test_expect_success \
319 "create $m (logged by config)" \
3e5584ca
KM
320 'test_config core.logAllRefUpdates true &&
321 GIT_COMMITTER_DATE="2005-05-26 23:32" \
5be60078 322 git update-ref HEAD'" $A "'-m "Initial Creation" &&
8aac4b45 323 test '"$A"' = $(cat .git/'"$m"')'
6de08ae6
SP
324test_expect_success \
325 "update $m (logged by config)" \
3e5584ca
KM
326 'test_config core.logAllRefUpdates true &&
327 GIT_COMMITTER_DATE="2005-05-26 23:33" \
5be60078 328 git update-ref HEAD'" $B $A "'-m "Switch" &&
8aac4b45 329 test '"$B"' = $(cat .git/'"$m"')'
6de08ae6
SP
330test_expect_success \
331 "set $m (logged by config)" \
3e5584ca
KM
332 'test_config core.logAllRefUpdates true &&
333 GIT_COMMITTER_DATE="2005-05-26 23:43" \
5be60078 334 git update-ref HEAD '"$A &&
8aac4b45 335 test $A"' = $(cat .git/'"$m"')'
6de08ae6
SP
336
337cat >expect <<EOF
338$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation
339$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch
340$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
341EOF
342test_expect_success \
f950e89f 343 "verifying $m's log (logged by config)" \
2405ed07
KM
344 'test_when_finished "rm -f .git/$m .git/logs/$m expect" &&
345 test_cmp expect .git/logs/$m'
6de08ae6 346
5be60078 347git update-ref $m $D
e5229042 348cat >.git/logs/$m <<EOF
d1a4489a
JK
3490000000000000000000000000000000000000000 $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
350$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
e5229042
SP
351$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
352$F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
353$Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
354EOF
355
356ed="Thu, 26 May 2005 18:32:00 -0500"
357gd="Thu, 26 May 2005 18:33:00 -0500"
358ld="Thu, 26 May 2005 18:43:00 -0500"
359test_expect_success \
cce91a2c 360 'Query "master@{May 25 2005}" (before history)' \
2405ed07 361 'test_when_finished "rm -f o e" &&
5be60078 362 git rev-parse --verify "master@{May 25 2005}" >o 2>e &&
8aac4b45 363 test '"$C"' = $(cat o) &&
16d7cc90 364 test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
e5229042 365test_expect_success \
cce91a2c 366 "Query master@{2005-05-25} (before history)" \
2405ed07 367 'test_when_finished "rm -f o e" &&
5be60078 368 git rev-parse --verify master@{2005-05-25} >o 2>e &&
8aac4b45 369 test '"$C"' = $(cat o) &&
16d7cc90 370 echo test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
e5229042 371test_expect_success \
cce91a2c 372 'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \
2405ed07 373 'test_when_finished "rm -f o e" &&
5be60078 374 git rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e &&
8aac4b45 375 test '"$C"' = $(cat o) &&
16d7cc90 376 test "warning: Log for '\''master'\'' only goes back to $ed." = "$(cat e)"'
e5229042 377test_expect_success \
cce91a2c 378 'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \
2405ed07 379 'test_when_finished "rm -f o e" &&
5be60078 380 git rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e &&
d1a4489a
JK
381 test '"$C"' = $(cat o) &&
382 test "" = "$(cat e)"'
383test_expect_success \
384 'Query "master@{May 26 2005 23:32:30}" (first non-creation change)' \
2405ed07 385 'test_when_finished "rm -f o e" &&
d1a4489a 386 git rev-parse --verify "master@{May 26 2005 23:32:30}" >o 2>e &&
8aac4b45 387 test '"$A"' = $(cat o) &&
e5229042
SP
388 test "" = "$(cat e)"'
389test_expect_success \
cce91a2c 390 'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \
2405ed07 391 'test_when_finished "rm -f o e" &&
5be60078 392 git rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e &&
8aac4b45 393 test '"$B"' = $(cat o) &&
4207ed28 394 test "warning: Log for ref '"$m has gap after $gd"'." = "$(cat e)"'
e5229042 395test_expect_success \
cce91a2c 396 'Query "master@{2005-05-26 23:38:00}" (middle of history)' \
2405ed07 397 'test_when_finished "rm -f o e" &&
5be60078 398 git rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e &&
8aac4b45 399 test '"$Z"' = $(cat o) &&
e5229042
SP
400 test "" = "$(cat e)"'
401test_expect_success \
cce91a2c 402 'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \
2405ed07 403 'test_when_finished "rm -f o e" &&
5be60078 404 git rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e &&
8aac4b45 405 test '"$E"' = $(cat o) &&
e5229042
SP
406 test "" = "$(cat e)"'
407test_expect_success \
cce91a2c 408 'Query "master@{2005-05-28}" (past end of history)' \
2405ed07 409 'test_when_finished "rm -f o e" &&
5be60078 410 git rev-parse --verify "master@{2005-05-28}" >o 2>e &&
8aac4b45 411 test '"$D"' = $(cat o) &&
4207ed28 412 test "warning: Log for ref '"$m unexpectedly ended on $ld"'." = "$(cat e)"'
e5229042 413
cce91a2c
SP
414
415rm -f .git/$m .git/logs/$m expect
416
417test_expect_success \
418 'creating initial files' \
2405ed07
KM
419 'test_when_finished rm -f M &&
420 echo TEST >F &&
5be60078 421 git add F &&
9c7466fa 422 GIT_AUTHOR_DATE="2005-05-26 23:30" \
0cb0e143 423 GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
2dec68cf 424 h_TEST=$(git rev-parse --verify HEAD) &&
9c7466fa
SP
425 echo The other day this did not work. >M &&
426 echo And then Bob told me how to fix it. >>M &&
427 echo OTHER >F &&
428 GIT_AUTHOR_DATE="2005-05-26 23:41" \
0cb0e143 429 GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a &&
5be60078 430 h_OTHER=$(git rev-parse --verify HEAD) &&
a3a733e6 431 GIT_AUTHOR_DATE="2005-05-26 23:44" \
0cb0e143 432 GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend &&
5be60078 433 h_FIXED=$(git rev-parse --verify HEAD) &&
a3a733e6
SP
434 echo Merged initial commit and a later commit. >M &&
435 echo $h_TEST >.git/MERGE_HEAD &&
436 GIT_AUTHOR_DATE="2005-05-26 23:45" \
0cb0e143 437 GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
2405ed07 438 h_MERGED=$(git rev-parse --verify HEAD)'
9c7466fa
SP
439
440cat >expect <<EOF
a3a733e6 441$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add
9c7466fa 442$h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work.
a3a733e6
SP
443$h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work.
444$h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit.
9c7466fa
SP
445EOF
446test_expect_success \
0cb0e143 447 'git commit logged updates' \
188c3827 448 "test_cmp expect .git/logs/$m"
a3a733e6 449unset h_TEST h_OTHER h_FIXED h_MERGED
cce91a2c
SP
450
451test_expect_success \
5be60078
JH
452 'git cat-file blob master:F (expect OTHER)' \
453 'test OTHER = $(git cat-file blob master:F)'
cce91a2c 454test_expect_success \
5be60078
JH
455 'git cat-file blob master@{2005-05-26 23:30}:F (expect TEST)' \
456 'test TEST = $(git cat-file blob "master@{2005-05-26 23:30}:F")'
cce91a2c 457test_expect_success \
5be60078
JH
458 'git cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \
459 'test OTHER = $(git cat-file blob "master@{2005-05-26 23:42}:F")'
cce91a2c 460
c6268bc0
BK
461a=refs/heads/a
462b=refs/heads/b
463c=refs/heads/c
464E='""'
465F='%s\0'
466pws='path with space'
467
468test_expect_success 'stdin test setup' '
469 echo "$pws" >"$pws" &&
470 git add -- "$pws" &&
471 git commit -m "$pws"
472'
473
474test_expect_success '-z fails without --stdin' '
475 test_must_fail git update-ref -z $m $m $m 2>err &&
1edbaac3 476 test_i18ngrep "usage: git update-ref" err
c6268bc0
BK
477'
478
479test_expect_success 'stdin works with no input' '
480 >stdin &&
481 git update-ref --stdin <stdin &&
482 git rev-parse --verify -q $m
483'
484
485test_expect_success 'stdin fails on empty line' '
486 echo "" >stdin &&
487 test_must_fail git update-ref --stdin <stdin 2>err &&
488 grep "fatal: empty command in input" err
489'
490
491test_expect_success 'stdin fails on only whitespace' '
492 echo " " >stdin &&
493 test_must_fail git update-ref --stdin <stdin 2>err &&
494 grep "fatal: whitespace before command: " err
495'
496
497test_expect_success 'stdin fails on leading whitespace' '
498 echo " create $a $m" >stdin &&
499 test_must_fail git update-ref --stdin <stdin 2>err &&
500 grep "fatal: whitespace before command: create $a $m" err
501'
502
503test_expect_success 'stdin fails on unknown command' '
504 echo "unknown $a" >stdin &&
505 test_must_fail git update-ref --stdin <stdin 2>err &&
506 grep "fatal: unknown command: unknown $a" err
507'
508
20fcffcc 509test_expect_success 'stdin fails on unbalanced quotes' '
c6268bc0
BK
510 echo "create $a \"master" >stdin &&
511 test_must_fail git update-ref --stdin <stdin 2>err &&
512 grep "fatal: badly quoted argument: \\\"master" err
513'
514
20fcffcc
MH
515test_expect_success 'stdin fails on invalid escape' '
516 echo "create $a \"ma\zter\"" >stdin &&
517 test_must_fail git update-ref --stdin <stdin 2>err &&
518 grep "fatal: badly quoted argument: \\\"ma\\\\zter\\\"" err
519'
520
697a4151 521test_expect_success 'stdin fails on junk after quoted argument' '
c6268bc0
BK
522 echo "create \"$a\"master" >stdin &&
523 test_must_fail git update-ref --stdin <stdin 2>err &&
697a4151 524 grep "fatal: unexpected character after quoted argument: \\\"$a\\\"master" err
c6268bc0
BK
525'
526
527test_expect_success 'stdin fails create with no ref' '
528 echo "create " >stdin &&
529 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 530 grep "fatal: create: missing <ref>" err
c6268bc0
BK
531'
532
c6268bc0
BK
533test_expect_success 'stdin fails create with no new value' '
534 echo "create $a" >stdin &&
535 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 536 grep "fatal: create $a: missing <newvalue>" err
c6268bc0
BK
537'
538
539test_expect_success 'stdin fails create with too many arguments' '
540 echo "create $a $m $m" >stdin &&
541 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 542 grep "fatal: create $a: extra input: $m" err
c6268bc0
BK
543'
544
545test_expect_success 'stdin fails update with no ref' '
546 echo "update " >stdin &&
547 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 548 grep "fatal: update: missing <ref>" err
c6268bc0
BK
549'
550
c6268bc0
BK
551test_expect_success 'stdin fails update with no new value' '
552 echo "update $a" >stdin &&
553 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 554 grep "fatal: update $a: missing <newvalue>" err
c6268bc0
BK
555'
556
557test_expect_success 'stdin fails update with too many arguments' '
558 echo "update $a $m $m $m" >stdin &&
559 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 560 grep "fatal: update $a: extra input: $m" err
c6268bc0
BK
561'
562
563test_expect_success 'stdin fails delete with no ref' '
564 echo "delete " >stdin &&
565 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 566 grep "fatal: delete: missing <ref>" err
c6268bc0
BK
567'
568
c6268bc0
BK
569test_expect_success 'stdin fails delete with too many arguments' '
570 echo "delete $a $m $m" >stdin &&
571 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 572 grep "fatal: delete $a: extra input: $m" err
c6268bc0
BK
573'
574
575test_expect_success 'stdin fails verify with too many arguments' '
576 echo "verify $a $m $m" >stdin &&
577 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 578 grep "fatal: verify $a: extra input: $m" err
c6268bc0
BK
579'
580
581test_expect_success 'stdin fails option with unknown name' '
582 echo "option unknown" >stdin &&
583 test_must_fail git update-ref --stdin <stdin 2>err &&
584 grep "fatal: option unknown: unknown" err
585'
586
587test_expect_success 'stdin fails with duplicate refs' '
588 cat >stdin <<-EOF &&
589 create $a $m
590 create $b $m
591 create $a $m
592 EOF
593 test_must_fail git update-ref --stdin <stdin 2>err &&
0568c8e9 594 grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed." err
c6268bc0
BK
595'
596
597test_expect_success 'stdin create ref works' '
598 echo "create $a $m" >stdin &&
599 git update-ref --stdin <stdin &&
600 git rev-parse $m >expect &&
601 git rev-parse $a >actual &&
602 test_cmp expect actual
603'
604
144c76fa
DT
605test_expect_success 'stdin does not create reflogs by default' '
606 test_when_finished "git update-ref -d $outside" &&
607 echo "create $outside $m" >stdin &&
608 git update-ref --stdin <stdin &&
609 git rev-parse $m >expect &&
610 git rev-parse $outside >actual &&
611 test_cmp expect actual &&
612 test_must_fail git reflog exists $outside
613'
614
615test_expect_success 'stdin creates reflogs with --create-reflog' '
341fb286 616 test_when_finished "git update-ref -d $outside" &&
144c76fa
DT
617 echo "create $outside $m" >stdin &&
618 git update-ref --create-reflog --stdin <stdin &&
619 git rev-parse $m >expect &&
620 git rev-parse $outside >actual &&
621 test_cmp expect actual &&
622 git reflog exists $outside
623'
624
20fcffcc
MH
625test_expect_success 'stdin succeeds with quoted argument' '
626 git update-ref -d $a &&
627 echo "create $a \"$m\"" >stdin &&
628 git update-ref --stdin <stdin &&
629 git rev-parse $m >expect &&
630 git rev-parse $a >actual &&
631 test_cmp expect actual
632'
633
634test_expect_success 'stdin succeeds with escaped character' '
635 git update-ref -d $a &&
636 echo "create $a \"ma\\163ter\"" >stdin &&
637 git update-ref --stdin <stdin &&
638 git rev-parse $m >expect &&
639 git rev-parse $a >actual &&
640 test_cmp expect actual
641'
642
c6268bc0
BK
643test_expect_success 'stdin update ref creates with zero old value' '
644 echo "update $b $m $Z" >stdin &&
645 git update-ref --stdin <stdin &&
646 git rev-parse $m >expect &&
647 git rev-parse $b >actual &&
648 test_cmp expect actual &&
649 git update-ref -d $b
650'
651
652test_expect_success 'stdin update ref creates with empty old value' '
653 echo "update $b $m $E" >stdin &&
654 git update-ref --stdin <stdin &&
655 git rev-parse $m >expect &&
656 git rev-parse $b >actual &&
657 test_cmp expect actual
658'
659
660test_expect_success 'stdin create ref works with path with space to blob' '
661 echo "create refs/blobs/pws \"$m:$pws\"" >stdin &&
662 git update-ref --stdin <stdin &&
663 git rev-parse "$m:$pws" >expect &&
664 git rev-parse refs/blobs/pws >actual &&
665 test_cmp expect actual &&
666 git update-ref -d refs/blobs/pws
667'
668
669test_expect_success 'stdin update ref fails with wrong old value' '
670 echo "update $c $m $m~1" >stdin &&
671 test_must_fail git update-ref --stdin <stdin 2>err &&
c2e0a718 672 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
c6268bc0
BK
673 test_must_fail git rev-parse --verify -q $c
674'
675
676test_expect_success 'stdin update ref fails with bad old value' '
677 echo "update $c $m does-not-exist" >stdin &&
678 test_must_fail git update-ref --stdin <stdin 2>err &&
9255f059 679 grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
c6268bc0
BK
680 test_must_fail git rev-parse --verify -q $c
681'
682
683test_expect_success 'stdin create ref fails with bad new value' '
684 echo "create $c does-not-exist" >stdin &&
685 test_must_fail git update-ref --stdin <stdin 2>err &&
9255f059 686 grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
c6268bc0
BK
687 test_must_fail git rev-parse --verify -q $c
688'
689
690test_expect_success 'stdin create ref fails with zero new value' '
691 echo "create $c " >stdin &&
692 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 693 grep "fatal: create $c: zero <newvalue>" err &&
c6268bc0
BK
694 test_must_fail git rev-parse --verify -q $c
695'
696
697test_expect_success 'stdin update ref works with right old value' '
698 echo "update $b $m~1 $m" >stdin &&
699 git update-ref --stdin <stdin &&
700 git rev-parse $m~1 >expect &&
701 git rev-parse $b >actual &&
702 test_cmp expect actual
703'
704
705test_expect_success 'stdin delete ref fails with wrong old value' '
706 echo "delete $a $m~1" >stdin &&
707 test_must_fail git update-ref --stdin <stdin 2>err &&
c2e0a718 708 grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
c6268bc0
BK
709 git rev-parse $m >expect &&
710 git rev-parse $a >actual &&
711 test_cmp expect actual
712'
713
714test_expect_success 'stdin delete ref fails with zero old value' '
715 echo "delete $a " >stdin &&
716 test_must_fail git update-ref --stdin <stdin 2>err &&
f11b09fb 717 grep "fatal: delete $a: zero <oldvalue>" err &&
c6268bc0
BK
718 git rev-parse $m >expect &&
719 git rev-parse $a >actual &&
720 test_cmp expect actual
721'
722
723test_expect_success 'stdin update symref works option no-deref' '
724 git symbolic-ref TESTSYMREF $b &&
725 cat >stdin <<-EOF &&
726 option no-deref
727 update TESTSYMREF $a $b
728 EOF
729 git update-ref --stdin <stdin &&
730 git rev-parse TESTSYMREF >expect &&
731 git rev-parse $a >actual &&
732 test_cmp expect actual &&
733 git rev-parse $m~1 >expect &&
734 git rev-parse $b >actual &&
735 test_cmp expect actual
736'
737
738test_expect_success 'stdin delete symref works option no-deref' '
739 git symbolic-ref TESTSYMREF $b &&
740 cat >stdin <<-EOF &&
741 option no-deref
742 delete TESTSYMREF $b
743 EOF
744 git update-ref --stdin <stdin &&
745 test_must_fail git rev-parse --verify -q TESTSYMREF &&
746 git rev-parse $m~1 >expect &&
747 git rev-parse $b >actual &&
748 test_cmp expect actual
749'
750
751test_expect_success 'stdin delete ref works with right old value' '
752 echo "delete $b $m~1" >stdin &&
753 git update-ref --stdin <stdin &&
754 test_must_fail git rev-parse --verify -q $b
755'
756
757test_expect_success 'stdin update/create/verify combination works' '
758 cat >stdin <<-EOF &&
759 update $a $m
760 create $b $m
761 verify $c
762 EOF
763 git update-ref --stdin <stdin &&
764 git rev-parse $m >expect &&
765 git rev-parse $a >actual &&
766 test_cmp expect actual &&
767 git rev-parse $b >actual &&
768 test_cmp expect actual &&
769 test_must_fail git rev-parse --verify -q $c
770'
771
a46e41fc
MH
772test_expect_success 'stdin verify succeeds for correct value' '
773 git rev-parse $m >expect &&
774 echo "verify $m $m" >stdin &&
775 git update-ref --stdin <stdin &&
776 git rev-parse $m >actual &&
777 test_cmp expect actual
778'
779
780test_expect_success 'stdin verify succeeds for missing reference' '
781 echo "verify refs/heads/missing $Z" >stdin &&
782 git update-ref --stdin <stdin &&
783 test_must_fail git rev-parse --verify -q refs/heads/missing
784'
785
786test_expect_success 'stdin verify treats no value as missing' '
787 echo "verify refs/heads/missing" >stdin &&
788 git update-ref --stdin <stdin &&
789 test_must_fail git rev-parse --verify -q refs/heads/missing
790'
791
792test_expect_success 'stdin verify fails for wrong value' '
793 git rev-parse $m >expect &&
794 echo "verify $m $m~1" >stdin &&
795 test_must_fail git update-ref --stdin <stdin &&
796 git rev-parse $m >actual &&
797 test_cmp expect actual
798'
799
800test_expect_success 'stdin verify fails for mistaken null value' '
801 git rev-parse $m >expect &&
802 echo "verify $m $Z" >stdin &&
803 test_must_fail git update-ref --stdin <stdin &&
804 git rev-parse $m >actual &&
805 test_cmp expect actual
806'
807
0e729c7e 808test_expect_success 'stdin verify fails for mistaken empty value' '
a46e41fc
MH
809 M=$(git rev-parse $m) &&
810 test_when_finished "git update-ref $m $M" &&
811 git rev-parse $m >expect &&
812 echo "verify $m" >stdin &&
813 test_must_fail git update-ref --stdin <stdin &&
814 git rev-parse $m >actual &&
815 test_cmp expect actual
816'
817
c6268bc0
BK
818test_expect_success 'stdin update refs works with identity updates' '
819 cat >stdin <<-EOF &&
820 update $a $m $m
821 update $b $m $m
822 update $c $Z $E
823 EOF
824 git update-ref --stdin <stdin &&
825 git rev-parse $m >expect &&
826 git rev-parse $a >actual &&
827 test_cmp expect actual &&
828 git rev-parse $b >actual &&
829 test_cmp expect actual &&
830 test_must_fail git rev-parse --verify -q $c
831'
832
833test_expect_success 'stdin update refs fails with wrong old value' '
834 git update-ref $c $m &&
835 cat >stdin <<-EOF &&
836 update $a $m $m
837 update $b $m $m
838 update $c ''
839 EOF
840 test_must_fail git update-ref --stdin <stdin 2>err &&
c2e0a718 841 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
c6268bc0
BK
842 git rev-parse $m >expect &&
843 git rev-parse $a >actual &&
844 test_cmp expect actual &&
845 git rev-parse $b >actual &&
846 test_cmp expect actual &&
847 git rev-parse $c >actual &&
848 test_cmp expect actual
849'
850
851test_expect_success 'stdin delete refs works with packed and loose refs' '
852 git pack-refs --all &&
853 git update-ref $c $m~1 &&
854 cat >stdin <<-EOF &&
855 delete $a $m
856 update $b $Z $m
857 update $c $E $m~1
858 EOF
859 git update-ref --stdin <stdin &&
860 test_must_fail git rev-parse --verify -q $a &&
861 test_must_fail git rev-parse --verify -q $b &&
862 test_must_fail git rev-parse --verify -q $c
863'
864
865test_expect_success 'stdin -z works on empty input' '
866 >stdin &&
867 git update-ref -z --stdin <stdin &&
868 git rev-parse --verify -q $m
869'
870
871test_expect_success 'stdin -z fails on empty line' '
872 echo "" >stdin &&
873 test_must_fail git update-ref -z --stdin <stdin 2>err &&
874 grep "fatal: whitespace before command: " err
875'
876
877test_expect_success 'stdin -z fails on empty command' '
878 printf $F "" >stdin &&
879 test_must_fail git update-ref -z --stdin <stdin 2>err &&
880 grep "fatal: empty command in input" err
881'
882
883test_expect_success 'stdin -z fails on only whitespace' '
884 printf $F " " >stdin &&
885 test_must_fail git update-ref -z --stdin <stdin 2>err &&
886 grep "fatal: whitespace before command: " err
887'
888
889test_expect_success 'stdin -z fails on leading whitespace' '
890 printf $F " create $a" "$m" >stdin &&
891 test_must_fail git update-ref -z --stdin <stdin 2>err &&
892 grep "fatal: whitespace before command: create $a" err
893'
894
895test_expect_success 'stdin -z fails on unknown command' '
896 printf $F "unknown $a" >stdin &&
897 test_must_fail git update-ref -z --stdin <stdin 2>err &&
898 grep "fatal: unknown command: unknown $a" err
899'
900
901test_expect_success 'stdin -z fails create with no ref' '
902 printf $F "create " >stdin &&
903 test_must_fail git update-ref -z --stdin <stdin 2>err &&
f11b09fb 904 grep "fatal: create: missing <ref>" err
c6268bc0
BK
905'
906
c6268bc0
BK
907test_expect_success 'stdin -z fails create with no new value' '
908 printf $F "create $a" >stdin &&
909 test_must_fail git update-ref -z --stdin <stdin 2>err &&
726f6916 910 grep "fatal: create $a: unexpected end of input when reading <newvalue>" err
c6268bc0
BK
911'
912
913test_expect_success 'stdin -z fails create with too many arguments' '
914 printf $F "create $a" "$m" "$m" >stdin &&
915 test_must_fail git update-ref -z --stdin <stdin 2>err &&
916 grep "fatal: unknown command: $m" err
917'
918
919test_expect_success 'stdin -z fails update with no ref' '
920 printf $F "update " >stdin &&
921 test_must_fail git update-ref -z --stdin <stdin 2>err &&
f11b09fb 922 grep "fatal: update: missing <ref>" err
c6268bc0
BK
923'
924
ff6ee395
MH
925test_expect_success 'stdin -z fails update with too few args' '
926 printf $F "update $a" "$m" >stdin &&
927 test_must_fail git update-ref -z --stdin <stdin 2>err &&
726f6916 928 grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
c6268bc0
BK
929'
930
1fbd5049 931test_expect_success 'stdin -z emits warning with empty new value' '
191f241b
MH
932 git update-ref $a $m &&
933 printf $F "update $a" "" "" >stdin &&
1fbd5049
MH
934 git update-ref -z --stdin <stdin 2>err &&
935 grep "warning: update $a: missing <newvalue>, treating as zero" err &&
191f241b
MH
936 test_must_fail git rev-parse --verify -q $a
937'
938
c6268bc0
BK
939test_expect_success 'stdin -z fails update with no new value' '
940 printf $F "update $a" >stdin &&
941 test_must_fail git update-ref -z --stdin <stdin 2>err &&
726f6916 942 grep "fatal: update $a: unexpected end of input when reading <newvalue>" err
c6268bc0
BK
943'
944
945test_expect_success 'stdin -z fails update with no old value' '
946 printf $F "update $a" "$m" >stdin &&
947 test_must_fail git update-ref -z --stdin <stdin 2>err &&
726f6916 948 grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
c6268bc0
BK
949'
950
951test_expect_success 'stdin -z fails update with too many arguments' '
952 printf $F "update $a" "$m" "$m" "$m" >stdin &&
953 test_must_fail git update-ref -z --stdin <stdin 2>err &&
954 grep "fatal: unknown command: $m" err
955'
956
957test_expect_success 'stdin -z fails delete with no ref' '
958 printf $F "delete " >stdin &&
959 test_must_fail git update-ref -z --stdin <stdin 2>err &&
f11b09fb 960 grep "fatal: delete: missing <ref>" err
c6268bc0
BK
961'
962
c6268bc0
BK
963test_expect_success 'stdin -z fails delete with no old value' '
964 printf $F "delete $a" >stdin &&
965 test_must_fail git update-ref -z --stdin <stdin 2>err &&
726f6916 966 grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err
c6268bc0
BK
967'
968
969test_expect_success 'stdin -z fails delete with too many arguments' '
970 printf $F "delete $a" "$m" "$m" >stdin &&
971 test_must_fail git update-ref -z --stdin <stdin 2>err &&
972 grep "fatal: unknown command: $m" err
973'
974
975test_expect_success 'stdin -z fails verify with too many arguments' '
976 printf $F "verify $a" "$m" "$m" >stdin &&
977 test_must_fail git update-ref -z --stdin <stdin 2>err &&
978 grep "fatal: unknown command: $m" err
979'
980
981test_expect_success 'stdin -z fails verify with no old value' '
982 printf $F "verify $a" >stdin &&
983 test_must_fail git update-ref -z --stdin <stdin 2>err &&
726f6916 984 grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err
c6268bc0
BK
985'
986
987test_expect_success 'stdin -z fails option with unknown name' '
988 printf $F "option unknown" >stdin &&
989 test_must_fail git update-ref -z --stdin <stdin 2>err &&
990 grep "fatal: option unknown: unknown" err
991'
992
993test_expect_success 'stdin -z fails with duplicate refs' '
994 printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
995 test_must_fail git update-ref -z --stdin <stdin 2>err &&
0568c8e9 996 grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed." err
c6268bc0
BK
997'
998
999test_expect_success 'stdin -z create ref works' '
1000 printf $F "create $a" "$m" >stdin &&
1001 git update-ref -z --stdin <stdin &&
1002 git rev-parse $m >expect &&
1003 git rev-parse $a >actual &&
1004 test_cmp expect actual
1005'
1006
1007test_expect_success 'stdin -z update ref creates with zero old value' '
1008 printf $F "update $b" "$m" "$Z" >stdin &&
1009 git update-ref -z --stdin <stdin &&
1010 git rev-parse $m >expect &&
1011 git rev-parse $b >actual &&
1012 test_cmp expect actual &&
1013 git update-ref -d $b
1014'
1015
1016test_expect_success 'stdin -z update ref creates with empty old value' '
1017 printf $F "update $b" "$m" "" >stdin &&
1018 git update-ref -z --stdin <stdin &&
1019 git rev-parse $m >expect &&
1020 git rev-parse $b >actual &&
1021 test_cmp expect actual
1022'
1023
1024test_expect_success 'stdin -z create ref works with path with space to blob' '
1025 printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
1026 git update-ref -z --stdin <stdin &&
1027 git rev-parse "$m:$pws" >expect &&
1028 git rev-parse refs/blobs/pws >actual &&
1029 test_cmp expect actual &&
1030 git update-ref -d refs/blobs/pws
1031'
1032
1033test_expect_success 'stdin -z update ref fails with wrong old value' '
1034 printf $F "update $c" "$m" "$m~1" >stdin &&
1035 test_must_fail git update-ref -z --stdin <stdin 2>err &&
c2e0a718 1036 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
c6268bc0
BK
1037 test_must_fail git rev-parse --verify -q $c
1038'
1039
1040test_expect_success 'stdin -z update ref fails with bad old value' '
1041 printf $F "update $c" "$m" "does-not-exist" >stdin &&
1042 test_must_fail git update-ref -z --stdin <stdin 2>err &&
9255f059 1043 grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
c6268bc0
BK
1044 test_must_fail git rev-parse --verify -q $c
1045'
1046
b9d56b5d
AG
1047test_expect_success 'stdin -z create ref fails when ref exists' '
1048 git update-ref $c $m &&
1049 git rev-parse "$c" >expect &&
1050 printf $F "create $c" "$m~1" >stdin &&
1051 test_must_fail git update-ref -z --stdin <stdin 2>err &&
c2e0a718 1052 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
b9d56b5d
AG
1053 git rev-parse "$c" >actual &&
1054 test_cmp expect actual
1055'
1056
c6268bc0 1057test_expect_success 'stdin -z create ref fails with bad new value' '
b9d56b5d 1058 git update-ref -d "$c" &&
c6268bc0
BK
1059 printf $F "create $c" "does-not-exist" >stdin &&
1060 test_must_fail git update-ref -z --stdin <stdin 2>err &&
9255f059 1061 grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
c6268bc0
BK
1062 test_must_fail git rev-parse --verify -q $c
1063'
1064
3afcc463 1065test_expect_success 'stdin -z create ref fails with empty new value' '
c6268bc0
BK
1066 printf $F "create $c" "" >stdin &&
1067 test_must_fail git update-ref -z --stdin <stdin 2>err &&
f11b09fb 1068 grep "fatal: create $c: missing <newvalue>" err &&
c6268bc0
BK
1069 test_must_fail git rev-parse --verify -q $c
1070'
1071
1072test_expect_success 'stdin -z update ref works with right old value' '
1073 printf $F "update $b" "$m~1" "$m" >stdin &&
1074 git update-ref -z --stdin <stdin &&
1075 git rev-parse $m~1 >expect &&
1076 git rev-parse $b >actual &&
1077 test_cmp expect actual
1078'
1079
1080test_expect_success 'stdin -z delete ref fails with wrong old value' '
1081 printf $F "delete $a" "$m~1" >stdin &&
1082 test_must_fail git update-ref -z --stdin <stdin 2>err &&
c2e0a718 1083 grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
c6268bc0
BK
1084 git rev-parse $m >expect &&
1085 git rev-parse $a >actual &&
1086 test_cmp expect actual
1087'
1088
1089test_expect_success 'stdin -z delete ref fails with zero old value' '
1090 printf $F "delete $a" "$Z" >stdin &&
1091 test_must_fail git update-ref -z --stdin <stdin 2>err &&
f11b09fb 1092 grep "fatal: delete $a: zero <oldvalue>" err &&
c6268bc0
BK
1093 git rev-parse $m >expect &&
1094 git rev-parse $a >actual &&
1095 test_cmp expect actual
1096'
1097
1098test_expect_success 'stdin -z update symref works option no-deref' '
1099 git symbolic-ref TESTSYMREF $b &&
1100 printf $F "option no-deref" "update TESTSYMREF" "$a" "$b" >stdin &&
1101 git update-ref -z --stdin <stdin &&
1102 git rev-parse TESTSYMREF >expect &&
1103 git rev-parse $a >actual &&
1104 test_cmp expect actual &&
1105 git rev-parse $m~1 >expect &&
1106 git rev-parse $b >actual &&
1107 test_cmp expect actual
1108'
1109
1110test_expect_success 'stdin -z delete symref works option no-deref' '
1111 git symbolic-ref TESTSYMREF $b &&
1112 printf $F "option no-deref" "delete TESTSYMREF" "$b" >stdin &&
1113 git update-ref -z --stdin <stdin &&
1114 test_must_fail git rev-parse --verify -q TESTSYMREF &&
1115 git rev-parse $m~1 >expect &&
1116 git rev-parse $b >actual &&
1117 test_cmp expect actual
1118'
1119
1120test_expect_success 'stdin -z delete ref works with right old value' '
1121 printf $F "delete $b" "$m~1" >stdin &&
1122 git update-ref -z --stdin <stdin &&
1123 test_must_fail git rev-parse --verify -q $b
1124'
1125
1126test_expect_success 'stdin -z update/create/verify combination works' '
1127 printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >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 git rev-parse $b >actual &&
1133 test_cmp expect actual &&
1134 test_must_fail git rev-parse --verify -q $c
1135'
1136
a46e41fc
MH
1137test_expect_success 'stdin -z verify succeeds for correct value' '
1138 git rev-parse $m >expect &&
1139 printf $F "verify $m" "$m" >stdin &&
1140 git update-ref -z --stdin <stdin &&
1141 git rev-parse $m >actual &&
1142 test_cmp expect actual
1143'
1144
1145test_expect_success 'stdin -z verify succeeds for missing reference' '
1146 printf $F "verify refs/heads/missing" "$Z" >stdin &&
1147 git update-ref -z --stdin <stdin &&
1148 test_must_fail git rev-parse --verify -q refs/heads/missing
1149'
1150
1151test_expect_success 'stdin -z verify treats no value as missing' '
1152 printf $F "verify refs/heads/missing" "" >stdin &&
1153 git update-ref -z --stdin <stdin &&
1154 test_must_fail git rev-parse --verify -q refs/heads/missing
1155'
1156
1157test_expect_success 'stdin -z verify fails for wrong value' '
1158 git rev-parse $m >expect &&
1159 printf $F "verify $m" "$m~1" >stdin &&
1160 test_must_fail git update-ref -z --stdin <stdin &&
1161 git rev-parse $m >actual &&
1162 test_cmp expect actual
1163'
1164
1165test_expect_success 'stdin -z verify fails for mistaken null value' '
1166 git rev-parse $m >expect &&
1167 printf $F "verify $m" "$Z" >stdin &&
1168 test_must_fail git update-ref -z --stdin <stdin &&
1169 git rev-parse $m >actual &&
1170 test_cmp expect actual
1171'
1172
0e729c7e 1173test_expect_success 'stdin -z verify fails for mistaken empty value' '
a46e41fc
MH
1174 M=$(git rev-parse $m) &&
1175 test_when_finished "git update-ref $m $M" &&
1176 git rev-parse $m >expect &&
1177 printf $F "verify $m" "" >stdin &&
1178 test_must_fail git update-ref -z --stdin <stdin &&
1179 git rev-parse $m >actual &&
1180 test_cmp expect actual
1181'
1182
c6268bc0
BK
1183test_expect_success 'stdin -z update refs works with identity updates' '
1184 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
1185 git update-ref -z --stdin <stdin &&
1186 git rev-parse $m >expect &&
1187 git rev-parse $a >actual &&
1188 test_cmp expect actual &&
1189 git rev-parse $b >actual &&
1190 test_cmp expect actual &&
1191 test_must_fail git rev-parse --verify -q $c
1192'
1193
1194test_expect_success 'stdin -z update refs fails with wrong old value' '
1195 git update-ref $c $m &&
c1329110 1196 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
c6268bc0 1197 test_must_fail git update-ref -z --stdin <stdin 2>err &&
c2e0a718 1198 grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
c6268bc0
BK
1199 git rev-parse $m >expect &&
1200 git rev-parse $a >actual &&
1201 test_cmp expect actual &&
1202 git rev-parse $b >actual &&
1203 test_cmp expect actual &&
1204 git rev-parse $c >actual &&
1205 test_cmp expect actual
1206'
1207
1208test_expect_success 'stdin -z delete refs works with packed and loose refs' '
1209 git pack-refs --all &&
1210 git update-ref $c $m~1 &&
1211 printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
1212 git update-ref -z --stdin <stdin &&
1213 test_must_fail git rev-parse --verify -q $a &&
1214 test_must_fail git rev-parse --verify -q $b &&
1215 test_must_fail git rev-parse --verify -q $c
1216'
1217
92b1551b
MH
1218test_expect_success 'fails with duplicate HEAD update' '
1219 git branch target1 $A &&
1220 git checkout target1 &&
1221 cat >stdin <<-EOF &&
1222 update refs/heads/target1 $C
1223 option no-deref
1224 update HEAD $B
1225 EOF
1226 test_must_fail git update-ref --stdin <stdin 2>err &&
1227 grep "fatal: multiple updates for '\''HEAD'\'' (including one via its referent .refs/heads/target1.) are not allowed" err &&
1228 echo "refs/heads/target1" >expect &&
1229 git symbolic-ref HEAD >actual &&
1230 test_cmp expect actual &&
1231 echo "$A" >expect &&
1232 git rev-parse refs/heads/target1 >actual &&
1233 test_cmp expect actual
1234'
1235
1236test_expect_success 'fails with duplicate ref update via symref' '
1237 git branch target2 $A &&
1238 git symbolic-ref refs/heads/symref2 refs/heads/target2 &&
1239 cat >stdin <<-EOF &&
1240 update refs/heads/target2 $C
1241 update refs/heads/symref2 $B
1242 EOF
1243 test_must_fail git update-ref --stdin <stdin 2>err &&
1244 grep "fatal: multiple updates for '\''refs/heads/target2'\'' (including one via symref .refs/heads/symref2.) are not allowed" err &&
1245 echo "refs/heads/target2" >expect &&
1246 git symbolic-ref refs/heads/symref2 >actual &&
1247 test_cmp expect actual &&
1248 echo "$A" >expect &&
1249 git rev-parse refs/heads/target2 >actual &&
1250 test_cmp expect actual
1251'
1252
d415ad02
SB
1253run_with_limited_open_files () {
1254 (ulimit -n 32 && "$@")
1255}
1256
1257test_lazy_prereq ULIMIT_FILE_DESCRIPTORS 'run_with_limited_open_files true'
1258
cf018ee0 1259test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
d415ad02
SB
1260(
1261 for i in $(test_seq 33)
1262 do
1263 echo "create refs/heads/$i HEAD"
1264 done >large_input &&
1265 run_with_limited_open_files git update-ref --stdin <large_input &&
1266 git rev-parse --verify -q refs/heads/33
1267)
1268'
1269
cf018ee0 1270test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
d415ad02
SB
1271(
1272 for i in $(test_seq 33)
1273 do
1274 echo "delete refs/heads/$i HEAD"
1275 done >large_input &&
1276 run_with_limited_open_files git update-ref --stdin <large_input &&
1277 test_must_fail git rev-parse --verify -q refs/heads/33
1278)
1279'
1280
ce414b33
DT
1281test_expect_success 'handle per-worktree refs in refs/bisect' '
1282 git commit --allow-empty -m "initial commit" &&
1283 git worktree add -b branch worktree &&
1284 (
1285 cd worktree &&
1286 git commit --allow-empty -m "test commit" &&
1287 git for-each-ref >for-each-ref.out &&
1288 ! grep refs/bisect for-each-ref.out &&
1289 git update-ref refs/bisect/something HEAD &&
1290 git rev-parse refs/bisect/something >../worktree-head &&
1291 git for-each-ref | grep refs/bisect/something
1292 ) &&
1293 test_path_is_missing .git/refs/bisect &&
1294 test_must_fail git rev-parse refs/bisect/something &&
1295 git update-ref refs/bisect/something HEAD &&
1296 git rev-parse refs/bisect/something >main-head &&
1297 ! test_cmp main-head worktree-head
1298'
1299
6de08ae6 1300test_done