t / t3200-branch.shon commit branch: update output to include worktree info (ab31381)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Amos Waterland
   4#
   5
   6test_description='git branch assorted tests'
   7
   8. ./test-lib.sh
   9. "$TEST_DIRECTORY"/lib-rebase.sh
  10
  11test_expect_success 'prepare a trivial repository' '
  12        echo Hello >A &&
  13        git update-index --add A &&
  14        git commit -m "Initial commit." &&
  15        echo World >>A &&
  16        git update-index --add A &&
  17        git commit -m "Second commit." &&
  18        HEAD=$(git rev-parse --verify HEAD)
  19'
  20
  21test_expect_success 'git branch --help should not have created a bogus branch' '
  22        test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
  23        test_path_is_missing .git/refs/heads/--help
  24'
  25
  26test_expect_success 'branch -h in broken repository' '
  27        mkdir broken &&
  28        (
  29                cd broken &&
  30                git init &&
  31                >.git/refs/heads/master &&
  32                test_expect_code 129 git branch -h >usage 2>&1
  33        ) &&
  34        test_i18ngrep "[Uu]sage" broken/usage
  35'
  36
  37test_expect_success 'git branch abc should create a branch' '
  38        git branch abc && test_path_is_file .git/refs/heads/abc
  39'
  40
  41test_expect_success 'git branch a/b/c should create a branch' '
  42        git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
  43'
  44
  45test_expect_success 'git branch HEAD should fail' '
  46        test_must_fail git branch HEAD
  47'
  48
  49cat >expect <<EOF
  50$ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000     branch: Created from master
  51EOF
  52test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
  53        GIT_COMMITTER_DATE="2005-05-26 23:30" \
  54        git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
  55        test_path_is_file .git/refs/heads/d/e/f &&
  56        test_path_is_file .git/logs/refs/heads/d/e/f &&
  57        test_cmp expect .git/logs/refs/heads/d/e/f
  58'
  59
  60test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
  61        git branch -d d/e/f &&
  62        test_path_is_missing .git/refs/heads/d/e/f &&
  63        test_must_fail git reflog exists refs/heads/d/e/f
  64'
  65
  66test_expect_success 'git branch j/k should work after branch j has been deleted' '
  67        git branch j &&
  68        git branch -d j &&
  69        git branch j/k
  70'
  71
  72test_expect_success 'git branch l should work after branch l/m has been deleted' '
  73        git branch l/m &&
  74        git branch -d l/m &&
  75        git branch l
  76'
  77
  78test_expect_success 'git branch -m dumps usage' '
  79        test_expect_code 128 git branch -m 2>err &&
  80        test_i18ngrep "branch name required" err
  81'
  82
  83test_expect_success 'git branch -m m broken_symref should work' '
  84        test_when_finished "git branch -D broken_symref" &&
  85        git branch --create-reflog m &&
  86        git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
  87        git branch -m m broken_symref &&
  88        git reflog exists refs/heads/broken_symref &&
  89        test_must_fail git reflog exists refs/heads/i_am_broken
  90'
  91
  92test_expect_success 'git branch -m m m/m should work' '
  93        git branch --create-reflog m &&
  94        git branch -m m m/m &&
  95        git reflog exists refs/heads/m/m
  96'
  97
  98test_expect_success 'git branch -m n/n n should work' '
  99        git branch --create-reflog n/n &&
 100        git branch -m n/n n &&
 101        git reflog exists refs/heads/n
 102'
 103
 104# The topmost entry in reflog for branch bbb is about branch creation.
 105# Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
 106
 107test_expect_success 'git branch -m bbb should rename checked out branch' '
 108        test_when_finished git branch -D bbb &&
 109        test_when_finished git checkout master &&
 110        git checkout -b aaa &&
 111        git commit --allow-empty -m "a new commit" &&
 112        git rev-parse aaa@{0} >expect &&
 113        git branch -m bbb &&
 114        git rev-parse bbb@{1} >actual &&
 115        test_cmp expect actual &&
 116        git symbolic-ref HEAD >actual &&
 117        echo refs/heads/bbb >expect &&
 118        test_cmp expect actual
 119'
 120
 121test_expect_success 'renaming checked out branch works with d/f conflict' '
 122        test_when_finished "git branch -D foo/bar || git branch -D foo" &&
 123        test_when_finished git checkout master &&
 124        git checkout -b foo &&
 125        git branch -m foo/bar &&
 126        git symbolic-ref HEAD >actual &&
 127        echo refs/heads/foo/bar >expect &&
 128        test_cmp expect actual
 129'
 130
 131test_expect_success 'git branch -m o/o o should fail when o/p exists' '
 132        git branch o/o &&
 133        git branch o/p &&
 134        test_must_fail git branch -m o/o o
 135'
 136
 137test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
 138        git branch o/q &&
 139        test_must_fail git branch -m o/q o/p
 140'
 141
 142test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
 143        git branch -M o/q o/p
 144'
 145
 146test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
 147        git branch o/q &&
 148        git branch -m -f o/q o/p
 149'
 150
 151test_expect_success 'git branch -m q r/q should fail when r exists' '
 152        git branch q &&
 153        git branch r &&
 154        test_must_fail git branch -m q r/q
 155'
 156
 157test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
 158        git branch bar &&
 159        git checkout -b foo &&
 160        test_must_fail git branch -M bar foo
 161'
 162
 163test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
 164        git checkout -b baz &&
 165        git branch bam &&
 166        git branch -M baz bam &&
 167        test $(git rev-parse --abbrev-ref HEAD) = bam
 168'
 169
 170test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
 171        msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
 172        grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
 173        grep "^0\{40\}.*$msg$" .git/logs/HEAD
 174'
 175
 176test_expect_success 'git branch -M should leave orphaned HEAD alone' '
 177        git init orphan &&
 178        (
 179                cd orphan &&
 180                test_commit initial &&
 181                git checkout --orphan lonely &&
 182                grep lonely .git/HEAD &&
 183                test_path_is_missing .git/refs/head/lonely &&
 184                git branch -M master mistress &&
 185                grep lonely .git/HEAD
 186        )
 187'
 188
 189test_expect_success 'resulting reflog can be shown by log -g' '
 190        oid=$(git rev-parse HEAD) &&
 191        cat >expect <<-EOF &&
 192        HEAD@{0} $oid $msg
 193        HEAD@{2} $oid checkout: moving from foo to baz
 194        EOF
 195        git log -g --format="%gd %H %gs" -2 HEAD >actual &&
 196        test_cmp expect actual
 197'
 198
 199test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
 200        git checkout master &&
 201        git worktree add -b baz bazdir &&
 202        git worktree add -f bazdir2 baz &&
 203        git branch -M baz bam &&
 204        test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
 205        test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
 206        rm -r bazdir bazdir2 &&
 207        git worktree prune
 208'
 209
 210test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
 211        git checkout -b baz &&
 212        git worktree add -f bazdir baz &&
 213        (
 214                cd bazdir &&
 215                git branch -M baz bam &&
 216                test $(git rev-parse --abbrev-ref HEAD) = bam
 217        ) &&
 218        test $(git rev-parse --abbrev-ref HEAD) = bam &&
 219        rm -r bazdir &&
 220        git worktree prune
 221'
 222
 223test_expect_success 'git branch -M master should work when master is checked out' '
 224        git checkout master &&
 225        git branch -M master
 226'
 227
 228test_expect_success 'git branch -M master master should work when master is checked out' '
 229        git checkout master &&
 230        git branch -M master master
 231'
 232
 233test_expect_success 'git branch -M master2 master2 should work when master is checked out' '
 234        git checkout master &&
 235        git branch master2 &&
 236        git branch -M master2 master2
 237'
 238
 239test_expect_success 'git branch -v -d t should work' '
 240        git branch t &&
 241        git rev-parse --verify refs/heads/t &&
 242        git branch -v -d t &&
 243        test_must_fail git rev-parse --verify refs/heads/t
 244'
 245
 246test_expect_success 'git branch -v -m t s should work' '
 247        git branch t &&
 248        git rev-parse --verify refs/heads/t &&
 249        git branch -v -m t s &&
 250        test_must_fail git rev-parse --verify refs/heads/t &&
 251        git rev-parse --verify refs/heads/s &&
 252        git branch -d s
 253'
 254
 255test_expect_success 'git branch -m -d t s should fail' '
 256        git branch t &&
 257        git rev-parse refs/heads/t &&
 258        test_must_fail git branch -m -d t s &&
 259        git branch -d t &&
 260        test_must_fail git rev-parse refs/heads/t
 261'
 262
 263test_expect_success 'git branch --list -d t should fail' '
 264        git branch t &&
 265        git rev-parse refs/heads/t &&
 266        test_must_fail git branch --list -d t &&
 267        git branch -d t &&
 268        test_must_fail git rev-parse refs/heads/t
 269'
 270
 271test_expect_success 'git branch --list -v with --abbrev' '
 272        test_when_finished "git branch -D t" &&
 273        git branch t &&
 274        git branch -v --list t >actual.default &&
 275        git branch -v --list --abbrev t >actual.abbrev &&
 276        test_cmp actual.default actual.abbrev &&
 277
 278        git branch -v --list --no-abbrev t >actual.noabbrev &&
 279        git branch -v --list --abbrev=0 t >actual.0abbrev &&
 280        test_cmp actual.noabbrev actual.0abbrev &&
 281
 282        git branch -v --list --abbrev=36 t >actual.36abbrev &&
 283        # how many hexdigits are used?
 284        read name objdefault rest <actual.abbrev &&
 285        read name obj36 rest <actual.36abbrev &&
 286        objfull=$(git rev-parse --verify t) &&
 287
 288        # are we really getting abbreviations?
 289        test "$obj36" != "$objdefault" &&
 290        expr "$obj36" : "$objdefault" >/dev/null &&
 291        test "$objfull" != "$obj36" &&
 292        expr "$objfull" : "$obj36" >/dev/null
 293
 294'
 295
 296test_expect_success 'git branch --column' '
 297        COLUMNS=81 git branch --column=column >actual &&
 298        cat >expected <<\EOF &&
 299  a/b/c     bam       foo       l       * master    n         o/p       r
 300  abc       bar       j/k       m/m       master2   o/o       q
 301EOF
 302        test_cmp expected actual
 303'
 304
 305test_expect_success 'git branch --column with an extremely long branch name' '
 306        long=this/is/a/part/of/long/branch/name &&
 307        long=z$long/$long/$long/$long &&
 308        test_when_finished "git branch -d $long" &&
 309        git branch $long &&
 310        COLUMNS=80 git branch --column=column >actual &&
 311        cat >expected <<EOF &&
 312  a/b/c
 313  abc
 314  bam
 315  bar
 316  foo
 317  j/k
 318  l
 319  m/m
 320* master
 321  master2
 322  n
 323  o/o
 324  o/p
 325  q
 326  r
 327  $long
 328EOF
 329        test_cmp expected actual
 330'
 331
 332test_expect_success 'git branch with column.*' '
 333        git config column.ui column &&
 334        git config column.branch "dense" &&
 335        COLUMNS=80 git branch >actual &&
 336        git config --unset column.branch &&
 337        git config --unset column.ui &&
 338        cat >expected <<\EOF &&
 339  a/b/c   bam   foo   l   * master    n     o/p   r
 340  abc     bar   j/k   m/m   master2   o/o   q
 341EOF
 342        test_cmp expected actual
 343'
 344
 345test_expect_success 'git branch --column -v should fail' '
 346        test_must_fail git branch --column -v
 347'
 348
 349test_expect_success 'git branch -v with column.ui ignored' '
 350        git config column.ui column &&
 351        COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
 352        git config --unset column.ui &&
 353        cat >expected <<\EOF &&
 354  a/b/c
 355  abc
 356  bam
 357  bar
 358  foo
 359  j/k
 360  l
 361  m/m
 362* master
 363  master2
 364  n
 365  o/o
 366  o/p
 367  q
 368  r
 369EOF
 370        test_cmp expected actual
 371'
 372
 373mv .git/config .git/config-saved
 374
 375test_expect_success 'git branch -m q q2 without config should succeed' '
 376        git branch -m q q2 &&
 377        git branch -m q2 q
 378'
 379
 380mv .git/config-saved .git/config
 381
 382git config branch.s/s.dummy Hello
 383
 384test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
 385        git branch --create-reflog s/s &&
 386        git reflog exists refs/heads/s/s &&
 387        git branch --create-reflog s/t &&
 388        git reflog exists refs/heads/s/t &&
 389        git branch -d s/t &&
 390        git branch -m s/s s &&
 391        git reflog exists refs/heads/s
 392'
 393
 394test_expect_success 'config information was renamed, too' '
 395        test $(git config branch.s.dummy) = Hello &&
 396        test_must_fail git config branch.s/s.dummy
 397'
 398
 399test_expect_success 'git branch -m correctly renames multiple config sections' '
 400        test_when_finished "git checkout master" &&
 401        git checkout -b source master &&
 402
 403        # Assert that a config file with multiple config sections has
 404        # those sections preserved...
 405        cat >expect <<-\EOF &&
 406        branch.dest.key1=value1
 407        some.gar.b=age
 408        branch.dest.key2=value2
 409        EOF
 410        cat >config.branch <<\EOF &&
 411;; Note the lack of -\EOF above & mixed indenting here. This is
 412;; intentional, we are also testing that the formatting of copied
 413;; sections is preserved.
 414
 415;; Comment for source. Tabs
 416[branch "source"]
 417        ;; Comment for the source value
 418        key1 = value1
 419;; Comment for some.gar. Spaces
 420[some "gar"]
 421    ;; Comment for the some.gar value
 422    b = age
 423;; Comment for source, again. Mixed tabs/spaces.
 424[branch "source"]
 425    ;; Comment for the source value, again
 426        key2 = value2
 427EOF
 428        cat config.branch >>.git/config &&
 429        git branch -m source dest &&
 430        git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
 431        test_cmp expect actual &&
 432
 433        # ...and that the comments for those sections are also
 434        # preserved.
 435        cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
 436        sed -n -e "/Note the lack/,\$p" .git/config >actual &&
 437        test_cmp expect actual
 438'
 439
 440test_expect_success 'git branch -c dumps usage' '
 441        test_expect_code 128 git branch -c 2>err &&
 442        test_i18ngrep "branch name required" err
 443'
 444
 445test_expect_success 'git branch --copy dumps usage' '
 446        test_expect_code 128 git branch --copy 2>err &&
 447        test_i18ngrep "branch name required" err
 448'
 449
 450test_expect_success 'git branch -c d e should work' '
 451        git branch --create-reflog d &&
 452        git reflog exists refs/heads/d &&
 453        git config branch.d.dummy Hello &&
 454        git branch -c d e &&
 455        git reflog exists refs/heads/d &&
 456        git reflog exists refs/heads/e &&
 457        echo Hello >expect &&
 458        git config branch.e.dummy >actual &&
 459        test_cmp expect actual &&
 460        echo Hello >expect &&
 461        git config branch.d.dummy >actual &&
 462        test_cmp expect actual
 463'
 464
 465test_expect_success 'git branch --copy is a synonym for -c' '
 466        git branch --create-reflog copy &&
 467        git reflog exists refs/heads/copy &&
 468        git config branch.copy.dummy Hello &&
 469        git branch --copy copy copy-to &&
 470        git reflog exists refs/heads/copy &&
 471        git reflog exists refs/heads/copy-to &&
 472        echo Hello >expect &&
 473        git config branch.copy.dummy >actual &&
 474        test_cmp expect actual &&
 475        echo Hello >expect &&
 476        git config branch.copy-to.dummy >actual &&
 477        test_cmp expect actual
 478'
 479
 480test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
 481        git checkout -b ee &&
 482        git reflog exists refs/heads/ee &&
 483        git config branch.ee.dummy Hello &&
 484        git branch -c ee ef &&
 485        git reflog exists refs/heads/ee &&
 486        git reflog exists refs/heads/ef &&
 487        test $(git config branch.ee.dummy) = Hello &&
 488        test $(git config branch.ef.dummy) = Hello &&
 489        test $(git rev-parse --abbrev-ref HEAD) = ee
 490'
 491
 492test_expect_success 'git branch -c f/f g/g should work' '
 493        git branch --create-reflog f/f &&
 494        git reflog exists refs/heads/f/f &&
 495        git config branch.f/f.dummy Hello &&
 496        git branch -c f/f g/g &&
 497        git reflog exists refs/heads/f/f &&
 498        git reflog exists refs/heads/g/g &&
 499        test $(git config branch.f/f.dummy) = Hello &&
 500        test $(git config branch.g/g.dummy) = Hello
 501'
 502
 503test_expect_success 'git branch -c m2 m2 should work' '
 504        git branch --create-reflog m2 &&
 505        git reflog exists refs/heads/m2 &&
 506        git config branch.m2.dummy Hello &&
 507        git branch -c m2 m2 &&
 508        git reflog exists refs/heads/m2 &&
 509        test $(git config branch.m2.dummy) = Hello
 510'
 511
 512test_expect_success 'git branch -c zz zz/zz should fail' '
 513        git branch --create-reflog zz &&
 514        git reflog exists refs/heads/zz &&
 515        test_must_fail git branch -c zz zz/zz
 516'
 517
 518test_expect_success 'git branch -c b/b b should fail' '
 519        git branch --create-reflog b/b &&
 520        test_must_fail git branch -c b/b b
 521'
 522
 523test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
 524        git branch --create-reflog o/q &&
 525        git reflog exists refs/heads/o/q &&
 526        git reflog exists refs/heads/o/p &&
 527        git branch -C o/q o/p
 528'
 529
 530test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
 531        git reflog exists refs/heads/o/q &&
 532        git reflog exists refs/heads/o/p &&
 533        git branch -c -f o/q o/p
 534'
 535
 536test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
 537        git branch qq &&
 538        git branch rr &&
 539        test_must_fail git branch -c qq rr/qq
 540'
 541
 542test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
 543        git branch b1 &&
 544        git checkout -b b2 &&
 545        test_must_fail git branch -C b1 b2
 546'
 547
 548test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
 549        git checkout -b c1 &&
 550        git branch c2 &&
 551        git branch -C c1 c2 &&
 552        test $(git rev-parse --abbrev-ref HEAD) = c1
 553'
 554
 555test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
 556        msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
 557        ! grep "$msg$" .git/logs/HEAD
 558'
 559
 560test_expect_success 'git branch -C master should work when master is checked out' '
 561        git checkout master &&
 562        git branch -C master
 563'
 564
 565test_expect_success 'git branch -C master master should work when master is checked out' '
 566        git checkout master &&
 567        git branch -C master master
 568'
 569
 570test_expect_success 'git branch -C master5 master5 should work when master is checked out' '
 571        git checkout master &&
 572        git branch master5 &&
 573        git branch -C master5 master5
 574'
 575
 576test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
 577        git branch --create-reflog cd &&
 578        git reflog exists refs/heads/cd &&
 579        git config branch.cd.dummy CD &&
 580        git branch --create-reflog ab &&
 581        git reflog exists refs/heads/ab &&
 582        git config branch.ab.dummy AB &&
 583        git branch -C ab cd &&
 584        git reflog exists refs/heads/ab &&
 585        git reflog exists refs/heads/cd &&
 586        test $(git config branch.ab.dummy) = AB &&
 587        test $(git config branch.cd.dummy) = AB
 588'
 589
 590test_expect_success 'git branch -c correctly copies multiple config sections' '
 591        FOO=1 &&
 592        export FOO &&
 593        test_when_finished "git checkout master" &&
 594        git checkout -b source2 master &&
 595
 596        # Assert that a config file with multiple config sections has
 597        # those sections preserved...
 598        cat >expect <<-\EOF &&
 599        branch.source2.key1=value1
 600        branch.dest2.key1=value1
 601        more.gar.b=age
 602        branch.source2.key2=value2
 603        branch.dest2.key2=value2
 604        EOF
 605        cat >config.branch <<\EOF &&
 606;; Note the lack of -\EOF above & mixed indenting here. This is
 607;; intentional, we are also testing that the formatting of copied
 608;; sections is preserved.
 609
 610;; Comment for source2. Tabs
 611[branch "source2"]
 612        ;; Comment for the source2 value
 613        key1 = value1
 614;; Comment for more.gar. Spaces
 615[more "gar"]
 616    ;; Comment for the more.gar value
 617    b = age
 618;; Comment for source2, again. Mixed tabs/spaces.
 619[branch "source2"]
 620    ;; Comment for the source2 value, again
 621        key2 = value2
 622EOF
 623        cat config.branch >>.git/config &&
 624        git branch -c source2 dest2 &&
 625        git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
 626        test_cmp expect actual &&
 627
 628        # ...and that the comments and formatting for those sections
 629        # is also preserved.
 630        cat >expect <<\EOF &&
 631;; Comment for source2. Tabs
 632[branch "source2"]
 633        ;; Comment for the source2 value
 634        key1 = value1
 635;; Comment for more.gar. Spaces
 636[branch "dest2"]
 637        ;; Comment for the source2 value
 638        key1 = value1
 639;; Comment for more.gar. Spaces
 640[more "gar"]
 641    ;; Comment for the more.gar value
 642    b = age
 643;; Comment for source2, again. Mixed tabs/spaces.
 644[branch "source2"]
 645    ;; Comment for the source2 value, again
 646        key2 = value2
 647[branch "dest2"]
 648    ;; Comment for the source2 value, again
 649        key2 = value2
 650EOF
 651        sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
 652        test_cmp expect actual
 653'
 654
 655test_expect_success 'deleting a symref' '
 656        git branch target &&
 657        git symbolic-ref refs/heads/symref refs/heads/target &&
 658        echo "Deleted branch symref (was refs/heads/target)." >expect &&
 659        git branch -d symref >actual &&
 660        test_path_is_file .git/refs/heads/target &&
 661        test_path_is_missing .git/refs/heads/symref &&
 662        test_i18ncmp expect actual
 663'
 664
 665test_expect_success 'deleting a dangling symref' '
 666        git symbolic-ref refs/heads/dangling-symref nowhere &&
 667        test_path_is_file .git/refs/heads/dangling-symref &&
 668        echo "Deleted branch dangling-symref (was nowhere)." >expect &&
 669        git branch -d dangling-symref >actual &&
 670        test_path_is_missing .git/refs/heads/dangling-symref &&
 671        test_i18ncmp expect actual
 672'
 673
 674test_expect_success 'deleting a self-referential symref' '
 675        git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
 676        test_path_is_file .git/refs/heads/self-reference &&
 677        echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
 678        git branch -d self-reference >actual &&
 679        test_path_is_missing .git/refs/heads/self-reference &&
 680        test_i18ncmp expect actual
 681'
 682
 683test_expect_success 'renaming a symref is not allowed' '
 684        git symbolic-ref refs/heads/master2 refs/heads/master &&
 685        test_must_fail git branch -m master2 master3 &&
 686        git symbolic-ref refs/heads/master2 &&
 687        test_path_is_file .git/refs/heads/master &&
 688        test_path_is_missing .git/refs/heads/master3
 689'
 690
 691test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
 692        git branch --create-reflog u &&
 693        mv .git/logs/refs/heads/u real-u &&
 694        ln -s real-u .git/logs/refs/heads/u &&
 695        test_must_fail git branch -m u v
 696'
 697
 698test_expect_success 'test tracking setup via --track' '
 699        git config remote.local.url . &&
 700        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 701        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 702        git branch --track my1 local/master &&
 703        test $(git config branch.my1.remote) = local &&
 704        test $(git config branch.my1.merge) = refs/heads/master
 705'
 706
 707test_expect_success 'test tracking setup (non-wildcard, matching)' '
 708        git config remote.local.url . &&
 709        git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
 710        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 711        git branch --track my4 local/master &&
 712        test $(git config branch.my4.remote) = local &&
 713        test $(git config branch.my4.merge) = refs/heads/master
 714'
 715
 716test_expect_success 'tracking setup fails on non-matching refspec' '
 717        git config remote.local.url . &&
 718        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 719        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 720        git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
 721        test_must_fail git branch --track my5 local/master &&
 722        test_must_fail git config branch.my5.remote &&
 723        test_must_fail git config branch.my5.merge
 724'
 725
 726test_expect_success 'test tracking setup via config' '
 727        git config branch.autosetupmerge true &&
 728        git config remote.local.url . &&
 729        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 730        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 731        git branch my3 local/master &&
 732        test $(git config branch.my3.remote) = local &&
 733        test $(git config branch.my3.merge) = refs/heads/master
 734'
 735
 736test_expect_success 'test overriding tracking setup via --no-track' '
 737        git config branch.autosetupmerge true &&
 738        git config remote.local.url . &&
 739        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 740        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 741        git branch --no-track my2 local/master &&
 742        git config branch.autosetupmerge false &&
 743        ! test "$(git config branch.my2.remote)" = local &&
 744        ! test "$(git config branch.my2.merge)" = refs/heads/master
 745'
 746
 747test_expect_success 'no tracking without .fetch entries' '
 748        git config branch.autosetupmerge true &&
 749        git branch my6 s &&
 750        git config branch.autosetupmerge false &&
 751        test -z "$(git config branch.my6.remote)" &&
 752        test -z "$(git config branch.my6.merge)"
 753'
 754
 755test_expect_success 'test tracking setup via --track but deeper' '
 756        git config remote.local.url . &&
 757        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 758        (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
 759        git branch --track my7 local/o/o &&
 760        test "$(git config branch.my7.remote)" = local &&
 761        test "$(git config branch.my7.merge)" = refs/heads/o/o
 762'
 763
 764test_expect_success 'test deleting branch deletes branch config' '
 765        git branch -d my7 &&
 766        test -z "$(git config branch.my7.remote)" &&
 767        test -z "$(git config branch.my7.merge)"
 768'
 769
 770test_expect_success 'test deleting branch without config' '
 771        git branch my7 s &&
 772        sha1=$(git rev-parse my7 | cut -c 1-7) &&
 773        echo "Deleted branch my7 (was $sha1)." >expect &&
 774        git branch -d my7 >actual 2>&1 &&
 775        test_i18ncmp expect actual
 776'
 777
 778test_expect_success 'deleting currently checked out branch fails' '
 779        git worktree add -b my7 my7 &&
 780        test_must_fail git -C my7 branch -d my7 &&
 781        test_must_fail git branch -d my7 &&
 782        rm -r my7 &&
 783        git worktree prune
 784'
 785
 786test_expect_success 'test --track without .fetch entries' '
 787        git branch --track my8 &&
 788        test "$(git config branch.my8.remote)" &&
 789        test "$(git config branch.my8.merge)"
 790'
 791
 792test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
 793        git config branch.autosetupmerge always &&
 794        git branch my9 HEAD^ &&
 795        git config branch.autosetupmerge false
 796'
 797
 798test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
 799        test_must_fail git branch --track my10 HEAD^
 800'
 801
 802test_expect_success 'branch from tag w/--track causes failure' '
 803        git tag foobar &&
 804        test_must_fail git branch --track my11 foobar
 805'
 806
 807test_expect_success '--set-upstream-to fails on multiple branches' '
 808        test_must_fail git branch --set-upstream-to master a b c
 809'
 810
 811test_expect_success '--set-upstream-to fails on detached HEAD' '
 812        git checkout HEAD^{} &&
 813        test_must_fail git branch --set-upstream-to master &&
 814        git checkout -
 815'
 816
 817test_expect_success '--set-upstream-to fails on a missing dst branch' '
 818        test_must_fail git branch --set-upstream-to master does-not-exist
 819'
 820
 821test_expect_success '--set-upstream-to fails on a missing src branch' '
 822        test_must_fail git branch --set-upstream-to does-not-exist master
 823'
 824
 825test_expect_success '--set-upstream-to fails on a non-ref' '
 826        test_must_fail git branch --set-upstream-to HEAD^{}
 827'
 828
 829test_expect_success '--set-upstream-to fails on locked config' '
 830        test_when_finished "rm -f .git/config.lock" &&
 831        >.git/config.lock &&
 832        git branch locked &&
 833        test_must_fail git branch --set-upstream-to locked
 834'
 835
 836test_expect_success 'use --set-upstream-to modify HEAD' '
 837        test_config branch.master.remote foo &&
 838        test_config branch.master.merge foo &&
 839        git branch my12 &&
 840        git branch --set-upstream-to my12 &&
 841        test "$(git config branch.master.remote)" = "." &&
 842        test "$(git config branch.master.merge)" = "refs/heads/my12"
 843'
 844
 845test_expect_success 'use --set-upstream-to modify a particular branch' '
 846        git branch my13 &&
 847        git branch --set-upstream-to master my13 &&
 848        test_when_finished "git branch --unset-upstream my13" &&
 849        test "$(git config branch.my13.remote)" = "." &&
 850        test "$(git config branch.my13.merge)" = "refs/heads/master"
 851'
 852
 853test_expect_success '--unset-upstream should fail if given a non-existent branch' '
 854        test_must_fail git branch --unset-upstream i-dont-exist
 855'
 856
 857test_expect_success '--unset-upstream should fail if config is locked' '
 858        test_when_finished "rm -f .git/config.lock" &&
 859        git branch --set-upstream-to locked &&
 860        >.git/config.lock &&
 861        test_must_fail git branch --unset-upstream
 862'
 863
 864test_expect_success 'test --unset-upstream on HEAD' '
 865        git branch my14 &&
 866        test_config branch.master.remote foo &&
 867        test_config branch.master.merge foo &&
 868        git branch --set-upstream-to my14 &&
 869        git branch --unset-upstream &&
 870        test_must_fail git config branch.master.remote &&
 871        test_must_fail git config branch.master.merge &&
 872        # fail for a branch without upstream set
 873        test_must_fail git branch --unset-upstream
 874'
 875
 876test_expect_success '--unset-upstream should fail on multiple branches' '
 877        test_must_fail git branch --unset-upstream a b c
 878'
 879
 880test_expect_success '--unset-upstream should fail on detached HEAD' '
 881        git checkout HEAD^{} &&
 882        test_must_fail git branch --unset-upstream &&
 883        git checkout -
 884'
 885
 886test_expect_success 'test --unset-upstream on a particular branch' '
 887        git branch my15 &&
 888        git branch --set-upstream-to master my14 &&
 889        git branch --unset-upstream my14 &&
 890        test_must_fail git config branch.my14.remote &&
 891        test_must_fail git config branch.my14.merge
 892'
 893
 894test_expect_success 'disabled option --set-upstream fails' '
 895    test_must_fail git branch --set-upstream origin/master
 896'
 897
 898test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
 899        git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
 900        cat >expected <<-\EOF &&
 901        warning: Not setting branch my13 as its own upstream.
 902        EOF
 903        test_expect_code 1 git config branch.my13.remote &&
 904        test_expect_code 1 git config branch.my13.merge &&
 905        test_i18ncmp expected actual
 906'
 907
 908# Keep this test last, as it changes the current branch
 909cat >expect <<EOF
 910$ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000     branch: Created from master
 911EOF
 912test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
 913        GIT_COMMITTER_DATE="2005-05-26 23:30" \
 914        git checkout -b g/h/i -l master &&
 915        test_path_is_file .git/refs/heads/g/h/i &&
 916        test_path_is_file .git/logs/refs/heads/g/h/i &&
 917        test_cmp expect .git/logs/refs/heads/g/h/i
 918'
 919
 920test_expect_success 'checkout -b makes reflog by default' '
 921        git checkout master &&
 922        git config --unset core.logAllRefUpdates &&
 923        git checkout -b alpha &&
 924        git rev-parse --verify alpha@{0}
 925'
 926
 927test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
 928        git checkout master &&
 929        git config core.logAllRefUpdates false &&
 930        git checkout -b beta &&
 931        test_must_fail git rev-parse --verify beta@{0}
 932'
 933
 934test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
 935        git checkout master &&
 936        git checkout -lb gamma &&
 937        git config --unset core.logAllRefUpdates &&
 938        git rev-parse --verify gamma@{0}
 939'
 940
 941test_expect_success 'avoid ambiguous track' '
 942        git config branch.autosetupmerge true &&
 943        git config remote.ambi1.url lalala &&
 944        git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
 945        git config remote.ambi2.url lilili &&
 946        git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
 947        test_must_fail git branch all1 master &&
 948        test -z "$(git config branch.all1.merge)"
 949'
 950
 951test_expect_success 'autosetuprebase local on a tracked local branch' '
 952        git config remote.local.url . &&
 953        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 954        git config branch.autosetuprebase local &&
 955        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 956        git branch mybase &&
 957        git branch --track myr1 mybase &&
 958        test "$(git config branch.myr1.remote)" = . &&
 959        test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
 960        test "$(git config branch.myr1.rebase)" = true
 961'
 962
 963test_expect_success 'autosetuprebase always on a tracked local branch' '
 964        git config remote.local.url . &&
 965        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 966        git config branch.autosetuprebase always &&
 967        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 968        git branch mybase2 &&
 969        git branch --track myr2 mybase &&
 970        test "$(git config branch.myr2.remote)" = . &&
 971        test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
 972        test "$(git config branch.myr2.rebase)" = true
 973'
 974
 975test_expect_success 'autosetuprebase remote on a tracked local branch' '
 976        git config remote.local.url . &&
 977        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 978        git config branch.autosetuprebase remote &&
 979        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 980        git branch mybase3 &&
 981        git branch --track myr3 mybase2 &&
 982        test "$(git config branch.myr3.remote)" = . &&
 983        test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
 984        ! test "$(git config branch.myr3.rebase)" = true
 985'
 986
 987test_expect_success 'autosetuprebase never on a tracked local branch' '
 988        git config remote.local.url . &&
 989        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 990        git config branch.autosetuprebase never &&
 991        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 992        git branch mybase4 &&
 993        git branch --track myr4 mybase2 &&
 994        test "$(git config branch.myr4.remote)" = . &&
 995        test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
 996        ! test "$(git config branch.myr4.rebase)" = true
 997'
 998
 999test_expect_success 'autosetuprebase local on a tracked remote branch' '
1000        git config remote.local.url . &&
1001        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1002        git config branch.autosetuprebase local &&
1003        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1004        git branch --track myr5 local/master &&
1005        test "$(git config branch.myr5.remote)" = local &&
1006        test "$(git config branch.myr5.merge)" = refs/heads/master &&
1007        ! test "$(git config branch.myr5.rebase)" = true
1008'
1009
1010test_expect_success 'autosetuprebase never on a tracked remote branch' '
1011        git config remote.local.url . &&
1012        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1013        git config branch.autosetuprebase never &&
1014        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1015        git branch --track myr6 local/master &&
1016        test "$(git config branch.myr6.remote)" = local &&
1017        test "$(git config branch.myr6.merge)" = refs/heads/master &&
1018        ! test "$(git config branch.myr6.rebase)" = true
1019'
1020
1021test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1022        git config remote.local.url . &&
1023        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1024        git config branch.autosetuprebase remote &&
1025        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1026        git branch --track myr7 local/master &&
1027        test "$(git config branch.myr7.remote)" = local &&
1028        test "$(git config branch.myr7.merge)" = refs/heads/master &&
1029        test "$(git config branch.myr7.rebase)" = true
1030'
1031
1032test_expect_success 'autosetuprebase always on a tracked remote branch' '
1033        git config remote.local.url . &&
1034        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1035        git config branch.autosetuprebase remote &&
1036        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1037        git branch --track myr8 local/master &&
1038        test "$(git config branch.myr8.remote)" = local &&
1039        test "$(git config branch.myr8.merge)" = refs/heads/master &&
1040        test "$(git config branch.myr8.rebase)" = true
1041'
1042
1043test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1044        git config --unset branch.autosetuprebase &&
1045        git config remote.local.url . &&
1046        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1047        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1048        git branch --track myr9 local/master &&
1049        test "$(git config branch.myr9.remote)" = local &&
1050        test "$(git config branch.myr9.merge)" = refs/heads/master &&
1051        test "z$(git config branch.myr9.rebase)" = z
1052'
1053
1054test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1055        git config remote.local.url . &&
1056        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1057        (git show-ref -q refs/remotes/local/o || git fetch local) &&
1058        git branch mybase10 &&
1059        git branch --track myr10 mybase2 &&
1060        test "$(git config branch.myr10.remote)" = . &&
1061        test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1062        test "z$(git config branch.myr10.rebase)" = z
1063'
1064
1065test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1066        git config remote.local.url . &&
1067        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1068        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1069        git branch --no-track myr11 mybase2 &&
1070        test "z$(git config branch.myr11.remote)" = z &&
1071        test "z$(git config branch.myr11.merge)" = z &&
1072        test "z$(git config branch.myr11.rebase)" = z
1073'
1074
1075test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1076        git config remote.local.url . &&
1077        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1078        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1079        git branch --no-track myr12 local/master &&
1080        test "z$(git config branch.myr12.remote)" = z &&
1081        test "z$(git config branch.myr12.merge)" = z &&
1082        test "z$(git config branch.myr12.rebase)" = z
1083'
1084
1085test_expect_success 'autosetuprebase never on an untracked local branch' '
1086        git config branch.autosetuprebase never &&
1087        git config remote.local.url . &&
1088        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1089        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1090        git branch --no-track myr13 mybase2 &&
1091        test "z$(git config branch.myr13.remote)" = z &&
1092        test "z$(git config branch.myr13.merge)" = z &&
1093        test "z$(git config branch.myr13.rebase)" = z
1094'
1095
1096test_expect_success 'autosetuprebase local on an untracked local branch' '
1097        git config branch.autosetuprebase local &&
1098        git config remote.local.url . &&
1099        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1100        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1101        git branch --no-track myr14 mybase2 &&
1102        test "z$(git config branch.myr14.remote)" = z &&
1103        test "z$(git config branch.myr14.merge)" = z &&
1104        test "z$(git config branch.myr14.rebase)" = z
1105'
1106
1107test_expect_success 'autosetuprebase remote on an untracked local branch' '
1108        git config branch.autosetuprebase remote &&
1109        git config remote.local.url . &&
1110        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1111        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1112        git branch --no-track myr15 mybase2 &&
1113        test "z$(git config branch.myr15.remote)" = z &&
1114        test "z$(git config branch.myr15.merge)" = z &&
1115        test "z$(git config branch.myr15.rebase)" = z
1116'
1117
1118test_expect_success 'autosetuprebase always on an untracked local branch' '
1119        git config branch.autosetuprebase always &&
1120        git config remote.local.url . &&
1121        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1122        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1123        git branch --no-track myr16 mybase2 &&
1124        test "z$(git config branch.myr16.remote)" = z &&
1125        test "z$(git config branch.myr16.merge)" = z &&
1126        test "z$(git config branch.myr16.rebase)" = z
1127'
1128
1129test_expect_success 'autosetuprebase never on an untracked remote branch' '
1130        git config branch.autosetuprebase never &&
1131        git config remote.local.url . &&
1132        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1133        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1134        git branch --no-track myr17 local/master &&
1135        test "z$(git config branch.myr17.remote)" = z &&
1136        test "z$(git config branch.myr17.merge)" = z &&
1137        test "z$(git config branch.myr17.rebase)" = z
1138'
1139
1140test_expect_success 'autosetuprebase local on an untracked remote branch' '
1141        git config branch.autosetuprebase local &&
1142        git config remote.local.url . &&
1143        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1144        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1145        git branch --no-track myr18 local/master &&
1146        test "z$(git config branch.myr18.remote)" = z &&
1147        test "z$(git config branch.myr18.merge)" = z &&
1148        test "z$(git config branch.myr18.rebase)" = z
1149'
1150
1151test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1152        git config branch.autosetuprebase remote &&
1153        git config remote.local.url . &&
1154        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1155        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1156        git branch --no-track myr19 local/master &&
1157        test "z$(git config branch.myr19.remote)" = z &&
1158        test "z$(git config branch.myr19.merge)" = z &&
1159        test "z$(git config branch.myr19.rebase)" = z
1160'
1161
1162test_expect_success 'autosetuprebase always on an untracked remote branch' '
1163        git config branch.autosetuprebase always &&
1164        git config remote.local.url . &&
1165        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1166        (git show-ref -q refs/remotes/local/master || git fetch local) &&
1167        git branch --no-track myr20 local/master &&
1168        test "z$(git config branch.myr20.remote)" = z &&
1169        test "z$(git config branch.myr20.merge)" = z &&
1170        test "z$(git config branch.myr20.rebase)" = z
1171'
1172
1173test_expect_success 'autosetuprebase always on detached HEAD' '
1174        git config branch.autosetupmerge always &&
1175        test_when_finished git checkout master &&
1176        git checkout HEAD^0 &&
1177        git branch my11 &&
1178        test -z "$(git config branch.my11.remote)" &&
1179        test -z "$(git config branch.my11.merge)"
1180'
1181
1182test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1183        git config branch.autosetuprebase garbage &&
1184        test_must_fail git branch
1185'
1186
1187test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1188        git config --unset branch.autosetuprebase &&
1189        echo "[branch] autosetuprebase" >>.git/config &&
1190        test_must_fail git branch &&
1191        git config --unset branch.autosetuprebase
1192'
1193
1194test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1195        git checkout my9 &&
1196        git config --unset branch.my8.merge &&
1197        test_must_fail git branch -d my8
1198'
1199
1200test_expect_success 'attempt to delete a branch merged to its base' '
1201        # we are on my9 which is the initial commit; traditionally
1202        # we would not have allowed deleting my8 that is not merged
1203        # to my9, but it is set to track master that already has my8
1204        git config branch.my8.merge refs/heads/master &&
1205        git branch -d my8
1206'
1207
1208test_expect_success 'attempt to delete a branch merged to its base' '
1209        git checkout master &&
1210        echo Third >>A &&
1211        git commit -m "Third commit" A &&
1212        git branch -t my10 my9 &&
1213        git branch -f my10 HEAD^ &&
1214        # we are on master which is at the third commit, and my10
1215        # is behind us, so traditionally we would have allowed deleting
1216        # it; but my10 is set to track my9 that is further behind.
1217        test_must_fail git branch -d my10
1218'
1219
1220test_expect_success 'use --edit-description' '
1221        write_script editor <<-\EOF &&
1222                echo "New contents" >"$1"
1223        EOF
1224        EDITOR=./editor git branch --edit-description &&
1225                write_script editor <<-\EOF &&
1226                git stripspace -s <"$1" >"EDITOR_OUTPUT"
1227        EOF
1228        EDITOR=./editor git branch --edit-description &&
1229        echo "New contents" >expect &&
1230        test_cmp expect EDITOR_OUTPUT
1231'
1232
1233test_expect_success 'detect typo in branch name when using --edit-description' '
1234        write_script editor <<-\EOF &&
1235                echo "New contents" >"$1"
1236        EOF
1237        test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1238'
1239
1240test_expect_success 'refuse --edit-description on unborn branch for now' '
1241        write_script editor <<-\EOF &&
1242                echo "New contents" >"$1"
1243        EOF
1244        git checkout --orphan unborn &&
1245        test_must_fail env EDITOR=./editor git branch --edit-description
1246'
1247
1248test_expect_success '--merged catches invalid object names' '
1249        test_must_fail git branch --merged 0000000000000000000000000000000000000000
1250'
1251
1252test_expect_success '--merged is incompatible with --no-merged' '
1253        test_must_fail git branch --merged HEAD --no-merged HEAD
1254'
1255
1256test_expect_success '--list during rebase' '
1257        test_when_finished "reset_rebase" &&
1258        git checkout master &&
1259        FAKE_LINES="1 edit 2" &&
1260        export FAKE_LINES &&
1261        set_fake_editor &&
1262        git rebase -i HEAD~2 &&
1263        git branch --list >actual &&
1264        test_i18ngrep "rebasing master" actual
1265'
1266
1267test_expect_success '--list during rebase from detached HEAD' '
1268        test_when_finished "reset_rebase && git checkout master" &&
1269        git checkout master^0 &&
1270        oid=$(git rev-parse --short HEAD) &&
1271        FAKE_LINES="1 edit 2" &&
1272        export FAKE_LINES &&
1273        set_fake_editor &&
1274        git rebase -i HEAD~2 &&
1275        git branch --list >actual &&
1276        test_i18ngrep "rebasing detached HEAD $oid" actual
1277'
1278
1279test_expect_success 'tracking with unexpected .fetch refspec' '
1280        rm -rf a b c d &&
1281        git init a &&
1282        (
1283                cd a &&
1284                test_commit a
1285        ) &&
1286        git init b &&
1287        (
1288                cd b &&
1289                test_commit b
1290        ) &&
1291        git init c &&
1292        (
1293                cd c &&
1294                test_commit c &&
1295                git remote add a ../a &&
1296                git remote add b ../b &&
1297                git fetch --all
1298        ) &&
1299        git init d &&
1300        (
1301                cd d &&
1302                git remote add c ../c &&
1303                git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1304                git fetch c &&
1305                git branch --track local/a/master remotes/a/master &&
1306                test "$(git config branch.local/a/master.remote)" = "c" &&
1307                test "$(git config branch.local/a/master.merge)" = "refs/remotes/a/master" &&
1308                git rev-parse --verify a >expect &&
1309                git rev-parse --verify local/a/master >actual &&
1310                test_cmp expect actual
1311        )
1312'
1313
1314test_expect_success 'configured committerdate sort' '
1315        git init sort &&
1316        (
1317                cd sort &&
1318                git config branch.sort committerdate &&
1319                test_commit initial &&
1320                git checkout -b a &&
1321                test_commit a &&
1322                git checkout -b c &&
1323                test_commit c &&
1324                git checkout -b b &&
1325                test_commit b &&
1326                git branch >actual &&
1327                cat >expect <<-\EOF &&
1328                  master
1329                  a
1330                  c
1331                * b
1332                EOF
1333                test_cmp expect actual
1334        )
1335'
1336
1337test_expect_success 'option override configured sort' '
1338        (
1339                cd sort &&
1340                git config branch.sort committerdate &&
1341                git branch --sort=refname >actual &&
1342                cat >expect <<-\EOF &&
1343                  a
1344                * b
1345                  c
1346                  master
1347                EOF
1348                test_cmp expect actual
1349        )
1350'
1351
1352test_expect_success 'invalid sort parameter in configuration' '
1353        (
1354                cd sort &&
1355                git config branch.sort "v:notvalid" &&
1356                test_must_fail git branch
1357        )
1358'
1359
1360test_done