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