t / t1400-update-ref.shon commit MinGW: update tests to handle a native eol of crlf (5f4e02e)
   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 for ref '"$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 for ref '"$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 unbalanced quotes' '
 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 invalid escape' '
 360        echo "create $a \"ma\zter\"" >stdin &&
 361        test_must_fail git update-ref --stdin <stdin 2>err &&
 362        grep "fatal: badly quoted argument: \\\"ma\\\\zter\\\"" err
 363'
 364
 365test_expect_success 'stdin fails on junk after quoted argument' '
 366        echo "create \"$a\"master" >stdin &&
 367        test_must_fail git update-ref --stdin <stdin 2>err &&
 368        grep "fatal: unexpected character after quoted argument: \\\"$a\\\"master" err
 369'
 370
 371test_expect_success 'stdin fails create with no ref' '
 372        echo "create " >stdin &&
 373        test_must_fail git update-ref --stdin <stdin 2>err &&
 374        grep "fatal: create: missing <ref>" err
 375'
 376
 377test_expect_success 'stdin fails create with bad ref name' '
 378        echo "create ~a $m" >stdin &&
 379        test_must_fail git update-ref --stdin <stdin 2>err &&
 380        grep "fatal: invalid ref format: ~a" err
 381'
 382
 383test_expect_success 'stdin fails create with no new value' '
 384        echo "create $a" >stdin &&
 385        test_must_fail git update-ref --stdin <stdin 2>err &&
 386        grep "fatal: create $a: missing <newvalue>" err
 387'
 388
 389test_expect_success 'stdin fails create with too many arguments' '
 390        echo "create $a $m $m" >stdin &&
 391        test_must_fail git update-ref --stdin <stdin 2>err &&
 392        grep "fatal: create $a: extra input:  $m" err
 393'
 394
 395test_expect_success 'stdin fails update with no ref' '
 396        echo "update " >stdin &&
 397        test_must_fail git update-ref --stdin <stdin 2>err &&
 398        grep "fatal: update: missing <ref>" err
 399'
 400
 401test_expect_success 'stdin fails update with bad ref name' '
 402        echo "update ~a $m" >stdin &&
 403        test_must_fail git update-ref --stdin <stdin 2>err &&
 404        grep "fatal: invalid ref format: ~a" err
 405'
 406
 407test_expect_success 'stdin fails update with no new value' '
 408        echo "update $a" >stdin &&
 409        test_must_fail git update-ref --stdin <stdin 2>err &&
 410        grep "fatal: update $a: missing <newvalue>" err
 411'
 412
 413test_expect_success 'stdin fails update with too many arguments' '
 414        echo "update $a $m $m $m" >stdin &&
 415        test_must_fail git update-ref --stdin <stdin 2>err &&
 416        grep "fatal: update $a: extra input:  $m" err
 417'
 418
 419test_expect_success 'stdin fails delete with no ref' '
 420        echo "delete " >stdin &&
 421        test_must_fail git update-ref --stdin <stdin 2>err &&
 422        grep "fatal: delete: missing <ref>" err
 423'
 424
 425test_expect_success 'stdin fails delete with bad ref name' '
 426        echo "delete ~a $m" >stdin &&
 427        test_must_fail git update-ref --stdin <stdin 2>err &&
 428        grep "fatal: invalid ref format: ~a" err
 429'
 430
 431test_expect_success 'stdin fails delete with too many arguments' '
 432        echo "delete $a $m $m" >stdin &&
 433        test_must_fail git update-ref --stdin <stdin 2>err &&
 434        grep "fatal: delete $a: extra input:  $m" err
 435'
 436
 437test_expect_success 'stdin fails verify with too many arguments' '
 438        echo "verify $a $m $m" >stdin &&
 439        test_must_fail git update-ref --stdin <stdin 2>err &&
 440        grep "fatal: verify $a: extra input:  $m" err
 441'
 442
 443test_expect_success 'stdin fails option with unknown name' '
 444        echo "option unknown" >stdin &&
 445        test_must_fail git update-ref --stdin <stdin 2>err &&
 446        grep "fatal: option unknown: unknown" err
 447'
 448
 449test_expect_success 'stdin fails with duplicate refs' '
 450        cat >stdin <<-EOF &&
 451        create $a $m
 452        create $b $m
 453        create $a $m
 454        EOF
 455        test_must_fail git update-ref --stdin <stdin 2>err &&
 456        grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err
 457'
 458
 459test_expect_success 'stdin create ref works' '
 460        echo "create $a $m" >stdin &&
 461        git update-ref --stdin <stdin &&
 462        git rev-parse $m >expect &&
 463        git rev-parse $a >actual &&
 464        test_cmp expect actual
 465'
 466
 467test_expect_success 'stdin succeeds with quoted argument' '
 468        git update-ref -d $a &&
 469        echo "create $a \"$m\"" >stdin &&
 470        git update-ref --stdin <stdin &&
 471        git rev-parse $m >expect &&
 472        git rev-parse $a >actual &&
 473        test_cmp expect actual
 474'
 475
 476test_expect_success 'stdin succeeds with escaped character' '
 477        git update-ref -d $a &&
 478        echo "create $a \"ma\\163ter\"" >stdin &&
 479        git update-ref --stdin <stdin &&
 480        git rev-parse $m >expect &&
 481        git rev-parse $a >actual &&
 482        test_cmp expect actual
 483'
 484
 485test_expect_success 'stdin update ref creates with zero old value' '
 486        echo "update $b $m $Z" >stdin &&
 487        git update-ref --stdin <stdin &&
 488        git rev-parse $m >expect &&
 489        git rev-parse $b >actual &&
 490        test_cmp expect actual &&
 491        git update-ref -d $b
 492'
 493
 494test_expect_success 'stdin update ref creates with empty old value' '
 495        echo "update $b $m $E" >stdin &&
 496        git update-ref --stdin <stdin &&
 497        git rev-parse $m >expect &&
 498        git rev-parse $b >actual &&
 499        test_cmp expect actual
 500'
 501
 502test_expect_success 'stdin create ref works with path with space to blob' '
 503        echo "create refs/blobs/pws \"$m:$pws\"" >stdin &&
 504        git update-ref --stdin <stdin &&
 505        git rev-parse "$m:$pws" >expect &&
 506        git rev-parse refs/blobs/pws >actual &&
 507        test_cmp expect actual &&
 508        git update-ref -d refs/blobs/pws
 509'
 510
 511test_expect_success 'stdin update ref fails with wrong old value' '
 512        echo "update $c $m $m~1" >stdin &&
 513        test_must_fail git update-ref --stdin <stdin 2>err &&
 514        grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
 515        test_must_fail git rev-parse --verify -q $c
 516'
 517
 518test_expect_success 'stdin update ref fails with bad old value' '
 519        echo "update $c $m does-not-exist" >stdin &&
 520        test_must_fail git update-ref --stdin <stdin 2>err &&
 521        grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
 522        test_must_fail git rev-parse --verify -q $c
 523'
 524
 525test_expect_success 'stdin create ref fails with bad new value' '
 526        echo "create $c does-not-exist" >stdin &&
 527        test_must_fail git update-ref --stdin <stdin 2>err &&
 528        grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
 529        test_must_fail git rev-parse --verify -q $c
 530'
 531
 532test_expect_success 'stdin create ref fails with zero new value' '
 533        echo "create $c " >stdin &&
 534        test_must_fail git update-ref --stdin <stdin 2>err &&
 535        grep "fatal: create $c: zero <newvalue>" err &&
 536        test_must_fail git rev-parse --verify -q $c
 537'
 538
 539test_expect_success 'stdin update ref works with right old value' '
 540        echo "update $b $m~1 $m" >stdin &&
 541        git update-ref --stdin <stdin &&
 542        git rev-parse $m~1 >expect &&
 543        git rev-parse $b >actual &&
 544        test_cmp expect actual
 545'
 546
 547test_expect_success 'stdin delete ref fails with wrong old value' '
 548        echo "delete $a $m~1" >stdin &&
 549        test_must_fail git update-ref --stdin <stdin 2>err &&
 550        grep "fatal: Cannot lock the ref '"'"'$a'"'"'" err &&
 551        git rev-parse $m >expect &&
 552        git rev-parse $a >actual &&
 553        test_cmp expect actual
 554'
 555
 556test_expect_success 'stdin delete ref fails with zero old value' '
 557        echo "delete $a " >stdin &&
 558        test_must_fail git update-ref --stdin <stdin 2>err &&
 559        grep "fatal: delete $a: zero <oldvalue>" err &&
 560        git rev-parse $m >expect &&
 561        git rev-parse $a >actual &&
 562        test_cmp expect actual
 563'
 564
 565test_expect_success 'stdin update symref works option no-deref' '
 566        git symbolic-ref TESTSYMREF $b &&
 567        cat >stdin <<-EOF &&
 568        option no-deref
 569        update TESTSYMREF $a $b
 570        EOF
 571        git update-ref --stdin <stdin &&
 572        git rev-parse TESTSYMREF >expect &&
 573        git rev-parse $a >actual &&
 574        test_cmp expect actual &&
 575        git rev-parse $m~1 >expect &&
 576        git rev-parse $b >actual &&
 577        test_cmp expect actual
 578'
 579
 580test_expect_success 'stdin delete symref works option no-deref' '
 581        git symbolic-ref TESTSYMREF $b &&
 582        cat >stdin <<-EOF &&
 583        option no-deref
 584        delete TESTSYMREF $b
 585        EOF
 586        git update-ref --stdin <stdin &&
 587        test_must_fail git rev-parse --verify -q TESTSYMREF &&
 588        git rev-parse $m~1 >expect &&
 589        git rev-parse $b >actual &&
 590        test_cmp expect actual
 591'
 592
 593test_expect_success 'stdin delete ref works with right old value' '
 594        echo "delete $b $m~1" >stdin &&
 595        git update-ref --stdin <stdin &&
 596        test_must_fail git rev-parse --verify -q $b
 597'
 598
 599test_expect_success 'stdin update/create/verify combination works' '
 600        cat >stdin <<-EOF &&
 601        update $a $m
 602        create $b $m
 603        verify $c
 604        EOF
 605        git update-ref --stdin <stdin &&
 606        git rev-parse $m >expect &&
 607        git rev-parse $a >actual &&
 608        test_cmp expect actual &&
 609        git rev-parse $b >actual &&
 610        test_cmp expect actual &&
 611        test_must_fail git rev-parse --verify -q $c
 612'
 613
 614test_expect_success 'stdin update refs works with identity updates' '
 615        cat >stdin <<-EOF &&
 616        update $a $m $m
 617        update $b $m $m
 618        update $c $Z $E
 619        EOF
 620        git update-ref --stdin <stdin &&
 621        git rev-parse $m >expect &&
 622        git rev-parse $a >actual &&
 623        test_cmp expect actual &&
 624        git rev-parse $b >actual &&
 625        test_cmp expect actual &&
 626        test_must_fail git rev-parse --verify -q $c
 627'
 628
 629test_expect_success 'stdin update refs fails with wrong old value' '
 630        git update-ref $c $m &&
 631        cat >stdin <<-EOF &&
 632        update $a $m $m
 633        update $b $m $m
 634        update $c  ''
 635        EOF
 636        test_must_fail git update-ref --stdin <stdin 2>err &&
 637        grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
 638        git rev-parse $m >expect &&
 639        git rev-parse $a >actual &&
 640        test_cmp expect actual &&
 641        git rev-parse $b >actual &&
 642        test_cmp expect actual &&
 643        git rev-parse $c >actual &&
 644        test_cmp expect actual
 645'
 646
 647test_expect_success 'stdin delete refs works with packed and loose refs' '
 648        git pack-refs --all &&
 649        git update-ref $c $m~1 &&
 650        cat >stdin <<-EOF &&
 651        delete $a $m
 652        update $b $Z $m
 653        update $c $E $m~1
 654        EOF
 655        git update-ref --stdin <stdin &&
 656        test_must_fail git rev-parse --verify -q $a &&
 657        test_must_fail git rev-parse --verify -q $b &&
 658        test_must_fail git rev-parse --verify -q $c
 659'
 660
 661test_expect_success 'stdin -z works on empty input' '
 662        >stdin &&
 663        git update-ref -z --stdin <stdin &&
 664        git rev-parse --verify -q $m
 665'
 666
 667test_expect_success 'stdin -z fails on empty line' '
 668        echo "" >stdin &&
 669        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 670        grep "fatal: whitespace before command: " err
 671'
 672
 673test_expect_success 'stdin -z fails on empty command' '
 674        printf $F "" >stdin &&
 675        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 676        grep "fatal: empty command in input" err
 677'
 678
 679test_expect_success 'stdin -z fails on only whitespace' '
 680        printf $F " " >stdin &&
 681        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 682        grep "fatal: whitespace before command:  " err
 683'
 684
 685test_expect_success 'stdin -z fails on leading whitespace' '
 686        printf $F " create $a" "$m" >stdin &&
 687        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 688        grep "fatal: whitespace before command:  create $a" err
 689'
 690
 691test_expect_success 'stdin -z fails on unknown command' '
 692        printf $F "unknown $a" >stdin &&
 693        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 694        grep "fatal: unknown command: unknown $a" err
 695'
 696
 697test_expect_success 'stdin -z fails create with no ref' '
 698        printf $F "create " >stdin &&
 699        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 700        grep "fatal: create: missing <ref>" err
 701'
 702
 703test_expect_success 'stdin -z fails create with bad ref name' '
 704        printf $F "create ~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 create with no new value' '
 710        printf $F "create $a" >stdin &&
 711        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 712        grep "fatal: create $a: unexpected end of input when reading <newvalue>" err
 713'
 714
 715test_expect_success 'stdin -z fails create with too many arguments' '
 716        printf $F "create $a" "$m" "$m" >stdin &&
 717        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 718        grep "fatal: unknown command: $m" err
 719'
 720
 721test_expect_success 'stdin -z fails update with no ref' '
 722        printf $F "update " >stdin &&
 723        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 724        grep "fatal: update: missing <ref>" err
 725'
 726
 727test_expect_success 'stdin -z fails update with too few args' '
 728        printf $F "update $a" "$m" >stdin &&
 729        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 730        grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
 731'
 732
 733test_expect_success 'stdin -z fails update with bad ref name' '
 734        printf $F "update ~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 emits warning with empty new value' '
 740        git update-ref $a $m &&
 741        printf $F "update $a" "" "" >stdin &&
 742        git update-ref -z --stdin <stdin 2>err &&
 743        grep "warning: update $a: missing <newvalue>, treating as zero" err &&
 744        test_must_fail git rev-parse --verify -q $a
 745'
 746
 747test_expect_success 'stdin -z fails update with no new value' '
 748        printf $F "update $a" >stdin &&
 749        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 750        grep "fatal: update $a: unexpected end of input when reading <newvalue>" err
 751'
 752
 753test_expect_success 'stdin -z fails update with no old value' '
 754        printf $F "update $a" "$m" >stdin &&
 755        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 756        grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
 757'
 758
 759test_expect_success 'stdin -z fails update with too many arguments' '
 760        printf $F "update $a" "$m" "$m" "$m" >stdin &&
 761        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 762        grep "fatal: unknown command: $m" err
 763'
 764
 765test_expect_success 'stdin -z fails delete with no ref' '
 766        printf $F "delete " >stdin &&
 767        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 768        grep "fatal: delete: missing <ref>" err
 769'
 770
 771test_expect_success 'stdin -z fails delete with bad ref name' '
 772        printf $F "delete ~a" "$m" >stdin &&
 773        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 774        grep "fatal: invalid ref format: ~a" err
 775'
 776
 777test_expect_success 'stdin -z fails delete with no old value' '
 778        printf $F "delete $a" >stdin &&
 779        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 780        grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err
 781'
 782
 783test_expect_success 'stdin -z fails delete with too many arguments' '
 784        printf $F "delete $a" "$m" "$m" >stdin &&
 785        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 786        grep "fatal: unknown command: $m" err
 787'
 788
 789test_expect_success 'stdin -z fails verify with too many arguments' '
 790        printf $F "verify $a" "$m" "$m" >stdin &&
 791        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 792        grep "fatal: unknown command: $m" err
 793'
 794
 795test_expect_success 'stdin -z fails verify with no old value' '
 796        printf $F "verify $a" >stdin &&
 797        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 798        grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err
 799'
 800
 801test_expect_success 'stdin -z fails option with unknown name' '
 802        printf $F "option unknown" >stdin &&
 803        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 804        grep "fatal: option unknown: unknown" err
 805'
 806
 807test_expect_success 'stdin -z fails with duplicate refs' '
 808        printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
 809        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 810        grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err
 811'
 812
 813test_expect_success 'stdin -z create ref works' '
 814        printf $F "create $a" "$m" >stdin &&
 815        git update-ref -z --stdin <stdin &&
 816        git rev-parse $m >expect &&
 817        git rev-parse $a >actual &&
 818        test_cmp expect actual
 819'
 820
 821test_expect_success 'stdin -z update ref creates with zero old value' '
 822        printf $F "update $b" "$m" "$Z" >stdin &&
 823        git update-ref -z --stdin <stdin &&
 824        git rev-parse $m >expect &&
 825        git rev-parse $b >actual &&
 826        test_cmp expect actual &&
 827        git update-ref -d $b
 828'
 829
 830test_expect_success 'stdin -z update ref creates with empty old value' '
 831        printf $F "update $b" "$m" "" >stdin &&
 832        git update-ref -z --stdin <stdin &&
 833        git rev-parse $m >expect &&
 834        git rev-parse $b >actual &&
 835        test_cmp expect actual
 836'
 837
 838test_expect_success 'stdin -z create ref works with path with space to blob' '
 839        printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
 840        git update-ref -z --stdin <stdin &&
 841        git rev-parse "$m:$pws" >expect &&
 842        git rev-parse refs/blobs/pws >actual &&
 843        test_cmp expect actual &&
 844        git update-ref -d refs/blobs/pws
 845'
 846
 847test_expect_success 'stdin -z update ref fails with wrong old value' '
 848        printf $F "update $c" "$m" "$m~1" >stdin &&
 849        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 850        grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
 851        test_must_fail git rev-parse --verify -q $c
 852'
 853
 854test_expect_success 'stdin -z update ref fails with bad old value' '
 855        printf $F "update $c" "$m" "does-not-exist" >stdin &&
 856        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 857        grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
 858        test_must_fail git rev-parse --verify -q $c
 859'
 860
 861test_expect_success 'stdin -z create ref fails when ref exists' '
 862        git update-ref $c $m &&
 863        git rev-parse "$c" >expect &&
 864        printf $F "create $c" "$m~1" >stdin &&
 865        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 866        grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
 867        git rev-parse "$c" >actual &&
 868        test_cmp expect actual
 869'
 870
 871test_expect_success 'stdin -z create ref fails with bad new value' '
 872        git update-ref -d "$c" &&
 873        printf $F "create $c" "does-not-exist" >stdin &&
 874        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 875        grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
 876        test_must_fail git rev-parse --verify -q $c
 877'
 878
 879test_expect_success 'stdin -z create ref fails with empty new value' '
 880        printf $F "create $c" "" >stdin &&
 881        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 882        grep "fatal: create $c: missing <newvalue>" err &&
 883        test_must_fail git rev-parse --verify -q $c
 884'
 885
 886test_expect_success 'stdin -z update ref works with right old value' '
 887        printf $F "update $b" "$m~1" "$m" >stdin &&
 888        git update-ref -z --stdin <stdin &&
 889        git rev-parse $m~1 >expect &&
 890        git rev-parse $b >actual &&
 891        test_cmp expect actual
 892'
 893
 894test_expect_success 'stdin -z delete ref fails with wrong old value' '
 895        printf $F "delete $a" "$m~1" >stdin &&
 896        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 897        grep "fatal: Cannot lock the ref '"'"'$a'"'"'" err &&
 898        git rev-parse $m >expect &&
 899        git rev-parse $a >actual &&
 900        test_cmp expect actual
 901'
 902
 903test_expect_success 'stdin -z delete ref fails with zero old value' '
 904        printf $F "delete $a" "$Z" >stdin &&
 905        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 906        grep "fatal: delete $a: zero <oldvalue>" err &&
 907        git rev-parse $m >expect &&
 908        git rev-parse $a >actual &&
 909        test_cmp expect actual
 910'
 911
 912test_expect_success 'stdin -z update symref works option no-deref' '
 913        git symbolic-ref TESTSYMREF $b &&
 914        printf $F "option no-deref" "update TESTSYMREF" "$a" "$b" >stdin &&
 915        git update-ref -z --stdin <stdin &&
 916        git rev-parse TESTSYMREF >expect &&
 917        git rev-parse $a >actual &&
 918        test_cmp expect actual &&
 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 symref works option no-deref' '
 925        git symbolic-ref TESTSYMREF $b &&
 926        printf $F "option no-deref" "delete TESTSYMREF" "$b" >stdin &&
 927        git update-ref -z --stdin <stdin &&
 928        test_must_fail git rev-parse --verify -q TESTSYMREF &&
 929        git rev-parse $m~1 >expect &&
 930        git rev-parse $b >actual &&
 931        test_cmp expect actual
 932'
 933
 934test_expect_success 'stdin -z delete ref works with right old value' '
 935        printf $F "delete $b" "$m~1" >stdin &&
 936        git update-ref -z --stdin <stdin &&
 937        test_must_fail git rev-parse --verify -q $b
 938'
 939
 940test_expect_success 'stdin -z update/create/verify combination works' '
 941        printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin &&
 942        git update-ref -z --stdin <stdin &&
 943        git rev-parse $m >expect &&
 944        git rev-parse $a >actual &&
 945        test_cmp expect actual &&
 946        git rev-parse $b >actual &&
 947        test_cmp expect actual &&
 948        test_must_fail git rev-parse --verify -q $c
 949'
 950
 951test_expect_success 'stdin -z update refs works with identity updates' '
 952        printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
 953        git update-ref -z --stdin <stdin &&
 954        git rev-parse $m >expect &&
 955        git rev-parse $a >actual &&
 956        test_cmp expect actual &&
 957        git rev-parse $b >actual &&
 958        test_cmp expect actual &&
 959        test_must_fail git rev-parse --verify -q $c
 960'
 961
 962test_expect_success 'stdin -z update refs fails with wrong old value' '
 963        git update-ref $c $m &&
 964        printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
 965        test_must_fail git update-ref -z --stdin <stdin 2>err &&
 966        grep "fatal: Cannot lock the ref '"'"'$c'"'"'" err &&
 967        git rev-parse $m >expect &&
 968        git rev-parse $a >actual &&
 969        test_cmp expect actual &&
 970        git rev-parse $b >actual &&
 971        test_cmp expect actual &&
 972        git rev-parse $c >actual &&
 973        test_cmp expect actual
 974'
 975
 976test_expect_success 'stdin -z delete refs works with packed and loose refs' '
 977        git pack-refs --all &&
 978        git update-ref $c $m~1 &&
 979        printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
 980        git update-ref -z --stdin <stdin &&
 981        test_must_fail git rev-parse --verify -q $a &&
 982        test_must_fail git rev-parse --verify -q $b &&
 983        test_must_fail git rev-parse --verify -q $c
 984'
 985
 986test_done