Convert lookup_tree to struct object_id
[alt-git.git] / t / t5520-pull.sh
blob17f4d0fe4e7244cb58eaedcf41bcaa9c7b157bb4
1 #!/bin/sh
3 test_description='pulling into void'
5 . ./test-lib.sh
7 modify () {
8 sed -e "$1" <"$2" >"$2.x" &&
9 mv "$2.x" "$2"
12 test_pull_autostash () {
13 git reset --hard before-rebase &&
14 echo dirty >new_file &&
15 git add new_file &&
16 git pull "$@" . copy &&
17 test_cmp_rev HEAD^ copy &&
18 test "$(cat new_file)" = dirty &&
19 test "$(cat file)" = "modified again"
22 test_pull_autostash_fail () {
23 git reset --hard before-rebase &&
24 echo dirty >new_file &&
25 git add new_file &&
26 test_must_fail git pull "$@" . copy 2>err &&
27 test_i18ngrep "uncommitted changes." err
30 test_expect_success setup '
31 echo file >file &&
32 git add file &&
33 git commit -a -m original
36 test_expect_success 'pulling into void' '
37 git init cloned &&
39 cd cloned &&
40 git pull ..
41 ) &&
42 test -f file &&
43 test -f cloned/file &&
44 test_cmp file cloned/file
47 test_expect_success 'pulling into void using master:master' '
48 git init cloned-uho &&
50 cd cloned-uho &&
51 git pull .. master:master
52 ) &&
53 test -f file &&
54 test -f cloned-uho/file &&
55 test_cmp file cloned-uho/file
58 test_expect_success 'pulling into void does not overwrite untracked files' '
59 git init cloned-untracked &&
61 cd cloned-untracked &&
62 echo untracked >file &&
63 test_must_fail git pull .. master &&
64 echo untracked >expect &&
65 test_cmp expect file
69 test_expect_success 'pulling into void does not overwrite staged files' '
70 git init cloned-staged-colliding &&
72 cd cloned-staged-colliding &&
73 echo "alternate content" >file &&
74 git add file &&
75 test_must_fail git pull .. master &&
76 echo "alternate content" >expect &&
77 test_cmp expect file &&
78 git cat-file blob :file >file.index &&
79 test_cmp expect file.index
83 test_expect_success 'pulling into void does not remove new staged files' '
84 git init cloned-staged-new &&
86 cd cloned-staged-new &&
87 echo "new tracked file" >newfile &&
88 git add newfile &&
89 git pull .. master &&
90 echo "new tracked file" >expect &&
91 test_cmp expect newfile &&
92 git cat-file blob :newfile >newfile.index &&
93 test_cmp expect newfile.index
97 test_expect_success 'pulling into void must not create an octopus' '
98 git init cloned-octopus &&
100 cd cloned-octopus &&
101 test_must_fail git pull .. master master &&
102 ! test -f file
106 test_expect_success 'test . as a remote' '
107 git branch copy master &&
108 git config branch.copy.remote . &&
109 git config branch.copy.merge refs/heads/master &&
110 echo updated >file &&
111 git commit -a -m updated &&
112 git checkout copy &&
113 test "$(cat file)" = file &&
114 git pull &&
115 test "$(cat file)" = updated &&
116 git reflog -1 >reflog.actual &&
117 sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
118 echo "OBJID HEAD@{0}: pull: Fast-forward" >reflog.expected &&
119 test_cmp reflog.expected reflog.fuzzy
122 test_expect_success 'the default remote . should not break explicit pull' '
123 git checkout -b second master^ &&
124 echo modified >file &&
125 git commit -a -m modified &&
126 git checkout copy &&
127 git reset --hard HEAD^ &&
128 test "$(cat file)" = file &&
129 git pull . second &&
130 test "$(cat file)" = modified &&
131 git reflog -1 >reflog.actual &&
132 sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
133 echo "OBJID HEAD@{0}: pull . second: Fast-forward" >reflog.expected &&
134 test_cmp reflog.expected reflog.fuzzy
137 test_expect_success 'fail if wildcard spec does not match any refs' '
138 git checkout -b test copy^ &&
139 test_when_finished "git checkout -f copy && git branch -D test" &&
140 test "$(cat file)" = file &&
141 test_must_fail git pull . "refs/nonexisting1/*:refs/nonexisting2/*" 2>err &&
142 test_i18ngrep "no candidates for merging" err &&
143 test "$(cat file)" = file
146 test_expect_success 'fail if no branches specified with non-default remote' '
147 git remote add test_remote . &&
148 test_when_finished "git remote remove test_remote" &&
149 git checkout -b test copy^ &&
150 test_when_finished "git checkout -f copy && git branch -D test" &&
151 test "$(cat file)" = file &&
152 test_config branch.test.remote origin &&
153 test_must_fail git pull test_remote 2>err &&
154 test_i18ngrep "specify a branch on the command line" err &&
155 test "$(cat file)" = file
158 test_expect_success 'fail if not on a branch' '
159 git remote add origin . &&
160 test_when_finished "git remote remove origin" &&
161 git checkout HEAD^ &&
162 test_when_finished "git checkout -f copy" &&
163 test "$(cat file)" = file &&
164 test_must_fail git pull 2>err &&
165 test_i18ngrep "not currently on a branch" err &&
166 test "$(cat file)" = file
169 test_expect_success 'fail if no configuration for current branch' '
170 git remote add test_remote . &&
171 test_when_finished "git remote remove test_remote" &&
172 git checkout -b test copy^ &&
173 test_when_finished "git checkout -f copy && git branch -D test" &&
174 test_config branch.test.remote test_remote &&
175 test "$(cat file)" = file &&
176 test_must_fail git pull 2>err &&
177 test_i18ngrep "no tracking information" err &&
178 test "$(cat file)" = file
181 test_expect_success 'pull --all: fail if no configuration for current branch' '
182 git remote add test_remote . &&
183 test_when_finished "git remote remove test_remote" &&
184 git checkout -b test copy^ &&
185 test_when_finished "git checkout -f copy && git branch -D test" &&
186 test_config branch.test.remote test_remote &&
187 test "$(cat file)" = file &&
188 test_must_fail git pull --all 2>err &&
189 test_i18ngrep "There is no tracking information" err &&
190 test "$(cat file)" = file
193 test_expect_success 'fail if upstream branch does not exist' '
194 git checkout -b test copy^ &&
195 test_when_finished "git checkout -f copy && git branch -D test" &&
196 test_config branch.test.remote . &&
197 test_config branch.test.merge refs/heads/nonexisting &&
198 test "$(cat file)" = file &&
199 test_must_fail git pull 2>err &&
200 test_i18ngrep "no such ref was fetched" err &&
201 test "$(cat file)" = file
204 test_expect_success 'fail if the index has unresolved entries' '
205 git checkout -b third second^ &&
206 test_when_finished "git checkout -f copy && git branch -D third" &&
207 test "$(cat file)" = file &&
208 test_commit modified2 file &&
209 test -z "$(git ls-files -u)" &&
210 test_must_fail git pull . second &&
211 test -n "$(git ls-files -u)" &&
212 cp file expected &&
213 test_must_fail git pull . second 2>err &&
214 test_i18ngrep "Pulling is not possible because you have unmerged files." err &&
215 test_cmp expected file &&
216 git add file &&
217 test -z "$(git ls-files -u)" &&
218 test_must_fail git pull . second 2>err &&
219 test_i18ngrep "You have not concluded your merge" err &&
220 test_cmp expected file
223 test_expect_success 'fast-forwards working tree if branch head is updated' '
224 git checkout -b third second^ &&
225 test_when_finished "git checkout -f copy && git branch -D third" &&
226 test "$(cat file)" = file &&
227 git pull . second:third 2>err &&
228 test_i18ngrep "fetch updated the current branch head" err &&
229 test "$(cat file)" = modified &&
230 test "$(git rev-parse third)" = "$(git rev-parse second)"
233 test_expect_success 'fast-forward fails with conflicting work tree' '
234 git checkout -b third second^ &&
235 test_when_finished "git checkout -f copy && git branch -D third" &&
236 test "$(cat file)" = file &&
237 echo conflict >file &&
238 test_must_fail git pull . second:third 2>err &&
239 test_i18ngrep "Cannot fast-forward your working tree" err &&
240 test "$(cat file)" = conflict &&
241 test "$(git rev-parse third)" = "$(git rev-parse second)"
244 test_expect_success '--rebase' '
245 git branch to-rebase &&
246 echo modified again > file &&
247 git commit -m file file &&
248 git checkout to-rebase &&
249 echo new > file2 &&
250 git add file2 &&
251 git commit -m "new file" &&
252 git tag before-rebase &&
253 git pull --rebase . copy &&
254 test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
255 test new = "$(git show HEAD:file2)"
258 test_expect_success '--rebase fast forward' '
259 git reset --hard before-rebase &&
260 git checkout -b ff &&
261 echo another modification >file &&
262 git commit -m third file &&
264 git checkout to-rebase &&
265 git pull --rebase . ff &&
266 test "$(git rev-parse HEAD)" = "$(git rev-parse ff)" &&
268 # The above only validates the result. Did we actually bypass rebase?
269 git reflog -1 >reflog.actual &&
270 sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
271 echo "OBJID HEAD@{0}: pull --rebase . ff: Fast-forward" >reflog.expected &&
272 test_cmp reflog.expected reflog.fuzzy
275 test_expect_success '--rebase with conflicts shows advice' '
276 test_when_finished "git rebase --abort; git checkout -f to-rebase" &&
277 git checkout -b seq &&
278 test_seq 5 >seq.txt &&
279 git add seq.txt &&
280 test_tick &&
281 git commit -m "Add seq.txt" &&
282 echo 6 >>seq.txt &&
283 test_tick &&
284 git commit -m "Append to seq.txt" seq.txt &&
285 git checkout -b with-conflicts HEAD^ &&
286 echo conflicting >>seq.txt &&
287 test_tick &&
288 git commit -m "Create conflict" seq.txt &&
289 test_must_fail git pull --rebase . seq 2>err >out &&
290 test_i18ngrep "When you have resolved this problem" out
293 test_expect_success 'failed --rebase shows advice' '
294 test_when_finished "git rebase --abort; git checkout -f to-rebase" &&
295 git checkout -b diverging &&
296 test_commit attributes .gitattributes "* text=auto" attrs &&
297 sha1="$(printf "1\\r\\n" | git hash-object -w --stdin)" &&
298 git update-index --cacheinfo 0644 $sha1 file &&
299 git commit -m v1-with-cr &&
300 # force checkout because `git reset --hard` will not leave clean `file`
301 git checkout -f -b fails-to-rebase HEAD^ &&
302 test_commit v2-without-cr file "2" file2-lf &&
303 test_must_fail git pull --rebase . diverging 2>err >out &&
304 test_i18ngrep "When you have resolved this problem" out
307 test_expect_success '--rebase fails with multiple branches' '
308 git reset --hard before-rebase &&
309 test_must_fail git pull --rebase . copy master 2>err &&
310 test "$(git rev-parse HEAD)" = "$(git rev-parse before-rebase)" &&
311 test_i18ngrep "Cannot rebase onto multiple branches" err &&
312 test modified = "$(git show HEAD:file)"
315 test_expect_success 'pull --rebase succeeds with dirty working directory and rebase.autostash set' '
316 test_config rebase.autostash true &&
317 test_pull_autostash --rebase
320 test_expect_success 'pull --rebase --autostash & rebase.autostash=true' '
321 test_config rebase.autostash true &&
322 test_pull_autostash --rebase --autostash
325 test_expect_success 'pull --rebase --autostash & rebase.autostash=false' '
326 test_config rebase.autostash false &&
327 test_pull_autostash --rebase --autostash
330 test_expect_success 'pull --rebase --autostash & rebase.autostash unset' '
331 test_unconfig rebase.autostash &&
332 test_pull_autostash --rebase --autostash
335 test_expect_success 'pull --rebase --no-autostash & rebase.autostash=true' '
336 test_config rebase.autostash true &&
337 test_pull_autostash_fail --rebase --no-autostash
340 test_expect_success 'pull --rebase --no-autostash & rebase.autostash=false' '
341 test_config rebase.autostash false &&
342 test_pull_autostash_fail --rebase --no-autostash
345 test_expect_success 'pull --rebase --no-autostash & rebase.autostash unset' '
346 test_unconfig rebase.autostash &&
347 test_pull_autostash_fail --rebase --no-autostash
350 for i in --autostash --no-autostash
352 test_expect_success "pull $i (without --rebase) is illegal" '
353 test_must_fail git pull $i . copy 2>err &&
354 test_i18ngrep "only valid with --rebase" err
356 done
358 test_expect_success 'pull.rebase' '
359 git reset --hard before-rebase &&
360 test_config pull.rebase true &&
361 git pull . copy &&
362 test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
363 test new = "$(git show HEAD:file2)"
366 test_expect_success 'pull --autostash & pull.rebase=true' '
367 test_config pull.rebase true &&
368 test_pull_autostash --autostash
371 test_expect_success 'pull --no-autostash & pull.rebase=true' '
372 test_config pull.rebase true &&
373 test_pull_autostash_fail --no-autostash
376 test_expect_success 'branch.to-rebase.rebase' '
377 git reset --hard before-rebase &&
378 test_config branch.to-rebase.rebase true &&
379 git pull . copy &&
380 test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
381 test new = "$(git show HEAD:file2)"
384 test_expect_success 'branch.to-rebase.rebase should override pull.rebase' '
385 git reset --hard before-rebase &&
386 test_config pull.rebase true &&
387 test_config branch.to-rebase.rebase false &&
388 git pull . copy &&
389 test "$(git rev-parse HEAD^)" != "$(git rev-parse copy)" &&
390 test new = "$(git show HEAD:file2)"
393 test_expect_success "pull --rebase warns on --verify-signatures" '
394 git reset --hard before-rebase &&
395 git pull --rebase --verify-signatures . copy 2>err &&
396 test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
397 test new = "$(git show HEAD:file2)" &&
398 test_i18ngrep "ignoring --verify-signatures for rebase" err
401 test_expect_success "pull --rebase does not warn on --no-verify-signatures" '
402 git reset --hard before-rebase &&
403 git pull --rebase --no-verify-signatures . copy 2>err &&
404 test "$(git rev-parse HEAD^)" = "$(git rev-parse copy)" &&
405 test new = "$(git show HEAD:file2)" &&
406 test_i18ngrep ! "verify-signatures" err
409 # add a feature branch, keep-merge, that is merged into master, so the
410 # test can try preserving the merge commit (or not) with various
411 # --rebase flags/pull.rebase settings.
412 test_expect_success 'preserve merge setup' '
413 git reset --hard before-rebase &&
414 git checkout -b keep-merge second^ &&
415 test_commit file3 &&
416 git checkout to-rebase &&
417 git merge keep-merge &&
418 git tag before-preserve-rebase
421 test_expect_success 'pull.rebase=false create a new merge commit' '
422 git reset --hard before-preserve-rebase &&
423 test_config pull.rebase false &&
424 git pull . copy &&
425 test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
426 test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
427 test file3 = "$(git show HEAD:file3.t)"
430 test_expect_success 'pull.rebase=true flattens keep-merge' '
431 git reset --hard before-preserve-rebase &&
432 test_config pull.rebase true &&
433 git pull . copy &&
434 test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
435 test file3 = "$(git show HEAD:file3.t)"
438 test_expect_success 'pull.rebase=1 is treated as true and flattens keep-merge' '
439 git reset --hard before-preserve-rebase &&
440 test_config pull.rebase 1 &&
441 git pull . copy &&
442 test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
443 test file3 = "$(git show HEAD:file3.t)"
446 test_expect_success 'pull.rebase=preserve rebases and merges keep-merge' '
447 git reset --hard before-preserve-rebase &&
448 test_config pull.rebase preserve &&
449 git pull . copy &&
450 test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
451 test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
454 test_expect_success 'pull.rebase=interactive' '
455 write_script "$TRASH_DIRECTORY/fake-editor" <<-\EOF &&
456 echo I was here >fake.out &&
457 false
459 test_set_editor "$TRASH_DIRECTORY/fake-editor" &&
460 test_must_fail git pull --rebase=interactive . copy &&
461 test "I was here" = "$(cat fake.out)"
464 test_expect_success 'pull.rebase=invalid fails' '
465 git reset --hard before-preserve-rebase &&
466 test_config pull.rebase invalid &&
467 ! git pull . copy
470 test_expect_success '--rebase=false create a new merge commit' '
471 git reset --hard before-preserve-rebase &&
472 test_config pull.rebase true &&
473 git pull --rebase=false . copy &&
474 test "$(git rev-parse HEAD^1)" = "$(git rev-parse before-preserve-rebase)" &&
475 test "$(git rev-parse HEAD^2)" = "$(git rev-parse copy)" &&
476 test file3 = "$(git show HEAD:file3.t)"
479 test_expect_success '--rebase=true rebases and flattens keep-merge' '
480 git reset --hard before-preserve-rebase &&
481 test_config pull.rebase preserve &&
482 git pull --rebase=true . copy &&
483 test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
484 test file3 = "$(git show HEAD:file3.t)"
487 test_expect_success '--rebase=preserve rebases and merges keep-merge' '
488 git reset --hard before-preserve-rebase &&
489 test_config pull.rebase true &&
490 git pull --rebase=preserve . copy &&
491 test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
492 test "$(git rev-parse HEAD^2)" = "$(git rev-parse keep-merge)"
495 test_expect_success '--rebase=invalid fails' '
496 git reset --hard before-preserve-rebase &&
497 ! git pull --rebase=invalid . copy
500 test_expect_success '--rebase overrides pull.rebase=preserve and flattens keep-merge' '
501 git reset --hard before-preserve-rebase &&
502 test_config pull.rebase preserve &&
503 git pull --rebase . copy &&
504 test "$(git rev-parse HEAD^^)" = "$(git rev-parse copy)" &&
505 test file3 = "$(git show HEAD:file3.t)"
508 test_expect_success '--rebase with rebased upstream' '
510 git remote add -f me . &&
511 git checkout copy &&
512 git tag copy-orig &&
513 git reset --hard HEAD^ &&
514 echo conflicting modification > file &&
515 git commit -m conflict file &&
516 git checkout to-rebase &&
517 echo file > file2 &&
518 git commit -m to-rebase file2 &&
519 git tag to-rebase-orig &&
520 git pull --rebase me copy &&
521 test "conflicting modification" = "$(cat file)" &&
522 test file = "$(cat file2)"
526 test_expect_success '--rebase -f with rebased upstream' '
527 test_when_finished "test_might_fail git rebase --abort" &&
528 git reset --hard to-rebase-orig &&
529 git pull --rebase -f me copy &&
530 test "conflicting modification" = "$(cat file)" &&
531 test file = "$(cat file2)"
534 test_expect_success '--rebase with rebased default upstream' '
536 git update-ref refs/remotes/me/copy copy-orig &&
537 git checkout --track -b to-rebase2 me/copy &&
538 git reset --hard to-rebase-orig &&
539 git pull --rebase &&
540 test "conflicting modification" = "$(cat file)" &&
541 test file = "$(cat file2)"
545 test_expect_success 'rebased upstream + fetch + pull --rebase' '
547 git update-ref refs/remotes/me/copy copy-orig &&
548 git reset --hard to-rebase-orig &&
549 git checkout --track -b to-rebase3 me/copy &&
550 git reset --hard to-rebase-orig &&
551 git fetch &&
552 git pull --rebase &&
553 test "conflicting modification" = "$(cat file)" &&
554 test file = "$(cat file2)"
558 test_expect_success 'pull --rebase dies early with dirty working directory' '
560 git checkout to-rebase &&
561 git update-ref refs/remotes/me/copy copy^ &&
562 COPY="$(git rev-parse --verify me/copy)" &&
563 git rebase --onto $COPY copy &&
564 test_config branch.to-rebase.remote me &&
565 test_config branch.to-rebase.merge refs/heads/copy &&
566 test_config branch.to-rebase.rebase true &&
567 echo dirty >> file &&
568 git add file &&
569 test_must_fail git pull &&
570 test "$COPY" = "$(git rev-parse --verify me/copy)" &&
571 git checkout HEAD -- file &&
572 git pull &&
573 test "$COPY" != "$(git rev-parse --verify me/copy)"
577 test_expect_success 'pull --rebase works on branch yet to be born' '
578 git rev-parse master >expect &&
579 mkdir empty_repo &&
580 (cd empty_repo &&
581 git init &&
582 git pull --rebase .. master &&
583 git rev-parse HEAD >../actual
584 ) &&
585 test_cmp expect actual
588 test_expect_success 'pull --rebase fails on unborn branch with staged changes' '
589 test_when_finished "rm -rf empty_repo2" &&
590 git init empty_repo2 &&
592 cd empty_repo2 &&
593 echo staged-file >staged-file &&
594 git add staged-file &&
595 test "$(git ls-files)" = staged-file &&
596 test_must_fail git pull --rebase .. master 2>err &&
597 test "$(git ls-files)" = staged-file &&
598 test "$(git show :staged-file)" = staged-file &&
599 test_i18ngrep "unborn branch with changes added to the index" err
603 test_expect_success 'setup for detecting upstreamed changes' '
604 mkdir src &&
605 (cd src &&
606 git init &&
607 printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
608 git add stuff &&
609 git commit -m "Initial revision"
610 ) &&
611 git clone src dst &&
612 (cd src &&
613 modify s/5/43/ stuff &&
614 git commit -a -m "5->43" &&
615 modify s/6/42/ stuff &&
616 git commit -a -m "Make it bigger"
617 ) &&
618 (cd dst &&
619 modify s/5/43/ stuff &&
620 git commit -a -m "Independent discovery of 5->43"
624 test_expect_success 'git pull --rebase detects upstreamed changes' '
625 (cd dst &&
626 git pull --rebase &&
627 test -z "$(git ls-files -u)"
631 test_expect_success 'setup for avoiding reapplying old patches' '
632 (cd dst &&
633 test_might_fail git rebase --abort &&
634 git reset --hard origin/master
635 ) &&
636 git clone --bare src src-replace.git &&
637 rm -rf src &&
638 mv src-replace.git src &&
639 (cd dst &&
640 modify s/2/22/ stuff &&
641 git commit -a -m "Change 2" &&
642 modify s/3/33/ stuff &&
643 git commit -a -m "Change 3" &&
644 modify s/4/44/ stuff &&
645 git commit -a -m "Change 4" &&
646 git push &&
648 modify s/44/55/ stuff &&
649 git commit --amend -a -m "Modified Change 4"
653 test_expect_success 'git pull --rebase does not reapply old patches' '
654 (cd dst &&
655 test_must_fail git pull --rebase &&
656 test 1 = $(find .git/rebase-apply -name "000*" | wc -l)
660 test_expect_success 'git pull --rebase against local branch' '
661 git checkout -b copy2 to-rebase-orig &&
662 git pull --rebase . to-rebase &&
663 test "conflicting modification" = "$(cat file)" &&
664 test file = "$(cat file2)"
667 test_done