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