merge: apply autostash if merge strategy fails
[git/debian.git] / t / t7600-merge.sh
blob2ef39d3088e80690781e43c671ab1de11beed55f
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
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_i18ngrep "[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 c3 with c7 with commit.cleanup = scissors' '
259 git config commit.cleanup scissors &&
260 git reset --hard c3 &&
261 test_must_fail git merge c7 &&
262 cat result.9z >file &&
263 git commit --no-edit -a &&
265 cat >expect <<-\EOF &&
266 Merge tag '"'"'c7'"'"'
268 # ------------------------ >8 ------------------------
269 # Do not modify or remove the line above.
270 # Everything below it will be ignored.
272 # Conflicts:
273 # file
275 git cat-file commit HEAD >raw &&
276 sed -e "1,/^$/d" raw >actual &&
277 test_cmp expect actual
280 test_expect_success 'merge c3 with c7 with --squash commit.cleanup = scissors' '
281 git config commit.cleanup scissors &&
282 git reset --hard c3 &&
283 test_must_fail git merge --squash c7 &&
284 cat result.9z >file &&
285 git commit --no-edit -a &&
287 cat >expect <<-EOF &&
288 Squashed commit of the following:
290 $(git show -s c7)
292 # ------------------------ >8 ------------------------
293 # Do not modify or remove the line above.
294 # Everything below it will be ignored.
296 # Conflicts:
297 # file
299 git cat-file commit HEAD >raw &&
300 sed -e "1,/^$/d" raw >actual &&
301 test_cmp expect actual
304 test_debug 'git log --graph --decorate --oneline --all'
306 test_expect_success 'merge c1 with c2 and c3' '
307 git reset --hard c1 &&
308 test_tick &&
309 git merge c2 c3 &&
310 verify_merge file result.1-5-9 msg.1-5-9 &&
311 verify_parents $c1 $c2 $c3
314 test_debug 'git log --graph --decorate --oneline --all'
316 test_expect_success 'merges with --ff-only' '
317 git reset --hard c1 &&
318 test_tick &&
319 test_must_fail git merge --ff-only c2 &&
320 test_must_fail git merge --ff-only c3 &&
321 test_must_fail git merge --ff-only c2 c3 &&
322 git reset --hard c0 &&
323 git merge c3 &&
324 verify_head $c3
327 test_expect_success 'merges with merge.ff=only' '
328 git reset --hard c1 &&
329 test_tick &&
330 test_config merge.ff "only" &&
331 test_must_fail git merge c2 &&
332 test_must_fail git merge c3 &&
333 test_must_fail git merge c2 c3 &&
334 git reset --hard c0 &&
335 git merge c3 &&
336 verify_head $c3
339 test_expect_success 'merge c0 with c1 (no-commit)' '
340 git reset --hard c0 &&
341 git merge --no-commit c1 &&
342 verify_merge file result.1 &&
343 verify_head $c1
346 test_debug 'git log --graph --decorate --oneline --all'
348 test_expect_success 'merge c1 with c2 (no-commit)' '
349 git reset --hard c1 &&
350 git merge --no-commit c2 &&
351 verify_merge file result.1-5 &&
352 verify_head $c1 &&
353 verify_mergeheads $c2
356 test_debug 'git log --graph --decorate --oneline --all'
358 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
359 git reset --hard c1 &&
360 git merge --no-commit c2 c3 &&
361 verify_merge file result.1-5-9 &&
362 verify_head $c1 &&
363 verify_mergeheads $c2 $c3
366 test_debug 'git log --graph --decorate --oneline --all'
368 test_expect_success 'merge c0 with c1 (squash)' '
369 git reset --hard c0 &&
370 git merge --squash c1 &&
371 verify_merge file result.1 &&
372 verify_head $c0 &&
373 verify_no_mergehead &&
374 test_cmp squash.1 .git/SQUASH_MSG
377 test_debug 'git log --graph --decorate --oneline --all'
379 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
380 git reset --hard c0 &&
381 git merge --squash --ff-only c1 &&
382 verify_merge file result.1 &&
383 verify_head $c0 &&
384 verify_no_mergehead &&
385 test_cmp squash.1 .git/SQUASH_MSG
388 test_debug 'git log --graph --decorate --oneline --all'
390 test_expect_success 'merge c1 with c2 (squash)' '
391 git reset --hard c1 &&
392 git merge --squash c2 &&
393 verify_merge file result.1-5 &&
394 verify_head $c1 &&
395 verify_no_mergehead &&
396 test_cmp squash.1-5 .git/SQUASH_MSG
399 test_debug 'git log --graph --decorate --oneline --all'
401 test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
402 git reset --hard c1 &&
403 test_must_fail git merge --squash --ff-only c2
406 test_debug 'git log --graph --decorate --oneline --all'
408 test_expect_success 'merge c1 with c2 and c3 (squash)' '
409 git reset --hard c1 &&
410 git merge --squash c2 c3 &&
411 verify_merge file result.1-5-9 &&
412 verify_head $c1 &&
413 verify_no_mergehead &&
414 test_cmp squash.1-5-9 .git/SQUASH_MSG
417 test_debug 'git log --graph --decorate --oneline --all'
419 test_expect_success 'merge c1 with c2 (no-commit in config)' '
420 git reset --hard c1 &&
421 test_config branch.main.mergeoptions "--no-commit" &&
422 git merge c2 &&
423 verify_merge file result.1-5 &&
424 verify_head $c1 &&
425 verify_mergeheads $c2
428 test_debug 'git log --graph --decorate --oneline --all'
430 test_expect_success 'merge c1 with c2 (log in config)' '
431 git reset --hard c1 &&
432 git merge --log c2 &&
433 git show -s --pretty=tformat:%s%n%b >expect &&
435 test_config branch.main.mergeoptions "--log" &&
436 git reset --hard c1 &&
437 git merge c2 &&
438 git show -s --pretty=tformat:%s%n%b >actual &&
440 test_cmp expect actual
443 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
444 git reset --hard c1 &&
445 git merge c2 &&
446 git show -s --pretty=tformat:%s%n%b >expect &&
448 test_config branch.main.mergeoptions "--no-log" &&
449 test_config merge.log "true" &&
450 git reset --hard c1 &&
451 git merge c2 &&
452 git show -s --pretty=tformat:%s%n%b >actual &&
454 test_cmp expect actual
457 test_expect_success 'merge c1 with c2 (squash in config)' '
458 git reset --hard c1 &&
459 test_config branch.main.mergeoptions "--squash" &&
460 git merge c2 &&
461 verify_merge file result.1-5 &&
462 verify_head $c1 &&
463 verify_no_mergehead &&
464 test_cmp squash.1-5 .git/SQUASH_MSG
467 test_debug 'git log --graph --decorate --oneline --all'
469 test_expect_success 'override config option -n with --summary' '
470 git reset --hard c1 &&
471 test_config branch.main.mergeoptions "-n" &&
472 test_tick &&
473 git merge --summary c2 >diffstat.txt &&
474 verify_merge file result.1-5 msg.1-5 &&
475 verify_parents $c1 $c2 &&
476 if ! grep "^ file | *2 +-$" diffstat.txt
477 then
478 echo "[OOPS] diffstat was not generated with --summary"
479 false
483 test_expect_success 'override config option -n with --stat' '
484 git reset --hard c1 &&
485 test_config branch.main.mergeoptions "-n" &&
486 test_tick &&
487 git merge --stat c2 >diffstat.txt &&
488 verify_merge file result.1-5 msg.1-5 &&
489 verify_parents $c1 $c2 &&
490 if ! grep "^ file | *2 +-$" diffstat.txt
491 then
492 echo "[OOPS] diffstat was not generated with --stat"
493 false
497 test_debug 'git log --graph --decorate --oneline --all'
499 test_expect_success 'override config option --stat' '
500 git reset --hard c1 &&
501 test_config branch.main.mergeoptions "--stat" &&
502 test_tick &&
503 git merge -n c2 >diffstat.txt &&
504 verify_merge file result.1-5 msg.1-5 &&
505 verify_parents $c1 $c2 &&
506 if grep "^ file | *2 +-$" diffstat.txt
507 then
508 echo "[OOPS] diffstat was generated"
509 false
513 test_debug 'git log --graph --decorate --oneline --all'
515 test_expect_success 'merge c1 with c2 (override --no-commit)' '
516 git reset --hard c1 &&
517 test_config branch.main.mergeoptions "--no-commit" &&
518 test_tick &&
519 git merge --commit c2 &&
520 verify_merge file result.1-5 msg.1-5 &&
521 verify_parents $c1 $c2
524 test_debug 'git log --graph --decorate --oneline --all'
526 test_expect_success 'merge c1 with c2 (override --squash)' '
527 git reset --hard c1 &&
528 test_config branch.main.mergeoptions "--squash" &&
529 test_tick &&
530 git merge --no-squash c2 &&
531 verify_merge file result.1-5 msg.1-5 &&
532 verify_parents $c1 $c2
535 test_debug 'git log --graph --decorate --oneline --all'
537 test_expect_success 'merge c0 with c1 (no-ff)' '
538 git reset --hard c0 &&
539 test_tick &&
540 git merge --no-ff c1 &&
541 verify_merge file result.1 &&
542 verify_parents $c0 $c1
545 test_debug 'git log --graph --decorate --oneline --all'
547 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
548 git reset --hard c0 &&
549 test_config merge.ff "false" &&
550 test_tick &&
551 git merge c1 &&
552 verify_merge file result.1 &&
553 verify_parents $c0 $c1
555 test_debug 'git log --graph --decorate --oneline --all'
557 test_expect_success 'combine branch.main.mergeoptions with merge.ff' '
558 git reset --hard c0 &&
559 test_config branch.main.mergeoptions "--ff" &&
560 test_config merge.ff "false" &&
561 test_tick &&
562 git merge c1 &&
563 verify_merge file result.1 &&
564 verify_parents "$c0"
567 test_expect_success 'tolerate unknown values for merge.ff' '
568 git reset --hard c0 &&
569 test_config merge.ff "something-new" &&
570 test_tick &&
571 git merge c1 2>message &&
572 verify_head "$c1" &&
573 test_must_be_empty message
576 test_expect_success 'combining --squash and --no-ff is refused' '
577 git reset --hard c0 &&
578 test_must_fail git merge --squash --no-ff c1 &&
579 test_must_fail git merge --no-ff --squash c1
582 test_expect_success 'combining --squash and --commit is refused' '
583 git reset --hard c0 &&
584 test_must_fail git merge --squash --commit c1 &&
585 test_must_fail git merge --commit --squash c1
588 test_expect_success 'option --ff-only overwrites --no-ff' '
589 git merge --no-ff --ff-only c1 &&
590 test_must_fail git merge --no-ff --ff-only c2
593 test_expect_success 'option --no-ff overrides merge.ff=only config' '
594 git reset --hard c0 &&
595 test_config merge.ff only &&
596 git merge --no-ff c1
599 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
600 git reset --hard c0 &&
601 test_config branch.main.mergeoptions "--no-ff" &&
602 git merge --ff c1 &&
603 verify_merge file result.1 &&
604 verify_head $c1
607 test_expect_success 'merge log message' '
608 git reset --hard c0 &&
609 git merge --no-log c2 &&
610 git show -s --pretty=format:%b HEAD >msg.act &&
611 test_must_be_empty msg.act &&
613 git reset --hard c0 &&
614 test_config branch.main.mergeoptions "--no-ff" &&
615 git merge --no-log c2 &&
616 git show -s --pretty=format:%b HEAD >msg.act &&
617 test_must_be_empty msg.act &&
619 git merge --log c3 &&
620 git show -s --pretty=format:%b HEAD >msg.act &&
621 test_cmp msg.log msg.act &&
623 git reset --hard HEAD^ &&
624 test_config merge.log "yes" &&
625 git merge c3 &&
626 git show -s --pretty=format:%b HEAD >msg.act &&
627 test_cmp msg.log msg.act
630 test_debug 'git log --graph --decorate --oneline --all'
632 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
633 git reset --hard c1 &&
634 test_tick &&
635 git merge c0 c2 c0 c1 &&
636 verify_merge file result.1-5 &&
637 verify_parents $c1 $c2
640 test_debug 'git log --graph --decorate --oneline --all'
642 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
643 git reset --hard c1 &&
644 test_tick &&
645 git merge c0 c2 c0 c1 &&
646 verify_merge file result.1-5 &&
647 verify_parents $c1 $c2
650 test_debug 'git log --graph --decorate --oneline --all'
652 test_expect_success 'merge c1 with c1 and c2' '
653 git reset --hard c1 &&
654 test_tick &&
655 git merge c1 c2 &&
656 verify_merge file result.1-5 &&
657 verify_parents $c1 $c2
660 test_debug 'git log --graph --decorate --oneline --all'
662 test_expect_success 'merge fast-forward in a dirty tree' '
663 git reset --hard c0 &&
664 mv file file1 &&
665 cat file1 >file &&
666 rm -f file1 &&
667 git merge c2
670 test_debug 'git log --graph --decorate --oneline --all'
672 test_expect_success 'in-index merge' '
673 git reset --hard c0 &&
674 git merge --no-ff -s resolve c1 >out &&
675 test_i18ngrep "Wonderful." out &&
676 verify_parents $c0 $c1
679 test_debug 'git log --graph --decorate --oneline --all'
681 test_expect_success 'refresh the index before merging' '
682 git reset --hard c1 &&
683 cp file file.n && mv -f file.n file &&
684 git merge c3
687 test_expect_success 'merge with --autostash' '
688 git reset --hard c1 &&
689 git merge-file file file.orig file.9 &&
690 git merge --autostash c2 2>err &&
691 test_i18ngrep "Applied autostash." err &&
692 git show HEAD:file >merge-result &&
693 test_cmp result.1-5 merge-result &&
694 test_cmp result.1-5-9 file
697 test_expect_success 'merge with merge.autoStash' '
698 test_config merge.autoStash true &&
699 git reset --hard c1 &&
700 git merge-file file file.orig file.9 &&
701 git merge c2 2>err &&
702 test_i18ngrep "Applied autostash." err &&
703 git show HEAD:file >merge-result &&
704 test_cmp result.1-5 merge-result &&
705 test_cmp result.1-5-9 file
708 test_expect_success 'fast-forward merge with --autostash' '
709 git reset --hard c0 &&
710 git merge-file file file.orig file.5 &&
711 git merge --autostash c1 2>err &&
712 test_i18ngrep "Applied autostash." err &&
713 test_cmp result.1-5 file
716 test_expect_success 'failed fast-forward merge with --autostash' '
717 git reset --hard c0 &&
718 git merge-file file file.orig file.5 &&
719 cp file.5 other &&
720 test_must_fail git merge --autostash c1 2>err &&
721 test_i18ngrep "Applied autostash." err &&
722 test_cmp file.5 file
725 test_expect_success 'octopus merge with --autostash' '
726 git reset --hard c1 &&
727 git merge-file file file.orig file.3 &&
728 git merge --autostash c2 c3 2>err &&
729 test_i18ngrep "Applied autostash." err &&
730 git show HEAD:file >merge-result &&
731 test_cmp result.1-5-9 merge-result &&
732 test_cmp result.1-3-5-9 file
735 test_expect_success 'failed merge (exit 2) with --autostash' '
736 git reset --hard c1 &&
737 git merge-file file file.orig file.5 &&
738 test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
739 test_i18ngrep "Applied autostash." err &&
740 test_cmp result.1-5 file
743 test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
744 git reset --hard c3 &&
745 cp file.1 file &&
746 test_must_fail git merge --autostash c7 &&
747 git merge --abort 2>err &&
748 test_i18ngrep "Applied autostash." err &&
749 test_cmp file.1 file
752 test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
753 git reset --hard c1 &&
754 git merge-file file file.orig file.9 &&
755 git diff >expect &&
756 git merge --no-commit --autostash c2 &&
757 git stash show -p MERGE_AUTOSTASH >actual &&
758 test_cmp expect actual &&
759 git commit 2>err &&
760 test_i18ngrep "Applied autostash." err &&
761 git show HEAD:file >merge-result &&
762 test_cmp result.1-5 merge-result &&
763 test_cmp result.1-5-9 file
766 test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
767 git reset --hard c1 &&
768 git merge-file file file.orig file.9 &&
769 git diff >expect &&
770 git merge --no-commit --autostash c2 &&
771 git stash show -p MERGE_AUTOSTASH >actual &&
772 test_cmp expect actual &&
773 git merge --continue 2>err &&
774 test_i18ngrep "Applied autostash." err &&
775 git show HEAD:file >merge-result &&
776 test_cmp result.1-5 merge-result &&
777 test_cmp result.1-5-9 file
780 test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
781 git reset --hard c1 &&
782 git merge-file file file.orig file.9 &&
783 git diff >expect &&
784 git merge --no-commit --autostash c2 &&
785 git stash show -p MERGE_AUTOSTASH >actual &&
786 test_cmp expect actual &&
787 git merge --abort 2>err &&
788 test_i18ngrep "Applied autostash." err &&
789 git diff >actual &&
790 test_cmp expect actual
793 test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
794 git reset --hard c1 &&
795 git merge-file file file.orig file.9 &&
796 git diff >expect &&
797 git merge --no-commit --autostash c2 &&
798 git stash show -p MERGE_AUTOSTASH >actual &&
799 test_cmp expect actual &&
800 git reset --hard 2>err &&
801 test_i18ngrep "Autostash exists; creating a new stash entry." err &&
802 git diff --exit-code
805 test_expect_success 'quit merge with --no-commit and --autostash' '
806 git reset --hard c1 &&
807 git merge-file file file.orig file.9 &&
808 git diff >expect &&
809 git merge --no-commit --autostash c2 &&
810 git stash show -p MERGE_AUTOSTASH >actual &&
811 test_cmp expect actual &&
812 git diff HEAD >expect &&
813 git merge --quit 2>err &&
814 test_i18ngrep "Autostash exists; creating a new stash entry." err &&
815 git diff HEAD >actual &&
816 test_cmp expect actual
819 test_expect_success 'merge with conflicted --autostash changes' '
820 git reset --hard c1 &&
821 git merge-file file file.orig file.9y &&
822 git diff >expect &&
823 test_when_finished "test_might_fail git stash drop" &&
824 git merge --autostash c3 2>err &&
825 test_i18ngrep "Applying autostash resulted in conflicts." err &&
826 git show HEAD:file >merge-result &&
827 test_cmp result.1-9 merge-result &&
828 git stash show -p >actual &&
829 test_cmp expect actual
832 cat >expected.branch <<\EOF
833 Merge branch 'c5-branch' (early part)
835 cat >expected.tag <<\EOF
836 Merge commit 'c5~1'
839 test_expect_success 'merge early part of c2' '
840 git reset --hard c3 &&
841 echo c4 >c4.c &&
842 git add c4.c &&
843 git commit -m c4 &&
844 git tag c4 &&
845 echo c5 >c5.c &&
846 git add c5.c &&
847 git commit -m c5 &&
848 git tag c5 &&
849 git reset --hard c3 &&
850 echo c6 >c6.c &&
851 git add c6.c &&
852 git commit -m c6 &&
853 git tag c6 &&
854 git branch -f c5-branch c5 &&
855 git merge c5-branch~1 &&
856 git show -s --pretty=tformat:%s HEAD >actual.branch &&
857 git reset --keep HEAD^ &&
858 git merge c5~1 &&
859 git show -s --pretty=tformat:%s HEAD >actual.tag &&
860 test_cmp expected.branch actual.branch &&
861 test_cmp expected.tag actual.tag
864 test_debug 'git log --graph --decorate --oneline --all'
866 test_expect_success 'merge --no-ff --no-commit && commit' '
867 git reset --hard c0 &&
868 git merge --no-ff --no-commit c1 &&
869 EDITOR=: git commit &&
870 verify_parents $c0 $c1
873 test_debug 'git log --graph --decorate --oneline --all'
875 test_expect_success 'amending no-ff merge commit' '
876 EDITOR=: git commit --amend &&
877 verify_parents $c0 $c1
880 test_debug 'git log --graph --decorate --oneline --all'
882 cat >editor <<\EOF
883 #!/bin/sh
884 # Add a new message string that was not in the template
886 echo "Merge work done on the side branch c1"
887 echo
888 cat "$1"
889 ) >"$1.tmp" && mv "$1.tmp" "$1"
890 # strip comments and blank lines from end of message
891 sed -e '/^#/d' "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' >expected
893 chmod 755 editor
895 test_expect_success 'merge --no-ff --edit' '
896 git reset --hard c0 &&
897 EDITOR=./editor git merge --no-ff --edit c1 &&
898 verify_parents $c0 $c1 &&
899 git cat-file commit HEAD >raw &&
900 grep "work done on the side branch" raw &&
901 sed "1,/^$/d" >actual raw &&
902 test_cmp expected actual
905 test_expect_success 'merge annotated/signed tag w/o tracking' '
906 test_when_finished "rm -rf dst; git tag -d anno1" &&
907 git tag -a -m "anno c1" anno1 c1 &&
908 git init dst &&
909 git rev-parse c1 >dst/expect &&
911 # c0 fast-forwards to c1 but because this repository
912 # is not a "downstream" whose refs/tags follows along
913 # tag from the "upstream", this pull defaults to --no-ff
914 cd dst &&
915 git pull .. c0 &&
916 git pull .. anno1 &&
917 git rev-parse HEAD^2 >actual &&
918 test_cmp expect actual
922 test_expect_success 'merge annotated/signed tag w/ tracking' '
923 test_when_finished "rm -rf dst; git tag -d anno1" &&
924 git tag -a -m "anno c1" anno1 c1 &&
925 git init dst &&
926 git rev-parse c1 >dst/expect &&
928 # c0 fast-forwards to c1 and because this repository
929 # is a "downstream" whose refs/tags follows along
930 # tag from the "upstream", this pull defaults to --ff
931 cd dst &&
932 git remote add origin .. &&
933 git pull origin c0 &&
934 git fetch origin &&
935 git merge anno1 &&
936 git rev-parse HEAD >actual &&
937 test_cmp expect actual
941 test_expect_success GPG 'merge --ff-only tag' '
942 git reset --hard c0 &&
943 git commit --allow-empty -m "A newer commit" &&
944 git tag -s -m "A newer commit" signed &&
945 git reset --hard c0 &&
947 git merge --ff-only signed &&
948 git rev-parse signed^0 >expect &&
949 git rev-parse HEAD >actual &&
950 test_cmp expect actual
953 test_expect_success GPG 'merge --no-edit tag should skip editor' '
954 git reset --hard c0 &&
955 git commit --allow-empty -m "A newer commit" &&
956 git tag -f -s -m "A newer commit" signed &&
957 git reset --hard c0 &&
959 EDITOR=false git merge --no-edit --no-ff signed &&
960 git rev-parse signed^0 >expect &&
961 git rev-parse HEAD^2 >actual &&
962 test_cmp expect actual
965 test_expect_success 'set up mod-256 conflict scenario' '
966 # 256 near-identical stanzas...
967 for i in $(test_seq 1 256); do
968 for j in 1 2 3 4 5; do
969 echo $i-$j
970 done
971 done >file &&
972 git add file &&
973 git commit -m base &&
975 # one side changes the first line of each to "main"
976 sed s/-1/-main/ file >tmp &&
977 mv tmp file &&
978 git commit -am main &&
980 # and the other to "side"; merging the two will
981 # yield 256 separate conflicts
982 git checkout -b side HEAD^ &&
983 sed s/-1/-side/ file >tmp &&
984 mv tmp file &&
985 git commit -am side
988 test_expect_success 'merge detects mod-256 conflicts (recursive)' '
989 git reset --hard &&
990 test_must_fail git merge -s recursive main
993 test_expect_success 'merge detects mod-256 conflicts (resolve)' '
994 git reset --hard &&
995 test_must_fail git merge -s resolve main
998 test_expect_success 'merge nothing into void' '
999 git init void &&
1001 cd void &&
1002 git remote add up .. &&
1003 git fetch up &&
1004 test_must_fail git merge FETCH_HEAD
1008 test_expect_success 'merge can be completed with --continue' '
1009 git reset --hard c0 &&
1010 git merge --no-ff --no-commit c1 &&
1011 git merge --continue &&
1012 verify_parents $c0 $c1
1015 write_script .git/FAKE_EDITOR <<EOF
1016 # kill -TERM command added below.
1019 test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue' '
1020 git reset --hard c0 &&
1021 ! "$SHELL_PATH" -c '\''
1022 echo kill -TERM $$ >>.git/FAKE_EDITOR
1023 GIT_EDITOR=.git/FAKE_EDITOR
1024 export GIT_EDITOR
1025 exec git merge --no-ff --edit c1'\'' &&
1026 git merge --continue &&
1027 verify_parents $c0 $c1
1030 test_expect_success 'merge --quit' '
1031 git init merge-quit &&
1033 cd merge-quit &&
1034 test_commit base &&
1035 echo one >>base.t &&
1036 git commit -am one &&
1037 git branch one &&
1038 git checkout base &&
1039 echo two >>base.t &&
1040 git commit -am two &&
1041 test_must_fail git -c rerere.enabled=true merge one &&
1042 test_path_is_file .git/MERGE_HEAD &&
1043 test_path_is_file .git/MERGE_MODE &&
1044 test_path_is_file .git/MERGE_MSG &&
1045 git rerere status >rerere.before &&
1046 git merge --quit &&
1047 test_path_is_missing .git/MERGE_HEAD &&
1048 test_path_is_missing .git/MERGE_MODE &&
1049 test_path_is_missing .git/MERGE_MSG &&
1050 git rerere status >rerere.after &&
1051 test_must_be_empty rerere.after &&
1052 ! test_cmp rerere.after rerere.before
1056 test_expect_success 'merge suggests matching remote refname' '
1057 git commit --allow-empty -m not-local &&
1058 git update-ref refs/remotes/origin/not-local HEAD &&
1059 git reset --hard HEAD^ &&
1061 # This is white-box testing hackery; we happen to know
1062 # that reading packed refs is more picky about the memory
1063 # ownership of strings we pass to for_each_ref() callbacks.
1064 git pack-refs --all --prune &&
1066 test_must_fail git merge not-local 2>stderr &&
1067 grep origin/not-local stderr
1070 test_expect_success 'suggested names are not ambiguous' '
1071 git update-ref refs/heads/origin/not-local HEAD &&
1072 test_must_fail git merge not-local 2>stderr &&
1073 grep remotes/origin/not-local stderr
1076 test_done