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