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