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=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_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_when_finished "git config --unset merge.ff" &&
248 git config merge.ff only &&
249 test_must_fail git merge c2 &&
250 test_must_fail git merge c3 &&
251 test_must_fail git merge c2 c3 &&
252 git reset --hard c0 &&
257 test_expect_success
'merge c0 with c1 (no-commit)' '
258 git reset --hard c0 &&
259 git merge --no-commit c1 &&
260 verify_merge file result.1 &&
264 test_debug
'git log --graph --decorate --oneline --all'
266 test_expect_success
'merge c1 with c2 (no-commit)' '
267 git reset --hard c1 &&
268 git merge --no-commit c2 &&
269 verify_merge file result.1-5 &&
271 verify_mergeheads $c2
274 test_debug
'git log --graph --decorate --oneline --all'
276 test_expect_success
'merge c1 with c2 and c3 (no-commit)' '
277 git reset --hard c1 &&
278 git merge --no-commit c2 c3 &&
279 verify_merge file result.1-5-9 &&
281 verify_mergeheads $c2 $c3
284 test_debug
'git log --graph --decorate --oneline --all'
286 test_expect_success
'merge c0 with c1 (squash)' '
287 git reset --hard c0 &&
288 git merge --squash c1 &&
289 verify_merge file result.1 &&
291 verify_no_mergehead &&
292 test_cmp squash.1 .git/SQUASH_MSG
295 test_debug
'git log --graph --decorate --oneline --all'
297 test_expect_success
'merge c0 with c1 (squash, ff-only)' '
298 git reset --hard c0 &&
299 git merge --squash --ff-only c1 &&
300 verify_merge file result.1 &&
302 verify_no_mergehead &&
303 test_cmp squash.1 .git/SQUASH_MSG
306 test_debug
'git log --graph --decorate --oneline --all'
308 test_expect_success
'merge c1 with c2 (squash)' '
309 git reset --hard c1 &&
310 git merge --squash c2 &&
311 verify_merge file result.1-5 &&
313 verify_no_mergehead &&
314 test_cmp squash.1-5 .git/SQUASH_MSG
317 test_debug
'git log --graph --decorate --oneline --all'
319 test_expect_success
'unsuccesful merge of c1 with c2 (squash, ff-only)' '
320 git reset --hard c1 &&
321 test_must_fail git merge --squash --ff-only c2
324 test_debug
'git log --graph --decorate --oneline --all'
326 test_expect_success
'merge c1 with c2 and c3 (squash)' '
327 git reset --hard c1 &&
328 git merge --squash c2 c3 &&
329 verify_merge file result.1-5-9 &&
331 verify_no_mergehead &&
332 test_cmp squash.1-5-9 .git/SQUASH_MSG
335 test_debug
'git log --graph --decorate --oneline --all'
337 test_expect_success
'merge c1 with c2 (no-commit in config)' '
338 git reset --hard c1 &&
339 git config branch.master.mergeoptions "--no-commit" &&
341 verify_merge file result.1-5 &&
343 verify_mergeheads $c2
346 test_debug
'git log --graph --decorate --oneline --all'
348 test_expect_success
'merge c1 with c2 (log in config)' '
349 git config branch.master.mergeoptions "" &&
350 git reset --hard c1 &&
351 git merge --log c2 &&
352 git show -s --pretty=tformat:%s%n%b >expect &&
354 git config branch.master.mergeoptions --log &&
355 git reset --hard c1 &&
357 git show -s --pretty=tformat:%s%n%b >actual &&
359 test_cmp expect actual
362 test_expect_success
'merge c1 with c2 (log in config gets overridden)' '
363 test_when_finished "git config --remove-section branch.master" &&
364 test_when_finished "git config --remove-section merge" &&
365 test_might_fail git config --remove-section branch.master &&
366 test_might_fail git config --remove-section merge &&
368 git reset --hard c1 &&
370 git show -s --pretty=tformat:%s%n%b >expect &&
372 git config branch.master.mergeoptions "--no-log" &&
373 git config merge.log true &&
374 git reset --hard c1 &&
376 git show -s --pretty=tformat:%s%n%b >actual &&
378 test_cmp expect actual
381 test_expect_success
'merge c1 with c2 (squash in config)' '
382 git reset --hard c1 &&
383 git config branch.master.mergeoptions "--squash" &&
385 verify_merge file result.1-5 &&
387 verify_no_mergehead &&
388 test_cmp squash.1-5 .git/SQUASH_MSG
391 test_debug
'git log --graph --decorate --oneline --all'
393 test_expect_success
'override config option -n with --summary' '
394 git reset --hard c1 &&
395 git config branch.master.mergeoptions "-n" &&
397 git merge --summary c2 >diffstat.txt &&
398 verify_merge file result.1-5 msg.1-5 &&
399 verify_parents $c1 $c2 &&
400 if ! grep "^ file | *2 +-$" diffstat.txt
402 echo "[OOPS] diffstat was not generated with --summary"
407 test_expect_success
'override config option -n with --stat' '
408 git reset --hard c1 &&
409 git config branch.master.mergeoptions "-n" &&
411 git merge --stat c2 >diffstat.txt &&
412 verify_merge file result.1-5 msg.1-5 &&
413 verify_parents $c1 $c2 &&
414 if ! grep "^ file | *2 +-$" diffstat.txt
416 echo "[OOPS] diffstat was not generated with --stat"
421 test_debug
'git log --graph --decorate --oneline --all'
423 test_expect_success
'override config option --stat' '
424 git reset --hard c1 &&
425 git config branch.master.mergeoptions "--stat" &&
427 git merge -n c2 >diffstat.txt &&
428 verify_merge file result.1-5 msg.1-5 &&
429 verify_parents $c1 $c2 &&
430 if grep "^ file | *2 +-$" diffstat.txt
432 echo "[OOPS] diffstat was generated"
437 test_debug
'git log --graph --decorate --oneline --all'
439 test_expect_success
'merge c1 with c2 (override --no-commit)' '
440 git reset --hard c1 &&
441 git config branch.master.mergeoptions "--no-commit" &&
443 git merge --commit c2 &&
444 verify_merge file result.1-5 msg.1-5 &&
445 verify_parents $c1 $c2
448 test_debug
'git log --graph --decorate --oneline --all'
450 test_expect_success
'merge c1 with c2 (override --squash)' '
451 git reset --hard c1 &&
452 git config branch.master.mergeoptions "--squash" &&
454 git merge --no-squash c2 &&
455 verify_merge file result.1-5 msg.1-5 &&
456 verify_parents $c1 $c2
459 test_debug
'git log --graph --decorate --oneline --all'
461 test_expect_success
'merge c0 with c1 (no-ff)' '
462 git reset --hard c0 &&
463 git config branch.master.mergeoptions "" &&
465 git merge --no-ff c1 &&
466 verify_merge file result.1 &&
467 verify_parents $c0 $c1
470 test_debug
'git log --graph --decorate --oneline --all'
472 test_expect_success
'merge c0 with c1 (merge.ff=false)' '
473 git reset --hard c0 &&
474 git config merge.ff false &&
477 git config --remove-section merge &&
478 verify_merge file result.1 &&
479 verify_parents $c0 $c1
481 test_debug
'git log --graph --decorate --oneline --all'
483 test_expect_success
'combine branch.master.mergeoptions with merge.ff' '
484 git reset --hard c0 &&
485 git config branch.master.mergeoptions --ff &&
486 git config merge.ff false &&
489 git config --remove-section "branch.master" &&
490 git config --remove-section "merge" &&
491 verify_merge file result.1 &&
495 test_expect_success
'tolerate unknown values for merge.ff' '
496 git reset --hard c0 &&
497 git config merge.ff something-new &&
499 git merge c1 2>message &&
500 git config --remove-section "merge" &&
502 test_cmp empty message
505 test_expect_success
'combining --squash and --no-ff is refused' '
506 git reset --hard c0 &&
507 test_must_fail git merge --squash --no-ff c1 &&
508 test_must_fail git merge --no-ff --squash c1
511 test_expect_success
'combining --ff-only and --no-ff is refused' '
512 test_must_fail git merge --ff-only --no-ff c1 &&
513 test_must_fail git merge --no-ff --ff-only c1
516 test_expect_success
'merge c0 with c1 (ff overrides no-ff)' '
517 git reset --hard c0 &&
518 git config branch.master.mergeoptions "--no-ff" &&
520 verify_merge file result.1 &&
524 test_expect_success
'merge log message' '
525 git reset --hard c0 &&
526 git merge --no-log c2 &&
527 git show -s --pretty=format:%b HEAD >msg.act &&
528 test_cmp msg.nolog 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 git 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 &&
545 git config branch.master.mergeoptions "" &&
547 git merge c0 c2 c0 c1 &&
548 verify_merge file result.1-5 &&
549 verify_parents $c1 $c2
552 test_debug
'git log --graph --decorate --oneline --all'
554 test_expect_success
'merge c1 with c0, c2, c0, and c1' '
555 git reset --hard c1 &&
556 git config branch.master.mergeoptions "" &&
558 git merge c0 c2 c0 c1 &&
559 verify_merge file result.1-5 &&
560 verify_parents $c1 $c2
563 test_debug
'git log --graph --decorate --oneline --all'
565 test_expect_success
'merge c1 with c1 and c2' '
566 git reset --hard c1 &&
567 git config branch.master.mergeoptions "" &&
570 verify_merge file result.1-5 &&
571 verify_parents $c1 $c2
574 test_debug
'git log --graph --decorate --oneline --all'
576 test_expect_success
'merge fast-forward in a dirty tree' '
577 git reset --hard c0 &&
584 test_debug
'git log --graph --decorate --oneline --all'
586 test_expect_success
'in-index merge' '
587 git reset --hard c0 &&
588 git merge --no-ff -s resolve c1 >out &&
589 test_i18ngrep "Wonderful." out &&
590 verify_parents $c0 $c1
593 test_debug
'git log --graph --decorate --oneline --all'
595 test_expect_success
'refresh the index before merging' '
596 git reset --hard c1 &&
597 cp file file.n && mv -f file.n file &&
601 cat >expected.branch
<<\EOF
602 Merge branch
'c5-branch' (early part
)
604 cat >expected.tag
<<\EOF
608 test_expect_success
'merge early part of c2' '
609 git reset --hard c3 &&
618 git reset --hard c3 &&
623 git branch -f c5-branch c5 &&
624 git merge c5-branch~1 &&
625 git show -s --pretty=format:%s HEAD >actual.branch &&
626 git reset --keep HEAD^ &&
628 git show -s --pretty=format:%s HEAD >actual.tag &&
629 test_cmp expected.branch actual.branch &&
630 test_cmp expected.tag actual.tag
633 test_debug
'git log --graph --decorate --oneline --all'
635 test_expect_success
'merge --no-ff --no-commit && commit' '
636 git reset --hard c0 &&
637 git merge --no-ff --no-commit c1 &&
638 EDITOR=: git commit &&
639 verify_parents $c0 $c1
642 test_debug
'git log --graph --decorate --oneline --all'
644 test_expect_success
'amending no-ff merge commit' '
645 EDITOR=: git commit --amend &&
646 verify_parents $c0 $c1
649 test_debug
'git log --graph --decorate --oneline --all'
653 # Add a new message string that was not in the template
655 echo "Merge work done on the side branch c1"
658 ) >"$1.tmp" && mv "$1.tmp" "$1"
659 # strip comments and blank lines from end of message
660 sed -e '/^#/d' < "$1" |
sed -e :a
-e '/^\n*$/{$d;N;ba' -e '}' > expected
664 test_expect_success
'merge --no-ff --edit' '
665 git reset --hard c0 &&
666 EDITOR=./editor git merge --no-ff --edit c1 &&
667 verify_parents $c0 $c1 &&
668 git cat-file commit HEAD >raw &&
669 grep "work done on the side branch" raw &&
670 sed "1,/^$/d" >actual raw &&
671 test_cmp actual expected
674 test_expect_success GPG
'merge --ff-only tag' '
675 git reset --hard c0 &&
676 git commit --allow-empty -m "A newer commit" &&
677 git tag -s -m "A newer commit" signed &&
678 git reset --hard c0 &&
680 git merge --ff-only signed &&
681 git rev-parse signed^0 >expect &&
682 git rev-parse HEAD >actual &&
683 test_cmp actual expect
686 test_expect_success GPG
'merge --no-edit tag should skip editor' '
687 git reset --hard c0 &&
688 git commit --allow-empty -m "A newer commit" &&
689 git tag -f -s -m "A newer commit" signed &&
690 git reset --hard c0 &&
692 EDITOR=false git merge --no-edit signed &&
693 git rev-parse signed^0 >expect &&
694 git rev-parse HEAD^2 >actual &&
695 test_cmp actual expect