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