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
160 test_expect_success
'merge -h with invalid index' '
166 test_expect_code 129 git merge -h 2>usage
168 test_i18ngrep "[Uu]sage: git merge" broken/usage
171 test_expect_success
'reject non-strategy with a git-merge-foo name' '
172 test_must_fail git merge -s index c1
175 test_expect_success
'merge c0 with c1' '
176 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
178 git reset --hard c0 &&
180 verify_merge file result.1 &&
183 git reflog -1 >reflog.actual &&
184 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
185 test_cmp reflog.expected reflog.fuzzy
188 test_debug
'git log --graph --decorate --oneline --all'
190 test_expect_success
'merge c0 with c1 with --ff-only' '
191 git reset --hard c0 &&
192 git merge --ff-only c1 &&
193 git merge --ff-only HEAD c0 c1 &&
194 verify_merge file result.1 &&
198 test_debug
'git log --graph --decorate --oneline --all'
200 test_expect_success
'merge from unborn branch' '
201 git checkout -f master &&
202 test_might_fail git branch -D kid &&
204 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
206 git checkout --orphan kid &&
207 test_when_finished "git checkout -f master" &&
210 git merge --ff-only c1 &&
211 verify_merge file result.1 &&
214 git reflog -1 >reflog.actual &&
215 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
216 test_cmp reflog.expected reflog.fuzzy
219 test_debug
'git log --graph --decorate --oneline --all'
221 test_expect_success
'merge c1 with c2' '
222 git reset --hard c1 &&
225 verify_merge file result.1-5 msg.1-5 &&
226 verify_parents $c1 $c2
229 test_expect_success
'merge --squash c3 with c7' '
230 git reset --hard c3 &&
231 test_must_fail git merge --squash c7 &&
232 cat result.9z >file &&
233 git commit --no-edit -a &&
237 Squashed commit of the following:
245 git cat-file commit HEAD | sed -e '1,/^$
/d
' >actual &&
246 test_cmp expect actual
249 test_debug
'git log --graph --decorate --oneline --all'
251 test_expect_success
'merge c1 with c2 and c3' '
252 git reset --hard c1 &&
255 verify_merge file result.1-5-9 msg.1-5-9 &&
256 verify_parents $c1 $c2 $c3
259 test_debug
'git log --graph --decorate --oneline --all'
261 test_expect_success
'merges with --ff-only' '
262 git reset --hard c1 &&
264 test_must_fail git merge --ff-only c2 &&
265 test_must_fail git merge --ff-only c3 &&
266 test_must_fail git merge --ff-only c2 c3 &&
267 git reset --hard c0 &&
272 test_expect_success
'merges with merge.ff=only' '
273 git reset --hard c1 &&
275 test_config merge.ff "only" &&
276 test_must_fail git merge c2 &&
277 test_must_fail git merge c3 &&
278 test_must_fail git merge c2 c3 &&
279 git reset --hard c0 &&
284 test_expect_success
'merge c0 with c1 (no-commit)' '
285 git reset --hard c0 &&
286 git merge --no-commit c1 &&
287 verify_merge file result.1 &&
291 test_debug
'git log --graph --decorate --oneline --all'
293 test_expect_success
'merge c1 with c2 (no-commit)' '
294 git reset --hard c1 &&
295 git merge --no-commit c2 &&
296 verify_merge file result.1-5 &&
298 verify_mergeheads $c2
301 test_debug
'git log --graph --decorate --oneline --all'
303 test_expect_success
'merge c1 with c2 and c3 (no-commit)' '
304 git reset --hard c1 &&
305 git merge --no-commit c2 c3 &&
306 verify_merge file result.1-5-9 &&
308 verify_mergeheads $c2 $c3
311 test_debug
'git log --graph --decorate --oneline --all'
313 test_expect_success
'merge c0 with c1 (squash)' '
314 git reset --hard c0 &&
315 git merge --squash c1 &&
316 verify_merge file result.1 &&
318 verify_no_mergehead &&
319 test_cmp squash.1 .git/SQUASH_MSG
322 test_debug
'git log --graph --decorate --oneline --all'
324 test_expect_success
'merge c0 with c1 (squash, ff-only)' '
325 git reset --hard c0 &&
326 git merge --squash --ff-only c1 &&
327 verify_merge file result.1 &&
329 verify_no_mergehead &&
330 test_cmp squash.1 .git/SQUASH_MSG
333 test_debug
'git log --graph --decorate --oneline --all'
335 test_expect_success
'merge c1 with c2 (squash)' '
336 git reset --hard c1 &&
337 git merge --squash c2 &&
338 verify_merge file result.1-5 &&
340 verify_no_mergehead &&
341 test_cmp squash.1-5 .git/SQUASH_MSG
344 test_debug
'git log --graph --decorate --oneline --all'
346 test_expect_success
'unsuccessful merge of c1 with c2 (squash, ff-only)' '
347 git reset --hard c1 &&
348 test_must_fail git merge --squash --ff-only c2
351 test_debug
'git log --graph --decorate --oneline --all'
353 test_expect_success
'merge c1 with c2 and c3 (squash)' '
354 git reset --hard c1 &&
355 git merge --squash c2 c3 &&
356 verify_merge file result.1-5-9 &&
358 verify_no_mergehead &&
359 test_cmp squash.1-5-9 .git/SQUASH_MSG
362 test_debug
'git log --graph --decorate --oneline --all'
364 test_expect_success
'merge c1 with c2 (no-commit in config)' '
365 git reset --hard c1 &&
366 test_config branch.master.mergeoptions "--no-commit" &&
368 verify_merge file result.1-5 &&
370 verify_mergeheads $c2
373 test_debug
'git log --graph --decorate --oneline --all'
375 test_expect_success
'merge c1 with c2 (log in config)' '
376 git reset --hard c1 &&
377 git merge --log c2 &&
378 git show -s --pretty=tformat:%s%n%b >expect &&
380 test_config branch.master.mergeoptions "--log" &&
381 git reset --hard c1 &&
383 git show -s --pretty=tformat:%s%n%b >actual &&
385 test_cmp expect actual
388 test_expect_success
'merge c1 with c2 (log in config gets overridden)' '
389 git reset --hard c1 &&
391 git show -s --pretty=tformat:%s%n%b >expect &&
393 test_config branch.master.mergeoptions "--no-log" &&
394 test_config merge.log "true" &&
395 git reset --hard c1 &&
397 git show -s --pretty=tformat:%s%n%b >actual &&
399 test_cmp expect actual
402 test_expect_success
'merge c1 with c2 (squash in config)' '
403 git reset --hard c1 &&
404 test_config branch.master.mergeoptions "--squash" &&
406 verify_merge file result.1-5 &&
408 verify_no_mergehead &&
409 test_cmp squash.1-5 .git/SQUASH_MSG
412 test_debug
'git log --graph --decorate --oneline --all'
414 test_expect_success
'override config option -n with --summary' '
415 git reset --hard c1 &&
416 test_config branch.master.mergeoptions "-n" &&
418 git merge --summary c2 >diffstat.txt &&
419 verify_merge file result.1-5 msg.1-5 &&
420 verify_parents $c1 $c2 &&
421 if ! grep "^ file | *2 +-$" diffstat.txt
423 echo "[OOPS] diffstat was not generated with --summary"
428 test_expect_success
'override config option -n with --stat' '
429 git reset --hard c1 &&
430 test_config branch.master.mergeoptions "-n" &&
432 git merge --stat c2 >diffstat.txt &&
433 verify_merge file result.1-5 msg.1-5 &&
434 verify_parents $c1 $c2 &&
435 if ! grep "^ file | *2 +-$" diffstat.txt
437 echo "[OOPS] diffstat was not generated with --stat"
442 test_debug
'git log --graph --decorate --oneline --all'
444 test_expect_success
'override config option --stat' '
445 git reset --hard c1 &&
446 test_config branch.master.mergeoptions "--stat" &&
448 git merge -n c2 >diffstat.txt &&
449 verify_merge file result.1-5 msg.1-5 &&
450 verify_parents $c1 $c2 &&
451 if grep "^ file | *2 +-$" diffstat.txt
453 echo "[OOPS] diffstat was generated"
458 test_debug
'git log --graph --decorate --oneline --all'
460 test_expect_success
'merge c1 with c2 (override --no-commit)' '
461 git reset --hard c1 &&
462 test_config branch.master.mergeoptions "--no-commit" &&
464 git merge --commit c2 &&
465 verify_merge file result.1-5 msg.1-5 &&
466 verify_parents $c1 $c2
469 test_debug
'git log --graph --decorate --oneline --all'
471 test_expect_success
'merge c1 with c2 (override --squash)' '
472 git reset --hard c1 &&
473 test_config branch.master.mergeoptions "--squash" &&
475 git merge --no-squash c2 &&
476 verify_merge file result.1-5 msg.1-5 &&
477 verify_parents $c1 $c2
480 test_debug
'git log --graph --decorate --oneline --all'
482 test_expect_success
'merge c0 with c1 (no-ff)' '
483 git reset --hard c0 &&
485 git merge --no-ff c1 &&
486 verify_merge file result.1 &&
487 verify_parents $c0 $c1
490 test_debug
'git log --graph --decorate --oneline --all'
492 test_expect_success
'merge c0 with c1 (merge.ff=false)' '
493 git reset --hard c0 &&
494 test_config merge.ff "false" &&
497 verify_merge file result.1 &&
498 verify_parents $c0 $c1
500 test_debug
'git log --graph --decorate --oneline --all'
502 test_expect_success
'combine branch.master.mergeoptions with merge.ff' '
503 git reset --hard c0 &&
504 test_config branch.master.mergeoptions "--ff" &&
505 test_config merge.ff "false" &&
508 verify_merge file result.1 &&
512 test_expect_success
'tolerate unknown values for merge.ff' '
513 git reset --hard c0 &&
514 test_config merge.ff "something-new" &&
516 git merge c1 2>message &&
518 test_cmp empty message
521 test_expect_success
'combining --squash and --no-ff is refused' '
522 git reset --hard c0 &&
523 test_must_fail git merge --squash --no-ff c1 &&
524 test_must_fail git merge --no-ff --squash c1
527 test_expect_success
'option --ff-only overwrites --no-ff' '
528 git merge --no-ff --ff-only c1 &&
529 test_must_fail git merge --no-ff --ff-only c2
532 test_expect_success
'option --no-ff overrides merge.ff=only config' '
533 git reset --hard c0 &&
534 test_config merge.ff only &&
538 test_expect_success
'merge c0 with c1 (ff overrides no-ff)' '
539 git reset --hard c0 &&
540 test_config branch.master.mergeoptions "--no-ff" &&
542 verify_merge file result.1 &&
546 test_expect_success
'merge log message' '
547 git reset --hard c0 &&
548 git merge --no-log c2 &&
549 git show -s --pretty=format:%b HEAD >msg.act &&
550 test_cmp msg.nologff msg.act &&
552 git reset --hard c0 &&
553 test_config branch.master.mergeoptions "--no-ff" &&
554 git merge --no-log c2 &&
555 git show -s --pretty=format:%b HEAD >msg.act &&
556 test_cmp msg.nolognoff msg.act &&
558 git merge --log c3 &&
559 git show -s --pretty=format:%b HEAD >msg.act &&
560 test_cmp msg.log msg.act &&
562 git reset --hard HEAD^ &&
563 test_config merge.log "yes" &&
565 git show -s --pretty=format:%b HEAD >msg.act &&
566 test_cmp msg.log msg.act
569 test_debug
'git log --graph --decorate --oneline --all'
571 test_expect_success
'merge c1 with c0, c2, c0, and c1' '
572 git reset --hard c1 &&
574 git merge c0 c2 c0 c1 &&
575 verify_merge file result.1-5 &&
576 verify_parents $c1 $c2
579 test_debug
'git log --graph --decorate --oneline --all'
581 test_expect_success
'merge c1 with c0, c2, c0, and c1' '
582 git reset --hard c1 &&
584 git merge c0 c2 c0 c1 &&
585 verify_merge file result.1-5 &&
586 verify_parents $c1 $c2
589 test_debug
'git log --graph --decorate --oneline --all'
591 test_expect_success
'merge c1 with c1 and c2' '
592 git reset --hard c1 &&
595 verify_merge file result.1-5 &&
596 verify_parents $c1 $c2
599 test_debug
'git log --graph --decorate --oneline --all'
601 test_expect_success
'merge fast-forward in a dirty tree' '
602 git reset --hard c0 &&
609 test_debug
'git log --graph --decorate --oneline --all'
611 test_expect_success
'in-index merge' '
612 git reset --hard c0 &&
613 git merge --no-ff -s resolve c1 >out &&
614 test_i18ngrep "Wonderful." out &&
615 verify_parents $c0 $c1
618 test_debug
'git log --graph --decorate --oneline --all'
620 test_expect_success
'refresh the index before merging' '
621 git reset --hard c1 &&
622 cp file file.n && mv -f file.n file &&
626 cat >expected.branch
<<\EOF
627 Merge branch
'c5-branch' (early part
)
629 cat >expected.tag
<<\EOF
633 test_expect_success
'merge early part of c2' '
634 git reset --hard c3 &&
643 git reset --hard c3 &&
648 git branch -f c5-branch c5 &&
649 git merge c5-branch~1 &&
650 git show -s --pretty=tformat:%s HEAD >actual.branch &&
651 git reset --keep HEAD^ &&
653 git show -s --pretty=tformat:%s HEAD >actual.tag &&
654 test_cmp expected.branch actual.branch &&
655 test_cmp expected.tag actual.tag
658 test_debug
'git log --graph --decorate --oneline --all'
660 test_expect_success
'merge --no-ff --no-commit && commit' '
661 git reset --hard c0 &&
662 git merge --no-ff --no-commit c1 &&
663 EDITOR=: git commit &&
664 verify_parents $c0 $c1
667 test_debug
'git log --graph --decorate --oneline --all'
669 test_expect_success
'amending no-ff merge commit' '
670 EDITOR=: git commit --amend &&
671 verify_parents $c0 $c1
674 test_debug
'git log --graph --decorate --oneline --all'
678 # Add a new message string that was not in the template
680 echo "Merge work done on the side branch c1"
683 ) >"$1.tmp" && mv "$1.tmp" "$1"
684 # strip comments and blank lines from end of message
685 sed -e '/^#/d' < "$1" |
sed -e :a
-e '/^\n*$/{$d;N;ba' -e '}' > expected
689 test_expect_success
'merge --no-ff --edit' '
690 git reset --hard c0 &&
691 EDITOR=./editor git merge --no-ff --edit c1 &&
692 verify_parents $c0 $c1 &&
693 git cat-file commit HEAD >raw &&
694 grep "work done on the side branch" raw &&
695 sed "1,/^$/d" >actual raw &&
696 test_cmp actual expected
699 test_expect_success GPG
'merge --ff-only tag' '
700 git reset --hard c0 &&
701 git commit --allow-empty -m "A newer commit" &&
702 git tag -s -m "A newer commit" signed &&
703 git reset --hard c0 &&
705 git merge --ff-only signed &&
706 git rev-parse signed^0 >expect &&
707 git rev-parse HEAD >actual &&
708 test_cmp actual expect
711 test_expect_success GPG
'merge --no-edit tag should skip editor' '
712 git reset --hard c0 &&
713 git commit --allow-empty -m "A newer commit" &&
714 git tag -f -s -m "A newer commit" signed &&
715 git reset --hard c0 &&
717 EDITOR=false git merge --no-edit signed &&
718 git rev-parse signed^0 >expect &&
719 git rev-parse HEAD^2 >actual &&
720 test_cmp actual expect
723 test_expect_success
'set up mod-256 conflict scenario' '
724 # 256 near-identical stanzas...
725 for i in $(test_seq 1 256); do
726 for j in 1 2 3 4 5; do
731 git commit -m base &&
733 # one side changes the first line of each to "master"
734 sed s/-1/-master/ <file >tmp &&
736 git commit -am master &&
738 # and the other to "side"; merging the two will
739 # yield 256 separate conflicts
740 git checkout -b side HEAD^ &&
741 sed s/-1/-side/ <file >tmp &&
746 test_expect_success
'merge detects mod-256 conflicts (recursive)' '
748 test_must_fail git merge -s recursive master
751 test_expect_success
'merge detects mod-256 conflicts (resolve)' '
753 test_must_fail git merge -s resolve master
756 test_expect_success
'merge nothing into void' '
760 git remote add up .. &&
762 test_must_fail git merge FETCH_HEAD