t / t3200-branch.shon commit branch: make create_branch accept a merge base rev (e3d6539)
   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 mb master... should create a branch' '
  46        git branch mb master... && test_path_is_file .git/refs/heads/mb
  47'
  48
  49test_expect_success 'git branch HEAD should fail' '
  50        test_must_fail git branch HEAD
  51'
  52
  53cat >expect <<EOF
  54$ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000     branch: Created from master
  55EOF
  56test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
  57        GIT_COMMITTER_DATE="2005-05-26 23:30" \
  58        git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
  59        test_path_is_file .git/refs/heads/d/e/f &&
  60        test_path_is_file .git/logs/refs/heads/d/e/f &&
  61        test_cmp expect .git/logs/refs/heads/d/e/f
  62'
  63
  64test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
  65        git branch -d d/e/f &&
  66        test_path_is_missing .git/refs/heads/d/e/f &&
  67        test_must_fail git reflog exists refs/heads/d/e/f
  68'
  69
  70test_expect_success 'git branch j/k should work after branch j has been deleted' '
  71        git branch j &&
  72        git branch -d j &&
  73        git branch j/k
  74'
  75
  76test_expect_success 'git branch l should work after branch l/m has been deleted' '
  77        git branch l/m &&
  78        git branch -d l/m &&
  79        git branch l
  80'
  81
  82test_expect_success 'git branch -m dumps usage' '
  83        test_expect_code 128 git branch -m 2>err &&
  84        test_i18ngrep "branch name required" err
  85'
  86
  87test_expect_success 'git branch -m m broken_symref should work' '
  88        test_when_finished "git branch -D broken_symref" &&
  89        git branch --create-reflog m &&
  90        git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
  91        git branch -m m broken_symref &&
  92        git reflog exists refs/heads/broken_symref &&
  93        test_must_fail git reflog exists refs/heads/i_am_broken
  94'
  95
  96test_expect_success 'git branch -m m m/m should work' '
  97        git branch --create-reflog m &&
  98        git branch -m m m/m &&
  99        git reflog exists refs/heads/m/m
 100'
 101
 102test_expect_success 'git branch -m n/n n should work' '
 103        git branch --create-reflog n/n &&
 104        git branch -m n/n n &&
 105        git reflog exists refs/heads/n
 106'
 107
 108# The topmost entry in reflog for branch bbb is about branch creation.
 109# Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
 110
 111test_expect_success 'git branch -m bbb should rename checked out branch' '
 112        test_when_finished git branch -D bbb &&
 113        test_when_finished git checkout master &&
 114        git checkout -b aaa &&
 115        git commit --allow-empty -m "a new commit" &&
 116        git rev-parse aaa@{0} >expect &&
 117        git branch -m bbb &&
 118        git rev-parse bbb@{1} >actual &&
 119        test_cmp expect actual &&
 120        git symbolic-ref HEAD >actual &&
 121        echo refs/heads/bbb >expect &&
 122        test_cmp expect actual
 123'
 124
 125test_expect_success 'renaming checked out branch works with d/f conflict' '
 126        test_when_finished "git branch -D foo/bar || git branch -D foo" &&
 127        test_when_finished git checkout master &&
 128        git checkout -b foo &&
 129        git branch -m foo/bar &&
 130        git symbolic-ref HEAD >actual &&
 131        echo refs/heads/foo/bar >expect &&
 132        test_cmp expect actual
 133'
 134
 135test_expect_success 'git branch -m o/o o should fail when o/p exists' '
 136        git branch o/o &&
 137        git branch o/p &&
 138        test_must_fail git branch -m o/o o
 139'
 140
 141test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
 142        git branch o/q &&
 143        test_must_fail git branch -m o/q o/p
 144'
 145
 146test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
 147        git branch -M o/q o/p
 148'
 149
 150test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
 151        git branch o/q &&
 152        git branch -m -f o/q o/p
 153'
 154
 155test_expect_success 'git branch -m q r/q should fail when r exists' '
 156        git branch q &&
 157        git branch r &&
 158        test_must_fail git branch -m q r/q
 159'
 160
 161test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
 162        git branch bar &&
 163        git checkout -b foo &&
 164        test_must_fail git branch -M bar foo
 165'
 166
 167test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
 168        git checkout -b baz &&
 169        git branch bam &&
 170        git branch -M baz bam &&
 171        test $(git rev-parse --abbrev-ref HEAD) = bam
 172'
 173
 174test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
 175        msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
 176        grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
 177        grep "^0\{40\}.*$msg$" .git/logs/HEAD
 178'
 179
 180test_expect_success 'git branch -M should leave orphaned HEAD alone' '
 181        git init orphan &&
 182        (
 183                cd orphan &&
 184                test_commit initial &&
 185                git checkout --orphan lonely &&
 186                grep lonely .git/HEAD &&
 187                test_path_is_missing .git/refs/head/lonely &&
 188                git branch -M master mistress &&
 189                grep lonely .git/HEAD
 190        )
 191'
 192
 193test_expect_success 'resulting reflog can be shown by log -g' '
 194        oid=$(git rev-parse HEAD) &&
 195        cat >expect <<-EOF &&
 196        HEAD@{0} $oid $msg
 197        HEAD@{2} $oid checkout: moving from foo to baz
 198        EOF
 199        git log -g --format="%gd %H %gs" -2 HEAD >actual &&
 200        test_cmp expect actual
 201'
 202
 203test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
 204        git checkout master &&
 205        git worktree add -b baz bazdir &&
 206        git worktree add -f bazdir2 baz &&
 207        git branch -M baz bam &&
 208        test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
 209        test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam
 210'
 211
 212test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
 213        git checkout -b baz &&
 214        git worktree add -f bazdir3 baz &&
 215        (
 216                cd bazdir3 &&
 217                git branch -M baz bam &&
 218                test $(git rev-parse --abbrev-ref HEAD) = bam
 219        ) &&
 220        test $(git rev-parse --abbrev-ref HEAD) = bam
 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    mb        o/o       q
 300  abc       bar       j/k       m/m       master2   n         o/p       r
 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  mb
 323  n
 324  o/o
 325  o/p
 326  q
 327  r
 328  $long
 329EOF
 330        test_cmp expected actual
 331'
 332
 333test_expect_success 'git branch with column.*' '
 334        git config column.ui column &&
 335        git config column.branch "dense" &&
 336        COLUMNS=80 git branch >actual &&
 337        git config --unset column.branch &&
 338        git config --unset column.ui &&
 339        cat >expected <<\EOF &&
 340  a/b/c   bam   foo   l   * master    mb   o/o   q
 341  abc     bar   j/k   m/m   master2   n    o/p   r
 342EOF
 343        test_cmp expected actual
 344'
 345
 346test_expect_success 'git branch --column -v should fail' '
 347        test_must_fail git branch --column -v
 348'
 349
 350test_expect_success 'git branch -v with column.ui ignored' '
 351        git config column.ui column &&
 352        COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
 353        git config --unset column.ui &&
 354        cat >expected <<\EOF &&
 355  a/b/c
 356  abc
 357  bam
 358  bar
 359  foo
 360  j/k
 361  l
 362  m/m
 363* master
 364  master2
 365  mb
 366  n
 367  o/o
 368  o/p
 369  q
 370  r
 371EOF
 372        test_cmp expected actual
 373'
 374
 375mv .git/config .git/config-saved
 376
 377test_expect_success 'git branch -m q q2 without config should succeed' '
 378        git branch -m q q2 &&
 379        git branch -m q2 q
 380'
 381
 382mv .git/config-saved .git/config
 383
 384git config branch.s/s.dummy Hello
 385
 386test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
 387        git branch --create-reflog s/s &&
 388        git reflog exists refs/heads/s/s &&
 389        git branch --create-reflog s/t &&
 390        git reflog exists refs/heads/s/t &&
 391        git branch -d s/t &&
 392        git branch -m s/s s &&
 393        git reflog exists refs/heads/s
 394'
 395
 396test_expect_success 'config information was renamed, too' '
 397        test $(git config branch.s.dummy) = Hello &&
 398        test_must_fail git config branch.s/s.dummy
 399'
 400
 401test_expect_success 'git branch -m correctly renames multiple config sections' '
 402        test_when_finished "git checkout master" &&
 403        git checkout -b source master &&
 404
 405        # Assert that a config file with multiple config sections has
 406        # those sections preserved...
 407        cat >expect <<-\EOF &&
 408        branch.dest.key1=value1
 409        some.gar.b=age
 410        branch.dest.key2=value2
 411        EOF
 412        cat >config.branch <<\EOF &&
 413;; Note the lack of -\EOF above & mixed indenting here. This is
 414;; intentional, we are also testing that the formatting of copied
 415;; sections is preserved.
 416
 417;; Comment for source. Tabs
 418[branch "source"]
 419        ;; Comment for the source value
 420        key1 = value1
 421;; Comment for some.gar. Spaces
 422[some "gar"]
 423    ;; Comment for the some.gar value
 424    b = age
 425;; Comment for source, again. Mixed tabs/spaces.
 426[branch "source"]
 427    ;; Comment for the source value, again
 428        key2 = value2
 429EOF
 430        cat config.branch >>.git/config &&
 431        git branch -m source dest &&
 432        git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
 433        test_cmp expect actual &&
 434
 435        # ...and that the comments for those sections are also
 436        # preserved.
 437        cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
 438        sed -n -e "/Note the lack/,\$p" .git/config >actual &&
 439        test_cmp expect actual
 440'
 441
 442test_expect_success 'git branch -c dumps usage' '
 443        test_expect_code 128 git branch -c 2>err &&
 444        test_i18ngrep "branch name required" err
 445'
 446
 447test_expect_success 'git branch --copy dumps usage' '
 448        test_expect_code 128 git branch --copy 2>err &&
 449        test_i18ngrep "branch name required" err
 450'
 451
 452test_expect_success 'git branch -c d e should work' '
 453        git branch --create-reflog d &&
 454        git reflog exists refs/heads/d &&
 455        git config branch.d.dummy Hello &&
 456        git branch -c d e &&
 457        git reflog exists refs/heads/d &&
 458        git reflog exists refs/heads/e &&
 459        echo Hello >expect &&
 460        git config branch.e.dummy >actual &&
 461        test_cmp expect actual &&
 462        echo Hello >expect &&
 463        git config branch.d.dummy >actual &&
 464        test_cmp expect actual
 465'
 466
 467test_expect_success 'git branch --copy is a synonym for -c' '
 468        git branch --create-reflog copy &&
 469        git reflog exists refs/heads/copy &&
 470        git config branch.copy.dummy Hello &&
 471        git branch --copy copy copy-to &&
 472        git reflog exists refs/heads/copy &&
 473        git reflog exists refs/heads/copy-to &&
 474        echo Hello >expect &&
 475        git config branch.copy.dummy >actual &&
 476        test_cmp expect actual &&
 477        echo Hello >expect &&
 478        git config branch.copy-to.dummy >actual &&
 479        test_cmp expect actual
 480'
 481
 482test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
 483        git checkout -b ee &&
 484        git reflog exists refs/heads/ee &&
 485        git config branch.ee.dummy Hello &&
 486        git branch -c ee ef &&
 487        git reflog exists refs/heads/ee &&
 488        git reflog exists refs/heads/ef &&
 489        test $(git config branch.ee.dummy) = Hello &&
 490        test $(git config branch.ef.dummy) = Hello &&
 491        test $(git rev-parse --abbrev-ref HEAD) = ee
 492'
 493
 494test_expect_success 'git branch -c f/f g/g should work' '
 495        git branch --create-reflog f/f &&
 496        git reflog exists refs/heads/f/f &&
 497        git config branch.f/f.dummy Hello &&
 498        git branch -c f/f g/g &&
 499        git reflog exists refs/heads/f/f &&
 500        git reflog exists refs/heads/g/g &&
 501        test $(git config branch.f/f.dummy) = Hello &&
 502        test $(git config branch.g/g.dummy) = Hello
 503'
 504
 505test_expect_success 'git branch -c m2 m2 should work' '
 506        git branch --create-reflog m2 &&
 507        git reflog exists refs/heads/m2 &&
 508        git config branch.m2.dummy Hello &&
 509        git branch -c m2 m2 &&
 510        git reflog exists refs/heads/m2 &&
 511        test $(git config branch.m2.dummy) = Hello
 512'
 513
 514test_expect_success 'git branch -c zz zz/zz should fail' '
 515        git branch --create-reflog zz &&
 516        git reflog exists refs/heads/zz &&
 517        test_must_fail git branch -c zz zz/zz
 518'
 519
 520test_expect_success 'git branch -c b/b b should fail' '
 521        git branch --create-reflog b/b &&
 522        test_must_fail git branch -c b/b b
 523'
 524
 525test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
 526        git branch --create-reflog o/q &&
 527        git reflog exists refs/heads/o/q &&
 528        git reflog exists refs/heads/o/p &&
 529        git branch -C o/q o/p
 530'
 531
 532test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
 533        git reflog exists refs/heads/o/q &&
 534        git reflog exists refs/heads/o/p &&
 535        git branch -c -f o/q o/p
 536'
 537
 538test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
 539        git branch qq &&
 540        git branch rr &&
 541        test_must_fail git branch -c qq rr/qq
 542'
 543
 544test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
 545        git branch b1 &&
 546        git checkout -b b2 &&
 547        test_must_fail git branch -C b1 b2
 548'
 549
 550test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
 551        git checkout -b c1 &&
 552        git branch c2 &&
 553        git branch -C c1 c2 &&
 554        test $(git rev-parse --abbrev-ref HEAD) = c1
 555'
 556
 557test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
 558        msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
 559        ! grep "$msg$" .git/logs/HEAD
 560'
 561
 562test_expect_success 'git branch -C master should work when master is checked out' '
 563        git checkout master &&
 564        git branch -C master
 565'
 566
 567test_expect_success 'git branch -C master master should work when master is checked out' '
 568        git checkout master &&
 569        git branch -C master master
 570'
 571
 572test_expect_success 'git branch -C master5 master5 should work when master is checked out' '
 573        git checkout master &&
 574        git branch master5 &&
 575        git branch -C master5 master5
 576'
 577
 578test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
 579        git branch --create-reflog cd &&
 580        git reflog exists refs/heads/cd &&
 581        git config branch.cd.dummy CD &&
 582        git branch --create-reflog ab &&
 583        git reflog exists refs/heads/ab &&
 584        git config branch.ab.dummy AB &&
 585        git branch -C ab cd &&
 586        git reflog exists refs/heads/ab &&
 587        git reflog exists refs/heads/cd &&
 588        test $(git config branch.ab.dummy) = AB &&
 589        test $(git config branch.cd.dummy) = AB
 590'
 591
 592test_expect_success 'git branch -c correctly copies multiple config sections' '
 593        FOO=1 &&
 594        export FOO &&
 595        test_when_finished "git checkout master" &&
 596        git checkout -b source2 master &&
 597
 598        # Assert that a config file with multiple config sections has
 599        # those sections preserved...
 600        cat >expect <<-\EOF &&
 601        branch.source2.key1=value1
 602        branch.dest2.key1=value1
 603        more.gar.b=age
 604        branch.source2.key2=value2
 605        branch.dest2.key2=value2
 606        EOF
 607        cat >config.branch <<\EOF &&
 608;; Note the lack of -\EOF above & mixed indenting here. This is
 609;; intentional, we are also testing that the formatting of copied
 610;; sections is preserved.
 611
 612;; Comment for source2. Tabs
 613[branch "source2"]
 614        ;; Comment for the source2 value
 615        key1 = value1
 616;; Comment for more.gar. Spaces
 617[more "gar"]
 618    ;; Comment for the more.gar value
 619    b = age
 620;; Comment for source2, again. Mixed tabs/spaces.
 621[branch "source2"]
 622    ;; Comment for the source2 value, again
 623        key2 = value2
 624EOF
 625        cat config.branch >>.git/config &&
 626        git branch -c source2 dest2 &&
 627        git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
 628        test_cmp expect actual &&
 629
 630        # ...and that the comments and formatting for those sections
 631        # is also preserved.
 632        cat >expect <<\EOF &&
 633;; Comment for source2. Tabs
 634[branch "source2"]
 635        ;; Comment for the source2 value
 636        key1 = value1
 637;; Comment for more.gar. Spaces
 638[branch "dest2"]
 639        ;; Comment for the source2 value
 640        key1 = value1
 641;; Comment for more.gar. Spaces
 642[more "gar"]
 643    ;; Comment for the more.gar value
 644    b = age
 645;; Comment for source2, again. Mixed tabs/spaces.
 646[branch "source2"]
 647    ;; Comment for the source2 value, again
 648        key2 = value2
 649[branch "dest2"]
 650    ;; Comment for the source2 value, again
 651        key2 = value2
 652EOF
 653        sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
 654        test_cmp expect actual
 655'
 656
 657test_expect_success 'deleting a symref' '
 658        git branch target &&
 659        git symbolic-ref refs/heads/symref refs/heads/target &&
 660        echo "Deleted branch symref (was refs/heads/target)." >expect &&
 661        git branch -d symref >actual &&
 662        test_path_is_file .git/refs/heads/target &&
 663        test_path_is_missing .git/refs/heads/symref &&
 664        test_i18ncmp expect actual
 665'
 666
 667test_expect_success 'deleting a dangling symref' '
 668        git symbolic-ref refs/heads/dangling-symref nowhere &&
 669        test_path_is_file .git/refs/heads/dangling-symref &&
 670        echo "Deleted branch dangling-symref (was nowhere)." >expect &&
 671        git branch -d dangling-symref >actual &&
 672        test_path_is_missing .git/refs/heads/dangling-symref &&
 673        test_i18ncmp expect actual
 674'
 675
 676test_expect_success 'deleting a self-referential symref' '
 677        git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
 678        test_path_is_file .git/refs/heads/self-reference &&
 679        echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
 680        git branch -d self-reference >actual &&
 681        test_path_is_missing .git/refs/heads/self-reference &&
 682        test_i18ncmp expect actual
 683'
 684
 685test_expect_success 'renaming a symref is not allowed' '
 686        git symbolic-ref refs/heads/master2 refs/heads/master &&
 687        test_must_fail git branch -m master2 master3 &&
 688        git symbolic-ref refs/heads/master2 &&
 689        test_path_is_file .git/refs/heads/master &&
 690        test_path_is_missing .git/refs/heads/master3
 691'
 692
 693test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
 694        git branch --create-reflog u &&
 695        mv .git/logs/refs/heads/u real-u &&
 696        ln -s real-u .git/logs/refs/heads/u &&
 697        test_must_fail git branch -m u v
 698'
 699
 700test_expect_success 'test tracking setup via --track' '
 701        git config remote.local.url . &&
 702        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 703        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 704        git branch --track my1 local/master &&
 705        test $(git config branch.my1.remote) = local &&
 706        test $(git config branch.my1.merge) = refs/heads/master
 707'
 708
 709test_expect_success 'test tracking setup (non-wildcard, matching)' '
 710        git config remote.local.url . &&
 711        git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
 712        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 713        git branch --track my4 local/master &&
 714        test $(git config branch.my4.remote) = local &&
 715        test $(git config branch.my4.merge) = refs/heads/master
 716'
 717
 718test_expect_success 'tracking setup fails on non-matching refspec' '
 719        git config remote.local.url . &&
 720        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 721        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 722        git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
 723        test_must_fail git branch --track my5 local/master &&
 724        test_must_fail git config branch.my5.remote &&
 725        test_must_fail git config branch.my5.merge
 726'
 727
 728test_expect_success 'test tracking setup via config' '
 729        git config branch.autosetupmerge true &&
 730        git config remote.local.url . &&
 731        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 732        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 733        git branch my3 local/master &&
 734        test $(git config branch.my3.remote) = local &&
 735        test $(git config branch.my3.merge) = refs/heads/master
 736'
 737
 738test_expect_success 'test overriding tracking setup via --no-track' '
 739        git config branch.autosetupmerge true &&
 740        git config remote.local.url . &&
 741        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 742        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 743        git branch --no-track my2 local/master &&
 744        git config branch.autosetupmerge false &&
 745        ! test "$(git config branch.my2.remote)" = local &&
 746        ! test "$(git config branch.my2.merge)" = refs/heads/master
 747'
 748
 749test_expect_success 'no tracking without .fetch entries' '
 750        git config branch.autosetupmerge true &&
 751        git branch my6 s &&
 752        git config branch.autosetupmerge false &&
 753        test -z "$(git config branch.my6.remote)" &&
 754        test -z "$(git config branch.my6.merge)"
 755'
 756
 757test_expect_success 'test tracking setup via --track but deeper' '
 758        git config remote.local.url . &&
 759        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 760        (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
 761        git branch --track my7 local/o/o &&
 762        test "$(git config branch.my7.remote)" = local &&
 763        test "$(git config branch.my7.merge)" = refs/heads/o/o
 764'
 765
 766test_expect_success 'test deleting branch deletes branch config' '
 767        git branch -d my7 &&
 768        test -z "$(git config branch.my7.remote)" &&
 769        test -z "$(git config branch.my7.merge)"
 770'
 771
 772test_expect_success 'test deleting branch without config' '
 773        git branch my7 s &&
 774        sha1=$(git rev-parse my7 | cut -c 1-7) &&
 775        echo "Deleted branch my7 (was $sha1)." >expect &&
 776        git branch -d my7 >actual 2>&1 &&
 777        test_i18ncmp expect actual
 778'
 779
 780test_expect_success 'deleting currently checked out branch fails' '
 781        git worktree add -b my7 my7 &&
 782        test_must_fail git -C my7 branch -d my7 &&
 783        test_must_fail git branch -d my7
 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