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