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
42 create_merge_msgs () {
43 echo "Merge tag 'c2'" >msg.1-5 &&
44 echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
46 echo "Squashed commit of the following:" &&
48 git log --no-merges ^HEAD c1
51 echo "Squashed commit of the following:" &&
53 git log --no-merges ^HEAD c2
56 echo "Squashed commit of the following:" &&
58 git log --no-merges ^HEAD c2 c3
68 git update-index --refresh &&
69 git diff --exit-code &&
72 git show -s --pretty=tformat:%s HEAD >msg.act &&
78 echo "$1" >head.expected &&
79 git rev-parse HEAD >head.actual &&
80 test_cmp head.expected head.actual
84 printf '%s\n' "$@" >parents.expected &&
89 git rev-parse HEAD^$i >>parents.actual &&
93 test_must_fail git rev-parse --verify "HEAD^$i" &&
94 test_cmp parents.expected parents.actual
97 verify_mergeheads () {
98 printf '%s\n' "$@" >mergehead.expected &&
102 done <.git/MERGE_HEAD >mergehead.actual &&
103 test_cmp mergehead.expected mergehead.actual
106 verify_no_mergehead () {
107 ! test -e .git/MERGE_HEAD
110 test_expect_success 'setup' '
113 git commit -m "commit 0" &&
115 c0=$(git rev-parse HEAD) &&
119 git commit -m "commit 1" &&
121 c1=$(git rev-parse HEAD) &&
122 git reset --hard "$c0" &&
126 git commit -m "commit 2" &&
128 c2=$(git rev-parse HEAD) &&
129 git reset --hard "$c0" &&
133 git commit -m "commit 7" &&
135 git reset --hard "$c0" &&
139 git commit -m "commit 3" &&
141 c3=$(git rev-parse HEAD) &&
142 git reset --hard "$c0" &&
146 test_debug 'git log --graph --decorate --oneline --all'
148 test_expect_success 'test option parsing' '
149 test_must_fail git merge -$ c1 &&
150 test_must_fail git merge --no-such c1 &&
151 test_must_fail git merge -s foobar c1 &&
152 test_must_fail git merge -s=foobar c1 &&
153 test_must_fail git merge -m &&
154 test_must_fail git merge --abort foobar &&
155 test_must_fail git merge --abort --quiet &&
156 test_must_fail git merge --continue foobar &&
157 test_must_fail git merge --continue --quiet &&
158 test_must_fail git merge
161 test_expect_success 'merge -h with invalid index' '
167 test_expect_code 129 git merge -h 2>usage
169 test_i18ngrep "[Uu]sage: git merge" broken/usage
172 test_expect_success 'reject non-strategy with a git-merge-foo name' '
173 test_must_fail git merge -s index c1
176 test_expect_success 'merge c0 with c1' '
177 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
179 git reset --hard c0 &&
181 verify_merge file result.1 &&
184 git reflog -1 >reflog.actual &&
185 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
186 test_cmp reflog.expected reflog.fuzzy
189 test_debug 'git log --graph --decorate --oneline --all'
191 test_expect_success 'merge c0 with c1 with --ff-only' '
192 git reset --hard c0 &&
193 git merge --ff-only c1 &&
194 git merge --ff-only HEAD c0 c1 &&
195 verify_merge file result.1 &&
199 test_debug 'git log --graph --decorate --oneline --all'
201 test_expect_success 'merge from unborn branch' '
202 git checkout -f master &&
203 test_might_fail git branch -D kid &&
205 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
207 git checkout --orphan kid &&
208 test_when_finished "git checkout -f master" &&
211 git merge --ff-only c1 &&
212 verify_merge file result.1 &&
215 git reflog -1 >reflog.actual &&
216 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
217 test_cmp reflog.expected reflog.fuzzy
220 test_debug 'git log --graph --decorate --oneline --all'
222 test_expect_success 'merge c1 with c2' '
223 git reset --hard c1 &&
226 verify_merge file result.1-5 msg.1-5 &&
227 verify_parents $c1 $c2
230 test_expect_success 'merge --squash c3 with c7' '
231 git reset --hard c3 &&
232 test_must_fail git merge --squash c7 &&
233 cat result.9z >file &&
234 git commit --no-edit -a &&
238 Squashed commit of the following:
246 git cat-file commit HEAD | sed -e '1,/^$/d' >actual &&
247 test_cmp expect actual
250 test_debug 'git log --graph --decorate --oneline --all'
252 test_expect_success 'merge c1 with c2 and c3' '
253 git reset --hard c1 &&
256 verify_merge file result.1-5-9 msg.1-5-9 &&
257 verify_parents $c1 $c2 $c3
260 test_debug 'git log --graph --decorate --oneline --all'
262 test_expect_success 'merges with --ff-only' '
263 git reset --hard c1 &&
265 test_must_fail git merge --ff-only c2 &&
266 test_must_fail git merge --ff-only c3 &&
267 test_must_fail git merge --ff-only c2 c3 &&
268 git reset --hard c0 &&
273 test_expect_success 'merges with merge.ff=only' '
274 git reset --hard c1 &&
276 test_config merge.ff "only" &&
277 test_must_fail git merge c2 &&
278 test_must_fail git merge c3 &&
279 test_must_fail git merge c2 c3 &&
280 git reset --hard c0 &&
285 test_expect_success 'merge c0 with c1 (no-commit)' '
286 git reset --hard c0 &&
287 git merge --no-commit c1 &&
288 verify_merge file result.1 &&
292 test_debug 'git log --graph --decorate --oneline --all'
294 test_expect_success 'merge c1 with c2 (no-commit)' '
295 git reset --hard c1 &&
296 git merge --no-commit c2 &&
297 verify_merge file result.1-5 &&
299 verify_mergeheads $c2
302 test_debug 'git log --graph --decorate --oneline --all'
304 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
305 git reset --hard c1 &&
306 git merge --no-commit c2 c3 &&
307 verify_merge file result.1-5-9 &&
309 verify_mergeheads $c2 $c3
312 test_debug 'git log --graph --decorate --oneline --all'
314 test_expect_success 'merge c0 with c1 (squash)' '
315 git reset --hard c0 &&
316 git merge --squash c1 &&
317 verify_merge file result.1 &&
319 verify_no_mergehead &&
320 test_cmp squash.1 .git/SQUASH_MSG
323 test_debug 'git log --graph --decorate --oneline --all'
325 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
326 git reset --hard c0 &&
327 git merge --squash --ff-only c1 &&
328 verify_merge file result.1 &&
330 verify_no_mergehead &&
331 test_cmp squash.1 .git/SQUASH_MSG
334 test_debug 'git log --graph --decorate --oneline --all'
336 test_expect_success 'merge c1 with c2 (squash)' '
337 git reset --hard c1 &&
338 git merge --squash c2 &&
339 verify_merge file result.1-5 &&
341 verify_no_mergehead &&
342 test_cmp squash.1-5 .git/SQUASH_MSG
345 test_debug 'git log --graph --decorate --oneline --all'
347 test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
348 git reset --hard c1 &&
349 test_must_fail git merge --squash --ff-only c2
352 test_debug 'git log --graph --decorate --oneline --all'
354 test_expect_success 'merge c1 with c2 and c3 (squash)' '
355 git reset --hard c1 &&
356 git merge --squash c2 c3 &&
357 verify_merge file result.1-5-9 &&
359 verify_no_mergehead &&
360 test_cmp squash.1-5-9 .git/SQUASH_MSG
363 test_debug 'git log --graph --decorate --oneline --all'
365 test_expect_success 'merge c1 with c2 (no-commit in config)' '
366 git reset --hard c1 &&
367 test_config branch.master.mergeoptions "--no-commit" &&
369 verify_merge file result.1-5 &&
371 verify_mergeheads $c2
374 test_debug 'git log --graph --decorate --oneline --all'
376 test_expect_success 'merge c1 with c2 (log in config)' '
377 git reset --hard c1 &&
378 git merge --log c2 &&
379 git show -s --pretty=tformat:%s%n%b >expect &&
381 test_config branch.master.mergeoptions "--log" &&
382 git reset --hard c1 &&
384 git show -s --pretty=tformat:%s%n%b >actual &&
386 test_cmp expect actual
389 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
390 git reset --hard c1 &&
392 git show -s --pretty=tformat:%s%n%b >expect &&
394 test_config branch.master.mergeoptions "--no-log" &&
395 test_config merge.log "true" &&
396 git reset --hard c1 &&
398 git show -s --pretty=tformat:%s%n%b >actual &&
400 test_cmp expect actual
403 test_expect_success 'merge c1 with c2 (squash in config)' '
404 git reset --hard c1 &&
405 test_config branch.master.mergeoptions "--squash" &&
407 verify_merge file result.1-5 &&
409 verify_no_mergehead &&
410 test_cmp squash.1-5 .git/SQUASH_MSG
413 test_debug 'git log --graph --decorate --oneline --all'
415 test_expect_success 'override config option -n with --summary' '
416 git reset --hard c1 &&
417 test_config branch.master.mergeoptions "-n" &&
419 git merge --summary c2 >diffstat.txt &&
420 verify_merge file result.1-5 msg.1-5 &&
421 verify_parents $c1 $c2 &&
422 if ! grep "^ file | *2 +-$" diffstat.txt
424 echo "[OOPS] diffstat was not generated with --summary"
429 test_expect_success 'override config option -n with --stat' '
430 git reset --hard c1 &&
431 test_config branch.master.mergeoptions "-n" &&
433 git merge --stat c2 >diffstat.txt &&
434 verify_merge file result.1-5 msg.1-5 &&
435 verify_parents $c1 $c2 &&
436 if ! grep "^ file | *2 +-$" diffstat.txt
438 echo "[OOPS] diffstat was not generated with --stat"
443 test_debug 'git log --graph --decorate --oneline --all'
445 test_expect_success 'override config option --stat' '
446 git reset --hard c1 &&
447 test_config branch.master.mergeoptions "--stat" &&
449 git merge -n c2 >diffstat.txt &&
450 verify_merge file result.1-5 msg.1-5 &&
451 verify_parents $c1 $c2 &&
452 if grep "^ file | *2 +-$" diffstat.txt
454 echo "[OOPS] diffstat was generated"
459 test_debug 'git log --graph --decorate --oneline --all'
461 test_expect_success 'merge c1 with c2 (override --no-commit)' '
462 git reset --hard c1 &&
463 test_config branch.master.mergeoptions "--no-commit" &&
465 git merge --commit c2 &&
466 verify_merge file result.1-5 msg.1-5 &&
467 verify_parents $c1 $c2
470 test_debug 'git log --graph --decorate --oneline --all'
472 test_expect_success 'merge c1 with c2 (override --squash)' '
473 git reset --hard c1 &&
474 test_config branch.master.mergeoptions "--squash" &&
476 git merge --no-squash c2 &&
477 verify_merge file result.1-5 msg.1-5 &&
478 verify_parents $c1 $c2
481 test_debug 'git log --graph --decorate --oneline --all'
483 test_expect_success 'merge c0 with c1 (no-ff)' '
484 git reset --hard c0 &&
486 git merge --no-ff c1 &&
487 verify_merge file result.1 &&
488 verify_parents $c0 $c1
491 test_debug 'git log --graph --decorate --oneline --all'
493 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
494 git reset --hard c0 &&
495 test_config merge.ff "false" &&
498 verify_merge file result.1 &&
499 verify_parents $c0 $c1
501 test_debug 'git log --graph --decorate --oneline --all'
503 test_expect_success 'combine branch.master.mergeoptions with merge.ff' '
504 git reset --hard c0 &&
505 test_config branch.master.mergeoptions "--ff" &&
506 test_config merge.ff "false" &&
509 verify_merge file result.1 &&
513 test_expect_success 'tolerate unknown values for merge.ff' '
514 git reset --hard c0 &&
515 test_config merge.ff "something-new" &&
517 git merge c1 2>message &&
519 test_must_be_empty message
522 test_expect_success 'combining --squash and --no-ff is refused' '
523 git reset --hard c0 &&
524 test_must_fail git merge --squash --no-ff c1 &&
525 test_must_fail git merge --no-ff --squash c1
528 test_expect_success 'option --ff-only overwrites --no-ff' '
529 git merge --no-ff --ff-only c1 &&
530 test_must_fail git merge --no-ff --ff-only c2
533 test_expect_success 'option --no-ff overrides merge.ff=only config' '
534 git reset --hard c0 &&
535 test_config merge.ff only &&
539 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
540 git reset --hard c0 &&
541 test_config branch.master.mergeoptions "--no-ff" &&
543 verify_merge file result.1 &&
547 test_expect_success 'merge log message' '
548 git reset --hard c0 &&
549 git merge --no-log c2 &&
550 git show -s --pretty=format:%b HEAD >msg.act &&
551 test_must_be_empty msg.act &&
553 git reset --hard c0 &&
554 test_config branch.master.mergeoptions "--no-ff" &&
555 git merge --no-log c2 &&
556 git show -s --pretty=format:%b HEAD >msg.act &&
557 test_must_be_empty msg.act &&
559 git merge --log c3 &&
560 git show -s --pretty=format:%b HEAD >msg.act &&
561 test_cmp msg.log msg.act &&
563 git reset --hard HEAD^ &&
564 test_config merge.log "yes" &&
566 git show -s --pretty=format:%b HEAD >msg.act &&
567 test_cmp msg.log msg.act
570 test_debug 'git log --graph --decorate --oneline --all'
572 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
573 git reset --hard c1 &&
575 git merge c0 c2 c0 c1 &&
576 verify_merge file result.1-5 &&
577 verify_parents $c1 $c2
580 test_debug 'git log --graph --decorate --oneline --all'
582 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
583 git reset --hard c1 &&
585 git merge c0 c2 c0 c1 &&
586 verify_merge file result.1-5 &&
587 verify_parents $c1 $c2
590 test_debug 'git log --graph --decorate --oneline --all'
592 test_expect_success 'merge c1 with c1 and c2' '
593 git reset --hard c1 &&
596 verify_merge file result.1-5 &&
597 verify_parents $c1 $c2
600 test_debug 'git log --graph --decorate --oneline --all'
602 test_expect_success 'merge fast-forward in a dirty tree' '
603 git reset --hard c0 &&
610 test_debug 'git log --graph --decorate --oneline --all'
612 test_expect_success 'in-index merge' '
613 git reset --hard c0 &&
614 git merge --no-ff -s resolve c1 >out &&
615 test_i18ngrep "Wonderful." out &&
616 verify_parents $c0 $c1
619 test_debug 'git log --graph --decorate --oneline --all'
621 test_expect_success 'refresh the index before merging' '
622 git reset --hard c1 &&
623 cp file file.n && mv -f file.n file &&
627 cat >expected.branch <<\EOF
628 Merge branch 'c5-branch' (early part)
630 cat >expected.tag <<\EOF
634 test_expect_success 'merge early part of c2' '
635 git reset --hard c3 &&
644 git reset --hard c3 &&
649 git branch -f c5-branch c5 &&
650 git merge c5-branch~1 &&
651 git show -s --pretty=tformat:%s HEAD >actual.branch &&
652 git reset --keep HEAD^ &&
654 git show -s --pretty=tformat:%s HEAD >actual.tag &&
655 test_cmp expected.branch actual.branch &&
656 test_cmp expected.tag actual.tag
659 test_debug 'git log --graph --decorate --oneline --all'
661 test_expect_success 'merge --no-ff --no-commit && commit' '
662 git reset --hard c0 &&
663 git merge --no-ff --no-commit c1 &&
664 EDITOR=: git commit &&
665 verify_parents $c0 $c1
668 test_debug 'git log --graph --decorate --oneline --all'
670 test_expect_success 'amending no-ff merge commit' '
671 EDITOR=: git commit --amend &&
672 verify_parents $c0 $c1
675 test_debug 'git log --graph --decorate --oneline --all'
679 # Add a new message string that was not in the template
681 echo "Merge work done on the side branch c1"
684 ) >"$1.tmp" && mv "$1.tmp" "$1"
685 # strip comments and blank lines from end of message
686 sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
690 test_expect_success 'merge --no-ff --edit' '
691 git reset --hard c0 &&
692 EDITOR=./editor git merge --no-ff --edit c1 &&
693 verify_parents $c0 $c1 &&
694 git cat-file commit HEAD >raw &&
695 grep "work done on the side branch" raw &&
696 sed "1,/^$/d" >actual raw &&
697 test_cmp expected actual
700 test_expect_success 'merge annotated/signed tag w/o tracking' '
701 test_when_finished "rm -rf dst; git tag -d anno1" &&
702 git tag -a -m "anno c1" anno1 c1 &&
704 git rev-parse c1 >dst/expect &&
706 # c0 fast-forwards to c1 but because this repository
707 # is not a "downstream" whose refs/tags follows along
708 # tag from the "upstream", this pull defaults to --no-ff
712 git rev-parse HEAD^2 >actual &&
713 test_cmp expect actual
717 test_expect_success 'merge annotated/signed tag w/ tracking' '
718 test_when_finished "rm -rf dst; git tag -d anno1" &&
719 git tag -a -m "anno c1" anno1 c1 &&
721 git rev-parse c1 >dst/expect &&
723 # c0 fast-forwards to c1 and because this repository
724 # is a "downstream" whose refs/tags follows along
725 # tag from the "upstream", this pull defaults to --ff
727 git remote add origin .. &&
728 git pull origin c0 &&
731 git rev-parse HEAD >actual &&
732 test_cmp expect actual
736 test_expect_success GPG 'merge --ff-only tag' '
737 git reset --hard c0 &&
738 git commit --allow-empty -m "A newer commit" &&
739 git tag -s -m "A newer commit" signed &&
740 git reset --hard c0 &&
742 git merge --ff-only signed &&
743 git rev-parse signed^0 >expect &&
744 git rev-parse HEAD >actual &&
745 test_cmp expect actual
748 test_expect_success GPG 'merge --no-edit tag should skip editor' '
749 git reset --hard c0 &&
750 git commit --allow-empty -m "A newer commit" &&
751 git tag -f -s -m "A newer commit" signed &&
752 git reset --hard c0 &&
754 EDITOR=false git merge --no-edit --no-ff signed &&
755 git rev-parse signed^0 >expect &&
756 git rev-parse HEAD^2 >actual &&
757 test_cmp expect actual
760 test_expect_success 'set up mod-256 conflict scenario' '
761 # 256 near-identical stanzas...
762 for i in $(test_seq 1 256); do
763 for j in 1 2 3 4 5; do
768 git commit -m base &&
770 # one side changes the first line of each to "master"
771 sed s/-1/-master/ <file >tmp &&
773 git commit -am master &&
775 # and the other to "side"; merging the two will
776 # yield 256 separate conflicts
777 git checkout -b side HEAD^ &&
778 sed s/-1/-side/ <file >tmp &&
783 test_expect_success 'merge detects mod-256 conflicts (recursive)' '
785 test_must_fail git merge -s recursive master
788 test_expect_success 'merge detects mod-256 conflicts (resolve)' '
790 test_must_fail git merge -s resolve master
793 test_expect_success 'merge nothing into void' '
797 git remote add up .. &&
799 test_must_fail git merge FETCH_HEAD
803 test_expect_success 'merge can be completed with --continue' '
804 git reset --hard c0 &&
805 git merge --no-ff --no-commit c1 &&
806 git merge --continue &&
807 verify_parents $c0 $c1
810 write_script .git/FAKE_EDITOR <<EOF
811 # kill -TERM command added below.
814 test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue' '
815 git reset --hard c0 &&
816 ! "$SHELL_PATH" -c '\''
817 echo kill -TERM $$ >> .git/FAKE_EDITOR
818 GIT_EDITOR=.git/FAKE_EDITOR
820 exec git merge --no-ff --edit c1'\'' &&
821 git merge --continue &&
822 verify_parents $c0 $c1