t / t3200-branch.shon commit git am: Display some help text when patch is empty (dc267b1)
   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         diff 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         diff expect .git/logs/refs/heads/g/h/i'
 226
 227test_expect_success 'avoid ambiguous track' '
 228        git config branch.autosetupmerge true &&
 229        git config remote.ambi1.url lalala &&
 230        git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
 231        git config remote.ambi2.url lilili &&
 232        git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
 233        git branch all1 master &&
 234        test -z "$(git config branch.all1.merge)"
 235'
 236
 237test_expect_success 'autosetuprebase local on a tracked local branch' '
 238        git config remote.local.url . &&
 239        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 240        git config branch.autosetuprebase local &&
 241        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 242        git branch mybase &&
 243        git branch --track myr1 mybase &&
 244        test "$(git config branch.myr1.remote)" = . &&
 245        test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
 246        test "$(git config branch.myr1.rebase)" = true
 247'
 248
 249test_expect_success 'autosetuprebase always on a tracked local branch' '
 250        git config remote.local.url . &&
 251        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 252        git config branch.autosetuprebase always &&
 253        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 254        git branch mybase2 &&
 255        git branch --track myr2 mybase &&
 256        test "$(git config branch.myr2.remote)" = . &&
 257        test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
 258        test "$(git config branch.myr2.rebase)" = true
 259'
 260
 261test_expect_success 'autosetuprebase remote on a tracked local branch' '
 262        git config remote.local.url . &&
 263        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 264        git config branch.autosetuprebase remote &&
 265        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 266        git branch mybase3 &&
 267        git branch --track myr3 mybase2 &&
 268        test "$(git config branch.myr3.remote)" = . &&
 269        test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
 270        ! test "$(git config branch.myr3.rebase)" = true
 271'
 272
 273test_expect_success 'autosetuprebase never on a tracked local branch' '
 274        git config remote.local.url . &&
 275        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 276        git config branch.autosetuprebase never &&
 277        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 278        git branch mybase4 &&
 279        git branch --track myr4 mybase2 &&
 280        test "$(git config branch.myr4.remote)" = . &&
 281        test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
 282        ! test "$(git config branch.myr4.rebase)" = true
 283'
 284
 285test_expect_success 'autosetuprebase local on a tracked remote branch' '
 286        git config remote.local.url . &&
 287        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 288        git config branch.autosetuprebase local &&
 289        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 290        git branch --track myr5 local/master &&
 291        test "$(git config branch.myr5.remote)" = local &&
 292        test "$(git config branch.myr5.merge)" = refs/heads/master &&
 293        ! test "$(git config branch.myr5.rebase)" = true
 294'
 295
 296test_expect_success 'autosetuprebase never on a tracked remote branch' '
 297        git config remote.local.url . &&
 298        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 299        git config branch.autosetuprebase never &&
 300        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 301        git branch --track myr6 local/master &&
 302        test "$(git config branch.myr6.remote)" = local &&
 303        test "$(git config branch.myr6.merge)" = refs/heads/master &&
 304        ! test "$(git config branch.myr6.rebase)" = true
 305'
 306
 307test_expect_success 'autosetuprebase remote on a tracked remote branch' '
 308        git config remote.local.url . &&
 309        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 310        git config branch.autosetuprebase remote &&
 311        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 312        git branch --track myr7 local/master &&
 313        test "$(git config branch.myr7.remote)" = local &&
 314        test "$(git config branch.myr7.merge)" = refs/heads/master &&
 315        test "$(git config branch.myr7.rebase)" = true
 316'
 317
 318test_expect_success 'autosetuprebase always on a tracked remote branch' '
 319        git config remote.local.url . &&
 320        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 321        git config branch.autosetuprebase remote &&
 322        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 323        git branch --track myr8 local/master &&
 324        test "$(git config branch.myr8.remote)" = local &&
 325        test "$(git config branch.myr8.merge)" = refs/heads/master &&
 326        test "$(git config branch.myr8.rebase)" = true
 327'
 328
 329test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
 330        git config --unset branch.autosetuprebase &&
 331        git config remote.local.url . &&
 332        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 333        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 334        git branch --track myr9 local/master &&
 335        test "$(git config branch.myr9.remote)" = local &&
 336        test "$(git config branch.myr9.merge)" = refs/heads/master &&
 337        test "z$(git config branch.myr9.rebase)" = z
 338'
 339
 340test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
 341        git config remote.local.url . &&
 342        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 343        (git show-ref -q refs/remotes/local/o || git fetch local) &&
 344        git branch mybase10 &&
 345        git branch --track myr10 mybase2 &&
 346        test "$(git config branch.myr10.remote)" = . &&
 347        test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
 348        test "z$(git config branch.myr10.rebase)" = z
 349'
 350
 351test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
 352        git config remote.local.url . &&
 353        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 354        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 355        git branch --no-track myr11 mybase2 &&
 356        test "z$(git config branch.myr11.remote)" = z &&
 357        test "z$(git config branch.myr11.merge)" = z &&
 358        test "z$(git config branch.myr11.rebase)" = z
 359'
 360
 361test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
 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 --no-track myr12 local/master &&
 366        test "z$(git config branch.myr12.remote)" = z &&
 367        test "z$(git config branch.myr12.merge)" = z &&
 368        test "z$(git config branch.myr12.rebase)" = z
 369'
 370
 371test_expect_success 'autosetuprebase never on an untracked local branch' '
 372        git config branch.autosetuprebase never &&
 373        git config remote.local.url . &&
 374        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 375        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 376        git branch --no-track myr13 mybase2 &&
 377        test "z$(git config branch.myr13.remote)" = z &&
 378        test "z$(git config branch.myr13.merge)" = z &&
 379        test "z$(git config branch.myr13.rebase)" = z
 380'
 381
 382test_expect_success 'autosetuprebase local on an untracked local branch' '
 383        git config branch.autosetuprebase local &&
 384        git config remote.local.url . &&
 385        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 386        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 387        git branch --no-track myr14 mybase2 &&
 388        test "z$(git config branch.myr14.remote)" = z &&
 389        test "z$(git config branch.myr14.merge)" = z &&
 390        test "z$(git config branch.myr14.rebase)" = z
 391'
 392
 393test_expect_success 'autosetuprebase remote on an untracked local branch' '
 394        git config branch.autosetuprebase remote &&
 395        git config remote.local.url . &&
 396        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 397        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 398        git branch --no-track myr15 mybase2 &&
 399        test "z$(git config branch.myr15.remote)" = z &&
 400        test "z$(git config branch.myr15.merge)" = z &&
 401        test "z$(git config branch.myr15.rebase)" = z
 402'
 403
 404test_expect_success 'autosetuprebase always on an untracked local branch' '
 405        git config branch.autosetuprebase always &&
 406        git config remote.local.url . &&
 407        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 408        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 409        git branch --no-track myr16 mybase2 &&
 410        test "z$(git config branch.myr16.remote)" = z &&
 411        test "z$(git config branch.myr16.merge)" = z &&
 412        test "z$(git config branch.myr16.rebase)" = z
 413'
 414
 415test_expect_success 'autosetuprebase never on an untracked remote branch' '
 416        git config branch.autosetuprebase never &&
 417        git config remote.local.url . &&
 418        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 419        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 420        git branch --no-track myr17 local/master &&
 421        test "z$(git config branch.myr17.remote)" = z &&
 422        test "z$(git config branch.myr17.merge)" = z &&
 423        test "z$(git config branch.myr17.rebase)" = z
 424'
 425
 426test_expect_success 'autosetuprebase local on an untracked remote branch' '
 427        git config branch.autosetuprebase local &&
 428        git config remote.local.url . &&
 429        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 430        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 431        git branch --no-track myr18 local/master &&
 432        test "z$(git config branch.myr18.remote)" = z &&
 433        test "z$(git config branch.myr18.merge)" = z &&
 434        test "z$(git config branch.myr18.rebase)" = z
 435'
 436
 437test_expect_success 'autosetuprebase remote on an untracked remote branch' '
 438        git config branch.autosetuprebase remote &&
 439        git config remote.local.url . &&
 440        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 441        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 442        git branch --no-track myr19 local/master &&
 443        test "z$(git config branch.myr19.remote)" = z &&
 444        test "z$(git config branch.myr19.merge)" = z &&
 445        test "z$(git config branch.myr19.rebase)" = z
 446'
 447
 448test_expect_success 'autosetuprebase always on an untracked remote branch' '
 449        git config branch.autosetuprebase always &&
 450        git config remote.local.url . &&
 451        git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
 452        (git show-ref -q refs/remotes/local/master || git fetch local) &&
 453        git branch --no-track myr20 local/master &&
 454        test "z$(git config branch.myr20.remote)" = z &&
 455        test "z$(git config branch.myr20.merge)" = z &&
 456        test "z$(git config branch.myr20.rebase)" = z
 457'
 458
 459test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
 460        git config branch.autosetuprebase garbage &&
 461        test_must_fail git branch
 462'
 463
 464test_expect_success 'detect misconfigured autosetuprebase (no value)' '
 465        git config --unset branch.autosetuprebase &&
 466        echo "[branch] autosetuprebase" >> .git/config &&
 467        test_must_fail git branch &&
 468        git config --unset branch.autosetuprebase
 469'
 470
 471test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
 472        git checkout my9 &&
 473        git config --unset branch.my8.merge &&
 474        test_must_fail git branch -d my8
 475'
 476
 477test_expect_success 'attempt to delete a branch merged to its base' '
 478        # we are on my9 which is the initial commit; traditionally
 479        # we would not have allowed deleting my8 that is not merged
 480        # to my9, but it is set to track master that already has my8
 481        git config branch.my8.merge refs/heads/master &&
 482        git branch -d my8
 483'
 484
 485test_expect_success 'attempt to delete a branch merged to its base' '
 486        git checkout master &&
 487        echo Third >>A &&
 488        git commit -m "Third commit" A &&
 489        git branch -t my10 my9 &&
 490        git branch -f my10 HEAD^ &&
 491        # we are on master which is at the third commit, and my10
 492        # is behind us, so traditionally we would have allowed deleting
 493        # it; but my10 is set to track my9 that is further behind.
 494        test_must_fail git branch -d my10
 495'
 496
 497test_done