1323f6ff718d876ef06a071504390afa19a885a1
   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 129 git branch -m 2>err &&
  77        test_i18ngrep "[Uu]sage: git branch" 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        sha1=$(git rev-parse symref | cut -c 1-7) &&
 269        echo "Deleted branch symref (was $sha1)." >expect &&
 270        git branch -d symref >actual &&
 271        test_path_is_file .git/refs/heads/target &&
 272        test_path_is_missing .git/refs/heads/symref &&
 273        test_i18ncmp expect actual
 274'
 275
 276test_expect_success 'deleting a dangling symref' '
 277        git symbolic-ref refs/heads/dangling-symref nowhere &&
 278        test_path_is_file .git/refs/heads/dangling-symref &&
 279        echo "Deleted branch dangling-symref (was 0000000)." >expect &&
 280        git branch -d dangling-symref >actual &&
 281        test_path_is_missing .git/refs/heads/dangling-symref &&
 282        test_i18ncmp expect actual
 283'
 284
 285test_expect_success 'renaming a symref is not allowed' \
 286'
 287        git symbolic-ref refs/heads/master2 refs/heads/master &&
 288        test_must_fail git branch -m master2 master3 &&
 289        git symbolic-ref refs/heads/master2 &&
 290        test_path_is_file .git/refs/heads/master &&
 291        test_path_is_missing .git/refs/heads/master3
 292'
 293
 294test_expect_success SYMLINKS \
 295    'git branch -m u v should fail when the reflog for u is a symlink' '
 296     git branch -l u &&
 297     mv .git/logs/refs/heads/u real-u &&
 298     ln -s real-u .git/logs/refs/heads/u &&
 299     test_must_fail git branch -m u v
 300'
 301
 302test_expect_success 'test tracking setup via --track' \
 303    'git config remote.local.url . &&
 304     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 305     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 306     git branch --track my1 local/master &&
 307     test $(git config branch.my1.remote) = local &&
 308     test $(git config branch.my1.merge) = refs/heads/master'
 309
 310test_expect_success 'test tracking setup (non-wildcard, matching)' \
 311    'git config remote.local.url . &&
 312     git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
 313     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 314     git branch --track my4 local/master &&
 315     test $(git config branch.my4.remote) = local &&
 316     test $(git config branch.my4.merge) = refs/heads/master'
 317
 318test_expect_success 'test tracking setup (non-wildcard, not matching)' \
 319    'git config remote.local.url . &&
 320     git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
 321     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 322     git branch --track my5 local/master &&
 323     ! test "$(git config branch.my5.remote)" = local &&
 324     ! test "$(git config branch.my5.merge)" = refs/heads/master'
 325
 326test_expect_success 'test tracking setup via config' \
 327    'git config branch.autosetupmerge true &&
 328     git config remote.local.url . &&
 329     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 330     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 331     git branch my3 local/master &&
 332     test $(git config branch.my3.remote) = local &&
 333     test $(git config branch.my3.merge) = refs/heads/master'
 334
 335test_expect_success 'test overriding tracking setup via --no-track' \
 336    'git config branch.autosetupmerge true &&
 337     git config remote.local.url . &&
 338     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 339     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 340     git branch --no-track my2 local/master &&
 341     git config branch.autosetupmerge false &&
 342     ! test "$(git config branch.my2.remote)" = local &&
 343     ! test "$(git config branch.my2.merge)" = refs/heads/master'
 344
 345test_expect_success 'no tracking without .fetch entries' \
 346    'git config branch.autosetupmerge true &&
 347     git branch my6 s &&
 348     git config branch.automsetupmerge false &&
 349     test -z "$(git config branch.my6.remote)" &&
 350     test -z "$(git config branch.my6.merge)"'
 351
 352test_expect_success 'test tracking setup via --track but deeper' \
 353    'git config remote.local.url . &&
 354     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 355     (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
 356     git branch --track my7 local/o/o &&
 357     test "$(git config branch.my7.remote)" = local &&
 358     test "$(git config branch.my7.merge)" = refs/heads/o/o'
 359
 360test_expect_success 'test deleting branch deletes branch config' \
 361    'git branch -d my7 &&
 362     test -z "$(git config branch.my7.remote)" &&
 363     test -z "$(git config branch.my7.merge)"'
 364
 365test_expect_success 'test deleting branch without config' \
 366    'git branch my7 s &&
 367     sha1=$(git rev-parse my7 | cut -c 1-7) &&
 368     echo "Deleted branch my7 (was $sha1)." >expect &&
 369     git branch -d my7 >actual 2>&1 &&
 370     test_i18ncmp expect actual'
 371
 372test_expect_success 'test --track without .fetch entries' \
 373    'git branch --track my8 &&
 374     test "$(git config branch.my8.remote)" &&
 375     test "$(git config branch.my8.merge)"'
 376
 377test_expect_success \
 378    'branch from non-branch HEAD w/autosetupmerge=always' \
 379    'git config branch.autosetupmerge always &&
 380     git branch my9 HEAD^ &&
 381     git config branch.autosetupmerge false'
 382
 383test_expect_success \
 384    'branch from non-branch HEAD w/--track causes failure' \
 385    'test_must_fail git branch --track my10 HEAD^'
 386
 387test_expect_success \
 388    'branch from tag w/--track causes failure' \
 389    'git tag foobar &&
 390     test_must_fail git branch --track my11 foobar'
 391
 392test_expect_success 'use --set-upstream-to modify HEAD' \
 393    'test_config branch.master.remote foo &&
 394     test_config branch.master.merge foo &&
 395     git branch my12
 396     git branch --set-upstream-to my12 &&
 397     test "$(git config branch.master.remote)" = "." &&
 398     test "$(git config branch.master.merge)" = "refs/heads/my12"'
 399
 400test_expect_success 'use --set-upstream-to modify a particular branch' \
 401    'git branch my13
 402     git branch --set-upstream-to master my13 &&
 403     test "$(git config branch.my13.remote)" = "." &&
 404     test "$(git config branch.my13.merge)" = "refs/heads/master"'
 405
 406test_expect_success '--unset-upstream should fail if given a non-existent branch' \
 407    'test_must_fail git branch --unset-upstream i-dont-exist'
 408
 409test_expect_success 'test --unset-upstream on HEAD' \
 410    'git branch my14
 411     test_config branch.master.remote foo &&
 412     test_config branch.master.merge foo &&
 413     git branch --set-upstream-to my14 &&
 414     git branch --unset-upstream &&
 415     test_must_fail git config branch.master.remote &&
 416     test_must_fail git config branch.master.merge &&
 417     # fail for a branch without upstream set
 418     test_must_fail git branch --unset-upstream
 419'
 420
 421test_expect_success 'test --unset-upstream on a particular branch' \
 422    'git branch my15
 423     git branch --set-upstream-to master my14 &&
 424     git branch --unset-upstream my14 &&
 425     test_must_fail git config branch.my14.remote &&
 426     test_must_fail git config branch.my14.merge'
 427
 428test_expect_success '--set-upstream shows message when creating a new branch that exists as remote-tracking' \
 429    'git update-ref refs/remotes/origin/master HEAD &&
 430     git branch --set-upstream origin/master 2>actual &&
 431     test_when_finished git update-ref -d refs/remotes/origin/master &&
 432     test_when_finished git branch -d origin/master &&
 433     cat >expected <<EOF &&
 434The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
 435
 436If you wanted to make '"'master'"' track '"'origin/master'"', do this:
 437
 438    git branch -d origin/master
 439    git branch --set-upstream-to origin/master
 440EOF
 441     test_cmp expected actual
 442'
 443
 444test_expect_success '--set-upstream with two args only shows the deprecation message' \
 445    'git branch --set-upstream master my13 2>actual &&
 446     test_when_finished git branch --unset-upstream master &&
 447     cat >expected <<EOF &&
 448The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
 449EOF
 450     test_cmp expected actual
 451'
 452
 453test_expect_success '--set-upstream with one arg only shows the deprecation message if the branch existed' \
 454    'git branch --set-upstream my13 2>actual &&
 455     test_when_finished git branch --unset-upstream my13 &&
 456     cat >expected <<EOF &&
 457The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
 458EOF
 459     test_cmp expected actual
 460'
 461
 462# Keep this test last, as it changes the current branch
 463cat >expect <<EOF
 464$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
 465EOF
 466test_expect_success \
 467    'git checkout -b g/h/i -l should create a branch and a log' \
 468        'GIT_COMMITTER_DATE="2005-05-26 23:30" \
 469     git checkout -b g/h/i -l master &&
 470         test_path_is_file .git/refs/heads/g/h/i &&
 471         test_path_is_file .git/logs/refs/heads/g/h/i &&
 472         test_cmp expect .git/logs/refs/heads/g/h/i'
 473
 474test_expect_success 'checkout -b makes reflog by default' '
 475        git checkout master &&
 476        git config --unset core.logAllRefUpdates &&
 477        git checkout -b alpha &&
 478        git rev-parse --verify alpha@{0}
 479'
 480
 481test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
 482        git checkout master &&
 483        git config core.logAllRefUpdates false &&
 484        git checkout -b beta &&
 485        test_must_fail git rev-parse --verify beta@{0}
 486'
 487
 488test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
 489        git checkout master &&
 490        git checkout -lb gamma &&
 491        git config --unset core.logAllRefUpdates &&
 492        git rev-parse --verify gamma@{0}
 493'
 494
 495test_expect_success 'avoid ambiguous track' '
 496        git config branch.autosetupmerge true &&
 497        git config remote.ambi1.url lalala &&
 498        git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
 499        git config remote.ambi2.url lilili &&
 500        git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
 501        git branch all1 master &&
 502        test -z "$(git config branch.all1.merge)"
 503'
 504
 505test_expect_success 'autosetuprebase local on a tracked local branch' '
 506        git config remote.local.url . &&
 507        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 508        git config branch.autosetuprebase local &&
 509        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 510        git branch mybase &&
 511        git branch --track myr1 mybase &&
 512        test "$(git config branch.myr1.remote)" = . &&
 513        test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
 514        test "$(git config branch.myr1.rebase)" = true
 515'
 516
 517test_expect_success 'autosetuprebase always on a tracked local branch' '
 518        git config remote.local.url . &&
 519        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 520        git config branch.autosetuprebase always &&
 521        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 522        git branch mybase2 &&
 523        git branch --track myr2 mybase &&
 524        test "$(git config branch.myr2.remote)" = . &&
 525        test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
 526        test "$(git config branch.myr2.rebase)" = true
 527'
 528
 529test_expect_success 'autosetuprebase remote on a tracked local branch' '
 530        git config remote.local.url . &&
 531        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 532        git config branch.autosetuprebase remote &&
 533        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 534        git branch mybase3 &&
 535        git branch --track myr3 mybase2 &&
 536        test "$(git config branch.myr3.remote)" = . &&
 537        test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
 538        ! test "$(git config branch.myr3.rebase)" = true
 539'
 540
 541test_expect_success 'autosetuprebase never on a tracked local branch' '
 542        git config remote.local.url . &&
 543        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 544        git config branch.autosetuprebase never &&
 545        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 546        git branch mybase4 &&
 547        git branch --track myr4 mybase2 &&
 548        test "$(git config branch.myr4.remote)" = . &&
 549        test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
 550        ! test "$(git config branch.myr4.rebase)" = true
 551'
 552
 553test_expect_success 'autosetuprebase local on a tracked remote branch' '
 554        git config remote.local.url . &&
 555        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 556        git config branch.autosetuprebase local &&
 557        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 558        git branch --track myr5 local/master &&
 559        test "$(git config branch.myr5.remote)" = local &&
 560        test "$(git config branch.myr5.merge)" = refs/heads/master &&
 561        ! test "$(git config branch.myr5.rebase)" = true
 562'
 563
 564test_expect_success 'autosetuprebase never on a tracked remote branch' '
 565        git config remote.local.url . &&
 566        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 567        git config branch.autosetuprebase never &&
 568        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 569        git branch --track myr6 local/master &&
 570        test "$(git config branch.myr6.remote)" = local &&
 571        test "$(git config branch.myr6.merge)" = refs/heads/master &&
 572        ! test "$(git config branch.myr6.rebase)" = true
 573'
 574
 575test_expect_success 'autosetuprebase remote on a tracked remote branch' '
 576        git config remote.local.url . &&
 577        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 578        git config branch.autosetuprebase remote &&
 579        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 580        git branch --track myr7 local/master &&
 581        test "$(git config branch.myr7.remote)" = local &&
 582        test "$(git config branch.myr7.merge)" = refs/heads/master &&
 583        test "$(git config branch.myr7.rebase)" = true
 584'
 585
 586test_expect_success 'autosetuprebase always on a tracked remote branch' '
 587        git config remote.local.url . &&
 588        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 589        git config branch.autosetuprebase remote &&
 590        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 591        git branch --track myr8 local/master &&
 592        test "$(git config branch.myr8.remote)" = local &&
 593        test "$(git config branch.myr8.merge)" = refs/heads/master &&
 594        test "$(git config branch.myr8.rebase)" = true
 595'
 596
 597test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
 598        git config --unset branch.autosetuprebase &&
 599        git config remote.local.url . &&
 600        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 601        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 602        git branch --track myr9 local/master &&
 603        test "$(git config branch.myr9.remote)" = local &&
 604        test "$(git config branch.myr9.merge)" = refs/heads/master &&
 605        test "z$(git config branch.myr9.rebase)" = z
 606'
 607
 608test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
 609        git config remote.local.url . &&
 610        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 611        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 612        git branch mybase10 &&
 613        git branch --track myr10 mybase2 &&
 614        test "$(git config branch.myr10.remote)" = . &&
 615        test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
 616        test "z$(git config branch.myr10.rebase)" = z
 617'
 618
 619test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
 620        git config remote.local.url . &&
 621        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 622        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 623        git branch --no-track myr11 mybase2 &&
 624        test "z$(git config branch.myr11.remote)" = z &&
 625        test "z$(git config branch.myr11.merge)" = z &&
 626        test "z$(git config branch.myr11.rebase)" = z
 627'
 628
 629test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
 630        git config remote.local.url . &&
 631        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 632        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 633        git branch --no-track myr12 local/master &&
 634        test "z$(git config branch.myr12.remote)" = z &&
 635        test "z$(git config branch.myr12.merge)" = z &&
 636        test "z$(git config branch.myr12.rebase)" = z
 637'
 638
 639test_expect_success 'autosetuprebase never on an untracked local branch' '
 640        git config branch.autosetuprebase never &&
 641        git config remote.local.url . &&
 642        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 643        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 644        git branch --no-track myr13 mybase2 &&
 645        test "z$(git config branch.myr13.remote)" = z &&
 646        test "z$(git config branch.myr13.merge)" = z &&
 647        test "z$(git config branch.myr13.rebase)" = z
 648'
 649
 650test_expect_success 'autosetuprebase local on an untracked local branch' '
 651        git config branch.autosetuprebase local &&
 652        git config remote.local.url . &&
 653        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 654        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 655        git branch --no-track myr14 mybase2 &&
 656        test "z$(git config branch.myr14.remote)" = z &&
 657        test "z$(git config branch.myr14.merge)" = z &&
 658        test "z$(git config branch.myr14.rebase)" = z
 659'
 660
 661test_expect_success 'autosetuprebase remote on an untracked local branch' '
 662        git config branch.autosetuprebase remote &&
 663        git config remote.local.url . &&
 664        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 665        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 666        git branch --no-track myr15 mybase2 &&
 667        test "z$(git config branch.myr15.remote)" = z &&
 668        test "z$(git config branch.myr15.merge)" = z &&
 669        test "z$(git config branch.myr15.rebase)" = z
 670'
 671
 672test_expect_success 'autosetuprebase always on an untracked local branch' '
 673        git config branch.autosetuprebase always &&
 674        git config remote.local.url . &&
 675        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 676        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 677        git branch --no-track myr16 mybase2 &&
 678        test "z$(git config branch.myr16.remote)" = z &&
 679        test "z$(git config branch.myr16.merge)" = z &&
 680        test "z$(git config branch.myr16.rebase)" = z
 681'
 682
 683test_expect_success 'autosetuprebase never on an untracked remote branch' '
 684        git config branch.autosetuprebase never &&
 685        git config remote.local.url . &&
 686        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 687        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 688        git branch --no-track myr17 local/master &&
 689        test "z$(git config branch.myr17.remote)" = z &&
 690        test "z$(git config branch.myr17.merge)" = z &&
 691        test "z$(git config branch.myr17.rebase)" = z
 692'
 693
 694test_expect_success 'autosetuprebase local on an untracked remote branch' '
 695        git config branch.autosetuprebase local &&
 696        git config remote.local.url . &&
 697        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 698        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 699        git branch --no-track myr18 local/master &&
 700        test "z$(git config branch.myr18.remote)" = z &&
 701        test "z$(git config branch.myr18.merge)" = z &&
 702        test "z$(git config branch.myr18.rebase)" = z
 703'
 704
 705test_expect_success 'autosetuprebase remote on an untracked remote branch' '
 706        git config branch.autosetuprebase remote &&
 707        git config remote.local.url . &&
 708        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 709        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 710        git branch --no-track myr19 local/master &&
 711        test "z$(git config branch.myr19.remote)" = z &&
 712        test "z$(git config branch.myr19.merge)" = z &&
 713        test "z$(git config branch.myr19.rebase)" = z
 714'
 715
 716test_expect_success 'autosetuprebase always on an untracked remote branch' '
 717        git config branch.autosetuprebase always &&
 718        git config remote.local.url . &&
 719        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 720        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 721        git branch --no-track myr20 local/master &&
 722        test "z$(git config branch.myr20.remote)" = z &&
 723        test "z$(git config branch.myr20.merge)" = z &&
 724        test "z$(git config branch.myr20.rebase)" = z
 725'
 726
 727test_expect_success 'autosetuprebase always on detached HEAD' '
 728        git config branch.autosetupmerge always &&
 729        test_when_finished git checkout master &&
 730        git checkout HEAD^0 &&
 731        git branch my11 &&
 732        test -z "$(git config branch.my11.remote)" &&
 733        test -z "$(git config branch.my11.merge)"
 734'
 735
 736test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
 737        git config branch.autosetuprebase garbage &&
 738        test_must_fail git branch
 739'
 740
 741test_expect_success 'detect misconfigured autosetuprebase (no value)' '
 742        git config --unset branch.autosetuprebase &&
 743        echo "[branch] autosetuprebase" >> .git/config &&
 744        test_must_fail git branch &&
 745        git config --unset branch.autosetuprebase
 746'
 747
 748test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
 749        git checkout my9 &&
 750        git config --unset branch.my8.merge &&
 751        test_must_fail git branch -d my8
 752'
 753
 754test_expect_success 'attempt to delete a branch merged to its base' '
 755        # we are on my9 which is the initial commit; traditionally
 756        # we would not have allowed deleting my8 that is not merged
 757        # to my9, but it is set to track master that already has my8
 758        git config branch.my8.merge refs/heads/master &&
 759        git branch -d my8
 760'
 761
 762test_expect_success 'attempt to delete a branch merged to its base' '
 763        git checkout master &&
 764        echo Third >>A &&
 765        git commit -m "Third commit" A &&
 766        git branch -t my10 my9 &&
 767        git branch -f my10 HEAD^ &&
 768        # we are on master which is at the third commit, and my10
 769        # is behind us, so traditionally we would have allowed deleting
 770        # it; but my10 is set to track my9 that is further behind.
 771        test_must_fail git branch -d my10
 772'
 773
 774test_expect_success 'use set-upstream on the current branch' '
 775        git checkout master &&
 776        git --bare init myupstream.git &&
 777        git push myupstream.git master:refs/heads/frotz &&
 778        git remote add origin myupstream.git &&
 779        git fetch &&
 780        git branch --set-upstream master origin/frotz &&
 781
 782        test "z$(git config branch.master.remote)" = "zorigin" &&
 783        test "z$(git config branch.master.merge)" = "zrefs/heads/frotz"
 784
 785'
 786
 787test_expect_success 'use --edit-description' '
 788        write_script editor <<-\EOF &&
 789                echo "New contents" >"$1"
 790        EOF
 791        EDITOR=./editor git branch --edit-description &&
 792                write_script editor <<-\EOF &&
 793                git stripspace -s <"$1" >"EDITOR_OUTPUT"
 794        EOF
 795        EDITOR=./editor git branch --edit-description &&
 796        echo "New contents" >expect &&
 797        test_cmp EDITOR_OUTPUT expect
 798'
 799
 800test_expect_success 'detect typo in branch name when using --edit-description' '
 801        write_script editor <<-\EOF &&
 802                echo "New contents" >"$1"
 803        EOF
 804        (
 805                EDITOR=./editor &&
 806                export EDITOR &&
 807                test_must_fail git branch --edit-description no-such-branch
 808        )
 809'
 810
 811test_expect_success 'refuse --edit-description on unborn branch for now' '
 812        write_script editor <<-\EOF &&
 813                echo "New contents" >"$1"
 814        EOF
 815        git checkout --orphan unborn &&
 816        (
 817                EDITOR=./editor &&
 818                export EDITOR &&
 819                test_must_fail git branch --edit-description
 820        )
 821'
 822
 823test_expect_success '--merged catches invalid object names' '
 824        test_must_fail git branch --merged 0000000000000000000000000000000000000000
 825'
 826
 827test_done