pull: fast-forward "pull --rebase=true"
[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 fast forward' '
241         git reset --hard before-rebase &&
242         git checkout -b ff &&
243         echo another modification >file &&
244         git commit -m third file &&
245
246         git checkout to-rebase &&
247         git pull --rebase . ff &&
248         test "$(git rev-parse HEAD)" = "$(git rev-parse ff)" &&
249
250         # The above only validates the result.  Did we actually bypass rebase?
251         git reflog -1 >reflog.actual &&
252         sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
253         echo "OBJID HEAD@{0}: pull --rebase . ff: Fast-forward" >reflog.expected &&
254         test_cmp reflog.expected reflog.fuzzy
255 '
256
257 test_expect_success '--rebase fails with multiple branches' '
258         git reset --hard before-rebase &&
259         test_must_fail git pull --rebase . copy master 2>err &&
260         test "$(git rev-parse HEAD)" = "$(git rev-parse before-rebase)" &&
261         test_i18ngrep "Cannot rebase onto multiple branches" err &&
262         test modified = "$(git show HEAD:file)"
263 '
264
265 test_expect_success 'pull --rebase succeeds with dirty working directory and rebase.autostash set' '
266         test_config rebase.autostash true &&
267         git reset --hard before-rebase &&
268         echo dirty >new_file &&
269         git add new_file &&
270         git pull --rebase . copy &&
271         test_cmp_rev HEAD^ copy &&
272         test "$(cat new_file)" = dirty &&
273         test "$(cat file)" = "modified again"
274 '
275
276 test_expect_success 'pull.rebase' '
277         git reset --hard before-rebase &&
278         test_config pull.rebase true &&
279         git pull . copy &&
280         test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
281         test new = "$(git show HEAD:file2)"
282 '
283
284 test_expect_success 'branch.to-rebase.rebase' '
285         git reset --hard before-rebase &&
286         test_config branch.to-rebase.rebase true &&
287         git pull . copy &&
288         test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
289         test new = "$(git show HEAD:file2)"
290 '
291
292 test_expect_success 'branch.to-rebase.rebase should override pull.rebase' '
293         git reset --hard before-rebase &&
294         test_config pull.rebase true &&
295         test_config branch.to-rebase.rebase false &&
296         git pull . copy &&
297         test "$(git rev-parse HEAD^)" != "$(git rev-parse copy)" &&
298         test new = "$(git show HEAD:file2)"
299 '
300
301 # add a feature branch, keep-merge, that is merged into master, so the
302 # test can try preserving the merge commit (or not) with various
303 # --rebase flags/pull.rebase settings.
304 test_expect_success 'preserve merge setup' '
305         git reset --hard before-rebase &&
306         git checkout -b keep-merge second^ &&
307         test_commit file3 &&
308         git checkout to-rebase &&
309         git merge keep-merge &&
310         git tag before-preserve-rebase
311 '
312
313 test_expect_success 'pull.rebase=false create a new merge commit' '
314         git reset --hard before-preserve-rebase &&
315         test_config pull.rebase false &&
316         git pull . copy &&
317         test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
318         test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
319         test file3 = "$(git show HEAD:file3.t)"
320 '
321
322 test_expect_success 'pull.rebase=true flattens keep-merge' '
323         git reset --hard before-preserve-rebase &&
324         test_config pull.rebase true &&
325         git pull . copy &&
326         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
327         test file3 = "$(git show HEAD:file3.t)"
328 '
329
330 test_expect_success 'pull.rebase=1 is treated as true and flattens keep-merge' '
331         git reset --hard before-preserve-rebase &&
332         test_config pull.rebase 1 &&
333         git pull . copy &&
334         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
335         test file3 = "$(git show HEAD:file3.t)"
336 '
337
338 test_expect_success 'pull.rebase=preserve rebases and merges keep-merge' '
339         git reset --hard before-preserve-rebase &&
340         test_config pull.rebase preserve &&
341         git pull . copy &&
342         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
343         test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
344 '
345
346 test_expect_success 'pull.rebase=invalid fails' '
347         git reset --hard before-preserve-rebase &&
348         test_config pull.rebase invalid &&
349         ! git pull . copy
350 '
351
352 test_expect_success '--rebase=false create a new merge commit' '
353         git reset --hard before-preserve-rebase &&
354         test_config pull.rebase true &&
355         git pull --rebase=false . copy &&
356         test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
357         test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
358         test file3 = "$(git show HEAD:file3.t)"
359 '
360
361 test_expect_success '--rebase=true rebases and flattens keep-merge' '
362         git reset --hard before-preserve-rebase &&
363         test_config pull.rebase preserve &&
364         git pull --rebase=true . copy &&
365         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
366         test file3 = "$(git show HEAD:file3.t)"
367 '
368
369 test_expect_success '--rebase=preserve rebases and merges keep-merge' '
370         git reset --hard before-preserve-rebase &&
371         test_config pull.rebase true &&
372         git pull --rebase=preserve . copy &&
373         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
374         test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
375 '
376
377 test_expect_success '--rebase=invalid fails' '
378         git reset --hard before-preserve-rebase &&
379         ! git pull --rebase=invalid . copy
380 '
381
382 test_expect_success '--rebase overrides pull.rebase=preserve and flattens keep-merge' '
383         git reset --hard before-preserve-rebase &&
384         test_config pull.rebase preserve &&
385         git pull --rebase . copy &&
386         test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
387         test file3 = "$(git show HEAD:file3.t)"
388 '
389
390 test_expect_success '--rebase with rebased upstream' '
391
392         git remote add -f me . &&
393         git checkout copy &&
394         git tag copy-orig &&
395         git reset --hard HEAD^ &&
396         echo conflicting modification > file &&
397         git commit -m conflict file &&
398         git checkout to-rebase &&
399         echo file > file2 &&
400         git commit -m to-rebase file2 &&
401         git tag to-rebase-orig &&
402         git pull --rebase me copy &&
403         test "conflicting modification" = "$(cat file)" &&
404         test file = "$(cat file2)"
405
406 '
407
408 test_expect_success '--rebase -f with rebased upstream' '
409         test_when_finished "test_might_fail git rebase --abort" &&
410         git reset --hard to-rebase-orig &&
411         git pull --rebase -f me copy &&
412         test "conflicting modification" = "$(cat file)" &&
413         test file = "$(cat file2)"
414 '
415
416 test_expect_success '--rebase with rebased default upstream' '
417
418         git update-ref refs/remotes/me/copy copy-orig &&
419         git checkout --track -b to-rebase2 me/copy &&
420         git reset --hard to-rebase-orig &&
421         git pull --rebase &&
422         test "conflicting modification" = "$(cat file)" &&
423         test file = "$(cat file2)"
424
425 '
426
427 test_expect_success 'rebased upstream + fetch + pull --rebase' '
428
429         git update-ref refs/remotes/me/copy copy-orig &&
430         git reset --hard to-rebase-orig &&
431         git checkout --track -b to-rebase3 me/copy &&
432         git reset --hard to-rebase-orig &&
433         git fetch &&
434         git pull --rebase &&
435         test "conflicting modification" = "$(cat file)" &&
436         test file = "$(cat file2)"
437
438 '
439
440 test_expect_success 'pull --rebase dies early with dirty working directory' '
441
442         git checkout to-rebase &&
443         git update-ref refs/remotes/me/copy copy^ &&
444         COPY="$(git rev-parse --verify me/copy)" &&
445         git rebase --onto $COPY copy &&
446         test_config branch.to-rebase.remote me &&
447         test_config branch.to-rebase.merge refs/heads/copy &&
448         test_config branch.to-rebase.rebase true &&
449         echo dirty >> file &&
450         git add file &&
451         test_must_fail git pull &&
452         test "$COPY" = "$(git rev-parse --verify me/copy)" &&
453         git checkout HEAD -- file &&
454         git pull &&
455         test "$COPY" != "$(git rev-parse --verify me/copy)"
456
457 '
458
459 test_expect_success 'pull --rebase works on branch yet to be born' '
460         git rev-parse master >expect &&
461         mkdir empty_repo &&
462         (cd empty_repo &&
463          git init &&
464          git pull --rebase .. master &&
465          git rev-parse HEAD >../actual
466         ) &&
467         test_cmp expect actual
468 '
469
470 test_expect_success 'pull --rebase fails on unborn branch with staged changes' '
471         test_when_finished "rm -rf empty_repo2" &&
472         git init empty_repo2 &&
473         (
474                 cd empty_repo2 &&
475                 echo staged-file >staged-file &&
476                 git add staged-file &&
477                 test "$(git ls-files)" = staged-file &&
478                 test_must_fail git pull --rebase .. master 2>err &&
479                 test "$(git ls-files)" = staged-file &&
480                 test "$(git show :staged-file)" = staged-file &&
481                 test_i18ngrep "unborn branch with changes added to the index" err
482         )
483 '
484
485 test_expect_success 'setup for detecting upstreamed changes' '
486         mkdir src &&
487         (cd src &&
488          git init &&
489          printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
490          git add stuff &&
491          git commit -m "Initial revision"
492         ) &&
493         git clone src dst &&
494         (cd src &&
495          modify s/5/43/ stuff &&
496          git commit -a -m "5->43" &&
497          modify s/6/42/ stuff &&
498          git commit -a -m "Make it bigger"
499         ) &&
500         (cd dst &&
501          modify s/5/43/ stuff &&
502          git commit -a -m "Independent discovery of 5->43"
503         )
504 '
505
506 test_expect_success 'git pull --rebase detects upstreamed changes' '
507         (cd dst &&
508          git pull --rebase &&
509          test -z "$(git ls-files -u)"
510         )
511 '
512
513 test_expect_success 'setup for avoiding reapplying old patches' '
514         (cd dst &&
515          test_might_fail git rebase --abort &&
516          git reset --hard origin/master
517         ) &&
518         git clone --bare src src-replace.git &&
519         rm -rf src &&
520         mv src-replace.git src &&
521         (cd dst &&
522          modify s/2/22/ stuff &&
523          git commit -a -m "Change 2" &&
524          modify s/3/33/ stuff &&
525          git commit -a -m "Change 3" &&
526          modify s/4/44/ stuff &&
527          git commit -a -m "Change 4" &&
528          git push &&
529
530          modify s/44/55/ stuff &&
531          git commit --amend -a -m "Modified Change 4"
532         )
533 '
534
535 test_expect_success 'git pull --rebase does not reapply old patches' '
536         (cd dst &&
537          test_must_fail git pull --rebase &&
538          test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
539         )
540 '
541
542 test_expect_success 'git pull --rebase against local branch' '
543         git checkout -b copy2 to-rebase-orig &&
544         git pull --rebase . to-rebase &&
545         test "conflicting modification" = "$(cat file)" &&
546         test file = "$(cat file2)"
547 '
548
549 test_done