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