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