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