Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Lars Hjemli
4 #
5
6 test_description='git merge
7
8 Testing 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
29 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
30 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
31
32 . ./test-lib.sh
33 . "$TEST_DIRECTORY"/lib-gpg.sh
34
35 test_write_lines 1 2 3 4 5 6 7 8 9 >file
36 cp file file.orig
37 test_write_lines '1 X' 2 3 4 5 6 7 8 9 >file.1
38 test_write_lines 1 2 '3 X' 4 5 6 7 8 9 >file.3
39 test_write_lines 1 2 3 4 '5 X' 6 7 8 9 >file.5
40 test_write_lines 1 2 3 4 5 6 7 8 '9 X' >file.9
41 test_write_lines 1 2 3 4 5 6 7 8 '9 Y' >file.9y
42 test_write_lines '1 X' 2 3 4 5 6 7 8 9 >result.1
43 test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
44 test_write_lines '1 X' 2 3 4 5 6 7 8 '9 X' >result.1-9
45 test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
46 test_write_lines '1 X' 2 '3 X' 4 '5 X' 6 7 8 '9 X' >result.1-3-5-9
47 test_write_lines 1 2 3 4 5 6 7 8 '9 Z' >result.9z
48
49 create_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
73 verify_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
84 verify_head () {
85 echo "$1" >head.expected &&
86 git rev-parse HEAD >head.actual &&
87 test_cmp head.expected head.actual
88 }
89
90 verify_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
104 verify_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
113 verify_no_mergehead () {
114 ! test -e .git/MERGE_HEAD
115 }
116
117 test_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
155 test_debug 'git log --graph --decorate --oneline --all'
156
157 test_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
170 test_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
181 test_expect_success 'reject non-strategy with a git-merge-foo name' '
182 test_must_fail git merge -s index c1
183 '
184
185 test_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
209 test_debug 'git log --graph --decorate --oneline --all'
210
211 test_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
219 test_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
235 test_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
250 test_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
265 test_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
280 test_debug 'git log --graph --decorate --oneline --all'
281
282 test_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
301 test_debug 'git log --graph --decorate --oneline --all'
302
303 test_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
311 test_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
321 test_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
339 test_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
348 test_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
369 test_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
392 test_debug 'git log --graph --decorate --oneline --all'
393
394 test_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
402 test_debug 'git log --graph --decorate --oneline --all'
403
404 test_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
415 test_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
427 test_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
434 test_debug 'git log --graph --decorate --oneline --all'
435
436 test_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
444 test_debug 'git log --graph --decorate --oneline --all'
445
446 test_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
454 test_debug 'git log --graph --decorate --oneline --all'
455
456 test_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
465 test_debug 'git log --graph --decorate --oneline --all'
466
467 test_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
476 test_debug 'git log --graph --decorate --oneline --all'
477
478 test_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
487 test_debug 'git log --graph --decorate --oneline --all'
488
489 test_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
494 test_debug 'git log --graph --decorate --oneline --all'
495
496 test_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
505 test_debug 'git log --graph --decorate --oneline --all'
506
507 test_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
516 test_debug 'git log --graph --decorate --oneline --all'
517
518 test_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
531 test_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
545 test_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
555 test_debug 'git log --graph --decorate --oneline --all'
556
557 test_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
571 test_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
585 test_debug 'git log --graph --decorate --oneline --all'
586
587 test_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
601 test_debug 'git log --graph --decorate --oneline --all'
602
603 test_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
612 test_debug 'git log --graph --decorate --oneline --all'
613
614 test_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
623 test_debug 'git log --graph --decorate --oneline --all'
624
625 test_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
633 test_debug 'git log --graph --decorate --oneline --all'
634
635 test_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 '
643 test_debug 'git log --graph --decorate --oneline --all'
644
645 test_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
655 test_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
664 test_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
670 test_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
676 test_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
681 test_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
687 test_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
695 test_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
718 test_debug 'git log --graph --decorate --oneline --all'
719
720 test_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
728 test_debug 'git log --graph --decorate --oneline --all'
729
730 test_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
738 test_debug 'git log --graph --decorate --oneline --all'
739
740 test_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
748 test_debug 'git log --graph --decorate --oneline --all'
749
750 test_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
758 test_debug 'git log --graph --decorate --oneline --all'
759
760 test_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
767 test_debug 'git log --graph --decorate --oneline --all'
768
769 test_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
775 test_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
785 test_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
796 test_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
804 test_expect_success 'failed fast-forward merge with --autostash' '
805 git reset --hard c0 &&
806 git merge-file file file.orig file.5 &&
807 cp file.5 other &&
808 test_when_finished "rm other" &&
809 test_must_fail git merge --autostash c1 2>err &&
810 test_grep "Applied autostash." err &&
811 test_cmp file.5 file
812 '
813
814 test_expect_success 'octopus merge with --autostash' '
815 git reset --hard c1 &&
816 git merge-file file file.orig file.3 &&
817 git merge --autostash c2 c3 2>err &&
818 test_grep "Applied autostash." err &&
819 git show HEAD:file >merge-result &&
820 test_cmp result.1-5-9 merge-result &&
821 test_cmp result.1-3-5-9 file
822 '
823
824 test_expect_success 'failed merge (exit 2) with --autostash' '
825 git reset --hard c1 &&
826 git merge-file file file.orig file.5 &&
827 test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
828 test_grep "Applied autostash." err &&
829 test_cmp result.1-5 file
830 '
831
832 test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
833 git reset --hard c3 &&
834 cp file.1 file &&
835 test_must_fail git merge --autostash c7 &&
836 git merge --abort 2>err &&
837 test_grep "Applied autostash." err &&
838 test_cmp file.1 file
839 '
840
841 test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
842 git reset --hard c1 &&
843 git merge-file file file.orig file.9 &&
844 git diff >expect &&
845 git merge --no-commit --autostash c2 &&
846 git stash show -p MERGE_AUTOSTASH >actual &&
847 test_cmp expect actual &&
848 git commit 2>err &&
849 test_grep "Applied autostash." err &&
850 git show HEAD:file >merge-result &&
851 test_cmp result.1-5 merge-result &&
852 test_cmp result.1-5-9 file
853 '
854
855 test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
856 git reset --hard c1 &&
857 git merge-file file file.orig file.9 &&
858 git diff >expect &&
859 git merge --no-commit --autostash c2 &&
860 git stash show -p MERGE_AUTOSTASH >actual &&
861 test_cmp expect actual &&
862 git merge --continue 2>err &&
863 test_grep "Applied autostash." err &&
864 git show HEAD:file >merge-result &&
865 test_cmp result.1-5 merge-result &&
866 test_cmp result.1-5-9 file
867 '
868
869 test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
870 git reset --hard c1 &&
871 git merge-file file file.orig file.9 &&
872 git diff >expect &&
873 git merge --no-commit --autostash c2 &&
874 git stash show -p MERGE_AUTOSTASH >actual &&
875 test_cmp expect actual &&
876 git merge --abort 2>err &&
877 test_grep "Applied autostash." err &&
878 git diff >actual &&
879 test_cmp expect actual
880 '
881
882 test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
883 git reset --hard c1 &&
884 git merge-file file file.orig file.9 &&
885 git diff >expect &&
886 git merge --no-commit --autostash c2 &&
887 git stash show -p MERGE_AUTOSTASH >actual &&
888 test_cmp expect actual &&
889 git reset --hard 2>err &&
890 test_grep "Autostash exists; creating a new stash entry." err &&
891 git diff --exit-code
892 '
893
894 test_expect_success 'quit merge with --no-commit and --autostash' '
895 git reset --hard c1 &&
896 git merge-file file file.orig file.9 &&
897 git diff >expect &&
898 git merge --no-commit --autostash c2 &&
899 git stash show -p MERGE_AUTOSTASH >actual &&
900 test_cmp expect actual &&
901 git diff HEAD >expect &&
902 git merge --quit 2>err &&
903 test_grep "Autostash exists; creating a new stash entry." err &&
904 git diff HEAD >actual &&
905 test_cmp expect actual
906 '
907
908 test_expect_success 'merge with conflicted --autostash changes' '
909 git reset --hard c1 &&
910 git merge-file file file.orig file.9y &&
911 git diff >expect &&
912 test_when_finished "test_might_fail git stash drop" &&
913 git merge --autostash c3 2>err &&
914 test_grep "applying them" err &&
915 test_grep "resulted in conflicts" err &&
916 git show HEAD:file >merge-result &&
917 test_cmp result.1-9 merge-result &&
918 git stash show -p >actual &&
919 test_cmp expect actual
920 '
921
922 cat >expected.branch <<\EOF
923 Merge branch 'c5-branch' (early part)
924 EOF
925 cat >expected.tag <<\EOF
926 Merge commit 'c5~1'
927 EOF
928
929 test_expect_success 'merge early part of c2' '
930 git reset --hard c3 &&
931 echo c4 >c4.c &&
932 git add c4.c &&
933 git commit -m c4 &&
934 git tag c4 &&
935 echo c5 >c5.c &&
936 git add c5.c &&
937 git commit -m c5 &&
938 git tag c5 &&
939 git reset --hard c3 &&
940 echo c6 >c6.c &&
941 git add c6.c &&
942 git commit -m c6 &&
943 git tag c6 &&
944 git branch -f c5-branch c5 &&
945 git merge c5-branch~1 &&
946 git show -s --pretty=tformat:%s HEAD >actual.branch &&
947 git reset --keep HEAD^ &&
948 git merge c5~1 &&
949 git show -s --pretty=tformat:%s HEAD >actual.tag &&
950 test_cmp expected.branch actual.branch &&
951 test_cmp expected.tag actual.tag
952 '
953
954 test_debug 'git log --graph --decorate --oneline --all'
955
956 test_expect_success 'merge --no-ff --no-commit && commit' '
957 git reset --hard c0 &&
958 git merge --no-ff --no-commit c1 &&
959 EDITOR=: git commit &&
960 verify_parents $c0 $c1
961 '
962
963 test_debug 'git log --graph --decorate --oneline --all'
964
965 test_expect_success 'amending no-ff merge commit' '
966 EDITOR=: git commit --amend &&
967 verify_parents $c0 $c1
968 '
969
970 test_debug 'git log --graph --decorate --oneline --all'
971
972 cat >editor <<\EOF
973 #!/bin/sh
974 # Add a new message string that was not in the template
975 (
976 echo "Merge work done on the side branch c1"
977 echo
978 cat "$1"
979 ) >"$1.tmp" && mv "$1.tmp" "$1"
980 # strip comments and blank lines from end of message
981 sed -e '/^#/d' "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' >expected
982 EOF
983 chmod 755 editor
984
985 test_expect_success 'merge --no-ff --edit' '
986 git reset --hard c0 &&
987 EDITOR=./editor git merge --no-ff --edit c1 &&
988 verify_parents $c0 $c1 &&
989 commit_body HEAD >actual &&
990 test_grep "work done on the side branch" actual &&
991 test_cmp expected actual
992 '
993
994 test_expect_success 'merge annotated/signed tag w/o tracking' '
995 test_when_finished "rm -rf dst; git tag -d anno1" &&
996 git tag -a -m "anno c1" anno1 c1 &&
997 git init dst &&
998 git rev-parse c1 >dst/expect &&
999 (
1000 # c0 fast-forwards to c1 but because this repository
1001 # is not a "downstream" whose refs/tags follows along
1002 # tag from the "upstream", this pull defaults to --no-ff
1003 cd dst &&
1004 git pull .. c0 &&
1005 git pull .. anno1 &&
1006 git rev-parse HEAD^2 >actual &&
1007 test_cmp expect actual
1008 )
1009 '
1010
1011 test_expect_success 'merge annotated/signed tag w/ tracking' '
1012 test_when_finished "rm -rf dst; git tag -d anno1" &&
1013 git tag -a -m "anno c1" anno1 c1 &&
1014 git init dst &&
1015 git rev-parse c1 >dst/expect &&
1016 (
1017 # c0 fast-forwards to c1 and because this repository
1018 # is a "downstream" whose refs/tags follows along
1019 # tag from the "upstream", this pull defaults to --ff
1020 cd dst &&
1021 git remote add origin .. &&
1022 git pull origin c0 &&
1023 git fetch origin &&
1024 git merge anno1 &&
1025 git rev-parse HEAD >actual &&
1026 test_cmp expect actual
1027 )
1028 '
1029
1030 test_expect_success GPG 'merge --ff-only tag' '
1031 git reset --hard c0 &&
1032 git commit --allow-empty -m "A newer commit" &&
1033 git tag -s -m "A newer commit" signed &&
1034 git reset --hard c0 &&
1035
1036 git merge --ff-only signed &&
1037 git rev-parse signed^0 >expect &&
1038 git rev-parse HEAD >actual &&
1039 test_cmp expect actual
1040 '
1041
1042 test_expect_success GPG 'merge --no-edit tag should skip editor' '
1043 git reset --hard c0 &&
1044 git commit --allow-empty -m "A newer commit" &&
1045 git tag -f -s -m "A newer commit" signed &&
1046 git reset --hard c0 &&
1047
1048 EDITOR=false git merge --no-edit --no-ff signed &&
1049 git rev-parse signed^0 >expect &&
1050 git rev-parse HEAD^2 >actual &&
1051 test_cmp expect actual
1052 '
1053
1054 test_expect_success 'set up mod-256 conflict scenario' '
1055 # 256 near-identical stanzas...
1056 for i in $(test_seq 1 256); do
1057 for j in 1 2 3 4 5; do
1058 echo $i-$j || return 1
1059 done
1060 done >file &&
1061 git add file &&
1062 git commit -m base &&
1063
1064 # one side changes the first line of each to "main"
1065 sed s/-1/-main/ file >tmp &&
1066 mv tmp file &&
1067 git commit -am main &&
1068
1069 # and the other to "side"; merging the two will
1070 # yield 256 separate conflicts
1071 git checkout -b side HEAD^ &&
1072 sed s/-1/-side/ file >tmp &&
1073 mv tmp file &&
1074 git commit -am side
1075 '
1076
1077 test_expect_success 'merge detects mod-256 conflicts (recursive)' '
1078 git reset --hard &&
1079 test_must_fail git merge -s recursive main
1080 '
1081
1082 test_expect_success 'merge detects mod-256 conflicts (resolve)' '
1083 git reset --hard &&
1084 test_must_fail git merge -s resolve main
1085 '
1086
1087 test_expect_success 'merge nothing into void' '
1088 git init void &&
1089 (
1090 cd void &&
1091 git remote add up .. &&
1092 git fetch up &&
1093 test_must_fail git merge FETCH_HEAD
1094 )
1095 '
1096
1097 test_expect_success 'merge can be completed with --continue' '
1098 git reset --hard c0 &&
1099 git merge --no-ff --no-commit c1 &&
1100 git merge --continue &&
1101 verify_parents $c0 $c1
1102 '
1103
1104 write_script .git/FAKE_EDITOR <<EOF
1105 # kill -TERM command added below.
1106 EOF
1107
1108 test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue' '
1109 git reset --hard c0 &&
1110 ! "$SHELL_PATH" -c '\''
1111 echo kill -TERM $$ >>.git/FAKE_EDITOR
1112 GIT_EDITOR=.git/FAKE_EDITOR
1113 export GIT_EDITOR
1114 exec git merge --no-ff --edit c1'\'' &&
1115 git merge --continue &&
1116 verify_parents $c0 $c1
1117 '
1118
1119 test_expect_success 'merge --quit' '
1120 git init merge-quit &&
1121 (
1122 cd merge-quit &&
1123 test_commit base &&
1124 echo one >>base.t &&
1125 git commit -am one &&
1126 git branch one &&
1127 git checkout base &&
1128 echo two >>base.t &&
1129 git commit -am two &&
1130 test_must_fail git -c rerere.enabled=true merge one &&
1131 test_path_is_file .git/MERGE_HEAD &&
1132 test_path_is_file .git/MERGE_MODE &&
1133 test_path_is_file .git/MERGE_MSG &&
1134 git rerere status >rerere.before &&
1135 git merge --quit &&
1136 test_path_is_missing .git/MERGE_HEAD &&
1137 test_path_is_missing .git/MERGE_MODE &&
1138 test_path_is_missing .git/MERGE_MSG &&
1139 git rerere status >rerere.after &&
1140 test_must_be_empty rerere.after &&
1141 ! test_cmp rerere.after rerere.before
1142 )
1143 '
1144
1145 test_expect_success 'merge suggests matching remote refname' '
1146 git commit --allow-empty -m not-local &&
1147 git update-ref refs/remotes/origin/not-local HEAD &&
1148 git reset --hard HEAD^ &&
1149
1150 # This is white-box testing hackery; we happen to know
1151 # that reading packed refs is more picky about the memory
1152 # ownership of strings we pass to for_each_ref() callbacks.
1153 git pack-refs --all --prune &&
1154
1155 test_must_fail git merge not-local 2>stderr &&
1156 test_grep origin/not-local stderr
1157 '
1158
1159 test_expect_success 'suggested names are not ambiguous' '
1160 git update-ref refs/heads/origin/not-local HEAD &&
1161 test_must_fail git merge not-local 2>stderr &&
1162 test_grep remotes/origin/not-local stderr
1163 '
1164
1165 test_expect_success 'merge with no argument defaults to upstream' '
1166 test_when_finished "rm -rf upstream downstream" &&
1167 git init upstream &&
1168 (
1169 cd upstream &&
1170 test_commit one &&
1171 test_commit two
1172 ) &&
1173 git clone upstream downstream &&
1174 (
1175 cd downstream &&
1176 git reset --hard HEAD^ &&
1177 git merge &&
1178 test_cmp_rev origin/main HEAD
1179 )
1180 '
1181
1182 test_done