builtin: stop using `the_index`
[alt-git.git] / t / t7600-merge.sh
blobe5ff073099a983d0da7205d3a3325af28caf8a9b
1 #!/bin/sh
3 # Copyright (c) 2007 Lars Hjemli
6 test_description='git merge
8 Testing basic merge operations/option parsing.
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
29 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
30 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
32 . ./test-lib.sh
33 . "$TEST_DIRECTORY"/lib-gpg.sh
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
49 create_merge_msgs () {
50 echo "Merge tag 'c2'" >msg.1-5 &&
51 echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
53 echo "Squashed commit of the following:" &&
54 echo &&
55 git log --no-merges ^HEAD c1
56 } >squash.1 &&
58 echo "Squashed commit of the following:" &&
59 echo &&
60 git log --no-merges ^HEAD c2
61 } >squash.1-5 &&
63 echo "Squashed commit of the following:" &&
64 echo &&
65 git log --no-merges ^HEAD c2 c3
66 } >squash.1-5-9 &&
68 echo "* tag 'c3':" &&
69 echo " commit 3"
70 } >msg.log
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
84 verify_head () {
85 echo "$1" >head.expected &&
86 git rev-parse HEAD >head.actual &&
87 test_cmp head.expected head.actual
90 verify_parents () {
91 test_write_lines "$@" >parents.expected &&
92 >parents.actual &&
93 i=1 &&
94 while test $i -le $#
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
104 verify_mergeheads () {
105 test_write_lines "$@" >mergehead.expected &&
106 while read sha1 rest
108 git rev-parse $sha1 || return 1
109 done <.git/MERGE_HEAD >mergehead.actual &&
110 test_cmp mergehead.expected mergehead.actual
113 verify_no_mergehead () {
114 ! test -e .git/MERGE_HEAD
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
155 test_debug 'git log --graph --decorate --oneline --all'
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
170 test_expect_success 'merge -h with invalid index' '
171 mkdir broken &&
173 cd broken &&
174 git init &&
175 >.git/index &&
176 test_expect_code 129 git merge -h 2>usage
177 ) &&
178 test_grep "[Uu]sage: git merge" broken/usage
181 test_expect_success 'reject non-strategy with a git-merge-foo name' '
182 test_must_fail git merge -s index c1
185 test_expect_success 'merge c0 with c1' '
186 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
188 git reset --hard c0 &&
189 git merge c1 &&
190 verify_merge file result.1 &&
191 verify_head "$c1" &&
193 git reflog -1 >reflog.actual &&
194 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
195 test_cmp reflog.expected reflog.fuzzy
198 test_debug 'git log --graph --decorate --oneline --all'
200 test_expect_success 'merge c0 with c1 with --ff-only' '
201 git reset --hard c0 &&
202 git merge --ff-only c1 &&
203 git merge --ff-only HEAD c0 c1 &&
204 verify_merge file result.1 &&
205 verify_head "$c1"
208 test_debug 'git log --graph --decorate --oneline --all'
210 test_expect_success 'merge from unborn branch' '
211 git checkout -f main &&
212 test_might_fail git branch -D kid &&
214 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
216 git checkout --orphan kid &&
217 test_when_finished "git checkout -f main" &&
218 git rm -fr . &&
219 test_tick &&
220 git merge --ff-only c1 &&
221 verify_merge file result.1 &&
222 verify_head "$c1" &&
224 git reflog -1 >reflog.actual &&
225 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
226 test_cmp reflog.expected reflog.fuzzy
229 test_debug 'git log --graph --decorate --oneline --all'
231 test_expect_success 'merge c1 with c2' '
232 git reset --hard c1 &&
233 test_tick &&
234 git merge c2 &&
235 verify_merge file result.1-5 msg.1-5 &&
236 verify_parents $c1 $c2
239 test_expect_success 'merge --squash c3 with c7' '
240 git reset --hard c3 &&
241 test_must_fail git merge --squash c7 &&
242 cat result.9z >file &&
243 git commit --no-edit -a &&
245 cat >expect <<-EOF &&
246 Squashed commit of the following:
248 $(git show -s c7)
250 # Conflicts:
251 # file
253 git cat-file commit HEAD >raw &&
254 sed -e "1,/^$/d" raw >actual &&
255 test_cmp expect actual
258 test_expect_success 'merge --squash --autostash conflict does not attempt to apply autostash' '
259 git reset --hard c3 &&
260 >unrelated &&
261 git add unrelated &&
262 test_must_fail git merge --squash c7 --autostash >out 2>err &&
263 ! grep "Applying autostash resulted in conflicts." err &&
264 grep "When finished, apply stashed changes with \`git stash pop\`" out
267 test_expect_success 'merge c3 with c7 with commit.cleanup = scissors' '
268 git config commit.cleanup scissors &&
269 git reset --hard c3 &&
270 test_must_fail git merge c7 &&
271 cat result.9z >file &&
272 git commit --no-edit -a &&
274 cat >expect <<-\EOF &&
275 Merge tag '"'"'c7'"'"'
277 # ------------------------ >8 ------------------------
278 # Do not modify or remove the line above.
279 # Everything below it will be ignored.
281 # Conflicts:
282 # file
284 git cat-file commit HEAD >raw &&
285 sed -e "1,/^$/d" raw >actual &&
286 test_cmp expect actual
289 test_expect_success 'merge c3 with c7 with --squash commit.cleanup = scissors' '
290 git config commit.cleanup scissors &&
291 git reset --hard c3 &&
292 test_must_fail git merge --squash c7 &&
293 cat result.9z >file &&
294 git commit --no-edit -a &&
296 cat >expect <<-EOF &&
297 Squashed commit of the following:
299 $(git show -s c7)
301 # ------------------------ >8 ------------------------
302 # Do not modify or remove the line above.
303 # Everything below it will be ignored.
305 # Conflicts:
306 # file
308 git cat-file commit HEAD >raw &&
309 sed -e "1,/^$/d" raw >actual &&
310 test_cmp expect actual
313 test_debug 'git log --graph --decorate --oneline --all'
315 test_expect_success 'merge c1 with c2 and c3' '
316 git reset --hard c1 &&
317 test_tick &&
318 git merge c2 c3 &&
319 verify_merge file result.1-5-9 msg.1-5-9 &&
320 verify_parents $c1 $c2 $c3
323 test_debug 'git log --graph --decorate --oneline --all'
325 test_expect_success 'merges with --ff-only' '
326 git reset --hard c1 &&
327 test_tick &&
328 test_must_fail git merge --ff-only c2 &&
329 test_must_fail git merge --ff-only c3 &&
330 test_must_fail git merge --ff-only c2 c3 &&
331 git reset --hard c0 &&
332 git merge c3 &&
333 verify_head $c3
336 test_expect_success 'merges with merge.ff=only' '
337 git reset --hard c1 &&
338 test_tick &&
339 test_config merge.ff "only" &&
340 test_must_fail git merge c2 &&
341 test_must_fail git merge c3 &&
342 test_must_fail git merge c2 c3 &&
343 git reset --hard c0 &&
344 git merge c3 &&
345 verify_head $c3
348 test_expect_success 'merge c0 with c1 (no-commit)' '
349 git reset --hard c0 &&
350 git merge --no-commit c1 &&
351 verify_merge file result.1 &&
352 verify_head $c1
355 test_debug 'git log --graph --decorate --oneline --all'
357 test_expect_success 'merge c1 with c2 (no-commit)' '
358 git reset --hard c1 &&
359 git merge --no-commit c2 &&
360 verify_merge file result.1-5 &&
361 verify_head $c1 &&
362 verify_mergeheads $c2
365 test_debug 'git log --graph --decorate --oneline --all'
367 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
368 git reset --hard c1 &&
369 git merge --no-commit c2 c3 &&
370 verify_merge file result.1-5-9 &&
371 verify_head $c1 &&
372 verify_mergeheads $c2 $c3
375 test_debug 'git log --graph --decorate --oneline --all'
377 test_expect_success 'merge c0 with c1 (squash)' '
378 git reset --hard c0 &&
379 git merge --squash c1 &&
380 verify_merge file result.1 &&
381 verify_head $c0 &&
382 verify_no_mergehead &&
383 test_cmp squash.1 .git/SQUASH_MSG
386 test_debug 'git log --graph --decorate --oneline --all'
388 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
389 git reset --hard c0 &&
390 git merge --squash --ff-only c1 &&
391 verify_merge file result.1 &&
392 verify_head $c0 &&
393 verify_no_mergehead &&
394 test_cmp squash.1 .git/SQUASH_MSG
397 test_debug 'git log --graph --decorate --oneline --all'
399 test_expect_success 'merge c1 with c2 (squash)' '
400 git reset --hard c1 &&
401 git merge --squash c2 &&
402 verify_merge file result.1-5 &&
403 verify_head $c1 &&
404 verify_no_mergehead &&
405 test_cmp squash.1-5 .git/SQUASH_MSG
408 test_debug 'git log --graph --decorate --oneline --all'
410 test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
411 git reset --hard c1 &&
412 test_must_fail git merge --squash --ff-only c2
415 test_debug 'git log --graph --decorate --oneline --all'
417 test_expect_success 'merge c1 with c2 and c3 (squash)' '
418 git reset --hard c1 &&
419 git merge --squash c2 c3 &&
420 verify_merge file result.1-5-9 &&
421 verify_head $c1 &&
422 verify_no_mergehead &&
423 test_cmp squash.1-5-9 .git/SQUASH_MSG
426 test_debug 'git log --graph --decorate --oneline --all'
428 test_expect_success 'merge c1 with c2 (no-commit in config)' '
429 git reset --hard c1 &&
430 test_config branch.main.mergeoptions "--no-commit" &&
431 git merge c2 &&
432 verify_merge file result.1-5 &&
433 verify_head $c1 &&
434 verify_mergeheads $c2
437 test_debug 'git log --graph --decorate --oneline --all'
439 test_expect_success 'merge c1 with c2 (log in config)' '
440 git reset --hard c1 &&
441 git merge --log c2 &&
442 git show -s --pretty=tformat:%s%n%b >expect &&
444 test_config branch.main.mergeoptions "--log" &&
445 git reset --hard c1 &&
446 git merge c2 &&
447 git show -s --pretty=tformat:%s%n%b >actual &&
449 test_cmp expect actual
452 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
453 git reset --hard c1 &&
454 git merge c2 &&
455 git show -s --pretty=tformat:%s%n%b >expect &&
457 test_config branch.main.mergeoptions "--no-log" &&
458 test_config merge.log "true" &&
459 git reset --hard c1 &&
460 git merge c2 &&
461 git show -s --pretty=tformat:%s%n%b >actual &&
463 test_cmp expect actual
466 test_expect_success 'merge c1 with c2 (squash in config)' '
467 git reset --hard c1 &&
468 test_config branch.main.mergeoptions "--squash" &&
469 git merge c2 &&
470 verify_merge file result.1-5 &&
471 verify_head $c1 &&
472 verify_no_mergehead &&
473 test_cmp squash.1-5 .git/SQUASH_MSG
476 test_debug 'git log --graph --decorate --oneline --all'
478 test_expect_success 'override config option -n with --summary' '
479 git reset --hard c1 &&
480 test_config branch.main.mergeoptions "-n" &&
481 test_tick &&
482 git merge --summary c2 >diffstat.txt &&
483 verify_merge file result.1-5 msg.1-5 &&
484 verify_parents $c1 $c2 &&
485 if ! grep "^ file | *2 +-$" diffstat.txt
486 then
487 echo "[OOPS] diffstat was not generated with --summary"
488 false
492 test_expect_success 'override config option -n with --stat' '
493 git reset --hard c1 &&
494 test_config branch.main.mergeoptions "-n" &&
495 test_tick &&
496 git merge --stat c2 >diffstat.txt &&
497 verify_merge file result.1-5 msg.1-5 &&
498 verify_parents $c1 $c2 &&
499 if ! grep "^ file | *2 +-$" diffstat.txt
500 then
501 echo "[OOPS] diffstat was not generated with --stat"
502 false
506 test_debug 'git log --graph --decorate --oneline --all'
508 test_expect_success 'override config option --stat' '
509 git reset --hard c1 &&
510 test_config branch.main.mergeoptions "--stat" &&
511 test_tick &&
512 git merge -n c2 >diffstat.txt &&
513 verify_merge file result.1-5 msg.1-5 &&
514 verify_parents $c1 $c2 &&
515 if grep "^ file | *2 +-$" diffstat.txt
516 then
517 echo "[OOPS] diffstat was generated"
518 false
522 test_debug 'git log --graph --decorate --oneline --all'
524 test_expect_success 'merge c1 with c2 (override --no-commit)' '
525 git reset --hard c1 &&
526 test_config branch.main.mergeoptions "--no-commit" &&
527 test_tick &&
528 git merge --commit c2 &&
529 verify_merge file result.1-5 msg.1-5 &&
530 verify_parents $c1 $c2
533 test_debug 'git log --graph --decorate --oneline --all'
535 test_expect_success 'merge c1 with c2 (override --squash)' '
536 git reset --hard c1 &&
537 test_config branch.main.mergeoptions "--squash" &&
538 test_tick &&
539 git merge --no-squash c2 &&
540 verify_merge file result.1-5 msg.1-5 &&
541 verify_parents $c1 $c2
544 test_debug 'git log --graph --decorate --oneline --all'
546 test_expect_success 'merge c0 with c1 (no-ff)' '
547 git reset --hard c0 &&
548 test_tick &&
549 git merge --no-ff c1 &&
550 verify_merge file result.1 &&
551 verify_parents $c0 $c1
554 test_debug 'git log --graph --decorate --oneline --all'
556 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
557 git reset --hard c0 &&
558 test_config merge.ff "false" &&
559 test_tick &&
560 git merge c1 &&
561 verify_merge file result.1 &&
562 verify_parents $c0 $c1
564 test_debug 'git log --graph --decorate --oneline --all'
566 test_expect_success 'combine branch.main.mergeoptions with merge.ff' '
567 git reset --hard c0 &&
568 test_config branch.main.mergeoptions "--ff" &&
569 test_config merge.ff "false" &&
570 test_tick &&
571 git merge c1 &&
572 verify_merge file result.1 &&
573 verify_parents "$c0"
576 test_expect_success 'tolerate unknown values for merge.ff' '
577 git reset --hard c0 &&
578 test_config merge.ff "something-new" &&
579 test_tick &&
580 git merge c1 2>message &&
581 verify_head "$c1" &&
582 test_must_be_empty message
585 test_expect_success 'combining --squash and --no-ff is refused' '
586 git reset --hard c0 &&
587 test_must_fail git merge --squash --no-ff c1 &&
588 test_must_fail git merge --no-ff --squash c1
591 test_expect_success 'combining --squash and --commit is refused' '
592 git reset --hard c0 &&
593 test_must_fail git merge --squash --commit c1 &&
594 test_must_fail git merge --commit --squash c1
597 test_expect_success 'option --ff-only overwrites --no-ff' '
598 git merge --no-ff --ff-only c1 &&
599 test_must_fail git merge --no-ff --ff-only c2
602 test_expect_success 'option --no-ff overrides merge.ff=only config' '
603 git reset --hard c0 &&
604 test_config merge.ff only &&
605 git merge --no-ff c1
608 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
609 git reset --hard c0 &&
610 test_config branch.main.mergeoptions "--no-ff" &&
611 git merge --ff c1 &&
612 verify_merge file result.1 &&
613 verify_head $c1
616 test_expect_success 'merge log message' '
617 git reset --hard c0 &&
618 git merge --no-log c2 &&
619 git show -s --pretty=format:%b HEAD >msg.act &&
620 test_must_be_empty msg.act &&
622 git reset --hard c0 &&
623 test_config branch.main.mergeoptions "--no-ff" &&
624 git merge --no-log c2 &&
625 git show -s --pretty=format:%b HEAD >msg.act &&
626 test_must_be_empty msg.act &&
628 git merge --log c3 &&
629 git show -s --pretty=format:%b HEAD >msg.act &&
630 test_cmp msg.log msg.act &&
632 git reset --hard HEAD^ &&
633 test_config merge.log "yes" &&
634 git merge c3 &&
635 git show -s --pretty=format:%b HEAD >msg.act &&
636 test_cmp msg.log msg.act
639 test_debug 'git log --graph --decorate --oneline --all'
641 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
642 git reset --hard c1 &&
643 test_tick &&
644 git merge c0 c2 c0 c1 &&
645 verify_merge file result.1-5 &&
646 verify_parents $c1 $c2
649 test_debug 'git log --graph --decorate --oneline --all'
651 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
652 git reset --hard c1 &&
653 test_tick &&
654 git merge c0 c2 c0 c1 &&
655 verify_merge file result.1-5 &&
656 verify_parents $c1 $c2
659 test_debug 'git log --graph --decorate --oneline --all'
661 test_expect_success 'merge c1 with c1 and c2' '
662 git reset --hard c1 &&
663 test_tick &&
664 git merge c1 c2 &&
665 verify_merge file result.1-5 &&
666 verify_parents $c1 $c2
669 test_debug 'git log --graph --decorate --oneline --all'
671 test_expect_success 'merge fast-forward in a dirty tree' '
672 git reset --hard c0 &&
673 mv file file1 &&
674 cat file1 >file &&
675 rm -f file1 &&
676 git merge c2
679 test_debug 'git log --graph --decorate --oneline --all'
681 test_expect_success 'in-index merge' '
682 git reset --hard c0 &&
683 git merge --no-ff -s resolve c1 >out &&
684 test_grep "Wonderful." out &&
685 verify_parents $c0 $c1
688 test_debug 'git log --graph --decorate --oneline --all'
690 test_expect_success 'refresh the index before merging' '
691 git reset --hard c1 &&
692 cp file file.n && mv -f file.n file &&
693 git merge c3
696 test_expect_success 'merge with --autostash' '
697 git reset --hard c1 &&
698 git merge-file file file.orig file.9 &&
699 git merge --autostash c2 2>err &&
700 test_grep "Applied autostash." err &&
701 git show HEAD:file >merge-result &&
702 test_cmp result.1-5 merge-result &&
703 test_cmp result.1-5-9 file
706 test_expect_success 'merge with merge.autoStash' '
707 test_config merge.autoStash true &&
708 git reset --hard c1 &&
709 git merge-file file file.orig file.9 &&
710 git merge c2 2>err &&
711 test_grep "Applied autostash." err &&
712 git show HEAD:file >merge-result &&
713 test_cmp result.1-5 merge-result &&
714 test_cmp result.1-5-9 file
717 test_expect_success 'fast-forward merge with --autostash' '
718 git reset --hard c0 &&
719 git merge-file file file.orig file.5 &&
720 git merge --autostash c1 2>err &&
721 test_grep "Applied autostash." err &&
722 test_cmp result.1-5 file
725 test_expect_success 'failed fast-forward merge with --autostash' '
726 git reset --hard c0 &&
727 git merge-file file file.orig file.5 &&
728 cp file.5 other &&
729 test_when_finished "rm other" &&
730 test_must_fail git merge --autostash c1 2>err &&
731 test_grep "Applied autostash." err &&
732 test_cmp file.5 file
735 test_expect_success 'octopus merge with --autostash' '
736 git reset --hard c1 &&
737 git merge-file file file.orig file.3 &&
738 git merge --autostash c2 c3 2>err &&
739 test_grep "Applied autostash." err &&
740 git show HEAD:file >merge-result &&
741 test_cmp result.1-5-9 merge-result &&
742 test_cmp result.1-3-5-9 file
745 test_expect_success 'failed merge (exit 2) with --autostash' '
746 git reset --hard c1 &&
747 git merge-file file file.orig file.5 &&
748 test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
749 test_grep "Applied autostash." err &&
750 test_cmp result.1-5 file
753 test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
754 git reset --hard c3 &&
755 cp file.1 file &&
756 test_must_fail git merge --autostash c7 &&
757 git merge --abort 2>err &&
758 test_grep "Applied autostash." err &&
759 test_cmp file.1 file
762 test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
763 git reset --hard c1 &&
764 git merge-file file file.orig file.9 &&
765 git diff >expect &&
766 git merge --no-commit --autostash c2 &&
767 git stash show -p MERGE_AUTOSTASH >actual &&
768 test_cmp expect actual &&
769 git commit 2>err &&
770 test_grep "Applied autostash." err &&
771 git show HEAD:file >merge-result &&
772 test_cmp result.1-5 merge-result &&
773 test_cmp result.1-5-9 file
776 test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
777 git reset --hard c1 &&
778 git merge-file file file.orig file.9 &&
779 git diff >expect &&
780 git merge --no-commit --autostash c2 &&
781 git stash show -p MERGE_AUTOSTASH >actual &&
782 test_cmp expect actual &&
783 git merge --continue 2>err &&
784 test_grep "Applied autostash." err &&
785 git show HEAD:file >merge-result &&
786 test_cmp result.1-5 merge-result &&
787 test_cmp result.1-5-9 file
790 test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
791 git reset --hard c1 &&
792 git merge-file file file.orig file.9 &&
793 git diff >expect &&
794 git merge --no-commit --autostash c2 &&
795 git stash show -p MERGE_AUTOSTASH >actual &&
796 test_cmp expect actual &&
797 git merge --abort 2>err &&
798 test_grep "Applied autostash." err &&
799 git diff >actual &&
800 test_cmp expect actual
803 test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
804 git reset --hard c1 &&
805 git merge-file file file.orig file.9 &&
806 git diff >expect &&
807 git merge --no-commit --autostash c2 &&
808 git stash show -p MERGE_AUTOSTASH >actual &&
809 test_cmp expect actual &&
810 git reset --hard 2>err &&
811 test_grep "Autostash exists; creating a new stash entry." err &&
812 git diff --exit-code
815 test_expect_success 'quit merge with --no-commit and --autostash' '
816 git reset --hard c1 &&
817 git merge-file file file.orig file.9 &&
818 git diff >expect &&
819 git merge --no-commit --autostash c2 &&
820 git stash show -p MERGE_AUTOSTASH >actual &&
821 test_cmp expect actual &&
822 git diff HEAD >expect &&
823 git merge --quit 2>err &&
824 test_grep "Autostash exists; creating a new stash entry." err &&
825 git diff HEAD >actual &&
826 test_cmp expect actual
829 test_expect_success 'merge with conflicted --autostash changes' '
830 git reset --hard c1 &&
831 git merge-file file file.orig file.9y &&
832 git diff >expect &&
833 test_when_finished "test_might_fail git stash drop" &&
834 git merge --autostash c3 2>err &&
835 test_grep "Applying autostash resulted in conflicts." err &&
836 git show HEAD:file >merge-result &&
837 test_cmp result.1-9 merge-result &&
838 git stash show -p >actual &&
839 test_cmp expect actual
842 cat >expected.branch <<\EOF
843 Merge branch 'c5-branch' (early part)
845 cat >expected.tag <<\EOF
846 Merge commit 'c5~1'
849 test_expect_success 'merge early part of c2' '
850 git reset --hard c3 &&
851 echo c4 >c4.c &&
852 git add c4.c &&
853 git commit -m c4 &&
854 git tag c4 &&
855 echo c5 >c5.c &&
856 git add c5.c &&
857 git commit -m c5 &&
858 git tag c5 &&
859 git reset --hard c3 &&
860 echo c6 >c6.c &&
861 git add c6.c &&
862 git commit -m c6 &&
863 git tag c6 &&
864 git branch -f c5-branch c5 &&
865 git merge c5-branch~1 &&
866 git show -s --pretty=tformat:%s HEAD >actual.branch &&
867 git reset --keep HEAD^ &&
868 git merge c5~1 &&
869 git show -s --pretty=tformat:%s HEAD >actual.tag &&
870 test_cmp expected.branch actual.branch &&
871 test_cmp expected.tag actual.tag
874 test_debug 'git log --graph --decorate --oneline --all'
876 test_expect_success 'merge --no-ff --no-commit && commit' '
877 git reset --hard c0 &&
878 git merge --no-ff --no-commit c1 &&
879 EDITOR=: git commit &&
880 verify_parents $c0 $c1
883 test_debug 'git log --graph --decorate --oneline --all'
885 test_expect_success 'amending no-ff merge commit' '
886 EDITOR=: git commit --amend &&
887 verify_parents $c0 $c1
890 test_debug 'git log --graph --decorate --oneline --all'
892 cat >editor <<\EOF
893 #!/bin/sh
894 # Add a new message string that was not in the template
896 echo "Merge work done on the side branch c1"
897 echo
898 cat "$1"
899 ) >"$1.tmp" && mv "$1.tmp" "$1"
900 # strip comments and blank lines from end of message
901 sed -e '/^#/d' "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' >expected
903 chmod 755 editor
905 test_expect_success 'merge --no-ff --edit' '
906 git reset --hard c0 &&
907 EDITOR=./editor git merge --no-ff --edit c1 &&
908 verify_parents $c0 $c1 &&
909 git cat-file commit HEAD >raw &&
910 grep "work done on the side branch" raw &&
911 sed "1,/^$/d" >actual raw &&
912 test_cmp expected actual
915 test_expect_success 'merge annotated/signed tag w/o tracking' '
916 test_when_finished "rm -rf dst; git tag -d anno1" &&
917 git tag -a -m "anno c1" anno1 c1 &&
918 git init dst &&
919 git rev-parse c1 >dst/expect &&
921 # c0 fast-forwards to c1 but because this repository
922 # is not a "downstream" whose refs/tags follows along
923 # tag from the "upstream", this pull defaults to --no-ff
924 cd dst &&
925 git pull .. c0 &&
926 git pull .. anno1 &&
927 git rev-parse HEAD^2 >actual &&
928 test_cmp expect actual
932 test_expect_success 'merge annotated/signed tag w/ tracking' '
933 test_when_finished "rm -rf dst; git tag -d anno1" &&
934 git tag -a -m "anno c1" anno1 c1 &&
935 git init dst &&
936 git rev-parse c1 >dst/expect &&
938 # c0 fast-forwards to c1 and because this repository
939 # is a "downstream" whose refs/tags follows along
940 # tag from the "upstream", this pull defaults to --ff
941 cd dst &&
942 git remote add origin .. &&
943 git pull origin c0 &&
944 git fetch origin &&
945 git merge anno1 &&
946 git rev-parse HEAD >actual &&
947 test_cmp expect actual
951 test_expect_success GPG 'merge --ff-only tag' '
952 git reset --hard c0 &&
953 git commit --allow-empty -m "A newer commit" &&
954 git tag -s -m "A newer commit" signed &&
955 git reset --hard c0 &&
957 git merge --ff-only signed &&
958 git rev-parse signed^0 >expect &&
959 git rev-parse HEAD >actual &&
960 test_cmp expect actual
963 test_expect_success GPG 'merge --no-edit tag should skip editor' '
964 git reset --hard c0 &&
965 git commit --allow-empty -m "A newer commit" &&
966 git tag -f -s -m "A newer commit" signed &&
967 git reset --hard c0 &&
969 EDITOR=false git merge --no-edit --no-ff signed &&
970 git rev-parse signed^0 >expect &&
971 git rev-parse HEAD^2 >actual &&
972 test_cmp expect actual
975 test_expect_success 'set up mod-256 conflict scenario' '
976 # 256 near-identical stanzas...
977 for i in $(test_seq 1 256); do
978 for j in 1 2 3 4 5; do
979 echo $i-$j || return 1
980 done
981 done >file &&
982 git add file &&
983 git commit -m base &&
985 # one side changes the first line of each to "main"
986 sed s/-1/-main/ file >tmp &&
987 mv tmp file &&
988 git commit -am main &&
990 # and the other to "side"; merging the two will
991 # yield 256 separate conflicts
992 git checkout -b side HEAD^ &&
993 sed s/-1/-side/ file >tmp &&
994 mv tmp file &&
995 git commit -am side
998 test_expect_success 'merge detects mod-256 conflicts (recursive)' '
999 git reset --hard &&
1000 test_must_fail git merge -s recursive main
1003 test_expect_success 'merge detects mod-256 conflicts (resolve)' '
1004 git reset --hard &&
1005 test_must_fail git merge -s resolve main
1008 test_expect_success 'merge nothing into void' '
1009 git init void &&
1011 cd void &&
1012 git remote add up .. &&
1013 git fetch up &&
1014 test_must_fail git merge FETCH_HEAD
1018 test_expect_success 'merge can be completed with --continue' '
1019 git reset --hard c0 &&
1020 git merge --no-ff --no-commit c1 &&
1021 git merge --continue &&
1022 verify_parents $c0 $c1
1025 write_script .git/FAKE_EDITOR <<EOF
1026 # kill -TERM command added below.
1029 test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue' '
1030 git reset --hard c0 &&
1031 ! "$SHELL_PATH" -c '\''
1032 echo kill -TERM $$ >>.git/FAKE_EDITOR
1033 GIT_EDITOR=.git/FAKE_EDITOR
1034 export GIT_EDITOR
1035 exec git merge --no-ff --edit c1'\'' &&
1036 git merge --continue &&
1037 verify_parents $c0 $c1
1040 test_expect_success 'merge --quit' '
1041 git init merge-quit &&
1043 cd merge-quit &&
1044 test_commit base &&
1045 echo one >>base.t &&
1046 git commit -am one &&
1047 git branch one &&
1048 git checkout base &&
1049 echo two >>base.t &&
1050 git commit -am two &&
1051 test_must_fail git -c rerere.enabled=true merge one &&
1052 test_path_is_file .git/MERGE_HEAD &&
1053 test_path_is_file .git/MERGE_MODE &&
1054 test_path_is_file .git/MERGE_MSG &&
1055 git rerere status >rerere.before &&
1056 git merge --quit &&
1057 test_path_is_missing .git/MERGE_HEAD &&
1058 test_path_is_missing .git/MERGE_MODE &&
1059 test_path_is_missing .git/MERGE_MSG &&
1060 git rerere status >rerere.after &&
1061 test_must_be_empty rerere.after &&
1062 ! test_cmp rerere.after rerere.before
1066 test_expect_success 'merge suggests matching remote refname' '
1067 git commit --allow-empty -m not-local &&
1068 git update-ref refs/remotes/origin/not-local HEAD &&
1069 git reset --hard HEAD^ &&
1071 # This is white-box testing hackery; we happen to know
1072 # that reading packed refs is more picky about the memory
1073 # ownership of strings we pass to for_each_ref() callbacks.
1074 git pack-refs --all --prune &&
1076 test_must_fail git merge not-local 2>stderr &&
1077 grep origin/not-local stderr
1080 test_expect_success 'suggested names are not ambiguous' '
1081 git update-ref refs/heads/origin/not-local HEAD &&
1082 test_must_fail git merge not-local 2>stderr &&
1083 grep remotes/origin/not-local stderr
1086 test_done