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