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