]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7600-merge.sh
9838094b66ac393e550eaabba7201ff987028a98
[thirdparty/git.git] / t / t7600-merge.sh
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 test_expect_code 129 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 git cat-file commit HEAD >raw &&
336 sed -e "1,/^$/d" raw >actual &&
337 test_cmp expect actual
338 '
339
340 test_expect_success 'merge --squash --autostash conflict does not attempt to apply autostash' '
341 git reset --hard c3 &&
342 >unrelated &&
343 git add unrelated &&
344 test_must_fail git merge --squash c7 --autostash >out 2>err &&
345 ! grep "Applying autostash resulted in conflicts." err &&
346 grep "When finished, apply stashed changes with \`git stash pop\`" out
347 '
348
349 test_expect_success 'merge c3 with c7 with commit.cleanup = scissors' '
350 git config commit.cleanup scissors &&
351 git reset --hard c3 &&
352 test_must_fail git merge c7 &&
353 cat result.9z >file &&
354 git commit --no-edit -a &&
355
356 cat >expect <<-\EOF &&
357 Merge tag '"'"'c7'"'"'
358
359 # ------------------------ >8 ------------------------
360 # Do not modify or remove the line above.
361 # Everything below it will be ignored.
362 #
363 # Conflicts:
364 # file
365 EOF
366 git cat-file commit HEAD >raw &&
367 sed -e "1,/^$/d" raw >actual &&
368 test_cmp expect actual
369 '
370
371 test_expect_success 'merge c3 with c7 with --squash commit.cleanup = scissors' '
372 git config commit.cleanup scissors &&
373 git reset --hard c3 &&
374 test_must_fail git merge --squash c7 &&
375 cat result.9z >file &&
376 git commit --no-edit -a &&
377
378 cat >expect <<-EOF &&
379 Squashed commit of the following:
380
381 $(git show -s c7)
382
383 # ------------------------ >8 ------------------------
384 # Do not modify or remove the line above.
385 # Everything below it will be ignored.
386 #
387 # Conflicts:
388 # file
389 EOF
390 git cat-file commit HEAD >raw &&
391 sed -e "1,/^$/d" raw >actual &&
392 test_cmp expect actual
393 '
394
395 test_debug 'git log --graph --decorate --oneline --all'
396
397 test_expect_success 'merge c1 with c2 and c3' '
398 git reset --hard c1 &&
399 test_tick &&
400 git merge c2 c3 &&
401 verify_merge file result.1-5-9 msg.1-5-9 &&
402 verify_parents $c1 $c2 $c3
403 '
404
405 test_debug 'git log --graph --decorate --oneline --all'
406
407 test_expect_success 'merges with --ff-only' '
408 git reset --hard c1 &&
409 test_tick &&
410 test_must_fail git merge --ff-only c2 &&
411 test_must_fail git merge --ff-only c3 &&
412 test_must_fail git merge --ff-only c2 c3 &&
413 git reset --hard c0 &&
414 git merge c3 &&
415 verify_head $c3
416 '
417
418 test_expect_success 'merges with merge.ff=only' '
419 git reset --hard c1 &&
420 test_tick &&
421 test_config merge.ff "only" &&
422 test_must_fail git merge c2 &&
423 test_must_fail git merge c3 &&
424 test_must_fail git merge c2 c3 &&
425 git reset --hard c0 &&
426 git merge c3 &&
427 verify_head $c3
428 '
429
430 test_expect_success 'merge c0 with c1 (no-commit)' '
431 git reset --hard c0 &&
432 git merge --no-commit c1 &&
433 verify_merge file result.1 &&
434 verify_head $c1
435 '
436
437 test_debug 'git log --graph --decorate --oneline --all'
438
439 test_expect_success 'merge c1 with c2 (no-commit)' '
440 git reset --hard c1 &&
441 git merge --no-commit c2 &&
442 verify_merge file result.1-5 &&
443 verify_head $c1 &&
444 verify_mergeheads $c2
445 '
446
447 test_debug 'git log --graph --decorate --oneline --all'
448
449 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
450 git reset --hard c1 &&
451 git merge --no-commit c2 c3 &&
452 verify_merge file result.1-5-9 &&
453 verify_head $c1 &&
454 verify_mergeheads $c2 $c3
455 '
456
457 test_debug 'git log --graph --decorate --oneline --all'
458
459 test_expect_success 'merge c0 with c1 (squash)' '
460 git reset --hard c0 &&
461 git merge --squash c1 &&
462 verify_merge file result.1 &&
463 verify_head $c0 &&
464 verify_no_mergehead &&
465 test_cmp squash.1 .git/SQUASH_MSG
466 '
467
468 test_debug 'git log --graph --decorate --oneline --all'
469
470 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
471 git reset --hard c0 &&
472 git merge --squash --ff-only c1 &&
473 verify_merge file result.1 &&
474 verify_head $c0 &&
475 verify_no_mergehead &&
476 test_cmp squash.1 .git/SQUASH_MSG
477 '
478
479 test_debug 'git log --graph --decorate --oneline --all'
480
481 test_expect_success 'merge c1 with c2 (squash)' '
482 git reset --hard c1 &&
483 git merge --squash c2 &&
484 verify_merge file result.1-5 &&
485 verify_head $c1 &&
486 verify_no_mergehead &&
487 test_cmp squash.1-5 .git/SQUASH_MSG
488 '
489
490 test_debug 'git log --graph --decorate --oneline --all'
491
492 test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
493 git reset --hard c1 &&
494 test_must_fail git merge --squash --ff-only c2
495 '
496
497 test_debug 'git log --graph --decorate --oneline --all'
498
499 test_expect_success 'merge c1 with c2 and c3 (squash)' '
500 git reset --hard c1 &&
501 git merge --squash c2 c3 &&
502 verify_merge file result.1-5-9 &&
503 verify_head $c1 &&
504 verify_no_mergehead &&
505 test_cmp squash.1-5-9 .git/SQUASH_MSG
506 '
507
508 test_debug 'git log --graph --decorate --oneline --all'
509
510 test_expect_success 'merge c1 with c2 (no-commit in config)' '
511 git reset --hard c1 &&
512 test_config branch.main.mergeoptions "--no-commit" &&
513 git merge c2 &&
514 verify_merge file result.1-5 &&
515 verify_head $c1 &&
516 verify_mergeheads $c2
517 '
518
519 test_debug 'git log --graph --decorate --oneline --all'
520
521 test_expect_success 'merge c1 with c2 (log in config)' '
522 git reset --hard c1 &&
523 git merge --log c2 &&
524 git show -s --pretty=tformat:%s%n%b >expect &&
525
526 test_config branch.main.mergeoptions "--log" &&
527 git reset --hard c1 &&
528 git merge c2 &&
529 git show -s --pretty=tformat:%s%n%b >actual &&
530
531 test_cmp expect actual
532 '
533
534 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
535 git reset --hard c1 &&
536 git merge c2 &&
537 git show -s --pretty=tformat:%s%n%b >expect &&
538
539 test_config branch.main.mergeoptions "--no-log" &&
540 test_config merge.log "true" &&
541 git reset --hard c1 &&
542 git merge c2 &&
543 git show -s --pretty=tformat:%s%n%b >actual &&
544
545 test_cmp expect actual
546 '
547
548 test_expect_success 'merge c1 with c2 (squash in config)' '
549 git reset --hard c1 &&
550 test_config branch.main.mergeoptions "--squash" &&
551 git merge c2 &&
552 verify_merge file result.1-5 &&
553 verify_head $c1 &&
554 verify_no_mergehead &&
555 test_cmp squash.1-5 .git/SQUASH_MSG
556 '
557
558 test_debug 'git log --graph --decorate --oneline --all'
559
560 test_expect_success 'override config option -n with --summary' '
561 git reset --hard c1 &&
562 test_config branch.main.mergeoptions "-n" &&
563 test_tick &&
564 git merge --summary c2 >diffstat.txt &&
565 verify_merge file result.1-5 msg.1-5 &&
566 verify_parents $c1 $c2 &&
567 if ! grep "^ file | *2 +-$" diffstat.txt
568 then
569 echo "[OOPS] diffstat was not generated with --summary"
570 false
571 fi
572 '
573
574 test_expect_success 'override config option -n with --stat' '
575 git reset --hard c1 &&
576 test_config branch.main.mergeoptions "-n" &&
577 test_tick &&
578 git merge --stat c2 >diffstat.txt &&
579 verify_merge file result.1-5 msg.1-5 &&
580 verify_parents $c1 $c2 &&
581 if ! grep "^ file | *2 +-$" diffstat.txt
582 then
583 echo "[OOPS] diffstat was not generated with --stat"
584 false
585 fi
586 '
587
588 test_debug 'git log --graph --decorate --oneline --all'
589
590 test_expect_success 'override config option --stat' '
591 git reset --hard c1 &&
592 test_config branch.main.mergeoptions "--stat" &&
593 test_tick &&
594 git merge -n c2 >diffstat.txt &&
595 verify_merge file result.1-5 msg.1-5 &&
596 verify_parents $c1 $c2 &&
597 if grep "^ file | *2 +-$" diffstat.txt
598 then
599 echo "[OOPS] diffstat was generated"
600 false
601 fi
602 '
603
604 test_debug 'git log --graph --decorate --oneline --all'
605
606 test_expect_success 'merge c1 with c2 (override --no-commit)' '
607 git reset --hard c1 &&
608 test_config branch.main.mergeoptions "--no-commit" &&
609 test_tick &&
610 git merge --commit c2 &&
611 verify_merge file result.1-5 msg.1-5 &&
612 verify_parents $c1 $c2
613 '
614
615 test_debug 'git log --graph --decorate --oneline --all'
616
617 test_expect_success 'merge c1 with c2 (override --squash)' '
618 git reset --hard c1 &&
619 test_config branch.main.mergeoptions "--squash" &&
620 test_tick &&
621 git merge --no-squash c2 &&
622 verify_merge file result.1-5 msg.1-5 &&
623 verify_parents $c1 $c2
624 '
625
626 test_debug 'git log --graph --decorate --oneline --all'
627
628 test_expect_success 'merge c0 with c1 (no-ff)' '
629 git reset --hard c0 &&
630 test_tick &&
631 git merge --no-ff c1 &&
632 verify_merge file result.1 &&
633 verify_parents $c0 $c1
634 '
635
636 test_debug 'git log --graph --decorate --oneline --all'
637
638 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
639 git reset --hard c0 &&
640 test_config merge.ff "false" &&
641 test_tick &&
642 git merge c1 &&
643 verify_merge file result.1 &&
644 verify_parents $c0 $c1
645 '
646 test_debug 'git log --graph --decorate --oneline --all'
647
648 test_expect_success 'combine branch.main.mergeoptions with merge.ff' '
649 git reset --hard c0 &&
650 test_config branch.main.mergeoptions "--ff" &&
651 test_config merge.ff "false" &&
652 test_tick &&
653 git merge c1 &&
654 verify_merge file result.1 &&
655 verify_parents "$c0"
656 '
657
658 test_expect_success 'tolerate unknown values for merge.ff' '
659 git reset --hard c0 &&
660 test_config merge.ff "something-new" &&
661 test_tick &&
662 git merge c1 2>message &&
663 verify_head "$c1" &&
664 test_must_be_empty message
665 '
666
667 test_expect_success 'combining --squash and --no-ff is refused' '
668 git reset --hard c0 &&
669 test_must_fail git merge --squash --no-ff c1 &&
670 test_must_fail git merge --no-ff --squash c1
671 '
672
673 test_expect_success 'combining --squash and --commit is refused' '
674 git reset --hard c0 &&
675 test_must_fail git merge --squash --commit c1 &&
676 test_must_fail git merge --commit --squash c1
677 '
678
679 test_expect_success 'option --ff-only overwrites --no-ff' '
680 git merge --no-ff --ff-only c1 &&
681 test_must_fail git merge --no-ff --ff-only c2
682 '
683
684 test_expect_success 'option --no-ff overrides merge.ff=only config' '
685 git reset --hard c0 &&
686 test_config merge.ff only &&
687 git merge --no-ff c1
688 '
689
690 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
691 git reset --hard c0 &&
692 test_config branch.main.mergeoptions "--no-ff" &&
693 git merge --ff c1 &&
694 verify_merge file result.1 &&
695 verify_head $c1
696 '
697
698 test_expect_success 'merge log message' '
699 git reset --hard c0 &&
700 git merge --no-log c2 &&
701 git show -s --pretty=format:%b HEAD >msg.act &&
702 test_must_be_empty msg.act &&
703
704 git reset --hard c0 &&
705 test_config branch.main.mergeoptions "--no-ff" &&
706 git merge --no-log c2 &&
707 git show -s --pretty=format:%b HEAD >msg.act &&
708 test_must_be_empty msg.act &&
709
710 git merge --log c3 &&
711 git show -s --pretty=format:%b HEAD >msg.act &&
712 test_cmp msg.log msg.act &&
713
714 git reset --hard HEAD^ &&
715 test_config merge.log "yes" &&
716 git merge c3 &&
717 git show -s --pretty=format:%b HEAD >msg.act &&
718 test_cmp msg.log msg.act
719 '
720
721 test_debug 'git log --graph --decorate --oneline --all'
722
723 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
724 git reset --hard c1 &&
725 test_tick &&
726 git merge c0 c2 c0 c1 &&
727 verify_merge file result.1-5 &&
728 verify_parents $c1 $c2
729 '
730
731 test_debug 'git log --graph --decorate --oneline --all'
732
733 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
734 git reset --hard c1 &&
735 test_tick &&
736 git merge c0 c2 c0 c1 &&
737 verify_merge file result.1-5 &&
738 verify_parents $c1 $c2
739 '
740
741 test_debug 'git log --graph --decorate --oneline --all'
742
743 test_expect_success 'merge c1 with c1 and c2' '
744 git reset --hard c1 &&
745 test_tick &&
746 git merge c1 c2 &&
747 verify_merge file result.1-5 &&
748 verify_parents $c1 $c2
749 '
750
751 test_debug 'git log --graph --decorate --oneline --all'
752
753 test_expect_success 'merge fast-forward in a dirty tree' '
754 git reset --hard c0 &&
755 mv file file1 &&
756 cat file1 >file &&
757 rm -f file1 &&
758 git merge c2
759 '
760
761 test_debug 'git log --graph --decorate --oneline --all'
762
763 test_expect_success 'in-index merge' '
764 git reset --hard c0 &&
765 git merge --no-ff -s resolve c1 >out &&
766 test_grep "Wonderful." out &&
767 verify_parents $c0 $c1
768 '
769
770 test_debug 'git log --graph --decorate --oneline --all'
771
772 test_expect_success 'refresh the index before merging' '
773 git reset --hard c1 &&
774 cp file file.n && mv -f file.n file &&
775 git merge c3
776 '
777
778 test_expect_success 'merge with --autostash' '
779 git reset --hard c1 &&
780 git merge-file file file.orig file.9 &&
781 git merge --autostash c2 2>err &&
782 test_grep "Applied autostash." err &&
783 git show HEAD:file >merge-result &&
784 test_cmp result.1-5 merge-result &&
785 test_cmp result.1-5-9 file
786 '
787
788 test_expect_success 'merge with merge.autoStash' '
789 test_config merge.autoStash true &&
790 git reset --hard c1 &&
791 git merge-file file file.orig file.9 &&
792 git merge c2 2>err &&
793 test_grep "Applied autostash." err &&
794 git show HEAD:file >merge-result &&
795 test_cmp result.1-5 merge-result &&
796 test_cmp result.1-5-9 file
797 '
798
799 test_expect_success 'fast-forward merge with --autostash' '
800 git reset --hard c0 &&
801 git merge-file file file.orig file.5 &&
802 git merge --autostash c1 2>err &&
803 test_grep "Applied autostash." err &&
804 test_cmp result.1-5 file
805 '
806
807 test_expect_success 'failed fast-forward merge with --autostash' '
808 git reset --hard c0 &&
809 git merge-file file file.orig file.5 &&
810 cp file.5 other &&
811 test_when_finished "rm other" &&
812 test_must_fail git merge --autostash c1 2>err &&
813 test_grep "Applied autostash." err &&
814 test_cmp file.5 file
815 '
816
817 test_expect_success 'octopus merge with --autostash' '
818 git reset --hard c1 &&
819 git merge-file file file.orig file.3 &&
820 git merge --autostash c2 c3 2>err &&
821 test_grep "Applied autostash." err &&
822 git show HEAD:file >merge-result &&
823 test_cmp result.1-5-9 merge-result &&
824 test_cmp result.1-3-5-9 file
825 '
826
827 test_expect_success 'failed merge (exit 2) with --autostash' '
828 git reset --hard c1 &&
829 git merge-file file file.orig file.5 &&
830 test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
831 test_grep "Applied autostash." err &&
832 test_cmp result.1-5 file
833 '
834
835 test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
836 git reset --hard c3 &&
837 cp file.1 file &&
838 test_must_fail git merge --autostash c7 &&
839 git merge --abort 2>err &&
840 test_grep "Applied autostash." err &&
841 test_cmp file.1 file
842 '
843
844 test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
845 git reset --hard c1 &&
846 git merge-file file file.orig file.9 &&
847 git diff >expect &&
848 git merge --no-commit --autostash c2 &&
849 git stash show -p MERGE_AUTOSTASH >actual &&
850 test_cmp expect actual &&
851 git commit 2>err &&
852 test_grep "Applied autostash." err &&
853 git show HEAD:file >merge-result &&
854 test_cmp result.1-5 merge-result &&
855 test_cmp result.1-5-9 file
856 '
857
858 test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
859 git reset --hard c1 &&
860 git merge-file file file.orig file.9 &&
861 git diff >expect &&
862 git merge --no-commit --autostash c2 &&
863 git stash show -p MERGE_AUTOSTASH >actual &&
864 test_cmp expect actual &&
865 git merge --continue 2>err &&
866 test_grep "Applied autostash." err &&
867 git show HEAD:file >merge-result &&
868 test_cmp result.1-5 merge-result &&
869 test_cmp result.1-5-9 file
870 '
871
872 test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
873 git reset --hard c1 &&
874 git merge-file file file.orig file.9 &&
875 git diff >expect &&
876 git merge --no-commit --autostash c2 &&
877 git stash show -p MERGE_AUTOSTASH >actual &&
878 test_cmp expect actual &&
879 git merge --abort 2>err &&
880 test_grep "Applied autostash." err &&
881 git diff >actual &&
882 test_cmp expect actual
883 '
884
885 test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
886 git reset --hard c1 &&
887 git merge-file file file.orig file.9 &&
888 git diff >expect &&
889 git merge --no-commit --autostash c2 &&
890 git stash show -p MERGE_AUTOSTASH >actual &&
891 test_cmp expect actual &&
892 git reset --hard 2>err &&
893 test_grep "Autostash exists; creating a new stash entry." err &&
894 git diff --exit-code
895 '
896
897 test_expect_success 'quit merge with --no-commit and --autostash' '
898 git reset --hard c1 &&
899 git merge-file file file.orig file.9 &&
900 git diff >expect &&
901 git merge --no-commit --autostash c2 &&
902 git stash show -p MERGE_AUTOSTASH >actual &&
903 test_cmp expect actual &&
904 git diff HEAD >expect &&
905 git merge --quit 2>err &&
906 test_grep "Autostash exists; creating a new stash entry." err &&
907 git diff HEAD >actual &&
908 test_cmp expect actual
909 '
910
911 test_expect_success 'merge with conflicted --autostash changes' '
912 git reset --hard c1 &&
913 git merge-file file file.orig file.9y &&
914 git diff >expect &&
915 test_when_finished "test_might_fail git stash drop" &&
916 git merge --autostash c3 2>err &&
917 test_grep "Applying autostash resulted in conflicts." err &&
918 git show HEAD:file >merge-result &&
919 test_cmp result.1-9 merge-result &&
920 git stash show -p >actual &&
921 test_cmp expect actual
922 '
923
924 cat >expected.branch <<\EOF
925 Merge branch 'c5-branch' (early part)
926 EOF
927 cat >expected.tag <<\EOF
928 Merge commit 'c5~1'
929 EOF
930
931 test_expect_success 'merge early part of c2' '
932 git reset --hard c3 &&
933 echo c4 >c4.c &&
934 git add c4.c &&
935 git commit -m c4 &&
936 git tag c4 &&
937 echo c5 >c5.c &&
938 git add c5.c &&
939 git commit -m c5 &&
940 git tag c5 &&
941 git reset --hard c3 &&
942 echo c6 >c6.c &&
943 git add c6.c &&
944 git commit -m c6 &&
945 git tag c6 &&
946 git branch -f c5-branch c5 &&
947 git merge c5-branch~1 &&
948 git show -s --pretty=tformat:%s HEAD >actual.branch &&
949 git reset --keep HEAD^ &&
950 git merge c5~1 &&
951 git show -s --pretty=tformat:%s HEAD >actual.tag &&
952 test_cmp expected.branch actual.branch &&
953 test_cmp expected.tag actual.tag
954 '
955
956 test_debug 'git log --graph --decorate --oneline --all'
957
958 test_expect_success 'merge --no-ff --no-commit && commit' '
959 git reset --hard c0 &&
960 git merge --no-ff --no-commit c1 &&
961 EDITOR=: git commit &&
962 verify_parents $c0 $c1
963 '
964
965 test_debug 'git log --graph --decorate --oneline --all'
966
967 test_expect_success 'amending no-ff merge commit' '
968 EDITOR=: git commit --amend &&
969 verify_parents $c0 $c1
970 '
971
972 test_debug 'git log --graph --decorate --oneline --all'
973
974 cat >editor <<\EOF
975 #!/bin/sh
976 # Add a new message string that was not in the template
977 (
978 echo "Merge work done on the side branch c1"
979 echo
980 cat "$1"
981 ) >"$1.tmp" && mv "$1.tmp" "$1"
982 # strip comments and blank lines from end of message
983 sed -e '/^#/d' "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' >expected
984 EOF
985 chmod 755 editor
986
987 test_expect_success 'merge --no-ff --edit' '
988 git reset --hard c0 &&
989 EDITOR=./editor git merge --no-ff --edit c1 &&
990 verify_parents $c0 $c1 &&
991 git cat-file commit HEAD >raw &&
992 grep "work done on the side branch" raw &&
993 sed "1,/^$/d" >actual raw &&
994 test_cmp expected actual
995 '
996
997 test_expect_success 'merge annotated/signed tag w/o tracking' '
998 test_when_finished "rm -rf dst; git tag -d anno1" &&
999 git tag -a -m "anno c1" anno1 c1 &&
1000 git init dst &&
1001 git rev-parse c1 >dst/expect &&
1002 (
1003 # c0 fast-forwards to c1 but because this repository
1004 # is not a "downstream" whose refs/tags follows along
1005 # tag from the "upstream", this pull defaults to --no-ff
1006 cd dst &&
1007 git pull .. c0 &&
1008 git pull .. anno1 &&
1009 git rev-parse HEAD^2 >actual &&
1010 test_cmp expect actual
1011 )
1012 '
1013
1014 test_expect_success 'merge annotated/signed tag w/ tracking' '
1015 test_when_finished "rm -rf dst; git tag -d anno1" &&
1016 git tag -a -m "anno c1" anno1 c1 &&
1017 git init dst &&
1018 git rev-parse c1 >dst/expect &&
1019 (
1020 # c0 fast-forwards to c1 and because this repository
1021 # is a "downstream" whose refs/tags follows along
1022 # tag from the "upstream", this pull defaults to --ff
1023 cd dst &&
1024 git remote add origin .. &&
1025 git pull origin c0 &&
1026 git fetch origin &&
1027 git merge anno1 &&
1028 git rev-parse HEAD >actual &&
1029 test_cmp expect actual
1030 )
1031 '
1032
1033 test_expect_success GPG 'merge --ff-only tag' '
1034 git reset --hard c0 &&
1035 git commit --allow-empty -m "A newer commit" &&
1036 git tag -s -m "A newer commit" signed &&
1037 git reset --hard c0 &&
1038
1039 git merge --ff-only signed &&
1040 git rev-parse signed^0 >expect &&
1041 git rev-parse HEAD >actual &&
1042 test_cmp expect actual
1043 '
1044
1045 test_expect_success GPG 'merge --no-edit tag should skip editor' '
1046 git reset --hard c0 &&
1047 git commit --allow-empty -m "A newer commit" &&
1048 git tag -f -s -m "A newer commit" signed &&
1049 git reset --hard c0 &&
1050
1051 EDITOR=false git merge --no-edit --no-ff signed &&
1052 git rev-parse signed^0 >expect &&
1053 git rev-parse HEAD^2 >actual &&
1054 test_cmp expect actual
1055 '
1056
1057 test_expect_success 'set up mod-256 conflict scenario' '
1058 # 256 near-identical stanzas...
1059 for i in $(test_seq 1 256); do
1060 for j in 1 2 3 4 5; do
1061 echo $i-$j || return 1
1062 done
1063 done >file &&
1064 git add file &&
1065 git commit -m base &&
1066
1067 # one side changes the first line of each to "main"
1068 sed s/-1/-main/ file >tmp &&
1069 mv tmp file &&
1070 git commit -am main &&
1071
1072 # and the other to "side"; merging the two will
1073 # yield 256 separate conflicts
1074 git checkout -b side HEAD^ &&
1075 sed s/-1/-side/ file >tmp &&
1076 mv tmp file &&
1077 git commit -am side
1078 '
1079
1080 test_expect_success 'merge detects mod-256 conflicts (recursive)' '
1081 git reset --hard &&
1082 test_must_fail git merge -s recursive main
1083 '
1084
1085 test_expect_success 'merge detects mod-256 conflicts (resolve)' '
1086 git reset --hard &&
1087 test_must_fail git merge -s resolve main
1088 '
1089
1090 test_expect_success 'merge nothing into void' '
1091 git init void &&
1092 (
1093 cd void &&
1094 git remote add up .. &&
1095 git fetch up &&
1096 test_must_fail git merge FETCH_HEAD
1097 )
1098 '
1099
1100 test_expect_success 'merge can be completed with --continue' '
1101 git reset --hard c0 &&
1102 git merge --no-ff --no-commit c1 &&
1103 git merge --continue &&
1104 verify_parents $c0 $c1
1105 '
1106
1107 write_script .git/FAKE_EDITOR <<EOF
1108 # kill -TERM command added below.
1109 EOF
1110
1111 test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue' '
1112 git reset --hard c0 &&
1113 ! "$SHELL_PATH" -c '\''
1114 echo kill -TERM $$ >>.git/FAKE_EDITOR
1115 GIT_EDITOR=.git/FAKE_EDITOR
1116 export GIT_EDITOR
1117 exec git merge --no-ff --edit c1'\'' &&
1118 git merge --continue &&
1119 verify_parents $c0 $c1
1120 '
1121
1122 test_expect_success 'merge --quit' '
1123 git init merge-quit &&
1124 (
1125 cd merge-quit &&
1126 test_commit base &&
1127 echo one >>base.t &&
1128 git commit -am one &&
1129 git branch one &&
1130 git checkout base &&
1131 echo two >>base.t &&
1132 git commit -am two &&
1133 test_must_fail git -c rerere.enabled=true merge one &&
1134 test_path_is_file .git/MERGE_HEAD &&
1135 test_path_is_file .git/MERGE_MODE &&
1136 test_path_is_file .git/MERGE_MSG &&
1137 git rerere status >rerere.before &&
1138 git merge --quit &&
1139 test_path_is_missing .git/MERGE_HEAD &&
1140 test_path_is_missing .git/MERGE_MODE &&
1141 test_path_is_missing .git/MERGE_MSG &&
1142 git rerere status >rerere.after &&
1143 test_must_be_empty rerere.after &&
1144 ! test_cmp rerere.after rerere.before
1145 )
1146 '
1147
1148 test_expect_success 'merge suggests matching remote refname' '
1149 git commit --allow-empty -m not-local &&
1150 git update-ref refs/remotes/origin/not-local HEAD &&
1151 git reset --hard HEAD^ &&
1152
1153 # This is white-box testing hackery; we happen to know
1154 # that reading packed refs is more picky about the memory
1155 # ownership of strings we pass to for_each_ref() callbacks.
1156 git pack-refs --all --prune &&
1157
1158 test_must_fail git merge not-local 2>stderr &&
1159 grep origin/not-local stderr
1160 '
1161
1162 test_expect_success 'suggested names are not ambiguous' '
1163 git update-ref refs/heads/origin/not-local HEAD &&
1164 test_must_fail git merge not-local 2>stderr &&
1165 grep remotes/origin/not-local stderr
1166 '
1167
1168 test_done