]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7600-merge.sh
The fifteenth batch
[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
1e2ae142 17 * [main] Merge commit 'c1'
4c073457 18--------
1e2ae142 19 - [main] Merge commit 'c1'
4c073457
JN
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 28
1e2ae142 29GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
30export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
31
a85d1b69 32. ./test-lib.sh
b5c9f1c1 33. "$TEST_DIRECTORY"/lib-gpg.sh
a85d1b69 34
fd6852ca 35test_write_lines 1 2 3 4 5 6 7 8 9 >file
a03b5553 36cp file file.orig
fd6852ca 37test_write_lines '1 X' 2 3 4 5 6 7 8 9 >file.1
a03b5553 38test_write_lines 1 2 '3 X' 4 5 6 7 8 9 >file.3
fd6852ca
DL
39test_write_lines 1 2 3 4 '5 X' 6 7 8 9 >file.5
40test_write_lines 1 2 3 4 5 6 7 8 '9 X' >file.9
41test_write_lines 1 2 3 4 5 6 7 8 '9 Y' >file.9y
42test_write_lines '1 X' 2 3 4 5 6 7 8 9 >result.1
43test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
a03b5553 44test_write_lines '1 X' 2 3 4 5 6 7 8 '9 X' >result.1-9
fd6852ca 45test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
a03b5553 46test_write_lines '1 X' 2 '3 X' 4 '5 X' 6 7 8 '9 X' >result.1-3-5-9
fd6852ca 47test_write_lines 1 2 3 4 5 6 7 8 '9 Z' >result.9z
4c073457 48
73151df0 49create_merge_msgs () {
21531927
JH
50 echo "Merge tag 'c2'" >msg.1-5 &&
51 echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
73151df0
JN
52 {
53 echo "Squashed commit of the following:" &&
54 echo &&
55 git log --no-merges ^HEAD c1
56 } >squash.1 &&
57 {
58 echo "Squashed commit of the following:" &&
59 echo &&
60 git log --no-merges ^HEAD c2
61 } >squash.1-5 &&
62 {
63 echo "Squashed commit of the following:" &&
64 echo &&
65 git log --no-merges ^HEAD c2 c3
66 } >squash.1-5-9 &&
73151df0 67 {
57b58db7 68 echo "* tag 'c3':" &&
ad2f7255 69 echo " commit 3"
73151df0
JN
70 } >msg.log
71}
4c073457 72
73151df0
JN
73verify_merge () {
74 test_cmp "$2" "$1" &&
75 git update-index --refresh &&
76 git diff --exit-code &&
77 if test -n "$3"
78 then
ad2f7255 79 git show -s --pretty=tformat:%s HEAD >msg.act &&
73151df0
JN
80 test_cmp "$3" msg.act
81 fi
82}
4c073457 83
73151df0
JN
84verify_head () {
85 echo "$1" >head.expected &&
86 git rev-parse HEAD >head.actual &&
87 test_cmp head.expected head.actual
88}
4c073457 89
73151df0 90verify_parents () {
fd6852ca 91 test_write_lines "$@" >parents.expected &&
73151df0
JN
92 >parents.actual &&
93 i=1 &&
94 while test $i -le $#
95 do
96 git rev-parse HEAD^$i >>parents.actual &&
97 i=$(expr $i + 1) ||
98 return 1
99 done &&
3fc0dbf0 100 test_must_fail git rev-parse --verify "HEAD^$i" &&
73151df0
JN
101 test_cmp parents.expected parents.actual
102}
a85d1b69 103
73151df0 104verify_mergeheads () {
fd6852ca 105 test_write_lines "$@" >mergehead.expected &&
274a5c06
JH
106 while read sha1 rest
107 do
4d81ce1b 108 git rev-parse $sha1 || return 1
274a5c06
JH
109 done <.git/MERGE_HEAD >mergehead.actual &&
110 test_cmp mergehead.expected mergehead.actual
73151df0 111}
a85d1b69 112
73151df0
JN
113verify_no_mergehead () {
114 ! test -e .git/MERGE_HEAD
115}
a85d1b69
LH
116
117test_expect_success 'setup' '
118 git add file &&
119 test_tick &&
120 git commit -m "commit 0" &&
121 git tag c0 &&
122 c0=$(git rev-parse HEAD) &&
123 cp file.1 file &&
124 git add file &&
12510bd5
PB
125 cp file.1 other &&
126 git add other &&
a85d1b69
LH
127 test_tick &&
128 git commit -m "commit 1" &&
129 git tag c1 &&
130 c1=$(git rev-parse HEAD) &&
131 git reset --hard "$c0" &&
132 cp file.5 file &&
133 git add file &&
134 test_tick &&
135 git commit -m "commit 2" &&
136 git tag c2 &&
137 c2=$(git rev-parse HEAD) &&
138 git reset --hard "$c0" &&
b64c1e07
SS
139 cp file.9y file &&
140 git add file &&
141 test_tick &&
142 git commit -m "commit 7" &&
143 git tag c7 &&
144 git reset --hard "$c0" &&
a85d1b69
LH
145 cp file.9 file &&
146 git add file &&
147 test_tick &&
148 git commit -m "commit 3" &&
149 git tag c3 &&
99094a7a 150 c3=$(git rev-parse HEAD) &&
a85d1b69
LH
151 git reset --hard "$c0" &&
152 create_merge_msgs
153'
154
df516fb5 155test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 156
74f5b7fb 157test_expect_success 'test option parsing' '
01941bd5
JS
158 test_must_fail git merge -$ c1 &&
159 test_must_fail git merge --no-such c1 &&
160 test_must_fail git merge -s foobar c1 &&
161 test_must_fail git merge -s=foobar c1 &&
162 test_must_fail git merge -m &&
042e290d
CP
163 test_must_fail git merge --abort foobar &&
164 test_must_fail git merge --abort --quiet &&
367ff694
CP
165 test_must_fail git merge --continue foobar &&
166 test_must_fail git merge --continue --quiet &&
01941bd5 167 test_must_fail git merge
a85d1b69
LH
168'
169
da53eec6
NTND
170test_expect_success 'merge -h with invalid index' '
171 mkdir broken &&
172 (
173 cd broken &&
174 git init &&
175 >.git/index &&
b821c999 176 test_expect_code 129 git merge -h >usage
da53eec6 177 ) &&
6789275d 178 test_grep "[Uu]sage: git merge" broken/usage
da53eec6
NTND
179'
180
dce61e72
MV
181test_expect_success 'reject non-strategy with a git-merge-foo name' '
182 test_must_fail git merge -s index c1
183'
184
a85d1b69 185test_expect_success 'merge c0 with c1' '
ff372c78
JN
186 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
187
3a54f5bd
JH
188 cat >expect <<-\EOF &&
189 Updating FROM..TO
190 Fast-forward
191 file | 2 +-
192 other | 9 +++++++++
193 2 files changed, 10 insertions(+), 1 deletion(-)
194 create mode 100644 other
195 EOF
196
a85d1b69 197 git reset --hard c0 &&
3a54f5bd
JH
198 git merge c1 >out &&
199 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
200 test_cmp expect actual &&
a85d1b69 201 verify_merge file result.1 &&
ff372c78
JN
202 verify_head "$c1" &&
203
204 git reflog -1 >reflog.actual &&
205 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
206 test_cmp reflog.expected reflog.fuzzy
a85d1b69
LH
207'
208
df516fb5 209test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 210
13474835
BG
211test_expect_success 'merge c0 with c1 with --ff-only' '
212 git reset --hard c0 &&
213 git merge --ff-only c1 &&
214 git merge --ff-only HEAD c0 c1 &&
215 verify_merge file result.1 &&
216 verify_head "$c1"
217'
218
c8b48058
JH
219test_expect_success 'the same merge with merge.stat=diffstat' '
220 cat >expect <<-\EOF &&
221 Updating FROM..TO
222 Fast-forward
223 file | 2 +-
224 other | 9 +++++++++
225 2 files changed, 10 insertions(+), 1 deletion(-)
226 create mode 100644 other
227 EOF
228
229 git reset --hard c0 &&
230 git -c merge.stat=diffstat merge c1 >out &&
231 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
232 test_cmp expect actual
233'
234
235test_expect_success 'the same merge with compact summary' '
236 cat >expect <<-\EOF &&
237 Updating FROM..TO
238 Fast-forward
239 file | 2 +-
240 other (new) | 9 +++++++++
241 2 files changed, 10 insertions(+), 1 deletion(-)
242 EOF
243
244 git reset --hard c0 &&
245 git merge --compact-summary c1 >out &&
246 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
247 test_cmp expect actual
248'
249
3a54f5bd
JH
250test_expect_success 'the same merge with compact summary' '
251 cat >expect <<-\EOF &&
252 Updating FROM..TO
253 Fast-forward
254 file | 2 +-
255 other (new) | 9 +++++++++
256 2 files changed, 10 insertions(+), 1 deletion(-)
257 EOF
258
259 git reset --hard c0 &&
260 git merge --compact-summary c1 >out &&
261 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
262 test_cmp expect actual
263'
264
c8b48058
JH
265test_expect_success 'the same merge with merge.stat=compact' '
266 cat >expect <<-\EOF &&
267 Updating FROM..TO
268 Fast-forward
269 file | 2 +-
270 other (new) | 9 +++++++++
271 2 files changed, 10 insertions(+), 1 deletion(-)
272 EOF
273
274 git reset --hard c0 &&
275 git -c merge.stat=compact merge c1 >out &&
276 sed -e "1s/^Updating [0-9a-f.]*/Updating FROM..TO/" out >actual &&
277 test_cmp expect actual
278'
279
df516fb5 280test_debug 'git log --graph --decorate --oneline --all'
94d63ce2
JN
281
282test_expect_success 'merge from unborn branch' '
1e2ae142 283 git checkout -f main &&
94d63ce2
JN
284 test_might_fail git branch -D kid &&
285
286 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
287
288 git checkout --orphan kid &&
1e2ae142 289 test_when_finished "git checkout -f main" &&
94d63ce2
JN
290 git rm -fr . &&
291 test_tick &&
292 git merge --ff-only c1 &&
293 verify_merge file result.1 &&
294 verify_head "$c1" &&
295
296 git reflog -1 >reflog.actual &&
297 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
298 test_cmp reflog.expected reflog.fuzzy
299'
300
301test_debug 'git log --graph --decorate --oneline --all'
13474835 302
a85d1b69
LH
303test_expect_success 'merge c1 with c2' '
304 git reset --hard c1 &&
305 test_tick &&
306 git merge c2 &&
307 verify_merge file result.1-5 msg.1-5 &&
308 verify_parents $c1 $c2
309'
310
2e5a6365
KZ
311test_expect_success 'merge c1 with c2 when index.lock exists' '
312 test_when_finished rm .git/index.lock &&
313 git reset --hard c1 &&
314 >.git/index.lock &&
315 test_must_fail git merge c2 &&
316 test_path_is_missing .git/MERGE_HEAD &&
317 test_path_is_missing .git/MERGE_MODE &&
318 test_path_is_missing .git/MERGE_MSG
319'
320
b64c1e07
SS
321test_expect_success 'merge --squash c3 with c7' '
322 git reset --hard c3 &&
323 test_must_fail git merge --squash c7 &&
324 cat result.9z >file &&
325 git commit --no-edit -a &&
326
b510f0be
DL
327 cat >expect <<-EOF &&
328 Squashed commit of the following:
b64c1e07 329
b510f0be 330 $(git show -s c7)
b64c1e07 331
b510f0be
DL
332 # Conflicts:
333 # file
334 EOF
335 git cat-file commit HEAD >raw &&
c76b84a1 336 sed -e "1,/^$/d" raw >actual &&
b64c1e07
SS
337 test_cmp expect actual
338'
339
d3a9295a
EN
340test_expect_success 'merge --squash --autostash conflict does not attempt to apply autostash' '
341 git reset --hard c3 &&
342 >unrelated &&
343 git add unrelated &&
344 test_must_fail git merge --squash c7 --autostash >out 2>err &&
345 ! grep "Applying autostash resulted in conflicts." err &&
346 grep "When finished, apply stashed changes with \`git stash pop\`" out
347'
348
1055997e
DL
349test_expect_success 'merge c3 with c7 with commit.cleanup = scissors' '
350 git config commit.cleanup scissors &&
351 git reset --hard c3 &&
352 test_must_fail git merge c7 &&
353 cat result.9z >file &&
354 git commit --no-edit -a &&
355
356 cat >expect <<-\EOF &&
21531927 357 Merge tag '"'"'c7'"'"'
1055997e
DL
358
359 # ------------------------ >8 ------------------------
360 # Do not modify or remove the line above.
361 # Everything below it will be ignored.
362 #
363 # Conflicts:
364 # file
365 EOF
366 git cat-file commit HEAD >raw &&
c76b84a1 367 sed -e "1,/^$/d" raw >actual &&
1108cea7 368 test_cmp expect actual
1055997e
DL
369'
370
371test_expect_success 'merge c3 with c7 with --squash commit.cleanup = scissors' '
372 git config commit.cleanup scissors &&
373 git reset --hard c3 &&
374 test_must_fail git merge --squash c7 &&
375 cat result.9z >file &&
376 git commit --no-edit -a &&
377
378 cat >expect <<-EOF &&
379 Squashed commit of the following:
380
381 $(git show -s c7)
382
383 # ------------------------ >8 ------------------------
384 # Do not modify or remove the line above.
385 # Everything below it will be ignored.
386 #
387 # Conflicts:
388 # file
389 EOF
390 git cat-file commit HEAD >raw &&
c76b84a1 391 sed -e "1,/^$/d" raw >actual &&
1108cea7 392 test_cmp expect actual
1055997e
DL
393'
394
df516fb5 395test_debug 'git log --graph --decorate --oneline --all'
a85d1b69
LH
396
397test_expect_success 'merge c1 with c2 and c3' '
398 git reset --hard c1 &&
399 test_tick &&
400 git merge c2 c3 &&
401 verify_merge file result.1-5-9 msg.1-5-9 &&
402 verify_parents $c1 $c2 $c3
403'
404
df516fb5 405test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 406
f23e8dec 407test_expect_success 'merges with --ff-only' '
13474835
BG
408 git reset --hard c1 &&
409 test_tick &&
410 test_must_fail git merge --ff-only c2 &&
411 test_must_fail git merge --ff-only c3 &&
f23e8dec
JH
412 test_must_fail git merge --ff-only c2 c3 &&
413 git reset --hard c0 &&
414 git merge c3 &&
415 verify_head $c3
416'
417
418test_expect_success 'merges with merge.ff=only' '
419 git reset --hard c1 &&
420 test_tick &&
cee683b7 421 test_config merge.ff "only" &&
f23e8dec
JH
422 test_must_fail git merge c2 &&
423 test_must_fail git merge c3 &&
424 test_must_fail git merge c2 c3 &&
425 git reset --hard c0 &&
426 git merge c3 &&
427 verify_head $c3
13474835
BG
428'
429
a85d1b69
LH
430test_expect_success 'merge c0 with c1 (no-commit)' '
431 git reset --hard c0 &&
432 git merge --no-commit c1 &&
433 verify_merge file result.1 &&
434 verify_head $c1
435'
436
df516fb5 437test_debug 'git log --graph --decorate --oneline --all'
a85d1b69
LH
438
439test_expect_success 'merge c1 with c2 (no-commit)' '
440 git reset --hard c1 &&
441 git merge --no-commit c2 &&
442 verify_merge file result.1-5 &&
443 verify_head $c1 &&
444 verify_mergeheads $c2
445'
446
df516fb5 447test_debug 'git log --graph --decorate --oneline --all'
a85d1b69
LH
448
449test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
450 git reset --hard c1 &&
451 git merge --no-commit c2 c3 &&
452 verify_merge file result.1-5-9 &&
453 verify_head $c1 &&
454 verify_mergeheads $c2 $c3
455'
456
df516fb5 457test_debug 'git log --graph --decorate --oneline --all'
a85d1b69
LH
458
459test_expect_success 'merge c0 with c1 (squash)' '
460 git reset --hard c0 &&
461 git merge --squash c1 &&
462 verify_merge file result.1 &&
463 verify_head $c0 &&
464 verify_no_mergehead &&
4c073457 465 test_cmp squash.1 .git/SQUASH_MSG
a85d1b69
LH
466'
467
df516fb5 468test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 469
13474835
BG
470test_expect_success 'merge c0 with c1 (squash, ff-only)' '
471 git reset --hard c0 &&
472 git merge --squash --ff-only c1 &&
473 verify_merge file result.1 &&
474 verify_head $c0 &&
475 verify_no_mergehead &&
4c073457 476 test_cmp squash.1 .git/SQUASH_MSG
13474835
BG
477'
478
df516fb5 479test_debug 'git log --graph --decorate --oneline --all'
13474835 480
a85d1b69
LH
481test_expect_success 'merge c1 with c2 (squash)' '
482 git reset --hard c1 &&
483 git merge --squash c2 &&
484 verify_merge file result.1-5 &&
485 verify_head $c1 &&
486 verify_no_mergehead &&
4c073457 487 test_cmp squash.1-5 .git/SQUASH_MSG
a85d1b69
LH
488'
489
df516fb5 490test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 491
f7e604ed 492test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
13474835
BG
493 git reset --hard c1 &&
494 test_must_fail git merge --squash --ff-only c2
495'
496
df516fb5 497test_debug 'git log --graph --decorate --oneline --all'
13474835 498
a85d1b69
LH
499test_expect_success 'merge c1 with c2 and c3 (squash)' '
500 git reset --hard c1 &&
501 git merge --squash c2 c3 &&
502 verify_merge file result.1-5-9 &&
503 verify_head $c1 &&
504 verify_no_mergehead &&
4c073457 505 test_cmp squash.1-5-9 .git/SQUASH_MSG
a85d1b69
LH
506'
507
df516fb5 508test_debug 'git log --graph --decorate --oneline --all'
a85d1b69 509
aec7b362
LH
510test_expect_success 'merge c1 with c2 (no-commit in config)' '
511 git reset --hard c1 &&
1e2ae142 512 test_config branch.main.mergeoptions "--no-commit" &&
aec7b362
LH
513 git merge c2 &&
514 verify_merge file result.1-5 &&
515 verify_head $c1 &&
516 verify_mergeheads $c2
517'
518
df516fb5 519test_debug 'git log --graph --decorate --oneline --all'
aec7b362 520
0d8fc3ef 521test_expect_success 'merge c1 with c2 (log in config)' '
0d8fc3ef
JH
522 git reset --hard c1 &&
523 git merge --log c2 &&
524 git show -s --pretty=tformat:%s%n%b >expect &&
525
1e2ae142 526 test_config branch.main.mergeoptions "--log" &&
0d8fc3ef
JH
527 git reset --hard c1 &&
528 git merge c2 &&
529 git show -s --pretty=tformat:%s%n%b >actual &&
530
531 test_cmp expect actual
532'
533
534test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
0d8fc3ef
JH
535 git reset --hard c1 &&
536 git merge c2 &&
537 git show -s --pretty=tformat:%s%n%b >expect &&
538
1e2ae142 539 test_config branch.main.mergeoptions "--no-log" &&
cee683b7 540 test_config merge.log "true" &&
0d8fc3ef
JH
541 git reset --hard c1 &&
542 git merge c2 &&
543 git show -s --pretty=tformat:%s%n%b >actual &&
544
545 test_cmp expect actual
546'
547
aec7b362
LH
548test_expect_success 'merge c1 with c2 (squash in config)' '
549 git reset --hard c1 &&
1e2ae142 550 test_config branch.main.mergeoptions "--squash" &&
aec7b362
LH
551 git merge c2 &&
552 verify_merge file result.1-5 &&
553 verify_head $c1 &&
554 verify_no_mergehead &&
4c073457 555 test_cmp squash.1-5 .git/SQUASH_MSG
aec7b362
LH
556'
557
df516fb5 558test_debug 'git log --graph --decorate --oneline --all'
aec7b362 559
d8abe148 560test_expect_success 'override config option -n with --summary' '
aec7b362 561 git reset --hard c1 &&
1e2ae142 562 test_config branch.main.mergeoptions "-n" &&
aec7b362
LH
563 test_tick &&
564 git merge --summary c2 >diffstat.txt &&
565 verify_merge file result.1-5 msg.1-5 &&
566 verify_parents $c1 $c2 &&
aadbe44f 567 if ! grep "^ file | *2 +-$" diffstat.txt
aec7b362 568 then
d8abe148
SG
569 echo "[OOPS] diffstat was not generated with --summary"
570 false
571 fi
572'
573
574test_expect_success 'override config option -n with --stat' '
575 git reset --hard c1 &&
1e2ae142 576 test_config branch.main.mergeoptions "-n" &&
d8abe148
SG
577 test_tick &&
578 git merge --stat c2 >diffstat.txt &&
579 verify_merge file result.1-5 msg.1-5 &&
580 verify_parents $c1 $c2 &&
581 if ! grep "^ file | *2 +-$" diffstat.txt
582 then
583 echo "[OOPS] diffstat was not generated with --stat"
584 false
aec7b362
LH
585 fi
586'
587
df516fb5 588test_debug 'git log --graph --decorate --oneline --all'
aec7b362 589
d8abe148 590test_expect_success 'override config option --stat' '
aec7b362 591 git reset --hard c1 &&
1e2ae142 592 test_config branch.main.mergeoptions "--stat" &&
aec7b362
LH
593 test_tick &&
594 git merge -n c2 >diffstat.txt &&
595 verify_merge file result.1-5 msg.1-5 &&
596 verify_parents $c1 $c2 &&
aadbe44f 597 if grep "^ file | *2 +-$" diffstat.txt
aec7b362
LH
598 then
599 echo "[OOPS] diffstat was generated"
600 false
601 fi
602'
603
df516fb5 604test_debug 'git log --graph --decorate --oneline --all'
aec7b362 605
d08af0ad
LH
606test_expect_success 'merge c1 with c2 (override --no-commit)' '
607 git reset --hard c1 &&
1e2ae142 608 test_config branch.main.mergeoptions "--no-commit" &&
d08af0ad
LH
609 test_tick &&
610 git merge --commit c2 &&
611 verify_merge file result.1-5 msg.1-5 &&
612 verify_parents $c1 $c2
613'
614
df516fb5 615test_debug 'git log --graph --decorate --oneline --all'
d08af0ad
LH
616
617test_expect_success 'merge c1 with c2 (override --squash)' '
618 git reset --hard c1 &&
1e2ae142 619 test_config branch.main.mergeoptions "--squash" &&
d08af0ad
LH
620 test_tick &&
621 git merge --no-squash c2 &&
622 verify_merge file result.1-5 msg.1-5 &&
623 verify_parents $c1 $c2
624'
625
df516fb5 626test_debug 'git log --graph --decorate --oneline --all'
d08af0ad 627
d66424c4
LH
628test_expect_success 'merge c0 with c1 (no-ff)' '
629 git reset --hard c0 &&
630 test_tick &&
631 git merge --no-ff c1 &&
632 verify_merge file result.1 &&
633 verify_parents $c0 $c1
634'
635
df516fb5 636test_debug 'git log --graph --decorate --oneline --all'
d66424c4 637
f23e8dec
JH
638test_expect_success 'merge c0 with c1 (merge.ff=false)' '
639 git reset --hard c0 &&
cee683b7 640 test_config merge.ff "false" &&
f23e8dec
JH
641 test_tick &&
642 git merge c1 &&
f23e8dec
JH
643 verify_merge file result.1 &&
644 verify_parents $c0 $c1
645'
646test_debug 'git log --graph --decorate --oneline --all'
647
1e2ae142 648test_expect_success 'combine branch.main.mergeoptions with merge.ff' '
f23e8dec 649 git reset --hard c0 &&
1e2ae142 650 test_config branch.main.mergeoptions "--ff" &&
cee683b7 651 test_config merge.ff "false" &&
f23e8dec
JH
652 test_tick &&
653 git merge c1 &&
f23e8dec
JH
654 verify_merge file result.1 &&
655 verify_parents "$c0"
656'
657
8c5cea00
JN
658test_expect_success 'tolerate unknown values for merge.ff' '
659 git reset --hard c0 &&
cee683b7 660 test_config merge.ff "something-new" &&
8c5cea00
JN
661 test_tick &&
662 git merge c1 2>message &&
8c5cea00 663 verify_head "$c1" &&
1c5e94f4 664 test_must_be_empty message
8c5cea00
JN
665'
666
e6d1f76c 667test_expect_success 'combining --squash and --no-ff is refused' '
8c5cea00 668 git reset --hard c0 &&
e6d1f76c
GP
669 test_must_fail git merge --squash --no-ff c1 &&
670 test_must_fail git merge --no-ff --squash c1
671'
672
1d14d0c9
VV
673test_expect_success 'combining --squash and --commit is refused' '
674 git reset --hard c0 &&
675 test_must_fail git merge --squash --commit c1 &&
676 test_must_fail git merge --commit --squash c1
677'
678
a54841e9
MV
679test_expect_success 'option --ff-only overwrites --no-ff' '
680 git merge --no-ff --ff-only c1 &&
681 test_must_fail git merge --no-ff --ff-only c2
682'
683
6d2d43dc 684test_expect_success 'option --no-ff overrides merge.ff=only config' '
a54841e9
MV
685 git reset --hard c0 &&
686 test_config merge.ff only &&
687 git merge --no-ff c1
13474835
BG
688'
689
d66424c4
LH
690test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
691 git reset --hard c0 &&
1e2ae142 692 test_config branch.main.mergeoptions "--no-ff" &&
d66424c4
LH
693 git merge --ff c1 &&
694 verify_merge file result.1 &&
695 verify_head $c1
696'
697
efb779f8
SG
698test_expect_success 'merge log message' '
699 git reset --hard c0 &&
700 git merge --no-log c2 &&
701 git show -s --pretty=format:%b HEAD >msg.act &&
1c5e94f4 702 test_must_be_empty msg.act &&
cee683b7
YD
703
704 git reset --hard c0 &&
1e2ae142 705 test_config branch.main.mergeoptions "--no-ff" &&
cee683b7
YD
706 git merge --no-log c2 &&
707 git show -s --pretty=format:%b HEAD >msg.act &&
1c5e94f4 708 test_must_be_empty msg.act &&
4393c237 709
efb779f8
SG
710 git merge --log c3 &&
711 git show -s --pretty=format:%b HEAD >msg.act &&
4c073457 712 test_cmp msg.log msg.act &&
4393c237
JH
713
714 git reset --hard HEAD^ &&
cee683b7 715 test_config merge.log "yes" &&
4393c237
JH
716 git merge c3 &&
717 git show -s --pretty=format:%b HEAD >msg.act &&
4c073457 718 test_cmp msg.log msg.act
efb779f8
SG
719'
720
df516fb5 721test_debug 'git log --graph --decorate --oneline --all'
d66424c4 722
3d1dd472 723test_expect_success 'merge c1 with c0, c2, c0, and c1' '
bd48dfad
JC
724 git reset --hard c1 &&
725 test_tick &&
726 git merge c0 c2 c0 c1 &&
727 verify_merge file result.1-5 &&
728 verify_parents $c1 $c2
3d1dd472
SHJ
729'
730
df516fb5 731test_debug 'git log --graph --decorate --oneline --all'
3d1dd472 732
711f6b29 733test_expect_success 'merge c1 with c0, c2, c0, and c1' '
bd48dfad
JC
734 git reset --hard c1 &&
735 test_tick &&
736 git merge c0 c2 c0 c1 &&
737 verify_merge file result.1-5 &&
738 verify_parents $c1 $c2
711f6b29
JH
739'
740
df516fb5 741test_debug 'git log --graph --decorate --oneline --all'
711f6b29
JH
742
743test_expect_success 'merge c1 with c1 and c2' '
bd48dfad
JC
744 git reset --hard c1 &&
745 test_tick &&
746 git merge c1 c2 &&
747 verify_merge file result.1-5 &&
748 verify_parents $c1 $c2
711f6b29
JH
749'
750
df516fb5 751test_debug 'git log --graph --decorate --oneline --all'
711f6b29 752
9ca8f607 753test_expect_success 'merge fast-forward in a dirty tree' '
bd48dfad
JC
754 git reset --hard c0 &&
755 mv file file1 &&
756 cat file1 >file &&
757 rm -f file1 &&
758 git merge c2
9ca8f607
JH
759'
760
df516fb5 761test_debug 'git log --graph --decorate --oneline --all'
9ca8f607 762
c9ea118e 763test_expect_success 'in-index merge' '
446247db 764 git reset --hard c0 &&
4c073457 765 git merge --no-ff -s resolve c1 >out &&
6789275d 766 test_grep "Wonderful." out &&
446247db
JH
767 verify_parents $c0 $c1
768'
769
df516fb5 770test_debug 'git log --graph --decorate --oneline --all'
446247db 771
668f26ff
MV
772test_expect_success 'refresh the index before merging' '
773 git reset --hard c1 &&
016e5ff2 774 cp file file.n && mv -f file.n file &&
668f26ff
MV
775 git merge c3
776'
777
a03b5553
DL
778test_expect_success 'merge with --autostash' '
779 git reset --hard c1 &&
780 git merge-file file file.orig file.9 &&
781 git merge --autostash c2 2>err &&
6789275d 782 test_grep "Applied autostash." err &&
a03b5553
DL
783 git show HEAD:file >merge-result &&
784 test_cmp result.1-5 merge-result &&
785 test_cmp result.1-5-9 file
786'
787
788test_expect_success 'merge with merge.autoStash' '
789 test_config merge.autoStash true &&
790 git reset --hard c1 &&
791 git merge-file file file.orig file.9 &&
792 git merge c2 2>err &&
6789275d 793 test_grep "Applied autostash." err &&
a03b5553
DL
794 git show HEAD:file >merge-result &&
795 test_cmp result.1-5 merge-result &&
796 test_cmp result.1-5-9 file
797'
798
799test_expect_success 'fast-forward merge with --autostash' '
800 git reset --hard c0 &&
801 git merge-file file file.orig file.5 &&
802 git merge --autostash c1 2>err &&
6789275d 803 test_grep "Applied autostash." err &&
a03b5553
DL
804 test_cmp result.1-5 file
805'
806
12510bd5
PB
807test_expect_success 'failed fast-forward merge with --autostash' '
808 git reset --hard c0 &&
809 git merge-file file file.orig file.5 &&
810 cp file.5 other &&
f222bd34 811 test_when_finished "rm other" &&
12510bd5 812 test_must_fail git merge --autostash c1 2>err &&
6789275d 813 test_grep "Applied autostash." err &&
12510bd5
PB
814 test_cmp file.5 file
815'
816
a03b5553
DL
817test_expect_success 'octopus merge with --autostash' '
818 git reset --hard c1 &&
819 git merge-file file file.orig file.3 &&
820 git merge --autostash c2 c3 2>err &&
6789275d 821 test_grep "Applied autostash." err &&
a03b5553
DL
822 git show HEAD:file >merge-result &&
823 test_cmp result.1-5-9 merge-result &&
824 test_cmp result.1-3-5-9 file
825'
826
e082631e
PB
827test_expect_success 'failed merge (exit 2) with --autostash' '
828 git reset --hard c1 &&
829 git merge-file file file.orig file.5 &&
830 test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
6789275d 831 test_grep "Applied autostash." err &&
e082631e
PB
832 test_cmp result.1-5 file
833'
834
a03b5553
DL
835test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
836 git reset --hard c3 &&
837 cp file.1 file &&
838 test_must_fail git merge --autostash c7 &&
839 git merge --abort 2>err &&
6789275d 840 test_grep "Applied autostash." err &&
a03b5553
DL
841 test_cmp file.1 file
842'
843
844test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
845 git reset --hard c1 &&
846 git merge-file file file.orig file.9 &&
847 git diff >expect &&
848 git merge --no-commit --autostash c2 &&
849 git stash show -p MERGE_AUTOSTASH >actual &&
850 test_cmp expect actual &&
851 git commit 2>err &&
6789275d 852 test_grep "Applied autostash." err &&
a03b5553
DL
853 git show HEAD:file >merge-result &&
854 test_cmp result.1-5 merge-result &&
855 test_cmp result.1-5-9 file
856'
857
858test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
859 git reset --hard c1 &&
860 git merge-file file file.orig file.9 &&
861 git diff >expect &&
862 git merge --no-commit --autostash c2 &&
863 git stash show -p MERGE_AUTOSTASH >actual &&
864 test_cmp expect actual &&
865 git merge --continue 2>err &&
6789275d 866 test_grep "Applied autostash." err &&
a03b5553
DL
867 git show HEAD:file >merge-result &&
868 test_cmp result.1-5 merge-result &&
869 test_cmp result.1-5-9 file
870'
871
872test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
873 git reset --hard c1 &&
874 git merge-file file file.orig file.9 &&
875 git diff >expect &&
876 git merge --no-commit --autostash c2 &&
877 git stash show -p MERGE_AUTOSTASH >actual &&
878 test_cmp expect actual &&
879 git merge --abort 2>err &&
6789275d 880 test_grep "Applied autostash." err &&
a03b5553
DL
881 git diff >actual &&
882 test_cmp expect actual
883'
884
885test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
886 git reset --hard c1 &&
887 git merge-file file file.orig file.9 &&
888 git diff >expect &&
889 git merge --no-commit --autostash c2 &&
890 git stash show -p MERGE_AUTOSTASH >actual &&
891 test_cmp expect actual &&
892 git reset --hard 2>err &&
6789275d 893 test_grep "Autostash exists; creating a new stash entry." err &&
a03b5553
DL
894 git diff --exit-code
895'
896
897test_expect_success 'quit merge with --no-commit and --autostash' '
898 git reset --hard c1 &&
899 git merge-file file file.orig file.9 &&
900 git diff >expect &&
901 git merge --no-commit --autostash c2 &&
902 git stash show -p MERGE_AUTOSTASH >actual &&
903 test_cmp expect actual &&
904 git diff HEAD >expect &&
905 git merge --quit 2>err &&
6789275d 906 test_grep "Autostash exists; creating a new stash entry." err &&
a03b5553
DL
907 git diff HEAD >actual &&
908 test_cmp expect actual
909'
910
911test_expect_success 'merge with conflicted --autostash changes' '
912 git reset --hard c1 &&
913 git merge-file file file.orig file.9y &&
914 git diff >expect &&
915 test_when_finished "test_might_fail git stash drop" &&
916 git merge --autostash c3 2>err &&
6789275d 917 test_grep "Applying autostash resulted in conflicts." err &&
a03b5553
DL
918 git show HEAD:file >merge-result &&
919 test_cmp result.1-9 merge-result &&
920 git stash show -p >actual &&
921 test_cmp expect actual
922'
923
b81f925f 924cat >expected.branch <<\EOF
21531927 925Merge branch 'c5-branch' (early part)
b81f925f
JN
926EOF
927cat >expected.tag <<\EOF
21531927 928Merge commit 'c5~1'
4e6d4bc0
MV
929EOF
930
931test_expect_success 'merge early part of c2' '
932 git reset --hard c3 &&
4c073457 933 echo c4 >c4.c &&
4e6d4bc0
MV
934 git add c4.c &&
935 git commit -m c4 &&
936 git tag c4 &&
4c073457 937 echo c5 >c5.c &&
4e6d4bc0
MV
938 git add c5.c &&
939 git commit -m c5 &&
940 git tag c5 &&
941 git reset --hard c3 &&
4c073457 942 echo c6 >c6.c &&
4e6d4bc0
MV
943 git add c6.c &&
944 git commit -m c6 &&
945 git tag c6 &&
b81f925f
JN
946 git branch -f c5-branch c5 &&
947 git merge c5-branch~1 &&
ad2f7255 948 git show -s --pretty=tformat:%s HEAD >actual.branch &&
b81f925f 949 git reset --keep HEAD^ &&
4e6d4bc0 950 git merge c5~1 &&
ad2f7255 951 git show -s --pretty=tformat:%s HEAD >actual.tag &&
b81f925f
JN
952 test_cmp expected.branch actual.branch &&
953 test_cmp expected.tag actual.tag
4e6d4bc0
MV
954'
955
df516fb5 956test_debug 'git log --graph --decorate --oneline --all'
668f26ff 957
cf10f9fd
MV
958test_expect_success 'merge --no-ff --no-commit && commit' '
959 git reset --hard c0 &&
960 git merge --no-ff --no-commit c1 &&
961 EDITOR=: git commit &&
962 verify_parents $c0 $c1
963'
964
df516fb5 965test_debug 'git log --graph --decorate --oneline --all'
cf10f9fd
MV
966
967test_expect_success 'amending no-ff merge commit' '
968 EDITOR=: git commit --amend &&
969 verify_parents $c0 $c1
970'
971
df516fb5 972test_debug 'git log --graph --decorate --oneline --all'
cf10f9fd 973
66f4b98a
JS
974cat >editor <<\EOF
975#!/bin/sh
976# Add a new message string that was not in the template
977(
978 echo "Merge work done on the side branch c1"
979 echo
b510f0be 980 cat "$1"
66f4b98a
JS
981) >"$1.tmp" && mv "$1.tmp" "$1"
982# strip comments and blank lines from end of message
b510f0be 983sed -e '/^#/d' "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' >expected
66f4b98a
JS
984EOF
985chmod 755 editor
986
987test_expect_success 'merge --no-ff --edit' '
988 git reset --hard c0 &&
989 EDITOR=./editor git merge --no-ff --edit c1 &&
990 verify_parents $c0 $c1 &&
991 git cat-file commit HEAD >raw &&
992 grep "work done on the side branch" raw &&
993 sed "1,/^$/d" >actual raw &&
9c5b2fab 994 test_cmp expected actual
66f4b98a
JS
995'
996
adcc94a0
JH
997test_expect_success 'merge annotated/signed tag w/o tracking' '
998 test_when_finished "rm -rf dst; git tag -d anno1" &&
999 git tag -a -m "anno c1" anno1 c1 &&
1000 git init dst &&
1001 git rev-parse c1 >dst/expect &&
1002 (
1003 # c0 fast-forwards to c1 but because this repository
1004 # is not a "downstream" whose refs/tags follows along
1005 # tag from the "upstream", this pull defaults to --no-ff
1006 cd dst &&
1007 git pull .. c0 &&
1008 git pull .. anno1 &&
1009 git rev-parse HEAD^2 >actual &&
1010 test_cmp expect actual
1011 )
1012'
1013
1014test_expect_success 'merge annotated/signed tag w/ tracking' '
1015 test_when_finished "rm -rf dst; git tag -d anno1" &&
1016 git tag -a -m "anno c1" anno1 c1 &&
1017 git init dst &&
1018 git rev-parse c1 >dst/expect &&
1019 (
1020 # c0 fast-forwards to c1 and because this repository
1021 # is a "downstream" whose refs/tags follows along
1022 # tag from the "upstream", this pull defaults to --ff
1023 cd dst &&
1024 git remote add origin .. &&
1025 git pull origin c0 &&
1026 git fetch origin &&
1027 git merge anno1 &&
1028 git rev-parse HEAD >actual &&
1029 test_cmp expect actual
1030 )
1031'
1032
b5c9f1c1
JH
1033test_expect_success GPG 'merge --ff-only tag' '
1034 git reset --hard c0 &&
1035 git commit --allow-empty -m "A newer commit" &&
1036 git tag -s -m "A newer commit" signed &&
1037 git reset --hard c0 &&
1038
1039 git merge --ff-only signed &&
1040 git rev-parse signed^0 >expect &&
1041 git rev-parse HEAD >actual &&
9c5b2fab 1042 test_cmp expect actual
b5c9f1c1
JH
1043'
1044
3adab6f3
JH
1045test_expect_success GPG 'merge --no-edit tag should skip editor' '
1046 git reset --hard c0 &&
1047 git commit --allow-empty -m "A newer commit" &&
1048 git tag -f -s -m "A newer commit" signed &&
1049 git reset --hard c0 &&
1050
adcc94a0 1051 EDITOR=false git merge --no-edit --no-ff signed &&
3adab6f3
JH
1052 git rev-parse signed^0 >expect &&
1053 git rev-parse HEAD^2 >actual &&
9c5b2fab 1054 test_cmp expect actual
3adab6f3
JH
1055'
1056
e34f8027
JK
1057test_expect_success 'set up mod-256 conflict scenario' '
1058 # 256 near-identical stanzas...
1059 for i in $(test_seq 1 256); do
1060 for j in 1 2 3 4 5; do
0c51d6b4 1061 echo $i-$j || return 1
e34f8027
JK
1062 done
1063 done >file &&
1064 git add file &&
1065 git commit -m base &&
1066
1e2ae142
JS
1067 # one side changes the first line of each to "main"
1068 sed s/-1/-main/ file >tmp &&
e34f8027 1069 mv tmp file &&
1e2ae142 1070 git commit -am main &&
e34f8027
JK
1071
1072 # and the other to "side"; merging the two will
1073 # yield 256 separate conflicts
1074 git checkout -b side HEAD^ &&
b510f0be 1075 sed s/-1/-side/ file >tmp &&
e34f8027
JK
1076 mv tmp file &&
1077 git commit -am side
1078'
1079
1080test_expect_success 'merge detects mod-256 conflicts (recursive)' '
1081 git reset --hard &&
1e2ae142 1082 test_must_fail git merge -s recursive main
e34f8027
JK
1083'
1084
1085test_expect_success 'merge detects mod-256 conflicts (resolve)' '
1086 git reset --hard &&
1e2ae142 1087 test_must_fail git merge -s resolve main
e34f8027
JK
1088'
1089
b84e65d4
JH
1090test_expect_success 'merge nothing into void' '
1091 git init void &&
1092 (
1093 cd void &&
1094 git remote add up .. &&
1095 git fetch up &&
1096 test_must_fail git merge FETCH_HEAD
1097 )
1098'
1099
367ff694
CP
1100test_expect_success 'merge can be completed with --continue' '
1101 git reset --hard c0 &&
1102 git merge --no-ff --no-commit c1 &&
1103 git merge --continue &&
1104 verify_parents $c0 $c1
1105'
1106
9d89b355
MG
1107write_script .git/FAKE_EDITOR <<EOF
1108# kill -TERM command added below.
1109EOF
1110
1111test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue' '
1112 git reset --hard c0 &&
1113 ! "$SHELL_PATH" -c '\''
b510f0be 1114 echo kill -TERM $$ >>.git/FAKE_EDITOR
9d89b355
MG
1115 GIT_EDITOR=.git/FAKE_EDITOR
1116 export GIT_EDITOR
1117 exec git merge --no-ff --edit c1'\'' &&
1118 git merge --continue &&
1119 verify_parents $c0 $c1
1120'
1121
f3f8311e
NTND
1122test_expect_success 'merge --quit' '
1123 git init merge-quit &&
1124 (
1125 cd merge-quit &&
1126 test_commit base &&
1127 echo one >>base.t &&
1128 git commit -am one &&
1129 git branch one &&
1130 git checkout base &&
1131 echo two >>base.t &&
1132 git commit -am two &&
1133 test_must_fail git -c rerere.enabled=true merge one &&
1134 test_path_is_file .git/MERGE_HEAD &&
1135 test_path_is_file .git/MERGE_MODE &&
1136 test_path_is_file .git/MERGE_MSG &&
1137 git rerere status >rerere.before &&
1138 git merge --quit &&
1139 test_path_is_missing .git/MERGE_HEAD &&
1140 test_path_is_missing .git/MERGE_MODE &&
1141 test_path_is_missing .git/MERGE_MSG &&
1142 git rerere status >rerere.after &&
1143 test_must_be_empty rerere.after &&
1144 ! test_cmp rerere.after rerere.before
1145 )
1146'
1147
8ed51b06
JK
1148test_expect_success 'merge suggests matching remote refname' '
1149 git commit --allow-empty -m not-local &&
1150 git update-ref refs/remotes/origin/not-local HEAD &&
1151 git reset --hard HEAD^ &&
1152
1153 # This is white-box testing hackery; we happen to know
1154 # that reading packed refs is more picky about the memory
1155 # ownership of strings we pass to for_each_ref() callbacks.
1156 git pack-refs --all --prune &&
1157
1158 test_must_fail git merge not-local 2>stderr &&
1159 grep origin/not-local stderr
1160'
1161
2ed2e199
JK
1162test_expect_success 'suggested names are not ambiguous' '
1163 git update-ref refs/heads/origin/not-local HEAD &&
1164 test_must_fail git merge not-local 2>stderr &&
1165 grep remotes/origin/not-local stderr
1166'
1167
a85d1b69 1168test_done