tests: check git does not barf on merge.ff values for future versions of git
[git] / t / t7600-merge.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Lars Hjemli
4 #
5
6 test_description='git merge
7
8 Testing basic merge operations/option parsing.
9
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 '
28
29 . ./test-lib.sh
30
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 &&
39         >empty &&
40
41         create_merge_msgs() {
42                 echo "Merge commit '\''c2'\''" >msg.1-5 &&
43                 echo "Merge commit '\''c2'\''; commit '\''c3'\''" >msg.1-5-9 &&
44                 {
45                         echo "Squashed commit of the following:" &&
46                         echo &&
47                         git log --no-merges ^HEAD c1
48                 } >squash.1 &&
49                 {
50                         echo "Squashed commit of the following:" &&
51                         echo &&
52                         git log --no-merges ^HEAD c2
53                 } >squash.1-5 &&
54                 {
55                         echo "Squashed commit of the following:" &&
56                         echo &&
57                         git log --no-merges ^HEAD c2 c3
58                 } >squash.1-5-9 &&
59                 echo >msg.nolog &&
60                 {
61                         echo "* commit '\''c3'\'':" &&
62                         echo "  commit 3" &&
63                         echo
64                 } >msg.log
65         } &&
66
67         verify_merge() {
68                 test_cmp "$2" "$1" &&
69                 git update-index --refresh &&
70                 git diff --exit-code &&
71                 if test -n "$3"
72                 then
73                         git show -s --pretty=format:%s HEAD >msg.act &&
74                         test_cmp "$3" msg.act
75                 fi
76         } &&
77
78         verify_head() {
79                 echo "$1" >head.expected &&
80                 git rev-parse HEAD >head.actual &&
81                 test_cmp head.expected head.actual
82         } &&
83
84         verify_parents() {
85                 printf "%s\n" "$@" >parents.expected &&
86                 >parents.actual &&
87                 i=1 &&
88                 while test $i -le $#
89                 do
90                         git rev-parse HEAD^$i >>parents.actual &&
91                         i=$(expr $i + 1) ||
92                         return 1
93                 done &&
94                 test_cmp parents.expected parents.actual
95         } &&
96
97         verify_mergeheads() {
98                 printf "%s\n" "$@" >mergehead.expected &&
99                 test_cmp mergehead.expected .git/MERGE_HEAD
100         } &&
101
102         verify_no_mergehead() {
103                 ! test -e .git/MERGE_HEAD
104         }
105 '
106
107 test_expect_success 'setup' '
108         git add file &&
109         test_tick &&
110         git commit -m "commit 0" &&
111         git tag c0 &&
112         c0=$(git rev-parse HEAD) &&
113         cp file.1 file &&
114         git add file &&
115         test_tick &&
116         git commit -m "commit 1" &&
117         git tag c1 &&
118         c1=$(git rev-parse HEAD) &&
119         git reset --hard "$c0" &&
120         cp file.5 file &&
121         git add file &&
122         test_tick &&
123         git commit -m "commit 2" &&
124         git tag c2 &&
125         c2=$(git rev-parse HEAD) &&
126         git reset --hard "$c0" &&
127         cp file.9 file &&
128         git add file &&
129         test_tick &&
130         git commit -m "commit 3" &&
131         git tag c3 &&
132         c3=$(git rev-parse HEAD)
133         git reset --hard "$c0" &&
134         create_merge_msgs
135 '
136
137 test_debug 'git log --graph --decorate --oneline --all'
138
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
146 '
147
148 test_expect_success 'merge -h with invalid index' '
149         mkdir broken &&
150         (
151                 cd broken &&
152                 git init &&
153                 >.git/index &&
154                 test_expect_code 129 git merge -h 2>usage
155         ) &&
156         grep "[Uu]sage: git merge" broken/usage
157 '
158
159 test_expect_success 'reject non-strategy with a git-merge-foo name' '
160         test_must_fail git merge -s index c1
161 '
162
163 test_expect_success 'merge c0 with c1' '
164         echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
165
166         git reset --hard c0 &&
167         git merge c1 &&
168         verify_merge file result.1 &&
169         verify_head "$c1" &&
170
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
174 '
175
176 test_debug 'git log --graph --decorate --oneline --all'
177
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 &&
183         verify_head "$c1"
184 '
185
186 test_debug 'git log --graph --decorate --oneline --all'
187
188 test_expect_success 'merge from unborn branch' '
189         git checkout -f master &&
190         test_might_fail git branch -D kid &&
191
192         echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
193
194         git checkout --orphan kid &&
195         test_when_finished "git checkout -f master" &&
196         git rm -fr . &&
197         test_tick &&
198         git merge --ff-only c1 &&
199         verify_merge file result.1 &&
200         verify_head "$c1" &&
201
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
205 '
206
207 test_debug 'git log --graph --decorate --oneline --all'
208
209 test_expect_success 'merge c1 with c2' '
210         git reset --hard c1 &&
211         test_tick &&
212         git merge c2 &&
213         verify_merge file result.1-5 msg.1-5 &&
214         verify_parents $c1 $c2
215 '
216
217 test_debug 'git log --graph --decorate --oneline --all'
218
219 test_expect_success 'merge c1 with c2 and c3' '
220         git reset --hard c1 &&
221         test_tick &&
222         git merge c2 c3 &&
223         verify_merge file result.1-5-9 msg.1-5-9 &&
224         verify_parents $c1 $c2 $c3
225 '
226
227 test_debug 'git log --graph --decorate --oneline --all'
228
229 test_expect_success 'merges with --ff-only' '
230         git reset --hard c1 &&
231         test_tick &&
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 &&
236         git merge c3 &&
237         verify_head $c3
238 '
239
240 test_expect_success 'merges with merge.ff=only' '
241         git reset --hard c1 &&
242         test_tick &&
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 &&
249         git merge c3 &&
250         verify_head $c3
251 '
252
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 &&
257         verify_head $c1
258 '
259
260 test_debug 'git log --graph --decorate --oneline --all'
261
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 &&
266         verify_head $c1 &&
267         verify_mergeheads $c2
268 '
269
270 test_debug 'git log --graph --decorate --oneline --all'
271
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 &&
276         verify_head $c1 &&
277         verify_mergeheads $c2 $c3
278 '
279
280 test_debug 'git log --graph --decorate --oneline --all'
281
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 &&
286         verify_head $c0 &&
287         verify_no_mergehead &&
288         test_cmp squash.1 .git/SQUASH_MSG
289 '
290
291 test_debug 'git log --graph --decorate --oneline --all'
292
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 &&
297         verify_head $c0 &&
298         verify_no_mergehead &&
299         test_cmp squash.1 .git/SQUASH_MSG
300 '
301
302 test_debug 'git log --graph --decorate --oneline --all'
303
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 &&
308         verify_head $c1 &&
309         verify_no_mergehead &&
310         test_cmp squash.1-5 .git/SQUASH_MSG
311 '
312
313 test_debug 'git log --graph --decorate --oneline --all'
314
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
318 '
319
320 test_debug 'git log --graph --decorate --oneline --all'
321
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 &&
326         verify_head $c1 &&
327         verify_no_mergehead &&
328         test_cmp squash.1-5-9 .git/SQUASH_MSG
329 '
330
331 test_debug 'git log --graph --decorate --oneline --all'
332
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" &&
336         git merge c2 &&
337         verify_merge file result.1-5 &&
338         verify_head $c1 &&
339         verify_mergeheads $c2
340 '
341
342 test_debug 'git log --graph --decorate --oneline --all'
343
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 &&
349
350         git config branch.master.mergeoptions --log &&
351         git reset --hard c1 &&
352         git merge c2 &&
353         git show -s --pretty=tformat:%s%n%b >actual &&
354
355         test_cmp expect actual
356 '
357
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 &&
363
364         git reset --hard c1 &&
365         git merge c2 &&
366         git show -s --pretty=tformat:%s%n%b >expect &&
367
368         git config branch.master.mergeoptions "--no-log" &&
369         git config merge.log true &&
370         git reset --hard c1 &&
371         git merge c2 &&
372         git show -s --pretty=tformat:%s%n%b >actual &&
373
374         test_cmp expect actual
375 '
376
377 test_expect_success 'merge c1 with c2 (squash in config)' '
378         git reset --hard c1 &&
379         git config branch.master.mergeoptions "--squash" &&
380         git merge c2 &&
381         verify_merge file result.1-5 &&
382         verify_head $c1 &&
383         verify_no_mergehead &&
384         test_cmp squash.1-5 .git/SQUASH_MSG
385 '
386
387 test_debug 'git log --graph --decorate --oneline --all'
388
389 test_expect_success 'override config option -n with --summary' '
390         git reset --hard c1 &&
391         git config branch.master.mergeoptions "-n" &&
392         test_tick &&
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
397         then
398                 echo "[OOPS] diffstat was not generated with --summary"
399                 false
400         fi
401 '
402
403 test_expect_success 'override config option -n with --stat' '
404         git reset --hard c1 &&
405         git config branch.master.mergeoptions "-n" &&
406         test_tick &&
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
411         then
412                 echo "[OOPS] diffstat was not generated with --stat"
413                 false
414         fi
415 '
416
417 test_debug 'git log --graph --decorate --oneline --all'
418
419 test_expect_success 'override config option --stat' '
420         git reset --hard c1 &&
421         git config branch.master.mergeoptions "--stat" &&
422         test_tick &&
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
427         then
428                 echo "[OOPS] diffstat was generated"
429                 false
430         fi
431 '
432
433 test_debug 'git log --graph --decorate --oneline --all'
434
435 test_expect_success 'merge c1 with c2 (override --no-commit)' '
436         git reset --hard c1 &&
437         git config branch.master.mergeoptions "--no-commit" &&
438         test_tick &&
439         git merge --commit c2 &&
440         verify_merge file result.1-5 msg.1-5 &&
441         verify_parents $c1 $c2
442 '
443
444 test_debug 'git log --graph --decorate --oneline --all'
445
446 test_expect_success 'merge c1 with c2 (override --squash)' '
447         git reset --hard c1 &&
448         git config branch.master.mergeoptions "--squash" &&
449         test_tick &&
450         git merge --no-squash c2 &&
451         verify_merge file result.1-5 msg.1-5 &&
452         verify_parents $c1 $c2
453 '
454
455 test_debug 'git log --graph --decorate --oneline --all'
456
457 test_expect_success 'merge c0 with c1 (no-ff)' '
458         git reset --hard c0 &&
459         git config branch.master.mergeoptions "" &&
460         test_tick &&
461         git merge --no-ff c1 &&
462         verify_merge file result.1 &&
463         verify_parents $c0 $c1
464 '
465
466 test_debug 'git log --graph --decorate --oneline --all'
467
468 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
469         git reset --hard c0 &&
470         git config merge.ff false &&
471         test_tick &&
472         git merge c1 &&
473         git config --remove-section merge &&
474         verify_merge file result.1 &&
475         verify_parents $c0 $c1
476 '
477 test_debug 'git log --graph --decorate --oneline --all'
478
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 &&
483         test_tick &&
484         git merge c1 &&
485         git config --remove-section "branch.master" &&
486         git config --remove-section "merge" &&
487         verify_merge file result.1 &&
488         verify_parents "$c0"
489 '
490
491 test_expect_success 'tolerate unknown values for merge.ff' '
492         git reset --hard c0 &&
493         git config merge.ff something-new &&
494         test_tick &&
495         git merge c1 2>message &&
496         git config --remove-section "merge" &&
497         verify_head "$c1" &&
498         test_cmp empty message
499 '
500
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
505 '
506
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
510 '
511
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" &&
515         git merge --ff c1 &&
516         verify_merge file result.1 &&
517         verify_head $c1
518 '
519
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 &&
525
526         git merge --log c3 &&
527         git show -s --pretty=format:%b HEAD >msg.act &&
528         test_cmp msg.log msg.act &&
529
530         git reset --hard HEAD^ &&
531         git config merge.log yes &&
532         git merge c3 &&
533         git show -s --pretty=format:%b HEAD >msg.act &&
534         test_cmp msg.log msg.act
535 '
536
537 test_debug 'git log --graph --decorate --oneline --all'
538
539 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
540        git reset --hard c1 &&
541        git config branch.master.mergeoptions "" &&
542        test_tick &&
543        git merge c0 c2 c0 c1 &&
544        verify_merge file result.1-5 &&
545        verify_parents $c1 $c2
546 '
547
548 test_debug 'git log --graph --decorate --oneline --all'
549
550 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
551        git reset --hard c1 &&
552        git config branch.master.mergeoptions "" &&
553        test_tick &&
554        git merge c0 c2 c0 c1 &&
555        verify_merge file result.1-5 &&
556        verify_parents $c1 $c2
557 '
558
559 test_debug 'git log --graph --decorate --oneline --all'
560
561 test_expect_success 'merge c1 with c1 and c2' '
562        git reset --hard c1 &&
563        git config branch.master.mergeoptions "" &&
564        test_tick &&
565        git merge c1 c2 &&
566        verify_merge file result.1-5 &&
567        verify_parents $c1 $c2
568 '
569
570 test_debug 'git log --graph --decorate --oneline --all'
571
572 test_expect_success 'merge fast-forward in a dirty tree' '
573        git reset --hard c0 &&
574        mv file file1 &&
575        cat file1 >file &&
576        rm -f file1 &&
577        git merge c2
578 '
579
580 test_debug 'git log --graph --decorate --oneline --all'
581
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
587 '
588
589 test_debug 'git log --graph --decorate --oneline --all'
590
591 test_expect_success 'refresh the index before merging' '
592         git reset --hard c1 &&
593         cp file file.n && mv -f file.n file &&
594         git merge c3
595 '
596
597 cat >expected.branch <<\EOF
598 Merge branch 'c5-branch' (early part)
599 EOF
600 cat >expected.tag <<\EOF
601 Merge commit 'c5~1'
602 EOF
603
604 test_expect_success 'merge early part of c2' '
605         git reset --hard c3 &&
606         echo c4 >c4.c &&
607         git add c4.c &&
608         git commit -m c4 &&
609         git tag c4 &&
610         echo c5 >c5.c &&
611         git add c5.c &&
612         git commit -m c5 &&
613         git tag c5 &&
614         git reset --hard c3 &&
615         echo c6 >c6.c &&
616         git add c6.c &&
617         git commit -m c6 &&
618         git tag c6 &&
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^ &&
623         git merge c5~1 &&
624         git show -s --pretty=format:%s HEAD >actual.tag &&
625         test_cmp expected.branch actual.branch &&
626         test_cmp expected.tag actual.tag
627 '
628
629 test_debug 'git log --graph --decorate --oneline --all'
630
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
636 '
637
638 test_debug 'git log --graph --decorate --oneline --all'
639
640 test_expect_success 'amending no-ff merge commit' '
641         EDITOR=: git commit --amend &&
642         verify_parents $c0 $c1
643 '
644
645 test_debug 'git log --graph --decorate --oneline --all'
646
647 test_done