Handle new t1501 test case properly with MinGW
[git/dscho.git] / t / t7600-merge.sh
blobe09b932650aab01e360a4f9ae4ecdb57a24d62f3
1 #!/bin/sh
3 # Copyright (c) 2007 Lars Hjemli
6 test_description='git merge
8 Testing basic merge operations/option parsing.
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
29 . ./test-lib.sh
30 . "$TEST_DIRECTORY"/lib-gpg.sh
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 >empty
41 create_merge_msgs () {
42 echo "Merge tag 'c2'" >msg.1-5 &&
43 echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
45 echo "Squashed commit of the following:" &&
46 echo &&
47 git log --no-merges ^HEAD c1
48 } >squash.1 &&
50 echo "Squashed commit of the following:" &&
51 echo &&
52 git log --no-merges ^HEAD c2
53 } >squash.1-5 &&
55 echo "Squashed commit of the following:" &&
56 echo &&
57 git log --no-merges ^HEAD c2 c3
58 } >squash.1-5-9 &&
59 echo >msg.nolog &&
61 echo "By A U Thor" &&
62 echo "* tag 'c3':" &&
63 echo " commit 3" &&
64 echo
65 } >msg.log
68 verify_merge () {
69 test_cmp "$2" "$1" &&
70 git update-index --refresh &&
71 git diff --exit-code &&
72 if test -n "$3"
73 then
74 git show -s --pretty=format:%s HEAD >msg.act &&
75 test_cmp "$3" msg.act
79 verify_head () {
80 echo "$1" >head.expected &&
81 git rev-parse HEAD >head.actual &&
82 test_cmp head.expected head.actual
85 verify_parents () {
86 printf '%s\n' "$@" >parents.expected &&
87 >parents.actual &&
88 i=1 &&
89 while test $i -le $#
91 git rev-parse HEAD^$i >>parents.actual &&
92 i=$(expr $i + 1) ||
93 return 1
94 done &&
95 test_must_fail git rev-parse --verify "HEAD^$i" &&
96 test_cmp parents.expected parents.actual
99 verify_mergeheads () {
100 printf '%s\n' "$@" >mergehead.expected &&
101 while read sha1 rest
103 git rev-parse $sha1
104 done <.git/MERGE_HEAD >mergehead.actual &&
105 test_cmp mergehead.expected mergehead.actual
108 verify_no_mergehead () {
109 ! test -e .git/MERGE_HEAD
112 test_expect_success 'setup' '
113 git add file &&
114 test_tick &&
115 git commit -m "commit 0" &&
116 git tag c0 &&
117 c0=$(git rev-parse HEAD) &&
118 cp file.1 file &&
119 git add file &&
120 test_tick &&
121 git commit -m "commit 1" &&
122 git tag c1 &&
123 c1=$(git rev-parse HEAD) &&
124 git reset --hard "$c0" &&
125 cp file.5 file &&
126 git add file &&
127 test_tick &&
128 git commit -m "commit 2" &&
129 git tag c2 &&
130 c2=$(git rev-parse HEAD) &&
131 git reset --hard "$c0" &&
132 cp file.9 file &&
133 git add file &&
134 test_tick &&
135 git commit -m "commit 3" &&
136 git tag c3 &&
137 c3=$(git rev-parse HEAD)
138 git reset --hard "$c0" &&
139 create_merge_msgs
142 test_debug 'git log --graph --decorate --oneline --all'
144 test_expect_success 'test option parsing' '
145 test_must_fail git merge -$ c1 &&
146 test_must_fail git merge --no-such c1 &&
147 test_must_fail git merge -s foobar c1 &&
148 test_must_fail git merge -s=foobar c1 &&
149 test_must_fail git merge -m &&
150 test_must_fail git merge
153 test_expect_success 'merge -h with invalid index' '
154 mkdir broken &&
156 cd broken &&
157 git init &&
158 >.git/index &&
159 test_expect_code 129 git merge -h 2>usage
160 ) &&
161 grep "[Uu]sage: git merge" broken/usage
164 test_expect_success 'reject non-strategy with a git-merge-foo name' '
165 test_must_fail git merge -s index c1
168 test_expect_success 'merge c0 with c1' '
169 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
171 git reset --hard c0 &&
172 git merge c1 &&
173 verify_merge file result.1 &&
174 verify_head "$c1" &&
176 git reflog -1 >reflog.actual &&
177 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
178 test_cmp reflog.expected reflog.fuzzy
181 test_debug 'git log --graph --decorate --oneline --all'
183 test_expect_success 'merge c0 with c1 with --ff-only' '
184 git reset --hard c0 &&
185 git merge --ff-only c1 &&
186 git merge --ff-only HEAD c0 c1 &&
187 verify_merge file result.1 &&
188 verify_head "$c1"
191 test_debug 'git log --graph --decorate --oneline --all'
193 test_expect_success 'merge from unborn branch' '
194 git checkout -f master &&
195 test_might_fail git branch -D kid &&
197 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
199 git checkout --orphan kid &&
200 test_when_finished "git checkout -f master" &&
201 git rm -fr . &&
202 test_tick &&
203 git merge --ff-only c1 &&
204 verify_merge file result.1 &&
205 verify_head "$c1" &&
207 git reflog -1 >reflog.actual &&
208 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
209 test_cmp reflog.expected reflog.fuzzy
212 test_debug 'git log --graph --decorate --oneline --all'
214 test_expect_success 'merge c1 with c2' '
215 git reset --hard c1 &&
216 test_tick &&
217 git merge c2 &&
218 verify_merge file result.1-5 msg.1-5 &&
219 verify_parents $c1 $c2
222 test_debug 'git log --graph --decorate --oneline --all'
224 test_expect_success 'merge c1 with c2 and c3' '
225 git reset --hard c1 &&
226 test_tick &&
227 git merge c2 c3 &&
228 verify_merge file result.1-5-9 msg.1-5-9 &&
229 verify_parents $c1 $c2 $c3
232 test_debug 'git log --graph --decorate --oneline --all'
234 test_expect_success 'merges with --ff-only' '
235 git reset --hard c1 &&
236 test_tick &&
237 test_must_fail git merge --ff-only c2 &&
238 test_must_fail git merge --ff-only c3 &&
239 test_must_fail git merge --ff-only c2 c3 &&
240 git reset --hard c0 &&
241 git merge c3 &&
242 verify_head $c3
245 test_expect_success 'merges with merge.ff=only' '
246 git reset --hard c1 &&
247 test_tick &&
248 test_when_finished "git config --unset merge.ff" &&
249 git config merge.ff only &&
250 test_must_fail git merge c2 &&
251 test_must_fail git merge c3 &&
252 test_must_fail git merge c2 c3 &&
253 git reset --hard c0 &&
254 git merge c3 &&
255 verify_head $c3
258 test_expect_success 'merge c0 with c1 (no-commit)' '
259 git reset --hard c0 &&
260 git merge --no-commit c1 &&
261 verify_merge file result.1 &&
262 verify_head $c1
265 test_debug 'git log --graph --decorate --oneline --all'
267 test_expect_success 'merge c1 with c2 (no-commit)' '
268 git reset --hard c1 &&
269 git merge --no-commit c2 &&
270 verify_merge file result.1-5 &&
271 verify_head $c1 &&
272 verify_mergeheads $c2
275 test_debug 'git log --graph --decorate --oneline --all'
277 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
278 git reset --hard c1 &&
279 git merge --no-commit c2 c3 &&
280 verify_merge file result.1-5-9 &&
281 verify_head $c1 &&
282 verify_mergeheads $c2 $c3
285 test_debug 'git log --graph --decorate --oneline --all'
287 test_expect_success 'merge c0 with c1 (squash)' '
288 git reset --hard c0 &&
289 git merge --squash c1 &&
290 verify_merge file result.1 &&
291 verify_head $c0 &&
292 verify_no_mergehead &&
293 test_cmp squash.1 .git/SQUASH_MSG
296 test_debug 'git log --graph --decorate --oneline --all'
298 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
299 git reset --hard c0 &&
300 git merge --squash --ff-only c1 &&
301 verify_merge file result.1 &&
302 verify_head $c0 &&
303 verify_no_mergehead &&
304 test_cmp squash.1 .git/SQUASH_MSG
307 test_debug 'git log --graph --decorate --oneline --all'
309 test_expect_success 'merge c1 with c2 (squash)' '
310 git reset --hard c1 &&
311 git merge --squash c2 &&
312 verify_merge file result.1-5 &&
313 verify_head $c1 &&
314 verify_no_mergehead &&
315 test_cmp squash.1-5 .git/SQUASH_MSG
318 test_debug 'git log --graph --decorate --oneline --all'
320 test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
321 git reset --hard c1 &&
322 test_must_fail git merge --squash --ff-only c2
325 test_debug 'git log --graph --decorate --oneline --all'
327 test_expect_success 'merge c1 with c2 and c3 (squash)' '
328 git reset --hard c1 &&
329 git merge --squash c2 c3 &&
330 verify_merge file result.1-5-9 &&
331 verify_head $c1 &&
332 verify_no_mergehead &&
333 test_cmp squash.1-5-9 .git/SQUASH_MSG
336 test_debug 'git log --graph --decorate --oneline --all'
338 test_expect_success 'merge c1 with c2 (no-commit in config)' '
339 git reset --hard c1 &&
340 git config branch.master.mergeoptions "--no-commit" &&
341 git merge c2 &&
342 verify_merge file result.1-5 &&
343 verify_head $c1 &&
344 verify_mergeheads $c2
347 test_debug 'git log --graph --decorate --oneline --all'
349 test_expect_success 'merge c1 with c2 (log in config)' '
350 git config branch.master.mergeoptions "" &&
351 git reset --hard c1 &&
352 git merge --log c2 &&
353 git show -s --pretty=tformat:%s%n%b >expect &&
355 git config branch.master.mergeoptions --log &&
356 git reset --hard c1 &&
357 git merge c2 &&
358 git show -s --pretty=tformat:%s%n%b >actual &&
360 test_cmp expect actual
363 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
364 test_when_finished "git config --remove-section branch.master" &&
365 test_when_finished "git config --remove-section merge" &&
366 test_might_fail git config --remove-section branch.master &&
367 test_might_fail git config --remove-section merge &&
369 git reset --hard c1 &&
370 git merge c2 &&
371 git show -s --pretty=tformat:%s%n%b >expect &&
373 git config branch.master.mergeoptions "--no-log" &&
374 git config merge.log true &&
375 git reset --hard c1 &&
376 git merge c2 &&
377 git show -s --pretty=tformat:%s%n%b >actual &&
379 test_cmp expect actual
382 test_expect_success 'merge c1 with c2 (squash in config)' '
383 git reset --hard c1 &&
384 git config branch.master.mergeoptions "--squash" &&
385 git merge c2 &&
386 verify_merge file result.1-5 &&
387 verify_head $c1 &&
388 verify_no_mergehead &&
389 test_cmp squash.1-5 .git/SQUASH_MSG
392 test_debug 'git log --graph --decorate --oneline --all'
394 test_expect_success 'override config option -n with --summary' '
395 git reset --hard c1 &&
396 git config branch.master.mergeoptions "-n" &&
397 test_tick &&
398 git merge --summary c2 >diffstat.txt &&
399 verify_merge file result.1-5 msg.1-5 &&
400 verify_parents $c1 $c2 &&
401 if ! grep "^ file | *2 +-$" diffstat.txt
402 then
403 echo "[OOPS] diffstat was not generated with --summary"
404 false
408 test_expect_success 'override config option -n with --stat' '
409 git reset --hard c1 &&
410 git config branch.master.mergeoptions "-n" &&
411 test_tick &&
412 git merge --stat c2 >diffstat.txt &&
413 verify_merge file result.1-5 msg.1-5 &&
414 verify_parents $c1 $c2 &&
415 if ! grep "^ file | *2 +-$" diffstat.txt
416 then
417 echo "[OOPS] diffstat was not generated with --stat"
418 false
422 test_debug 'git log --graph --decorate --oneline --all'
424 test_expect_success 'override config option --stat' '
425 git reset --hard c1 &&
426 git config branch.master.mergeoptions "--stat" &&
427 test_tick &&
428 git merge -n c2 >diffstat.txt &&
429 verify_merge file result.1-5 msg.1-5 &&
430 verify_parents $c1 $c2 &&
431 if grep "^ file | *2 +-$" diffstat.txt
432 then
433 echo "[OOPS] diffstat was generated"
434 false
438 test_debug 'git log --graph --decorate --oneline --all'
440 test_expect_success 'merge c1 with c2 (override --no-commit)' '
441 git reset --hard c1 &&
442 git config branch.master.mergeoptions "--no-commit" &&
443 test_tick &&
444 git merge --commit c2 &&
445 verify_merge file result.1-5 msg.1-5 &&
446 verify_parents $c1 $c2
449 test_debug 'git log --graph --decorate --oneline --all'
451 test_expect_success 'merge c1 with c2 (override --squash)' '
452 git reset --hard c1 &&
453 git config branch.master.mergeoptions "--squash" &&
454 test_tick &&
455 git merge --no-squash c2 &&
456 verify_merge file result.1-5 msg.1-5 &&
457 verify_parents $c1 $c2
460 test_debug 'git log --graph --decorate --oneline --all'
462 test_expect_success 'merge c0 with c1 (no-ff)' '
463 git reset --hard c0 &&
464 git config branch.master.mergeoptions "" &&
465 test_tick &&
466 git merge --no-ff c1 &&
467 verify_merge file result.1 &&
468 verify_parents $c0 $c1
471 test_debug 'git log --graph --decorate --oneline --all'
473 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
474 git reset --hard c0 &&
475 git config merge.ff false &&
476 test_tick &&
477 git merge c1 &&
478 git config --remove-section merge &&
479 verify_merge file result.1 &&
480 verify_parents $c0 $c1
482 test_debug 'git log --graph --decorate --oneline --all'
484 test_expect_success 'combine branch.master.mergeoptions with merge.ff' '
485 git reset --hard c0 &&
486 git config branch.master.mergeoptions --ff &&
487 git config merge.ff false &&
488 test_tick &&
489 git merge c1 &&
490 git config --remove-section "branch.master" &&
491 git config --remove-section "merge" &&
492 verify_merge file result.1 &&
493 verify_parents "$c0"
496 test_expect_success 'tolerate unknown values for merge.ff' '
497 git reset --hard c0 &&
498 git config merge.ff something-new &&
499 test_tick &&
500 git merge c1 2>message &&
501 git config --remove-section "merge" &&
502 verify_head "$c1" &&
503 test_cmp empty message
506 test_expect_success 'combining --squash and --no-ff is refused' '
507 git reset --hard c0 &&
508 test_must_fail git merge --squash --no-ff c1 &&
509 test_must_fail git merge --no-ff --squash c1
512 test_expect_success 'combining --ff-only and --no-ff is refused' '
513 test_must_fail git merge --ff-only --no-ff c1 &&
514 test_must_fail git merge --no-ff --ff-only c1
517 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
518 git reset --hard c0 &&
519 git config branch.master.mergeoptions "--no-ff" &&
520 git merge --ff c1 &&
521 verify_merge file result.1 &&
522 verify_head $c1
525 test_expect_success 'merge log message' '
526 git reset --hard c0 &&
527 git merge --no-log c2 &&
528 git show -s --pretty=format:%b HEAD >msg.act &&
529 test_cmp msg.nolog msg.act &&
531 git merge --log c3 &&
532 git show -s --pretty=format:%b HEAD >msg.act &&
533 test_cmp msg.log msg.act &&
535 git reset --hard HEAD^ &&
536 git config merge.log yes &&
537 git merge c3 &&
538 git show -s --pretty=format:%b HEAD >msg.act &&
539 test_cmp msg.log msg.act
542 test_debug 'git log --graph --decorate --oneline --all'
544 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
545 git reset --hard c1 &&
546 git config branch.master.mergeoptions "" &&
547 test_tick &&
548 git merge c0 c2 c0 c1 &&
549 verify_merge file result.1-5 &&
550 verify_parents $c1 $c2
553 test_debug 'git log --graph --decorate --oneline --all'
555 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
556 git reset --hard c1 &&
557 git config branch.master.mergeoptions "" &&
558 test_tick &&
559 git merge c0 c2 c0 c1 &&
560 verify_merge file result.1-5 &&
561 verify_parents $c1 $c2
564 test_debug 'git log --graph --decorate --oneline --all'
566 test_expect_success 'merge c1 with c1 and c2' '
567 git reset --hard c1 &&
568 git config branch.master.mergeoptions "" &&
569 test_tick &&
570 git merge c1 c2 &&
571 verify_merge file result.1-5 &&
572 verify_parents $c1 $c2
575 test_debug 'git log --graph --decorate --oneline --all'
577 test_expect_success 'merge fast-forward in a dirty tree' '
578 git reset --hard c0 &&
579 mv file file1 &&
580 cat file1 >file &&
581 rm -f file1 &&
582 git merge c2
585 test_debug 'git log --graph --decorate --oneline --all'
587 test_expect_success 'in-index merge' '
588 git reset --hard c0 &&
589 git merge --no-ff -s resolve c1 >out &&
590 test_i18ngrep "Wonderful." out &&
591 verify_parents $c0 $c1
594 test_debug 'git log --graph --decorate --oneline --all'
596 test_expect_success 'refresh the index before merging' '
597 git reset --hard c1 &&
598 cp file file.n && mv -f file.n file &&
599 git merge c3
602 cat >expected.branch <<\EOF
603 Merge branch 'c5-branch' (early part)
605 cat >expected.tag <<\EOF
606 Merge commit 'c5~1'
609 test_expect_success 'merge early part of c2' '
610 git reset --hard c3 &&
611 echo c4 >c4.c &&
612 git add c4.c &&
613 git commit -m c4 &&
614 git tag c4 &&
615 echo c5 >c5.c &&
616 git add c5.c &&
617 git commit -m c5 &&
618 git tag c5 &&
619 git reset --hard c3 &&
620 echo c6 >c6.c &&
621 git add c6.c &&
622 git commit -m c6 &&
623 git tag c6 &&
624 git branch -f c5-branch c5 &&
625 git merge c5-branch~1 &&
626 git show -s --pretty=format:%s HEAD >actual.branch &&
627 git reset --keep HEAD^ &&
628 git merge c5~1 &&
629 git show -s --pretty=format:%s HEAD >actual.tag &&
630 test_cmp expected.branch actual.branch &&
631 test_cmp expected.tag actual.tag
634 test_debug 'git log --graph --decorate --oneline --all'
636 test_expect_success 'merge --no-ff --no-commit && commit' '
637 git reset --hard c0 &&
638 git merge --no-ff --no-commit c1 &&
639 EDITOR=: git commit &&
640 verify_parents $c0 $c1
643 test_debug 'git log --graph --decorate --oneline --all'
645 test_expect_success 'amending no-ff merge commit' '
646 EDITOR=: git commit --amend &&
647 verify_parents $c0 $c1
650 test_debug 'git log --graph --decorate --oneline --all'
652 cat >editor <<\EOF
653 #!/bin/sh
654 # Add a new message string that was not in the template
656 echo "Merge work done on the side branch c1"
657 echo
658 cat <"$1"
659 ) >"$1.tmp" && mv "$1.tmp" "$1"
660 # strip comments and blank lines from end of message
661 sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
663 chmod 755 editor
665 test_expect_success 'merge --no-ff --edit' '
666 git reset --hard c0 &&
667 EDITOR=./editor git merge --no-ff --edit c1 &&
668 verify_parents $c0 $c1 &&
669 git cat-file commit HEAD >raw &&
670 grep "work done on the side branch" raw &&
671 sed "1,/^$/d" >actual raw &&
672 test_cmp actual expected
675 test_expect_success GPG 'merge --ff-only tag' '
676 git reset --hard c0 &&
677 git commit --allow-empty -m "A newer commit" &&
678 git tag -s -m "A newer commit" signed &&
679 git reset --hard c0 &&
681 git merge --ff-only signed &&
682 git rev-parse signed^0 >expect &&
683 git rev-parse HEAD >actual &&
684 test_cmp actual expect
687 test_expect_success GPG 'merge --no-edit tag should skip editor' '
688 git reset --hard c0 &&
689 git commit --allow-empty -m "A newer commit" &&
690 git tag -f -s -m "A newer commit" signed &&
691 git reset --hard c0 &&
693 EDITOR=false git merge --no-edit signed &&
694 git rev-parse signed^0 >expect &&
695 git rev-parse HEAD^2 >actual &&
696 test_cmp actual expect
699 test_done