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
31 printf '%s\n' 1 2 3 4 5 6 7 8 9 >file
32 printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >file.1
33 printf '%s\n' 1 2 3 4 '5 X' 6 7 8 9 >file.5
34 printf '%s\n' 1 2 3 4 5 6 7 8 '9 X' >file.9
35 printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >result.1
36 printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
37 printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
40 create_merge_msgs () {
41 echo "Merge commit 'c2'" >msg.1-5 &&
42 echo "Merge commit 'c2'; commit 'c3'" >msg.1-5-9 &&
44 echo "Squashed commit of the following:" &&
46 git log --no-merges ^HEAD c1
49 echo "Squashed commit of the following:" &&
51 git log --no-merges ^HEAD c2
54 echo "Squashed commit of the following:" &&
56 git log --no-merges ^HEAD c2 c3
60 echo "* commit 'c3':" &&
68 git update-index --refresh &&
69 git diff --exit-code &&
72 git show -s --pretty=format:%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 &&
99 test_cmp mergehead.expected .git/MERGE_HEAD
102 verify_no_mergehead () {
103 ! test -e .git/MERGE_HEAD
106 test_expect_success 'setup' '
109 git commit -m "commit 0" &&
111 c0=$(git rev-parse HEAD) &&
115 git commit -m "commit 1" &&
117 c1=$(git rev-parse HEAD) &&
118 git reset --hard "$c0" &&
122 git commit -m "commit 2" &&
124 c2=$(git rev-parse HEAD) &&
125 git reset --hard "$c0" &&
129 git commit -m "commit 3" &&
131 c3=$(git rev-parse HEAD)
132 git reset --hard "$c0" &&
136 test_debug 'git log --graph --decorate --oneline --all'
138 test_expect_success 'test option parsing' '
139 test_must_fail git merge -$ c1 &&
140 test_must_fail git merge --no-such c1 &&
141 test_must_fail git merge -s foobar c1 &&
142 test_must_fail git merge -s=foobar c1 &&
143 test_must_fail git merge -m &&
144 test_must_fail git merge
147 test_expect_success 'merge -h with invalid index' '
153 test_expect_code 129 git merge -h 2>usage
155 grep "[Uu]sage: git merge" broken/usage
158 test_expect_success 'reject non-strategy with a git-merge-foo name' '
159 test_must_fail git merge -s index c1
162 test_expect_success 'merge c0 with c1' '
163 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
165 git reset --hard c0 &&
167 verify_merge file result.1 &&
170 git reflog -1 >reflog.actual &&
171 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
172 test_cmp reflog.expected reflog.fuzzy
175 test_debug 'git log --graph --decorate --oneline --all'
177 test_expect_success 'merge c0 with c1 with --ff-only' '
178 git reset --hard c0 &&
179 git merge --ff-only c1 &&
180 git merge --ff-only HEAD c0 c1 &&
181 verify_merge file result.1 &&
185 test_debug 'git log --graph --decorate --oneline --all'
187 test_expect_success 'merge from unborn branch' '
188 git checkout -f master &&
189 test_might_fail git branch -D kid &&
191 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
193 git checkout --orphan kid &&
194 test_when_finished "git checkout -f master" &&
197 git merge --ff-only c1 &&
198 verify_merge file result.1 &&
201 git reflog -1 >reflog.actual &&
202 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
203 test_cmp reflog.expected reflog.fuzzy
206 test_debug 'git log --graph --decorate --oneline --all'
208 test_expect_success 'merge c1 with c2' '
209 git reset --hard c1 &&
212 verify_merge file result.1-5 msg.1-5 &&
213 verify_parents $c1 $c2
216 test_debug 'git log --graph --decorate --oneline --all'
218 test_expect_success 'merge c1 with c2 and c3' '
219 git reset --hard c1 &&
222 verify_merge file result.1-5-9 msg.1-5-9 &&
223 verify_parents $c1 $c2 $c3
226 test_debug 'git log --graph --decorate --oneline --all'
228 test_expect_success 'merges with --ff-only' '
229 git reset --hard c1 &&
231 test_must_fail git merge --ff-only c2 &&
232 test_must_fail git merge --ff-only c3 &&
233 test_must_fail git merge --ff-only c2 c3 &&
234 git reset --hard c0 &&
239 test_expect_success 'merges with merge.ff=only' '
240 git reset --hard c1 &&
242 test_when_finished "git config --unset merge.ff" &&
243 git config merge.ff only &&
244 test_must_fail git merge c2 &&
245 test_must_fail git merge c3 &&
246 test_must_fail git merge c2 c3 &&
247 git reset --hard c0 &&
252 test_expect_success 'merge c0 with c1 (no-commit)' '
253 git reset --hard c0 &&
254 git merge --no-commit c1 &&
255 verify_merge file result.1 &&
259 test_debug 'git log --graph --decorate --oneline --all'
261 test_expect_success 'merge c1 with c2 (no-commit)' '
262 git reset --hard c1 &&
263 git merge --no-commit c2 &&
264 verify_merge file result.1-5 &&
266 verify_mergeheads $c2
269 test_debug 'git log --graph --decorate --oneline --all'
271 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
272 git reset --hard c1 &&
273 git merge --no-commit c2 c3 &&
274 verify_merge file result.1-5-9 &&
276 verify_mergeheads $c2 $c3
279 test_debug 'git log --graph --decorate --oneline --all'
281 test_expect_success 'merge c0 with c1 (squash)' '
282 git reset --hard c0 &&
283 git merge --squash c1 &&
284 verify_merge file result.1 &&
286 verify_no_mergehead &&
287 test_cmp squash.1 .git/SQUASH_MSG
290 test_debug 'git log --graph --decorate --oneline --all'
292 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
293 git reset --hard c0 &&
294 git merge --squash --ff-only c1 &&
295 verify_merge file result.1 &&
297 verify_no_mergehead &&
298 test_cmp squash.1 .git/SQUASH_MSG
301 test_debug 'git log --graph --decorate --oneline --all'
303 test_expect_success 'merge c1 with c2 (squash)' '
304 git reset --hard c1 &&
305 git merge --squash c2 &&
306 verify_merge file result.1-5 &&
308 verify_no_mergehead &&
309 test_cmp squash.1-5 .git/SQUASH_MSG
312 test_debug 'git log --graph --decorate --oneline --all'
314 test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
315 git reset --hard c1 &&
316 test_must_fail git merge --squash --ff-only c2
319 test_debug 'git log --graph --decorate --oneline --all'
321 test_expect_success 'merge c1 with c2 and c3 (squash)' '
322 git reset --hard c1 &&
323 git merge --squash c2 c3 &&
324 verify_merge file result.1-5-9 &&
326 verify_no_mergehead &&
327 test_cmp squash.1-5-9 .git/SQUASH_MSG
330 test_debug 'git log --graph --decorate --oneline --all'
332 test_expect_success 'merge c1 with c2 (no-commit in config)' '
333 git reset --hard c1 &&
334 git config branch.master.mergeoptions "--no-commit" &&
336 verify_merge file result.1-5 &&
338 verify_mergeheads $c2
341 test_debug 'git log --graph --decorate --oneline --all'
343 test_expect_success 'merge c1 with c2 (log in config)' '
344 git config branch.master.mergeoptions "" &&
345 git reset --hard c1 &&
346 git merge --log c2 &&
347 git show -s --pretty=tformat:%s%n%b >expect &&
349 git config branch.master.mergeoptions --log &&
350 git reset --hard c1 &&
352 git show -s --pretty=tformat:%s%n%b >actual &&
354 test_cmp expect actual
357 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
358 test_when_finished "git config --remove-section branch.master" &&
359 test_when_finished "git config --remove-section merge" &&
360 test_might_fail git config --remove-section branch.master &&
361 test_might_fail git config --remove-section merge &&
363 git reset --hard c1 &&
365 git show -s --pretty=tformat:%s%n%b >expect &&
367 git config branch.master.mergeoptions "--no-log" &&
368 git config merge.log true &&
369 git reset --hard c1 &&
371 git show -s --pretty=tformat:%s%n%b >actual &&
373 test_cmp expect actual
376 test_expect_success 'merge c1 with c2 (squash in config)' '
377 git reset --hard c1 &&
378 git config branch.master.mergeoptions "--squash" &&
380 verify_merge file result.1-5 &&
382 verify_no_mergehead &&
383 test_cmp squash.1-5 .git/SQUASH_MSG
386 test_debug 'git log --graph --decorate --oneline --all'
388 test_expect_success 'override config option -n with --summary' '
389 git reset --hard c1 &&
390 git config branch.master.mergeoptions "-n" &&
392 git merge --summary c2 >diffstat.txt &&
393 verify_merge file result.1-5 msg.1-5 &&
394 verify_parents $c1 $c2 &&
395 if ! grep "^ file | *2 +-$" diffstat.txt
397 echo "[OOPS] diffstat was not generated with --summary"
402 test_expect_success 'override config option -n with --stat' '
403 git reset --hard c1 &&
404 git config branch.master.mergeoptions "-n" &&
406 git merge --stat c2 >diffstat.txt &&
407 verify_merge file result.1-5 msg.1-5 &&
408 verify_parents $c1 $c2 &&
409 if ! grep "^ file | *2 +-$" diffstat.txt
411 echo "[OOPS] diffstat was not generated with --stat"
416 test_debug 'git log --graph --decorate --oneline --all'
418 test_expect_success 'override config option --stat' '
419 git reset --hard c1 &&
420 git config branch.master.mergeoptions "--stat" &&
422 git merge -n 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 generated"
432 test_debug 'git log --graph --decorate --oneline --all'
434 test_expect_success 'merge c1 with c2 (override --no-commit)' '
435 git reset --hard c1 &&
436 git config branch.master.mergeoptions "--no-commit" &&
438 git merge --commit c2 &&
439 verify_merge file result.1-5 msg.1-5 &&
440 verify_parents $c1 $c2
443 test_debug 'git log --graph --decorate --oneline --all'
445 test_expect_success 'merge c1 with c2 (override --squash)' '
446 git reset --hard c1 &&
447 git config branch.master.mergeoptions "--squash" &&
449 git merge --no-squash c2 &&
450 verify_merge file result.1-5 msg.1-5 &&
451 verify_parents $c1 $c2
454 test_debug 'git log --graph --decorate --oneline --all'
456 test_expect_success 'merge c0 with c1 (no-ff)' '
457 git reset --hard c0 &&
458 git config branch.master.mergeoptions "" &&
460 git merge --no-ff c1 &&
461 verify_merge file result.1 &&
462 verify_parents $c0 $c1
465 test_debug 'git log --graph --decorate --oneline --all'
467 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
468 git reset --hard c0 &&
469 git config merge.ff false &&
472 git config --remove-section merge &&
473 verify_merge file result.1 &&
474 verify_parents $c0 $c1
476 test_debug 'git log --graph --decorate --oneline --all'
478 test_expect_success 'combine branch.master.mergeoptions with merge.ff' '
479 git reset --hard c0 &&
480 git config branch.master.mergeoptions --ff &&
481 git config merge.ff false &&
484 git config --remove-section "branch.master" &&
485 git config --remove-section "merge" &&
486 verify_merge file result.1 &&
490 test_expect_success 'tolerate unknown values for merge.ff' '
491 git reset --hard c0 &&
492 git config merge.ff something-new &&
494 git merge c1 2>message &&
495 git config --remove-section "merge" &&
497 test_cmp empty message
500 test_expect_success 'combining --squash and --no-ff is refused' '
501 git reset --hard c0 &&
502 test_must_fail git merge --squash --no-ff c1 &&
503 test_must_fail git merge --no-ff --squash c1
506 test_expect_success 'combining --ff-only and --no-ff is refused' '
507 test_must_fail git merge --ff-only --no-ff c1 &&
508 test_must_fail git merge --no-ff --ff-only c1
511 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
512 git reset --hard c0 &&
513 git config branch.master.mergeoptions "--no-ff" &&
515 verify_merge file result.1 &&
519 test_expect_success 'merge log message' '
520 git reset --hard c0 &&
521 git merge --no-log c2 &&
522 git show -s --pretty=format:%b HEAD >msg.act &&
523 test_cmp msg.nolog msg.act &&
525 git merge --log c3 &&
526 git show -s --pretty=format:%b HEAD >msg.act &&
527 test_cmp msg.log msg.act &&
529 git reset --hard HEAD^ &&
530 git config merge.log yes &&
532 git show -s --pretty=format:%b HEAD >msg.act &&
533 test_cmp msg.log msg.act
536 test_debug 'git log --graph --decorate --oneline --all'
538 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
539 git reset --hard c1 &&
540 git config branch.master.mergeoptions "" &&
542 git merge c0 c2 c0 c1 &&
543 verify_merge file result.1-5 &&
544 verify_parents $c1 $c2
547 test_debug 'git log --graph --decorate --oneline --all'
549 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
550 git reset --hard c1 &&
551 git config branch.master.mergeoptions "" &&
553 git merge c0 c2 c0 c1 &&
554 verify_merge file result.1-5 &&
555 verify_parents $c1 $c2
558 test_debug 'git log --graph --decorate --oneline --all'
560 test_expect_success 'merge c1 with c1 and c2' '
561 git reset --hard c1 &&
562 git config branch.master.mergeoptions "" &&
565 verify_merge file result.1-5 &&
566 verify_parents $c1 $c2
569 test_debug 'git log --graph --decorate --oneline --all'
571 test_expect_success 'merge fast-forward in a dirty tree' '
572 git reset --hard c0 &&
579 test_debug 'git log --graph --decorate --oneline --all'
581 test_expect_success 'in-index merge' '
582 git reset --hard c0 &&
583 git merge --no-ff -s resolve c1 >out &&
584 test_i18ngrep "Wonderful." out &&
585 verify_parents $c0 $c1
588 test_debug 'git log --graph --decorate --oneline --all'
590 test_expect_success 'refresh the index before merging' '
591 git reset --hard c1 &&
592 cp file file.n && mv -f file.n file &&
596 cat >expected.branch <<\EOF
597 Merge branch 'c5-branch' (early part)
599 cat >expected.tag <<\EOF
603 test_expect_success 'merge early part of c2' '
604 git reset --hard c3 &&
613 git reset --hard c3 &&
618 git branch -f c5-branch c5 &&
619 git merge c5-branch~1 &&
620 git show -s --pretty=format:%s HEAD >actual.branch &&
621 git reset --keep HEAD^ &&
623 git show -s --pretty=format:%s HEAD >actual.tag &&
624 test_cmp expected.branch actual.branch &&
625 test_cmp expected.tag actual.tag
628 test_debug 'git log --graph --decorate --oneline --all'
630 test_expect_success 'merge --no-ff --no-commit && commit' '
631 git reset --hard c0 &&
632 git merge --no-ff --no-commit c1 &&
633 EDITOR=: git commit &&
634 verify_parents $c0 $c1
637 test_debug 'git log --graph --decorate --oneline --all'
639 test_expect_success 'amending no-ff merge commit' '
640 EDITOR=: git commit --amend &&
641 verify_parents $c0 $c1
644 test_debug 'git log --graph --decorate --oneline --all'
648 # Add a new message string that was not in the template
650 echo "Merge work done on the side branch c1"
653 ) >"$1.tmp" && mv "$1.tmp" "$1"
654 # strip comments and blank lines from end of message
655 sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
659 test_expect_success 'merge --no-ff --edit' '
660 git reset --hard c0 &&
661 EDITOR=./editor git merge --no-ff --edit c1 &&
662 verify_parents $c0 $c1 &&
663 git cat-file commit HEAD >raw &&
664 grep "work done on the side branch" raw &&
665 sed "1,/^$/d" >actual raw &&
666 test_cmp actual expected