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