1#!/bin/sh
2#
3# Copyright (c) 2007 Lars Hjemli
4#
5
6test_description='git merge
7
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
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
31printf '%s\n' 1 2 3 4 5 6 7 8 9 >file
32printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >file.1
33printf '%s\n' 1 2 3 4 '5 X' 6 7 8 9 >file.5
34printf '%s\n' 1 2 3 4 5 6 7 8 '9 X' >file.9
35printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >result.1
36printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
37printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
38>empty
39
40create_merge_msgs () {
41 echo "Merge tag 'c2'" >msg.1-5 &&
42 echo "Merge tags 'c2' and '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 "* tag 'c3':" &&
61 echo " commit 3" &&
62 echo
63 } >msg.log
64}
65
66verify_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
77verify_head () {
78 echo "$1" >head.expected &&
79 git rev-parse HEAD >head.actual &&
80 test_cmp head.expected head.actual
81}
82
83verify_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_must_fail git rev-parse --verify "HEAD^$i" &&
94 test_cmp parents.expected parents.actual
95}
96
97verify_mergeheads () {
98 printf '%s\n' "$@" >mergehead.expected &&
99 while read sha1 rest
100 do
101 git rev-parse $sha1
102 done <.git/MERGE_HEAD >mergehead.actual &&
103 test_cmp mergehead.expected mergehead.actual
104}
105
106verify_no_mergehead () {
107 ! test -e .git/MERGE_HEAD
108}
109
110test_expect_success 'setup' '
111 git add file &&
112 test_tick &&
113 git commit -m "commit 0" &&
114 git tag c0 &&
115 c0=$(git rev-parse HEAD) &&
116 cp file.1 file &&
117 git add file &&
118 test_tick &&
119 git commit -m "commit 1" &&
120 git tag c1 &&
121 c1=$(git rev-parse HEAD) &&
122 git reset --hard "$c0" &&
123 cp file.5 file &&
124 git add file &&
125 test_tick &&
126 git commit -m "commit 2" &&
127 git tag c2 &&
128 c2=$(git rev-parse HEAD) &&
129 git reset --hard "$c0" &&
130 cp file.9 file &&
131 git add file &&
132 test_tick &&
133 git commit -m "commit 3" &&
134 git tag c3 &&
135 c3=$(git rev-parse HEAD)
136 git reset --hard "$c0" &&
137 create_merge_msgs
138'
139
140test_debug 'git log --graph --decorate --oneline --all'
141
142test_expect_success 'test option parsing' '
143 test_must_fail git merge -$ c1 &&
144 test_must_fail git merge --no-such c1 &&
145 test_must_fail git merge -s foobar c1 &&
146 test_must_fail git merge -s=foobar c1 &&
147 test_must_fail git merge -m &&
148 test_must_fail git merge
149'
150
151test_expect_success 'merge -h with invalid index' '
152 mkdir broken &&
153 (
154 cd broken &&
155 git init &&
156 >.git/index &&
157 test_expect_code 129 git merge -h 2>usage
158 ) &&
159 grep "[Uu]sage: git merge" broken/usage
160'
161
162test_expect_success 'reject non-strategy with a git-merge-foo name' '
163 test_must_fail git merge -s index c1
164'
165
166test_expect_success 'merge c0 with c1' '
167 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
168
169 git reset --hard c0 &&
170 git merge c1 &&
171 verify_merge file result.1 &&
172 verify_head "$c1" &&
173
174 git reflog -1 >reflog.actual &&
175 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
176 test_cmp reflog.expected reflog.fuzzy
177'
178
179test_debug 'git log --graph --decorate --oneline --all'
180
181test_expect_success 'merge c0 with c1 with --ff-only' '
182 git reset --hard c0 &&
183 git merge --ff-only c1 &&
184 git merge --ff-only HEAD c0 c1 &&
185 verify_merge file result.1 &&
186 verify_head "$c1"
187'
188
189test_debug 'git log --graph --decorate --oneline --all'
190
191test_expect_success 'merge from unborn branch' '
192 git checkout -f master &&
193 test_might_fail git branch -D kid &&
194
195 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
196
197 git checkout --orphan kid &&
198 test_when_finished "git checkout -f master" &&
199 git rm -fr . &&
200 test_tick &&
201 git merge --ff-only c1 &&
202 verify_merge file result.1 &&
203 verify_head "$c1" &&
204
205 git reflog -1 >reflog.actual &&
206 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
207 test_cmp reflog.expected reflog.fuzzy
208'
209
210test_debug 'git log --graph --decorate --oneline --all'
211
212test_expect_success 'merge c1 with c2' '
213 git reset --hard c1 &&
214 test_tick &&
215 git merge c2 &&
216 verify_merge file result.1-5 msg.1-5 &&
217 verify_parents $c1 $c2
218'
219
220test_debug 'git log --graph --decorate --oneline --all'
221
222test_expect_success 'merge c1 with c2 and c3' '
223 git reset --hard c1 &&
224 test_tick &&
225 git merge c2 c3 &&
226 verify_merge file result.1-5-9 msg.1-5-9 &&
227 verify_parents $c1 $c2 $c3
228'
229
230test_debug 'git log --graph --decorate --oneline --all'
231
232test_expect_success 'merges with --ff-only' '
233 git reset --hard c1 &&
234 test_tick &&
235 test_must_fail git merge --ff-only c2 &&
236 test_must_fail git merge --ff-only c3 &&
237 test_must_fail git merge --ff-only c2 c3 &&
238 git reset --hard c0 &&
239 git merge c3 &&
240 verify_head $c3
241'
242
243test_expect_success 'merges with merge.ff=only' '
244 git reset --hard c1 &&
245 test_tick &&
246 test_when_finished "git config --unset merge.ff" &&
247 git config merge.ff only &&
248 test_must_fail git merge c2 &&
249 test_must_fail git merge c3 &&
250 test_must_fail git merge c2 c3 &&
251 git reset --hard c0 &&
252 git merge c3 &&
253 verify_head $c3
254'
255
256test_expect_success 'merge c0 with c1 (no-commit)' '
257 git reset --hard c0 &&
258 git merge --no-commit c1 &&
259 verify_merge file result.1 &&
260 verify_head $c1
261'
262
263test_debug 'git log --graph --decorate --oneline --all'
264
265test_expect_success 'merge c1 with c2 (no-commit)' '
266 git reset --hard c1 &&
267 git merge --no-commit c2 &&
268 verify_merge file result.1-5 &&
269 verify_head $c1 &&
270 verify_mergeheads $c2
271'
272
273test_debug 'git log --graph --decorate --oneline --all'
274
275test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
276 git reset --hard c1 &&
277 git merge --no-commit c2 c3 &&
278 verify_merge file result.1-5-9 &&
279 verify_head $c1 &&
280 verify_mergeheads $c2 $c3
281'
282
283test_debug 'git log --graph --decorate --oneline --all'
284
285test_expect_success 'merge c0 with c1 (squash)' '
286 git reset --hard c0 &&
287 git merge --squash c1 &&
288 verify_merge file result.1 &&
289 verify_head $c0 &&
290 verify_no_mergehead &&
291 test_cmp squash.1 .git/SQUASH_MSG
292'
293
294test_debug 'git log --graph --decorate --oneline --all'
295
296test_expect_success 'merge c0 with c1 (squash, ff-only)' '
297 git reset --hard c0 &&
298 git merge --squash --ff-only c1 &&
299 verify_merge file result.1 &&
300 verify_head $c0 &&
301 verify_no_mergehead &&
302 test_cmp squash.1 .git/SQUASH_MSG
303'
304
305test_debug 'git log --graph --decorate --oneline --all'
306
307test_expect_success 'merge c1 with c2 (squash)' '
308 git reset --hard c1 &&
309 git merge --squash c2 &&
310 verify_merge file result.1-5 &&
311 verify_head $c1 &&
312 verify_no_mergehead &&
313 test_cmp squash.1-5 .git/SQUASH_MSG
314'
315
316test_debug 'git log --graph --decorate --oneline --all'
317
318test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
319 git reset --hard c1 &&
320 test_must_fail git merge --squash --ff-only c2
321'
322
323test_debug 'git log --graph --decorate --oneline --all'
324
325test_expect_success 'merge c1 with c2 and c3 (squash)' '
326 git reset --hard c1 &&
327 git merge --squash c2 c3 &&
328 verify_merge file result.1-5-9 &&
329 verify_head $c1 &&
330 verify_no_mergehead &&
331 test_cmp squash.1-5-9 .git/SQUASH_MSG
332'
333
334test_debug 'git log --graph --decorate --oneline --all'
335
336test_expect_success 'merge c1 with c2 (no-commit in config)' '
337 git reset --hard c1 &&
338 git config branch.master.mergeoptions "--no-commit" &&
339 git merge c2 &&
340 verify_merge file result.1-5 &&
341 verify_head $c1 &&
342 verify_mergeheads $c2
343'
344
345test_debug 'git log --graph --decorate --oneline --all'
346
347test_expect_success 'merge c1 with c2 (log in config)' '
348 git config branch.master.mergeoptions "" &&
349 git reset --hard c1 &&
350 git merge --log c2 &&
351 git show -s --pretty=tformat:%s%n%b >expect &&
352
353 git config branch.master.mergeoptions --log &&
354 git reset --hard c1 &&
355 git merge c2 &&
356 git show -s --pretty=tformat:%s%n%b >actual &&
357
358 test_cmp expect actual
359'
360
361test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
362 test_when_finished "git config --remove-section branch.master" &&
363 test_when_finished "git config --remove-section merge" &&
364 test_might_fail git config --remove-section branch.master &&
365 test_might_fail git config --remove-section merge &&
366
367 git reset --hard c1 &&
368 git merge c2 &&
369 git show -s --pretty=tformat:%s%n%b >expect &&
370
371 git config branch.master.mergeoptions "--no-log" &&
372 git config merge.log true &&
373 git reset --hard c1 &&
374 git merge c2 &&
375 git show -s --pretty=tformat:%s%n%b >actual &&
376
377 test_cmp expect actual
378'
379
380test_expect_success 'merge c1 with c2 (squash in config)' '
381 git reset --hard c1 &&
382 git config branch.master.mergeoptions "--squash" &&
383 git merge c2 &&
384 verify_merge file result.1-5 &&
385 verify_head $c1 &&
386 verify_no_mergehead &&
387 test_cmp squash.1-5 .git/SQUASH_MSG
388'
389
390test_debug 'git log --graph --decorate --oneline --all'
391
392test_expect_success 'override config option -n with --summary' '
393 git reset --hard c1 &&
394 git config branch.master.mergeoptions "-n" &&
395 test_tick &&
396 git merge --summary c2 >diffstat.txt &&
397 verify_merge file result.1-5 msg.1-5 &&
398 verify_parents $c1 $c2 &&
399 if ! grep "^ file | *2 +-$" diffstat.txt
400 then
401 echo "[OOPS] diffstat was not generated with --summary"
402 false
403 fi
404'
405
406test_expect_success 'override config option -n with --stat' '
407 git reset --hard c1 &&
408 git config branch.master.mergeoptions "-n" &&
409 test_tick &&
410 git merge --stat c2 >diffstat.txt &&
411 verify_merge file result.1-5 msg.1-5 &&
412 verify_parents $c1 $c2 &&
413 if ! grep "^ file | *2 +-$" diffstat.txt
414 then
415 echo "[OOPS] diffstat was not generated with --stat"
416 false
417 fi
418'
419
420test_debug 'git log --graph --decorate --oneline --all'
421
422test_expect_success 'override config option --stat' '
423 git reset --hard c1 &&
424 git config branch.master.mergeoptions "--stat" &&
425 test_tick &&
426 git merge -n c2 >diffstat.txt &&
427 verify_merge file result.1-5 msg.1-5 &&
428 verify_parents $c1 $c2 &&
429 if grep "^ file | *2 +-$" diffstat.txt
430 then
431 echo "[OOPS] diffstat was generated"
432 false
433 fi
434'
435
436test_debug 'git log --graph --decorate --oneline --all'
437
438test_expect_success 'merge c1 with c2 (override --no-commit)' '
439 git reset --hard c1 &&
440 git config branch.master.mergeoptions "--no-commit" &&
441 test_tick &&
442 git merge --commit c2 &&
443 verify_merge file result.1-5 msg.1-5 &&
444 verify_parents $c1 $c2
445'
446
447test_debug 'git log --graph --decorate --oneline --all'
448
449test_expect_success 'merge c1 with c2 (override --squash)' '
450 git reset --hard c1 &&
451 git config branch.master.mergeoptions "--squash" &&
452 test_tick &&
453 git merge --no-squash c2 &&
454 verify_merge file result.1-5 msg.1-5 &&
455 verify_parents $c1 $c2
456'
457
458test_debug 'git log --graph --decorate --oneline --all'
459
460test_expect_success 'merge c0 with c1 (no-ff)' '
461 git reset --hard c0 &&
462 git config branch.master.mergeoptions "" &&
463 test_tick &&
464 git merge --no-ff c1 &&
465 verify_merge file result.1 &&
466 verify_parents $c0 $c1
467'
468
469test_debug 'git log --graph --decorate --oneline --all'
470
471test_expect_success 'merge c0 with c1 (merge.ff=false)' '
472 git reset --hard c0 &&
473 git config merge.ff false &&
474 test_tick &&
475 git merge c1 &&
476 git config --remove-section merge &&
477 verify_merge file result.1 &&
478 verify_parents $c0 $c1
479'
480test_debug 'git log --graph --decorate --oneline --all'
481
482test_expect_success 'combine branch.master.mergeoptions with merge.ff' '
483 git reset --hard c0 &&
484 git config branch.master.mergeoptions --ff &&
485 git config merge.ff false &&
486 test_tick &&
487 git merge c1 &&
488 git config --remove-section "branch.master" &&
489 git config --remove-section "merge" &&
490 verify_merge file result.1 &&
491 verify_parents "$c0"
492'
493
494test_expect_success 'tolerate unknown values for merge.ff' '
495 git reset --hard c0 &&
496 git config merge.ff something-new &&
497 test_tick &&
498 git merge c1 2>message &&
499 git config --remove-section "merge" &&
500 verify_head "$c1" &&
501 test_cmp empty message
502'
503
504test_expect_success 'combining --squash and --no-ff is refused' '
505 git reset --hard c0 &&
506 test_must_fail git merge --squash --no-ff c1 &&
507 test_must_fail git merge --no-ff --squash c1
508'
509
510test_expect_success 'combining --ff-only and --no-ff is refused' '
511 test_must_fail git merge --ff-only --no-ff c1 &&
512 test_must_fail git merge --no-ff --ff-only c1
513'
514
515test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
516 git reset --hard c0 &&
517 git config branch.master.mergeoptions "--no-ff" &&
518 git merge --ff c1 &&
519 verify_merge file result.1 &&
520 verify_head $c1
521'
522
523test_expect_success 'merge log message' '
524 git reset --hard c0 &&
525 git merge --no-log c2 &&
526 git show -s --pretty=format:%b HEAD >msg.act &&
527 test_cmp msg.nolog msg.act &&
528
529 git merge --log c3 &&
530 git show -s --pretty=format:%b HEAD >msg.act &&
531 test_cmp msg.log msg.act &&
532
533 git reset --hard HEAD^ &&
534 git config merge.log yes &&
535 git merge c3 &&
536 git show -s --pretty=format:%b HEAD >msg.act &&
537 test_cmp msg.log msg.act
538'
539
540test_debug 'git log --graph --decorate --oneline --all'
541
542test_expect_success 'merge c1 with c0, c2, c0, and c1' '
543 git reset --hard c1 &&
544 git config branch.master.mergeoptions "" &&
545 test_tick &&
546 git merge c0 c2 c0 c1 &&
547 verify_merge file result.1-5 &&
548 verify_parents $c1 $c2
549'
550
551test_debug 'git log --graph --decorate --oneline --all'
552
553test_expect_success 'merge c1 with c0, c2, c0, and c1' '
554 git reset --hard c1 &&
555 git config branch.master.mergeoptions "" &&
556 test_tick &&
557 git merge c0 c2 c0 c1 &&
558 verify_merge file result.1-5 &&
559 verify_parents $c1 $c2
560'
561
562test_debug 'git log --graph --decorate --oneline --all'
563
564test_expect_success 'merge c1 with c1 and c2' '
565 git reset --hard c1 &&
566 git config branch.master.mergeoptions "" &&
567 test_tick &&
568 git merge c1 c2 &&
569 verify_merge file result.1-5 &&
570 verify_parents $c1 $c2
571'
572
573test_debug 'git log --graph --decorate --oneline --all'
574
575test_expect_success 'merge fast-forward in a dirty tree' '
576 git reset --hard c0 &&
577 mv file file1 &&
578 cat file1 >file &&
579 rm -f file1 &&
580 git merge c2
581'
582
583test_debug 'git log --graph --decorate --oneline --all'
584
585test_expect_success 'in-index merge' '
586 git reset --hard c0 &&
587 git merge --no-ff -s resolve c1 >out &&
588 test_i18ngrep "Wonderful." out &&
589 verify_parents $c0 $c1
590'
591
592test_debug 'git log --graph --decorate --oneline --all'
593
594test_expect_success 'refresh the index before merging' '
595 git reset --hard c1 &&
596 cp file file.n && mv -f file.n file &&
597 git merge c3
598'
599
600cat >expected.branch <<\EOF
601Merge branch 'c5-branch' (early part)
602EOF
603cat >expected.tag <<\EOF
604Merge commit 'c5~1'
605EOF
606
607test_expect_success 'merge early part of c2' '
608 git reset --hard c3 &&
609 echo c4 >c4.c &&
610 git add c4.c &&
611 git commit -m c4 &&
612 git tag c4 &&
613 echo c5 >c5.c &&
614 git add c5.c &&
615 git commit -m c5 &&
616 git tag c5 &&
617 git reset --hard c3 &&
618 echo c6 >c6.c &&
619 git add c6.c &&
620 git commit -m c6 &&
621 git tag c6 &&
622 git branch -f c5-branch c5 &&
623 git merge c5-branch~1 &&
624 git show -s --pretty=format:%s HEAD >actual.branch &&
625 git reset --keep HEAD^ &&
626 git merge c5~1 &&
627 git show -s --pretty=format:%s HEAD >actual.tag &&
628 test_cmp expected.branch actual.branch &&
629 test_cmp expected.tag actual.tag
630'
631
632test_debug 'git log --graph --decorate --oneline --all'
633
634test_expect_success 'merge --no-ff --no-commit && commit' '
635 git reset --hard c0 &&
636 git merge --no-ff --no-commit c1 &&
637 EDITOR=: git commit &&
638 verify_parents $c0 $c1
639'
640
641test_debug 'git log --graph --decorate --oneline --all'
642
643test_expect_success 'amending no-ff merge commit' '
644 EDITOR=: git commit --amend &&
645 verify_parents $c0 $c1
646'
647
648test_debug 'git log --graph --decorate --oneline --all'
649
650cat >editor <<\EOF
651#!/bin/sh
652# Add a new message string that was not in the template
653(
654 echo "Merge work done on the side branch c1"
655 echo
656 cat <"$1"
657) >"$1.tmp" && mv "$1.tmp" "$1"
658# strip comments and blank lines from end of message
659sed -e '/^#/d' < "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' > expected
660EOF
661chmod 755 editor
662
663test_expect_success 'merge --no-ff --edit' '
664 git reset --hard c0 &&
665 EDITOR=./editor git merge --no-ff --edit c1 &&
666 verify_parents $c0 $c1 &&
667 git cat-file commit HEAD >raw &&
668 grep "work done on the side branch" raw &&
669 sed "1,/^$/d" >actual raw &&
670 test_cmp actual expected
671'
672
673test_done