a07caa8eaec1cd148f68d7190880fbe537a14162
[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 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
39 create_merge_msgs () {
40         echo "Merge commit 'c2'" >msg.1-5 &&
41         echo "Merge commit 'c2'; commit 'c3'" >msg.1-5-9 &&
42         {
43                 echo "Squashed commit of the following:" &&
44                 echo &&
45                 git log --no-merges ^HEAD c1
46         } >squash.1 &&
47         {
48                 echo "Squashed commit of the following:" &&
49                 echo &&
50                 git log --no-merges ^HEAD c2
51         } >squash.1-5 &&
52         {
53                 echo "Squashed commit of the following:" &&
54                 echo &&
55                 git log --no-merges ^HEAD c2 c3
56         } >squash.1-5-9 &&
57         echo >msg.nolog &&
58         {
59                 echo "* commit 'c3':" &&
60                 echo "  commit 3" &&
61                 echo
62         } >msg.log
63 }
64
65 verify_merge () {
66         test_cmp "$2" "$1" &&
67         git update-index --refresh &&
68         git diff --exit-code &&
69         if test -n "$3"
70         then
71                 git show -s --pretty=format:%s HEAD >msg.act &&
72                 test_cmp "$3" msg.act
73         fi
74 }
75
76 verify_head () {
77         echo "$1" >head.expected &&
78         git rev-parse HEAD >head.actual &&
79         test_cmp head.expected head.actual
80 }
81
82 verify_parents () {
83         printf '%s\n' "$@" >parents.expected &&
84         >parents.actual &&
85         i=1 &&
86         while test $i -le $#
87         do
88                 git rev-parse HEAD^$i >>parents.actual &&
89                 i=$(expr $i + 1) ||
90                 return 1
91         done &&
92         test_must_fail git rev-parse --verify "HEAD^$i" &&
93         test_cmp parents.expected parents.actual
94 }
95
96 verify_mergeheads () {
97         printf '%s\n' "$@" >mergehead.expected &&
98         test_cmp mergehead.expected .git/MERGE_HEAD
99 }
100
101 verify_no_mergehead () {
102         ! test -e .git/MERGE_HEAD
103 }
104
105 test_expect_success 'setup' '
106         git add file &&
107         test_tick &&
108         git commit -m "commit 0" &&
109         git tag c0 &&
110         c0=$(git rev-parse HEAD) &&
111         cp file.1 file &&
112         git add file &&
113         test_tick &&
114         git commit -m "commit 1" &&
115         git tag c1 &&
116         c1=$(git rev-parse HEAD) &&
117         git reset --hard "$c0" &&
118         cp file.5 file &&
119         git add file &&
120         test_tick &&
121         git commit -m "commit 2" &&
122         git tag c2 &&
123         c2=$(git rev-parse HEAD) &&
124         git reset --hard "$c0" &&
125         cp file.9 file &&
126         git add file &&
127         test_tick &&
128         git commit -m "commit 3" &&
129         git tag c3 &&
130         c3=$(git rev-parse HEAD)
131         git reset --hard "$c0" &&
132         create_merge_msgs
133 '
134
135 test_debug 'git log --graph --decorate --oneline --all'
136
137 test_expect_success 'test option parsing' '
138         test_must_fail git merge -$ c1 &&
139         test_must_fail git merge --no-such c1 &&
140         test_must_fail git merge -s foobar c1 &&
141         test_must_fail git merge -s=foobar c1 &&
142         test_must_fail git merge -m &&
143         test_must_fail git merge
144 '
145
146 test_expect_success 'merge -h with invalid index' '
147         mkdir broken &&
148         (
149                 cd broken &&
150                 git init &&
151                 >.git/index &&
152                 test_expect_code 129 git merge -h 2>usage
153         ) &&
154         grep "[Uu]sage: git merge" broken/usage
155 '
156
157 test_expect_success 'reject non-strategy with a git-merge-foo name' '
158         test_must_fail git merge -s index c1
159 '
160
161 test_expect_success 'merge c0 with c1' '
162         echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
163
164         git reset --hard c0 &&
165         git merge c1 &&
166         verify_merge file result.1 &&
167         verify_head "$c1" &&
168
169         git reflog -1 >reflog.actual &&
170         sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
171         test_cmp reflog.expected reflog.fuzzy
172 '
173
174 test_debug 'git log --graph --decorate --oneline --all'
175
176 test_expect_success 'merge c0 with c1 with --ff-only' '
177         git reset --hard c0 &&
178         git merge --ff-only c1 &&
179         git merge --ff-only HEAD c0 c1 &&
180         verify_merge file result.1 &&
181         verify_head "$c1"
182 '
183
184 test_debug 'git log --graph --decorate --oneline --all'
185
186 test_expect_success 'merge from unborn branch' '
187         git checkout -f master &&
188         test_might_fail git branch -D kid &&
189
190         echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
191
192         git checkout --orphan kid &&
193         test_when_finished "git checkout -f master" &&
194         git rm -fr . &&
195         test_tick &&
196         git merge --ff-only c1 &&
197         verify_merge file result.1 &&
198         verify_head "$c1" &&
199
200         git reflog -1 >reflog.actual &&
201         sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
202         test_cmp reflog.expected reflog.fuzzy
203 '
204
205 test_debug 'git log --graph --decorate --oneline --all'
206
207 test_expect_success 'merge c1 with c2' '
208         git reset --hard c1 &&
209         test_tick &&
210         git merge c2 &&
211         verify_merge file result.1-5 msg.1-5 &&
212         verify_parents $c1 $c2
213 '
214
215 test_debug 'git log --graph --decorate --oneline --all'
216
217 test_expect_success 'merge c1 with c2 and c3' '
218         git reset --hard c1 &&
219         test_tick &&
220         git merge c2 c3 &&
221         verify_merge file result.1-5-9 msg.1-5-9 &&
222         verify_parents $c1 $c2 $c3
223 '
224
225 test_debug 'git log --graph --decorate --oneline --all'
226
227 test_expect_success 'failing merges with --ff-only' '
228         git reset --hard c1 &&
229         test_tick &&
230         test_must_fail git merge --ff-only c2 &&
231         test_must_fail git merge --ff-only c3 &&
232         test_must_fail git merge --ff-only c2 c3
233 '
234
235 test_expect_success 'merge c0 with c1 (no-commit)' '
236         git reset --hard c0 &&
237         git merge --no-commit c1 &&
238         verify_merge file result.1 &&
239         verify_head $c1
240 '
241
242 test_debug 'git log --graph --decorate --oneline --all'
243
244 test_expect_success 'merge c1 with c2 (no-commit)' '
245         git reset --hard c1 &&
246         git merge --no-commit c2 &&
247         verify_merge file result.1-5 &&
248         verify_head $c1 &&
249         verify_mergeheads $c2
250 '
251
252 test_debug 'git log --graph --decorate --oneline --all'
253
254 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
255         git reset --hard c1 &&
256         git merge --no-commit c2 c3 &&
257         verify_merge file result.1-5-9 &&
258         verify_head $c1 &&
259         verify_mergeheads $c2 $c3
260 '
261
262 test_debug 'git log --graph --decorate --oneline --all'
263
264 test_expect_success 'merge c0 with c1 (squash)' '
265         git reset --hard c0 &&
266         git merge --squash c1 &&
267         verify_merge file result.1 &&
268         verify_head $c0 &&
269         verify_no_mergehead &&
270         test_cmp squash.1 .git/SQUASH_MSG
271 '
272
273 test_debug 'git log --graph --decorate --oneline --all'
274
275 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
276         git reset --hard c0 &&
277         git merge --squash --ff-only c1 &&
278         verify_merge file result.1 &&
279         verify_head $c0 &&
280         verify_no_mergehead &&
281         test_cmp squash.1 .git/SQUASH_MSG
282 '
283
284 test_debug 'git log --graph --decorate --oneline --all'
285
286 test_expect_success 'merge c1 with c2 (squash)' '
287         git reset --hard c1 &&
288         git merge --squash c2 &&
289         verify_merge file result.1-5 &&
290         verify_head $c1 &&
291         verify_no_mergehead &&
292         test_cmp squash.1-5 .git/SQUASH_MSG
293 '
294
295 test_debug 'git log --graph --decorate --oneline --all'
296
297 test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
298         git reset --hard c1 &&
299         test_must_fail git merge --squash --ff-only c2
300 '
301
302 test_debug 'git log --graph --decorate --oneline --all'
303
304 test_expect_success 'merge c1 with c2 and c3 (squash)' '
305         git reset --hard c1 &&
306         git merge --squash c2 c3 &&
307         verify_merge file result.1-5-9 &&
308         verify_head $c1 &&
309         verify_no_mergehead &&
310         test_cmp squash.1-5-9 .git/SQUASH_MSG
311 '
312
313 test_debug 'git log --graph --decorate --oneline --all'
314
315 test_expect_success 'merge c1 with c2 (no-commit in config)' '
316         git reset --hard c1 &&
317         git config branch.master.mergeoptions "--no-commit" &&
318         git merge c2 &&
319         verify_merge file result.1-5 &&
320         verify_head $c1 &&
321         verify_mergeheads $c2
322 '
323
324 test_debug 'git log --graph --decorate --oneline --all'
325
326 test_expect_success 'merge c1 with c2 (log in config)' '
327         git config branch.master.mergeoptions "" &&
328         git reset --hard c1 &&
329         git merge --log c2 &&
330         git show -s --pretty=tformat:%s%n%b >expect &&
331
332         git config branch.master.mergeoptions --log &&
333         git reset --hard c1 &&
334         git merge c2 &&
335         git show -s --pretty=tformat:%s%n%b >actual &&
336
337         test_cmp expect actual
338 '
339
340 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
341         (
342                 git config --remove-section branch.master
343                 git config --remove-section merge
344         )
345         git reset --hard c1 &&
346         git merge c2 &&
347         git show -s --pretty=tformat:%s%n%b >expect &&
348
349         git config branch.master.mergeoptions "--no-log" &&
350         git config merge.log true &&
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 (squash in config)' '
359         git reset --hard c1 &&
360         git config branch.master.mergeoptions "--squash" &&
361         git merge c2 &&
362         verify_merge file result.1-5 &&
363         verify_head $c1 &&
364         verify_no_mergehead &&
365         test_cmp squash.1-5 .git/SQUASH_MSG
366 '
367
368 test_debug 'git log --graph --decorate --oneline --all'
369
370 test_expect_success 'override config option -n with --summary' '
371         git reset --hard c1 &&
372         git config branch.master.mergeoptions "-n" &&
373         test_tick &&
374         git merge --summary c2 >diffstat.txt &&
375         verify_merge file result.1-5 msg.1-5 &&
376         verify_parents $c1 $c2 &&
377         if ! grep "^ file |  *2 +-$" diffstat.txt
378         then
379                 echo "[OOPS] diffstat was not generated with --summary"
380                 false
381         fi
382 '
383
384 test_expect_success 'override config option -n with --stat' '
385         git reset --hard c1 &&
386         git config branch.master.mergeoptions "-n" &&
387         test_tick &&
388         git merge --stat c2 >diffstat.txt &&
389         verify_merge file result.1-5 msg.1-5 &&
390         verify_parents $c1 $c2 &&
391         if ! grep "^ file |  *2 +-$" diffstat.txt
392         then
393                 echo "[OOPS] diffstat was not generated with --stat"
394                 false
395         fi
396 '
397
398 test_debug 'git log --graph --decorate --oneline --all'
399
400 test_expect_success 'override config option --stat' '
401         git reset --hard c1 &&
402         git config branch.master.mergeoptions "--stat" &&
403         test_tick &&
404         git merge -n c2 >diffstat.txt &&
405         verify_merge file result.1-5 msg.1-5 &&
406         verify_parents $c1 $c2 &&
407         if grep "^ file |  *2 +-$" diffstat.txt
408         then
409                 echo "[OOPS] diffstat was generated"
410                 false
411         fi
412 '
413
414 test_debug 'git log --graph --decorate --oneline --all'
415
416 test_expect_success 'merge c1 with c2 (override --no-commit)' '
417         git reset --hard c1 &&
418         git config branch.master.mergeoptions "--no-commit" &&
419         test_tick &&
420         git merge --commit c2 &&
421         verify_merge file result.1-5 msg.1-5 &&
422         verify_parents $c1 $c2
423 '
424
425 test_debug 'git log --graph --decorate --oneline --all'
426
427 test_expect_success 'merge c1 with c2 (override --squash)' '
428         git reset --hard c1 &&
429         git config branch.master.mergeoptions "--squash" &&
430         test_tick &&
431         git merge --no-squash c2 &&
432         verify_merge file result.1-5 msg.1-5 &&
433         verify_parents $c1 $c2
434 '
435
436 test_debug 'git log --graph --decorate --oneline --all'
437
438 test_expect_success 'merge c0 with c1 (no-ff)' '
439         git reset --hard c0 &&
440         git config branch.master.mergeoptions "" &&
441         test_tick &&
442         git merge --no-ff c1 &&
443         verify_merge file result.1 &&
444         verify_parents $c0 $c1
445 '
446
447 test_debug 'git log --graph --decorate --oneline --all'
448
449 test_expect_success 'combining --squash and --no-ff is refused' '
450         test_must_fail git merge --squash --no-ff c1 &&
451         test_must_fail git merge --no-ff --squash c1
452 '
453
454 test_expect_success 'combining --ff-only and --no-ff is refused' '
455         test_must_fail git merge --ff-only --no-ff c1 &&
456         test_must_fail git merge --no-ff --ff-only c1
457 '
458
459 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
460         git reset --hard c0 &&
461         git config branch.master.mergeoptions "--no-ff" &&
462         git merge --ff c1 &&
463         verify_merge file result.1 &&
464         verify_head $c1
465 '
466
467 test_expect_success 'merge log message' '
468         git reset --hard c0 &&
469         git merge --no-log c2 &&
470         git show -s --pretty=format:%b HEAD >msg.act &&
471         test_cmp msg.nolog msg.act &&
472
473         git merge --log c3 &&
474         git show -s --pretty=format:%b HEAD >msg.act &&
475         test_cmp msg.log msg.act &&
476
477         git reset --hard HEAD^ &&
478         git config merge.log yes &&
479         git merge c3 &&
480         git show -s --pretty=format:%b HEAD >msg.act &&
481         test_cmp msg.log msg.act
482 '
483
484 test_debug 'git log --graph --decorate --oneline --all'
485
486 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
487        git reset --hard c1 &&
488        git config branch.master.mergeoptions "" &&
489        test_tick &&
490        git merge c0 c2 c0 c1 &&
491        verify_merge file result.1-5 &&
492        verify_parents $c1 $c2
493 '
494
495 test_debug 'git log --graph --decorate --oneline --all'
496
497 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
498        git reset --hard c1 &&
499        git config branch.master.mergeoptions "" &&
500        test_tick &&
501        git merge c0 c2 c0 c1 &&
502        verify_merge file result.1-5 &&
503        verify_parents $c1 $c2
504 '
505
506 test_debug 'git log --graph --decorate --oneline --all'
507
508 test_expect_success 'merge c1 with c1 and c2' '
509        git reset --hard c1 &&
510        git config branch.master.mergeoptions "" &&
511        test_tick &&
512        git merge c1 c2 &&
513        verify_merge file result.1-5 &&
514        verify_parents $c1 $c2
515 '
516
517 test_debug 'git log --graph --decorate --oneline --all'
518
519 test_expect_success 'merge fast-forward in a dirty tree' '
520        git reset --hard c0 &&
521        mv file file1 &&
522        cat file1 >file &&
523        rm -f file1 &&
524        git merge c2
525 '
526
527 test_debug 'git log --graph --decorate --oneline --all'
528
529 test_expect_success 'in-index merge' '
530         git reset --hard c0 &&
531         git merge --no-ff -s resolve c1 >out &&
532         test_i18ngrep "Wonderful." out &&
533         verify_parents $c0 $c1
534 '
535
536 test_debug 'git log --graph --decorate --oneline --all'
537
538 test_expect_success 'refresh the index before merging' '
539         git reset --hard c1 &&
540         cp file file.n && mv -f file.n file &&
541         git merge c3
542 '
543
544 cat >expected.branch <<\EOF
545 Merge branch 'c5-branch' (early part)
546 EOF
547 cat >expected.tag <<\EOF
548 Merge commit 'c5~1'
549 EOF
550
551 test_expect_success 'merge early part of c2' '
552         git reset --hard c3 &&
553         echo c4 >c4.c &&
554         git add c4.c &&
555         git commit -m c4 &&
556         git tag c4 &&
557         echo c5 >c5.c &&
558         git add c5.c &&
559         git commit -m c5 &&
560         git tag c5 &&
561         git reset --hard c3 &&
562         echo c6 >c6.c &&
563         git add c6.c &&
564         git commit -m c6 &&
565         git tag c6 &&
566         git branch -f c5-branch c5 &&
567         git merge c5-branch~1 &&
568         git show -s --pretty=format:%s HEAD >actual.branch &&
569         git reset --keep HEAD^ &&
570         git merge c5~1 &&
571         git show -s --pretty=format:%s HEAD >actual.tag &&
572         test_cmp expected.branch actual.branch &&
573         test_cmp expected.tag actual.tag
574 '
575
576 test_debug 'git log --graph --decorate --oneline --all'
577
578 test_expect_success 'merge --no-ff --no-commit && commit' '
579         git reset --hard c0 &&
580         git merge --no-ff --no-commit c1 &&
581         EDITOR=: git commit &&
582         verify_parents $c0 $c1
583 '
584
585 test_debug 'git log --graph --decorate --oneline --all'
586
587 test_expect_success 'amending no-ff merge commit' '
588         EDITOR=: git commit --amend &&
589         verify_parents $c0 $c1
590 '
591
592 test_debug 'git log --graph --decorate --oneline --all'
593
594 test_done