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