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