t / t3200-branch.shon commit Merge branch 'gp/avoid-explicit-mention-of-dot-git-refs' (9adf272)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Amos Waterland
   4#
   5
   6test_description='git branch assorted tests'
   7
   8. ./test-lib.sh
   9
  10test_expect_success \
  11    'prepare a trivial repository' \
  12    'echo Hello > A &&
  13     git update-index --add A &&
  14     git commit -m "Initial commit." &&
  15     echo World >> A &&
  16     git update-index --add A &&
  17     git commit -m "Second commit." &&
  18     HEAD=$(git rev-parse --verify HEAD)'
  19
  20test_expect_success \
  21    'git branch --help should not have created a bogus branch' '
  22     test_might_fail git branch --help </dev/null >/dev/null 2>/dev/null &&
  23     test_path_is_missing .git/refs/heads/--help
  24'
  25
  26test_expect_success 'branch -h in broken repository' '
  27        mkdir broken &&
  28        (
  29                cd broken &&
  30                git init &&
  31                >.git/refs/heads/master &&
  32                test_expect_code 129 git branch -h >usage 2>&1
  33        ) &&
  34        test_i18ngrep "[Uu]sage" broken/usage
  35'
  36
  37test_expect_success \
  38    'git branch abc should create a branch' \
  39    'git branch abc && test_path_is_file .git/refs/heads/abc'
  40
  41test_expect_success \
  42    'git branch a/b/c should create a branch' \
  43    'git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c'
  44
  45cat >expect <<EOF
  46$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
  47EOF
  48test_expect_success \
  49    'git branch -l d/e/f should create a branch and a log' \
  50        'GIT_COMMITTER_DATE="2005-05-26 23:30" \
  51     git branch -l d/e/f &&
  52         test_path_is_file .git/refs/heads/d/e/f &&
  53         test_path_is_file .git/logs/refs/heads/d/e/f &&
  54         test_cmp expect .git/logs/refs/heads/d/e/f'
  55
  56test_expect_success \
  57    'git branch -d d/e/f should delete a branch and a log' \
  58        'git branch -d d/e/f &&
  59         test_path_is_missing .git/refs/heads/d/e/f &&
  60         test_path_is_missing .git/logs/refs/heads/d/e/f'
  61
  62test_expect_success \
  63    'git branch j/k should work after branch j has been deleted' \
  64       'git branch j &&
  65        git branch -d j &&
  66        git branch j/k'
  67
  68test_expect_success \
  69    'git branch l should work after branch l/m has been deleted' \
  70       'git branch l/m &&
  71        git branch -d l/m &&
  72        git branch l'
  73
  74test_expect_success \
  75    'git branch -m dumps usage' \
  76       'test_expect_code 128 git branch -m 2>err &&
  77        test_i18ngrep "too many branches for a rename operation" err'
  78
  79test_expect_success \
  80    'git branch -m m m/m should work' \
  81       'git branch -l m &&
  82        git branch -m m m/m &&
  83        test_path_is_file .git/logs/refs/heads/m/m'
  84
  85test_expect_success \
  86    'git branch -m n/n n should work' \
  87       'git branch -l n/n &&
  88        git branch -m n/n n &&
  89        test_path_is_file .git/logs/refs/heads/n'
  90
  91test_expect_success 'git branch -m o/o o should fail when o/p exists' '
  92        git branch o/o &&
  93        git branch o/p &&
  94        test_must_fail git branch -m o/o o
  95'
  96
  97test_expect_success 'git branch -m q r/q should fail when r exists' '
  98        git branch q &&
  99        git branch r &&
 100        test_must_fail git branch -m q r/q
 101'
 102
 103test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
 104        git branch bar &&
 105        git checkout -b foo &&
 106        test_must_fail git branch -M bar foo
 107'
 108
 109test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
 110        git checkout -b baz &&
 111        git branch bam &&
 112        git branch -M baz bam
 113'
 114
 115test_expect_success 'git branch -M master should work when master is checked out' '
 116        git checkout master &&
 117        git branch -M master
 118'
 119
 120test_expect_success 'git branch -M master master should work when master is checked out' '
 121        git checkout master &&
 122        git branch -M master master
 123'
 124
 125test_expect_success 'git branch -M master2 master2 should work when master is checked out' '
 126        git checkout master &&
 127        git branch master2 &&
 128        git branch -M master2 master2
 129'
 130
 131test_expect_success 'git branch -v -d t should work' '
 132        git branch t &&
 133        test_path_is_file .git/refs/heads/t &&
 134        git branch -v -d t &&
 135        test_path_is_missing .git/refs/heads/t
 136'
 137
 138test_expect_success 'git branch -v -m t s should work' '
 139        git branch t &&
 140        test_path_is_file .git/refs/heads/t &&
 141        git branch -v -m t s &&
 142        test_path_is_missing .git/refs/heads/t &&
 143        test_path_is_file .git/refs/heads/s &&
 144        git branch -d s
 145'
 146
 147test_expect_success 'git branch -m -d t s should fail' '
 148        git branch t &&
 149        test_path_is_file .git/refs/heads/t &&
 150        test_must_fail git branch -m -d t s &&
 151        git branch -d t &&
 152        test_path_is_missing .git/refs/heads/t
 153'
 154
 155test_expect_success 'git branch --list -d t should fail' '
 156        git branch t &&
 157        test_path_is_file .git/refs/heads/t &&
 158        test_must_fail git branch --list -d t &&
 159        git branch -d t &&
 160        test_path_is_missing .git/refs/heads/t
 161'
 162
 163test_expect_success 'git branch --column' '
 164        COLUMNS=81 git branch --column=column >actual &&
 165        cat >expected <<\EOF &&
 166  a/b/c     bam       foo       l       * master    n         o/p       r
 167  abc       bar       j/k       m/m       master2   o/o       q
 168EOF
 169        test_cmp expected actual
 170'
 171
 172test_expect_success 'git branch --column with an extremely long branch name' '
 173        long=this/is/a/part/of/long/branch/name &&
 174        long=z$long/$long/$long/$long &&
 175        test_when_finished "git branch -d $long" &&
 176        git branch $long &&
 177        COLUMNS=80 git branch --column=column >actual &&
 178        cat >expected <<EOF &&
 179  a/b/c
 180  abc
 181  bam
 182  bar
 183  foo
 184  j/k
 185  l
 186  m/m
 187* master
 188  master2
 189  n
 190  o/o
 191  o/p
 192  q
 193  r
 194  $long
 195EOF
 196        test_cmp expected actual
 197'
 198
 199test_expect_success 'git branch with column.*' '
 200        git config column.ui column &&
 201        git config column.branch "dense" &&
 202        COLUMNS=80 git branch >actual &&
 203        git config --unset column.branch &&
 204        git config --unset column.ui &&
 205        cat >expected <<\EOF &&
 206  a/b/c   bam   foo   l   * master    n     o/p   r
 207  abc     bar   j/k   m/m   master2   o/o   q
 208EOF
 209        test_cmp expected actual
 210'
 211
 212test_expect_success 'git branch --column -v should fail' '
 213        test_must_fail git branch --column -v
 214'
 215
 216test_expect_success 'git branch -v with column.ui ignored' '
 217        git config column.ui column &&
 218        COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
 219        git config --unset column.ui &&
 220        cat >expected <<\EOF &&
 221  a/b/c
 222  abc
 223  bam
 224  bar
 225  foo
 226  j/k
 227  l
 228  m/m
 229* master
 230  master2
 231  n
 232  o/o
 233  o/p
 234  q
 235  r
 236EOF
 237        test_cmp expected actual
 238'
 239
 240mv .git/config .git/config-saved
 241
 242test_expect_success 'git branch -m q q2 without config should succeed' '
 243        git branch -m q q2 &&
 244        git branch -m q2 q
 245'
 246
 247mv .git/config-saved .git/config
 248
 249git config branch.s/s.dummy Hello
 250
 251test_expect_success \
 252    'git branch -m s/s s should work when s/t is deleted' \
 253       'git branch -l s/s &&
 254        test_path_is_file .git/logs/refs/heads/s/s &&
 255        git branch -l s/t &&
 256        test_path_is_file .git/logs/refs/heads/s/t &&
 257        git branch -d s/t &&
 258        git branch -m s/s s &&
 259        test_path_is_file .git/logs/refs/heads/s'
 260
 261test_expect_success 'config information was renamed, too' \
 262        "test $(git config branch.s.dummy) = Hello &&
 263         test_must_fail git config branch.s/s/dummy"
 264
 265test_expect_success 'deleting a symref' '
 266        git branch target &&
 267        git symbolic-ref refs/heads/symref refs/heads/target &&
 268        echo "Deleted branch symref (was refs/heads/target)." >expect &&
 269        git branch -d symref >actual &&
 270        test_path_is_file .git/refs/heads/target &&
 271        test_path_is_missing .git/refs/heads/symref &&
 272        test_i18ncmp expect actual
 273'
 274
 275test_expect_success 'deleting a dangling symref' '
 276        git symbolic-ref refs/heads/dangling-symref nowhere &&
 277        test_path_is_file .git/refs/heads/dangling-symref &&
 278        echo "Deleted branch dangling-symref (was nowhere)." >expect &&
 279        git branch -d dangling-symref >actual &&
 280        test_path_is_missing .git/refs/heads/dangling-symref &&
 281        test_i18ncmp expect actual
 282'
 283
 284test_expect_success 'renaming a symref is not allowed' \
 285'
 286        git symbolic-ref refs/heads/master2 refs/heads/master &&
 287        test_must_fail git branch -m master2 master3 &&
 288        git symbolic-ref refs/heads/master2 &&
 289        test_path_is_file .git/refs/heads/master &&
 290        test_path_is_missing .git/refs/heads/master3
 291'
 292
 293test_expect_success SYMLINKS \
 294    'git branch -m u v should fail when the reflog for u is a symlink' '
 295     git branch -l u &&
 296     mv .git/logs/refs/heads/u real-u &&
 297     ln -s real-u .git/logs/refs/heads/u &&
 298     test_must_fail git branch -m u v
 299'
 300
 301test_expect_success 'test tracking setup via --track' \
 302    'git config remote.local.url . &&
 303     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 304     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 305     git branch --track my1 local/master &&
 306     test $(git config branch.my1.remote) = local &&
 307     test $(git config branch.my1.merge) = refs/heads/master'
 308
 309test_expect_success 'test tracking setup (non-wildcard, matching)' \
 310    'git config remote.local.url . &&
 311     git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
 312     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 313     git branch --track my4 local/master &&
 314     test $(git config branch.my4.remote) = local &&
 315     test $(git config branch.my4.merge) = refs/heads/master'
 316
 317test_expect_success 'test tracking setup (non-wildcard, not matching)' \
 318    'git config remote.local.url . &&
 319     git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
 320     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 321     git branch --track my5 local/master &&
 322     ! test "$(git config branch.my5.remote)" = local &&
 323     ! test "$(git config branch.my5.merge)" = refs/heads/master'
 324
 325test_expect_success 'test tracking setup via config' \
 326    'git config branch.autosetupmerge true &&
 327     git config remote.local.url . &&
 328     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 329     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 330     git branch my3 local/master &&
 331     test $(git config branch.my3.remote) = local &&
 332     test $(git config branch.my3.merge) = refs/heads/master'
 333
 334test_expect_success 'test overriding tracking setup via --no-track' \
 335    'git config branch.autosetupmerge true &&
 336     git config remote.local.url . &&
 337     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 338     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 339     git branch --no-track my2 local/master &&
 340     git config branch.autosetupmerge false &&
 341     ! test "$(git config branch.my2.remote)" = local &&
 342     ! test "$(git config branch.my2.merge)" = refs/heads/master'
 343
 344test_expect_success 'no tracking without .fetch entries' \
 345    'git config branch.autosetupmerge true &&
 346     git branch my6 s &&
 347     git config branch.automsetupmerge false &&
 348     test -z "$(git config branch.my6.remote)" &&
 349     test -z "$(git config branch.my6.merge)"'
 350
 351test_expect_success 'test tracking setup via --track but deeper' \
 352    'git config remote.local.url . &&
 353     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 354     (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
 355     git branch --track my7 local/o/o &&
 356     test "$(git config branch.my7.remote)" = local &&
 357     test "$(git config branch.my7.merge)" = refs/heads/o/o'
 358
 359test_expect_success 'test deleting branch deletes branch config' \
 360    'git branch -d my7 &&
 361     test -z "$(git config branch.my7.remote)" &&
 362     test -z "$(git config branch.my7.merge)"'
 363
 364test_expect_success 'test deleting branch without config' \
 365    'git branch my7 s &&
 366     sha1=$(git rev-parse my7 | cut -c 1-7) &&
 367     echo "Deleted branch my7 (was $sha1)." >expect &&
 368     git branch -d my7 >actual 2>&1 &&
 369     test_i18ncmp expect actual'
 370
 371test_expect_success 'test --track without .fetch entries' \
 372    'git branch --track my8 &&
 373     test "$(git config branch.my8.remote)" &&
 374     test "$(git config branch.my8.merge)"'
 375
 376test_expect_success \
 377    'branch from non-branch HEAD w/autosetupmerge=always' \
 378    'git config branch.autosetupmerge always &&
 379     git branch my9 HEAD^ &&
 380     git config branch.autosetupmerge false'
 381
 382test_expect_success \
 383    'branch from non-branch HEAD w/--track causes failure' \
 384    'test_must_fail git branch --track my10 HEAD^'
 385
 386test_expect_success \
 387    'branch from tag w/--track causes failure' \
 388    'git tag foobar &&
 389     test_must_fail git branch --track my11 foobar'
 390
 391test_expect_success 'use --set-upstream-to modify HEAD' \
 392    'test_config branch.master.remote foo &&
 393     test_config branch.master.merge foo &&
 394     git branch my12
 395     git branch --set-upstream-to my12 &&
 396     test "$(git config branch.master.remote)" = "." &&
 397     test "$(git config branch.master.merge)" = "refs/heads/my12"'
 398
 399test_expect_success 'use --set-upstream-to modify a particular branch' \
 400    'git branch my13
 401     git branch --set-upstream-to master my13 &&
 402     test "$(git config branch.my13.remote)" = "." &&
 403     test "$(git config branch.my13.merge)" = "refs/heads/master"'
 404
 405test_expect_success '--unset-upstream should fail if given a non-existent branch' \
 406    'test_must_fail git branch --unset-upstream i-dont-exist'
 407
 408test_expect_success 'test --unset-upstream on HEAD' \
 409    'git branch my14
 410     test_config branch.master.remote foo &&
 411     test_config branch.master.merge foo &&
 412     git branch --set-upstream-to my14 &&
 413     git branch --unset-upstream &&
 414     test_must_fail git config branch.master.remote &&
 415     test_must_fail git config branch.master.merge &&
 416     # fail for a branch without upstream set
 417     test_must_fail git branch --unset-upstream
 418'
 419
 420test_expect_success 'test --unset-upstream on a particular branch' \
 421    'git branch my15
 422     git branch --set-upstream-to master my14 &&
 423     git branch --unset-upstream my14 &&
 424     test_must_fail git config branch.my14.remote &&
 425     test_must_fail git config branch.my14.merge'
 426
 427test_expect_success '--set-upstream shows message when creating a new branch that exists as remote-tracking' \
 428    'git update-ref refs/remotes/origin/master HEAD &&
 429     git branch --set-upstream origin/master 2>actual &&
 430     test_when_finished git update-ref -d refs/remotes/origin/master &&
 431     test_when_finished git branch -d origin/master &&
 432     cat >expected <<EOF &&
 433The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
 434
 435If you wanted to make '"'master'"' track '"'origin/master'"', do this:
 436
 437    git branch -d origin/master
 438    git branch --set-upstream-to origin/master
 439EOF
 440     test_cmp expected actual
 441'
 442
 443test_expect_success '--set-upstream with two args only shows the deprecation message' \
 444    'git branch --set-upstream master my13 2>actual &&
 445     test_when_finished git branch --unset-upstream master &&
 446     cat >expected <<EOF &&
 447The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
 448EOF
 449     test_cmp expected actual
 450'
 451
 452test_expect_success '--set-upstream with one arg only shows the deprecation message if the branch existed' \
 453    'git branch --set-upstream my13 2>actual &&
 454     test_when_finished git branch --unset-upstream my13 &&
 455     cat >expected <<EOF &&
 456The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
 457EOF
 458     test_cmp expected actual
 459'
 460
 461# Keep this test last, as it changes the current branch
 462cat >expect <<EOF
 463$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
 464EOF
 465test_expect_success \
 466    'git checkout -b g/h/i -l should create a branch and a log' \
 467        'GIT_COMMITTER_DATE="2005-05-26 23:30" \
 468     git checkout -b g/h/i -l master &&
 469         test_path_is_file .git/refs/heads/g/h/i &&
 470         test_path_is_file .git/logs/refs/heads/g/h/i &&
 471         test_cmp expect .git/logs/refs/heads/g/h/i'
 472
 473test_expect_success 'checkout -b makes reflog by default' '
 474        git checkout master &&
 475        git config --unset core.logAllRefUpdates &&
 476        git checkout -b alpha &&
 477        git rev-parse --verify alpha@{0}
 478'
 479
 480test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
 481        git checkout master &&
 482        git config core.logAllRefUpdates false &&
 483        git checkout -b beta &&
 484        test_must_fail git rev-parse --verify beta@{0}
 485'
 486
 487test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
 488        git checkout master &&
 489        git checkout -lb gamma &&
 490        git config --unset core.logAllRefUpdates &&
 491        git rev-parse --verify gamma@{0}
 492'
 493
 494test_expect_success 'avoid ambiguous track' '
 495        git config branch.autosetupmerge true &&
 496        git config remote.ambi1.url lalala &&
 497        git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
 498        git config remote.ambi2.url lilili &&
 499        git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
 500        git branch all1 master &&
 501        test -z "$(git config branch.all1.merge)"
 502'
 503
 504test_expect_success 'autosetuprebase local on a tracked local branch' '
 505        git config remote.local.url . &&
 506        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 507        git config branch.autosetuprebase local &&
 508        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 509        git branch mybase &&
 510        git branch --track myr1 mybase &&
 511        test "$(git config branch.myr1.remote)" = . &&
 512        test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
 513        test "$(git config branch.myr1.rebase)" = true
 514'
 515
 516test_expect_success 'autosetuprebase always on a tracked local branch' '
 517        git config remote.local.url . &&
 518        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 519        git config branch.autosetuprebase always &&
 520        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 521        git branch mybase2 &&
 522        git branch --track myr2 mybase &&
 523        test "$(git config branch.myr2.remote)" = . &&
 524        test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
 525        test "$(git config branch.myr2.rebase)" = true
 526'
 527
 528test_expect_success 'autosetuprebase remote on a tracked local branch' '
 529        git config remote.local.url . &&
 530        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 531        git config branch.autosetuprebase remote &&
 532        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 533        git branch mybase3 &&
 534        git branch --track myr3 mybase2 &&
 535        test "$(git config branch.myr3.remote)" = . &&
 536        test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
 537        ! test "$(git config branch.myr3.rebase)" = true
 538'
 539
 540test_expect_success 'autosetuprebase never on a tracked local branch' '
 541        git config remote.local.url . &&
 542        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 543        git config branch.autosetuprebase never &&
 544        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 545        git branch mybase4 &&
 546        git branch --track myr4 mybase2 &&
 547        test "$(git config branch.myr4.remote)" = . &&
 548        test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
 549        ! test "$(git config branch.myr4.rebase)" = true
 550'
 551
 552test_expect_success 'autosetuprebase local on a tracked remote branch' '
 553        git config remote.local.url . &&
 554        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 555        git config branch.autosetuprebase local &&
 556        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 557        git branch --track myr5 local/master &&
 558        test "$(git config branch.myr5.remote)" = local &&
 559        test "$(git config branch.myr5.merge)" = refs/heads/master &&
 560        ! test "$(git config branch.myr5.rebase)" = true
 561'
 562
 563test_expect_success 'autosetuprebase never on a tracked remote branch' '
 564        git config remote.local.url . &&
 565        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 566        git config branch.autosetuprebase never &&
 567        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 568        git branch --track myr6 local/master &&
 569        test "$(git config branch.myr6.remote)" = local &&
 570        test "$(git config branch.myr6.merge)" = refs/heads/master &&
 571        ! test "$(git config branch.myr6.rebase)" = true
 572'
 573
 574test_expect_success 'autosetuprebase remote on a tracked remote branch' '
 575        git config remote.local.url . &&
 576        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 577        git config branch.autosetuprebase remote &&
 578        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 579        git branch --track myr7 local/master &&
 580        test "$(git config branch.myr7.remote)" = local &&
 581        test "$(git config branch.myr7.merge)" = refs/heads/master &&
 582        test "$(git config branch.myr7.rebase)" = true
 583'
 584
 585test_expect_success 'autosetuprebase always on a tracked remote branch' '
 586        git config remote.local.url . &&
 587        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 588        git config branch.autosetuprebase remote &&
 589        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 590        git branch --track myr8 local/master &&
 591        test "$(git config branch.myr8.remote)" = local &&
 592        test "$(git config branch.myr8.merge)" = refs/heads/master &&
 593        test "$(git config branch.myr8.rebase)" = true
 594'
 595
 596test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
 597        git config --unset branch.autosetuprebase &&
 598        git config remote.local.url . &&
 599        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 600        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 601        git branch --track myr9 local/master &&
 602        test "$(git config branch.myr9.remote)" = local &&
 603        test "$(git config branch.myr9.merge)" = refs/heads/master &&
 604        test "z$(git config branch.myr9.rebase)" = z
 605'
 606
 607test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
 608        git config remote.local.url . &&
 609        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 610        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 611        git branch mybase10 &&
 612        git branch --track myr10 mybase2 &&
 613        test "$(git config branch.myr10.remote)" = . &&
 614        test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
 615        test "z$(git config branch.myr10.rebase)" = z
 616'
 617
 618test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
 619        git config remote.local.url . &&
 620        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 621        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 622        git branch --no-track myr11 mybase2 &&
 623        test "z$(git config branch.myr11.remote)" = z &&
 624        test "z$(git config branch.myr11.merge)" = z &&
 625        test "z$(git config branch.myr11.rebase)" = z
 626'
 627
 628test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
 629        git config remote.local.url . &&
 630        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 631        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 632        git branch --no-track myr12 local/master &&
 633        test "z$(git config branch.myr12.remote)" = z &&
 634        test "z$(git config branch.myr12.merge)" = z &&
 635        test "z$(git config branch.myr12.rebase)" = z
 636'
 637
 638test_expect_success 'autosetuprebase never on an untracked local branch' '
 639        git config branch.autosetuprebase never &&
 640        git config remote.local.url . &&
 641        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 642        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 643        git branch --no-track myr13 mybase2 &&
 644        test "z$(git config branch.myr13.remote)" = z &&
 645        test "z$(git config branch.myr13.merge)" = z &&
 646        test "z$(git config branch.myr13.rebase)" = z
 647'
 648
 649test_expect_success 'autosetuprebase local on an untracked local branch' '
 650        git config branch.autosetuprebase local &&
 651        git config remote.local.url . &&
 652        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 653        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 654        git branch --no-track myr14 mybase2 &&
 655        test "z$(git config branch.myr14.remote)" = z &&
 656        test "z$(git config branch.myr14.merge)" = z &&
 657        test "z$(git config branch.myr14.rebase)" = z
 658'
 659
 660test_expect_success 'autosetuprebase remote on an untracked local branch' '
 661        git config branch.autosetuprebase remote &&
 662        git config remote.local.url . &&
 663        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 664        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 665        git branch --no-track myr15 mybase2 &&
 666        test "z$(git config branch.myr15.remote)" = z &&
 667        test "z$(git config branch.myr15.merge)" = z &&
 668        test "z$(git config branch.myr15.rebase)" = z
 669'
 670
 671test_expect_success 'autosetuprebase always on an untracked local branch' '
 672        git config branch.autosetuprebase always &&
 673        git config remote.local.url . &&
 674        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 675        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 676        git branch --no-track myr16 mybase2 &&
 677        test "z$(git config branch.myr16.remote)" = z &&
 678        test "z$(git config branch.myr16.merge)" = z &&
 679        test "z$(git config branch.myr16.rebase)" = z
 680'
 681
 682test_expect_success 'autosetuprebase never on an untracked remote branch' '
 683        git config branch.autosetuprebase never &&
 684        git config remote.local.url . &&
 685        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 686        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 687        git branch --no-track myr17 local/master &&
 688        test "z$(git config branch.myr17.remote)" = z &&
 689        test "z$(git config branch.myr17.merge)" = z &&
 690        test "z$(git config branch.myr17.rebase)" = z
 691'
 692
 693test_expect_success 'autosetuprebase local on an untracked remote branch' '
 694        git config branch.autosetuprebase local &&
 695        git config remote.local.url . &&
 696        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 697        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 698        git branch --no-track myr18 local/master &&
 699        test "z$(git config branch.myr18.remote)" = z &&
 700        test "z$(git config branch.myr18.merge)" = z &&
 701        test "z$(git config branch.myr18.rebase)" = z
 702'
 703
 704test_expect_success 'autosetuprebase remote on an untracked remote branch' '
 705        git config branch.autosetuprebase remote &&
 706        git config remote.local.url . &&
 707        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 708        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 709        git branch --no-track myr19 local/master &&
 710        test "z$(git config branch.myr19.remote)" = z &&
 711        test "z$(git config branch.myr19.merge)" = z &&
 712        test "z$(git config branch.myr19.rebase)" = z
 713'
 714
 715test_expect_success 'autosetuprebase always on an untracked remote branch' '
 716        git config branch.autosetuprebase always &&
 717        git config remote.local.url . &&
 718        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 719        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 720        git branch --no-track myr20 local/master &&
 721        test "z$(git config branch.myr20.remote)" = z &&
 722        test "z$(git config branch.myr20.merge)" = z &&
 723        test "z$(git config branch.myr20.rebase)" = z
 724'
 725
 726test_expect_success 'autosetuprebase always on detached HEAD' '
 727        git config branch.autosetupmerge always &&
 728        test_when_finished git checkout master &&
 729        git checkout HEAD^0 &&
 730        git branch my11 &&
 731        test -z "$(git config branch.my11.remote)" &&
 732        test -z "$(git config branch.my11.merge)"
 733'
 734
 735test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
 736        git config branch.autosetuprebase garbage &&
 737        test_must_fail git branch
 738'
 739
 740test_expect_success 'detect misconfigured autosetuprebase (no value)' '
 741        git config --unset branch.autosetuprebase &&
 742        echo "[branch] autosetuprebase" >> .git/config &&
 743        test_must_fail git branch &&
 744        git config --unset branch.autosetuprebase
 745'
 746
 747test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
 748        git checkout my9 &&
 749        git config --unset branch.my8.merge &&
 750        test_must_fail git branch -d my8
 751'
 752
 753test_expect_success 'attempt to delete a branch merged to its base' '
 754        # we are on my9 which is the initial commit; traditionally
 755        # we would not have allowed deleting my8 that is not merged
 756        # to my9, but it is set to track master that already has my8
 757        git config branch.my8.merge refs/heads/master &&
 758        git branch -d my8
 759'
 760
 761test_expect_success 'attempt to delete a branch merged to its base' '
 762        git checkout master &&
 763        echo Third >>A &&
 764        git commit -m "Third commit" A &&
 765        git branch -t my10 my9 &&
 766        git branch -f my10 HEAD^ &&
 767        # we are on master which is at the third commit, and my10
 768        # is behind us, so traditionally we would have allowed deleting
 769        # it; but my10 is set to track my9 that is further behind.
 770        test_must_fail git branch -d my10
 771'
 772
 773test_expect_success 'use set-upstream on the current branch' '
 774        git checkout master &&
 775        git --bare init myupstream.git &&
 776        git push myupstream.git master:refs/heads/frotz &&
 777        git remote add origin myupstream.git &&
 778        git fetch &&
 779        git branch --set-upstream master origin/frotz &&
 780
 781        test "z$(git config branch.master.remote)" = "zorigin" &&
 782        test "z$(git config branch.master.merge)" = "zrefs/heads/frotz"
 783
 784'
 785
 786test_expect_success 'use --edit-description' '
 787        write_script editor <<-\EOF &&
 788                echo "New contents" >"$1"
 789        EOF
 790        EDITOR=./editor git branch --edit-description &&
 791                write_script editor <<-\EOF &&
 792                git stripspace -s <"$1" >"EDITOR_OUTPUT"
 793        EOF
 794        EDITOR=./editor git branch --edit-description &&
 795        echo "New contents" >expect &&
 796        test_cmp EDITOR_OUTPUT expect
 797'
 798
 799test_expect_success 'detect typo in branch name when using --edit-description' '
 800        write_script editor <<-\EOF &&
 801                echo "New contents" >"$1"
 802        EOF
 803        (
 804                EDITOR=./editor &&
 805                export EDITOR &&
 806                test_must_fail git branch --edit-description no-such-branch
 807        )
 808'
 809
 810test_expect_success 'refuse --edit-description on unborn branch for now' '
 811        write_script editor <<-\EOF &&
 812                echo "New contents" >"$1"
 813        EOF
 814        git checkout --orphan unborn &&
 815        (
 816                EDITOR=./editor &&
 817                export EDITOR &&
 818                test_must_fail git branch --edit-description
 819        )
 820'
 821
 822test_expect_success '--merged catches invalid object names' '
 823        test_must_fail git branch --merged 0000000000000000000000000000000000000000
 824'
 825
 826test_done