]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7600-merge.sh
merge: make branch.<name>.mergeoptions correctly override merge.<option>
[thirdparty/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 . ./test-lib.sh
11
12 cat >file <<EOF
13 1
14 2
15 3
16 4
17 5
18 6
19 7
20 8
21 9
22 EOF
23
24 cat >file.1 <<EOF
25 1 X
26 2
27 3
28 4
29 5
30 6
31 7
32 8
33 9
34 EOF
35
36 cat >file.5 <<EOF
37 1
38 2
39 3
40 4
41 5 X
42 6
43 7
44 8
45 9
46 EOF
47
48 cat >file.9 <<EOF
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9 X
58 EOF
59
60 cat >result.1 <<EOF
61 1 X
62 2
63 3
64 4
65 5
66 6
67 7
68 8
69 9
70 EOF
71
72 cat >result.1-5 <<EOF
73 1 X
74 2
75 3
76 4
77 5 X
78 6
79 7
80 8
81 9
82 EOF
83
84 cat >result.1-5-9 <<EOF
85 1 X
86 2
87 3
88 4
89 5 X
90 6
91 7
92 8
93 9 X
94 EOF
95
96 create_merge_msgs() {
97 echo "Merge commit 'c2'" >msg.1-5 &&
98 echo "Merge commit 'c2'; commit 'c3'" >msg.1-5-9 &&
99 echo "Squashed commit of the following:" >squash.1 &&
100 echo >>squash.1 &&
101 git log --no-merges ^HEAD c1 >>squash.1 &&
102 echo "Squashed commit of the following:" >squash.1-5 &&
103 echo >>squash.1-5 &&
104 git log --no-merges ^HEAD c2 >>squash.1-5 &&
105 echo "Squashed commit of the following:" >squash.1-5-9 &&
106 echo >>squash.1-5-9 &&
107 git log --no-merges ^HEAD c2 c3 >>squash.1-5-9 &&
108 echo > msg.nolog &&
109 echo "* commit 'c3':" >msg.log &&
110 echo " commit 3" >>msg.log &&
111 echo >>msg.log
112 }
113
114 verify_diff() {
115 if ! test_cmp "$1" "$2"
116 then
117 echo "$3"
118 false
119 fi
120 }
121
122 verify_merge() {
123 verify_diff "$2" "$1" "[OOPS] bad merge result" &&
124 if test $(git ls-files -u | wc -l) -gt 0
125 then
126 echo "[OOPS] unmerged files"
127 false
128 fi &&
129 if test_must_fail git diff --exit-code
130 then
131 echo "[OOPS] working tree != index"
132 false
133 fi &&
134 if test -n "$3"
135 then
136 git show -s --pretty=format:%s HEAD >msg.act &&
137 verify_diff "$3" msg.act "[OOPS] bad merge message"
138 fi
139 }
140
141 verify_head() {
142 if test "$1" != "$(git rev-parse HEAD)"
143 then
144 echo "[OOPS] HEAD != $1"
145 false
146 fi
147 }
148
149 verify_parents() {
150 i=1
151 while test $# -gt 0
152 do
153 if test "$1" != "$(git rev-parse HEAD^$i)"
154 then
155 echo "[OOPS] HEAD^$i != $1"
156 return 1
157 fi
158 i=$(expr $i + 1)
159 shift
160 done
161 }
162
163 verify_mergeheads() {
164 i=1
165 if ! test -f .git/MERGE_HEAD
166 then
167 echo "[OOPS] MERGE_HEAD is missing"
168 false
169 fi &&
170 while test $# -gt 0
171 do
172 head=$(head -n $i .git/MERGE_HEAD | sed -ne \$p)
173 if test "$1" != "$head"
174 then
175 echo "[OOPS] MERGE_HEAD $i != $1"
176 return 1
177 fi
178 i=$(expr $i + 1)
179 shift
180 done
181 }
182
183 verify_no_mergehead() {
184 if test -f .git/MERGE_HEAD
185 then
186 echo "[OOPS] MERGE_HEAD exists"
187 false
188 fi
189 }
190
191
192 test_expect_success 'setup' '
193 git add file &&
194 test_tick &&
195 git commit -m "commit 0" &&
196 git tag c0 &&
197 c0=$(git rev-parse HEAD) &&
198 cp file.1 file &&
199 git add file &&
200 test_tick &&
201 git commit -m "commit 1" &&
202 git tag c1 &&
203 c1=$(git rev-parse HEAD) &&
204 git reset --hard "$c0" &&
205 cp file.5 file &&
206 git add file &&
207 test_tick &&
208 git commit -m "commit 2" &&
209 git tag c2 &&
210 c2=$(git rev-parse HEAD) &&
211 git reset --hard "$c0" &&
212 cp file.9 file &&
213 git add file &&
214 test_tick &&
215 git commit -m "commit 3" &&
216 git tag c3 &&
217 c3=$(git rev-parse HEAD)
218 git reset --hard "$c0" &&
219 create_merge_msgs
220 '
221
222 test_debug 'gitk --all'
223
224 test_expect_success 'test option parsing' '
225 test_must_fail git merge -$ c1 &&
226 test_must_fail git merge --no-such c1 &&
227 test_must_fail git merge -s foobar c1 &&
228 test_must_fail git merge -s=foobar c1 &&
229 test_must_fail git merge -m &&
230 test_must_fail git merge
231 '
232
233 test_expect_success 'reject non-strategy with a git-merge-foo name' '
234 test_must_fail git merge -s index c1
235 '
236
237 test_expect_success 'merge c0 with c1' '
238 git reset --hard c0 &&
239 git merge c1 &&
240 verify_merge file result.1 &&
241 verify_head "$c1"
242 '
243
244 test_debug 'gitk --all'
245
246 test_expect_success 'merge c0 with c1 with --ff-only' '
247 git reset --hard c0 &&
248 git merge --ff-only c1 &&
249 git merge --ff-only HEAD c0 c1 &&
250 verify_merge file result.1 &&
251 verify_head "$c1"
252 '
253
254 test_debug 'gitk --all'
255
256 test_expect_success 'merge c1 with c2' '
257 git reset --hard c1 &&
258 test_tick &&
259 git merge c2 &&
260 verify_merge file result.1-5 msg.1-5 &&
261 verify_parents $c1 $c2
262 '
263
264 test_debug 'gitk --all'
265
266 test_expect_success 'merge c1 with c2 and c3' '
267 git reset --hard c1 &&
268 test_tick &&
269 git merge c2 c3 &&
270 verify_merge file result.1-5-9 msg.1-5-9 &&
271 verify_parents $c1 $c2 $c3
272 '
273
274 test_debug 'gitk --all'
275
276 test_expect_success 'failing merges with --ff-only' '
277 git reset --hard c1 &&
278 test_tick &&
279 test_must_fail git merge --ff-only c2 &&
280 test_must_fail git merge --ff-only c3 &&
281 test_must_fail git merge --ff-only c2 c3
282 '
283
284 test_expect_success 'merge c0 with c1 (no-commit)' '
285 git reset --hard c0 &&
286 git merge --no-commit c1 &&
287 verify_merge file result.1 &&
288 verify_head $c1
289 '
290
291 test_debug 'gitk --all'
292
293 test_expect_success 'merge c1 with c2 (no-commit)' '
294 git reset --hard c1 &&
295 git merge --no-commit c2 &&
296 verify_merge file result.1-5 &&
297 verify_head $c1 &&
298 verify_mergeheads $c2
299 '
300
301 test_debug 'gitk --all'
302
303 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
304 git reset --hard c1 &&
305 git merge --no-commit c2 c3 &&
306 verify_merge file result.1-5-9 &&
307 verify_head $c1 &&
308 verify_mergeheads $c2 $c3
309 '
310
311 test_debug 'gitk --all'
312
313 test_expect_success 'merge c0 with c1 (squash)' '
314 git reset --hard c0 &&
315 git merge --squash c1 &&
316 verify_merge file result.1 &&
317 verify_head $c0 &&
318 verify_no_mergehead &&
319 verify_diff squash.1 .git/SQUASH_MSG "[OOPS] bad squash message"
320 '
321
322 test_debug 'gitk --all'
323
324 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
325 git reset --hard c0 &&
326 git merge --squash --ff-only c1 &&
327 verify_merge file result.1 &&
328 verify_head $c0 &&
329 verify_no_mergehead &&
330 verify_diff squash.1 .git/SQUASH_MSG "[OOPS] bad squash message"
331 '
332
333 test_debug 'gitk --all'
334
335 test_expect_success 'merge c1 with c2 (squash)' '
336 git reset --hard c1 &&
337 git merge --squash c2 &&
338 verify_merge file result.1-5 &&
339 verify_head $c1 &&
340 verify_no_mergehead &&
341 verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
342 '
343
344 test_debug 'gitk --all'
345
346 test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
347 git reset --hard c1 &&
348 test_must_fail git merge --squash --ff-only c2
349 '
350
351 test_debug 'gitk --all'
352
353 test_expect_success 'merge c1 with c2 and c3 (squash)' '
354 git reset --hard c1 &&
355 git merge --squash c2 c3 &&
356 verify_merge file result.1-5-9 &&
357 verify_head $c1 &&
358 verify_no_mergehead &&
359 verify_diff squash.1-5-9 .git/SQUASH_MSG "[OOPS] bad squash message"
360 '
361
362 test_debug 'gitk --all'
363
364 test_expect_success 'merge c1 with c2 (no-commit in config)' '
365 git reset --hard c1 &&
366 git config branch.master.mergeoptions "--no-commit" &&
367 git merge c2 &&
368 verify_merge file result.1-5 &&
369 verify_head $c1 &&
370 verify_mergeheads $c2
371 '
372
373 test_debug 'gitk --all'
374
375 test_expect_success 'merge c1 with c2 (log in config)' '
376 git config branch.master.mergeoptions "" &&
377 git reset --hard c1 &&
378 git merge --log c2 &&
379 git show -s --pretty=tformat:%s%n%b >expect &&
380
381 git config branch.master.mergeoptions --log &&
382 git reset --hard c1 &&
383 git merge c2 &&
384 git show -s --pretty=tformat:%s%n%b >actual &&
385
386 test_cmp expect actual
387 '
388
389 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
390 (
391 git config --remove-section branch.master
392 git config --remove-section merge
393 )
394 git reset --hard c1 &&
395 git merge c2 &&
396 git show -s --pretty=tformat:%s%n%b >expect &&
397
398 git config branch.master.mergeoptions "--no-log" &&
399 git config merge.log true &&
400 git reset --hard c1 &&
401 git merge c2 &&
402 git show -s --pretty=tformat:%s%n%b >actual &&
403
404 test_cmp expect actual
405 '
406
407 test_expect_success 'merge c1 with c2 (squash in config)' '
408 git reset --hard c1 &&
409 git config branch.master.mergeoptions "--squash" &&
410 git merge c2 &&
411 verify_merge file result.1-5 &&
412 verify_head $c1 &&
413 verify_no_mergehead &&
414 verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
415 '
416
417 test_debug 'gitk --all'
418
419 test_expect_success 'override config option -n with --summary' '
420 git reset --hard c1 &&
421 git config branch.master.mergeoptions "-n" &&
422 test_tick &&
423 git merge --summary c2 >diffstat.txt &&
424 verify_merge file result.1-5 msg.1-5 &&
425 verify_parents $c1 $c2 &&
426 if ! grep "^ file | *2 +-$" diffstat.txt
427 then
428 echo "[OOPS] diffstat was not generated with --summary"
429 false
430 fi
431 '
432
433 test_expect_success 'override config option -n with --stat' '
434 git reset --hard c1 &&
435 git config branch.master.mergeoptions "-n" &&
436 test_tick &&
437 git merge --stat c2 >diffstat.txt &&
438 verify_merge file result.1-5 msg.1-5 &&
439 verify_parents $c1 $c2 &&
440 if ! grep "^ file | *2 +-$" diffstat.txt
441 then
442 echo "[OOPS] diffstat was not generated with --stat"
443 false
444 fi
445 '
446
447 test_debug 'gitk --all'
448
449 test_expect_success 'override config option --stat' '
450 git reset --hard c1 &&
451 git config branch.master.mergeoptions "--stat" &&
452 test_tick &&
453 git merge -n c2 >diffstat.txt &&
454 verify_merge file result.1-5 msg.1-5 &&
455 verify_parents $c1 $c2 &&
456 if grep "^ file | *2 +-$" diffstat.txt
457 then
458 echo "[OOPS] diffstat was generated"
459 false
460 fi
461 '
462
463 test_debug 'gitk --all'
464
465 test_expect_success 'merge c1 with c2 (override --no-commit)' '
466 git reset --hard c1 &&
467 git config branch.master.mergeoptions "--no-commit" &&
468 test_tick &&
469 git merge --commit c2 &&
470 verify_merge file result.1-5 msg.1-5 &&
471 verify_parents $c1 $c2
472 '
473
474 test_debug 'gitk --all'
475
476 test_expect_success 'merge c1 with c2 (override --squash)' '
477 git reset --hard c1 &&
478 git config branch.master.mergeoptions "--squash" &&
479 test_tick &&
480 git merge --no-squash c2 &&
481 verify_merge file result.1-5 msg.1-5 &&
482 verify_parents $c1 $c2
483 '
484
485 test_debug 'gitk --all'
486
487 test_expect_success 'merge c0 with c1 (no-ff)' '
488 git reset --hard c0 &&
489 git config branch.master.mergeoptions "" &&
490 test_tick &&
491 git merge --no-ff c1 &&
492 verify_merge file result.1 &&
493 verify_parents $c0 $c1
494 '
495
496 test_debug 'gitk --all'
497
498 test_expect_success 'combining --squash and --no-ff is refused' '
499 test_must_fail git merge --squash --no-ff c1 &&
500 test_must_fail git merge --no-ff --squash c1
501 '
502
503 test_expect_success 'combining --ff-only and --no-ff is refused' '
504 test_must_fail git merge --ff-only --no-ff c1 &&
505 test_must_fail git merge --no-ff --ff-only c1
506 '
507
508 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
509 git reset --hard c0 &&
510 git config branch.master.mergeoptions "--no-ff" &&
511 git merge --ff c1 &&
512 verify_merge file result.1 &&
513 verify_head $c1
514 '
515
516 test_expect_success 'merge log message' '
517 git reset --hard c0 &&
518 git merge --no-log c2 &&
519 git show -s --pretty=format:%b HEAD >msg.act &&
520 verify_diff msg.nolog msg.act "[OOPS] bad merge log message" &&
521
522 git merge --log c3 &&
523 git show -s --pretty=format:%b HEAD >msg.act &&
524 verify_diff msg.log msg.act "[OOPS] bad merge log message" &&
525
526 git reset --hard HEAD^ &&
527 git config merge.log yes &&
528 git merge c3 &&
529 git show -s --pretty=format:%b HEAD >msg.act &&
530 verify_diff msg.log msg.act "[OOPS] bad merge log message"
531 '
532
533 test_debug 'gitk --all'
534
535 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
536 git reset --hard c1 &&
537 git config branch.master.mergeoptions "" &&
538 test_tick &&
539 git merge c0 c2 c0 c1 &&
540 verify_merge file result.1-5 &&
541 verify_parents $c1 $c2
542 '
543
544 test_debug 'gitk --all'
545
546 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
547 git reset --hard c1 &&
548 git config branch.master.mergeoptions "" &&
549 test_tick &&
550 git merge c0 c2 c0 c1 &&
551 verify_merge file result.1-5 &&
552 verify_parents $c1 $c2
553 '
554
555 test_debug 'gitk --all'
556
557 test_expect_success 'merge c1 with c1 and c2' '
558 git reset --hard c1 &&
559 git config branch.master.mergeoptions "" &&
560 test_tick &&
561 git merge c1 c2 &&
562 verify_merge file result.1-5 &&
563 verify_parents $c1 $c2
564 '
565
566 test_debug 'gitk --all'
567
568 test_expect_success 'merge fast-forward in a dirty tree' '
569 git reset --hard c0 &&
570 mv file file1 &&
571 cat file1 >file &&
572 rm -f file1 &&
573 git merge c2
574 '
575
576 test_debug 'gitk --all'
577
578 test_expect_success 'in-index merge' '
579 git reset --hard c0 &&
580 git merge --no-ff -s resolve c1 > out &&
581 grep "Wonderful." out &&
582 verify_parents $c0 $c1
583 '
584
585 test_debug 'gitk --all'
586
587 test_expect_success 'refresh the index before merging' '
588 git reset --hard c1 &&
589 sleep 1 &&
590 touch file &&
591 git merge c3
592 '
593
594 cat >expected <<EOF
595 Merge branch 'c5' (early part)
596 EOF
597
598 test_expect_success 'merge early part of c2' '
599 git reset --hard c3 &&
600 echo c4 > c4.c &&
601 git add c4.c &&
602 git commit -m c4 &&
603 git tag c4 &&
604 echo c5 > c5.c &&
605 git add c5.c &&
606 git commit -m c5 &&
607 git tag c5 &&
608 git reset --hard c3 &&
609 echo c6 > c6.c &&
610 git add c6.c &&
611 git commit -m c6 &&
612 git tag c6 &&
613 git merge c5~1 &&
614 git show -s --pretty=format:%s HEAD > actual &&
615 test_cmp actual expected
616 '
617
618 test_debug 'gitk --all'
619
620 test_expect_success 'merge --no-ff --no-commit && commit' '
621 git reset --hard c0 &&
622 git merge --no-ff --no-commit c1 &&
623 EDITOR=: git commit &&
624 verify_parents $c0 $c1
625 '
626
627 test_debug 'gitk --all'
628
629 test_expect_success 'amending no-ff merge commit' '
630 EDITOR=: git commit --amend &&
631 verify_parents $c0 $c1
632 '
633
634 test_debug 'gitk --all'
635
636 test_done