bf7747dc331995fe377d4943f450ea295375565e
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Amos Waterland
   4#
   5
   6test_description='git branch --foo should not create bogus branch
   7
   8This test runs git branch --help and checks that the argument is properly
   9handled.  Specifically, that a bogus branch is not created.
  10'
  11. ./test-lib.sh
  12
  13test_expect_success \
  14    'prepare a trivial repository' \
  15    'echo Hello > A &&
  16     git update-index --add A &&
  17     git commit -m "Initial commit." &&
  18     echo World >> A &&
  19     git update-index --add A &&
  20     git commit -m "Second commit." &&
  21     HEAD=$(git rev-parse --verify HEAD)'
  22
  23test_expect_success \
  24    'git branch --help should not have created a bogus branch' '
  25     git branch --help </dev/null >/dev/null 2>/dev/null;
  26     ! test -f .git/refs/heads/--help
  27'
  28
  29test_expect_success \
  30    'git branch abc should create a branch' \
  31    'git branch abc && test -f .git/refs/heads/abc'
  32
  33test_expect_success \
  34    'git branch a/b/c should create a branch' \
  35    'git branch a/b/c && test -f .git/refs/heads/a/b/c'
  36
  37cat >expect <<EOF
  380000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000      branch: Created from master
  39EOF
  40test_expect_success \
  41    'git branch -l d/e/f should create a branch and a log' \
  42        'GIT_COMMITTER_DATE="2005-05-26 23:30" \
  43     git branch -l d/e/f &&
  44         test -f .git/refs/heads/d/e/f &&
  45         test -f .git/logs/refs/heads/d/e/f &&
  46         test_cmp expect .git/logs/refs/heads/d/e/f'
  47
  48test_expect_success \
  49    'git branch -d d/e/f should delete a branch and a log' \
  50        'git branch -d d/e/f &&
  51         test ! -f .git/refs/heads/d/e/f &&
  52         test ! -f .git/logs/refs/heads/d/e/f'
  53
  54test_expect_success \
  55    'git branch j/k should work after branch j has been deleted' \
  56       'git branch j &&
  57        git branch -d j &&
  58        git branch j/k'
  59
  60test_expect_success \
  61    'git branch l should work after branch l/m has been deleted' \
  62       'git branch l/m &&
  63        git branch -d l/m &&
  64        git branch l'
  65
  66test_expect_success \
  67    'git branch -m m m/m should work' \
  68       'git branch -l m &&
  69        git branch -m m m/m &&
  70        test -f .git/logs/refs/heads/m/m'
  71
  72test_expect_success \
  73    'git branch -m n/n n should work' \
  74       'git branch -l n/n &&
  75        git branch -m n/n n
  76        test -f .git/logs/refs/heads/n'
  77
  78test_expect_success 'git branch -m o/o o should fail when o/p exists' '
  79        git branch o/o &&
  80        git branch o/p &&
  81        test_must_fail git branch -m o/o o
  82'
  83
  84test_expect_success 'git branch -m q r/q should fail when r exists' '
  85        git branch q &&
  86        git branch r &&
  87        test_must_fail git branch -m q r/q
  88'
  89
  90mv .git/config .git/config-saved
  91
  92test_expect_success 'git branch -m q q2 without config should succeed' '
  93        git branch -m q q2 &&
  94        git branch -m q2 q
  95'
  96
  97mv .git/config-saved .git/config
  98
  99git config branch.s/s.dummy Hello
 100
 101test_expect_success \
 102    'git branch -m s/s s should work when s/t is deleted' \
 103       'git branch -l s/s &&
 104        test -f .git/logs/refs/heads/s/s &&
 105        git branch -l s/t &&
 106        test -f .git/logs/refs/heads/s/t &&
 107        git branch -d s/t &&
 108        git branch -m s/s s &&
 109        test -f .git/logs/refs/heads/s'
 110
 111test_expect_success 'config information was renamed, too' \
 112        "test $(git config branch.s.dummy) = Hello &&
 113         test_must_fail git config branch.s/s/dummy"
 114
 115test_expect_success 'renaming a symref is not allowed' \
 116'
 117        git symbolic-ref refs/heads/master2 refs/heads/master &&
 118        test_must_fail git branch -m master2 master3 &&
 119        git symbolic-ref refs/heads/master2 &&
 120        test -f .git/refs/heads/master &&
 121        ! test -f .git/refs/heads/master3
 122'
 123
 124test_expect_success SYMLINKS \
 125    'git branch -m u v should fail when the reflog for u is a symlink' '
 126     git branch -l u &&
 127     mv .git/logs/refs/heads/u real-u &&
 128     ln -s real-u .git/logs/refs/heads/u &&
 129     test_must_fail git branch -m u v
 130'
 131
 132test_expect_success 'test tracking setup via --track' \
 133    'git config remote.local.url . &&
 134     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 135     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 136     git branch --track my1 local/master &&
 137     test $(git config branch.my1.remote) = local &&
 138     test $(git config branch.my1.merge) = refs/heads/master'
 139
 140test_expect_success 'test tracking setup (non-wildcard, matching)' \
 141    'git config remote.local.url . &&
 142     git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
 143     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 144     git branch --track my4 local/master &&
 145     test $(git config branch.my4.remote) = local &&
 146     test $(git config branch.my4.merge) = refs/heads/master'
 147
 148test_expect_success 'test tracking setup (non-wildcard, not matching)' \
 149    'git config remote.local.url . &&
 150     git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
 151     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 152     git branch --track my5 local/master &&
 153     ! test "$(git config branch.my5.remote)" = local &&
 154     ! test "$(git config branch.my5.merge)" = refs/heads/master'
 155
 156test_expect_success 'test tracking setup via config' \
 157    'git config branch.autosetupmerge true &&
 158     git config remote.local.url . &&
 159     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 160     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 161     git branch my3 local/master &&
 162     test $(git config branch.my3.remote) = local &&
 163     test $(git config branch.my3.merge) = refs/heads/master'
 164
 165test_expect_success 'test overriding tracking setup via --no-track' \
 166    'git config branch.autosetupmerge true &&
 167     git config remote.local.url . &&
 168     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 169     (git show-ref -q refs/remotes/local/master || git fetch local) &&
 170     git branch --no-track my2 local/master &&
 171     git config branch.autosetupmerge false &&
 172     ! test "$(git config branch.my2.remote)" = local &&
 173     ! test "$(git config branch.my2.merge)" = refs/heads/master'
 174
 175test_expect_success 'no tracking without .fetch entries' \
 176    'git config branch.autosetupmerge true &&
 177     git branch my6 s &&
 178     git config branch.automsetupmerge false &&
 179     test -z "$(git config branch.my6.remote)" &&
 180     test -z "$(git config branch.my6.merge)"'
 181
 182test_expect_success 'test tracking setup via --track but deeper' \
 183    'git config remote.local.url . &&
 184     git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 185     (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
 186     git branch --track my7 local/o/o &&
 187     test "$(git config branch.my7.remote)" = local &&
 188     test "$(git config branch.my7.merge)" = refs/heads/o/o'
 189
 190test_expect_success 'test deleting branch deletes branch config' \
 191    'git branch -d my7 &&
 192     test -z "$(git config branch.my7.remote)" &&
 193     test -z "$(git config branch.my7.merge)"'
 194
 195test_expect_success 'test deleting branch without config' \
 196    'git branch my7 s &&
 197     sha1=$(git rev-parse my7 | cut -c 1-7) &&
 198     test "$(git branch -d my7 2>&1)" = "Deleted branch my7 (was $sha1)."'
 199
 200test_expect_success 'test --track without .fetch entries' \
 201    'git branch --track my8 &&
 202     test "$(git config branch.my8.remote)" &&
 203     test "$(git config branch.my8.merge)"'
 204
 205test_expect_success \
 206    'branch from non-branch HEAD w/autosetupmerge=always' \
 207    'git config branch.autosetupmerge always &&
 208     git branch my9 HEAD^ &&
 209     git config branch.autosetupmerge false'
 210
 211test_expect_success \
 212    'branch from non-branch HEAD w/--track causes failure' \
 213    'test_must_fail git branch --track my10 HEAD^'
 214
 215# Keep this test last, as it changes the current branch
 216cat >expect <<EOF
 2170000000000000000000000000000000000000000 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000      branch: Created from master
 218EOF
 219test_expect_success \
 220    'git checkout -b g/h/i -l should create a branch and a log' \
 221        'GIT_COMMITTER_DATE="2005-05-26 23:30" \
 222     git checkout -b g/h/i -l master &&
 223         test -f .git/refs/heads/g/h/i &&
 224         test -f .git/logs/refs/heads/g/h/i &&
 225         test_cmp expect .git/logs/refs/heads/g/h/i'
 226
 227test_expect_success 'checkout -b makes reflog by default' '
 228        git checkout master &&
 229        git config --unset core.logAllRefUpdates &&
 230        git checkout -b alpha &&
 231        test -f .git/logs/refs/heads/alpha &&
 232        PAGER= git reflog show alpha
 233'
 234
 235test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
 236        git checkout master &&
 237        git config core.logAllRefUpdates false &&
 238        git checkout -b beta &&
 239        ! test -f .git/logs/refs/heads/beta &&
 240        (
 241                PAGER= &&
 242                export PAGER &&
 243                test_must_fail git reflog show beta
 244        )
 245'
 246
 247test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
 248        git checkout master &&
 249        git checkout -lb gamma &&
 250        git config --unset core.logAllRefUpdates &&
 251        test -f .git/logs/refs/heads/gamma &&
 252        PAGER= git reflog show gamma
 253'
 254
 255test_expect_success 'avoid ambiguous track' '
 256        git config branch.autosetupmerge true &&
 257        git config remote.ambi1.url lalala &&
 258        git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
 259        git config remote.ambi2.url lilili &&
 260        git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
 261        git branch all1 master &&
 262        test -z "$(git config branch.all1.merge)"
 263'
 264
 265test_expect_success 'autosetuprebase local on a tracked local branch' '
 266        git config remote.local.url . &&
 267        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 268        git config branch.autosetuprebase local &&
 269        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 270        git branch mybase &&
 271        git branch --track myr1 mybase &&
 272        test "$(git config branch.myr1.remote)" = . &&
 273        test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
 274        test "$(git config branch.myr1.rebase)" = true
 275'
 276
 277test_expect_success 'autosetuprebase always on a tracked local branch' '
 278        git config remote.local.url . &&
 279        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 280        git config branch.autosetuprebase always &&
 281        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 282        git branch mybase2 &&
 283        git branch --track myr2 mybase &&
 284        test "$(git config branch.myr2.remote)" = . &&
 285        test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
 286        test "$(git config branch.myr2.rebase)" = true
 287'
 288
 289test_expect_success 'autosetuprebase remote on a tracked local branch' '
 290        git config remote.local.url . &&
 291        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 292        git config branch.autosetuprebase remote &&
 293        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 294        git branch mybase3 &&
 295        git branch --track myr3 mybase2 &&
 296        test "$(git config branch.myr3.remote)" = . &&
 297        test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
 298        ! test "$(git config branch.myr3.rebase)" = true
 299'
 300
 301test_expect_success 'autosetuprebase never on a tracked local branch' '
 302        git config remote.local.url . &&
 303        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 304        git config branch.autosetuprebase never &&
 305        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 306        git branch mybase4 &&
 307        git branch --track myr4 mybase2 &&
 308        test "$(git config branch.myr4.remote)" = . &&
 309        test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
 310        ! test "$(git config branch.myr4.rebase)" = true
 311'
 312
 313test_expect_success 'autosetuprebase local on a tracked remote branch' '
 314        git config remote.local.url . &&
 315        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 316        git config branch.autosetuprebase local &&
 317        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 318        git branch --track myr5 local/master &&
 319        test "$(git config branch.myr5.remote)" = local &&
 320        test "$(git config branch.myr5.merge)" = refs/heads/master &&
 321        ! test "$(git config branch.myr5.rebase)" = true
 322'
 323
 324test_expect_success 'autosetuprebase never on a tracked remote branch' '
 325        git config remote.local.url . &&
 326        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 327        git config branch.autosetuprebase never &&
 328        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 329        git branch --track myr6 local/master &&
 330        test "$(git config branch.myr6.remote)" = local &&
 331        test "$(git config branch.myr6.merge)" = refs/heads/master &&
 332        ! test "$(git config branch.myr6.rebase)" = true
 333'
 334
 335test_expect_success 'autosetuprebase remote on a tracked remote branch' '
 336        git config remote.local.url . &&
 337        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 338        git config branch.autosetuprebase remote &&
 339        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 340        git branch --track myr7 local/master &&
 341        test "$(git config branch.myr7.remote)" = local &&
 342        test "$(git config branch.myr7.merge)" = refs/heads/master &&
 343        test "$(git config branch.myr7.rebase)" = true
 344'
 345
 346test_expect_success 'autosetuprebase always on a tracked remote branch' '
 347        git config remote.local.url . &&
 348        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 349        git config branch.autosetuprebase remote &&
 350        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 351        git branch --track myr8 local/master &&
 352        test "$(git config branch.myr8.remote)" = local &&
 353        test "$(git config branch.myr8.merge)" = refs/heads/master &&
 354        test "$(git config branch.myr8.rebase)" = true
 355'
 356
 357test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
 358        git config --unset branch.autosetuprebase &&
 359        git config remote.local.url . &&
 360        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 361        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 362        git branch --track myr9 local/master &&
 363        test "$(git config branch.myr9.remote)" = local &&
 364        test "$(git config branch.myr9.merge)" = refs/heads/master &&
 365        test "z$(git config branch.myr9.rebase)" = z
 366'
 367
 368test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
 369        git config remote.local.url . &&
 370        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 371        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 372        git branch mybase10 &&
 373        git branch --track myr10 mybase2 &&
 374        test "$(git config branch.myr10.remote)" = . &&
 375        test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
 376        test "z$(git config branch.myr10.rebase)" = z
 377'
 378
 379test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
 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 branch --no-track myr11 mybase2 &&
 384        test "z$(git config branch.myr11.remote)" = z &&
 385        test "z$(git config branch.myr11.merge)" = z &&
 386        test "z$(git config branch.myr11.rebase)" = z
 387'
 388
 389test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
 390        git config remote.local.url . &&
 391        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 392        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 393        git branch --no-track myr12 local/master &&
 394        test "z$(git config branch.myr12.remote)" = z &&
 395        test "z$(git config branch.myr12.merge)" = z &&
 396        test "z$(git config branch.myr12.rebase)" = z
 397'
 398
 399test_expect_success 'autosetuprebase never on an untracked local branch' '
 400        git config branch.autosetuprebase never &&
 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 myr13 mybase2 &&
 405        test "z$(git config branch.myr13.remote)" = z &&
 406        test "z$(git config branch.myr13.merge)" = z &&
 407        test "z$(git config branch.myr13.rebase)" = z
 408'
 409
 410test_expect_success 'autosetuprebase local on an untracked local branch' '
 411        git config branch.autosetuprebase local &&
 412        git config remote.local.url . &&
 413        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 414        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 415        git branch --no-track myr14 mybase2 &&
 416        test "z$(git config branch.myr14.remote)" = z &&
 417        test "z$(git config branch.myr14.merge)" = z &&
 418        test "z$(git config branch.myr14.rebase)" = z
 419'
 420
 421test_expect_success 'autosetuprebase remote on an untracked local branch' '
 422        git config branch.autosetuprebase remote &&
 423        git config remote.local.url . &&
 424        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 425        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 426        git branch --no-track myr15 mybase2 &&
 427        test "z$(git config branch.myr15.remote)" = z &&
 428        test "z$(git config branch.myr15.merge)" = z &&
 429        test "z$(git config branch.myr15.rebase)" = z
 430'
 431
 432test_expect_success 'autosetuprebase always on an untracked local branch' '
 433        git config branch.autosetuprebase always &&
 434        git config remote.local.url . &&
 435        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 436        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 437        git branch --no-track myr16 mybase2 &&
 438        test "z$(git config branch.myr16.remote)" = z &&
 439        test "z$(git config branch.myr16.merge)" = z &&
 440        test "z$(git config branch.myr16.rebase)" = z
 441'
 442
 443test_expect_success 'autosetuprebase never on an untracked remote branch' '
 444        git config branch.autosetuprebase never &&
 445        git config remote.local.url . &&
 446        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 447        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 448        git branch --no-track myr17 local/master &&
 449        test "z$(git config branch.myr17.remote)" = z &&
 450        test "z$(git config branch.myr17.merge)" = z &&
 451        test "z$(git config branch.myr17.rebase)" = z
 452'
 453
 454test_expect_success 'autosetuprebase local on an untracked remote branch' '
 455        git config branch.autosetuprebase local &&
 456        git config remote.local.url . &&
 457        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 458        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 459        git branch --no-track myr18 local/master &&
 460        test "z$(git config branch.myr18.remote)" = z &&
 461        test "z$(git config branch.myr18.merge)" = z &&
 462        test "z$(git config branch.myr18.rebase)" = z
 463'
 464
 465test_expect_success 'autosetuprebase remote on an untracked remote branch' '
 466        git config branch.autosetuprebase remote &&
 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 myr19 local/master &&
 471        test "z$(git config branch.myr19.remote)" = z &&
 472        test "z$(git config branch.myr19.merge)" = z &&
 473        test "z$(git config branch.myr19.rebase)" = z
 474'
 475
 476test_expect_success 'autosetuprebase always on an untracked remote branch' '
 477        git config branch.autosetuprebase always &&
 478        git config remote.local.url . &&
 479        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 480        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 481        git branch --no-track myr20 local/master &&
 482        test "z$(git config branch.myr20.remote)" = z &&
 483        test "z$(git config branch.myr20.merge)" = z &&
 484        test "z$(git config branch.myr20.rebase)" = z
 485'
 486
 487test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
 488        git config branch.autosetuprebase garbage &&
 489        test_must_fail git branch
 490'
 491
 492test_expect_success 'detect misconfigured autosetuprebase (no value)' '
 493        git config --unset branch.autosetuprebase &&
 494        echo "[branch] autosetuprebase" >> .git/config &&
 495        test_must_fail git branch &&
 496        git config --unset branch.autosetuprebase
 497'
 498
 499test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
 500        git checkout my9 &&
 501        git config --unset branch.my8.merge &&
 502        test_must_fail git branch -d my8
 503'
 504
 505test_expect_success 'attempt to delete a branch merged to its base' '
 506        # we are on my9 which is the initial commit; traditionally
 507        # we would not have allowed deleting my8 that is not merged
 508        # to my9, but it is set to track master that already has my8
 509        git config branch.my8.merge refs/heads/master &&
 510        git branch -d my8
 511'
 512
 513test_expect_success 'attempt to delete a branch merged to its base' '
 514        git checkout master &&
 515        echo Third >>A &&
 516        git commit -m "Third commit" A &&
 517        git branch -t my10 my9 &&
 518        git branch -f my10 HEAD^ &&
 519        # we are on master which is at the third commit, and my10
 520        # is behind us, so traditionally we would have allowed deleting
 521        # it; but my10 is set to track my9 that is further behind.
 522        test_must_fail git branch -d my10
 523'
 524
 525test_done