]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7600-merge.sh
t7600 (merge): check reflog entry
[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
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
dce61e72
MV
147test_expect_success 'reject non-strategy with a git-merge-foo name' '
148 test_must_fail git merge -s index c1
149'
150
a85d1b69 151test_expect_success 'merge c0 with c1' '
ff372c78
JN
152 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
153
a85d1b69
LH
154 git reset --hard c0 &&
155 git merge c1 &&
156 verify_merge file result.1 &&
ff372c78
JN
157 verify_head "$c1" &&
158
159 git reflog -1 >reflog.actual &&
160 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
161 test_cmp reflog.expected reflog.fuzzy
a85d1b69
LH
162'
163
df516fb5 164test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 165
13474835
BG
166test_expect_success 'merge c0 with c1 with --ff-only' '
167 git reset --hard c0 &&
168 git merge --ff-only c1 &&
169 git merge --ff-only HEAD c0 c1 &&
170 verify_merge file result.1 &&
171 verify_head "$c1"
172'
173
df516fb5 174test_debug 'git log --graph --decorate --oneline --all'
13474835 175
a85d1b69
LH
176test_expect_success 'merge c1 with c2' '
177 git reset --hard c1 &&
178 test_tick &&
179 git merge c2 &&
180 verify_merge file result.1-5 msg.1-5 &&
181 verify_parents $c1 $c2
182'
183
df516fb5 184test_debug 'git log --graph --decorate --oneline --all'
a85d1b69
LH
185
186test_expect_success 'merge c1 with c2 and c3' '
187 git reset --hard c1 &&
188 test_tick &&
189 git merge c2 c3 &&
190 verify_merge file result.1-5-9 msg.1-5-9 &&
191 verify_parents $c1 $c2 $c3
192'
193
df516fb5 194test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 195
13474835
BG
196test_expect_success 'failing merges with --ff-only' '
197 git reset --hard c1 &&
198 test_tick &&
199 test_must_fail git merge --ff-only c2 &&
200 test_must_fail git merge --ff-only c3 &&
201 test_must_fail git merge --ff-only c2 c3
202'
203
a85d1b69
LH
204test_expect_success 'merge c0 with c1 (no-commit)' '
205 git reset --hard c0 &&
206 git merge --no-commit c1 &&
207 verify_merge file result.1 &&
208 verify_head $c1
209'
210
df516fb5 211test_debug 'git log --graph --decorate --oneline --all'
a85d1b69
LH
212
213test_expect_success 'merge c1 with c2 (no-commit)' '
214 git reset --hard c1 &&
215 git merge --no-commit c2 &&
216 verify_merge file result.1-5 &&
217 verify_head $c1 &&
218 verify_mergeheads $c2
219'
220
df516fb5 221test_debug 'git log --graph --decorate --oneline --all'
a85d1b69
LH
222
223test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
224 git reset --hard c1 &&
225 git merge --no-commit c2 c3 &&
226 verify_merge file result.1-5-9 &&
227 verify_head $c1 &&
228 verify_mergeheads $c2 $c3
229'
230
df516fb5 231test_debug 'git log --graph --decorate --oneline --all'
a85d1b69
LH
232
233test_expect_success 'merge c0 with c1 (squash)' '
234 git reset --hard c0 &&
235 git merge --squash c1 &&
236 verify_merge file result.1 &&
237 verify_head $c0 &&
238 verify_no_mergehead &&
4c073457 239 test_cmp squash.1 .git/SQUASH_MSG
a85d1b69
LH
240'
241
df516fb5 242test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 243
13474835
BG
244test_expect_success 'merge c0 with c1 (squash, ff-only)' '
245 git reset --hard c0 &&
246 git merge --squash --ff-only c1 &&
247 verify_merge file result.1 &&
248 verify_head $c0 &&
249 verify_no_mergehead &&
4c073457 250 test_cmp squash.1 .git/SQUASH_MSG
13474835
BG
251'
252
df516fb5 253test_debug 'git log --graph --decorate --oneline --all'
13474835 254
a85d1b69
LH
255test_expect_success 'merge c1 with c2 (squash)' '
256 git reset --hard c1 &&
257 git merge --squash c2 &&
258 verify_merge file result.1-5 &&
259 verify_head $c1 &&
260 verify_no_mergehead &&
4c073457 261 test_cmp squash.1-5 .git/SQUASH_MSG
a85d1b69
LH
262'
263
df516fb5 264test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 265
13474835
BG
266test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
267 git reset --hard c1 &&
268 test_must_fail git merge --squash --ff-only c2
269'
270
df516fb5 271test_debug 'git log --graph --decorate --oneline --all'
13474835 272
a85d1b69
LH
273test_expect_success 'merge c1 with c2 and c3 (squash)' '
274 git reset --hard c1 &&
275 git merge --squash c2 c3 &&
276 verify_merge file result.1-5-9 &&
277 verify_head $c1 &&
278 verify_no_mergehead &&
4c073457 279 test_cmp squash.1-5-9 .git/SQUASH_MSG
a85d1b69
LH
280'
281
df516fb5 282test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 283
aec7b362
LH
284test_expect_success 'merge c1 with c2 (no-commit in config)' '
285 git reset --hard c1 &&
286 git config branch.master.mergeoptions "--no-commit" &&
287 git merge c2 &&
288 verify_merge file result.1-5 &&
289 verify_head $c1 &&
290 verify_mergeheads $c2
291'
292
df516fb5 293test_debug 'git log --graph --decorate --oneline --all'
aec7b362
LH
294
295test_expect_success 'merge c1 with c2 (squash in config)' '
296 git reset --hard c1 &&
297 git config branch.master.mergeoptions "--squash" &&
298 git merge c2 &&
299 verify_merge file result.1-5 &&
300 verify_head $c1 &&
301 verify_no_mergehead &&
4c073457 302 test_cmp squash.1-5 .git/SQUASH_MSG
aec7b362
LH
303'
304
df516fb5 305test_debug 'git log --graph --decorate --oneline --all'
aec7b362 306
d8abe148 307test_expect_success 'override config option -n with --summary' '
aec7b362
LH
308 git reset --hard c1 &&
309 git config branch.master.mergeoptions "-n" &&
310 test_tick &&
311 git merge --summary c2 >diffstat.txt &&
312 verify_merge file result.1-5 msg.1-5 &&
313 verify_parents $c1 $c2 &&
aadbe44f 314 if ! grep "^ file | *2 +-$" diffstat.txt
aec7b362 315 then
d8abe148
SG
316 echo "[OOPS] diffstat was not generated with --summary"
317 false
318 fi
319'
320
321test_expect_success 'override config option -n with --stat' '
322 git reset --hard c1 &&
323 git config branch.master.mergeoptions "-n" &&
324 test_tick &&
325 git merge --stat c2 >diffstat.txt &&
326 verify_merge file result.1-5 msg.1-5 &&
327 verify_parents $c1 $c2 &&
328 if ! grep "^ file | *2 +-$" diffstat.txt
329 then
330 echo "[OOPS] diffstat was not generated with --stat"
331 false
aec7b362
LH
332 fi
333'
334
df516fb5 335test_debug 'git log --graph --decorate --oneline --all'
aec7b362 336
d8abe148 337test_expect_success 'override config option --stat' '
aec7b362 338 git reset --hard c1 &&
d8abe148 339 git config branch.master.mergeoptions "--stat" &&
aec7b362
LH
340 test_tick &&
341 git merge -n 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
LH
345 then
346 echo "[OOPS] diffstat was generated"
347 false
348 fi
349'
350
df516fb5 351test_debug 'git log --graph --decorate --oneline --all'
aec7b362 352
d08af0ad
LH
353test_expect_success 'merge c1 with c2 (override --no-commit)' '
354 git reset --hard c1 &&
355 git config branch.master.mergeoptions "--no-commit" &&
356 test_tick &&
357 git merge --commit c2 &&
358 verify_merge file result.1-5 msg.1-5 &&
359 verify_parents $c1 $c2
360'
361
df516fb5 362test_debug 'git log --graph --decorate --oneline --all'
d08af0ad
LH
363
364test_expect_success 'merge c1 with c2 (override --squash)' '
365 git reset --hard c1 &&
366 git config branch.master.mergeoptions "--squash" &&
367 test_tick &&
368 git merge --no-squash c2 &&
369 verify_merge file result.1-5 msg.1-5 &&
370 verify_parents $c1 $c2
371'
372
df516fb5 373test_debug 'git log --graph --decorate --oneline --all'
d08af0ad 374
d66424c4
LH
375test_expect_success 'merge c0 with c1 (no-ff)' '
376 git reset --hard c0 &&
e6d1f76c 377 git config branch.master.mergeoptions "" &&
d66424c4
LH
378 test_tick &&
379 git merge --no-ff c1 &&
380 verify_merge file result.1 &&
381 verify_parents $c0 $c1
382'
383
df516fb5 384test_debug 'git log --graph --decorate --oneline --all'
d66424c4 385
e6d1f76c
GP
386test_expect_success 'combining --squash and --no-ff is refused' '
387 test_must_fail git merge --squash --no-ff c1 &&
388 test_must_fail git merge --no-ff --squash c1
389'
390
13474835
BG
391test_expect_success 'combining --ff-only and --no-ff is refused' '
392 test_must_fail git merge --ff-only --no-ff c1 &&
393 test_must_fail git merge --no-ff --ff-only c1
394'
395
d66424c4
LH
396test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
397 git reset --hard c0 &&
398 git config branch.master.mergeoptions "--no-ff" &&
399 git merge --ff c1 &&
400 verify_merge file result.1 &&
401 verify_head $c1
402'
403
efb779f8
SG
404test_expect_success 'merge log message' '
405 git reset --hard c0 &&
406 git merge --no-log c2 &&
407 git show -s --pretty=format:%b HEAD >msg.act &&
4c073457 408 test_cmp msg.nolog msg.act &&
4393c237 409
efb779f8
SG
410 git merge --log c3 &&
411 git show -s --pretty=format:%b HEAD >msg.act &&
4c073457 412 test_cmp msg.log msg.act &&
4393c237
JH
413
414 git reset --hard HEAD^ &&
415 git config merge.log yes &&
416 git merge c3 &&
417 git show -s --pretty=format:%b HEAD >msg.act &&
4c073457 418 test_cmp msg.log msg.act
efb779f8
SG
419'
420
df516fb5 421test_debug 'git log --graph --decorate --oneline --all'
d66424c4 422
3d1dd472
SHJ
423test_expect_success 'merge c1 with c0, c2, c0, and c1' '
424 git reset --hard c1 &&
425 git config branch.master.mergeoptions "" &&
426 test_tick &&
427 git merge c0 c2 c0 c1 &&
428 verify_merge file result.1-5 &&
429 verify_parents $c1 $c2
430'
431
df516fb5 432test_debug 'git log --graph --decorate --oneline --all'
3d1dd472 433
711f6b29
JH
434test_expect_success 'merge c1 with c0, c2, c0, and c1' '
435 git reset --hard c1 &&
436 git config branch.master.mergeoptions "" &&
437 test_tick &&
438 git merge c0 c2 c0 c1 &&
439 verify_merge file result.1-5 &&
440 verify_parents $c1 $c2
441'
442
df516fb5 443test_debug 'git log --graph --decorate --oneline --all'
711f6b29
JH
444
445test_expect_success 'merge c1 with c1 and c2' '
446 git reset --hard c1 &&
447 git config branch.master.mergeoptions "" &&
448 test_tick &&
449 git merge c1 c2 &&
450 verify_merge file result.1-5 &&
451 verify_parents $c1 $c2
452'
453
df516fb5 454test_debug 'git log --graph --decorate --oneline --all'
711f6b29 455
9ca8f607
JH
456test_expect_success 'merge fast-forward in a dirty tree' '
457 git reset --hard c0 &&
458 mv file file1 &&
459 cat file1 >file &&
460 rm -f file1 &&
461 git merge c2
462'
463
df516fb5 464test_debug 'git log --graph --decorate --oneline --all'
9ca8f607 465
446247db
JH
466test_expect_success 'in-index merge' '
467 git reset --hard c0 &&
4c073457 468 git merge --no-ff -s resolve c1 >out &&
446247db
JH
469 grep "Wonderful." out &&
470 verify_parents $c0 $c1
471'
472
df516fb5 473test_debug 'git log --graph --decorate --oneline --all'
446247db 474
668f26ff
MV
475test_expect_success 'refresh the index before merging' '
476 git reset --hard c1 &&
016e5ff2 477 cp file file.n && mv -f file.n file &&
668f26ff
MV
478 git merge c3
479'
480
b81f925f
JN
481cat >expected.branch <<\EOF
482Merge branch 'c5-branch' (early part)
483EOF
484cat >expected.tag <<\EOF
485Merge commit 'c5~1'
4e6d4bc0
MV
486EOF
487
488test_expect_success 'merge early part of c2' '
489 git reset --hard c3 &&
4c073457 490 echo c4 >c4.c &&
4e6d4bc0
MV
491 git add c4.c &&
492 git commit -m c4 &&
493 git tag c4 &&
4c073457 494 echo c5 >c5.c &&
4e6d4bc0
MV
495 git add c5.c &&
496 git commit -m c5 &&
497 git tag c5 &&
498 git reset --hard c3 &&
4c073457 499 echo c6 >c6.c &&
4e6d4bc0
MV
500 git add c6.c &&
501 git commit -m c6 &&
502 git tag c6 &&
b81f925f
JN
503 git branch -f c5-branch c5 &&
504 git merge c5-branch~1 &&
505 git show -s --pretty=format:%s HEAD >actual.branch &&
506 git reset --keep HEAD^ &&
4e6d4bc0 507 git merge c5~1 &&
b81f925f
JN
508 git show -s --pretty=format:%s HEAD >actual.tag &&
509 test_cmp expected.branch actual.branch &&
510 test_cmp expected.tag actual.tag
4e6d4bc0
MV
511'
512
df516fb5 513test_debug 'git log --graph --decorate --oneline --all'
668f26ff 514
cf10f9fd
MV
515test_expect_success 'merge --no-ff --no-commit && commit' '
516 git reset --hard c0 &&
517 git merge --no-ff --no-commit c1 &&
518 EDITOR=: git commit &&
519 verify_parents $c0 $c1
520'
521
df516fb5 522test_debug 'git log --graph --decorate --oneline --all'
cf10f9fd
MV
523
524test_expect_success 'amending no-ff merge commit' '
525 EDITOR=: git commit --amend &&
526 verify_parents $c0 $c1
527'
528
df516fb5 529test_debug 'git log --graph --decorate --oneline --all'
cf10f9fd 530
a85d1b69 531test_done