t / t1400-update-ref.shon commit t5520: test pulling an octopus into an unborn branch (7ad39a2)
   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 '(not) create HEAD with old sha1' "
 140        test_must_fail git update-ref HEAD $A $B
 141"
 142test_expect_success "(not) prior created .git/$m" "
 143        ! test -f .git/$m
 144"
 145rm -f .git/$m
 146
 147test_expect_success \
 148        "create HEAD" \
 149        "git update-ref HEAD $A"
 150test_expect_success '(not) change HEAD with wrong SHA1' "
 151        test_must_fail git update-ref HEAD $B $Z
 152"
 153test_expect_success "(not) changed .git/$m" "
 154        ! test $B"' = $(cat .git/'"$m"')
 155'
 156rm -f .git/$m
 157
 158: a repository with working tree always has reflog these days...
 159: >.git/logs/refs/heads/master
 160test_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"')'
 165test_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"')'
 170test_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
 176cat >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
 180EOF
 181test_expect_success \
 182        "verifying $m's log" \
 183        "test_cmp expect .git/logs/$m"
 184rm -rf .git/$m .git/logs expect
 185
 186test_expect_success \
 187        'enable core.logAllRefUpdates' \
 188        'git config core.logAllRefUpdates true &&
 189         test true = $(git config --bool --get core.logAllRefUpdates)'
 190
 191test_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"')'
 196test_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"')'
 201test_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
 207cat >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
 211EOF
 212test_expect_success \
 213        "verifying $m's log" \
 214        'test_cmp expect .git/logs/$m'
 215rm -f .git/$m .git/logs/$m expect
 216
 217git update-ref $m $D
 218cat >.git/logs/$m <<EOF
 2190000000000000000000000000000000000000000 $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
 224EOF
 225
 226ed="Thu, 26 May 2005 18:32:00 -0500"
 227gd="Thu, 26 May 2005 18:33:00 -0500"
 228ld="Thu, 26 May 2005 18:43:00 -0500"
 229test_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)"'
 235test_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)"'
 241test_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)"'
 247test_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)"'
 253test_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)"'
 259test_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)"'
 265test_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)"'
 271test_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)"'
 277test_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
 285rm -f .git/$m .git/logs/$m expect
 286
 287test_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
 310cat >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.
 315EOF
 316test_expect_success \
 317        'git commit logged updates' \
 318        "test_cmp expect .git/logs/$m"
 319unset h_TEST h_OTHER h_FIXED h_MERGED
 320
 321test_expect_success \
 322        'git cat-file blob master:F (expect OTHER)' \
 323        'test OTHER = $(git cat-file blob master:F)'
 324test_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")'
 327test_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
 331a=refs/heads/a
 332b=refs/heads/b
 333c=refs/heads/c
 334E='""'
 335F='%s\0'
 336pws='path with space'
 337
 338test_expect_success 'stdin test setup' '
 339        echo "$pws" >"$pws" &&
 340        git add -- "$pws" &&
 341        git commit -m "$pws"
 342'
 343
 344test_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
 349test_expect_success 'stdin works with no input' '
 350        >stdin &&
 351        git update-ref --stdin <stdin &&
 352        git rev-parse --verify -q $m
 353'
 354
 355test_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
 361test_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
 367test_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
 373test_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
 379test_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
 385test_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
 391test_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
 397test_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
 403test_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
 409test_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
 415test_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
 421test_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
 427test_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
 433test_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
 439test_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
 445test_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
 451test_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
 457test_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
 467test_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
 475test_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
 484test_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
 493test_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
 502test_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
 510test_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
 519test_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 the ref '"'"'$c'"'"'" err &&
 523        test_must_fail git rev-parse --verify -q $c
 524'
 525
 526test_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
 533test_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
 540test_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
 547test_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
 555test_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 the ref '"'"'$a'"'"'" err &&
 559        git rev-parse $m >expect &&
 560        git rev-parse $a >actual &&
 561        test_cmp expect actual
 562'
 563
 564test_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
 573test_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
 588test_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
 601test_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
 607test_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
 622test_expect_success 'stdin update refs works with identity updates' '
 623        cat >stdin <<-EOF &&
 624        update $a $m $m
 625        update $b $m $m
 626        update $c $Z $E
 627        EOF
 628        git update-ref --stdin <stdin &&
 629        git rev-parse $m >expect &&
 630        git rev-parse $a >actual &&
 631        test_cmp expect actual &&
 632        git rev-parse $b >actual &&
 633        test_cmp expect actual &&
 634        test_must_fail git rev-parse --verify -q $c
 635'
 636
 637test_expect_success 'stdin update refs fails with wrong old value' '
 638        git update-ref $c $m &&
 639        cat >stdin <<-EOF &&
 640        update $a $m $m
 641        update $b $m $m
 642        update $c  ''
 643        EOF
 644        test_must_fail git update-ref --stdin <stdin 2>err &&
 645        grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
 646        git rev-parse $m >expect &&
 647        git rev-parse $a >actual &&
 648        test_cmp expect actual &&
 649        git rev-parse $b >actual &&
 650        test_cmp expect actual &&
 651        git rev-parse $c >actual &&
 652        test_cmp expect actual
 653'
 654
 655test_expect_success 'stdin delete refs works with packed and loose refs' '
 656        git pack-refs --all &&
 657        git update-ref $c $m~1 &&
 658        cat >stdin <<-EOF &&
 659        delete $a $m
 660        update $b $Z $m
 661        update $c $E $m~1
 662        EOF
 663        git update-ref --stdin <stdin &&
 664        test_must_fail git rev-parse --verify -q $a &&
 665        test_must_fail git rev-parse --verify -q $b &&
 666        test_must_fail git rev-parse --verify -q $c
 667'
 668
 669test_expect_success 'stdin -z works on empty input' '
 670        >stdin &&
 671        git update-ref -z --stdin <stdin &&
 672        git rev-parse --verify -q $m
 673'
 674
 675test_expect_success 'stdin -z fails on empty line' '
 676        echo "" >stdin &&
 677        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 678        grep "fatal: whitespace before command: " err
 679'
 680
 681test_expect_success 'stdin -z fails on empty command' '
 682        printf $F "" >stdin &&
 683        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 684        grep "fatal: empty command in input" err
 685'
 686
 687test_expect_success 'stdin -z fails on only whitespace' '
 688        printf $F " " >stdin &&
 689        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 690        grep "fatal: whitespace before command:  " err
 691'
 692
 693test_expect_success 'stdin -z fails on leading whitespace' '
 694        printf $F " create $a" "$m" >stdin &&
 695        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 696        grep "fatal: whitespace before command:  create $a" err
 697'
 698
 699test_expect_success 'stdin -z fails on unknown command' '
 700        printf $F "unknown $a" >stdin &&
 701        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 702        grep "fatal: unknown command: unknown $a" err
 703'
 704
 705test_expect_success 'stdin -z fails create with no ref' '
 706        printf $F "create " >stdin &&
 707        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 708        grep "fatal: create: missing <ref>" err
 709'
 710
 711test_expect_success 'stdin -z fails create with no new value' '
 712        printf $F "create $a" >stdin &&
 713        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 714        grep "fatal: create $a: unexpected end of input when reading <newvalue>" err
 715'
 716
 717test_expect_success 'stdin -z fails create with too many arguments' '
 718        printf $F "create $a" "$m" "$m" >stdin &&
 719        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 720        grep "fatal: unknown command: $m" err
 721'
 722
 723test_expect_success 'stdin -z fails update with no ref' '
 724        printf $F "update " >stdin &&
 725        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 726        grep "fatal: update: missing <ref>" err
 727'
 728
 729test_expect_success 'stdin -z fails update with too few args' '
 730        printf $F "update $a" "$m" >stdin &&
 731        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 732        grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
 733'
 734
 735test_expect_success 'stdin -z emits warning with empty new value' '
 736        git update-ref $a $m &&
 737        printf $F "update $a" "" "" >stdin &&
 738        git update-ref -z --stdin <stdin 2>err &&
 739        grep "warning: update $a: missing <newvalue>, treating as zero" err &&
 740        test_must_fail git rev-parse --verify -q $a
 741'
 742
 743test_expect_success 'stdin -z fails update with no new value' '
 744        printf $F "update $a" >stdin &&
 745        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 746        grep "fatal: update $a: unexpected end of input when reading <newvalue>" err
 747'
 748
 749test_expect_success 'stdin -z fails update with no old value' '
 750        printf $F "update $a" "$m" >stdin &&
 751        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 752        grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
 753'
 754
 755test_expect_success 'stdin -z fails update with too many arguments' '
 756        printf $F "update $a" "$m" "$m" "$m" >stdin &&
 757        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 758        grep "fatal: unknown command: $m" err
 759'
 760
 761test_expect_success 'stdin -z fails delete with no ref' '
 762        printf $F "delete " >stdin &&
 763        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 764        grep "fatal: delete: missing <ref>" err
 765'
 766
 767test_expect_success 'stdin -z fails delete with no old value' '
 768        printf $F "delete $a" >stdin &&
 769        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 770        grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err
 771'
 772
 773test_expect_success 'stdin -z fails delete with too many arguments' '
 774        printf $F "delete $a" "$m" "$m" >stdin &&
 775        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 776        grep "fatal: unknown command: $m" err
 777'
 778
 779test_expect_success 'stdin -z fails verify with too many arguments' '
 780        printf $F "verify $a" "$m" "$m" >stdin &&
 781        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 782        grep "fatal: unknown command: $m" err
 783'
 784
 785test_expect_success 'stdin -z fails verify with no old value' '
 786        printf $F "verify $a" >stdin &&
 787        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 788        grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err
 789'
 790
 791test_expect_success 'stdin -z fails option with unknown name' '
 792        printf $F "option unknown" >stdin &&
 793        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 794        grep "fatal: option unknown: unknown" err
 795'
 796
 797test_expect_success 'stdin -z fails with duplicate refs' '
 798        printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
 799        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 800        grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err
 801'
 802
 803test_expect_success 'stdin -z create ref works' '
 804        printf $F "create $a" "$m" >stdin &&
 805        git update-ref -z --stdin <stdin &&
 806        git rev-parse $m >expect &&
 807        git rev-parse $a >actual &&
 808        test_cmp expect actual
 809'
 810
 811test_expect_success 'stdin -z update ref creates with zero old value' '
 812        printf $F "update $b" "$m" "$Z" >stdin &&
 813        git update-ref -z --stdin <stdin &&
 814        git rev-parse $m >expect &&
 815        git rev-parse $b >actual &&
 816        test_cmp expect actual &&
 817        git update-ref -d $b
 818'
 819
 820test_expect_success 'stdin -z update ref creates with empty old value' '
 821        printf $F "update $b" "$m" "" >stdin &&
 822        git update-ref -z --stdin <stdin &&
 823        git rev-parse $m >expect &&
 824        git rev-parse $b >actual &&
 825        test_cmp expect actual
 826'
 827
 828test_expect_success 'stdin -z create ref works with path with space to blob' '
 829        printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
 830        git update-ref -z --stdin <stdin &&
 831        git rev-parse "$m:$pws" >expect &&
 832        git rev-parse refs/blobs/pws >actual &&
 833        test_cmp expect actual &&
 834        git update-ref -d refs/blobs/pws
 835'
 836
 837test_expect_success 'stdin -z update ref fails with wrong old value' '
 838        printf $F "update $c" "$m" "$m~1" >stdin &&
 839        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 840        grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
 841        test_must_fail git rev-parse --verify -q $c
 842'
 843
 844test_expect_success 'stdin -z update ref fails with bad old value' '
 845        printf $F "update $c" "$m" "does-not-exist" >stdin &&
 846        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 847        grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
 848        test_must_fail git rev-parse --verify -q $c
 849'
 850
 851test_expect_success 'stdin -z create ref fails when ref exists' '
 852        git update-ref $c $m &&
 853        git rev-parse "$c" >expect &&
 854        printf $F "create $c" "$m~1" >stdin &&
 855        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 856        grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
 857        git rev-parse "$c" >actual &&
 858        test_cmp expect actual
 859'
 860
 861test_expect_success 'stdin -z create ref fails with bad new value' '
 862        git update-ref -d "$c" &&
 863        printf $F "create $c" "does-not-exist" >stdin &&
 864        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 865        grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
 866        test_must_fail git rev-parse --verify -q $c
 867'
 868
 869test_expect_success 'stdin -z create ref fails with empty new value' '
 870        printf $F "create $c" "" >stdin &&
 871        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 872        grep "fatal: create $c: missing <newvalue>" err &&
 873        test_must_fail git rev-parse --verify -q $c
 874'
 875
 876test_expect_success 'stdin -z update ref works with right old value' '
 877        printf $F "update $b" "$m~1" "$m" >stdin &&
 878        git update-ref -z --stdin <stdin &&
 879        git rev-parse $m~1 >expect &&
 880        git rev-parse $b >actual &&
 881        test_cmp expect actual
 882'
 883
 884test_expect_success 'stdin -z delete ref fails with wrong old value' '
 885        printf $F "delete $a" "$m~1" >stdin &&
 886        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 887        grep "fatal: Cannot lock the ref '"'"'$a'"'"'" err &&
 888        git rev-parse $m >expect &&
 889        git rev-parse $a >actual &&
 890        test_cmp expect actual
 891'
 892
 893test_expect_success 'stdin -z delete ref fails with zero old value' '
 894        printf $F "delete $a" "$Z" >stdin &&
 895        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 896        grep "fatal: delete $a: zero <oldvalue>" err &&
 897        git rev-parse $m >expect &&
 898        git rev-parse $a >actual &&
 899        test_cmp expect actual
 900'
 901
 902test_expect_success 'stdin -z update symref works option no-deref' '
 903        git symbolic-ref TESTSYMREF $b &&
 904        printf $F "option no-deref" "update TESTSYMREF" "$a" "$b" >stdin &&
 905        git update-ref -z --stdin <stdin &&
 906        git rev-parse TESTSYMREF >expect &&
 907        git rev-parse $a >actual &&
 908        test_cmp expect actual &&
 909        git rev-parse $m~1 >expect &&
 910        git rev-parse $b >actual &&
 911        test_cmp expect actual
 912'
 913
 914test_expect_success 'stdin -z delete symref works option no-deref' '
 915        git symbolic-ref TESTSYMREF $b &&
 916        printf $F "option no-deref" "delete TESTSYMREF" "$b" >stdin &&
 917        git update-ref -z --stdin <stdin &&
 918        test_must_fail git rev-parse --verify -q TESTSYMREF &&
 919        git rev-parse $m~1 >expect &&
 920        git rev-parse $b >actual &&
 921        test_cmp expect actual
 922'
 923
 924test_expect_success 'stdin -z delete ref works with right old value' '
 925        printf $F "delete $b" "$m~1" >stdin &&
 926        git update-ref -z --stdin <stdin &&
 927        test_must_fail git rev-parse --verify -q $b
 928'
 929
 930test_expect_success 'stdin -z update/create/verify combination works' '
 931        printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin &&
 932        git update-ref -z --stdin <stdin &&
 933        git rev-parse $m >expect &&
 934        git rev-parse $a >actual &&
 935        test_cmp expect actual &&
 936        git rev-parse $b >actual &&
 937        test_cmp expect actual &&
 938        test_must_fail git rev-parse --verify -q $c
 939'
 940
 941test_expect_success 'stdin -z update refs works with identity updates' '
 942        printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
 943        git update-ref -z --stdin <stdin &&
 944        git rev-parse $m >expect &&
 945        git rev-parse $a >actual &&
 946        test_cmp expect actual &&
 947        git rev-parse $b >actual &&
 948        test_cmp expect actual &&
 949        test_must_fail git rev-parse --verify -q $c
 950'
 951
 952test_expect_success 'stdin -z update refs fails with wrong old value' '
 953        git update-ref $c $m &&
 954        printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
 955        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 956        grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
 957        git rev-parse $m >expect &&
 958        git rev-parse $a >actual &&
 959        test_cmp expect actual &&
 960        git rev-parse $b >actual &&
 961        test_cmp expect actual &&
 962        git rev-parse $c >actual &&
 963        test_cmp expect actual
 964'
 965
 966test_expect_success 'stdin -z delete refs works with packed and loose refs' '
 967        git pack-refs --all &&
 968        git update-ref $c $m~1 &&
 969        printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
 970        git update-ref -z --stdin <stdin &&
 971        test_must_fail git rev-parse --verify -q $a &&
 972        test_must_fail git rev-parse --verify -q $b &&
 973        test_must_fail git rev-parse --verify -q $c
 974'
 975
 976test_done