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 tag 'c2'" >msg.1-5 &&
42 echo "Merge tags 'c2' and '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
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 &&
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 3" &&
135 c3=$(git rev-parse HEAD)
136 git reset --hard "$c0" &&
140 test_debug 'git log --graph --decorate --oneline --all'
142 test_expect_success 'test option parsing' '
143 test_must_fail git merge -$ c1 &&
144 test_must_fail git merge --no-such c1 &&
145 test_must_fail git merge -s foobar c1 &&
146 test_must_fail git merge -s=foobar c1 &&
147 test_must_fail git merge -m &&
148 test_must_fail git merge
151 test_expect_success 'merge -h with invalid index' '
157 test_expect_code 129 git merge -h 2>usage
159 grep "[Uu]sage: git merge" broken/usage
162 test_expect_success 'reject non-strategy with a git-merge-foo name' '
163 test_must_fail git merge -s index c1
166 test_expect_success 'merge c0 with c1' '
167 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
169 git reset --hard c0 &&
171 verify_merge file result.1 &&
174 git reflog -1 >reflog.actual &&
175 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
176 test_cmp reflog.expected reflog.fuzzy
179 test_debug 'git log --graph --decorate --oneline --all'
181 test_expect_success 'merge c0 with c1 with --ff-only' '
182 git reset --hard c0 &&
183 git merge --ff-only c1 &&
184 git merge --ff-only HEAD c0 c1 &&
185 verify_merge file result.1 &&
189 test_debug 'git log --graph --decorate --oneline --all'
191 test_expect_success 'merge from unborn branch' '
192 git checkout -f master &&
193 test_might_fail git branch -D kid &&
195 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
197 git checkout --orphan kid &&
198 test_when_finished "git checkout -f master" &&
201 git merge --ff-only c1 &&
202 verify_merge file result.1 &&
205 git reflog -1 >reflog.actual &&
206 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
207 test_cmp reflog.expected reflog.fuzzy
210 test_debug 'git log --graph --decorate --oneline --all'
212 test_expect_success 'merge c1 with c2' '
213 git reset --hard c1 &&
216 verify_merge file result.1-5 msg.1-5 &&
217 verify_parents $c1 $c2
220 test_debug 'git log --graph --decorate --oneline --all'
222 test_expect_success 'merge c1 with c2 and c3' '
223 git reset --hard c1 &&
226 verify_merge file result.1-5-9 msg.1-5-9 &&
227 verify_parents $c1 $c2 $c3
230 test_debug 'git log --graph --decorate --oneline --all'
232 test_expect_success 'merges with --ff-only' '
233 git reset --hard c1 &&
235 test_must_fail git merge --ff-only c2 &&
236 test_must_fail git merge --ff-only c3 &&
237 test_must_fail git merge --ff-only c2 c3 &&
238 git reset --hard c0 &&
243 test_expect_success 'merges with merge.ff=only' '
244 git reset --hard c1 &&
246 test_when_finished "git config --unset merge.ff" &&
247 git 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 'unsuccesful 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 git 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 config branch.master.mergeoptions "" &&
349 git reset --hard c1 &&
350 git merge --log c2 &&
351 git show -s --pretty=tformat:%s%n%b >expect &&
353 git config branch.master.mergeoptions --log &&
354 git reset --hard c1 &&
356 git show -s --pretty=tformat:%s%n%b >actual &&
358 test_cmp expect actual
361 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
362 test_when_finished "git config --remove-section branch.master" &&
363 test_when_finished "git config --remove-section merge" &&
364 test_might_fail git config --remove-section branch.master &&
365 test_might_fail git config --remove-section merge &&
367 git reset --hard c1 &&
369 git show -s --pretty=tformat:%s%n%b >expect &&
371 git config branch.master.mergeoptions "--no-log" &&
372 git config merge.log true &&
373 git reset --hard c1 &&
375 git show -s --pretty=tformat:%s%n%b >actual &&
377 test_cmp expect actual
380 test_expect_success 'merge c1 with c2 (squash in config)' '
381 git reset --hard c1 &&
382 git config branch.master.mergeoptions "--squash" &&
384 verify_merge file result.1-5 &&
386 verify_no_mergehead &&
387 test_cmp squash.1-5 .git/SQUASH_MSG
390 test_debug 'git log --graph --decorate --oneline --all'
392 test_expect_success 'override config option -n with --summary' '
393 git reset --hard c1 &&
394 git config branch.master.mergeoptions "-n" &&
396 git merge --summary c2 >diffstat.txt &&
397 verify_merge file result.1-5 msg.1-5 &&
398 verify_parents $c1 $c2 &&
399 if ! grep "^ file | *2 +-$" diffstat.txt
401 echo "[OOPS] diffstat was not generated with --summary"
406 test_expect_success 'override config option -n with --stat' '
407 git reset --hard c1 &&
408 git config branch.master.mergeoptions "-n" &&
410 git merge --stat c2 >diffstat.txt &&
411 verify_merge file result.1-5 msg.1-5 &&
412 verify_parents $c1 $c2 &&
413 if ! grep "^ file | *2 +-$" diffstat.txt
415 echo "[OOPS] diffstat was not generated with --stat"
420 test_debug 'git log --graph --decorate --oneline --all'
422 test_expect_success 'override config option --stat' '
423 git reset --hard c1 &&
424 git config branch.master.mergeoptions "--stat" &&
426 git merge -n c2 >diffstat.txt &&
427 verify_merge file result.1-5 msg.1-5 &&
428 verify_parents $c1 $c2 &&
429 if grep "^ file | *2 +-$" diffstat.txt
431 echo "[OOPS] diffstat was generated"
436 test_debug 'git log --graph --decorate --oneline --all'
438 test_expect_success 'merge c1 with c2 (override --no-commit)' '
439 git reset --hard c1 &&
440 git config branch.master.mergeoptions "--no-commit" &&
442 git merge --commit c2 &&
443 verify_merge file result.1-5 msg.1-5 &&
444 verify_parents $c1 $c2
447 test_debug 'git log --graph --decorate --oneline --all'
449 test_expect_success 'merge c1 with c2 (override --squash)' '
450 git reset --hard c1 &&
451 git config branch.master.mergeoptions "--squash" &&
453 git merge --no-squash c2 &&
454 verify_merge file result.1-5 msg.1-5 &&
455 verify_parents $c1 $c2
458 test_debug 'git log --graph --decorate --oneline --all'
460 test_expect_success 'merge c0 with c1 (no-ff)' '
461 git reset --hard c0 &&
462 git config branch.master.mergeoptions "" &&
464 git merge --no-ff c1 &&
465 verify_merge file result.1 &&
466 verify_parents $c0 $c1
469 test_debug 'git log --graph --decorate --oneline --all'
471 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
472 git reset --hard c0 &&
473 git config merge.ff false &&
476 git config --remove-section merge &&
477 verify_merge file result.1 &&
478 verify_parents $c0 $c1
480 test_debug 'git log --graph --decorate --oneline --all'
482 test_expect_success 'combine branch.master.mergeoptions with merge.ff' '
483 git reset --hard c0 &&
484 git config branch.master.mergeoptions --ff &&
485 git config merge.ff false &&
488 git config --remove-section "branch.master" &&
489 git config --remove-section "merge" &&
490 verify_merge file result.1 &&
494 test_expect_success 'tolerate unknown values for merge.ff' '
495 git reset --hard c0 &&
496 git config merge.ff something-new &&
498 git merge c1 2>message &&
499 git config --remove-section "merge" &&
501 test_cmp empty message
504 test_expect_success 'combining --squash and --no-ff is refused' '
505 git reset --hard c0 &&
506 test_must_fail git merge --squash --no-ff c1 &&
507 test_must_fail git merge --no-ff --squash c1
510 test_expect_success 'combining --ff-only and --no-ff is refused' '
511 test_must_fail git merge --ff-only --no-ff c1 &&
512 test_must_fail git merge --no-ff --ff-only c1
515 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
516 git reset --hard c0 &&
517 git config branch.master.mergeoptions "--no-ff" &&
519 verify_merge file result.1 &&
523 test_expect_success 'merge log message' '
524 git reset --hard c0 &&
525 git merge --no-log c2 &&
526 git show -s --pretty=format:%b HEAD >msg.act &&
527 test_cmp msg.nolog msg.act &&
529 git merge --log c3 &&
530 git show -s --pretty=format:%b HEAD >msg.act &&
531 test_cmp msg.log msg.act &&
533 git reset --hard HEAD^ &&
534 git config merge.log yes &&
536 git show -s --pretty=format:%b HEAD >msg.act &&
537 test_cmp msg.log msg.act
540 test_debug 'git log --graph --decorate --oneline --all'
542 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
543 git reset --hard c1 &&
544 git config branch.master.mergeoptions "" &&
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 &&
555 git config branch.master.mergeoptions "" &&
557 git merge c0 c2 c0 c1 &&
558 verify_merge file result.1-5 &&
559 verify_parents $c1 $c2
562 test_debug 'git log --graph --decorate --oneline --all'
564 test_expect_success 'merge c1 with c1 and c2' '
565 git reset --hard c1 &&
566 git config branch.master.mergeoptions "" &&
569 verify_merge file result.1-5 &&
570 verify_parents $c1 $c2
573 test_debug 'git log --graph --decorate --oneline --all'
575 test_expect_success 'merge fast-forward in a dirty tree' '
576 git reset --hard c0 &&
583 test_debug 'git log --graph --decorate --oneline --all'
585 test_expect_success 'in-index merge' '
586 git reset --hard c0 &&
587 git merge --no-ff -s resolve c1 >out &&
588 test_i18ngrep "Wonderful." out &&
589 verify_parents $c0 $c1
592 test_debug 'git log --graph --decorate --oneline --all'
594 test_expect_success 'refresh the index before merging' '
595 git reset --hard c1 &&
596 cp file file.n && mv -f file.n file &&
600 cat >expected.branch <<\EOF
601 Merge branch 'c5-branch' (early part)
603 cat >expected.tag <<\EOF
607 test_expect_success 'merge early part of c2' '
608 git reset --hard c3 &&
617 git reset --hard c3 &&
622 git branch -f c5-branch c5 &&
623 git merge c5-branch~1 &&
624 git show -s --pretty=format:%s HEAD >actual.branch &&
625 git reset --keep HEAD^ &&
627 git show -s --pretty=format:%s HEAD >actual.tag &&
628 test_cmp expected.branch actual.branch &&
629 test_cmp expected.tag actual.tag
632 test_debug 'git log --graph --decorate --oneline --all'
634 test_expect_success 'merge --no-ff --no-commit && commit' '
635 git reset --hard c0 &&
636 git merge --no-ff --no-commit c1 &&
637 EDITOR=: git commit &&
638 verify_parents $c0 $c1
641 test_debug 'git log --graph --decorate --oneline --all'
643 test_expect_success 'amending no-ff merge commit' '
644 EDITOR=: git commit --amend &&
645 verify_parents $c0 $c1
648 test_debug 'git log --graph --decorate --oneline --all'
652 # Add a new message string that was not in the template
654 echo "Merge work done on the side branch c1"
657 ) >"$1.tmp" && mv "$1.tmp" "$1"
658 # strip comments and blank lines from end of message
659 sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
663 test_expect_success 'merge --no-ff --edit' '
664 git reset --hard c0 &&
665 EDITOR=./editor git merge --no-ff --edit c1 &&
666 verify_parents $c0 $c1 &&
667 git cat-file commit HEAD >raw &&
668 grep "work done on the side branch" raw &&
669 sed "1,/^$/d" >actual raw &&
670 test_cmp actual expected