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