interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t5520-pull.sh

924 lines · 27476 bytes Raw Blame History
1#!/bin/sh
2
3test_description='pulling into void'
4
5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8. ./test-lib.sh
9
10modify () {
11 sed -e "$1" "$2" >"$2.x" &&
12 mv "$2.x" "$2"
13}
14
15test_pull_autostash () {
16 expect_parent_num="$1" &&
17 shift &&
18 git reset --hard before-rebase &&
19 echo dirty >new_file &&
20 git add new_file &&
21 git pull "$@" . copy &&
22 test_cmp_rev HEAD^"$expect_parent_num" copy &&
23 echo dirty >expect &&
24 test_cmp expect new_file &&
25 echo "modified again" >expect &&
26 test_cmp expect file
27}
28
29test_pull_autostash_fail () {
30 git reset --hard before-rebase &&
31 echo dirty >new_file &&
32 git add new_file &&
33 test_must_fail git pull "$@" . copy 2>err &&
34 test_grep -E "uncommitted changes.|overwritten by merge:" err
35}
36
37test_expect_success setup '
38 # Commit dates are hardcoded to 2005, and the reflog entries will have
39 # a matching timestamp. Maintenance may thus immediately expire
40 # reflogs if it was running.
41 git config set gc.reflogExpire never &&
42 git config set gc.reflogExpireUnreachable never &&
43
44 echo file >file &&
45 git add file &&
46 git commit -a -m original
47'
48
49test_expect_success 'pulling into void' '
50 git init cloned &&
51 (
52 cd cloned &&
53 git pull ..
54 ) &&
55 test_path_is_file file &&
56 test_path_is_file cloned/file &&
57 test_cmp file cloned/file
58'
59
60test_expect_success 'pulling into void using main:main' '
61 git init cloned-uho &&
62 (
63 cd cloned-uho &&
64 git pull .. main:main
65 ) &&
66 test_path_is_file file &&
67 test_path_is_file cloned-uho/file &&
68 test_cmp file cloned-uho/file
69'
70
71test_expect_success 'pulling into void does not overwrite untracked files' '
72 git init cloned-untracked &&
73 (
74 cd cloned-untracked &&
75 echo untracked >file &&
76 test_must_fail git pull .. main &&
77 echo untracked >expect &&
78 test_cmp expect file
79 )
80'
81
82test_expect_success 'pulling into void does not overwrite staged files' '
83 git init cloned-staged-colliding &&
84 (
85 cd cloned-staged-colliding &&
86 echo "alternate content" >file &&
87 git add file &&
88 test_must_fail git pull .. main &&
89 echo "alternate content" >expect &&
90 test_cmp expect file &&
91 git cat-file blob :file >file.index &&
92 test_cmp expect file.index
93 )
94'
95
96test_expect_success 'pulling into void does not remove new staged files' '
97 git init cloned-staged-new &&
98 (
99 cd cloned-staged-new &&
100 echo "new tracked file" >newfile &&
101 git add newfile &&
102 git pull .. main &&
103 echo "new tracked file" >expect &&
104 test_cmp expect newfile &&
105 git cat-file blob :newfile >newfile.index &&
106 test_cmp expect newfile.index
107 )
108'
109
110test_expect_success 'pulling into void must not create an octopus' '
111 git init cloned-octopus &&
112 (
113 cd cloned-octopus &&
114 test_must_fail git pull .. main main &&
115 test_path_is_missing file
116 )
117'
118
119test_expect_success 'test . as a remote' '
120 git branch copy main &&
121 git config branch.copy.remote . &&
122 git config branch.copy.merge refs/heads/main &&
123 echo updated >file &&
124 git commit -a -m updated &&
125 git checkout copy &&
126 echo file >expect &&
127 test_cmp expect file &&
128 git pull &&
129 echo updated >expect &&
130 test_cmp expect file &&
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: Fast-forward" >reflog.expected &&
134 test_cmp reflog.expected reflog.fuzzy
135'
136
137test_expect_success 'the default remote . should not break explicit pull' '
138 git checkout -b second main^ &&
139 echo modified >file &&
140 git commit -a -m modified &&
141 git checkout copy &&
142 git reset --hard HEAD^ &&
143 echo file >expect &&
144 test_cmp expect file &&
145 git pull --no-rebase . second &&
146 echo modified >expect &&
147 test_cmp expect file &&
148 git reflog -1 >reflog.actual &&
149 sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
150 echo "OBJID HEAD@{0}: pull --no-rebase . second: Fast-forward" >reflog.expected &&
151 test_cmp reflog.expected reflog.fuzzy
152'
153
154test_expect_success 'fail if wildcard spec does not match any refs' '
155 git checkout -b test copy^ &&
156 test_when_finished "git checkout -f copy && git branch -D test" &&
157 echo file >expect &&
158 test_cmp expect file &&
159 test_must_fail git pull . "refs/nonexisting1/*:refs/nonexisting2/*" 2>err &&
160 test_grep "no candidates for merging" err &&
161 test_cmp expect file
162'
163
164test_expect_success 'fail if no branches specified with non-default remote' '
165 git remote add test_remote . &&
166 test_when_finished "git remote remove test_remote" &&
167 git checkout -b test copy^ &&
168 test_when_finished "git checkout -f copy && git branch -D test" &&
169 echo file >expect &&
170 test_cmp expect file &&
171 test_config branch.test.remote origin &&
172 test_must_fail git pull test_remote 2>err &&
173 test_grep "specify a branch on the command line" err &&
174 test_cmp expect file
175'
176
177test_expect_success 'fail if not on a branch' '
178 git remote add origin . &&
179 test_when_finished "git remote remove origin" &&
180 git checkout HEAD^ &&
181 test_when_finished "git checkout -f copy" &&
182 echo file >expect &&
183 test_cmp expect file &&
184 test_must_fail git pull 2>err &&
185 test_grep "not currently on a branch" err &&
186 test_cmp expect file
187'
188
189test_expect_success 'fail if no configuration for current branch' '
190 git remote add test_remote . &&
191 test_when_finished "git remote remove test_remote" &&
192 git checkout -b test copy^ &&
193 test_when_finished "git checkout -f copy && git branch -D test" &&
194 test_config branch.test.remote test_remote &&
195 echo file >expect &&
196 test_cmp expect file &&
197 test_must_fail git pull 2>err &&
198 test_grep "no tracking information" err &&
199 test_cmp expect file
200'
201
202test_expect_success 'pull --all: fail if no configuration for current branch' '
203 git remote add test_remote . &&
204 test_when_finished "git remote remove test_remote" &&
205 git checkout -b test copy^ &&
206 test_when_finished "git checkout -f copy && git branch -D test" &&
207 test_config branch.test.remote test_remote &&
208 echo file >expect &&
209 test_cmp expect file &&
210 test_must_fail git pull --all 2>err &&
211 test_grep "There is no tracking information" err &&
212 test_cmp expect file
213'
214
215test_expect_success 'fail if upstream branch does not exist' '
216 git checkout -b test copy^ &&
217 test_when_finished "git checkout -f copy && git branch -D test" &&
218 test_config branch.test.remote . &&
219 test_config branch.test.merge refs/heads/nonexisting &&
220 echo file >expect &&
221 test_cmp expect file &&
222 test_must_fail git pull 2>err &&
223 test_grep "no such ref was fetched" err &&
224 test_cmp expect file
225'
226
227test_expect_success 'fetch upstream branch even if refspec excludes it' '
228 # the branch names are not important here except that
229 # the first one must not be a prefix of the second,
230 # since otherwise the ref-prefix protocol extension
231 # would match both
232 git branch in-refspec HEAD^ &&
233 git branch not-in-refspec HEAD &&
234 git init -b in-refspec downstream &&
235 git -C downstream remote add -t in-refspec origin "file://$(pwd)/.git" &&
236 git -C downstream config branch.in-refspec.remote origin &&
237 git -C downstream config branch.in-refspec.merge refs/heads/not-in-refspec &&
238 git -C downstream pull &&
239 git rev-parse --verify not-in-refspec >expect &&
240 git -C downstream rev-parse --verify HEAD >actual &&
241 test_cmp expect actual
242'
243
244test_expect_success 'fail if the index has unresolved entries' '
245 git checkout -b third second^ &&
246 test_when_finished "git checkout -f copy && git branch -D third" &&
247 echo file >expect &&
248 test_cmp expect file &&
249 test_commit modified2 file &&
250 git ls-files -u >unmerged &&
251 test_must_be_empty unmerged &&
252 test_must_fail git pull --no-rebase . second &&
253 git ls-files -u >unmerged &&
254 test_file_not_empty unmerged &&
255 cp file expected &&
256 test_must_fail git pull . second 2>err &&
257 test_grep "Pulling is not possible because you have unmerged files." err &&
258 test_cmp expected file &&
259 git add file &&
260 git ls-files -u >unmerged &&
261 test_must_be_empty unmerged &&
262 test_must_fail git pull . second 2>err &&
263 test_grep "You have not concluded your merge" err &&
264 test_cmp expected file
265'
266
267test_expect_success 'fast-forwards working tree if branch head is updated' '
268 git checkout -b third second^ &&
269 test_when_finished "git checkout -f copy && git branch -D third" &&
270 echo file >expect &&
271 test_cmp expect file &&
272 git pull . second:third 2>err &&
273 test_grep "fetch updated the current branch head" err &&
274 echo modified >expect &&
275 test_cmp expect file &&
276 test_cmp_rev third second
277'
278
279test_expect_success 'fast-forward fails with conflicting work tree' '
280 git checkout -b third second^ &&
281 test_when_finished "git checkout -f copy && git branch -D third" &&
282 echo file >expect &&
283 test_cmp expect file &&
284 echo conflict >file &&
285 test_must_fail git pull . second:third 2>err &&
286 test_grep "Cannot fast-forward your working tree" err &&
287 echo conflict >expect &&
288 test_cmp expect file &&
289 test_cmp_rev third second
290'
291
292test_expect_success '--rebase' '
293 git branch to-rebase &&
294 echo modified again >file &&
295 git commit -m file file &&
296 git checkout to-rebase &&
297 echo new >file2 &&
298 git add file2 &&
299 git commit -m "new file" &&
300 git tag before-rebase &&
301 git pull --rebase . copy &&
302 test_cmp_rev HEAD^ copy &&
303 echo new >expect &&
304 git show HEAD:file2 >actual &&
305 test_cmp expect actual
306'
307
308test_expect_success '--rebase (merge) fast forward' '
309 git reset --hard before-rebase &&
310 git checkout -b ff &&
311 echo another modification >file &&
312 git commit -m third file &&
313
314 git checkout to-rebase &&
315 git -c rebase.backend=merge pull --rebase . ff &&
316 test_cmp_rev HEAD ff &&
317
318 # The above only validates the result. Did we actually bypass rebase?
319 git reflog -1 >reflog.actual &&
320 sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
321 echo "OBJID HEAD@{0}: pull --rebase . ff: Fast-forward" >reflog.expected &&
322 test_cmp reflog.expected reflog.fuzzy
323'
324
325test_expect_success '--rebase (am) fast forward' '
326 git reset --hard before-rebase &&
327
328 git -c rebase.backend=apply pull --rebase . ff &&
329 test_cmp_rev HEAD ff &&
330
331 # The above only validates the result. Did we actually bypass rebase?
332 git reflog -1 >reflog.actual &&
333 sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
334 echo "OBJID HEAD@{0}: pull --rebase . ff: Fast-forward" >reflog.expected &&
335 test_cmp reflog.expected reflog.fuzzy
336'
337
338test_expect_success '--rebase --autostash fast forward' '
339 test_when_finished "
340 git reset --hard
341 git checkout to-rebase
342 git branch -D to-rebase-ff
343 git branch -D behind" &&
344 git branch behind &&
345 git checkout -b to-rebase-ff &&
346 echo another modification >>file &&
347 git add file &&
348 git commit -m mod &&
349
350 git checkout behind &&
351 echo dirty >file &&
352 git pull --rebase --autostash . to-rebase-ff &&
353 test_cmp_rev HEAD to-rebase-ff
354'
355
356test_expect_success '--rebase with rebase.autostash succeeds on ff' '
357 test_when_finished "rm -fr src dst actual" &&
358 git init src &&
359 test_commit -C src "initial" file "content" &&
360 git clone src dst &&
361 test_commit -C src --printf "more_content" file "more content\ncontent\n" &&
362 echo "dirty" >>dst/file &&
363 test_config -C dst rebase.autostash true &&
364 git -C dst pull --rebase >actual 2>&1 &&
365 test_grep -q "Fast-forward" actual &&
366 test_grep -q "Applied autostash." actual
367'
368
369test_expect_success '--rebase with conflicts shows advice' '
370 test_when_finished "git rebase --abort; git checkout -f to-rebase" &&
371 git checkout -b seq &&
372 test_seq 5 >seq.txt &&
373 git add seq.txt &&
374 test_tick &&
375 git commit -m "Add seq.txt" &&
376 echo 6 >>seq.txt &&
377 test_tick &&
378 git commit -m "Append to seq.txt" seq.txt &&
379 git checkout -b with-conflicts HEAD^ &&
380 echo conflicting >>seq.txt &&
381 test_tick &&
382 git commit -m "Create conflict" seq.txt &&
383 test_must_fail git pull --rebase . seq 2>err >out &&
384 test_grep "Resolve all conflicts manually" err
385'
386
387test_expect_success 'failed --rebase shows advice' '
388 test_when_finished "git rebase --abort; git checkout -f to-rebase" &&
389 git checkout -b diverging &&
390 test_commit attributes .gitattributes "* text=auto" attrs &&
391 sha1="$(printf "1\\r\\n" | git hash-object -w --stdin)" &&
392 git update-index --cacheinfo 0644 $sha1 file &&
393 git commit -m v1-with-cr &&
394 # force checkout because `git reset --hard` will not leave clean `file`
395 git checkout -f -b fails-to-rebase HEAD^ &&
396 test_commit v2-without-cr file "2" file2-lf &&
397 test_must_fail git pull --rebase . diverging 2>err >out &&
398 test_grep "Resolve all conflicts manually" err
399'
400
401test_expect_success '--rebase fails with multiple branches' '
402 git reset --hard before-rebase &&
403 test_must_fail git pull --rebase . copy main 2>err &&
404 test_cmp_rev HEAD before-rebase &&
405 test_grep "Cannot rebase onto multiple branches" err &&
406 echo modified >expect &&
407 git show HEAD:file >actual &&
408 test_cmp expect actual
409'
410
411test_expect_success 'pull --rebase succeeds with dirty working directory and rebase.autostash set' '
412 test_config rebase.autostash true &&
413 test_pull_autostash 1 --rebase
414'
415
416test_expect_success 'pull --rebase --autostash & rebase.autostash=true' '
417 test_config rebase.autostash true &&
418 test_pull_autostash 1 --rebase --autostash
419'
420
421test_expect_success 'pull --rebase --autostash & rebase.autostash=false' '
422 test_config rebase.autostash false &&
423 test_pull_autostash 1 --rebase --autostash
424'
425
426test_expect_success 'pull --rebase --autostash & rebase.autostash unset' '
427 test_unconfig rebase.autostash &&
428 test_pull_autostash 1 --rebase --autostash
429'
430
431test_expect_success 'pull --rebase --no-autostash & rebase.autostash=true' '
432 test_config rebase.autostash true &&
433 test_pull_autostash_fail --rebase --no-autostash
434'
435
436test_expect_success 'pull --rebase --no-autostash & rebase.autostash=false' '
437 test_config rebase.autostash false &&
438 test_pull_autostash_fail --rebase --no-autostash
439'
440
441test_expect_success 'pull --rebase --no-autostash & rebase.autostash unset' '
442 test_unconfig rebase.autostash &&
443 test_pull_autostash_fail --rebase --no-autostash
444'
445
446test_expect_success 'pull succeeds with dirty working directory and merge.autostash set' '
447 test_config merge.autostash true &&
448 test_pull_autostash 2 --no-rebase
449'
450
451test_expect_success 'pull --autostash & merge.autostash=true' '
452 test_config merge.autostash true &&
453 test_pull_autostash 2 --autostash --no-rebase
454'
455
456test_expect_success 'pull --autostash & merge.autostash=false' '
457 test_config merge.autostash false &&
458 test_pull_autostash 2 --autostash --no-rebase
459'
460
461test_expect_success 'pull --autostash & merge.autostash unset' '
462 test_unconfig merge.autostash &&
463 test_pull_autostash 2 --autostash --no-rebase
464'
465
466test_expect_success 'pull --no-autostash & merge.autostash=true' '
467 test_config merge.autostash true &&
468 test_pull_autostash_fail --no-autostash --no-rebase
469'
470
471test_expect_success 'pull --no-autostash & merge.autostash=false' '
472 test_config merge.autostash false &&
473 test_pull_autostash_fail --no-autostash --no-rebase
474'
475
476test_expect_success 'pull --no-autostash & merge.autostash unset' '
477 test_unconfig merge.autostash &&
478 test_pull_autostash_fail --no-autostash --no-rebase
479'
480
481test_expect_success 'pull succeeds with dirty working directory and pull.autostash=true' '
482 test_config pull.autostash true &&
483 test_pull_autostash 1 --rebase &&
484 test_pull_autostash 2 --no-rebase &&
485 test_pull_autostash 1 --autostash --rebase &&
486 test_pull_autostash 2 --autostash --no-rebase
487'
488
489test_expect_success 'pull fails with dirty working directory and pull.autostash=false' '
490 test_config pull.autostash false &&
491 test_pull_autostash_fail --rebase &&
492 test_pull_autostash_fail --no-rebase &&
493 test_pull_autostash_fail --no-autostash --rebase &&
494 test_pull_autostash_fail --no-autostash --no-rebase
495'
496
497test_expect_success 'pull --autostash overrides pull.autostash=false' '
498 test_config pull.autostash false &&
499 test_pull_autostash 1 --autostash --rebase &&
500 test_pull_autostash 2 --autostash --no-rebase
501'
502
503test_expect_success 'pull --no-autostash overrides pull.autostash=true' '
504 test_config pull.autostash true &&
505 test_pull_autostash_fail --no-autostash --rebase &&
506 test_pull_autostash_fail --no-autostash --no-rebase
507'
508
509test_expect_success 'pull.autostash=true overrides rebase.autostash' '
510 test_config pull.autostash true &&
511 test_config rebase.autostash true &&
512 test_pull_autostash 1 --rebase &&
513 test_config rebase.autostash false &&
514 test_pull_autostash 1 --rebase
515'
516
517test_expect_success 'pull.autostash=false overrides rebase.autostash' '
518 test_config pull.autostash false &&
519 test_config rebase.autostash true &&
520 test_pull_autostash_fail --rebase &&
521 test_config rebase.autostash false &&
522 test_pull_autostash_fail --rebase
523'
524
525test_expect_success 'pull.autostash=true overrides merge.autostash' '
526 test_config pull.autostash true &&
527 test_config merge.autostash true &&
528 test_pull_autostash 2 --no-rebase &&
529 test_config merge.autostash false &&
530 test_pull_autostash 2 --no-rebase
531'
532
533test_expect_success 'pull.autostash=false overrides merge.autostash' '
534 test_config pull.autostash false &&
535 test_config merge.autostash true &&
536 test_pull_autostash_fail --no-rebase &&
537 test_config merge.autostash false &&
538 test_pull_autostash_fail --no-rebase
539'
540
541test_expect_success 'pull.rebase' '
542 git reset --hard before-rebase &&
543 test_config pull.rebase true &&
544 git pull . copy &&
545 test_cmp_rev HEAD^ copy &&
546 echo new >expect &&
547 git show HEAD:file2 >actual &&
548 test_cmp expect actual
549'
550
551test_expect_success 'pull --autostash & pull.rebase=true' '
552 test_config pull.rebase true &&
553 test_pull_autostash 1 --autostash
554'
555
556test_expect_success 'pull --no-autostash & pull.rebase=true' '
557 test_config pull.rebase true &&
558 test_pull_autostash_fail --no-autostash
559'
560
561test_expect_success 'branch.to-rebase.rebase' '
562 git reset --hard before-rebase &&
563 test_config branch.to-rebase.rebase true &&
564 git pull . copy &&
565 test_cmp_rev HEAD^ copy &&
566 echo new >expect &&
567 git show HEAD:file2 >actual &&
568 test_cmp expect actual
569'
570
571test_expect_success 'branch.to-rebase.rebase should override pull.rebase' '
572 git reset --hard before-rebase &&
573 test_config pull.rebase true &&
574 test_config branch.to-rebase.rebase false &&
575 git pull . copy &&
576 test_cmp_rev ! HEAD^ copy &&
577 echo new >expect &&
578 git show HEAD:file2 >actual &&
579 test_cmp expect actual
580'
581
582test_expect_success 'pull --rebase warns on --verify-signatures' '
583 git reset --hard before-rebase &&
584 git pull --rebase --verify-signatures . copy 2>err &&
585 test_cmp_rev HEAD^ copy &&
586 echo new >expect &&
587 git show HEAD:file2 >actual &&
588 test_cmp expect actual &&
589 test_grep "ignoring --verify-signatures for rebase" err
590'
591
592test_expect_success 'pull --rebase does not warn on --no-verify-signatures' '
593 git reset --hard before-rebase &&
594 git pull --rebase --no-verify-signatures . copy 2>err &&
595 test_cmp_rev HEAD^ copy &&
596 echo new >expect &&
597 git show HEAD:file2 >actual &&
598 test_cmp expect actual &&
599 test_grep ! "verify-signatures" err
600'
601
602# add a feature branch, keep-merge, that is merged into main, so the
603# test can try preserving the merge commit (or not) with various
604# --rebase flags/pull.rebase settings.
605test_expect_success 'preserve merge setup' '
606 git reset --hard before-rebase &&
607 git checkout -b keep-merge second^ &&
608 test_commit file3 &&
609 git checkout to-rebase &&
610 git merge keep-merge &&
611 git tag before-preserve-rebase
612'
613
614test_expect_success 'pull.rebase=false create a new merge commit' '
615 git reset --hard before-preserve-rebase &&
616 test_config pull.rebase false &&
617 git pull . copy &&
618 test_cmp_rev HEAD^1 before-preserve-rebase &&
619 test_cmp_rev HEAD^2 copy &&
620 echo file3 >expect &&
621 git show HEAD:file3.t >actual &&
622 test_cmp expect actual
623'
624
625test_expect_success 'pull.rebase=true flattens keep-merge' '
626 git reset --hard before-preserve-rebase &&
627 test_config pull.rebase true &&
628 git pull . copy &&
629 test_cmp_rev HEAD^^ copy &&
630 echo file3 >expect &&
631 git show HEAD:file3.t >actual &&
632 test_cmp expect actual
633'
634
635test_expect_success 'pull.rebase=1 is treated as true and flattens keep-merge' '
636 git reset --hard before-preserve-rebase &&
637 test_config pull.rebase 1 &&
638 git pull . copy &&
639 test_cmp_rev HEAD^^ copy &&
640 echo file3 >expect &&
641 git show HEAD:file3.t >actual &&
642 test_cmp expect actual
643'
644
645test_expect_success 'pull.rebase=interactive' '
646 write_script "$TRASH_DIRECTORY/fake-editor" <<-\EOF &&
647 echo I was here >fake.out &&
648 false
649 EOF
650 test_set_editor "$TRASH_DIRECTORY/fake-editor" &&
651 test_when_finished "test_might_fail git rebase --abort" &&
652 test_must_fail git pull --rebase=interactive . copy &&
653 echo "I was here" >expect &&
654 test_cmp expect fake.out
655'
656
657test_expect_success 'pull --rebase=i' '
658 write_script "$TRASH_DIRECTORY/fake-editor" <<-\EOF &&
659 echo I was here, too >fake.out &&
660 false
661 EOF
662 test_set_editor "$TRASH_DIRECTORY/fake-editor" &&
663 test_when_finished "test_might_fail git rebase --abort" &&
664 test_must_fail git pull --rebase=i . copy &&
665 echo "I was here, too" >expect &&
666 test_cmp expect fake.out
667'
668
669test_expect_success 'pull.rebase=invalid fails' '
670 git reset --hard before-preserve-rebase &&
671 test_config pull.rebase invalid &&
672 test_must_fail git pull . copy
673'
674
675test_expect_success '--rebase=false create a new merge commit' '
676 git reset --hard before-preserve-rebase &&
677 test_config pull.rebase true &&
678 git pull --rebase=false . copy &&
679 test_cmp_rev HEAD^1 before-preserve-rebase &&
680 test_cmp_rev HEAD^2 copy &&
681 echo file3 >expect &&
682 git show HEAD:file3.t >actual &&
683 test_cmp expect actual
684'
685
686test_expect_success '--rebase=true rebases and flattens keep-merge' '
687 git reset --hard before-preserve-rebase &&
688 test_config pull.rebase merges &&
689 git pull --rebase=true . copy &&
690 test_cmp_rev HEAD^^ copy &&
691 echo file3 >expect &&
692 git show HEAD:file3.t >actual &&
693 test_cmp expect actual
694'
695
696test_expect_success '--rebase=invalid fails' '
697 git reset --hard before-preserve-rebase &&
698 test_must_fail git pull --rebase=invalid . copy
699'
700
701test_expect_success '--rebase overrides pull.rebase=merges and flattens keep-merge' '
702 git reset --hard before-preserve-rebase &&
703 test_config pull.rebase merges &&
704 git pull --rebase . copy &&
705 test_cmp_rev HEAD^^ copy &&
706 echo file3 >expect &&
707 git show HEAD:file3.t >actual &&
708 test_cmp expect actual
709'
710
711test_expect_success '--rebase with rebased upstream' '
712 git remote add -f me . &&
713 git checkout copy &&
714 git tag copy-orig &&
715 git reset --hard HEAD^ &&
716 echo conflicting modification >file &&
717 git commit -m conflict file &&
718 git checkout to-rebase &&
719 echo file >file2 &&
720 git commit -m to-rebase file2 &&
721 git tag to-rebase-orig &&
722 git pull --rebase me copy &&
723 echo "conflicting modification" >expect &&
724 test_cmp expect file &&
725 echo file >expect &&
726 test_cmp expect file2
727'
728
729test_expect_success '--rebase -f with rebased upstream' '
730 test_when_finished "test_might_fail git rebase --abort" &&
731 git reset --hard to-rebase-orig &&
732 git pull --rebase -f me copy &&
733 echo "conflicting modification" >expect &&
734 test_cmp expect file &&
735 echo file >expect &&
736 test_cmp expect file2
737'
738
739test_expect_success '--rebase with rebased default upstream' '
740 git update-ref refs/remotes/me/copy copy-orig &&
741 git checkout --track -b to-rebase2 me/copy &&
742 git reset --hard to-rebase-orig &&
743 git pull --rebase &&
744 echo "conflicting modification" >expect &&
745 test_cmp expect file &&
746 echo file >expect &&
747 test_cmp expect file2
748'
749
750test_expect_success 'rebased upstream + fetch + pull --rebase' '
751
752 git update-ref refs/remotes/me/copy copy-orig &&
753 git reset --hard to-rebase-orig &&
754 git checkout --track -b to-rebase3 me/copy &&
755 git reset --hard to-rebase-orig &&
756 git fetch &&
757 git pull --rebase &&
758 echo "conflicting modification" >expect &&
759 test_cmp expect file &&
760 echo file >expect &&
761 test_cmp expect file2
762
763'
764
765test_expect_success 'pull --rebase dies early with dirty working directory' '
766 git checkout to-rebase &&
767 git update-ref refs/remotes/me/copy copy^ &&
768 COPY="$(git rev-parse --verify me/copy)" &&
769 git rebase --onto $COPY copy &&
770 test_config branch.to-rebase.remote me &&
771 test_config branch.to-rebase.merge refs/heads/copy &&
772 test_config branch.to-rebase.rebase true &&
773 echo dirty >>file &&
774 git add file &&
775 test_must_fail git pull &&
776 test_cmp_rev "$COPY" me/copy &&
777 git checkout HEAD -- file &&
778 git pull &&
779 test_cmp_rev ! "$COPY" me/copy
780'
781
782test_expect_success 'pull --rebase works on branch yet to be born' '
783 git rev-parse main >expect &&
784 mkdir empty_repo &&
785 (
786 cd empty_repo &&
787 git init &&
788 git pull --rebase .. main &&
789 git rev-parse HEAD >../actual
790 ) &&
791 test_cmp expect actual
792'
793
794test_expect_success 'pull --rebase fails on unborn branch with staged changes' '
795 test_when_finished "rm -rf empty_repo2" &&
796 git init empty_repo2 &&
797 (
798 cd empty_repo2 &&
799 echo staged-file >staged-file &&
800 git add staged-file &&
801 echo staged-file >expect &&
802 git ls-files >actual &&
803 test_cmp expect actual &&
804 test_must_fail git pull --rebase .. main 2>err &&
805 git ls-files >actual &&
806 test_cmp expect actual &&
807 git show :staged-file >actual &&
808 test_cmp expect actual &&
809 test_grep "unborn branch with changes added to the index" err
810 )
811'
812
813test_expect_success 'pull --rebase fails on corrupt HEAD' '
814 test_when_finished "rm -rf corrupt" &&
815 git init corrupt &&
816 (
817 cd corrupt &&
818 test_commit one &&
819 git rev-parse --verify HEAD >head &&
820 obj=$(sed "s#^..#&/#" head) &&
821 rm -f .git/objects/$obj &&
822 test_must_fail git pull --rebase
823 )
824'
825
826test_expect_success 'setup for detecting upstreamed changes' '
827 test_create_repo src &&
828 test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
829 git clone src dst &&
830 (
831 cd src &&
832 modify s/5/43/ stuff &&
833 git commit -a -m "5->43" &&
834 modify s/6/42/ stuff &&
835 git commit -a -m "Make it bigger"
836 ) &&
837 (
838 cd dst &&
839 modify s/5/43/ stuff &&
840 git commit -a -m "Independent discovery of 5->43"
841 )
842'
843
844test_expect_success 'git pull --rebase detects upstreamed changes' '
845 (
846 cd dst &&
847 git pull --rebase &&
848 git ls-files -u >untracked &&
849 test_must_be_empty untracked
850 )
851'
852
853test_expect_success 'setup for avoiding reapplying old patches' '
854 (
855 cd dst &&
856 test_might_fail git rebase --abort &&
857 git reset --hard origin/main
858 ) &&
859 git clone --bare src src-replace.git &&
860 rm -rf src &&
861 mv src-replace.git src &&
862 (
863 cd dst &&
864 modify s/2/22/ stuff &&
865 git commit -a -m "Change 2" &&
866 modify s/3/33/ stuff &&
867 git commit -a -m "Change 3" &&
868 modify s/4/44/ stuff &&
869 git commit -a -m "Change 4" &&
870 git push &&
871
872 modify s/44/55/ stuff &&
873 git commit --amend -a -m "Modified Change 4"
874 )
875'
876
877test_expect_success 'git pull --rebase does not reapply old patches' '
878 (
879 cd dst &&
880 test_must_fail git pull --rebase &&
881 cat .git/rebase-merge/done .git/rebase-merge/git-rebase-todo >work &&
882 grep -v -e ^\# -e ^$ work >patches &&
883 test_line_count = 1 patches &&
884 rm -f work
885 )
886'
887
888test_expect_success 'git pull --rebase against local branch' '
889 git checkout -b copy2 to-rebase-orig &&
890 git pull --rebase . to-rebase &&
891 echo "conflicting modification" >expect &&
892 test_cmp expect file &&
893 echo file >expect &&
894 test_cmp expect file2
895'
896
897test_expect_success 'pull does not crash when a merge head does not resolve' '
898 test_when_finished "rm -rf up dn" &&
899 git init up &&
900 (
901 cd up &&
902 test_commit base &&
903 git switch -c sideA &&
904 test_commit a &&
905 git switch -c sideB base &&
906 test_commit b
907 ) &&
908 git clone up dn &&
909 (
910 cd dn &&
911 git commit-graph write --reachable &&
912 oid=$(git rev-parse refs/remotes/origin/sideA) &&
913 obj=.git/objects/$(test_oid_to_path "$oid") &&
914
915 # Corrupt the object instead of removing it: a missing
916 # object that is still in the commit-graph is caught by
917 # fetch before pull ever reaches the fast-forward check.
918 rm -f "$obj" &&
919 echo garbage >"$obj" &&
920 test_must_fail git pull --no-rebase origin sideA sideB
921 )
922'
923
924test_done