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