t5520: use consistent capitalization in test titles
[git] / t / t5520-pull.sh
1 #!/bin/sh
2
3 test_description='pulling into void'
4
5 . ./test-lib.sh
6
7 modify () {
8         sed -e "$1" <"$2" >"$2.x" &&
9         mv "$2.x" "$2"
10 }
11
12 test_expect_success setup '
13         echo file >file &&
14         git add file &&
15         git commit -a -m original
16 '
17
18 test_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
29 test_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
40 test_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
51 test_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
65 test_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
79 test_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
88 test_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
104 test_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
119 test_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
128 test_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
140 test_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
151 test_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
163 test_expect_success 'pull --all: fail if no configuration for current branch' '
164         git remote add test_remote . &&
165         test_when_finished "git remote remove test_remote" &&
166         git checkout -b test copy^ &&
167         test_when_finished "git checkout -f copy && git branch -D test" &&
168         test_config branch.test.remote test_remote &&
169         test "$(cat file)" = file &&
170         test_must_fail git pull --all 2>err &&
171         test_i18ngrep "There is no tracking information" err &&
172         test "$(cat file)" = file
173 '
174
175 test_expect_success 'fail if upstream branch does not exist' '
176         git checkout -b test copy^ &&
177         test_when_finished "git checkout -f copy && git branch -D test" &&
178         test_config branch.test.remote . &&
179         test_config branch.test.merge refs/heads/nonexisting &&
180         test "$(cat file)" = file &&
181         test_must_fail git pull 2>err &&
182         test_i18ngrep "no such ref was fetched" err &&
183         test "$(cat file)" = file
184 '
185
186 test_expect_success 'fail if the index has unresolved entries' '
187         git checkout -b third second^ &&
188         test_when_finished "git checkout -f copy && git branch -D third" &&
189         test "$(cat file)" = file &&
190         test_commit modified2 file &&
191         test -z "$(git ls-files -u)" &&
192         test_must_fail git pull . second &&
193         test -n "$(git ls-files -u)" &&
194         cp file expected &&
195         test_must_fail git pull . second 2>err &&
196         test_i18ngrep "Pull is not possible because you have unmerged files" err &&
197         test_cmp expected file &&
198         git add file &&
199         test -z "$(git ls-files -u)" &&
200         test_must_fail git pull . second 2>err &&
201         test_i18ngrep "You have not concluded your merge" err &&
202         test_cmp expected file
203 '
204
205 test_expect_success 'fast-forwards working tree if branch head is updated' '
206         git checkout -b third second^ &&
207         test_when_finished "git checkout -f copy && git branch -D third" &&
208         test "$(cat file)" = file &&
209         git pull . second:third 2>err &&
210         test_i18ngrep "fetch updated the current branch head" err &&
211         test "$(cat file)" = modified &&
212         test "$(git rev-parse third)" = "$(git rev-parse second)"
213 '
214
215 test_expect_success 'fast-forward fails with conflicting work tree' '
216         git checkout -b third second^ &&
217         test_when_finished "git checkout -f copy && git branch -D third" &&
218         test "$(cat file)" = file &&
219         echo conflict >file &&
220         test_must_fail git pull . second:third 2>err &&
221         test_i18ngrep "Cannot fast-forward your working tree" err &&
222         test "$(cat file)" = conflict &&
223         test "$(git rev-parse third)" = "$(git rev-parse second)"
224 '
225
226 test_expect_success '--rebase' '
227         git branch to-rebase &&
228         echo modified again > file &&
229         git commit -m file file &&
230         git checkout to-rebase &&
231         echo new > file2 &&
232         git add file2 &&
233         git commit -m "new file" &&
234         git tag before-rebase &&
235         git pull --rebase . copy &&
236         test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
237         test new = "$(git show HEAD:file2)"
238 '
239
240 test_expect_success '--rebase fails with multiple branches' '
241         git reset --hard before-rebase &&
242         test_must_fail git pull --rebase . copy master 2>err &&
243         test "$(git rev-parse HEAD)" = "$(git rev-parse before-rebase)" &&
244         test_i18ngrep "Cannot rebase onto multiple branches" err &&
245         test modified = "$(git show HEAD:file)"
246 '
247
248 test_expect_success 'pull --rebase succeeds with dirty working directory and rebase.autostash set' '
249         test_config rebase.autostash true &&
250         git reset --hard before-rebase &&
251         echo dirty >new_file &&
252         git add new_file &&
253         git pull --rebase . copy &&
254         test_cmp_rev HEAD^ copy &&
255         test "$(cat new_file)" = dirty &&
256         test "$(cat file)" = "modified again"
257 '
258
259 test_expect_success 'pull --rebase --autostash & rebase.autostash=true' '
260         test_config rebase.autostash true &&
261         git reset --hard before-rebase &&
262         echo dirty >new_file &&
263         git add new_file &&
264         git pull --rebase --autostash . copy &&
265         test_cmp_rev HEAD^ copy &&
266         test "$(cat new_file)" = dirty &&
267         test "$(cat file)" = "modified again"
268 '
269
270 test_expect_success 'pull --rebase --autostash & rebase.autostash=false' '
271         test_config rebase.autostash false &&
272         git reset --hard before-rebase &&
273         echo dirty >new_file &&
274         git add new_file &&
275         git pull --rebase --autostash . copy &&
276         test_cmp_rev HEAD^ copy &&
277         test "$(cat new_file)" = dirty &&
278         test "$(cat file)" = "modified again"
279 '
280
281 test_expect_success 'pull --rebase: --autostash & rebase.autostash unset' '
282         git reset --hard before-rebase &&
283         echo dirty >new_file &&
284         git add new_file &&
285         git pull --rebase --autostash . copy &&
286         test_cmp_rev HEAD^ copy &&
287         test "$(cat new_file)" = dirty &&
288         test "$(cat file)" = "modified again"
289 '
290
291 test_expect_success 'pull --rebase --no-autostash & rebase.autostash=true' '
292         test_config rebase.autostash true &&
293         git reset --hard before-rebase &&
294         echo dirty >new_file &&
295         git add new_file &&
296         test_must_fail git pull --rebase --no-autostash . copy 2>err &&
297         test_i18ngrep "Cannot pull with rebase: Your index contains uncommitted changes." err
298 '
299
300 test_expect_success 'pull --rebase --no-autostash & rebase.autostash=false' '
301         test_config rebase.autostash false &&
302         git reset --hard before-rebase &&
303         echo dirty >new_file &&
304         git add new_file &&
305         test_must_fail git pull --rebase --no-autostash . copy 2>err &&
306         test_i18ngrep "Cannot pull with rebase: Your index contains uncommitted changes." err
307 '
308
309 test_expect_success 'pull --rebase --no-autostash & rebase.autostash unset' '
310         git reset --hard before-rebase &&
311         echo dirty >new_file &&
312         git add new_file &&
313         test_must_fail git pull --rebase --no-autostash . copy 2>err &&
314         test_i18ngrep "Cannot pull with rebase: Your index contains uncommitted changes." err
315 '
316
317 test_expect_success 'pull --autostash (without --rebase) should error out' '
318         test_must_fail git pull --autostash . copy 2>actual &&
319         echo "fatal: --[no-]autostash option is only valid with --rebase." >expect &&
320         test_i18ncmp actual expect
321 '
322
323 test_expect_success 'pull --no-autostash (without --rebase) should error out' '
324         test_must_fail git pull --no-autostash . copy 2>actual &&
325         echo "fatal: --[no-]autostash option is only valid with --rebase." >expect &&
326         test_i18ncmp actual expect
327 '
328
329 test_expect_success 'pull.rebase' '
330         git reset --hard before-rebase &&
331         test_config pull.rebase true &&
332         git pull . copy &&
333         test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
334         test new = "$(git show HEAD:file2)"
335 '
336
337 test_expect_success 'branch.to-rebase.rebase' '
338         git reset --hard before-rebase &&
339         test_config branch.to-rebase.rebase true &&
340         git pull . copy &&
341         test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
342         test new = "$(git show HEAD:file2)"
343 '
344
345 test_expect_success 'branch.to-rebase.rebase should override pull.rebase' '
346         git reset --hard before-rebase &&
347         test_config pull.rebase true &&
348         test_config branch.to-rebase.rebase false &&
349         git pull . copy &&
350         test "$(git rev-parse HEAD^)" != "$(git rev-parse copy)" &&
351         test new = "$(git show HEAD:file2)"
352 '
353
354 # add a feature branch, keep-merge, that is merged into master, so the
355 # test can try preserving the merge commit (or not) with various
356 # --rebase flags/pull.rebase settings.
357 test_expect_success 'preserve merge setup' '
358         git reset --hard before-rebase &&
359         git checkout -b keep-merge second^ &&
360         test_commit file3 &&
361         git checkout to-rebase &&
362         git merge keep-merge &&
363         git tag before-preserve-rebase
364 '
365
366 test_expect_success 'pull.rebase=false create a new merge commit' '
367         git reset --hard before-preserve-rebase &&
368         test_config pull.rebase false &&
369         git pull . copy &&
370         test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
371         test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
372         test file3 = "$(git show HEAD:file3.t)"
373 '
374
375 test_expect_success 'pull.rebase=true flattens keep-merge' '
376         git reset --hard before-preserve-rebase &&
377         test_config pull.rebase true &&
378         git pull . copy &&
379         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
380         test file3 = "$(git show HEAD:file3.t)"
381 '
382
383 test_expect_success 'pull.rebase=1 is treated as true and flattens keep-merge' '
384         git reset --hard before-preserve-rebase &&
385         test_config pull.rebase 1 &&
386         git pull . copy &&
387         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
388         test file3 = "$(git show HEAD:file3.t)"
389 '
390
391 test_expect_success 'pull.rebase=preserve rebases and merges keep-merge' '
392         git reset --hard before-preserve-rebase &&
393         test_config pull.rebase preserve &&
394         git pull . copy &&
395         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
396         test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
397 '
398
399 test_expect_success 'pull.rebase=interactive' '
400         write_script "$TRASH_DIRECTORY/fake-editor" <<-\EOF &&
401         echo I was here >fake.out &&
402         false
403         EOF
404         test_set_editor "$TRASH_DIRECTORY/fake-editor" &&
405         test_must_fail git pull --rebase=interactive . copy &&
406         test "I was here" = "$(cat fake.out)"
407 '
408
409 test_expect_success 'pull.rebase=invalid fails' '
410         git reset --hard before-preserve-rebase &&
411         test_config pull.rebase invalid &&
412         ! git pull . copy
413 '
414
415 test_expect_success '--rebase=false create a new merge commit' '
416         git reset --hard before-preserve-rebase &&
417         test_config pull.rebase true &&
418         git pull --rebase=false . copy &&
419         test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
420         test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
421         test file3 = "$(git show HEAD:file3.t)"
422 '
423
424 test_expect_success '--rebase=true rebases and flattens keep-merge' '
425         git reset --hard before-preserve-rebase &&
426         test_config pull.rebase preserve &&
427         git pull --rebase=true . copy &&
428         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
429         test file3 = "$(git show HEAD:file3.t)"
430 '
431
432 test_expect_success '--rebase=preserve rebases and merges keep-merge' '
433         git reset --hard before-preserve-rebase &&
434         test_config pull.rebase true &&
435         git pull --rebase=preserve . copy &&
436         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
437         test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
438 '
439
440 test_expect_success '--rebase=invalid fails' '
441         git reset --hard before-preserve-rebase &&
442         ! git pull --rebase=invalid . copy
443 '
444
445 test_expect_success '--rebase overrides pull.rebase=preserve and flattens keep-merge' '
446         git reset --hard before-preserve-rebase &&
447         test_config pull.rebase preserve &&
448         git pull --rebase . copy &&
449         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
450         test file3 = "$(git show HEAD:file3.t)"
451 '
452
453 test_expect_success '--rebase with rebased upstream' '
454
455         git remote add -f me . &&
456         git checkout copy &&
457         git tag copy-orig &&
458         git reset --hard HEAD^ &&
459         echo conflicting modification > file &&
460         git commit -m conflict file &&
461         git checkout to-rebase &&
462         echo file > file2 &&
463         git commit -m to-rebase file2 &&
464         git tag to-rebase-orig &&
465         git pull --rebase me copy &&
466         test "conflicting modification" = "$(cat file)" &&
467         test file = "$(cat file2)"
468
469 '
470
471 test_expect_success '--rebase -f with rebased upstream' '
472         test_when_finished "test_might_fail git rebase --abort" &&
473         git reset --hard to-rebase-orig &&
474         git pull --rebase -f me copy &&
475         test "conflicting modification" = "$(cat file)" &&
476         test file = "$(cat file2)"
477 '
478
479 test_expect_success '--rebase with rebased default upstream' '
480
481         git update-ref refs/remotes/me/copy copy-orig &&
482         git checkout --track -b to-rebase2 me/copy &&
483         git reset --hard to-rebase-orig &&
484         git pull --rebase &&
485         test "conflicting modification" = "$(cat file)" &&
486         test file = "$(cat file2)"
487
488 '
489
490 test_expect_success 'rebased upstream + fetch + pull --rebase' '
491
492         git update-ref refs/remotes/me/copy copy-orig &&
493         git reset --hard to-rebase-orig &&
494         git checkout --track -b to-rebase3 me/copy &&
495         git reset --hard to-rebase-orig &&
496         git fetch &&
497         git pull --rebase &&
498         test "conflicting modification" = "$(cat file)" &&
499         test file = "$(cat file2)"
500
501 '
502
503 test_expect_success 'pull --rebase dies early with dirty working directory' '
504
505         git checkout to-rebase &&
506         git update-ref refs/remotes/me/copy copy^ &&
507         COPY="$(git rev-parse --verify me/copy)" &&
508         git rebase --onto $COPY copy &&
509         test_config branch.to-rebase.remote me &&
510         test_config branch.to-rebase.merge refs/heads/copy &&
511         test_config branch.to-rebase.rebase true &&
512         echo dirty >> file &&
513         git add file &&
514         test_must_fail git pull &&
515         test "$COPY" = "$(git rev-parse --verify me/copy)" &&
516         git checkout HEAD -- file &&
517         git pull &&
518         test "$COPY" != "$(git rev-parse --verify me/copy)"
519
520 '
521
522 test_expect_success 'pull --rebase works on branch yet to be born' '
523         git rev-parse master >expect &&
524         mkdir empty_repo &&
525         (cd empty_repo &&
526          git init &&
527          git pull --rebase .. master &&
528          git rev-parse HEAD >../actual
529         ) &&
530         test_cmp expect actual
531 '
532
533 test_expect_success 'pull --rebase fails on unborn branch with staged changes' '
534         test_when_finished "rm -rf empty_repo2" &&
535         git init empty_repo2 &&
536         (
537                 cd empty_repo2 &&
538                 echo staged-file >staged-file &&
539                 git add staged-file &&
540                 test "$(git ls-files)" = staged-file &&
541                 test_must_fail git pull --rebase .. master 2>err &&
542                 test "$(git ls-files)" = staged-file &&
543                 test "$(git show :staged-file)" = staged-file &&
544                 test_i18ngrep "unborn branch with changes added to the index" err
545         )
546 '
547
548 test_expect_success 'setup for detecting upstreamed changes' '
549         mkdir src &&
550         (cd src &&
551          git init &&
552          printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
553          git add stuff &&
554          git commit -m "Initial revision"
555         ) &&
556         git clone src dst &&
557         (cd src &&
558          modify s/5/43/ stuff &&
559          git commit -a -m "5->43" &&
560          modify s/6/42/ stuff &&
561          git commit -a -m "Make it bigger"
562         ) &&
563         (cd dst &&
564          modify s/5/43/ stuff &&
565          git commit -a -m "Independent discovery of 5->43"
566         )
567 '
568
569 test_expect_success 'git pull --rebase detects upstreamed changes' '
570         (cd dst &&
571          git pull --rebase &&
572          test -z "$(git ls-files -u)"
573         )
574 '
575
576 test_expect_success 'setup for avoiding reapplying old patches' '
577         (cd dst &&
578          test_might_fail git rebase --abort &&
579          git reset --hard origin/master
580         ) &&
581         git clone --bare src src-replace.git &&
582         rm -rf src &&
583         mv src-replace.git src &&
584         (cd dst &&
585          modify s/2/22/ stuff &&
586          git commit -a -m "Change 2" &&
587          modify s/3/33/ stuff &&
588          git commit -a -m "Change 3" &&
589          modify s/4/44/ stuff &&
590          git commit -a -m "Change 4" &&
591          git push &&
592
593          modify s/44/55/ stuff &&
594          git commit --amend -a -m "Modified Change 4"
595         )
596 '
597
598 test_expect_success 'git pull --rebase does not reapply old patches' '
599         (cd dst &&
600          test_must_fail git pull --rebase &&
601          test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
602         )
603 '
604
605 test_expect_success 'git pull --rebase against local branch' '
606         git checkout -b copy2 to-rebase-orig &&
607         git pull --rebase . to-rebase &&
608         test "conflicting modification" = "$(cat file)" &&
609         test file = "$(cat file2)"
610 '
611
612 test_done