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