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 X' 2 3 4 5 6 7 8 9 >result.1
37 printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
38 printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
41 create_merge_msgs () {
42 echo "Merge tag 'c2'" >msg.1-5 &&
43 echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
45 echo "Squashed commit of the following:" &&
47 git log --no-merges ^HEAD c1
50 echo "Squashed commit of the following:" &&
52 git log --no-merges ^HEAD c2
55 echo "Squashed commit of the following:" &&
57 git log --no-merges ^HEAD c2 c3
69 git update-index --refresh &&
70 git diff --exit-code &&
73 git show -s --pretty=tformat:%s HEAD >msg.act &&
79 echo "$1" >head.expected &&
80 git rev-parse HEAD >head.actual &&
81 test_cmp head.expected head.actual
85 printf '%s\n' "$@" >parents.expected &&
90 git rev-parse HEAD^$i >>parents.actual &&
94 test_must_fail git rev-parse --verify "HEAD^$i" &&
95 test_cmp parents.expected parents.actual
98 verify_mergeheads () {
99 printf '%s\n' "$@" >mergehead.expected &&
103 done <.git/MERGE_HEAD >mergehead.actual &&
104 test_cmp mergehead.expected mergehead.actual
107 verify_no_mergehead () {
108 ! test -e .git/MERGE_HEAD
111 test_expect_success 'setup' '
114 git commit -m "commit 0" &&
116 c0=$(git rev-parse HEAD) &&
120 git commit -m "commit 1" &&
122 c1=$(git rev-parse HEAD) &&
123 git reset --hard "$c0" &&
127 git commit -m "commit 2" &&
129 c2=$(git rev-parse HEAD) &&
130 git reset --hard "$c0" &&
134 git commit -m "commit 3" &&
136 c3=$(git rev-parse HEAD)
137 git reset --hard "$c0" &&
141 test_debug 'git log --graph --decorate --oneline --all'
143 test_expect_success 'test option parsing' '
144 test_must_fail git merge -$ c1 &&
145 test_must_fail git merge --no-such c1 &&
146 test_must_fail git merge -s foobar c1 &&
147 test_must_fail git merge -s=foobar c1 &&
148 test_must_fail git merge -m &&
149 test_must_fail git merge
152 test_expect_success 'merge -h with invalid index' '
158 test_expect_code 129 git merge -h 2>usage
160 test_i18ngrep "[Uu]sage: git merge" broken/usage
163 test_expect_success 'reject non-strategy with a git-merge-foo name' '
164 test_must_fail git merge -s index c1
167 test_expect_success 'merge c0 with c1' '
168 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
170 git reset --hard c0 &&
172 verify_merge file result.1 &&
175 git reflog -1 >reflog.actual &&
176 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
177 test_cmp reflog.expected reflog.fuzzy
180 test_debug 'git log --graph --decorate --oneline --all'
182 test_expect_success 'merge c0 with c1 with --ff-only' '
183 git reset --hard c0 &&
184 git merge --ff-only c1 &&
185 git merge --ff-only HEAD c0 c1 &&
186 verify_merge file result.1 &&
190 test_debug 'git log --graph --decorate --oneline --all'
192 test_expect_success 'merge from unborn branch' '
193 git checkout -f master &&
194 test_might_fail git branch -D kid &&
196 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
198 git checkout --orphan kid &&
199 test_when_finished "git checkout -f master" &&
202 git merge --ff-only c1 &&
203 verify_merge file result.1 &&
206 git reflog -1 >reflog.actual &&
207 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
208 test_cmp reflog.expected reflog.fuzzy
211 test_debug 'git log --graph --decorate --oneline --all'
213 test_expect_success 'merge c1 with c2' '
214 git reset --hard c1 &&
217 verify_merge file result.1-5 msg.1-5 &&
218 verify_parents $c1 $c2
221 test_debug 'git log --graph --decorate --oneline --all'
223 test_expect_success 'merge c1 with c2 and c3' '
224 git reset --hard c1 &&
227 verify_merge file result.1-5-9 msg.1-5-9 &&
228 verify_parents $c1 $c2 $c3
231 test_debug 'git log --graph --decorate --oneline --all'
233 test_expect_success 'merges with --ff-only' '
234 git reset --hard c1 &&
236 test_must_fail git merge --ff-only c2 &&
237 test_must_fail git merge --ff-only c3 &&
238 test_must_fail git merge --ff-only c2 c3 &&
239 git reset --hard c0 &&
244 test_expect_success 'merges with merge.ff=only' '
245 git reset --hard c1 &&
247 test_config merge.ff "only" &&
248 test_must_fail git merge c2 &&
249 test_must_fail git merge c3 &&
250 test_must_fail git merge c2 c3 &&
251 git reset --hard c0 &&
256 test_expect_success 'merge c0 with c1 (no-commit)' '
257 git reset --hard c0 &&
258 git merge --no-commit c1 &&
259 verify_merge file result.1 &&
263 test_debug 'git log --graph --decorate --oneline --all'
265 test_expect_success 'merge c1 with c2 (no-commit)' '
266 git reset --hard c1 &&
267 git merge --no-commit c2 &&
268 verify_merge file result.1-5 &&
270 verify_mergeheads $c2
273 test_debug 'git log --graph --decorate --oneline --all'
275 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
276 git reset --hard c1 &&
277 git merge --no-commit c2 c3 &&
278 verify_merge file result.1-5-9 &&
280 verify_mergeheads $c2 $c3
283 test_debug 'git log --graph --decorate --oneline --all'
285 test_expect_success 'merge c0 with c1 (squash)' '
286 git reset --hard c0 &&
287 git merge --squash c1 &&
288 verify_merge file result.1 &&
290 verify_no_mergehead &&
291 test_cmp squash.1 .git/SQUASH_MSG
294 test_debug 'git log --graph --decorate --oneline --all'
296 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
297 git reset --hard c0 &&
298 git merge --squash --ff-only c1 &&
299 verify_merge file result.1 &&
301 verify_no_mergehead &&
302 test_cmp squash.1 .git/SQUASH_MSG
305 test_debug 'git log --graph --decorate --oneline --all'
307 test_expect_success 'merge c1 with c2 (squash)' '
308 git reset --hard c1 &&
309 git merge --squash c2 &&
310 verify_merge file result.1-5 &&
312 verify_no_mergehead &&
313 test_cmp squash.1-5 .git/SQUASH_MSG
316 test_debug 'git log --graph --decorate --oneline --all'
318 test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
319 git reset --hard c1 &&
320 test_must_fail git merge --squash --ff-only c2
323 test_debug 'git log --graph --decorate --oneline --all'
325 test_expect_success 'merge c1 with c2 and c3 (squash)' '
326 git reset --hard c1 &&
327 git merge --squash c2 c3 &&
328 verify_merge file result.1-5-9 &&
330 verify_no_mergehead &&
331 test_cmp squash.1-5-9 .git/SQUASH_MSG
334 test_debug 'git log --graph --decorate --oneline --all'
336 test_expect_success 'merge c1 with c2 (no-commit in config)' '
337 git reset --hard c1 &&
338 test_config branch.master.mergeoptions "--no-commit" &&
340 verify_merge file result.1-5 &&
342 verify_mergeheads $c2
345 test_debug 'git log --graph --decorate --oneline --all'
347 test_expect_success 'merge c1 with c2 (log in config)' '
348 git reset --hard c1 &&
349 git merge --log c2 &&
350 git show -s --pretty=tformat:%s%n%b >expect &&
352 test_config branch.master.mergeoptions "--log" &&
353 git reset --hard c1 &&
355 git show -s --pretty=tformat:%s%n%b >actual &&
357 test_cmp expect actual
360 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
361 git reset --hard c1 &&
363 git show -s --pretty=tformat:%s%n%b >expect &&
365 test_config branch.master.mergeoptions "--no-log" &&
366 test_config merge.log "true" &&
367 git reset --hard c1 &&
369 git show -s --pretty=tformat:%s%n%b >actual &&
371 test_cmp expect actual
374 test_expect_success 'merge c1 with c2 (squash in config)' '
375 git reset --hard c1 &&
376 test_config branch.master.mergeoptions "--squash" &&
378 verify_merge file result.1-5 &&
380 verify_no_mergehead &&
381 test_cmp squash.1-5 .git/SQUASH_MSG
384 test_debug 'git log --graph --decorate --oneline --all'
386 test_expect_success 'override config option -n with --summary' '
387 git reset --hard c1 &&
388 test_config branch.master.mergeoptions "-n" &&
390 git merge --summary c2 >diffstat.txt &&
391 verify_merge file result.1-5 msg.1-5 &&
392 verify_parents $c1 $c2 &&
393 if ! grep "^ file | *2 +-$" diffstat.txt
395 echo "[OOPS] diffstat was not generated with --summary"
400 test_expect_success 'override config option -n with --stat' '
401 git reset --hard c1 &&
402 test_config branch.master.mergeoptions "-n" &&
404 git merge --stat c2 >diffstat.txt &&
405 verify_merge file result.1-5 msg.1-5 &&
406 verify_parents $c1 $c2 &&
407 if ! grep "^ file | *2 +-$" diffstat.txt
409 echo "[OOPS] diffstat was not generated with --stat"
414 test_debug 'git log --graph --decorate --oneline --all'
416 test_expect_success 'override config option --stat' '
417 git reset --hard c1 &&
418 test_config branch.master.mergeoptions "--stat" &&
420 git merge -n c2 >diffstat.txt &&
421 verify_merge file result.1-5 msg.1-5 &&
422 verify_parents $c1 $c2 &&
423 if grep "^ file | *2 +-$" diffstat.txt
425 echo "[OOPS] diffstat was generated"
430 test_debug 'git log --graph --decorate --oneline --all'
432 test_expect_success 'merge c1 with c2 (override --no-commit)' '
433 git reset --hard c1 &&
434 test_config branch.master.mergeoptions "--no-commit" &&
436 git merge --commit c2 &&
437 verify_merge file result.1-5 msg.1-5 &&
438 verify_parents $c1 $c2
441 test_debug 'git log --graph --decorate --oneline --all'
443 test_expect_success 'merge c1 with c2 (override --squash)' '
444 git reset --hard c1 &&
445 test_config branch.master.mergeoptions "--squash" &&
447 git merge --no-squash c2 &&
448 verify_merge file result.1-5 msg.1-5 &&
449 verify_parents $c1 $c2
452 test_debug 'git log --graph --decorate --oneline --all'
454 test_expect_success 'merge c0 with c1 (no-ff)' '
455 git reset --hard c0 &&
457 git merge --no-ff c1 &&
458 verify_merge file result.1 &&
459 verify_parents $c0 $c1
462 test_debug 'git log --graph --decorate --oneline --all'
464 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
465 git reset --hard c0 &&
466 test_config merge.ff "false" &&
469 verify_merge file result.1 &&
470 verify_parents $c0 $c1
472 test_debug 'git log --graph --decorate --oneline --all'
474 test_expect_success 'combine branch.master.mergeoptions with merge.ff' '
475 git reset --hard c0 &&
476 test_config branch.master.mergeoptions "--ff" &&
477 test_config merge.ff "false" &&
480 verify_merge file result.1 &&
484 test_expect_success 'tolerate unknown values for merge.ff' '
485 git reset --hard c0 &&
486 test_config merge.ff "something-new" &&
488 git merge c1 2>message &&
490 test_cmp empty message
493 test_expect_success 'combining --squash and --no-ff is refused' '
494 git reset --hard c0 &&
495 test_must_fail git merge --squash --no-ff c1 &&
496 test_must_fail git merge --no-ff --squash c1
499 test_expect_success 'option --ff-only overwrites --no-ff' '
500 git merge --no-ff --ff-only c1 &&
501 test_must_fail git merge --no-ff --ff-only c2
504 test_expect_success 'option --no-ff overrides merge.ff=only config' '
505 git reset --hard c0 &&
506 test_config merge.ff only &&
510 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
511 git reset --hard c0 &&
512 test_config branch.master.mergeoptions "--no-ff" &&
514 verify_merge file result.1 &&
518 test_expect_success 'merge log message' '
519 git reset --hard c0 &&
520 git merge --no-log c2 &&
521 git show -s --pretty=format:%b HEAD >msg.act &&
522 test_cmp msg.nologff msg.act &&
524 git reset --hard c0 &&
525 test_config branch.master.mergeoptions "--no-ff" &&
526 git merge --no-log c2 &&
527 git show -s --pretty=format:%b HEAD >msg.act &&
528 test_cmp msg.nolognoff msg.act &&
530 git merge --log c3 &&
531 git show -s --pretty=format:%b HEAD >msg.act &&
532 test_cmp msg.log msg.act &&
534 git reset --hard HEAD^ &&
535 test_config merge.log "yes" &&
537 git show -s --pretty=format:%b HEAD >msg.act &&
538 test_cmp msg.log msg.act
541 test_debug 'git log --graph --decorate --oneline --all'
543 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
544 git reset --hard c1 &&
546 git merge c0 c2 c0 c1 &&
547 verify_merge file result.1-5 &&
548 verify_parents $c1 $c2
551 test_debug 'git log --graph --decorate --oneline --all'
553 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
554 git reset --hard c1 &&
556 git merge c0 c2 c0 c1 &&
557 verify_merge file result.1-5 &&
558 verify_parents $c1 $c2
561 test_debug 'git log --graph --decorate --oneline --all'
563 test_expect_success 'merge c1 with c1 and c2' '
564 git reset --hard c1 &&
567 verify_merge file result.1-5 &&
568 verify_parents $c1 $c2
571 test_debug 'git log --graph --decorate --oneline --all'
573 test_expect_success 'merge fast-forward in a dirty tree' '
574 git reset --hard c0 &&
581 test_debug 'git log --graph --decorate --oneline --all'
583 test_expect_success 'in-index merge' '
584 git reset --hard c0 &&
585 git merge --no-ff -s resolve c1 >out &&
586 test_i18ngrep "Wonderful." out &&
587 verify_parents $c0 $c1
590 test_debug 'git log --graph --decorate --oneline --all'
592 test_expect_success 'refresh the index before merging' '
593 git reset --hard c1 &&
594 cp file file.n && mv -f file.n file &&
598 cat >expected.branch <<\EOF
599 Merge branch 'c5-branch' (early part)
601 cat >expected.tag <<\EOF
605 test_expect_success 'merge early part of c2' '
606 git reset --hard c3 &&
615 git reset --hard c3 &&
620 git branch -f c5-branch c5 &&
621 git merge c5-branch~1 &&
622 git show -s --pretty=tformat:%s HEAD >actual.branch &&
623 git reset --keep HEAD^ &&
625 git show -s --pretty=tformat:%s HEAD >actual.tag &&
626 test_cmp expected.branch actual.branch &&
627 test_cmp expected.tag actual.tag
630 test_debug 'git log --graph --decorate --oneline --all'
632 test_expect_success 'merge --no-ff --no-commit && commit' '
633 git reset --hard c0 &&
634 git merge --no-ff --no-commit c1 &&
635 EDITOR=: git commit &&
636 verify_parents $c0 $c1
639 test_debug 'git log --graph --decorate --oneline --all'
641 test_expect_success 'amending no-ff merge commit' '
642 EDITOR=: git commit --amend &&
643 verify_parents $c0 $c1
646 test_debug 'git log --graph --decorate --oneline --all'
650 # Add a new message string that was not in the template
652 echo "Merge work done on the side branch c1"
655 ) >"$1.tmp" && mv "$1.tmp" "$1"
656 # strip comments and blank lines from end of message
657 sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
661 test_expect_success 'merge --no-ff --edit' '
662 git reset --hard c0 &&
663 EDITOR=./editor git merge --no-ff --edit c1 &&
664 verify_parents $c0 $c1 &&
665 git cat-file commit HEAD >raw &&
666 grep "work done on the side branch" raw &&
667 sed "1,/^$/d" >actual raw &&
668 test_cmp actual expected
671 test_expect_success GPG 'merge --ff-only tag' '
672 git reset --hard c0 &&
673 git commit --allow-empty -m "A newer commit" &&
674 git tag -s -m "A newer commit" signed &&
675 git reset --hard c0 &&
677 git merge --ff-only signed &&
678 git rev-parse signed^0 >expect &&
679 git rev-parse HEAD >actual &&
680 test_cmp actual expect
683 test_expect_success GPG 'merge --no-edit tag should skip editor' '
684 git reset --hard c0 &&
685 git commit --allow-empty -m "A newer commit" &&
686 git tag -f -s -m "A newer commit" signed &&
687 git reset --hard c0 &&
689 EDITOR=false git merge --no-edit signed &&
690 git rev-parse signed^0 >expect &&
691 git rev-parse HEAD^2 >actual &&
692 test_cmp actual expect