3 # Copyright (c) 2007 Lars Hjemli
6 test_description='git merge
8 Testing basic merge operations/option parsing.
17 * [master] Merge commit 'c1'
19 - [master] Merge commit 'c1'
26 +++++++* [c0] commit 0
30 . "$TEST_DIRECTORY"/lib-gpg.sh
32 printf '%s\n' 1 2 3 4 5 6 7 8 9 >file
33 printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >file.1
34 printf '%s\n' 1 2 3 4 '5 X' 6 7 8 9 >file.5
35 printf '%s\n' 1 2 3 4 5 6 7 8 '9 X' >file.9
36 printf '%s\n' 1 2 3 4 5 6 7 8 '9 Y' >file.9y
37 printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >result.1
38 printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
39 printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
40 printf '%s\n' 1 2 3 4 5 6 7 8 '9 Z' >result.9z
43 create_merge_msgs () {
44 echo "Merge tag 'c2'" >msg.1-5 &&
45 echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
47 echo "Squashed commit of the following:" &&
49 git log --no-merges ^HEAD c1
52 echo "Squashed commit of the following:" &&
54 git log --no-merges ^HEAD c2
57 echo "Squashed commit of the following:" &&
59 git log --no-merges ^HEAD c2 c3
71 git update-index --refresh &&
72 git diff --exit-code &&
75 git show -s --pretty=tformat:%s HEAD >msg.act &&
81 echo "$1" >head.expected &&
82 git rev-parse HEAD >head.actual &&
83 test_cmp head.expected head.actual
87 printf '%s\n' "$@" >parents.expected &&
92 git rev-parse HEAD^$i >>parents.actual &&
96 test_must_fail git rev-parse --verify "HEAD^$i" &&
97 test_cmp parents.expected parents.actual
100 verify_mergeheads () {
101 printf '%s\n' "$@" >mergehead.expected &&
105 done <.git/MERGE_HEAD >mergehead.actual &&
106 test_cmp mergehead.expected mergehead.actual
109 verify_no_mergehead () {
110 ! test -e .git/MERGE_HEAD
113 test_expect_success 'setup' '
116 git commit -m "commit 0" &&
118 c0=$(git rev-parse HEAD) &&
122 git commit -m "commit 1" &&
124 c1=$(git rev-parse HEAD) &&
125 git reset --hard "$c0" &&
129 git commit -m "commit 2" &&
131 c2=$(git rev-parse HEAD) &&
132 git reset --hard "$c0" &&
136 git commit -m "commit 7" &&
138 git reset --hard "$c0" &&
142 git commit -m "commit 3" &&
144 c3=$(git rev-parse HEAD) &&
145 git reset --hard "$c0" &&
149 test_debug 'git log --graph --decorate --oneline --all'
151 test_expect_success 'test option parsing' '
152 test_must_fail git merge -$ c1 &&
153 test_must_fail git merge --no-such c1 &&
154 test_must_fail git merge -s foobar c1 &&
155 test_must_fail git merge -s=foobar c1 &&
156 test_must_fail git merge -m &&
157 test_must_fail git merge --abort foobar &&
158 test_must_fail git merge --abort --quiet &&
159 test_must_fail git merge --continue foobar &&
160 test_must_fail git merge --continue --quiet &&
161 test_must_fail git merge
164 test_expect_success 'merge -h with invalid index' '
170 test_expect_code 129 git merge -h 2>usage
172 test_i18ngrep "[Uu]sage: git merge" broken/usage
175 test_expect_success 'reject non-strategy with a git-merge-foo name' '
176 test_must_fail git merge -s index c1
179 test_expect_success 'merge c0 with c1' '
180 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
182 git reset --hard c0 &&
184 verify_merge file result.1 &&
187 git reflog -1 >reflog.actual &&
188 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
189 test_cmp reflog.expected reflog.fuzzy
192 test_debug 'git log --graph --decorate --oneline --all'
194 test_expect_success 'merge c0 with c1 with --ff-only' '
195 git reset --hard c0 &&
196 git merge --ff-only c1 &&
197 git merge --ff-only HEAD c0 c1 &&
198 verify_merge file result.1 &&
202 test_debug 'git log --graph --decorate --oneline --all'
204 test_expect_success 'merge from unborn branch' '
205 git checkout -f master &&
206 test_might_fail git branch -D kid &&
208 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
210 git checkout --orphan kid &&
211 test_when_finished "git checkout -f master" &&
214 git merge --ff-only c1 &&
215 verify_merge file result.1 &&
218 git reflog -1 >reflog.actual &&
219 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
220 test_cmp reflog.expected reflog.fuzzy
223 test_debug 'git log --graph --decorate --oneline --all'
225 test_expect_success 'merge c1 with c2' '
226 git reset --hard c1 &&
229 verify_merge file result.1-5 msg.1-5 &&
230 verify_parents $c1 $c2
233 test_expect_success 'merge --squash c3 with c7' '
234 git reset --hard c3 &&
235 test_must_fail git merge --squash c7 &&
236 cat result.9z >file &&
237 git commit --no-edit -a &&
241 Squashed commit of the following:
249 git cat-file commit HEAD | sed -e '1,/^$/d' >actual &&
250 test_cmp expect actual
253 test_debug 'git log --graph --decorate --oneline --all'
255 test_expect_success 'merge c1 with c2 and c3' '
256 git reset --hard c1 &&
259 verify_merge file result.1-5-9 msg.1-5-9 &&
260 verify_parents $c1 $c2 $c3
263 test_debug 'git log --graph --decorate --oneline --all'
265 test_expect_success 'merges with --ff-only' '
266 git reset --hard c1 &&
268 test_must_fail git merge --ff-only c2 &&
269 test_must_fail git merge --ff-only c3 &&
270 test_must_fail git merge --ff-only c2 c3 &&
271 git reset --hard c0 &&
276 test_expect_success 'merges with merge.ff=only' '
277 git reset --hard c1 &&
279 test_config merge.ff "only" &&
280 test_must_fail git merge c2 &&
281 test_must_fail git merge c3 &&
282 test_must_fail git merge c2 c3 &&
283 git reset --hard c0 &&
288 test_expect_success 'merge c0 with c1 (no-commit)' '
289 git reset --hard c0 &&
290 git merge --no-commit c1 &&
291 verify_merge file result.1 &&
295 test_debug 'git log --graph --decorate --oneline --all'
297 test_expect_success 'merge c1 with c2 (no-commit)' '
298 git reset --hard c1 &&
299 git merge --no-commit c2 &&
300 verify_merge file result.1-5 &&
302 verify_mergeheads $c2
305 test_debug 'git log --graph --decorate --oneline --all'
307 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
308 git reset --hard c1 &&
309 git merge --no-commit c2 c3 &&
310 verify_merge file result.1-5-9 &&
312 verify_mergeheads $c2 $c3
315 test_debug 'git log --graph --decorate --oneline --all'
317 test_expect_success 'merge c0 with c1 (squash)' '
318 git reset --hard c0 &&
319 git merge --squash c1 &&
320 verify_merge file result.1 &&
322 verify_no_mergehead &&
323 test_cmp squash.1 .git/SQUASH_MSG
326 test_debug 'git log --graph --decorate --oneline --all'
328 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
329 git reset --hard c0 &&
330 git merge --squash --ff-only c1 &&
331 verify_merge file result.1 &&
333 verify_no_mergehead &&
334 test_cmp squash.1 .git/SQUASH_MSG
337 test_debug 'git log --graph --decorate --oneline --all'
339 test_expect_success 'merge c1 with c2 (squash)' '
340 git reset --hard c1 &&
341 git merge --squash c2 &&
342 verify_merge file result.1-5 &&
344 verify_no_mergehead &&
345 test_cmp squash.1-5 .git/SQUASH_MSG
348 test_debug 'git log --graph --decorate --oneline --all'
350 test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
351 git reset --hard c1 &&
352 test_must_fail git merge --squash --ff-only c2
355 test_debug 'git log --graph --decorate --oneline --all'
357 test_expect_success 'merge c1 with c2 and c3 (squash)' '
358 git reset --hard c1 &&
359 git merge --squash c2 c3 &&
360 verify_merge file result.1-5-9 &&
362 verify_no_mergehead &&
363 test_cmp squash.1-5-9 .git/SQUASH_MSG
366 test_debug 'git log --graph --decorate --oneline --all'
368 test_expect_success 'merge c1 with c2 (no-commit in config)' '
369 git reset --hard c1 &&
370 test_config branch.master.mergeoptions "--no-commit" &&
372 verify_merge file result.1-5 &&
374 verify_mergeheads $c2
377 test_debug 'git log --graph --decorate --oneline --all'
379 test_expect_success 'merge c1 with c2 (log in config)' '
380 git reset --hard c1 &&
381 git merge --log c2 &&
382 git show -s --pretty=tformat:%s%n%b >expect &&
384 test_config branch.master.mergeoptions "--log" &&
385 git reset --hard c1 &&
387 git show -s --pretty=tformat:%s%n%b >actual &&
389 test_cmp expect actual
392 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
393 git reset --hard c1 &&
395 git show -s --pretty=tformat:%s%n%b >expect &&
397 test_config branch.master.mergeoptions "--no-log" &&
398 test_config merge.log "true" &&
399 git reset --hard c1 &&
401 git show -s --pretty=tformat:%s%n%b >actual &&
403 test_cmp expect actual
406 test_expect_success 'merge c1 with c2 (squash in config)' '
407 git reset --hard c1 &&
408 test_config branch.master.mergeoptions "--squash" &&
410 verify_merge file result.1-5 &&
412 verify_no_mergehead &&
413 test_cmp squash.1-5 .git/SQUASH_MSG
416 test_debug 'git log --graph --decorate --oneline --all'
418 test_expect_success 'override config option -n with --summary' '
419 git reset --hard c1 &&
420 test_config branch.master.mergeoptions "-n" &&
422 git merge --summary c2 >diffstat.txt &&
423 verify_merge file result.1-5 msg.1-5 &&
424 verify_parents $c1 $c2 &&
425 if ! grep "^ file | *2 +-$" diffstat.txt
427 echo "[OOPS] diffstat was not generated with --summary"
432 test_expect_success 'override config option -n with --stat' '
433 git reset --hard c1 &&
434 test_config branch.master.mergeoptions "-n" &&
436 git merge --stat c2 >diffstat.txt &&
437 verify_merge file result.1-5 msg.1-5 &&
438 verify_parents $c1 $c2 &&
439 if ! grep "^ file | *2 +-$" diffstat.txt
441 echo "[OOPS] diffstat was not generated with --stat"
446 test_debug 'git log --graph --decorate --oneline --all'
448 test_expect_success 'override config option --stat' '
449 git reset --hard c1 &&
450 test_config branch.master.mergeoptions "--stat" &&
452 git merge -n c2 >diffstat.txt &&
453 verify_merge file result.1-5 msg.1-5 &&
454 verify_parents $c1 $c2 &&
455 if grep "^ file | *2 +-$" diffstat.txt
457 echo "[OOPS] diffstat was generated"
462 test_debug 'git log --graph --decorate --oneline --all'
464 test_expect_success 'merge c1 with c2 (override --no-commit)' '
465 git reset --hard c1 &&
466 test_config branch.master.mergeoptions "--no-commit" &&
468 git merge --commit c2 &&
469 verify_merge file result.1-5 msg.1-5 &&
470 verify_parents $c1 $c2
473 test_debug 'git log --graph --decorate --oneline --all'
475 test_expect_success 'merge c1 with c2 (override --squash)' '
476 git reset --hard c1 &&
477 test_config branch.master.mergeoptions "--squash" &&
479 git merge --no-squash c2 &&
480 verify_merge file result.1-5 msg.1-5 &&
481 verify_parents $c1 $c2
484 test_debug 'git log --graph --decorate --oneline --all'
486 test_expect_success 'merge c0 with c1 (no-ff)' '
487 git reset --hard c0 &&
489 git merge --no-ff c1 &&
490 verify_merge file result.1 &&
491 verify_parents $c0 $c1
494 test_debug 'git log --graph --decorate --oneline --all'
496 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
497 git reset --hard c0 &&
498 test_config merge.ff "false" &&
501 verify_merge file result.1 &&
502 verify_parents $c0 $c1
504 test_debug 'git log --graph --decorate --oneline --all'
506 test_expect_success 'combine branch.master.mergeoptions with merge.ff' '
507 git reset --hard c0 &&
508 test_config branch.master.mergeoptions "--ff" &&
509 test_config merge.ff "false" &&
512 verify_merge file result.1 &&
516 test_expect_success 'tolerate unknown values for merge.ff' '
517 git reset --hard c0 &&
518 test_config merge.ff "something-new" &&
520 git merge c1 2>message &&
522 test_cmp empty message
525 test_expect_success 'combining --squash and --no-ff is refused' '
526 git reset --hard c0 &&
527 test_must_fail git merge --squash --no-ff c1 &&
528 test_must_fail git merge --no-ff --squash c1
531 test_expect_success 'option --ff-only overwrites --no-ff' '
532 git merge --no-ff --ff-only c1 &&
533 test_must_fail git merge --no-ff --ff-only c2
536 test_expect_success 'option --no-ff overrides merge.ff=only config' '
537 git reset --hard c0 &&
538 test_config merge.ff only &&
542 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
543 git reset --hard c0 &&
544 test_config branch.master.mergeoptions "--no-ff" &&
546 verify_merge file result.1 &&
550 test_expect_success 'merge log message' '
551 git reset --hard c0 &&
552 git merge --no-log c2 &&
553 git show -s --pretty=format:%b HEAD >msg.act &&
554 test_cmp msg.nologff msg.act &&
556 git reset --hard c0 &&
557 test_config branch.master.mergeoptions "--no-ff" &&
558 git merge --no-log c2 &&
559 git show -s --pretty=format:%b HEAD >msg.act &&
560 test_cmp msg.nolognoff msg.act &&
562 git merge --log c3 &&
563 git show -s --pretty=format:%b HEAD >msg.act &&
564 test_cmp msg.log msg.act &&
566 git reset --hard HEAD^ &&
567 test_config merge.log "yes" &&
569 git show -s --pretty=format:%b HEAD >msg.act &&
570 test_cmp msg.log msg.act
573 test_debug 'git log --graph --decorate --oneline --all'
575 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
576 git reset --hard c1 &&
578 git merge c0 c2 c0 c1 &&
579 verify_merge file result.1-5 &&
580 verify_parents $c1 $c2
583 test_debug 'git log --graph --decorate --oneline --all'
585 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
586 git reset --hard c1 &&
588 git merge c0 c2 c0 c1 &&
589 verify_merge file result.1-5 &&
590 verify_parents $c1 $c2
593 test_debug 'git log --graph --decorate --oneline --all'
595 test_expect_success 'merge c1 with c1 and c2' '
596 git reset --hard c1 &&
599 verify_merge file result.1-5 &&
600 verify_parents $c1 $c2
603 test_debug 'git log --graph --decorate --oneline --all'
605 test_expect_success 'merge fast-forward in a dirty tree' '
606 git reset --hard c0 &&
613 test_debug 'git log --graph --decorate --oneline --all'
615 test_expect_success 'in-index merge' '
616 git reset --hard c0 &&
617 git merge --no-ff -s resolve c1 >out &&
618 test_i18ngrep "Wonderful." out &&
619 verify_parents $c0 $c1
622 test_debug 'git log --graph --decorate --oneline --all'
624 test_expect_success 'refresh the index before merging' '
625 git reset --hard c1 &&
626 cp file file.n && mv -f file.n file &&
630 cat >expected.branch <<\EOF
631 Merge branch 'c5-branch' (early part)
633 cat >expected.tag <<\EOF
637 test_expect_success 'merge early part of c2' '
638 git reset --hard c3 &&
647 git reset --hard c3 &&
652 git branch -f c5-branch c5 &&
653 git merge c5-branch~1 &&
654 git show -s --pretty=tformat:%s HEAD >actual.branch &&
655 git reset --keep HEAD^ &&
657 git show -s --pretty=tformat:%s HEAD >actual.tag &&
658 test_cmp expected.branch actual.branch &&
659 test_cmp expected.tag actual.tag
662 test_debug 'git log --graph --decorate --oneline --all'
664 test_expect_success 'merge --no-ff --no-commit && commit' '
665 git reset --hard c0 &&
666 git merge --no-ff --no-commit c1 &&
667 EDITOR=: git commit &&
668 verify_parents $c0 $c1
671 test_debug 'git log --graph --decorate --oneline --all'
673 test_expect_success 'amending no-ff merge commit' '
674 EDITOR=: git commit --amend &&
675 verify_parents $c0 $c1
678 test_debug 'git log --graph --decorate --oneline --all'
682 # Add a new message string that was not in the template
684 echo "Merge work done on the side branch c1"
687 ) >"$1.tmp" && mv "$1.tmp" "$1"
688 # strip comments and blank lines from end of message
689 sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
693 test_expect_success 'merge --no-ff --edit' '
694 git reset --hard c0 &&
695 EDITOR=./editor git merge --no-ff --edit c1 &&
696 verify_parents $c0 $c1 &&
697 git cat-file commit HEAD >raw &&
698 grep "work done on the side branch" raw &&
699 sed "1,/^$/d" >actual raw &&
700 test_cmp actual expected
703 test_expect_success GPG 'merge --ff-only tag' '
704 git reset --hard c0 &&
705 git commit --allow-empty -m "A newer commit" &&
706 git tag -s -m "A newer commit" signed &&
707 git reset --hard c0 &&
709 git merge --ff-only signed &&
710 git rev-parse signed^0 >expect &&
711 git rev-parse HEAD >actual &&
712 test_cmp actual expect
715 test_expect_success GPG 'merge --no-edit tag should skip editor' '
716 git reset --hard c0 &&
717 git commit --allow-empty -m "A newer commit" &&
718 git tag -f -s -m "A newer commit" signed &&
719 git reset --hard c0 &&
721 EDITOR=false git merge --no-edit signed &&
722 git rev-parse signed^0 >expect &&
723 git rev-parse HEAD^2 >actual &&
724 test_cmp actual expect
727 test_expect_success 'set up mod-256 conflict scenario' '
728 # 256 near-identical stanzas...
729 for i in $(test_seq 1 256); do
730 for j in 1 2 3 4 5; do
735 git commit -m base &&
737 # one side changes the first line of each to "master"
738 sed s/-1/-master/ <file >tmp &&
740 git commit -am master &&
742 # and the other to "side"; merging the two will
743 # yield 256 separate conflicts
744 git checkout -b side HEAD^ &&
745 sed s/-1/-side/ <file >tmp &&
750 test_expect_success 'merge detects mod-256 conflicts (recursive)' '
752 test_must_fail git merge -s recursive master
755 test_expect_success 'merge detects mod-256 conflicts (resolve)' '
757 test_must_fail git merge -s resolve master
760 test_expect_success 'merge nothing into void' '
764 git remote add up .. &&
766 test_must_fail git merge FETCH_HEAD
770 test_expect_success 'merge can be completed with --continue' '
771 git reset --hard c0 &&
772 git merge --no-ff --no-commit c1 &&
773 git merge --continue &&
774 verify_parents $c0 $c1