]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1400-update-ref.sh
ba89f4c00959112b2cfc0efaff94f3447df32357
[thirdparty/git.git] / t / t1400-update-ref.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Shawn Pearce
4 #
5
6 test_description='Test git update-ref and basic ref logging'
7 . ./test-lib.sh
8
9 Z=$_z40
10
11 test_expect_success setup '
12
13 for name in A B C D E F
14 do
15 test_tick &&
16 T=$(git write-tree) &&
17 sha1=$(echo $name | git commit-tree $T) &&
18 eval $name=$sha1
19 done
20
21 '
22
23 m=refs/heads/master
24 n_dir=refs/heads/gu
25 n=$n_dir/fixes
26
27 test_expect_success \
28 "create $m" \
29 "git update-ref $m $A &&
30 test $A"' = $(cat .git/'"$m"')'
31 test_expect_success \
32 "create $m" \
33 "git update-ref $m $B $A &&
34 test $B"' = $(cat .git/'"$m"')'
35 test_expect_success "fail to delete $m with stale ref" '
36 test_must_fail git update-ref -d $m $A &&
37 test $B = "$(cat .git/$m)"
38 '
39 test_expect_success "delete $m" '
40 git update-ref -d $m $B &&
41 ! test -f .git/$m
42 '
43 rm -f .git/$m
44
45 test_expect_success "delete $m without oldvalue verification" "
46 git update-ref $m $A &&
47 test $A = \$(cat .git/$m) &&
48 git update-ref -d $m &&
49 ! test -f .git/$m
50 "
51 rm -f .git/$m
52
53 test_expect_success \
54 "fail to create $n" \
55 "touch .git/$n_dir &&
56 test_must_fail git update-ref $n $A >out 2>err"
57 rm -f .git/$n_dir out err
58
59 test_expect_success \
60 "create $m (by HEAD)" \
61 "git update-ref HEAD $A &&
62 test $A"' = $(cat .git/'"$m"')'
63 test_expect_success \
64 "create $m (by HEAD)" \
65 "git update-ref HEAD $B $A &&
66 test $B"' = $(cat .git/'"$m"')'
67 test_expect_success "fail to delete $m (by HEAD) with stale ref" '
68 test_must_fail git update-ref -d HEAD $A &&
69 test $B = $(cat .git/$m)
70 '
71 test_expect_success "delete $m (by HEAD)" '
72 git update-ref -d HEAD $B &&
73 ! test -f .git/$m
74 '
75 rm -f .git/$m
76
77 test_expect_success \
78 "create $m (by HEAD)" \
79 "git update-ref HEAD $A &&
80 test $A"' = $(cat .git/'"$m"')'
81 test_expect_success \
82 "pack refs" \
83 "git pack-refs --all"
84 test_expect_success \
85 "move $m (by HEAD)" \
86 "git update-ref HEAD $B $A &&
87 test $B"' = $(cat .git/'"$m"')'
88 test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" '
89 git update-ref -d HEAD $B &&
90 ! grep "$m" .git/packed-refs &&
91 ! test -f .git/$m
92 '
93 rm -f .git/$m
94
95 cp -f .git/HEAD .git/HEAD.orig
96 test_expect_success "delete symref without dereference" '
97 git update-ref --no-deref -d HEAD &&
98 ! test -f .git/HEAD
99 '
100 cp -f .git/HEAD.orig .git/HEAD
101
102 test_expect_success "delete symref without dereference when the referred ref is packed" '
103 echo foo >foo.c &&
104 git add foo.c &&
105 git commit -m foo &&
106 git pack-refs --all &&
107 git update-ref --no-deref -d HEAD &&
108 ! test -f .git/HEAD
109 '
110 cp -f .git/HEAD.orig .git/HEAD
111 git update-ref -d $m
112
113 test_expect_success 'update-ref -d is not confused by self-reference' '
114 git symbolic-ref refs/heads/self refs/heads/self &&
115 test_when_finished "rm -f .git/refs/heads/self" &&
116 test_path_is_file .git/refs/heads/self &&
117 test_must_fail git update-ref -d refs/heads/self &&
118 test_path_is_file .git/refs/heads/self
119 '
120
121 test_expect_success 'update-ref --no-deref -d can delete self-reference' '
122 git symbolic-ref refs/heads/self refs/heads/self &&
123 test_when_finished "rm -f .git/refs/heads/self" &&
124 test_path_is_file .git/refs/heads/self &&
125 git update-ref --no-deref -d refs/heads/self &&
126 test_path_is_missing .git/refs/heads/self
127 '
128
129 test_expect_success 'update-ref --no-deref -d can delete reference to bad ref' '
130 >.git/refs/heads/bad &&
131 test_when_finished "rm -f .git/refs/heads/bad" &&
132 git symbolic-ref refs/heads/ref-to-bad refs/heads/bad &&
133 test_when_finished "rm -f .git/refs/heads/ref-to-bad" &&
134 test_path_is_file .git/refs/heads/ref-to-bad &&
135 git update-ref --no-deref -d refs/heads/ref-to-bad &&
136 test_path_is_missing .git/refs/heads/ref-to-bad
137 '
138
139 test_expect_success '(not) create HEAD with old sha1' "
140 test_must_fail git update-ref HEAD $A $B
141 "
142 test_expect_success "(not) prior created .git/$m" "
143 ! test -f .git/$m
144 "
145 rm -f .git/$m
146
147 test_expect_success \
148 "create HEAD" \
149 "git update-ref HEAD $A"
150 test_expect_success '(not) change HEAD with wrong SHA1' "
151 test_must_fail git update-ref HEAD $B $Z
152 "
153 test_expect_success "(not) changed .git/$m" "
154 ! test $B"' = $(cat .git/'"$m"')
155 '
156 rm -f .git/$m
157
158 : a repository with working tree always has reflog these days...
159 : >.git/logs/refs/heads/master
160 test_expect_success \
161 "create $m (logged by touch)" \
162 'GIT_COMMITTER_DATE="2005-05-26 23:30" \
163 git update-ref HEAD '"$A"' -m "Initial Creation" &&
164 test '"$A"' = $(cat .git/'"$m"')'
165 test_expect_success \
166 "update $m (logged by touch)" \
167 'GIT_COMMITTER_DATE="2005-05-26 23:31" \
168 git update-ref HEAD'" $B $A "'-m "Switch" &&
169 test '"$B"' = $(cat .git/'"$m"')'
170 test_expect_success \
171 "set $m (logged by touch)" \
172 'GIT_COMMITTER_DATE="2005-05-26 23:41" \
173 git update-ref HEAD'" $A &&
174 test $A"' = $(cat .git/'"$m"')'
175
176 cat >expect <<EOF
177 $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation
178 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch
179 $B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
180 EOF
181 test_expect_success \
182 "verifying $m's log" \
183 "test_cmp expect .git/logs/$m"
184 rm -rf .git/$m .git/logs expect
185
186 test_expect_success \
187 'enable core.logAllRefUpdates' \
188 'git config core.logAllRefUpdates true &&
189 test true = $(git config --bool --get core.logAllRefUpdates)'
190
191 test_expect_success \
192 "create $m (logged by config)" \
193 'GIT_COMMITTER_DATE="2005-05-26 23:32" \
194 git update-ref HEAD'" $A "'-m "Initial Creation" &&
195 test '"$A"' = $(cat .git/'"$m"')'
196 test_expect_success \
197 "update $m (logged by config)" \
198 'GIT_COMMITTER_DATE="2005-05-26 23:33" \
199 git update-ref HEAD'" $B $A "'-m "Switch" &&
200 test '"$B"' = $(cat .git/'"$m"')'
201 test_expect_success \
202 "set $m (logged by config)" \
203 'GIT_COMMITTER_DATE="2005-05-26 23:43" \
204 git update-ref HEAD '"$A &&
205 test $A"' = $(cat .git/'"$m"')'
206
207 cat >expect <<EOF
208 $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation
209 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch
210 $B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
211 EOF
212 test_expect_success \
213 "verifying $m's log" \
214 'test_cmp expect .git/logs/$m'
215 rm -f .git/$m .git/logs/$m expect
216
217 git update-ref $m $D
218 cat >.git/logs/$m <<EOF
219 0000000000000000000000000000000000000000 $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
220 $C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
221 $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
222 $F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
223 $Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
224 EOF
225
226 ed="Thu, 26 May 2005 18:32:00 -0500"
227 gd="Thu, 26 May 2005 18:33:00 -0500"
228 ld="Thu, 26 May 2005 18:43:00 -0500"
229 test_expect_success \
230 'Query "master@{May 25 2005}" (before history)' \
231 'rm -f o e &&
232 git rev-parse --verify "master@{May 25 2005}" >o 2>e &&
233 test '"$C"' = $(cat o) &&
234 test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
235 test_expect_success \
236 "Query master@{2005-05-25} (before history)" \
237 'rm -f o e &&
238 git rev-parse --verify master@{2005-05-25} >o 2>e &&
239 test '"$C"' = $(cat o) &&
240 echo test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
241 test_expect_success \
242 'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \
243 'rm -f o e &&
244 git rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e &&
245 test '"$C"' = $(cat o) &&
246 test "warning: Log for '\''master'\'' only goes back to $ed." = "$(cat e)"'
247 test_expect_success \
248 'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \
249 'rm -f o e &&
250 git rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e &&
251 test '"$C"' = $(cat o) &&
252 test "" = "$(cat e)"'
253 test_expect_success \
254 'Query "master@{May 26 2005 23:32:30}" (first non-creation change)' \
255 'rm -f o e &&
256 git rev-parse --verify "master@{May 26 2005 23:32:30}" >o 2>e &&
257 test '"$A"' = $(cat o) &&
258 test "" = "$(cat e)"'
259 test_expect_success \
260 'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \
261 'rm -f o e &&
262 git rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e &&
263 test '"$B"' = $(cat o) &&
264 test "warning: Log for ref '"$m has gap after $gd"'." = "$(cat e)"'
265 test_expect_success \
266 'Query "master@{2005-05-26 23:38:00}" (middle of history)' \
267 'rm -f o e &&
268 git rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e &&
269 test '"$Z"' = $(cat o) &&
270 test "" = "$(cat e)"'
271 test_expect_success \
272 'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \
273 'rm -f o e &&
274 git rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e &&
275 test '"$E"' = $(cat o) &&
276 test "" = "$(cat e)"'
277 test_expect_success \
278 'Query "master@{2005-05-28}" (past end of history)' \
279 'rm -f o e &&
280 git rev-parse --verify "master@{2005-05-28}" >o 2>e &&
281 test '"$D"' = $(cat o) &&
282 test "warning: Log for ref '"$m unexpectedly ended on $ld"'." = "$(cat e)"'
283
284
285 rm -f .git/$m .git/logs/$m expect
286
287 test_expect_success \
288 'creating initial files' \
289 'echo TEST >F &&
290 git add F &&
291 GIT_AUTHOR_DATE="2005-05-26 23:30" \
292 GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
293 h_TEST=$(git rev-parse --verify HEAD) &&
294 echo The other day this did not work. >M &&
295 echo And then Bob told me how to fix it. >>M &&
296 echo OTHER >F &&
297 GIT_AUTHOR_DATE="2005-05-26 23:41" \
298 GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a &&
299 h_OTHER=$(git rev-parse --verify HEAD) &&
300 GIT_AUTHOR_DATE="2005-05-26 23:44" \
301 GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend &&
302 h_FIXED=$(git rev-parse --verify HEAD) &&
303 echo Merged initial commit and a later commit. >M &&
304 echo $h_TEST >.git/MERGE_HEAD &&
305 GIT_AUTHOR_DATE="2005-05-26 23:45" \
306 GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
307 h_MERGED=$(git rev-parse --verify HEAD) &&
308 rm -f M'
309
310 cat >expect <<EOF
311 $Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add
312 $h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work.
313 $h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work.
314 $h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit.
315 EOF
316 test_expect_success \
317 'git commit logged updates' \
318 "test_cmp expect .git/logs/$m"
319 unset h_TEST h_OTHER h_FIXED h_MERGED
320
321 test_expect_success \
322 'git cat-file blob master:F (expect OTHER)' \
323 'test OTHER = $(git cat-file blob master:F)'
324 test_expect_success \
325 'git cat-file blob master@{2005-05-26 23:30}:F (expect TEST)' \
326 'test TEST = $(git cat-file blob "master@{2005-05-26 23:30}:F")'
327 test_expect_success \
328 'git cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \
329 'test OTHER = $(git cat-file blob "master@{2005-05-26 23:42}:F")'
330
331 a=refs/heads/a
332 b=refs/heads/b
333 c=refs/heads/c
334 E='""'
335 F='%s\0'
336 pws='path with space'
337
338 test_expect_success 'stdin test setup' '
339 echo "$pws" >"$pws" &&
340 git add -- "$pws" &&
341 git commit -m "$pws"
342 '
343
344 test_expect_success '-z fails without --stdin' '
345 test_must_fail git update-ref -z $m $m $m 2>err &&
346 grep "usage: git update-ref" err
347 '
348
349 test_expect_success 'stdin works with no input' '
350 >stdin &&
351 git update-ref --stdin <stdin &&
352 git rev-parse --verify -q $m
353 '
354
355 test_expect_success 'stdin fails on empty line' '
356 echo "" >stdin &&
357 test_must_fail git update-ref --stdin <stdin 2>err &&
358 grep "fatal: empty command in input" err
359 '
360
361 test_expect_success 'stdin fails on only whitespace' '
362 echo " " >stdin &&
363 test_must_fail git update-ref --stdin <stdin 2>err &&
364 grep "fatal: whitespace before command: " err
365 '
366
367 test_expect_success 'stdin fails on leading whitespace' '
368 echo " create $a $m" >stdin &&
369 test_must_fail git update-ref --stdin <stdin 2>err &&
370 grep "fatal: whitespace before command: create $a $m" err
371 '
372
373 test_expect_success 'stdin fails on unknown command' '
374 echo "unknown $a" >stdin &&
375 test_must_fail git update-ref --stdin <stdin 2>err &&
376 grep "fatal: unknown command: unknown $a" err
377 '
378
379 test_expect_success 'stdin fails on unbalanced quotes' '
380 echo "create $a \"master" >stdin &&
381 test_must_fail git update-ref --stdin <stdin 2>err &&
382 grep "fatal: badly quoted argument: \\\"master" err
383 '
384
385 test_expect_success 'stdin fails on invalid escape' '
386 echo "create $a \"ma\zter\"" >stdin &&
387 test_must_fail git update-ref --stdin <stdin 2>err &&
388 grep "fatal: badly quoted argument: \\\"ma\\\\zter\\\"" err
389 '
390
391 test_expect_success 'stdin fails on junk after quoted argument' '
392 echo "create \"$a\"master" >stdin &&
393 test_must_fail git update-ref --stdin <stdin 2>err &&
394 grep "fatal: unexpected character after quoted argument: \\\"$a\\\"master" err
395 '
396
397 test_expect_success 'stdin fails create with no ref' '
398 echo "create " >stdin &&
399 test_must_fail git update-ref --stdin <stdin 2>err &&
400 grep "fatal: create: missing <ref>" err
401 '
402
403 test_expect_success 'stdin fails create with no new value' '
404 echo "create $a" >stdin &&
405 test_must_fail git update-ref --stdin <stdin 2>err &&
406 grep "fatal: create $a: missing <newvalue>" err
407 '
408
409 test_expect_success 'stdin fails create with too many arguments' '
410 echo "create $a $m $m" >stdin &&
411 test_must_fail git update-ref --stdin <stdin 2>err &&
412 grep "fatal: create $a: extra input: $m" err
413 '
414
415 test_expect_success 'stdin fails update with no ref' '
416 echo "update " >stdin &&
417 test_must_fail git update-ref --stdin <stdin 2>err &&
418 grep "fatal: update: missing <ref>" err
419 '
420
421 test_expect_success 'stdin fails update with no new value' '
422 echo "update $a" >stdin &&
423 test_must_fail git update-ref --stdin <stdin 2>err &&
424 grep "fatal: update $a: missing <newvalue>" err
425 '
426
427 test_expect_success 'stdin fails update with too many arguments' '
428 echo "update $a $m $m $m" >stdin &&
429 test_must_fail git update-ref --stdin <stdin 2>err &&
430 grep "fatal: update $a: extra input: $m" err
431 '
432
433 test_expect_success 'stdin fails delete with no ref' '
434 echo "delete " >stdin &&
435 test_must_fail git update-ref --stdin <stdin 2>err &&
436 grep "fatal: delete: missing <ref>" err
437 '
438
439 test_expect_success 'stdin fails delete with too many arguments' '
440 echo "delete $a $m $m" >stdin &&
441 test_must_fail git update-ref --stdin <stdin 2>err &&
442 grep "fatal: delete $a: extra input: $m" err
443 '
444
445 test_expect_success 'stdin fails verify with too many arguments' '
446 echo "verify $a $m $m" >stdin &&
447 test_must_fail git update-ref --stdin <stdin 2>err &&
448 grep "fatal: verify $a: extra input: $m" err
449 '
450
451 test_expect_success 'stdin fails option with unknown name' '
452 echo "option unknown" >stdin &&
453 test_must_fail git update-ref --stdin <stdin 2>err &&
454 grep "fatal: option unknown: unknown" err
455 '
456
457 test_expect_success 'stdin fails with duplicate refs' '
458 cat >stdin <<-EOF &&
459 create $a $m
460 create $b $m
461 create $a $m
462 EOF
463 test_must_fail git update-ref --stdin <stdin 2>err &&
464 grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err
465 '
466
467 test_expect_success 'stdin create ref works' '
468 echo "create $a $m" >stdin &&
469 git update-ref --stdin <stdin &&
470 git rev-parse $m >expect &&
471 git rev-parse $a >actual &&
472 test_cmp expect actual
473 '
474
475 test_expect_success 'stdin succeeds with quoted argument' '
476 git update-ref -d $a &&
477 echo "create $a \"$m\"" >stdin &&
478 git update-ref --stdin <stdin &&
479 git rev-parse $m >expect &&
480 git rev-parse $a >actual &&
481 test_cmp expect actual
482 '
483
484 test_expect_success 'stdin succeeds with escaped character' '
485 git update-ref -d $a &&
486 echo "create $a \"ma\\163ter\"" >stdin &&
487 git update-ref --stdin <stdin &&
488 git rev-parse $m >expect &&
489 git rev-parse $a >actual &&
490 test_cmp expect actual
491 '
492
493 test_expect_success 'stdin update ref creates with zero old value' '
494 echo "update $b $m $Z" >stdin &&
495 git update-ref --stdin <stdin &&
496 git rev-parse $m >expect &&
497 git rev-parse $b >actual &&
498 test_cmp expect actual &&
499 git update-ref -d $b
500 '
501
502 test_expect_success 'stdin update ref creates with empty old value' '
503 echo "update $b $m $E" >stdin &&
504 git update-ref --stdin <stdin &&
505 git rev-parse $m >expect &&
506 git rev-parse $b >actual &&
507 test_cmp expect actual
508 '
509
510 test_expect_success 'stdin create ref works with path with space to blob' '
511 echo "create refs/blobs/pws \"$m:$pws\"" >stdin &&
512 git update-ref --stdin <stdin &&
513 git rev-parse "$m:$pws" >expect &&
514 git rev-parse refs/blobs/pws >actual &&
515 test_cmp expect actual &&
516 git update-ref -d refs/blobs/pws
517 '
518
519 test_expect_success 'stdin update ref fails with wrong old value' '
520 echo "update $c $m $m~1" >stdin &&
521 test_must_fail git update-ref --stdin <stdin 2>err &&
522 grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
523 test_must_fail git rev-parse --verify -q $c
524 '
525
526 test_expect_success 'stdin update ref fails with bad old value' '
527 echo "update $c $m does-not-exist" >stdin &&
528 test_must_fail git update-ref --stdin <stdin 2>err &&
529 grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
530 test_must_fail git rev-parse --verify -q $c
531 '
532
533 test_expect_success 'stdin create ref fails with bad new value' '
534 echo "create $c does-not-exist" >stdin &&
535 test_must_fail git update-ref --stdin <stdin 2>err &&
536 grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
537 test_must_fail git rev-parse --verify -q $c
538 '
539
540 test_expect_success 'stdin create ref fails with zero new value' '
541 echo "create $c " >stdin &&
542 test_must_fail git update-ref --stdin <stdin 2>err &&
543 grep "fatal: create $c: zero <newvalue>" err &&
544 test_must_fail git rev-parse --verify -q $c
545 '
546
547 test_expect_success 'stdin update ref works with right old value' '
548 echo "update $b $m~1 $m" >stdin &&
549 git update-ref --stdin <stdin &&
550 git rev-parse $m~1 >expect &&
551 git rev-parse $b >actual &&
552 test_cmp expect actual
553 '
554
555 test_expect_success 'stdin delete ref fails with wrong old value' '
556 echo "delete $a $m~1" >stdin &&
557 test_must_fail git update-ref --stdin <stdin 2>err &&
558 grep "fatal: Cannot lock ref '"'"'$a'"'"'" err &&
559 git rev-parse $m >expect &&
560 git rev-parse $a >actual &&
561 test_cmp expect actual
562 '
563
564 test_expect_success 'stdin delete ref fails with zero old value' '
565 echo "delete $a " >stdin &&
566 test_must_fail git update-ref --stdin <stdin 2>err &&
567 grep "fatal: delete $a: zero <oldvalue>" err &&
568 git rev-parse $m >expect &&
569 git rev-parse $a >actual &&
570 test_cmp expect actual
571 '
572
573 test_expect_success 'stdin update symref works option no-deref' '
574 git symbolic-ref TESTSYMREF $b &&
575 cat >stdin <<-EOF &&
576 option no-deref
577 update TESTSYMREF $a $b
578 EOF
579 git update-ref --stdin <stdin &&
580 git rev-parse TESTSYMREF >expect &&
581 git rev-parse $a >actual &&
582 test_cmp expect actual &&
583 git rev-parse $m~1 >expect &&
584 git rev-parse $b >actual &&
585 test_cmp expect actual
586 '
587
588 test_expect_success 'stdin delete symref works option no-deref' '
589 git symbolic-ref TESTSYMREF $b &&
590 cat >stdin <<-EOF &&
591 option no-deref
592 delete TESTSYMREF $b
593 EOF
594 git update-ref --stdin <stdin &&
595 test_must_fail git rev-parse --verify -q TESTSYMREF &&
596 git rev-parse $m~1 >expect &&
597 git rev-parse $b >actual &&
598 test_cmp expect actual
599 '
600
601 test_expect_success 'stdin delete ref works with right old value' '
602 echo "delete $b $m~1" >stdin &&
603 git update-ref --stdin <stdin &&
604 test_must_fail git rev-parse --verify -q $b
605 '
606
607 test_expect_success 'stdin update/create/verify combination works' '
608 cat >stdin <<-EOF &&
609 update $a $m
610 create $b $m
611 verify $c
612 EOF
613 git update-ref --stdin <stdin &&
614 git rev-parse $m >expect &&
615 git rev-parse $a >actual &&
616 test_cmp expect actual &&
617 git rev-parse $b >actual &&
618 test_cmp expect actual &&
619 test_must_fail git rev-parse --verify -q $c
620 '
621
622 test_expect_success 'stdin verify succeeds for correct value' '
623 git rev-parse $m >expect &&
624 echo "verify $m $m" >stdin &&
625 git update-ref --stdin <stdin &&
626 git rev-parse $m >actual &&
627 test_cmp expect actual
628 '
629
630 test_expect_success 'stdin verify succeeds for missing reference' '
631 echo "verify refs/heads/missing $Z" >stdin &&
632 git update-ref --stdin <stdin &&
633 test_must_fail git rev-parse --verify -q refs/heads/missing
634 '
635
636 test_expect_success 'stdin verify treats no value as missing' '
637 echo "verify refs/heads/missing" >stdin &&
638 git update-ref --stdin <stdin &&
639 test_must_fail git rev-parse --verify -q refs/heads/missing
640 '
641
642 test_expect_success 'stdin verify fails for wrong value' '
643 git rev-parse $m >expect &&
644 echo "verify $m $m~1" >stdin &&
645 test_must_fail git update-ref --stdin <stdin &&
646 git rev-parse $m >actual &&
647 test_cmp expect actual
648 '
649
650 test_expect_success 'stdin verify fails for mistaken null value' '
651 git rev-parse $m >expect &&
652 echo "verify $m $Z" >stdin &&
653 test_must_fail git update-ref --stdin <stdin &&
654 git rev-parse $m >actual &&
655 test_cmp expect actual
656 '
657
658 test_expect_success 'stdin verify fails for mistaken empty value' '
659 M=$(git rev-parse $m) &&
660 test_when_finished "git update-ref $m $M" &&
661 git rev-parse $m >expect &&
662 echo "verify $m" >stdin &&
663 test_must_fail git update-ref --stdin <stdin &&
664 git rev-parse $m >actual &&
665 test_cmp expect actual
666 '
667
668 test_expect_success 'stdin update refs works with identity updates' '
669 cat >stdin <<-EOF &&
670 update $a $m $m
671 update $b $m $m
672 update $c $Z $E
673 EOF
674 git update-ref --stdin <stdin &&
675 git rev-parse $m >expect &&
676 git rev-parse $a >actual &&
677 test_cmp expect actual &&
678 git rev-parse $b >actual &&
679 test_cmp expect actual &&
680 test_must_fail git rev-parse --verify -q $c
681 '
682
683 test_expect_success 'stdin update refs fails with wrong old value' '
684 git update-ref $c $m &&
685 cat >stdin <<-EOF &&
686 update $a $m $m
687 update $b $m $m
688 update $c ''
689 EOF
690 test_must_fail git update-ref --stdin <stdin 2>err &&
691 grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
692 git rev-parse $m >expect &&
693 git rev-parse $a >actual &&
694 test_cmp expect actual &&
695 git rev-parse $b >actual &&
696 test_cmp expect actual &&
697 git rev-parse $c >actual &&
698 test_cmp expect actual
699 '
700
701 test_expect_success 'stdin delete refs works with packed and loose refs' '
702 git pack-refs --all &&
703 git update-ref $c $m~1 &&
704 cat >stdin <<-EOF &&
705 delete $a $m
706 update $b $Z $m
707 update $c $E $m~1
708 EOF
709 git update-ref --stdin <stdin &&
710 test_must_fail git rev-parse --verify -q $a &&
711 test_must_fail git rev-parse --verify -q $b &&
712 test_must_fail git rev-parse --verify -q $c
713 '
714
715 test_expect_success 'stdin -z works on empty input' '
716 >stdin &&
717 git update-ref -z --stdin <stdin &&
718 git rev-parse --verify -q $m
719 '
720
721 test_expect_success 'stdin -z fails on empty line' '
722 echo "" >stdin &&
723 test_must_fail git update-ref -z --stdin <stdin 2>err &&
724 grep "fatal: whitespace before command: " err
725 '
726
727 test_expect_success 'stdin -z fails on empty command' '
728 printf $F "" >stdin &&
729 test_must_fail git update-ref -z --stdin <stdin 2>err &&
730 grep "fatal: empty command in input" err
731 '
732
733 test_expect_success 'stdin -z fails on only whitespace' '
734 printf $F " " >stdin &&
735 test_must_fail git update-ref -z --stdin <stdin 2>err &&
736 grep "fatal: whitespace before command: " err
737 '
738
739 test_expect_success 'stdin -z fails on leading whitespace' '
740 printf $F " create $a" "$m" >stdin &&
741 test_must_fail git update-ref -z --stdin <stdin 2>err &&
742 grep "fatal: whitespace before command: create $a" err
743 '
744
745 test_expect_success 'stdin -z fails on unknown command' '
746 printf $F "unknown $a" >stdin &&
747 test_must_fail git update-ref -z --stdin <stdin 2>err &&
748 grep "fatal: unknown command: unknown $a" err
749 '
750
751 test_expect_success 'stdin -z fails create with no ref' '
752 printf $F "create " >stdin &&
753 test_must_fail git update-ref -z --stdin <stdin 2>err &&
754 grep "fatal: create: missing <ref>" err
755 '
756
757 test_expect_success 'stdin -z fails create with no new value' '
758 printf $F "create $a" >stdin &&
759 test_must_fail git update-ref -z --stdin <stdin 2>err &&
760 grep "fatal: create $a: unexpected end of input when reading <newvalue>" err
761 '
762
763 test_expect_success 'stdin -z fails create with too many arguments' '
764 printf $F "create $a" "$m" "$m" >stdin &&
765 test_must_fail git update-ref -z --stdin <stdin 2>err &&
766 grep "fatal: unknown command: $m" err
767 '
768
769 test_expect_success 'stdin -z fails update with no ref' '
770 printf $F "update " >stdin &&
771 test_must_fail git update-ref -z --stdin <stdin 2>err &&
772 grep "fatal: update: missing <ref>" err
773 '
774
775 test_expect_success 'stdin -z fails update with too few args' '
776 printf $F "update $a" "$m" >stdin &&
777 test_must_fail git update-ref -z --stdin <stdin 2>err &&
778 grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
779 '
780
781 test_expect_success 'stdin -z emits warning with empty new value' '
782 git update-ref $a $m &&
783 printf $F "update $a" "" "" >stdin &&
784 git update-ref -z --stdin <stdin 2>err &&
785 grep "warning: update $a: missing <newvalue>, treating as zero" err &&
786 test_must_fail git rev-parse --verify -q $a
787 '
788
789 test_expect_success 'stdin -z fails update with no new value' '
790 printf $F "update $a" >stdin &&
791 test_must_fail git update-ref -z --stdin <stdin 2>err &&
792 grep "fatal: update $a: unexpected end of input when reading <newvalue>" err
793 '
794
795 test_expect_success 'stdin -z fails update with no old value' '
796 printf $F "update $a" "$m" >stdin &&
797 test_must_fail git update-ref -z --stdin <stdin 2>err &&
798 grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
799 '
800
801 test_expect_success 'stdin -z fails update with too many arguments' '
802 printf $F "update $a" "$m" "$m" "$m" >stdin &&
803 test_must_fail git update-ref -z --stdin <stdin 2>err &&
804 grep "fatal: unknown command: $m" err
805 '
806
807 test_expect_success 'stdin -z fails delete with no ref' '
808 printf $F "delete " >stdin &&
809 test_must_fail git update-ref -z --stdin <stdin 2>err &&
810 grep "fatal: delete: missing <ref>" err
811 '
812
813 test_expect_success 'stdin -z fails delete with no old value' '
814 printf $F "delete $a" >stdin &&
815 test_must_fail git update-ref -z --stdin <stdin 2>err &&
816 grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err
817 '
818
819 test_expect_success 'stdin -z fails delete with too many arguments' '
820 printf $F "delete $a" "$m" "$m" >stdin &&
821 test_must_fail git update-ref -z --stdin <stdin 2>err &&
822 grep "fatal: unknown command: $m" err
823 '
824
825 test_expect_success 'stdin -z fails verify with too many arguments' '
826 printf $F "verify $a" "$m" "$m" >stdin &&
827 test_must_fail git update-ref -z --stdin <stdin 2>err &&
828 grep "fatal: unknown command: $m" err
829 '
830
831 test_expect_success 'stdin -z fails verify with no old value' '
832 printf $F "verify $a" >stdin &&
833 test_must_fail git update-ref -z --stdin <stdin 2>err &&
834 grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err
835 '
836
837 test_expect_success 'stdin -z fails option with unknown name' '
838 printf $F "option unknown" >stdin &&
839 test_must_fail git update-ref -z --stdin <stdin 2>err &&
840 grep "fatal: option unknown: unknown" err
841 '
842
843 test_expect_success 'stdin -z fails with duplicate refs' '
844 printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
845 test_must_fail git update-ref -z --stdin <stdin 2>err &&
846 grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err
847 '
848
849 test_expect_success 'stdin -z create ref works' '
850 printf $F "create $a" "$m" >stdin &&
851 git update-ref -z --stdin <stdin &&
852 git rev-parse $m >expect &&
853 git rev-parse $a >actual &&
854 test_cmp expect actual
855 '
856
857 test_expect_success 'stdin -z update ref creates with zero old value' '
858 printf $F "update $b" "$m" "$Z" >stdin &&
859 git update-ref -z --stdin <stdin &&
860 git rev-parse $m >expect &&
861 git rev-parse $b >actual &&
862 test_cmp expect actual &&
863 git update-ref -d $b
864 '
865
866 test_expect_success 'stdin -z update ref creates with empty old value' '
867 printf $F "update $b" "$m" "" >stdin &&
868 git update-ref -z --stdin <stdin &&
869 git rev-parse $m >expect &&
870 git rev-parse $b >actual &&
871 test_cmp expect actual
872 '
873
874 test_expect_success 'stdin -z create ref works with path with space to blob' '
875 printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
876 git update-ref -z --stdin <stdin &&
877 git rev-parse "$m:$pws" >expect &&
878 git rev-parse refs/blobs/pws >actual &&
879 test_cmp expect actual &&
880 git update-ref -d refs/blobs/pws
881 '
882
883 test_expect_success 'stdin -z update ref fails with wrong old value' '
884 printf $F "update $c" "$m" "$m~1" >stdin &&
885 test_must_fail git update-ref -z --stdin <stdin 2>err &&
886 grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
887 test_must_fail git rev-parse --verify -q $c
888 '
889
890 test_expect_success 'stdin -z update ref fails with bad old value' '
891 printf $F "update $c" "$m" "does-not-exist" >stdin &&
892 test_must_fail git update-ref -z --stdin <stdin 2>err &&
893 grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
894 test_must_fail git rev-parse --verify -q $c
895 '
896
897 test_expect_success 'stdin -z create ref fails when ref exists' '
898 git update-ref $c $m &&
899 git rev-parse "$c" >expect &&
900 printf $F "create $c" "$m~1" >stdin &&
901 test_must_fail git update-ref -z --stdin <stdin 2>err &&
902 grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
903 git rev-parse "$c" >actual &&
904 test_cmp expect actual
905 '
906
907 test_expect_success 'stdin -z create ref fails with bad new value' '
908 git update-ref -d "$c" &&
909 printf $F "create $c" "does-not-exist" >stdin &&
910 test_must_fail git update-ref -z --stdin <stdin 2>err &&
911 grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
912 test_must_fail git rev-parse --verify -q $c
913 '
914
915 test_expect_success 'stdin -z create ref fails with empty new value' '
916 printf $F "create $c" "" >stdin &&
917 test_must_fail git update-ref -z --stdin <stdin 2>err &&
918 grep "fatal: create $c: missing <newvalue>" err &&
919 test_must_fail git rev-parse --verify -q $c
920 '
921
922 test_expect_success 'stdin -z update ref works with right old value' '
923 printf $F "update $b" "$m~1" "$m" >stdin &&
924 git update-ref -z --stdin <stdin &&
925 git rev-parse $m~1 >expect &&
926 git rev-parse $b >actual &&
927 test_cmp expect actual
928 '
929
930 test_expect_success 'stdin -z delete ref fails with wrong old value' '
931 printf $F "delete $a" "$m~1" >stdin &&
932 test_must_fail git update-ref -z --stdin <stdin 2>err &&
933 grep "fatal: Cannot lock ref '"'"'$a'"'"'" err &&
934 git rev-parse $m >expect &&
935 git rev-parse $a >actual &&
936 test_cmp expect actual
937 '
938
939 test_expect_success 'stdin -z delete ref fails with zero old value' '
940 printf $F "delete $a" "$Z" >stdin &&
941 test_must_fail git update-ref -z --stdin <stdin 2>err &&
942 grep "fatal: delete $a: zero <oldvalue>" err &&
943 git rev-parse $m >expect &&
944 git rev-parse $a >actual &&
945 test_cmp expect actual
946 '
947
948 test_expect_success 'stdin -z update symref works option no-deref' '
949 git symbolic-ref TESTSYMREF $b &&
950 printf $F "option no-deref" "update TESTSYMREF" "$a" "$b" >stdin &&
951 git update-ref -z --stdin <stdin &&
952 git rev-parse TESTSYMREF >expect &&
953 git rev-parse $a >actual &&
954 test_cmp expect actual &&
955 git rev-parse $m~1 >expect &&
956 git rev-parse $b >actual &&
957 test_cmp expect actual
958 '
959
960 test_expect_success 'stdin -z delete symref works option no-deref' '
961 git symbolic-ref TESTSYMREF $b &&
962 printf $F "option no-deref" "delete TESTSYMREF" "$b" >stdin &&
963 git update-ref -z --stdin <stdin &&
964 test_must_fail git rev-parse --verify -q TESTSYMREF &&
965 git rev-parse $m~1 >expect &&
966 git rev-parse $b >actual &&
967 test_cmp expect actual
968 '
969
970 test_expect_success 'stdin -z delete ref works with right old value' '
971 printf $F "delete $b" "$m~1" >stdin &&
972 git update-ref -z --stdin <stdin &&
973 test_must_fail git rev-parse --verify -q $b
974 '
975
976 test_expect_success 'stdin -z update/create/verify combination works' '
977 printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin &&
978 git update-ref -z --stdin <stdin &&
979 git rev-parse $m >expect &&
980 git rev-parse $a >actual &&
981 test_cmp expect actual &&
982 git rev-parse $b >actual &&
983 test_cmp expect actual &&
984 test_must_fail git rev-parse --verify -q $c
985 '
986
987 test_expect_success 'stdin -z verify succeeds for correct value' '
988 git rev-parse $m >expect &&
989 printf $F "verify $m" "$m" >stdin &&
990 git update-ref -z --stdin <stdin &&
991 git rev-parse $m >actual &&
992 test_cmp expect actual
993 '
994
995 test_expect_success 'stdin -z verify succeeds for missing reference' '
996 printf $F "verify refs/heads/missing" "$Z" >stdin &&
997 git update-ref -z --stdin <stdin &&
998 test_must_fail git rev-parse --verify -q refs/heads/missing
999 '
1000
1001 test_expect_success 'stdin -z verify treats no value as missing' '
1002 printf $F "verify refs/heads/missing" "" >stdin &&
1003 git update-ref -z --stdin <stdin &&
1004 test_must_fail git rev-parse --verify -q refs/heads/missing
1005 '
1006
1007 test_expect_success 'stdin -z verify fails for wrong value' '
1008 git rev-parse $m >expect &&
1009 printf $F "verify $m" "$m~1" >stdin &&
1010 test_must_fail git update-ref -z --stdin <stdin &&
1011 git rev-parse $m >actual &&
1012 test_cmp expect actual
1013 '
1014
1015 test_expect_success 'stdin -z verify fails for mistaken null value' '
1016 git rev-parse $m >expect &&
1017 printf $F "verify $m" "$Z" >stdin &&
1018 test_must_fail git update-ref -z --stdin <stdin &&
1019 git rev-parse $m >actual &&
1020 test_cmp expect actual
1021 '
1022
1023 test_expect_success 'stdin -z verify fails for mistaken empty value' '
1024 M=$(git rev-parse $m) &&
1025 test_when_finished "git update-ref $m $M" &&
1026 git rev-parse $m >expect &&
1027 printf $F "verify $m" "" >stdin &&
1028 test_must_fail git update-ref -z --stdin <stdin &&
1029 git rev-parse $m >actual &&
1030 test_cmp expect actual
1031 '
1032
1033 test_expect_success 'stdin -z update refs works with identity updates' '
1034 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
1035 git update-ref -z --stdin <stdin &&
1036 git rev-parse $m >expect &&
1037 git rev-parse $a >actual &&
1038 test_cmp expect actual &&
1039 git rev-parse $b >actual &&
1040 test_cmp expect actual &&
1041 test_must_fail git rev-parse --verify -q $c
1042 '
1043
1044 test_expect_success 'stdin -z update refs fails with wrong old value' '
1045 git update-ref $c $m &&
1046 printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
1047 test_must_fail git update-ref -z --stdin <stdin 2>err &&
1048 grep "fatal: Cannot lock ref '"'"'$c'"'"'" err &&
1049 git rev-parse $m >expect &&
1050 git rev-parse $a >actual &&
1051 test_cmp expect actual &&
1052 git rev-parse $b >actual &&
1053 test_cmp expect actual &&
1054 git rev-parse $c >actual &&
1055 test_cmp expect actual
1056 '
1057
1058 test_expect_success 'stdin -z delete refs works with packed and loose refs' '
1059 git pack-refs --all &&
1060 git update-ref $c $m~1 &&
1061 printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
1062 git update-ref -z --stdin <stdin &&
1063 test_must_fail git rev-parse --verify -q $a &&
1064 test_must_fail git rev-parse --verify -q $b &&
1065 test_must_fail git rev-parse --verify -q $c
1066 '
1067
1068 run_with_limited_open_files () {
1069 (ulimit -n 32 && "$@")
1070 }
1071
1072 test_lazy_prereq ULIMIT_FILE_DESCRIPTORS 'run_with_limited_open_files true'
1073
1074 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
1075 (
1076 for i in $(test_seq 33)
1077 do
1078 echo "create refs/heads/$i HEAD"
1079 done >large_input &&
1080 run_with_limited_open_files git update-ref --stdin <large_input &&
1081 git rev-parse --verify -q refs/heads/33
1082 )
1083 '
1084
1085 test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
1086 (
1087 for i in $(test_seq 33)
1088 do
1089 echo "delete refs/heads/$i HEAD"
1090 done >large_input &&
1091 run_with_limited_open_files git update-ref --stdin <large_input &&
1092 test_must_fail git rev-parse --verify -q refs/heads/33
1093 )
1094 '
1095
1096 test_done