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