t / t5520-pull.shon commit send-email: simplify Gmail example in the documentation (4855f06)
   1#!/bin/sh
   2
   3test_description='pulling into void'
   4
   5. ./test-lib.sh
   6
   7modify () {
   8        sed -e "$1" <"$2" >"$2.x" &&
   9        mv "$2.x" "$2"
  10}
  11
  12test_pull_autostash () {
  13        git reset --hard before-rebase &&
  14        echo dirty >new_file &&
  15        git add new_file &&
  16        git pull "$@" . copy &&
  17        test_cmp_rev HEAD^ copy &&
  18        test "$(cat new_file)" = dirty &&
  19        test "$(cat file)" = "modified again"
  20}
  21
  22test_pull_autostash_fail () {
  23        git reset --hard before-rebase &&
  24        echo dirty >new_file &&
  25        git add new_file &&
  26        test_must_fail git pull "$@" . copy 2>err &&
  27        test_i18ngrep "uncommitted changes." err
  28}
  29
  30test_expect_success setup '
  31        echo file >file &&
  32        git add file &&
  33        git commit -a -m original
  34'
  35
  36test_expect_success 'pulling into void' '
  37        git init cloned &&
  38        (
  39                cd cloned &&
  40                git pull ..
  41        ) &&
  42        test -f file &&
  43        test -f cloned/file &&
  44        test_cmp file cloned/file
  45'
  46
  47test_expect_success 'pulling into void using master:master' '
  48        git init cloned-uho &&
  49        (
  50                cd cloned-uho &&
  51                git pull .. master:master
  52        ) &&
  53        test -f file &&
  54        test -f cloned-uho/file &&
  55        test_cmp file cloned-uho/file
  56'
  57
  58test_expect_success 'pulling into void does not overwrite untracked files' '
  59        git init cloned-untracked &&
  60        (
  61                cd cloned-untracked &&
  62                echo untracked >file &&
  63                test_must_fail git pull .. master &&
  64                echo untracked >expect &&
  65                test_cmp expect file
  66        )
  67'
  68
  69test_expect_success 'pulling into void does not overwrite staged files' '
  70        git init cloned-staged-colliding &&
  71        (
  72                cd cloned-staged-colliding &&
  73                echo "alternate content" >file &&
  74                git add file &&
  75                test_must_fail git pull .. master &&
  76                echo "alternate content" >expect &&
  77                test_cmp expect file &&
  78                git cat-file blob :file >file.index &&
  79                test_cmp expect file.index
  80        )
  81'
  82
  83test_expect_success 'pulling into void does not remove new staged files' '
  84        git init cloned-staged-new &&
  85        (
  86                cd cloned-staged-new &&
  87                echo "new tracked file" >newfile &&
  88                git add newfile &&
  89                git pull .. master &&
  90                echo "new tracked file" >expect &&
  91                test_cmp expect newfile &&
  92                git cat-file blob :newfile >newfile.index &&
  93                test_cmp expect newfile.index
  94        )
  95'
  96
  97test_expect_success 'pulling into void must not create an octopus' '
  98        git init cloned-octopus &&
  99        (
 100                cd cloned-octopus &&
 101                test_must_fail git pull .. master master &&
 102                ! test -f file
 103        )
 104'
 105
 106test_expect_success 'test . as a remote' '
 107        git branch copy master &&
 108        git config branch.copy.remote . &&
 109        git config branch.copy.merge refs/heads/master &&
 110        echo updated >file &&
 111        git commit -a -m updated &&
 112        git checkout copy &&
 113        test "$(cat file)" = file &&
 114        git pull &&
 115        test "$(cat file)" = updated &&
 116        git reflog -1 >reflog.actual &&
 117        sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
 118        echo "OBJID HEAD@{0}: pull: Fast-forward" >reflog.expected &&
 119        test_cmp reflog.expected reflog.fuzzy
 120'
 121
 122test_expect_success 'the default remote . should not break explicit pull' '
 123        git checkout -b second master^ &&
 124        echo modified >file &&
 125        git commit -a -m modified &&
 126        git checkout copy &&
 127        git reset --hard HEAD^ &&
 128        test "$(cat file)" = file &&
 129        git pull . second &&
 130        test "$(cat file)" = modified &&
 131        git reflog -1 >reflog.actual &&
 132        sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
 133        echo "OBJID HEAD@{0}: pull . second: Fast-forward" >reflog.expected &&
 134        test_cmp reflog.expected reflog.fuzzy
 135'
 136
 137test_expect_success 'fail if wildcard spec does not match any refs' '
 138        git checkout -b test copy^ &&
 139        test_when_finished "git checkout -f copy && git branch -D test" &&
 140        test "$(cat file)" = file &&
 141        test_must_fail git pull . "refs/nonexisting1/*:refs/nonexisting2/*" 2>err &&
 142        test_i18ngrep "no candidates for merging" err &&
 143        test "$(cat file)" = file
 144'
 145
 146test_expect_success 'fail if no branches specified with non-default remote' '
 147        git remote add test_remote . &&
 148        test_when_finished "git remote remove test_remote" &&
 149        git checkout -b test copy^ &&
 150        test_when_finished "git checkout -f copy && git branch -D test" &&
 151        test "$(cat file)" = file &&
 152        test_config branch.test.remote origin &&
 153        test_must_fail git pull test_remote 2>err &&
 154        test_i18ngrep "specify a branch on the command line" err &&
 155        test "$(cat file)" = file
 156'
 157
 158test_expect_success 'fail if not on a branch' '
 159        git remote add origin . &&
 160        test_when_finished "git remote remove origin" &&
 161        git checkout HEAD^ &&
 162        test_when_finished "git checkout -f copy" &&
 163        test "$(cat file)" = file &&
 164        test_must_fail git pull 2>err &&
 165        test_i18ngrep "not currently on a branch" err &&
 166        test "$(cat file)" = file
 167'
 168
 169test_expect_success 'fail if no configuration for current branch' '
 170        git remote add test_remote . &&
 171        test_when_finished "git remote remove test_remote" &&
 172        git checkout -b test copy^ &&
 173        test_when_finished "git checkout -f copy && git branch -D test" &&
 174        test_config branch.test.remote test_remote &&
 175        test "$(cat file)" = file &&
 176        test_must_fail git pull 2>err &&
 177        test_i18ngrep "no tracking information" err &&
 178        test "$(cat file)" = file
 179'
 180
 181test_expect_success 'pull --all: fail if no configuration for current branch' '
 182        git remote add test_remote . &&
 183        test_when_finished "git remote remove test_remote" &&
 184        git checkout -b test copy^ &&
 185        test_when_finished "git checkout -f copy && git branch -D test" &&
 186        test_config branch.test.remote test_remote &&
 187        test "$(cat file)" = file &&
 188        test_must_fail git pull --all 2>err &&
 189        test_i18ngrep "There is no tracking information" err &&
 190        test "$(cat file)" = file
 191'
 192
 193test_expect_success 'fail if upstream branch does not exist' '
 194        git checkout -b test copy^ &&
 195        test_when_finished "git checkout -f copy && git branch -D test" &&
 196        test_config branch.test.remote . &&
 197        test_config branch.test.merge refs/heads/nonexisting &&
 198        test "$(cat file)" = file &&
 199        test_must_fail git pull 2>err &&
 200        test_i18ngrep "no such ref was fetched" err &&
 201        test "$(cat file)" = file
 202'
 203
 204test_expect_success 'fail if the index has unresolved entries' '
 205        git checkout -b third second^ &&
 206        test_when_finished "git checkout -f copy && git branch -D third" &&
 207        test "$(cat file)" = file &&
 208        test_commit modified2 file &&
 209        test -z "$(git ls-files -u)" &&
 210        test_must_fail git pull . second &&
 211        test -n "$(git ls-files -u)" &&
 212        cp file expected &&
 213        test_must_fail git pull . second 2>err &&
 214        test_i18ngrep "Pull is not possible because you have unmerged files" err &&
 215        test_cmp expected file &&
 216        git add file &&
 217        test -z "$(git ls-files -u)" &&
 218        test_must_fail git pull . second 2>err &&
 219        test_i18ngrep "You have not concluded your merge" err &&
 220        test_cmp expected file
 221'
 222
 223test_expect_success 'fast-forwards working tree if branch head is updated' '
 224        git checkout -b third second^ &&
 225        test_when_finished "git checkout -f copy && git branch -D third" &&
 226        test "$(cat file)" = file &&
 227        git pull . second:third 2>err &&
 228        test_i18ngrep "fetch updated the current branch head" err &&
 229        test "$(cat file)" = modified &&
 230        test "$(git rev-parse third)" = "$(git rev-parse second)"
 231'
 232
 233test_expect_success 'fast-forward fails with conflicting work tree' '
 234        git checkout -b third second^ &&
 235        test_when_finished "git checkout -f copy && git branch -D third" &&
 236        test "$(cat file)" = file &&
 237        echo conflict >file &&
 238        test_must_fail git pull . second:third 2>err &&
 239        test_i18ngrep "Cannot fast-forward your working tree" err &&
 240        test "$(cat file)" = conflict &&
 241        test "$(git rev-parse third)" = "$(git rev-parse second)"
 242'
 243
 244test_expect_success '--rebase' '
 245        git branch to-rebase &&
 246        echo modified again > file &&
 247        git commit -m file file &&
 248        git checkout to-rebase &&
 249        echo new > file2 &&
 250        git add file2 &&
 251        git commit -m "new file" &&
 252        git tag before-rebase &&
 253        git pull --rebase . copy &&
 254        test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
 255        test new = "$(git show HEAD:file2)"
 256'
 257
 258test_expect_success '--rebase fails with multiple branches' '
 259        git reset --hard before-rebase &&
 260        test_must_fail git pull --rebase . copy master 2>err &&
 261        test "$(git rev-parse HEAD)" = "$(git rev-parse before-rebase)" &&
 262        test_i18ngrep "Cannot rebase onto multiple branches" err &&
 263        test modified = "$(git show HEAD:file)"
 264'
 265
 266test_expect_success 'pull --rebase succeeds with dirty working directory and rebase.autostash set' '
 267        test_config rebase.autostash true &&
 268        test_pull_autostash --rebase
 269'
 270
 271test_expect_success 'pull --rebase --autostash & rebase.autostash=true' '
 272        test_config rebase.autostash true &&
 273        test_pull_autostash --rebase --autostash
 274'
 275
 276test_expect_success 'pull --rebase --autostash & rebase.autostash=false' '
 277        test_config rebase.autostash false &&
 278        test_pull_autostash --rebase --autostash
 279'
 280
 281test_expect_success 'pull --rebase --autostash & rebase.autostash unset' '
 282        test_unconfig rebase.autostash &&
 283        test_pull_autostash --rebase --autostash
 284'
 285
 286test_expect_success 'pull --rebase --no-autostash & rebase.autostash=true' '
 287        test_config rebase.autostash true &&
 288        test_pull_autostash_fail --rebase --no-autostash
 289'
 290
 291test_expect_success 'pull --rebase --no-autostash & rebase.autostash=false' '
 292        test_config rebase.autostash false &&
 293        test_pull_autostash_fail --rebase --no-autostash
 294'
 295
 296test_expect_success 'pull --rebase --no-autostash & rebase.autostash unset' '
 297        test_unconfig rebase.autostash &&
 298        test_pull_autostash_fail --rebase --no-autostash
 299'
 300
 301for i in --autostash --no-autostash
 302do
 303        test_expect_success "pull $i (without --rebase) is illegal" '
 304                test_must_fail git pull $i . copy 2>err &&
 305                test_i18ngrep "only valid with --rebase" err
 306        '
 307done
 308
 309test_expect_success 'pull.rebase' '
 310        git reset --hard before-rebase &&
 311        test_config pull.rebase true &&
 312        git pull . copy &&
 313        test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
 314        test new = "$(git show HEAD:file2)"
 315'
 316
 317test_expect_success 'pull --autostash & pull.rebase=true' '
 318        test_config pull.rebase true &&
 319        test_pull_autostash --autostash
 320'
 321
 322test_expect_success 'pull --no-autostash & pull.rebase=true' '
 323        test_config pull.rebase true &&
 324        test_pull_autostash_fail --no-autostash
 325'
 326
 327test_expect_success 'branch.to-rebase.rebase' '
 328        git reset --hard before-rebase &&
 329        test_config branch.to-rebase.rebase true &&
 330        git pull . copy &&
 331        test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
 332        test new = "$(git show HEAD:file2)"
 333'
 334
 335test_expect_success 'branch.to-rebase.rebase should override pull.rebase' '
 336        git reset --hard before-rebase &&
 337        test_config pull.rebase true &&
 338        test_config branch.to-rebase.rebase false &&
 339        git pull . copy &&
 340        test "$(git rev-parse HEAD^)" != "$(git rev-parse copy)" &&
 341        test new = "$(git show HEAD:file2)"
 342'
 343
 344# add a feature branch, keep-merge, that is merged into master, so the
 345# test can try preserving the merge commit (or not) with various
 346# --rebase flags/pull.rebase settings.
 347test_expect_success 'preserve merge setup' '
 348        git reset --hard before-rebase &&
 349        git checkout -b keep-merge second^ &&
 350        test_commit file3 &&
 351        git checkout to-rebase &&
 352        git merge keep-merge &&
 353        git tag before-preserve-rebase
 354'
 355
 356test_expect_success 'pull.rebase=false create a new merge commit' '
 357        git reset --hard before-preserve-rebase &&
 358        test_config pull.rebase false &&
 359        git pull . copy &&
 360        test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
 361        test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
 362        test file3 = "$(git show HEAD:file3.t)"
 363'
 364
 365test_expect_success 'pull.rebase=true flattens keep-merge' '
 366        git reset --hard before-preserve-rebase &&
 367        test_config pull.rebase true &&
 368        git pull . copy &&
 369        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 370        test file3 = "$(git show HEAD:file3.t)"
 371'
 372
 373test_expect_success 'pull.rebase=1 is treated as true and flattens keep-merge' '
 374        git reset --hard before-preserve-rebase &&
 375        test_config pull.rebase 1 &&
 376        git pull . copy &&
 377        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 378        test file3 = "$(git show HEAD:file3.t)"
 379'
 380
 381test_expect_success 'pull.rebase=preserve rebases and merges keep-merge' '
 382        git reset --hard before-preserve-rebase &&
 383        test_config pull.rebase preserve &&
 384        git pull . copy &&
 385        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 386        test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
 387'
 388
 389test_expect_success 'pull.rebase=interactive' '
 390        write_script "$TRASH_DIRECTORY/fake-editor" <<-\EOF &&
 391        echo I was here >fake.out &&
 392        false
 393        EOF
 394        test_set_editor "$TRASH_DIRECTORY/fake-editor" &&
 395        test_must_fail git pull --rebase=interactive . copy &&
 396        test "I was here" = "$(cat fake.out)"
 397'
 398
 399test_expect_success 'pull.rebase=invalid fails' '
 400        git reset --hard before-preserve-rebase &&
 401        test_config pull.rebase invalid &&
 402        ! git pull . copy
 403'
 404
 405test_expect_success '--rebase=false create a new merge commit' '
 406        git reset --hard before-preserve-rebase &&
 407        test_config pull.rebase true &&
 408        git pull --rebase=false . copy &&
 409        test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
 410        test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
 411        test file3 = "$(git show HEAD:file3.t)"
 412'
 413
 414test_expect_success '--rebase=true rebases and flattens keep-merge' '
 415        git reset --hard before-preserve-rebase &&
 416        test_config pull.rebase preserve &&
 417        git pull --rebase=true . copy &&
 418        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 419        test file3 = "$(git show HEAD:file3.t)"
 420'
 421
 422test_expect_success '--rebase=preserve rebases and merges keep-merge' '
 423        git reset --hard before-preserve-rebase &&
 424        test_config pull.rebase true &&
 425        git pull --rebase=preserve . copy &&
 426        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 427        test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
 428'
 429
 430test_expect_success '--rebase=invalid fails' '
 431        git reset --hard before-preserve-rebase &&
 432        ! git pull --rebase=invalid . copy
 433'
 434
 435test_expect_success '--rebase overrides pull.rebase=preserve and flattens keep-merge' '
 436        git reset --hard before-preserve-rebase &&
 437        test_config pull.rebase preserve &&
 438        git pull --rebase . copy &&
 439        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 440        test file3 = "$(git show HEAD:file3.t)"
 441'
 442
 443test_expect_success '--rebase with rebased upstream' '
 444
 445        git remote add -f me . &&
 446        git checkout copy &&
 447        git tag copy-orig &&
 448        git reset --hard HEAD^ &&
 449        echo conflicting modification > file &&
 450        git commit -m conflict file &&
 451        git checkout to-rebase &&
 452        echo file > file2 &&
 453        git commit -m to-rebase file2 &&
 454        git tag to-rebase-orig &&
 455        git pull --rebase me copy &&
 456        test "conflicting modification" = "$(cat file)" &&
 457        test file = "$(cat file2)"
 458
 459'
 460
 461test_expect_success '--rebase -f with rebased upstream' '
 462        test_when_finished "test_might_fail git rebase --abort" &&
 463        git reset --hard to-rebase-orig &&
 464        git pull --rebase -f me copy &&
 465        test "conflicting modification" = "$(cat file)" &&
 466        test file = "$(cat file2)"
 467'
 468
 469test_expect_success '--rebase with rebased default upstream' '
 470
 471        git update-ref refs/remotes/me/copy copy-orig &&
 472        git checkout --track -b to-rebase2 me/copy &&
 473        git reset --hard to-rebase-orig &&
 474        git pull --rebase &&
 475        test "conflicting modification" = "$(cat file)" &&
 476        test file = "$(cat file2)"
 477
 478'
 479
 480test_expect_success 'rebased upstream + fetch + pull --rebase' '
 481
 482        git update-ref refs/remotes/me/copy copy-orig &&
 483        git reset --hard to-rebase-orig &&
 484        git checkout --track -b to-rebase3 me/copy &&
 485        git reset --hard to-rebase-orig &&
 486        git fetch &&
 487        git pull --rebase &&
 488        test "conflicting modification" = "$(cat file)" &&
 489        test file = "$(cat file2)"
 490
 491'
 492
 493test_expect_success 'pull --rebase dies early with dirty working directory' '
 494
 495        git checkout to-rebase &&
 496        git update-ref refs/remotes/me/copy copy^ &&
 497        COPY="$(git rev-parse --verify me/copy)" &&
 498        git rebase --onto $COPY copy &&
 499        test_config branch.to-rebase.remote me &&
 500        test_config branch.to-rebase.merge refs/heads/copy &&
 501        test_config branch.to-rebase.rebase true &&
 502        echo dirty >> file &&
 503        git add file &&
 504        test_must_fail git pull &&
 505        test "$COPY" = "$(git rev-parse --verify me/copy)" &&
 506        git checkout HEAD -- file &&
 507        git pull &&
 508        test "$COPY" != "$(git rev-parse --verify me/copy)"
 509
 510'
 511
 512test_expect_success 'pull --rebase works on branch yet to be born' '
 513        git rev-parse master >expect &&
 514        mkdir empty_repo &&
 515        (cd empty_repo &&
 516         git init &&
 517         git pull --rebase .. master &&
 518         git rev-parse HEAD >../actual
 519        ) &&
 520        test_cmp expect actual
 521'
 522
 523test_expect_success 'pull --rebase fails on unborn branch with staged changes' '
 524        test_when_finished "rm -rf empty_repo2" &&
 525        git init empty_repo2 &&
 526        (
 527                cd empty_repo2 &&
 528                echo staged-file >staged-file &&
 529                git add staged-file &&
 530                test "$(git ls-files)" = staged-file &&
 531                test_must_fail git pull --rebase .. master 2>err &&
 532                test "$(git ls-files)" = staged-file &&
 533                test "$(git show :staged-file)" = staged-file &&
 534                test_i18ngrep "unborn branch with changes added to the index" err
 535        )
 536'
 537
 538test_expect_success 'setup for detecting upstreamed changes' '
 539        mkdir src &&
 540        (cd src &&
 541         git init &&
 542         printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
 543         git add stuff &&
 544         git commit -m "Initial revision"
 545        ) &&
 546        git clone src dst &&
 547        (cd src &&
 548         modify s/5/43/ stuff &&
 549         git commit -a -m "5->43" &&
 550         modify s/6/42/ stuff &&
 551         git commit -a -m "Make it bigger"
 552        ) &&
 553        (cd dst &&
 554         modify s/5/43/ stuff &&
 555         git commit -a -m "Independent discovery of 5->43"
 556        )
 557'
 558
 559test_expect_success 'git pull --rebase detects upstreamed changes' '
 560        (cd dst &&
 561         git pull --rebase &&
 562         test -z "$(git ls-files -u)"
 563        )
 564'
 565
 566test_expect_success 'setup for avoiding reapplying old patches' '
 567        (cd dst &&
 568         test_might_fail git rebase --abort &&
 569         git reset --hard origin/master
 570        ) &&
 571        git clone --bare src src-replace.git &&
 572        rm -rf src &&
 573        mv src-replace.git src &&
 574        (cd dst &&
 575         modify s/2/22/ stuff &&
 576         git commit -a -m "Change 2" &&
 577         modify s/3/33/ stuff &&
 578         git commit -a -m "Change 3" &&
 579         modify s/4/44/ stuff &&
 580         git commit -a -m "Change 4" &&
 581         git push &&
 582
 583         modify s/44/55/ stuff &&
 584         git commit --amend -a -m "Modified Change 4"
 585        )
 586'
 587
 588test_expect_success 'git pull --rebase does not reapply old patches' '
 589        (cd dst &&
 590         test_must_fail git pull --rebase &&
 591         test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
 592        )
 593'
 594
 595test_expect_success 'git pull --rebase against local branch' '
 596        git checkout -b copy2 to-rebase-orig &&
 597        git pull --rebase . to-rebase &&
 598        test "conflicting modification" = "$(cat file)" &&
 599        test file = "$(cat file2)"
 600'
 601
 602test_done