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