Code

Merge branch 'da/maint-mergetool-twoway' into maint
[git.git] / t / t7600-merge.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Lars Hjemli
4 #
6 test_description='git merge
8 Testing basic merge operations/option parsing.
10 ! [c0] commit 0
11  ! [c1] commit 1
12   ! [c2] commit 2
13    ! [c3] commit 3
14     ! [c4] c4
15      ! [c5] c5
16       ! [c6] c6
17        * [master] Merge commit 'c1'
18 --------
19        - [master] Merge commit 'c1'
20  +     * [c1] commit 1
21       +  [c6] c6
22      +   [c5] c5
23     ++   [c4] c4
24    ++++  [c3] commit 3
25   +      [c2] commit 2
26 +++++++* [c0] commit 0
27 '
29 . ./test-lib.sh
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
38 >empty
40 create_merge_msgs () {
41         echo "Merge tag 'c2'" >msg.1-5 &&
42         echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
43         {
44                 echo "Squashed commit of the following:" &&
45                 echo &&
46                 git log --no-merges ^HEAD c1
47         } >squash.1 &&
48         {
49                 echo "Squashed commit of the following:" &&
50                 echo &&
51                 git log --no-merges ^HEAD c2
52         } >squash.1-5 &&
53         {
54                 echo "Squashed commit of the following:" &&
55                 echo &&
56                 git log --no-merges ^HEAD c2 c3
57         } >squash.1-5-9 &&
58         echo >msg.nolog &&
59         {
60                 echo "* tag 'c3':" &&
61                 echo "  commit 3" &&
62                 echo
63         } >msg.log
64 }
66 verify_merge () {
67         test_cmp "$2" "$1" &&
68         git update-index --refresh &&
69         git diff --exit-code &&
70         if test -n "$3"
71         then
72                 git show -s --pretty=format:%s HEAD >msg.act &&
73                 test_cmp "$3" msg.act
74         fi
75 }
77 verify_head () {
78         echo "$1" >head.expected &&
79         git rev-parse HEAD >head.actual &&
80         test_cmp head.expected head.actual
81 }
83 verify_parents () {
84         printf '%s\n' "$@" >parents.expected &&
85         >parents.actual &&
86         i=1 &&
87         while test $i -le $#
88         do
89                 git rev-parse HEAD^$i >>parents.actual &&
90                 i=$(expr $i + 1) ||
91                 return 1
92         done &&
93         test_must_fail git rev-parse --verify "HEAD^$i" &&
94         test_cmp parents.expected parents.actual
95 }
97 verify_mergeheads () {
98         printf '%s\n' "$@" >mergehead.expected &&
99         while read sha1 rest
100         do
101                 git rev-parse $sha1
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' '
111         git add file &&
112         test_tick &&
113         git commit -m "commit 0" &&
114         git tag c0 &&
115         c0=$(git rev-parse HEAD) &&
116         cp file.1 file &&
117         git add file &&
118         test_tick &&
119         git commit -m "commit 1" &&
120         git tag c1 &&
121         c1=$(git rev-parse HEAD) &&
122         git reset --hard "$c0" &&
123         cp file.5 file &&
124         git add file &&
125         test_tick &&
126         git commit -m "commit 2" &&
127         git tag c2 &&
128         c2=$(git rev-parse HEAD) &&
129         git reset --hard "$c0" &&
130         cp file.9 file &&
131         git add file &&
132         test_tick &&
133         git commit -m "commit 3" &&
134         git tag c3 &&
135         c3=$(git rev-parse HEAD)
136         git reset --hard "$c0" &&
137         create_merge_msgs
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' '
152         mkdir broken &&
153         (
154                 cd broken &&
155                 git init &&
156                 >.git/index &&
157                 test_expect_code 129 git merge -h 2>usage
158         ) &&
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 &&
170         git merge c1 &&
171         verify_merge file result.1 &&
172         verify_head "$c1" &&
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 &&
186         verify_head "$c1"
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" &&
199         git rm -fr . &&
200         test_tick &&
201         git merge --ff-only c1 &&
202         verify_merge file result.1 &&
203         verify_head "$c1" &&
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 &&
214         test_tick &&
215         git merge c2 &&
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 &&
224         test_tick &&
225         git merge c2 c3 &&
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 &&
234         test_tick &&
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 &&
239         git merge c3 &&
240         verify_head $c3
243 test_expect_success 'merges with merge.ff=only' '
244         git reset --hard c1 &&
245         test_tick &&
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 &&
252         git merge c3 &&
253         verify_head $c3
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 &&
260         verify_head $c1
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 &&
269         verify_head $c1 &&
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 &&
279         verify_head $c1 &&
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 &&
289         verify_head $c0 &&
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 &&
300         verify_head $c0 &&
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 &&
311         verify_head $c1 &&
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 &&
329         verify_head $c1 &&
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" &&
339         git merge c2 &&
340         verify_merge file result.1-5 &&
341         verify_head $c1 &&
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 &&
355         git merge c2 &&
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 &&
368         git merge c2 &&
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 &&
374         git merge c2 &&
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" &&
383         git merge c2 &&
384         verify_merge file result.1-5 &&
385         verify_head $c1 &&
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" &&
395         test_tick &&
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
400         then
401                 echo "[OOPS] diffstat was not generated with --summary"
402                 false
403         fi
406 test_expect_success 'override config option -n with --stat' '
407         git reset --hard c1 &&
408         git config branch.master.mergeoptions "-n" &&
409         test_tick &&
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
414         then
415                 echo "[OOPS] diffstat was not generated with --stat"
416                 false
417         fi
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" &&
425         test_tick &&
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
430         then
431                 echo "[OOPS] diffstat was generated"
432                 false
433         fi
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" &&
441         test_tick &&
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" &&
452         test_tick &&
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 "" &&
463         test_tick &&
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 &&
474         test_tick &&
475         git merge c1 &&
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 &&
486         test_tick &&
487         git merge c1 &&
488         git config --remove-section "branch.master" &&
489         git config --remove-section "merge" &&
490         verify_merge file result.1 &&
491         verify_parents "$c0"
494 test_expect_success 'tolerate unknown values for merge.ff' '
495         git reset --hard c0 &&
496         git config merge.ff something-new &&
497         test_tick &&
498         git merge c1 2>message &&
499         git config --remove-section "merge" &&
500         verify_head "$c1" &&
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" &&
518         git merge --ff c1 &&
519         verify_merge file result.1 &&
520         verify_head $c1
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 &&
535         git merge c3 &&
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 "" &&
545        test_tick &&
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 "" &&
556        test_tick &&
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 "" &&
567        test_tick &&
568        git merge c1 c2 &&
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 &&
577        mv file file1 &&
578        cat file1 >file &&
579        rm -f file1 &&
580        git merge c2
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 &&
597         git merge c3
600 cat >expected.branch <<\EOF
601 Merge branch 'c5-branch' (early part)
602 EOF
603 cat >expected.tag <<\EOF
604 Merge commit 'c5~1'
605 EOF
607 test_expect_success 'merge early part of c2' '
608         git reset --hard c3 &&
609         echo c4 >c4.c &&
610         git add c4.c &&
611         git commit -m c4 &&
612         git tag c4 &&
613         echo c5 >c5.c &&
614         git add c5.c &&
615         git commit -m c5 &&
616         git tag c5 &&
617         git reset --hard c3 &&
618         echo c6 >c6.c &&
619         git add c6.c &&
620         git commit -m c6 &&
621         git tag c6 &&
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^ &&
626         git merge c5~1 &&
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'
650 cat >editor <<\EOF
651 #!/bin/sh
652 # Add a new message string that was not in the template
654         echo "Merge work done on the side branch c1"
655         echo
656         cat <"$1"
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
660 EOF
661 chmod 755 editor
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
673 test_done