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
60 echo >msg.nolognoff
&&
70 git update-index
--refresh &&
71 git
diff --exit-code &&
74 git show
-s --pretty=format
:%s HEAD
>msg.act
&&
80 echo "$1" >head.expected
&&
81 git rev-parse HEAD
>head.actual
&&
82 test_cmp
head.expected
head.actual
86 printf '%s\n' "$@" >parents.expected
&&
91 git rev-parse HEAD^
$i >>parents.actual
&&
95 test_must_fail git rev-parse
--verify "HEAD^$i" &&
96 test_cmp parents.expected parents.actual
99 verify_mergeheads
() {
100 printf '%s\n' "$@" >mergehead.expected
&&
104 done <.git
/MERGE_HEAD
>mergehead.actual
&&
105 test_cmp mergehead.expected mergehead.actual
108 verify_no_mergehead
() {
109 ! test -e .git
/MERGE_HEAD
112 test_expect_success
'setup' '
115 git commit -m "commit 0" &&
117 c0=$(git rev-parse HEAD) &&
121 git commit -m "commit 1" &&
123 c1=$(git rev-parse HEAD) &&
124 git reset --hard "$c0" &&
128 git commit -m "commit 2" &&
130 c2=$(git rev-parse HEAD) &&
131 git reset --hard "$c0" &&
135 git commit -m "commit 3" &&
137 c3=$(git rev-parse HEAD)
138 git reset --hard "$c0" &&
142 test_debug
'git log --graph --decorate --oneline --all'
144 test_expect_success
'test option parsing' '
145 test_must_fail git merge -$ c1 &&
146 test_must_fail git merge --no-such c1 &&
147 test_must_fail git merge -s foobar c1 &&
148 test_must_fail git merge -s=foobar c1 &&
149 test_must_fail git merge -m &&
150 test_must_fail git merge
153 test_expect_success
'merge -h with invalid index' '
159 test_expect_code 129 git merge -h 2>usage
161 test_i18ngrep "[Uu]sage: git merge" broken/usage
164 test_expect_success
'reject non-strategy with a git-merge-foo name' '
165 test_must_fail git merge -s index c1
168 test_expect_success
'merge c0 with c1' '
169 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
171 git reset --hard c0 &&
173 verify_merge file result.1 &&
176 git reflog -1 >reflog.actual &&
177 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
178 test_cmp reflog.expected reflog.fuzzy
181 test_debug
'git log --graph --decorate --oneline --all'
183 test_expect_success
'merge c0 with c1 with --ff-only' '
184 git reset --hard c0 &&
185 git merge --ff-only c1 &&
186 git merge --ff-only HEAD c0 c1 &&
187 verify_merge file result.1 &&
191 test_debug
'git log --graph --decorate --oneline --all'
193 test_expect_success
'merge from unborn branch' '
194 git checkout -f master &&
195 test_might_fail git branch -D kid &&
197 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
199 git checkout --orphan kid &&
200 test_when_finished "git checkout -f master" &&
203 git merge --ff-only c1 &&
204 verify_merge file result.1 &&
207 git reflog -1 >reflog.actual &&
208 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
209 test_cmp reflog.expected reflog.fuzzy
212 test_debug
'git log --graph --decorate --oneline --all'
214 test_expect_success
'merge c1 with c2' '
215 git reset --hard c1 &&
218 verify_merge file result.1-5 msg.1-5 &&
219 verify_parents $c1 $c2
222 test_debug
'git log --graph --decorate --oneline --all'
224 test_expect_success
'merge c1 with c2 and c3' '
225 git reset --hard c1 &&
228 verify_merge file result.1-5-9 msg.1-5-9 &&
229 verify_parents $c1 $c2 $c3
232 test_debug
'git log --graph --decorate --oneline --all'
234 test_expect_success
'merges with --ff-only' '
235 git reset --hard c1 &&
237 test_must_fail git merge --ff-only c2 &&
238 test_must_fail git merge --ff-only c3 &&
239 test_must_fail git merge --ff-only c2 c3 &&
240 git reset --hard c0 &&
245 test_expect_success
'merges with merge.ff=only' '
246 git reset --hard c1 &&
248 test_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
'unsuccessful 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 test_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 reset --hard c1 &&
350 git merge --log c2 &&
351 git show -s --pretty=tformat:%s%n%b >expect &&
353 test_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 git reset --hard c1 &&
364 git show -s --pretty=tformat:%s%n%b >expect &&
366 test_config branch.master.mergeoptions "--no-log" &&
367 test_config merge.log "true" &&
368 git reset --hard c1 &&
370 git show -s --pretty=tformat:%s%n%b >actual &&
372 test_cmp expect actual
375 test_expect_success
'merge c1 with c2 (squash in config)' '
376 git reset --hard c1 &&
377 test_config branch.master.mergeoptions "--squash" &&
379 verify_merge file result.1-5 &&
381 verify_no_mergehead &&
382 test_cmp squash.1-5 .git/SQUASH_MSG
385 test_debug
'git log --graph --decorate --oneline --all'
387 test_expect_success
'override config option -n with --summary' '
388 git reset --hard c1 &&
389 test_config branch.master.mergeoptions "-n" &&
391 git merge --summary c2 >diffstat.txt &&
392 verify_merge file result.1-5 msg.1-5 &&
393 verify_parents $c1 $c2 &&
394 if ! grep "^ file | *2 +-$" diffstat.txt
396 echo "[OOPS] diffstat was not generated with --summary"
401 test_expect_success
'override config option -n with --stat' '
402 git reset --hard c1 &&
403 test_config branch.master.mergeoptions "-n" &&
405 git merge --stat c2 >diffstat.txt &&
406 verify_merge file result.1-5 msg.1-5 &&
407 verify_parents $c1 $c2 &&
408 if ! grep "^ file | *2 +-$" diffstat.txt
410 echo "[OOPS] diffstat was not generated with --stat"
415 test_debug
'git log --graph --decorate --oneline --all'
417 test_expect_success
'override config option --stat' '
418 git reset --hard c1 &&
419 test_config branch.master.mergeoptions "--stat" &&
421 git merge -n c2 >diffstat.txt &&
422 verify_merge file result.1-5 msg.1-5 &&
423 verify_parents $c1 $c2 &&
424 if grep "^ file | *2 +-$" diffstat.txt
426 echo "[OOPS] diffstat was generated"
431 test_debug
'git log --graph --decorate --oneline --all'
433 test_expect_success
'merge c1 with c2 (override --no-commit)' '
434 git reset --hard c1 &&
435 test_config branch.master.mergeoptions "--no-commit" &&
437 git merge --commit c2 &&
438 verify_merge file result.1-5 msg.1-5 &&
439 verify_parents $c1 $c2
442 test_debug
'git log --graph --decorate --oneline --all'
444 test_expect_success
'merge c1 with c2 (override --squash)' '
445 git reset --hard c1 &&
446 test_config branch.master.mergeoptions "--squash" &&
448 git merge --no-squash c2 &&
449 verify_merge file result.1-5 msg.1-5 &&
450 verify_parents $c1 $c2
453 test_debug
'git log --graph --decorate --oneline --all'
455 test_expect_success
'merge c0 with c1 (no-ff)' '
456 git reset --hard c0 &&
458 git merge --no-ff c1 &&
459 verify_merge file result.1 &&
460 verify_parents $c0 $c1
463 test_debug
'git log --graph --decorate --oneline --all'
465 test_expect_success
'merge c0 with c1 (merge.ff=false)' '
466 git reset --hard c0 &&
467 test_config merge.ff "false" &&
470 verify_merge file result.1 &&
471 verify_parents $c0 $c1
473 test_debug
'git log --graph --decorate --oneline --all'
475 test_expect_success
'combine branch.master.mergeoptions with merge.ff' '
476 git reset --hard c0 &&
477 test_config branch.master.mergeoptions "--ff" &&
478 test_config merge.ff "false" &&
481 verify_merge file result.1 &&
485 test_expect_success
'tolerate unknown values for merge.ff' '
486 git reset --hard c0 &&
487 test_config merge.ff "something-new" &&
489 git merge c1 2>message &&
491 test_cmp empty message
494 test_expect_success
'combining --squash and --no-ff is refused' '
495 git reset --hard c0 &&
496 test_must_fail git merge --squash --no-ff c1 &&
497 test_must_fail git merge --no-ff --squash c1
500 test_expect_success
'combining --ff-only and --no-ff is refused' '
501 test_must_fail git merge --ff-only --no-ff c1 &&
502 test_must_fail git merge --no-ff --ff-only c1
505 test_expect_success
'merge c0 with c1 (ff overrides no-ff)' '
506 git reset --hard c0 &&
507 test_config branch.master.mergeoptions "--no-ff" &&
509 verify_merge file result.1 &&
513 test_expect_success
'merge log message' '
514 git reset --hard c0 &&
515 git merge --no-log c2 &&
516 git show -s --pretty=format:%b HEAD >msg.act &&
517 test_cmp msg.nologff msg.act &&
519 git reset --hard c0 &&
520 test_config branch.master.mergeoptions "--no-ff" &&
521 git merge --no-log c2 &&
522 git show -s --pretty=format:%b HEAD >msg.act &&
523 test_cmp msg.nolognoff 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 test_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 &&
541 git merge c0 c2 c0 c1 &&
542 verify_merge file result.1-5 &&
543 verify_parents $c1 $c2
546 test_debug
'git log --graph --decorate --oneline --all'
548 test_expect_success
'merge c1 with c0, c2, c0, and c1' '
549 git reset --hard c1 &&
551 git merge c0 c2 c0 c1 &&
552 verify_merge file result.1-5 &&
553 verify_parents $c1 $c2
556 test_debug
'git log --graph --decorate --oneline --all'
558 test_expect_success
'merge c1 with c1 and c2' '
559 git reset --hard c1 &&
562 verify_merge file result.1-5 &&
563 verify_parents $c1 $c2
566 test_debug
'git log --graph --decorate --oneline --all'
568 test_expect_success
'merge fast-forward in a dirty tree' '
569 git reset --hard c0 &&
576 test_debug
'git log --graph --decorate --oneline --all'
578 test_expect_success
'in-index merge' '
579 git reset --hard c0 &&
580 git merge --no-ff -s resolve c1 >out &&
581 test_i18ngrep "Wonderful." out &&
582 verify_parents $c0 $c1
585 test_debug
'git log --graph --decorate --oneline --all'
587 test_expect_success
'refresh the index before merging' '
588 git reset --hard c1 &&
589 cp file file.n && mv -f file.n file &&
593 cat >expected.branch
<<\EOF
594 Merge branch
'c5-branch' (early part
)
596 cat >expected.tag
<<\EOF
600 test_expect_success
'merge early part of c2' '
601 git reset --hard c3 &&
610 git reset --hard c3 &&
615 git branch -f c5-branch c5 &&
616 git merge c5-branch~1 &&
617 git show -s --pretty=format:%s HEAD >actual.branch &&
618 git reset --keep HEAD^ &&
620 git show -s --pretty=format:%s HEAD >actual.tag &&
621 test_cmp expected.branch actual.branch &&
622 test_cmp expected.tag actual.tag
625 test_debug
'git log --graph --decorate --oneline --all'
627 test_expect_success
'merge --no-ff --no-commit && commit' '
628 git reset --hard c0 &&
629 git merge --no-ff --no-commit c1 &&
630 EDITOR=: git commit &&
631 verify_parents $c0 $c1
634 test_debug
'git log --graph --decorate --oneline --all'
636 test_expect_success
'amending no-ff merge commit' '
637 EDITOR=: git commit --amend &&
638 verify_parents $c0 $c1
641 test_debug
'git log --graph --decorate --oneline --all'
645 # Add a new message string that was not in the template
647 echo "Merge work done on the side branch c1"
650 ) >"$1.tmp" && mv "$1.tmp" "$1"
651 # strip comments and blank lines from end of message
652 sed -e '/^#/d' < "$1" |
sed -e :a
-e '/^\n*$/{$d;N;ba' -e '}' > expected
656 test_expect_success
'merge --no-ff --edit' '
657 git reset --hard c0 &&
658 EDITOR=./editor git merge --no-ff --edit c1 &&
659 verify_parents $c0 $c1 &&
660 git cat-file commit HEAD >raw &&
661 grep "work done on the side branch" raw &&
662 sed "1,/^$/d" >actual raw &&
663 test_cmp actual expected
666 test_expect_success GPG
'merge --ff-only tag' '
667 git reset --hard c0 &&
668 git commit --allow-empty -m "A newer commit" &&
669 git tag -s -m "A newer commit" signed &&
670 git reset --hard c0 &&
672 git merge --ff-only signed &&
673 git rev-parse signed^0 >expect &&
674 git rev-parse HEAD >actual &&
675 test_cmp actual expect
678 test_expect_success GPG
'merge --no-edit tag should skip editor' '
679 git reset --hard c0 &&
680 git commit --allow-empty -m "A newer commit" &&
681 git tag -f -s -m "A newer commit" signed &&
682 git reset --hard c0 &&
684 EDITOR=false git merge --no-edit signed &&
685 git rev-parse signed^0 >expect &&
686 git rev-parse HEAD^2 >actual &&
687 test_cmp actual expect