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