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 test_expect_success 'set up test data and helpers' '
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 &&
42 echo "Merge commit '\''c2'\''" >msg.1-5 &&
43 echo "Merge commit '\''c2'\''; commit '\''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
61 echo "* commit '\''c3'\'':" &&
69 git update-index --refresh &&
70 git diff --exit-code &&
73 git show -s --pretty=format:%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_cmp parents.expected parents.actual
98 printf "%s\n" "$@" >mergehead.expected &&
99 test_cmp mergehead.expected .git/MERGE_HEAD
102 verify_no_mergehead() {
103 ! test -e .git/MERGE_HEAD
107 test_expect_success 'setup' '
110 git commit -m "commit 0" &&
112 c0=$(git rev-parse HEAD) &&
116 git commit -m "commit 1" &&
118 c1=$(git rev-parse HEAD) &&
119 git reset --hard "$c0" &&
123 git commit -m "commit 2" &&
125 c2=$(git rev-parse HEAD) &&
126 git reset --hard "$c0" &&
130 git commit -m "commit 3" &&
132 c3=$(git rev-parse HEAD)
133 git reset --hard "$c0" &&
137 test_debug 'git log --graph --decorate --oneline --all'
139 test_expect_success 'test option parsing' '
140 test_must_fail git merge -$ c1 &&
141 test_must_fail git merge --no-such c1 &&
142 test_must_fail git merge -s foobar c1 &&
143 test_must_fail git merge -s=foobar c1 &&
144 test_must_fail git merge -m &&
145 test_must_fail git merge
148 test_expect_success 'merge -h with invalid index' '
154 test_expect_code 129 git merge -h 2>usage
156 grep "[Uu]sage: git merge" broken/usage
159 test_expect_success 'reject non-strategy with a git-merge-foo name' '
160 test_must_fail git merge -s index c1
163 test_expect_success 'merge c0 with c1' '
164 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
166 git reset --hard c0 &&
168 verify_merge file result.1 &&
171 git reflog -1 >reflog.actual &&
172 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
173 test_cmp reflog.expected reflog.fuzzy
176 test_debug 'git log --graph --decorate --oneline --all'
178 test_expect_success 'merge c0 with c1 with --ff-only' '
179 git reset --hard c0 &&
180 git merge --ff-only c1 &&
181 git merge --ff-only HEAD c0 c1 &&
182 verify_merge file result.1 &&
186 test_debug 'git log --graph --decorate --oneline --all'
188 test_expect_success 'merge from unborn branch' '
189 git checkout -f master &&
190 test_might_fail git branch -D kid &&
192 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
194 git checkout --orphan kid &&
195 test_when_finished "git checkout -f master" &&
198 git merge --ff-only c1 &&
199 verify_merge file result.1 &&
202 git reflog -1 >reflog.actual &&
203 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
204 test_cmp reflog.expected reflog.fuzzy
207 test_debug 'git log --graph --decorate --oneline --all'
209 test_expect_success 'merge c1 with c2' '
210 git reset --hard c1 &&
213 verify_merge file result.1-5 msg.1-5 &&
214 verify_parents $c1 $c2
217 test_debug 'git log --graph --decorate --oneline --all'
219 test_expect_success 'merge c1 with c2 and c3' '
220 git reset --hard c1 &&
223 verify_merge file result.1-5-9 msg.1-5-9 &&
224 verify_parents $c1 $c2 $c3
227 test_debug 'git log --graph --decorate --oneline --all'
229 test_expect_success 'merges with --ff-only' '
230 git reset --hard c1 &&
232 test_must_fail git merge --ff-only c2 &&
233 test_must_fail git merge --ff-only c3 &&
234 test_must_fail git merge --ff-only c2 c3 &&
235 git reset --hard c0 &&
240 test_expect_success 'merges with merge.ff=only' '
241 git reset --hard c1 &&
243 test_when_finished "git config --unset merge.ff" &&
244 git config merge.ff only &&
245 test_must_fail git merge c2 &&
246 test_must_fail git merge c3 &&
247 test_must_fail git merge c2 c3 &&
248 git reset --hard c0 &&
253 test_expect_success 'merge c0 with c1 (no-commit)' '
254 git reset --hard c0 &&
255 git merge --no-commit c1 &&
256 verify_merge file result.1 &&
260 test_debug 'git log --graph --decorate --oneline --all'
262 test_expect_success 'merge c1 with c2 (no-commit)' '
263 git reset --hard c1 &&
264 git merge --no-commit c2 &&
265 verify_merge file result.1-5 &&
267 verify_mergeheads $c2
270 test_debug 'git log --graph --decorate --oneline --all'
272 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
273 git reset --hard c1 &&
274 git merge --no-commit c2 c3 &&
275 verify_merge file result.1-5-9 &&
277 verify_mergeheads $c2 $c3
280 test_debug 'git log --graph --decorate --oneline --all'
282 test_expect_success 'merge c0 with c1 (squash)' '
283 git reset --hard c0 &&
284 git merge --squash c1 &&
285 verify_merge file result.1 &&
287 verify_no_mergehead &&
288 test_cmp squash.1 .git/SQUASH_MSG
291 test_debug 'git log --graph --decorate --oneline --all'
293 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
294 git reset --hard c0 &&
295 git merge --squash --ff-only c1 &&
296 verify_merge file result.1 &&
298 verify_no_mergehead &&
299 test_cmp squash.1 .git/SQUASH_MSG
302 test_debug 'git log --graph --decorate --oneline --all'
304 test_expect_success 'merge c1 with c2 (squash)' '
305 git reset --hard c1 &&
306 git merge --squash c2 &&
307 verify_merge file result.1-5 &&
309 verify_no_mergehead &&
310 test_cmp squash.1-5 .git/SQUASH_MSG
313 test_debug 'git log --graph --decorate --oneline --all'
315 test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
316 git reset --hard c1 &&
317 test_must_fail git merge --squash --ff-only c2
320 test_debug 'git log --graph --decorate --oneline --all'
322 test_expect_success 'merge c1 with c2 and c3 (squash)' '
323 git reset --hard c1 &&
324 git merge --squash c2 c3 &&
325 verify_merge file result.1-5-9 &&
327 verify_no_mergehead &&
328 test_cmp squash.1-5-9 .git/SQUASH_MSG
331 test_debug 'git log --graph --decorate --oneline --all'
333 test_expect_success 'merge c1 with c2 (no-commit in config)' '
334 git reset --hard c1 &&
335 git config branch.master.mergeoptions "--no-commit" &&
337 verify_merge file result.1-5 &&
339 verify_mergeheads $c2
342 test_debug 'git log --graph --decorate --oneline --all'
344 test_expect_success 'merge c1 with c2 (log in config)' '
345 git config branch.master.mergeoptions "" &&
346 git reset --hard c1 &&
347 git merge --log c2 &&
348 git show -s --pretty=tformat:%s%n%b >expect &&
350 git config branch.master.mergeoptions --log &&
351 git reset --hard c1 &&
353 git show -s --pretty=tformat:%s%n%b >actual &&
355 test_cmp expect actual
358 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
359 test_when_finished "git config --remove-section branch.master" &&
360 test_when_finished "git config --remove-section merge" &&
361 test_might_fail git config --remove-section branch.master &&
362 test_might_fail git config --remove-section merge &&
364 git reset --hard c1 &&
366 git show -s --pretty=tformat:%s%n%b >expect &&
368 git config branch.master.mergeoptions "--no-log" &&
369 git config merge.log true &&
370 git reset --hard c1 &&
372 git show -s --pretty=tformat:%s%n%b >actual &&
374 test_cmp expect actual
377 test_expect_success 'merge c1 with c2 (squash in config)' '
378 git reset --hard c1 &&
379 git config branch.master.mergeoptions "--squash" &&
381 verify_merge file result.1-5 &&
383 verify_no_mergehead &&
384 test_cmp squash.1-5 .git/SQUASH_MSG
387 test_debug 'git log --graph --decorate --oneline --all'
389 test_expect_success 'override config option -n with --summary' '
390 git reset --hard c1 &&
391 git config branch.master.mergeoptions "-n" &&
393 git merge --summary c2 >diffstat.txt &&
394 verify_merge file result.1-5 msg.1-5 &&
395 verify_parents $c1 $c2 &&
396 if ! grep "^ file | *2 +-$" diffstat.txt
398 echo "[OOPS] diffstat was not generated with --summary"
403 test_expect_success 'override config option -n with --stat' '
404 git reset --hard c1 &&
405 git config branch.master.mergeoptions "-n" &&
407 git merge --stat c2 >diffstat.txt &&
408 verify_merge file result.1-5 msg.1-5 &&
409 verify_parents $c1 $c2 &&
410 if ! grep "^ file | *2 +-$" diffstat.txt
412 echo "[OOPS] diffstat was not generated with --stat"
417 test_debug 'git log --graph --decorate --oneline --all'
419 test_expect_success 'override config option --stat' '
420 git reset --hard c1 &&
421 git config branch.master.mergeoptions "--stat" &&
423 git merge -n c2 >diffstat.txt &&
424 verify_merge file result.1-5 msg.1-5 &&
425 verify_parents $c1 $c2 &&
426 if grep "^ file | *2 +-$" diffstat.txt
428 echo "[OOPS] diffstat was generated"
433 test_debug 'git log --graph --decorate --oneline --all'
435 test_expect_success 'merge c1 with c2 (override --no-commit)' '
436 git reset --hard c1 &&
437 git config branch.master.mergeoptions "--no-commit" &&
439 git merge --commit c2 &&
440 verify_merge file result.1-5 msg.1-5 &&
441 verify_parents $c1 $c2
444 test_debug 'git log --graph --decorate --oneline --all'
446 test_expect_success 'merge c1 with c2 (override --squash)' '
447 git reset --hard c1 &&
448 git config branch.master.mergeoptions "--squash" &&
450 git merge --no-squash c2 &&
451 verify_merge file result.1-5 msg.1-5 &&
452 verify_parents $c1 $c2
455 test_debug 'git log --graph --decorate --oneline --all'
457 test_expect_success 'merge c0 with c1 (no-ff)' '
458 git reset --hard c0 &&
459 git config branch.master.mergeoptions "" &&
461 git merge --no-ff c1 &&
462 verify_merge file result.1 &&
463 verify_parents $c0 $c1
466 test_debug 'git log --graph --decorate --oneline --all'
468 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
469 git reset --hard c0 &&
470 git config merge.ff false &&
473 git config --remove-section merge &&
474 verify_merge file result.1 &&
475 verify_parents $c0 $c1
477 test_debug 'git log --graph --decorate --oneline --all'
479 test_expect_success 'combine branch.master.mergeoptions with merge.ff' '
480 git reset --hard c0 &&
481 git config branch.master.mergeoptions --ff &&
482 git config merge.ff false &&
485 git config --remove-section "branch.master" &&
486 git config --remove-section "merge" &&
487 verify_merge file result.1 &&
491 test_expect_success 'tolerate unknown values for merge.ff' '
492 git reset --hard c0 &&
493 git config merge.ff something-new &&
495 git merge c1 2>message &&
496 git config --remove-section "merge" &&
498 test_cmp empty message
501 test_expect_success 'combining --squash and --no-ff is refused' '
502 git reset --hard c0 &&
503 test_must_fail git merge --squash --no-ff c1 &&
504 test_must_fail git merge --no-ff --squash c1
507 test_expect_success 'combining --ff-only and --no-ff is refused' '
508 test_must_fail git merge --ff-only --no-ff c1 &&
509 test_must_fail git merge --no-ff --ff-only c1
512 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
513 git reset --hard c0 &&
514 git config branch.master.mergeoptions "--no-ff" &&
516 verify_merge file result.1 &&
520 test_expect_success 'merge log message' '
521 git reset --hard c0 &&
522 git merge --no-log c2 &&
523 git show -s --pretty=format:%b HEAD >msg.act &&
524 test_cmp msg.nolog msg.act &&
526 git merge --log c3 &&
527 git show -s --pretty=format:%b HEAD >msg.act &&
528 test_cmp msg.log msg.act &&
530 git reset --hard HEAD^ &&
531 git config merge.log yes &&
533 git show -s --pretty=format:%b HEAD >msg.act &&
534 test_cmp msg.log msg.act
537 test_debug 'git log --graph --decorate --oneline --all'
539 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
540 git reset --hard c1 &&
541 git config branch.master.mergeoptions "" &&
543 git merge c0 c2 c0 c1 &&
544 verify_merge file result.1-5 &&
545 verify_parents $c1 $c2
548 test_debug 'git log --graph --decorate --oneline --all'
550 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
551 git reset --hard c1 &&
552 git config branch.master.mergeoptions "" &&
554 git merge c0 c2 c0 c1 &&
555 verify_merge file result.1-5 &&
556 verify_parents $c1 $c2
559 test_debug 'git log --graph --decorate --oneline --all'
561 test_expect_success 'merge c1 with c1 and c2' '
562 git reset --hard c1 &&
563 git config branch.master.mergeoptions "" &&
566 verify_merge file result.1-5 &&
567 verify_parents $c1 $c2
570 test_debug 'git log --graph --decorate --oneline --all'
572 test_expect_success 'merge fast-forward in a dirty tree' '
573 git reset --hard c0 &&
580 test_debug 'git log --graph --decorate --oneline --all'
582 test_expect_success C_LOCALE_OUTPUT 'in-index merge' '
583 git reset --hard c0 &&
584 git merge --no-ff -s resolve c1 >out &&
585 grep "Wonderful." out &&
586 verify_parents $c0 $c1
589 test_debug 'git log --graph --decorate --oneline --all'
591 test_expect_success 'refresh the index before merging' '
592 git reset --hard c1 &&
593 cp file file.n && mv -f file.n file &&
597 cat >expected.branch <<\EOF
598 Merge branch 'c5-branch' (early part)
600 cat >expected.tag <<\EOF
604 test_expect_success 'merge early part of c2' '
605 git reset --hard c3 &&
614 git reset --hard c3 &&
619 git branch -f c5-branch c5 &&
620 git merge c5-branch~1 &&
621 git show -s --pretty=format:%s HEAD >actual.branch &&
622 git reset --keep HEAD^ &&
624 git show -s --pretty=format:%s HEAD >actual.tag &&
625 test_cmp expected.branch actual.branch &&
626 test_cmp expected.tag actual.tag
629 test_debug 'git log --graph --decorate --oneline --all'
631 test_expect_success 'merge --no-ff --no-commit && commit' '
632 git reset --hard c0 &&
633 git merge --no-ff --no-commit c1 &&
634 EDITOR=: git commit &&
635 verify_parents $c0 $c1
638 test_debug 'git log --graph --decorate --oneline --all'
640 test_expect_success 'amending no-ff merge commit' '
641 EDITOR=: git commit --amend &&
642 verify_parents $c0 $c1
645 test_debug 'git log --graph --decorate --oneline --all'