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 / t7600-merge.sh

1228 lines · 32131 bytes Raw Blame History
1#!/bin/sh
2#
3# Copyright (c) 2007 Lars Hjemli
4#
5
6test_description='git merge
7
8Testing basic merge operations/option parsing.
9
10! [c0] commit 0
11 ! [c1] commit 1
12 ! [c2] commit 2
13 ! [c3] commit 3
14 ! [c4] c4
15 ! [c5] c5
16 ! [c6] c6
17 * [main] Merge commit 'c1'
18--------
19 - [main] Merge commit 'c1'
20 + * [c1] commit 1
21 + [c6] c6
22 + [c5] c5
23 ++ [c4] c4
24 ++++ [c3] commit 3
25 + [c2] commit 2
26+++++++* [c0] commit 0
27'
28
29GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
30export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
31
32. ./test-lib.sh
33. "$TEST_DIRECTORY"/lib-gpg.sh
34
35test_write_lines 1 2 3 4 5 6 7 8 9 >file
36cp file file.orig
37test_write_lines '1 X' 2 3 4 5 6 7 8 9 >file.1
38test_write_lines 1 2 '3 X' 4 5 6 7 8 9 >file.3
39test_write_lines 1 2 3 4 '5 X' 6 7 8 9 >file.5
40test_write_lines 1 2 3 4 5 6 7 8 '9 X' >file.9
41test_write_lines 1 2 3 4 5 6 7 8 '9 Y' >file.9y
42test_write_lines '1 X' 2 3 4 5 6 7 8 9 >result.1
43test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
44test_write_lines '1 X' 2 3 4 5 6 7 8 '9 X' >result.1-9
45test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
46test_write_lines '1 X' 2 '3 X' 4 '5 X' 6 7 8 '9 X' >result.1-3-5-9
47test_write_lines 1 2 3 4 5 6 7 8 '9 Z' >result.9z
48
49create_merge_msgs () {
50 echo "Merge tag 'c2'" >msg.1-5 &&
51 echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
52 {
53 echo "Squashed commit of the following:" &&
54 echo &&
55 git log --no-merges ^HEAD c1
56 } >squash.1 &&
57 {
58 echo "Squashed commit of the following:" &&
59 echo &&
60 git log --no-merges ^HEAD c2
61 } >squash.1-5 &&
62 {
63 echo "Squashed commit of the following:" &&
64 echo &&
65 git log --no-merges ^HEAD c2 c3
66 } >squash.1-5-9 &&
67 {
68 echo "* tag 'c3':" &&
69 echo " commit 3"
70 } >msg.log
71}
72
73verify_merge () {
74 test_cmp "$2" "$1" &&
75 git update-index --refresh &&
76 git diff --exit-code &&
77 if test -n "$3"
78 then
79 git show -s --pretty=tformat:%s HEAD >msg.act &&
80 test_cmp "$3" msg.act
81 fi
82}
83
84verify_head () {
85 echo "$1" >head.expected &&
86 git rev-parse HEAD >head.actual &&
87 test_cmp head.expected head.actual
88}
89
90verify_parents () {
91 test_write_lines "$@" >parents.expected &&
92 >parents.actual &&
93 i=1 &&
94 while test $i -le $#
95 do
96 git rev-parse HEAD^$i >>parents.actual &&
97 i=$(expr $i + 1) ||
98 return 1
99 done &&
100 test_must_fail git rev-parse --verify "HEAD^$i" &&
101 test_cmp parents.expected parents.actual
102}
103
104verify_mergeheads () {
105 test_write_lines "$@" >mergehead.expected &&
106 while read sha1 rest
107 do
108 git rev-parse $sha1 || return 1
109 done <.git/MERGE_HEAD >mergehead.actual &&
110 test_cmp mergehead.expected mergehead.actual
111}
112
113verify_no_mergehead () {
114 ! test -e .git/MERGE_HEAD
115}
116
117test_expect_success 'setup' '
118 git add file &&
119 test_tick &&
120 git commit -m "commit 0" &&
121 git tag c0 &&
122 c0=$(git rev-parse HEAD) &&
123 cp file.1 file &&
124 git add file &&
125 cp file.1 other &&
126 git add other &&
127 test_tick &&
128 git commit -m "commit 1" &&
129 git tag c1 &&
130 c1=$(git rev-parse HEAD) &&
131 git reset --hard "$c0" &&
132 cp file.5 file &&
133 git add file &&
134 test_tick &&
135 git commit -m "commit 2" &&
136 git tag c2 &&
137 c2=$(git rev-parse HEAD) &&
138 git reset --hard "$c0" &&
139 cp file.9y file &&
140 git add file &&
141 test_tick &&
142 git commit -m "commit 7" &&
143 git tag c7 &&
144 git reset --hard "$c0" &&
145 cp file.9 file &&
146 git add file &&
147 test_tick &&
148 git commit -m "commit 3" &&
149 git tag c3 &&
150 c3=$(git rev-parse HEAD) &&
151 git reset --hard "$c0" &&
152 create_merge_msgs
153'
154
155test_debug 'git log --graph --decorate --oneline --all'
156
157test_expect_success 'test option parsing' '
158 test_must_fail git merge -$ c1 &&
159 test_must_fail git merge --no-such c1 &&
160 test_must_fail git merge -s foobar c1 &&
161 test_must_fail git merge -s=foobar c1 &&
162 test_must_fail git merge -m &&
163 test_must_fail git merge --abort foobar &&
164 test_must_fail git merge --abort --quiet &&
165 test_must_fail git merge --continue foobar &&
166 test_must_fail git merge --continue --quiet &&
167 test_must_fail git merge
168'
169
170test_expect_success 'merge -h with invalid index' '
171 mkdir broken &&
172 (
173 cd broken &&
174 git init &&
175 >.git/index &&
176 git merge -h >usage
177 ) &&
178 test_grep "[Uu]sage: git merge" broken/usage
179'
180
181test_expect_success 'reject non-strategy with a git-merge-foo name' '
182 test_must_fail git merge -s index c1
183'
184
185test_expect_success 'merge c0 with c1' '
186 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
187
188 cat >expect <<-\EOF &&
189 Updating FROM..TO
190 Fast-forward
191 file | 2 +-
192 other | 9 +++++++++
193 2 files changed, 10 insertions(+), 1 deletion(-)
194 create mode 100644 other
195 EOF
196
197 git reset --hard c0 &&
198 git merge c1 >out &&
199 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
200 test_cmp expect actual &&
201 verify_merge file result.1 &&
202 verify_head "$c1" &&
203
204 git reflog -1 >reflog.actual &&
205 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
206 test_cmp reflog.expected reflog.fuzzy
207'
208
209test_debug 'git log --graph --decorate --oneline --all'
210
211test_expect_success 'merge c0 with c1 with --ff-only' '
212 git reset --hard c0 &&
213 git merge --ff-only c1 &&
214 git merge --ff-only HEAD c0 c1 &&
215 verify_merge file result.1 &&
216 verify_head "$c1"
217'
218
219test_expect_success 'the same merge with merge.stat=diffstat' '
220 cat >expect <<-\EOF &&
221 Updating FROM..TO
222 Fast-forward
223 file | 2 +-
224 other | 9 +++++++++
225 2 files changed, 10 insertions(+), 1 deletion(-)
226 create mode 100644 other
227 EOF
228
229 git reset --hard c0 &&
230 git -c merge.stat=diffstat merge c1 >out &&
231 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
232 test_cmp expect actual
233'
234
235test_expect_success 'the same merge with compact summary' '
236 cat >expect <<-\EOF &&
237 Updating FROM..TO
238 Fast-forward
239 file | 2 +-
240 other (new) | 9 +++++++++
241 2 files changed, 10 insertions(+), 1 deletion(-)
242 EOF
243
244 git reset --hard c0 &&
245 git merge --compact-summary c1 >out &&
246 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
247 test_cmp expect actual
248'
249
250test_expect_success 'the same merge with compact summary' '
251 cat >expect <<-\EOF &&
252 Updating FROM..TO
253 Fast-forward
254 file | 2 +-
255 other (new) | 9 +++++++++
256 2 files changed, 10 insertions(+), 1 deletion(-)
257 EOF
258
259 git reset --hard c0 &&
260 git merge --compact-summary c1 >out &&
261 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
262 test_cmp expect actual
263'
264
265test_expect_success 'the same merge with merge.stat=compact' '
266 cat >expect <<-\EOF &&
267 Updating FROM..TO
268 Fast-forward
269 file | 2 +-
270 other (new) | 9 +++++++++
271 2 files changed, 10 insertions(+), 1 deletion(-)
272 EOF
273
274 git reset --hard c0 &&
275 git -c merge.stat=compact merge c1 >out &&
276 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
277 test_cmp expect actual
278'
279
280test_debug 'git log --graph --decorate --oneline --all'
281
282test_expect_success 'merge from unborn branch' '
283 git checkout -f main &&
284 test_might_fail git branch -D kid &&
285
286 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
287
288 git checkout --orphan kid &&
289 test_when_finished "git checkout -f main" &&
290 git rm -fr . &&
291 test_tick &&
292 git merge --ff-only c1 &&
293 verify_merge file result.1 &&
294 verify_head "$c1" &&
295
296 git reflog -1 >reflog.actual &&
297 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
298 test_cmp reflog.expected reflog.fuzzy
299'
300
301test_debug 'git log --graph --decorate --oneline --all'
302
303test_expect_success 'merge c1 with c2' '
304 git reset --hard c1 &&
305 test_tick &&
306 git merge c2 &&
307 verify_merge file result.1-5 msg.1-5 &&
308 verify_parents $c1 $c2
309'
310
311test_expect_success 'merge c1 with c2 when index.lock exists' '
312 test_when_finished rm .git/index.lock &&
313 git reset --hard c1 &&
314 >.git/index.lock &&
315 test_must_fail git merge c2 &&
316 test_path_is_missing .git/MERGE_HEAD &&
317 test_path_is_missing .git/MERGE_MODE &&
318 test_path_is_missing .git/MERGE_MSG
319'
320
321test_expect_success 'merge --squash c3 with c7' '
322 git reset --hard c3 &&
323 test_must_fail git merge --squash c7 &&
324 cat result.9z >file &&
325 git commit --no-edit -a &&
326
327 cat >expect <<-EOF &&
328 Squashed commit of the following:
329
330 $(git show -s c7)
331
332 # Conflicts:
333 # file
334 EOF
335 commit_body HEAD >actual &&
336 test_cmp expect actual
337'
338
339test_expect_success 'merge --squash --autostash conflict does not attempt to apply autostash' '
340 git reset --hard c3 &&
341 >unrelated &&
342 git add unrelated &&
343 test_must_fail git merge --squash c7 --autostash >out 2>err &&
344 test_grep ! "Applying autostash resulted in conflicts." err &&
345 test_grep "When finished, apply stashed changes with \`git stash pop\`" out
346'
347
348test_expect_success 'merge c3 with c7 with commit.cleanup = scissors' '
349 git config commit.cleanup scissors &&
350 git reset --hard c3 &&
351 test_must_fail git merge c7 &&
352 cat result.9z >file &&
353 git commit --no-edit -a &&
354
355 cat >expect <<-\EOF &&
356 Merge tag '"'"'c7'"'"'
357
358 # ------------------------ >8 ------------------------
359 # Do not modify or remove the line above.
360 # Everything below it will be ignored.
361 #
362 # Conflicts:
363 # file
364 EOF
365 commit_body HEAD >actual &&
366 test_cmp expect actual
367'
368
369test_expect_success 'merge c3 with c7 with --squash commit.cleanup = scissors' '
370 git config commit.cleanup scissors &&
371 git reset --hard c3 &&
372 test_must_fail git merge --squash c7 &&
373 cat result.9z >file &&
374 git commit --no-edit -a &&
375
376 cat >expect <<-EOF &&
377 Squashed commit of the following:
378
379 $(git show -s c7)
380
381 # ------------------------ >8 ------------------------
382 # Do not modify or remove the line above.
383 # Everything below it will be ignored.
384 #
385 # Conflicts:
386 # file
387 EOF
388 commit_body HEAD >actual &&
389 test_cmp expect actual
390'
391
392test_debug 'git log --graph --decorate --oneline --all'
393
394test_expect_success 'merge c1 with c2 and c3' '
395 git reset --hard c1 &&
396 test_tick &&
397 git merge c2 c3 &&
398 verify_merge file result.1-5-9 msg.1-5-9 &&
399 verify_parents $c1 $c2 $c3
400'
401
402test_debug 'git log --graph --decorate --oneline --all'
403
404test_expect_success 'merges with --ff-only' '
405 git reset --hard c1 &&
406 test_tick &&
407 test_must_fail git merge --ff-only c2 &&
408 test_must_fail git merge --ff-only c3 &&
409 test_must_fail git merge --ff-only c2 c3 &&
410 git reset --hard c0 &&
411 git merge c3 &&
412 verify_head $c3
413'
414
415test_expect_success 'merges with merge.ff=only' '
416 git reset --hard c1 &&
417 test_tick &&
418 test_config merge.ff "only" &&
419 test_must_fail git merge c2 &&
420 test_must_fail git merge c3 &&
421 test_must_fail git merge c2 c3 &&
422 git reset --hard c0 &&
423 git merge c3 &&
424 verify_head $c3
425'
426
427test_expect_success 'merge c0 with c1 (no-commit)' '
428 git reset --hard c0 &&
429 git merge --no-commit c1 &&
430 verify_merge file result.1 &&
431 verify_head $c1
432'
433
434test_debug 'git log --graph --decorate --oneline --all'
435
436test_expect_success 'merge c1 with c2 (no-commit)' '
437 git reset --hard c1 &&
438 git merge --no-commit c2 &&
439 verify_merge file result.1-5 &&
440 verify_head $c1 &&
441 verify_mergeheads $c2
442'
443
444test_debug 'git log --graph --decorate --oneline --all'
445
446test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
447 git reset --hard c1 &&
448 git merge --no-commit c2 c3 &&
449 verify_merge file result.1-5-9 &&
450 verify_head $c1 &&
451 verify_mergeheads $c2 $c3
452'
453
454test_debug 'git log --graph --decorate --oneline --all'
455
456test_expect_success 'merge c0 with c1 (squash)' '
457 git reset --hard c0 &&
458 git merge --squash c1 &&
459 verify_merge file result.1 &&
460 verify_head $c0 &&
461 verify_no_mergehead &&
462 test_cmp squash.1 .git/SQUASH_MSG
463'
464
465test_debug 'git log --graph --decorate --oneline --all'
466
467test_expect_success 'merge c0 with c1 (squash, ff-only)' '
468 git reset --hard c0 &&
469 git merge --squash --ff-only c1 &&
470 verify_merge file result.1 &&
471 verify_head $c0 &&
472 verify_no_mergehead &&
473 test_cmp squash.1 .git/SQUASH_MSG
474'
475
476test_debug 'git log --graph --decorate --oneline --all'
477
478test_expect_success 'merge c1 with c2 (squash)' '
479 git reset --hard c1 &&
480 git merge --squash c2 &&
481 verify_merge file result.1-5 &&
482 verify_head $c1 &&
483 verify_no_mergehead &&
484 test_cmp squash.1-5 .git/SQUASH_MSG
485'
486
487test_debug 'git log --graph --decorate --oneline --all'
488
489test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
490 git reset --hard c1 &&
491 test_must_fail git merge --squash --ff-only c2
492'
493
494test_debug 'git log --graph --decorate --oneline --all'
495
496test_expect_success 'merge c1 with c2 and c3 (squash)' '
497 git reset --hard c1 &&
498 git merge --squash c2 c3 &&
499 verify_merge file result.1-5-9 &&
500 verify_head $c1 &&
501 verify_no_mergehead &&
502 test_cmp squash.1-5-9 .git/SQUASH_MSG
503'
504
505test_debug 'git log --graph --decorate --oneline --all'
506
507test_expect_success 'merge c1 with c2 (no-commit in config)' '
508 git reset --hard c1 &&
509 test_config branch.main.mergeoptions "--no-commit" &&
510 git merge c2 &&
511 verify_merge file result.1-5 &&
512 verify_head $c1 &&
513 verify_mergeheads $c2
514'
515
516test_debug 'git log --graph --decorate --oneline --all'
517
518test_expect_success 'merge c1 with c2 (log in config)' '
519 git reset --hard c1 &&
520 git merge --log c2 &&
521 git show -s --pretty=tformat:%s%n%b >expect &&
522
523 test_config branch.main.mergeoptions "--log" &&
524 git reset --hard c1 &&
525 git merge c2 &&
526 git show -s --pretty=tformat:%s%n%b >actual &&
527
528 test_cmp expect actual
529'
530
531test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
532 git reset --hard c1 &&
533 git merge c2 &&
534 git show -s --pretty=tformat:%s%n%b >expect &&
535
536 test_config branch.main.mergeoptions "--no-log" &&
537 test_config merge.log "true" &&
538 git reset --hard c1 &&
539 git merge c2 &&
540 git show -s --pretty=tformat:%s%n%b >actual &&
541
542 test_cmp expect actual
543'
544
545test_expect_success 'merge c1 with c2 (squash in config)' '
546 git reset --hard c1 &&
547 test_config branch.main.mergeoptions "--squash" &&
548 git merge c2 &&
549 verify_merge file result.1-5 &&
550 verify_head $c1 &&
551 verify_no_mergehead &&
552 test_cmp squash.1-5 .git/SQUASH_MSG
553'
554
555test_debug 'git log --graph --decorate --oneline --all'
556
557test_expect_success 'override config option -n with --summary' '
558 git reset --hard c1 &&
559 test_config branch.main.mergeoptions "-n" &&
560 test_tick &&
561 git merge --summary c2 >diffstat.txt &&
562 verify_merge file result.1-5 msg.1-5 &&
563 verify_parents $c1 $c2 &&
564 if ! grep "^ file | *2 +-$" diffstat.txt
565 then
566 echo "[OOPS] diffstat was not generated with --summary"
567 false
568 fi
569'
570
571test_expect_success 'override config option -n with --stat' '
572 git reset --hard c1 &&
573 test_config branch.main.mergeoptions "-n" &&
574 test_tick &&
575 git merge --stat c2 >diffstat.txt &&
576 verify_merge file result.1-5 msg.1-5 &&
577 verify_parents $c1 $c2 &&
578 if ! grep "^ file | *2 +-$" diffstat.txt
579 then
580 echo "[OOPS] diffstat was not generated with --stat"
581 false
582 fi
583'
584
585test_debug 'git log --graph --decorate --oneline --all'
586
587test_expect_success 'override config option --stat' '
588 git reset --hard c1 &&
589 test_config branch.main.mergeoptions "--stat" &&
590 test_tick &&
591 git merge -n c2 >diffstat.txt &&
592 verify_merge file result.1-5 msg.1-5 &&
593 verify_parents $c1 $c2 &&
594 if grep "^ file | *2 +-$" diffstat.txt
595 then
596 echo "[OOPS] diffstat was generated"
597 false
598 fi
599'
600
601test_debug 'git log --graph --decorate --oneline --all'
602
603test_expect_success 'merge c1 with c2 (override --no-commit)' '
604 git reset --hard c1 &&
605 test_config branch.main.mergeoptions "--no-commit" &&
606 test_tick &&
607 git merge --commit c2 &&
608 verify_merge file result.1-5 msg.1-5 &&
609 verify_parents $c1 $c2
610'
611
612test_debug 'git log --graph --decorate --oneline --all'
613
614test_expect_success 'merge c1 with c2 (override --squash)' '
615 git reset --hard c1 &&
616 test_config branch.main.mergeoptions "--squash" &&
617 test_tick &&
618 git merge --no-squash c2 &&
619 verify_merge file result.1-5 msg.1-5 &&
620 verify_parents $c1 $c2
621'
622
623test_debug 'git log --graph --decorate --oneline --all'
624
625test_expect_success 'merge c0 with c1 (no-ff)' '
626 git reset --hard c0 &&
627 test_tick &&
628 git merge --no-ff c1 &&
629 verify_merge file result.1 &&
630 verify_parents $c0 $c1
631'
632
633test_debug 'git log --graph --decorate --oneline --all'
634
635test_expect_success 'merge c0 with c1 (merge.ff=false)' '
636 git reset --hard c0 &&
637 test_config merge.ff "false" &&
638 test_tick &&
639 git merge c1 &&
640 verify_merge file result.1 &&
641 verify_parents $c0 $c1
642'
643test_debug 'git log --graph --decorate --oneline --all'
644
645test_expect_success 'combine branch.main.mergeoptions with merge.ff' '
646 git reset --hard c0 &&
647 test_config branch.main.mergeoptions "--ff" &&
648 test_config merge.ff "false" &&
649 test_tick &&
650 git merge c1 &&
651 verify_merge file result.1 &&
652 verify_parents "$c0"
653'
654
655test_expect_success 'tolerate unknown values for merge.ff' '
656 git reset --hard c0 &&
657 test_config merge.ff "something-new" &&
658 test_tick &&
659 git merge c1 2>message &&
660 verify_head "$c1" &&
661 test_must_be_empty message
662'
663
664test_expect_success 'combining --squash and --no-ff is refused' '
665 git reset --hard c0 &&
666 test_must_fail git merge --squash --no-ff c1 &&
667 test_must_fail git merge --no-ff --squash c1
668'
669
670test_expect_success 'combining --squash and --commit is refused' '
671 git reset --hard c0 &&
672 test_must_fail git merge --squash --commit c1 &&
673 test_must_fail git merge --commit --squash c1
674'
675
676test_expect_success 'option --ff-only overwrites --no-ff' '
677 git merge --no-ff --ff-only c1 &&
678 test_must_fail git merge --no-ff --ff-only c2
679'
680
681test_expect_success 'option --no-ff overrides merge.ff=only config' '
682 git reset --hard c0 &&
683 test_config merge.ff only &&
684 git merge --no-ff c1
685'
686
687test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
688 git reset --hard c0 &&
689 test_config branch.main.mergeoptions "--no-ff" &&
690 git merge --ff c1 &&
691 verify_merge file result.1 &&
692 verify_head $c1
693'
694
695test_expect_success 'merge log message' '
696 git reset --hard c0 &&
697 git merge --no-log c2 &&
698 git show -s --pretty=format:%b HEAD >msg.act &&
699 test_must_be_empty msg.act &&
700
701 git reset --hard c0 &&
702 test_config branch.main.mergeoptions "--no-ff" &&
703 git merge --no-log c2 &&
704 git show -s --pretty=format:%b HEAD >msg.act &&
705 test_must_be_empty msg.act &&
706
707 git merge --log c3 &&
708 git show -s --pretty=format:%b HEAD >msg.act &&
709 test_cmp msg.log msg.act &&
710
711 git reset --hard HEAD^ &&
712 test_config merge.log "yes" &&
713 git merge c3 &&
714 git show -s --pretty=format:%b HEAD >msg.act &&
715 test_cmp msg.log msg.act
716'
717
718test_debug 'git log --graph --decorate --oneline --all'
719
720test_expect_success 'merge c1 with c0, c2, c0, and c1' '
721 git reset --hard c1 &&
722 test_tick &&
723 git merge c0 c2 c0 c1 &&
724 verify_merge file result.1-5 &&
725 verify_parents $c1 $c2
726'
727
728test_debug 'git log --graph --decorate --oneline --all'
729
730test_expect_success 'merge c1 with c0, c2, c0, and c1' '
731 git reset --hard c1 &&
732 test_tick &&
733 git merge c0 c2 c0 c1 &&
734 verify_merge file result.1-5 &&
735 verify_parents $c1 $c2
736'
737
738test_debug 'git log --graph --decorate --oneline --all'
739
740test_expect_success 'merge c1 with c1 and c2' '
741 git reset --hard c1 &&
742 test_tick &&
743 git merge c1 c2 &&
744 verify_merge file result.1-5 &&
745 verify_parents $c1 $c2
746'
747
748test_debug 'git log --graph --decorate --oneline --all'
749
750test_expect_success 'merge fast-forward in a dirty tree' '
751 git reset --hard c0 &&
752 mv file file1 &&
753 cat file1 >file &&
754 rm -f file1 &&
755 git merge c2
756'
757
758test_debug 'git log --graph --decorate --oneline --all'
759
760test_expect_success 'in-index merge' '
761 git reset --hard c0 &&
762 git merge --no-ff -s resolve c1 >out &&
763 test_grep "Wonderful." out &&
764 verify_parents $c0 $c1
765'
766
767test_debug 'git log --graph --decorate --oneline --all'
768
769test_expect_success 'refresh the index before merging' '
770 git reset --hard c1 &&
771 cp file file.n && mv -f file.n file &&
772 git merge c3
773'
774
775test_expect_success 'merge with --autostash' '
776 git reset --hard c1 &&
777 git merge-file file file.orig file.9 &&
778 git merge --autostash c2 2>err &&
779 test_grep "Applied autostash." err &&
780 git show HEAD:file >merge-result &&
781 test_cmp result.1-5 merge-result &&
782 test_cmp result.1-5-9 file
783'
784
785test_expect_success 'merge with merge.autoStash' '
786 test_config merge.autoStash true &&
787 git reset --hard c1 &&
788 git merge-file file file.orig file.9 &&
789 git merge c2 2>err &&
790 test_grep "Applied autostash." err &&
791 git show HEAD:file >merge-result &&
792 test_cmp result.1-5 merge-result &&
793 test_cmp result.1-5-9 file
794'
795
796test_expect_success 'fast-forward merge with --autostash' '
797 git reset --hard c0 &&
798 git merge-file file file.orig file.5 &&
799 git merge --autostash c1 2>err &&
800 test_grep "Applied autostash." err &&
801 test_cmp result.1-5 file
802'
803
804test_expect_success 'fast-forward merge with --autostash, stash.index' '
805 git reset --hard c0 &&
806 git stash clear &&
807 echo staged >>z && git add z &&
808 git -c stash.index=true merge --autostash c1 2>err &&
809 test_grep "Applied autostash." err &&
810 test_stdout_line_count = 0 git stash list
811'
812
813test_expect_success 'failed fast-forward merge with --autostash' '
814 git reset --hard c0 &&
815 git merge-file file file.orig file.5 &&
816 cp file.5 other &&
817 test_when_finished "rm other" &&
818 test_must_fail git merge --autostash c1 2>err &&
819 test_grep "Applied autostash." err &&
820 test_cmp file.5 file
821'
822
823test_expect_success 'octopus merge with --autostash' '
824 git reset --hard c1 &&
825 git merge-file file file.orig file.3 &&
826 git merge --autostash c2 c3 2>err &&
827 test_grep "Applied autostash." err &&
828 git show HEAD:file >merge-result &&
829 test_cmp result.1-5-9 merge-result &&
830 test_cmp result.1-3-5-9 file
831'
832
833test_expect_success 'failed merge (exit 2) with --autostash' '
834 git reset --hard c1 &&
835 git merge-file file file.orig file.5 &&
836 test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
837 test_grep "Applied autostash." err &&
838 test_cmp result.1-5 file
839'
840
841test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
842 git reset --hard c3 &&
843 cp file.1 file &&
844 test_must_fail git merge --autostash c7 &&
845 git merge --abort 2>err &&
846 test_grep "Applied autostash." err &&
847 test_cmp file.1 file
848'
849
850test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
851 git reset --hard c1 &&
852 git merge-file file file.orig file.9 &&
853 git diff >expect &&
854 git merge --no-commit --autostash c2 &&
855 git stash show -p MERGE_AUTOSTASH >actual &&
856 test_cmp expect actual &&
857 git commit 2>err &&
858 test_grep "Applied autostash." err &&
859 git show HEAD:file >merge-result &&
860 test_cmp result.1-5 merge-result &&
861 test_cmp result.1-5-9 file
862'
863
864test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
865 git reset --hard c1 &&
866 git merge-file file file.orig file.9 &&
867 git diff >expect &&
868 git merge --no-commit --autostash c2 &&
869 git stash show -p MERGE_AUTOSTASH >actual &&
870 test_cmp expect actual &&
871 git merge --continue 2>err &&
872 test_grep "Applied autostash." err &&
873 git show HEAD:file >merge-result &&
874 test_cmp result.1-5 merge-result &&
875 test_cmp result.1-5-9 file
876'
877
878test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
879 git reset --hard c1 &&
880 git merge-file file file.orig file.9 &&
881 git diff >expect &&
882 git merge --no-commit --autostash c2 &&
883 git stash show -p MERGE_AUTOSTASH >actual &&
884 test_cmp expect actual &&
885 git merge --abort 2>err &&
886 test_grep "Applied autostash." err &&
887 git diff >actual &&
888 test_cmp expect actual
889'
890
891test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
892 git reset --hard c1 &&
893 git merge-file file file.orig file.9 &&
894 git diff >expect &&
895 git merge --no-commit --autostash c2 &&
896 git stash show -p MERGE_AUTOSTASH >actual &&
897 test_cmp expect actual &&
898 git reset --hard 2>err &&
899 test_grep "Autostash exists; creating a new stash entry." err &&
900 git diff --exit-code
901'
902
903test_expect_success 'quit merge with --no-commit and --autostash' '
904 git reset --hard c1 &&
905 git merge-file file file.orig file.9 &&
906 git diff >expect &&
907 git merge --no-commit --autostash c2 &&
908 git stash show -p MERGE_AUTOSTASH >actual &&
909 test_cmp expect actual &&
910 git diff HEAD >expect &&
911 git merge --quit 2>err &&
912 test_grep "Autostash exists; creating a new stash entry." err &&
913 git diff HEAD >actual &&
914 test_cmp expect actual
915'
916
917test_expect_success 'merge with conflicted --autostash changes' '
918 git reset --hard c1 &&
919 git merge-file file file.orig file.9y &&
920 git diff >expect &&
921 test_when_finished "test_might_fail git stash drop" &&
922 git merge --autostash c3 2>err &&
923 test_grep "applying them" err &&
924 test_grep "resulted in conflicts" err &&
925 git show HEAD:file >merge-result &&
926 test_cmp result.1-9 merge-result &&
927 git stash show -p >actual &&
928 test_cmp expect actual
929'
930
931cat >expected.branch <<\EOF
932Merge branch 'c5-branch' (early part)
933EOF
934cat >expected.tag <<\EOF
935Merge commit 'c5~1'
936EOF
937
938test_expect_success 'merge early part of c2' '
939 git reset --hard c3 &&
940 echo c4 >c4.c &&
941 git add c4.c &&
942 git commit -m c4 &&
943 git tag c4 &&
944 echo c5 >c5.c &&
945 git add c5.c &&
946 git commit -m c5 &&
947 git tag c5 &&
948 git reset --hard c3 &&
949 echo c6 >c6.c &&
950 git add c6.c &&
951 git commit -m c6 &&
952 git tag c6 &&
953 git branch -f c5-branch c5 &&
954 git merge c5-branch~1 &&
955 git show -s --pretty=tformat:%s HEAD >actual.branch &&
956 git reset --keep HEAD^ &&
957 git merge c5~1 &&
958 git show -s --pretty=tformat:%s HEAD >actual.tag &&
959 test_cmp expected.branch actual.branch &&
960 test_cmp expected.tag actual.tag
961'
962
963test_debug 'git log --graph --decorate --oneline --all'
964
965test_expect_success 'merge --no-ff --no-commit && commit' '
966 git reset --hard c0 &&
967 git merge --no-ff --no-commit c1 &&
968 EDITOR=: git commit &&
969 verify_parents $c0 $c1
970'
971
972test_debug 'git log --graph --decorate --oneline --all'
973
974test_expect_success 'amending no-ff merge commit' '
975 EDITOR=: git commit --amend &&
976 verify_parents $c0 $c1
977'
978
979test_debug 'git log --graph --decorate --oneline --all'
980
981cat >editor <<\EOF
982#!/bin/sh
983# Add a new message string that was not in the template
984(
985 echo "Merge work done on the side branch c1"
986 echo
987 cat "$1"
988) >"$1.tmp" && mv "$1.tmp" "$1"
989# strip comments and blank lines from end of message
990sed -e '/^#/d' "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' >expected
991EOF
992chmod 755 editor
993
994test_expect_success 'merge --no-ff --edit' '
995 git reset --hard c0 &&
996 EDITOR=./editor git merge --no-ff --edit c1 &&
997 verify_parents $c0 $c1 &&
998 commit_body HEAD >actual &&
999 test_grep "work done on the side branch" actual &&
1000 test_cmp expected actual
1001'
1002
1003test_expect_success 'merge annotated/signed tag w/o tracking' '
1004 test_when_finished "rm -rf dst; git tag -d anno1" &&
1005 git tag -a -m "anno c1" anno1 c1 &&
1006 git init dst &&
1007 git rev-parse c1 >dst/expect &&
1008 (
1009 # c0 fast-forwards to c1 but because this repository
1010 # is not a "downstream" whose refs/tags follows along
1011 # tag from the "upstream", this pull defaults to --no-ff
1012 cd dst &&
1013 git pull .. c0 &&
1014 git pull .. anno1 &&
1015 git rev-parse HEAD^2 >actual &&
1016 test_cmp expect actual
1017 )
1018'
1019
1020test_expect_success 'merge annotated/signed tag w/ tracking' '
1021 test_when_finished "rm -rf dst; git tag -d anno1" &&
1022 git tag -a -m "anno c1" anno1 c1 &&
1023 git init dst &&
1024 git rev-parse c1 >dst/expect &&
1025 (
1026 # c0 fast-forwards to c1 and because this repository
1027 # is a "downstream" whose refs/tags follows along
1028 # tag from the "upstream", this pull defaults to --ff
1029 cd dst &&
1030 git remote add origin .. &&
1031 git pull origin c0 &&
1032 git fetch origin &&
1033 git merge anno1 &&
1034 git rev-parse HEAD >actual &&
1035 test_cmp expect actual
1036 )
1037'
1038
1039test_expect_success GPG 'merge --ff-only tag' '
1040 git reset --hard c0 &&
1041 git commit --allow-empty -m "A newer commit" &&
1042 git tag -s -m "A newer commit" signed &&
1043 git reset --hard c0 &&
1044
1045 git merge --ff-only signed &&
1046 git rev-parse signed^0 >expect &&
1047 git rev-parse HEAD >actual &&
1048 test_cmp expect actual
1049'
1050
1051test_expect_success GPG 'merge --no-edit tag should skip editor' '
1052 git reset --hard c0 &&
1053 git commit --allow-empty -m "A newer commit" &&
1054 git tag -f -s -m "A newer commit" signed &&
1055 git reset --hard c0 &&
1056
1057 EDITOR=false git merge --no-edit --no-ff signed &&
1058 git rev-parse signed^0 >expect &&
1059 git rev-parse HEAD^2 >actual &&
1060 test_cmp expect actual
1061'
1062
1063test_expect_success GPG 'merge tag with multiple signatures' '
1064 git commit --allow-empty -m "A newer commit" &&
1065 git tag -s -m "A newer commit" signed_base &&
1066
1067 OBJECT_ID=$(git rev-parse HEAD) &&
1068 git cat-file -p signed_base >base &&
1069 HEADERS=$(sed "/^$/q" base) &&
1070 BODY=$(sed "1,/^$/d" base) &&
1071
1072 # Manually craft a tag with multiple signatures. But we
1073 # utilize the signature of a valid tag, so that this new tag will
1074 # also pass verification.
1075 cat >tag-text <<-EOF &&
1076 $HEADERS
1077 gpgsig -----BEGIN PGP SIGNATURE-----
1078 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
1079 -----END PGP SIGNATURE-----
1080 gpgsig-sha256 -----BEGIN PGP SIGNATURE-----
1081 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
1082 -----END PGP SIGNATURE-----
1083 gpgsig-sha256 -----BEGIN PGP SIGNATURE-----
1084 VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
1085 -----END PGP SIGNATURE-----
1086
1087 $BODY
1088 EOF
1089
1090 TAG_HASH=$(git hash-object -t tag -w tag-text) &&
1091 git update-ref refs/tags/multiple-signs $TAG_HASH &&
1092 git reset --hard @~1 &&
1093 git merge --no-edit multiple-signs &&
1094 git cat-file -p HEAD >actual &&
1095 # During the merge of signed tags, validation is done against the
1096 # payload after stripping any additional signature.
1097 test_grep "Good signature" actual
1098'
1099
1100test_expect_success 'set up mod-256 conflict scenario' '
1101 # 256 near-identical stanzas...
1102 for i in $(test_seq 1 256); do
1103 for j in 1 2 3 4 5; do
1104 echo $i-$j || return 1
1105 done
1106 done >file &&
1107 git add file &&
1108 git commit -m base &&
1109
1110 # one side changes the first line of each to "main"
1111 sed s/-1/-main/ file >tmp &&
1112 mv tmp file &&
1113 git commit -am main &&
1114
1115 # and the other to "side"; merging the two will
1116 # yield 256 separate conflicts
1117 git checkout -b side HEAD^ &&
1118 sed s/-1/-side/ file >tmp &&
1119 mv tmp file &&
1120 git commit -am side
1121'
1122
1123test_expect_success 'merge detects mod-256 conflicts (recursive)' '
1124 git reset --hard &&
1125 test_must_fail git merge -s recursive main
1126'
1127
1128test_expect_success 'merge detects mod-256 conflicts (resolve)' '
1129 git reset --hard &&
1130 test_must_fail git merge -s resolve main
1131'
1132
1133test_expect_success 'merge nothing into void' '
1134 git init void &&
1135 (
1136 cd void &&
1137 git remote add up .. &&
1138 git fetch up &&
1139 test_must_fail git merge FETCH_HEAD
1140 )
1141'
1142
1143test_expect_success 'merge can be completed with --continue' '
1144 git reset --hard c0 &&
1145 git merge --no-ff --no-commit c1 &&
1146 git merge --continue &&
1147 verify_parents $c0 $c1
1148'
1149
1150write_script .git/FAKE_EDITOR <<EOF
1151# kill -TERM command added below.
1152EOF
1153
1154test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue' '
1155 git reset --hard c0 &&
1156 ! "$SHELL_PATH" -c '\''
1157 echo kill -TERM $$ >>.git/FAKE_EDITOR
1158 GIT_EDITOR=.git/FAKE_EDITOR
1159 export GIT_EDITOR
1160 exec git merge --no-ff --edit c1'\'' &&
1161 git merge --continue &&
1162 verify_parents $c0 $c1
1163'
1164
1165test_expect_success 'merge --quit' '
1166 git init merge-quit &&
1167 (
1168 cd merge-quit &&
1169 test_commit base &&
1170 echo one >>base.t &&
1171 git commit -am one &&
1172 git branch one &&
1173 git checkout base &&
1174 echo two >>base.t &&
1175 git commit -am two &&
1176 test_must_fail git -c rerere.enabled=true merge one &&
1177 test_path_is_file .git/MERGE_HEAD &&
1178 test_path_is_file .git/MERGE_MODE &&
1179 test_path_is_file .git/MERGE_MSG &&
1180 git rerere status >rerere.before &&
1181 git merge --quit &&
1182 test_path_is_missing .git/MERGE_HEAD &&
1183 test_path_is_missing .git/MERGE_MODE &&
1184 test_path_is_missing .git/MERGE_MSG &&
1185 git rerere status >rerere.after &&
1186 test_must_be_empty rerere.after &&
1187 ! test_cmp rerere.after rerere.before
1188 )
1189'
1190
1191test_expect_success 'merge suggests matching remote refname' '
1192 git commit --allow-empty -m not-local &&
1193 git update-ref refs/remotes/origin/not-local HEAD &&
1194 git reset --hard HEAD^ &&
1195
1196 # This is white-box testing hackery; we happen to know
1197 # that reading packed refs is more picky about the memory
1198 # ownership of strings we pass to for_each_ref() callbacks.
1199 git pack-refs --all --prune &&
1200
1201 test_must_fail git merge not-local 2>stderr &&
1202 test_grep origin/not-local stderr
1203'
1204
1205test_expect_success 'suggested names are not ambiguous' '
1206 git update-ref refs/heads/origin/not-local HEAD &&
1207 test_must_fail git merge not-local 2>stderr &&
1208 test_grep remotes/origin/not-local stderr
1209'
1210
1211test_expect_success 'merge with no argument defaults to upstream' '
1212 test_when_finished "rm -rf upstream downstream" &&
1213 git init upstream &&
1214 (
1215 cd upstream &&
1216 test_commit one &&
1217 test_commit two
1218 ) &&
1219 git clone upstream downstream &&
1220 (
1221 cd downstream &&
1222 git reset --hard HEAD^ &&
1223 git merge &&
1224 test_cmp_rev origin/main HEAD
1225 )
1226'
1227
1228test_done