4a2c0a1f269f12e78bd6b47fb7d21a406d638f97
   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 '--rebase' '
 168        git branch to-rebase &&
 169        echo modified again > file &&
 170        git commit -m file file &&
 171        git checkout to-rebase &&
 172        echo new > file2 &&
 173        git add file2 &&
 174        git commit -m "new file" &&
 175        git tag before-rebase &&
 176        git pull --rebase . copy &&
 177        test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
 178        test new = "$(git show HEAD:file2)"
 179'
 180test_expect_success 'pull.rebase' '
 181        git reset --hard before-rebase &&
 182        test_config pull.rebase true &&
 183        git pull . copy &&
 184        test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
 185        test new = "$(git show HEAD:file2)"
 186'
 187
 188test_expect_success 'branch.to-rebase.rebase' '
 189        git reset --hard before-rebase &&
 190        test_config branch.to-rebase.rebase true &&
 191        git pull . copy &&
 192        test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
 193        test new = "$(git show HEAD:file2)"
 194'
 195
 196test_expect_success 'branch.to-rebase.rebase should override pull.rebase' '
 197        git reset --hard before-rebase &&
 198        test_config pull.rebase true &&
 199        test_config branch.to-rebase.rebase false &&
 200        git pull . copy &&
 201        test "$(git rev-parse HEAD^)" != "$(git rev-parse copy)" &&
 202        test new = "$(git show HEAD:file2)"
 203'
 204
 205# add a feature branch, keep-merge, that is merged into master, so the
 206# test can try preserving the merge commit (or not) with various
 207# --rebase flags/pull.rebase settings.
 208test_expect_success 'preserve merge setup' '
 209        git reset --hard before-rebase &&
 210        git checkout -b keep-merge second^ &&
 211        test_commit file3 &&
 212        git checkout to-rebase &&
 213        git merge keep-merge &&
 214        git tag before-preserve-rebase
 215'
 216
 217test_expect_success 'pull.rebase=false create a new merge commit' '
 218        git reset --hard before-preserve-rebase &&
 219        test_config pull.rebase false &&
 220        git pull . copy &&
 221        test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
 222        test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
 223        test file3 = "$(git show HEAD:file3.t)"
 224'
 225
 226test_expect_success 'pull.rebase=true flattens keep-merge' '
 227        git reset --hard before-preserve-rebase &&
 228        test_config pull.rebase true &&
 229        git pull . copy &&
 230        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 231        test file3 = "$(git show HEAD:file3.t)"
 232'
 233
 234test_expect_success 'pull.rebase=1 is treated as true and flattens keep-merge' '
 235        git reset --hard before-preserve-rebase &&
 236        test_config pull.rebase 1 &&
 237        git pull . copy &&
 238        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 239        test file3 = "$(git show HEAD:file3.t)"
 240'
 241
 242test_expect_success 'pull.rebase=preserve rebases and merges keep-merge' '
 243        git reset --hard before-preserve-rebase &&
 244        test_config pull.rebase preserve &&
 245        git pull . copy &&
 246        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 247        test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
 248'
 249
 250test_expect_success 'pull.rebase=invalid fails' '
 251        git reset --hard before-preserve-rebase &&
 252        test_config pull.rebase invalid &&
 253        ! git pull . copy
 254'
 255
 256test_expect_success '--rebase=false create a new merge commit' '
 257        git reset --hard before-preserve-rebase &&
 258        test_config pull.rebase true &&
 259        git pull --rebase=false . copy &&
 260        test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
 261        test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
 262        test file3 = "$(git show HEAD:file3.t)"
 263'
 264
 265test_expect_success '--rebase=true rebases and flattens keep-merge' '
 266        git reset --hard before-preserve-rebase &&
 267        test_config pull.rebase preserve &&
 268        git pull --rebase=true . copy &&
 269        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 270        test file3 = "$(git show HEAD:file3.t)"
 271'
 272
 273test_expect_success '--rebase=preserve rebases and merges keep-merge' '
 274        git reset --hard before-preserve-rebase &&
 275        test_config pull.rebase true &&
 276        git pull --rebase=preserve . copy &&
 277        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 278        test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
 279'
 280
 281test_expect_success '--rebase=invalid fails' '
 282        git reset --hard before-preserve-rebase &&
 283        ! git pull --rebase=invalid . copy
 284'
 285
 286test_expect_success '--rebase overrides pull.rebase=preserve and flattens keep-merge' '
 287        git reset --hard before-preserve-rebase &&
 288        test_config pull.rebase preserve &&
 289        git pull --rebase . copy &&
 290        test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
 291        test file3 = "$(git show HEAD:file3.t)"
 292'
 293
 294test_expect_success '--rebase with rebased upstream' '
 295
 296        git remote add -f me . &&
 297        git checkout copy &&
 298        git tag copy-orig &&
 299        git reset --hard HEAD^ &&
 300        echo conflicting modification > file &&
 301        git commit -m conflict file &&
 302        git checkout to-rebase &&
 303        echo file > file2 &&
 304        git commit -m to-rebase file2 &&
 305        git tag to-rebase-orig &&
 306        git pull --rebase me copy &&
 307        test "conflicting modification" = "$(cat file)" &&
 308        test file = "$(cat file2)"
 309
 310'
 311
 312test_expect_success '--rebase with rebased default upstream' '
 313
 314        git update-ref refs/remotes/me/copy copy-orig &&
 315        git checkout --track -b to-rebase2 me/copy &&
 316        git reset --hard to-rebase-orig &&
 317        git pull --rebase &&
 318        test "conflicting modification" = "$(cat file)" &&
 319        test file = "$(cat file2)"
 320
 321'
 322
 323test_expect_success 'rebased upstream + fetch + pull --rebase' '
 324
 325        git update-ref refs/remotes/me/copy copy-orig &&
 326        git reset --hard to-rebase-orig &&
 327        git checkout --track -b to-rebase3 me/copy &&
 328        git reset --hard to-rebase-orig &&
 329        git fetch &&
 330        git pull --rebase &&
 331        test "conflicting modification" = "$(cat file)" &&
 332        test file = "$(cat file2)"
 333
 334'
 335
 336test_expect_success 'pull --rebase dies early with dirty working directory' '
 337
 338        git checkout to-rebase &&
 339        git update-ref refs/remotes/me/copy copy^ &&
 340        COPY="$(git rev-parse --verify me/copy)" &&
 341        git rebase --onto $COPY copy &&
 342        test_config branch.to-rebase.remote me &&
 343        test_config branch.to-rebase.merge refs/heads/copy &&
 344        test_config branch.to-rebase.rebase true &&
 345        echo dirty >> file &&
 346        git add file &&
 347        test_must_fail git pull &&
 348        test "$COPY" = "$(git rev-parse --verify me/copy)" &&
 349        git checkout HEAD -- file &&
 350        git pull &&
 351        test "$COPY" != "$(git rev-parse --verify me/copy)"
 352
 353'
 354
 355test_expect_success 'pull --rebase works on branch yet to be born' '
 356        git rev-parse master >expect &&
 357        mkdir empty_repo &&
 358        (cd empty_repo &&
 359         git init &&
 360         git pull --rebase .. master &&
 361         git rev-parse HEAD >../actual
 362        ) &&
 363        test_cmp expect actual
 364'
 365
 366test_expect_success 'setup for detecting upstreamed changes' '
 367        mkdir src &&
 368        (cd src &&
 369         git init &&
 370         printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
 371         git add stuff &&
 372         git commit -m "Initial revision"
 373        ) &&
 374        git clone src dst &&
 375        (cd src &&
 376         modify s/5/43/ stuff &&
 377         git commit -a -m "5->43" &&
 378         modify s/6/42/ stuff &&
 379         git commit -a -m "Make it bigger"
 380        ) &&
 381        (cd dst &&
 382         modify s/5/43/ stuff &&
 383         git commit -a -m "Independent discovery of 5->43"
 384        )
 385'
 386
 387test_expect_success 'git pull --rebase detects upstreamed changes' '
 388        (cd dst &&
 389         git pull --rebase &&
 390         test -z "$(git ls-files -u)"
 391        )
 392'
 393
 394test_expect_success 'setup for avoiding reapplying old patches' '
 395        (cd dst &&
 396         test_might_fail git rebase --abort &&
 397         git reset --hard origin/master
 398        ) &&
 399        git clone --bare src src-replace.git &&
 400        rm -rf src &&
 401        mv src-replace.git src &&
 402        (cd dst &&
 403         modify s/2/22/ stuff &&
 404         git commit -a -m "Change 2" &&
 405         modify s/3/33/ stuff &&
 406         git commit -a -m "Change 3" &&
 407         modify s/4/44/ stuff &&
 408         git commit -a -m "Change 4" &&
 409         git push &&
 410
 411         modify s/44/55/ stuff &&
 412         git commit --amend -a -m "Modified Change 4"
 413        )
 414'
 415
 416test_expect_success 'git pull --rebase does not reapply old patches' '
 417        (cd dst &&
 418         test_must_fail git pull --rebase &&
 419         test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
 420        )
 421'
 422
 423test_expect_success 'git pull --rebase against local branch' '
 424        git checkout -b copy2 to-rebase-orig &&
 425        git pull --rebase . to-rebase &&
 426        test "conflicting modification" = "$(cat file)" &&
 427        test file = "$(cat file2)"
 428'
 429
 430test_done