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