t / t3200-branch.shon commit i18n: rebase-interactive: mark comments of squash for translation (f2d1706)
   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 'deleting currently checked out branch fails' '
 428        git worktree add -b my7 my7 &&
 429        test_must_fail git -C my7 branch -d my7 &&
 430        test_must_fail git branch -d my7
 431'
 432
 433test_expect_success 'test --track without .fetch entries' '
 434        git branch --track my8 &&
 435        test "$(git config branch.my8.remote)" &&
 436        test "$(git config branch.my8.merge)"
 437'
 438
 439test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
 440        git config branch.autosetupmerge always &&
 441        git branch my9 HEAD^ &&
 442        git config branch.autosetupmerge false
 443'
 444
 445test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
 446        test_must_fail git branch --track my10 HEAD^
 447'
 448
 449test_expect_success 'branch from tag w/--track causes failure' '
 450        git tag foobar &&
 451        test_must_fail git branch --track my11 foobar
 452'
 453
 454test_expect_success '--set-upstream-to fails on multiple branches' '
 455        test_must_fail git branch --set-upstream-to master a b c
 456'
 457
 458test_expect_success '--set-upstream-to fails on detached HEAD' '
 459        git checkout HEAD^{} &&
 460        test_must_fail git branch --set-upstream-to master &&
 461        git checkout -
 462'
 463
 464test_expect_success '--set-upstream-to fails on a missing dst branch' '
 465        test_must_fail git branch --set-upstream-to master does-not-exist
 466'
 467
 468test_expect_success '--set-upstream-to fails on a missing src branch' '
 469        test_must_fail git branch --set-upstream-to does-not-exist master
 470'
 471
 472test_expect_success '--set-upstream-to fails on a non-ref' '
 473        test_must_fail git branch --set-upstream-to HEAD^{}
 474'
 475
 476test_expect_success '--set-upstream-to fails on locked config' '
 477        test_when_finished "rm -f .git/config.lock" &&
 478        >.git/config.lock &&
 479        git branch locked &&
 480        test_must_fail git branch --set-upstream-to locked
 481'
 482
 483test_expect_success 'use --set-upstream-to modify HEAD' '
 484        test_config branch.master.remote foo &&
 485        test_config branch.master.merge foo &&
 486        git branch my12 &&
 487        git branch --set-upstream-to my12 &&
 488        test "$(git config branch.master.remote)" = "." &&
 489        test "$(git config branch.master.merge)" = "refs/heads/my12"
 490'
 491
 492test_expect_success 'use --set-upstream-to modify a particular branch' '
 493        git branch my13 &&
 494        git branch --set-upstream-to master my13 &&
 495        test "$(git config branch.my13.remote)" = "." &&
 496        test "$(git config branch.my13.merge)" = "refs/heads/master"
 497'
 498
 499test_expect_success '--unset-upstream should fail if given a non-existent branch' '
 500        test_must_fail git branch --unset-upstream i-dont-exist
 501'
 502
 503test_expect_success '--unset-upstream should fail if config is locked' '
 504        test_when_finished "rm -f .git/config.lock" &&
 505        git branch --set-upstream-to locked &&
 506        >.git/config.lock &&
 507        test_must_fail git branch --unset-upstream
 508'
 509
 510test_expect_success 'test --unset-upstream on HEAD' '
 511        git branch my14 &&
 512        test_config branch.master.remote foo &&
 513        test_config branch.master.merge foo &&
 514        git branch --set-upstream-to my14 &&
 515        git branch --unset-upstream &&
 516        test_must_fail git config branch.master.remote &&
 517        test_must_fail git config branch.master.merge &&
 518        # fail for a branch without upstream set
 519        test_must_fail git branch --unset-upstream
 520'
 521
 522test_expect_success '--unset-upstream should fail on multiple branches' '
 523        test_must_fail git branch --unset-upstream a b c
 524'
 525
 526test_expect_success '--unset-upstream should fail on detached HEAD' '
 527        git checkout HEAD^{} &&
 528        test_must_fail git branch --unset-upstream &&
 529        git checkout -
 530'
 531
 532test_expect_success 'test --unset-upstream on a particular branch' '
 533        git branch my15 &&
 534        git branch --set-upstream-to master my14 &&
 535        git branch --unset-upstream my14 &&
 536        test_must_fail git config branch.my14.remote &&
 537        test_must_fail git config branch.my14.merge
 538'
 539
 540test_expect_success '--set-upstream shows message when creating a new branch that exists as remote-tracking' '
 541        git update-ref refs/remotes/origin/master HEAD &&
 542        git branch --set-upstream origin/master 2>actual &&
 543        test_when_finished git update-ref -d refs/remotes/origin/master &&
 544        test_when_finished git branch -d origin/master &&
 545        cat >expected <<EOF &&
 546The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
 547
 548If you wanted to make '"'master'"' track '"'origin/master'"', do this:
 549
 550    git branch -d origin/master
 551    git branch --set-upstream-to origin/master
 552EOF
 553        test_cmp expected actual
 554'
 555
 556test_expect_success '--set-upstream with two args only shows the deprecation message' '
 557        git branch --set-upstream master my13 2>actual &&
 558        test_when_finished git branch --unset-upstream master &&
 559        cat >expected <<EOF &&
 560The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
 561EOF
 562        test_cmp expected actual
 563'
 564
 565test_expect_success '--set-upstream with one arg only shows the deprecation message if the branch existed' '
 566        git branch --set-upstream my13 2>actual &&
 567        test_when_finished git branch --unset-upstream my13 &&
 568        cat >expected <<EOF &&
 569The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
 570EOF
 571        test_cmp expected actual
 572'
 573
 574test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
 575        git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
 576        cat >expected <<-\EOF &&
 577        warning: Not setting branch my13 as its own upstream.
 578        EOF
 579        test_expect_code 1 git config branch.my13.remote &&
 580        test_expect_code 1 git config branch.my13.merge &&
 581        test_i18ncmp expected actual
 582'
 583
 584# Keep this test last, as it changes the current branch
 585cat >expect <<EOF
 586$_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
 587EOF
 588test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
 589        GIT_COMMITTER_DATE="2005-05-26 23:30" \
 590        git checkout -b g/h/i -l master &&
 591        test_path_is_file .git/refs/heads/g/h/i &&
 592        test_path_is_file .git/logs/refs/heads/g/h/i &&
 593        test_cmp expect .git/logs/refs/heads/g/h/i
 594'
 595
 596test_expect_success 'checkout -b makes reflog by default' '
 597        git checkout master &&
 598        git config --unset core.logAllRefUpdates &&
 599        git checkout -b alpha &&
 600        git rev-parse --verify alpha@{0}
 601'
 602
 603test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
 604        git checkout master &&
 605        git config core.logAllRefUpdates false &&
 606        git checkout -b beta &&
 607        test_must_fail git rev-parse --verify beta@{0}
 608'
 609
 610test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
 611        git checkout master &&
 612        git checkout -lb gamma &&
 613        git config --unset core.logAllRefUpdates &&
 614        git rev-parse --verify gamma@{0}
 615'
 616
 617test_expect_success 'avoid ambiguous track' '
 618        git config branch.autosetupmerge true &&
 619        git config remote.ambi1.url lalala &&
 620        git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
 621        git config remote.ambi2.url lilili &&
 622        git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
 623        test_must_fail git branch all1 master &&
 624        test -z "$(git config branch.all1.merge)"
 625'
 626
 627test_expect_success 'autosetuprebase local on a tracked local branch' '
 628        git config remote.local.url . &&
 629        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 630        git config branch.autosetuprebase local &&
 631        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 632        git branch mybase &&
 633        git branch --track myr1 mybase &&
 634        test "$(git config branch.myr1.remote)" = . &&
 635        test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
 636        test "$(git config branch.myr1.rebase)" = true
 637'
 638
 639test_expect_success 'autosetuprebase always on a tracked local branch' '
 640        git config remote.local.url . &&
 641        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 642        git config branch.autosetuprebase always &&
 643        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 644        git branch mybase2 &&
 645        git branch --track myr2 mybase &&
 646        test "$(git config branch.myr2.remote)" = . &&
 647        test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
 648        test "$(git config branch.myr2.rebase)" = true
 649'
 650
 651test_expect_success 'autosetuprebase remote on a tracked local branch' '
 652        git config remote.local.url . &&
 653        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 654        git config branch.autosetuprebase remote &&
 655        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 656        git branch mybase3 &&
 657        git branch --track myr3 mybase2 &&
 658        test "$(git config branch.myr3.remote)" = . &&
 659        test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
 660        ! test "$(git config branch.myr3.rebase)" = true
 661'
 662
 663test_expect_success 'autosetuprebase never on a tracked local branch' '
 664        git config remote.local.url . &&
 665        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 666        git config branch.autosetuprebase never &&
 667        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 668        git branch mybase4 &&
 669        git branch --track myr4 mybase2 &&
 670        test "$(git config branch.myr4.remote)" = . &&
 671        test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
 672        ! test "$(git config branch.myr4.rebase)" = true
 673'
 674
 675test_expect_success 'autosetuprebase local on a tracked remote branch' '
 676        git config remote.local.url . &&
 677        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 678        git config branch.autosetuprebase local &&
 679        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 680        git branch --track myr5 local/master &&
 681        test "$(git config branch.myr5.remote)" = local &&
 682        test "$(git config branch.myr5.merge)" = refs/heads/master &&
 683        ! test "$(git config branch.myr5.rebase)" = true
 684'
 685
 686test_expect_success 'autosetuprebase never on a tracked remote branch' '
 687        git config remote.local.url . &&
 688        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 689        git config branch.autosetuprebase never &&
 690        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 691        git branch --track myr6 local/master &&
 692        test "$(git config branch.myr6.remote)" = local &&
 693        test "$(git config branch.myr6.merge)" = refs/heads/master &&
 694        ! test "$(git config branch.myr6.rebase)" = true
 695'
 696
 697test_expect_success 'autosetuprebase remote on a tracked remote branch' '
 698        git config remote.local.url . &&
 699        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 700        git config branch.autosetuprebase remote &&
 701        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 702        git branch --track myr7 local/master &&
 703        test "$(git config branch.myr7.remote)" = local &&
 704        test "$(git config branch.myr7.merge)" = refs/heads/master &&
 705        test "$(git config branch.myr7.rebase)" = true
 706'
 707
 708test_expect_success 'autosetuprebase always on a tracked remote branch' '
 709        git config remote.local.url . &&
 710        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 711        git config branch.autosetuprebase remote &&
 712        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 713        git branch --track myr8 local/master &&
 714        test "$(git config branch.myr8.remote)" = local &&
 715        test "$(git config branch.myr8.merge)" = refs/heads/master &&
 716        test "$(git config branch.myr8.rebase)" = true
 717'
 718
 719test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
 720        git config --unset branch.autosetuprebase &&
 721        git config remote.local.url . &&
 722        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 723        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 724        git branch --track myr9 local/master &&
 725        test "$(git config branch.myr9.remote)" = local &&
 726        test "$(git config branch.myr9.merge)" = refs/heads/master &&
 727        test "z$(git config branch.myr9.rebase)" = z
 728'
 729
 730test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
 731        git config remote.local.url . &&
 732        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 733        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 734        git branch mybase10 &&
 735        git branch --track myr10 mybase2 &&
 736        test "$(git config branch.myr10.remote)" = . &&
 737        test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
 738        test "z$(git config branch.myr10.rebase)" = z
 739'
 740
 741test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
 742        git config remote.local.url . &&
 743        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 744        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 745        git branch --no-track myr11 mybase2 &&
 746        test "z$(git config branch.myr11.remote)" = z &&
 747        test "z$(git config branch.myr11.merge)" = z &&
 748        test "z$(git config branch.myr11.rebase)" = z
 749'
 750
 751test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
 752        git config remote.local.url . &&
 753        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 754        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 755        git branch --no-track myr12 local/master &&
 756        test "z$(git config branch.myr12.remote)" = z &&
 757        test "z$(git config branch.myr12.merge)" = z &&
 758        test "z$(git config branch.myr12.rebase)" = z
 759'
 760
 761test_expect_success 'autosetuprebase never on an untracked local branch' '
 762        git config branch.autosetuprebase never &&
 763        git config remote.local.url . &&
 764        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 765        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 766        git branch --no-track myr13 mybase2 &&
 767        test "z$(git config branch.myr13.remote)" = z &&
 768        test "z$(git config branch.myr13.merge)" = z &&
 769        test "z$(git config branch.myr13.rebase)" = z
 770'
 771
 772test_expect_success 'autosetuprebase local on an untracked local branch' '
 773        git config branch.autosetuprebase local &&
 774        git config remote.local.url . &&
 775        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 776        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 777        git branch --no-track myr14 mybase2 &&
 778        test "z$(git config branch.myr14.remote)" = z &&
 779        test "z$(git config branch.myr14.merge)" = z &&
 780        test "z$(git config branch.myr14.rebase)" = z
 781'
 782
 783test_expect_success 'autosetuprebase remote on an untracked local branch' '
 784        git config branch.autosetuprebase remote &&
 785        git config remote.local.url . &&
 786        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 787        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 788        git branch --no-track myr15 mybase2 &&
 789        test "z$(git config branch.myr15.remote)" = z &&
 790        test "z$(git config branch.myr15.merge)" = z &&
 791        test "z$(git config branch.myr15.rebase)" = z
 792'
 793
 794test_expect_success 'autosetuprebase always on an untracked local branch' '
 795        git config branch.autosetuprebase always &&
 796        git config remote.local.url . &&
 797        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 798        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 799        git branch --no-track myr16 mybase2 &&
 800        test "z$(git config branch.myr16.remote)" = z &&
 801        test "z$(git config branch.myr16.merge)" = z &&
 802        test "z$(git config branch.myr16.rebase)" = z
 803'
 804
 805test_expect_success 'autosetuprebase never on an untracked remote branch' '
 806        git config branch.autosetuprebase never &&
 807        git config remote.local.url . &&
 808        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 809        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 810        git branch --no-track myr17 local/master &&
 811        test "z$(git config branch.myr17.remote)" = z &&
 812        test "z$(git config branch.myr17.merge)" = z &&
 813        test "z$(git config branch.myr17.rebase)" = z
 814'
 815
 816test_expect_success 'autosetuprebase local on an untracked remote branch' '
 817        git config branch.autosetuprebase local &&
 818        git config remote.local.url . &&
 819        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 820        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 821        git branch --no-track myr18 local/master &&
 822        test "z$(git config branch.myr18.remote)" = z &&
 823        test "z$(git config branch.myr18.merge)" = z &&
 824        test "z$(git config branch.myr18.rebase)" = z
 825'
 826
 827test_expect_success 'autosetuprebase remote on an untracked remote branch' '
 828        git config branch.autosetuprebase remote &&
 829        git config remote.local.url . &&
 830        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 831        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 832        git branch --no-track myr19 local/master &&
 833        test "z$(git config branch.myr19.remote)" = z &&
 834        test "z$(git config branch.myr19.merge)" = z &&
 835        test "z$(git config branch.myr19.rebase)" = z
 836'
 837
 838test_expect_success 'autosetuprebase always on an untracked remote branch' '
 839        git config branch.autosetuprebase always &&
 840        git config remote.local.url . &&
 841        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 842        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 843        git branch --no-track myr20 local/master &&
 844        test "z$(git config branch.myr20.remote)" = z &&
 845        test "z$(git config branch.myr20.merge)" = z &&
 846        test "z$(git config branch.myr20.rebase)" = z
 847'
 848
 849test_expect_success 'autosetuprebase always on detached HEAD' '
 850        git config branch.autosetupmerge always &&
 851        test_when_finished git checkout master &&
 852        git checkout HEAD^0 &&
 853        git branch my11 &&
 854        test -z "$(git config branch.my11.remote)" &&
 855        test -z "$(git config branch.my11.merge)"
 856'
 857
 858test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
 859        git config branch.autosetuprebase garbage &&
 860        test_must_fail git branch
 861'
 862
 863test_expect_success 'detect misconfigured autosetuprebase (no value)' '
 864        git config --unset branch.autosetuprebase &&
 865        echo "[branch] autosetuprebase" >>.git/config &&
 866        test_must_fail git branch &&
 867        git config --unset branch.autosetuprebase
 868'
 869
 870test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
 871        git checkout my9 &&
 872        git config --unset branch.my8.merge &&
 873        test_must_fail git branch -d my8
 874'
 875
 876test_expect_success 'attempt to delete a branch merged to its base' '
 877        # we are on my9 which is the initial commit; traditionally
 878        # we would not have allowed deleting my8 that is not merged
 879        # to my9, but it is set to track master that already has my8
 880        git config branch.my8.merge refs/heads/master &&
 881        git branch -d my8
 882'
 883
 884test_expect_success 'attempt to delete a branch merged to its base' '
 885        git checkout master &&
 886        echo Third >>A &&
 887        git commit -m "Third commit" A &&
 888        git branch -t my10 my9 &&
 889        git branch -f my10 HEAD^ &&
 890        # we are on master which is at the third commit, and my10
 891        # is behind us, so traditionally we would have allowed deleting
 892        # it; but my10 is set to track my9 that is further behind.
 893        test_must_fail git branch -d my10
 894'
 895
 896test_expect_success 'use set-upstream on the current branch' '
 897        git checkout master &&
 898        git --bare init myupstream.git &&
 899        git push myupstream.git master:refs/heads/frotz &&
 900        git remote add origin myupstream.git &&
 901        git fetch &&
 902        git branch --set-upstream master origin/frotz &&
 903
 904        test "z$(git config branch.master.remote)" = "zorigin" &&
 905        test "z$(git config branch.master.merge)" = "zrefs/heads/frotz"
 906
 907'
 908
 909test_expect_success 'use --edit-description' '
 910        write_script editor <<-\EOF &&
 911                echo "New contents" >"$1"
 912        EOF
 913        EDITOR=./editor git branch --edit-description &&
 914                write_script editor <<-\EOF &&
 915                git stripspace -s <"$1" >"EDITOR_OUTPUT"
 916        EOF
 917        EDITOR=./editor git branch --edit-description &&
 918        echo "New contents" >expect &&
 919        test_cmp EDITOR_OUTPUT expect
 920'
 921
 922test_expect_success 'detect typo in branch name when using --edit-description' '
 923        write_script editor <<-\EOF &&
 924                echo "New contents" >"$1"
 925        EOF
 926        test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
 927'
 928
 929test_expect_success 'refuse --edit-description on unborn branch for now' '
 930        write_script editor <<-\EOF &&
 931                echo "New contents" >"$1"
 932        EOF
 933        git checkout --orphan unborn &&
 934        test_must_fail env EDITOR=./editor git branch --edit-description
 935'
 936
 937test_expect_success '--merged catches invalid object names' '
 938        test_must_fail git branch --merged 0000000000000000000000000000000000000000
 939'
 940
 941test_expect_success 'tracking with unexpected .fetch refspec' '
 942        rm -rf a b c d &&
 943        git init a &&
 944        (
 945                cd a &&
 946                test_commit a
 947        ) &&
 948        git init b &&
 949        (
 950                cd b &&
 951                test_commit b
 952        ) &&
 953        git init c &&
 954        (
 955                cd c &&
 956                test_commit c &&
 957                git remote add a ../a &&
 958                git remote add b ../b &&
 959                git fetch --all
 960        ) &&
 961        git init d &&
 962        (
 963                cd d &&
 964                git remote add c ../c &&
 965                git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
 966                git fetch c &&
 967                git branch --track local/a/master remotes/a/master &&
 968                test "$(git config branch.local/a/master.remote)" = "c" &&
 969                test "$(git config branch.local/a/master.merge)" = "refs/remotes/a/master" &&
 970                git rev-parse --verify a >expect &&
 971                git rev-parse --verify local/a/master >actual &&
 972                test_cmp expect actual
 973        )
 974'
 975
 976test_done