1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Lars Hjemli
4 #
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
27 '
29 . ./test-lib.sh
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 &&
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 } &&
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 } &&
77 verify_head() {
78 echo "$1" >head.expected &&
79 git rev-parse HEAD >head.actual &&
80 test_cmp head.expected head.actual
81 } &&
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 } &&
96 verify_mergeheads() {
97 printf "%s\n" "$@" >mergehead.expected &&
98 test_cmp mergehead.expected .git/MERGE_HEAD
99 } &&
101 verify_no_mergehead() {
102 ! test -e .git/MERGE_HEAD
103 }
104 '
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 '
136 test_debug 'git log --graph --decorate --oneline --all'
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 '
147 test_expect_success 'reject non-strategy with a git-merge-foo name' '
148 test_must_fail git merge -s index c1
149 '
151 test_expect_success 'merge c0 with c1' '
152 git reset --hard c0 &&
153 git merge c1 &&
154 verify_merge file result.1 &&
155 verify_head "$c1"
156 '
158 test_debug 'git log --graph --decorate --oneline --all'
160 test_expect_success 'merge c0 with c1 with --ff-only' '
161 git reset --hard c0 &&
162 git merge --ff-only c1 &&
163 git merge --ff-only HEAD c0 c1 &&
164 verify_merge file result.1 &&
165 verify_head "$c1"
166 '
168 test_debug 'git log --graph --decorate --oneline --all'
170 test_expect_success 'merge c1 with c2' '
171 git reset --hard c1 &&
172 test_tick &&
173 git merge c2 &&
174 verify_merge file result.1-5 msg.1-5 &&
175 verify_parents $c1 $c2
176 '
178 test_debug 'git log --graph --decorate --oneline --all'
180 test_expect_success 'merge c1 with c2 and c3' '
181 git reset --hard c1 &&
182 test_tick &&
183 git merge c2 c3 &&
184 verify_merge file result.1-5-9 msg.1-5-9 &&
185 verify_parents $c1 $c2 $c3
186 '
188 test_debug 'git log --graph --decorate --oneline --all'
190 test_expect_success 'failing merges with --ff-only' '
191 git reset --hard c1 &&
192 test_tick &&
193 test_must_fail git merge --ff-only c2 &&
194 test_must_fail git merge --ff-only c3 &&
195 test_must_fail git merge --ff-only c2 c3
196 '
198 test_expect_success 'merge c0 with c1 (no-commit)' '
199 git reset --hard c0 &&
200 git merge --no-commit c1 &&
201 verify_merge file result.1 &&
202 verify_head $c1
203 '
205 test_debug 'git log --graph --decorate --oneline --all'
207 test_expect_success 'merge c1 with c2 (no-commit)' '
208 git reset --hard c1 &&
209 git merge --no-commit c2 &&
210 verify_merge file result.1-5 &&
211 verify_head $c1 &&
212 verify_mergeheads $c2
213 '
215 test_debug 'git log --graph --decorate --oneline --all'
217 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
218 git reset --hard c1 &&
219 git merge --no-commit c2 c3 &&
220 verify_merge file result.1-5-9 &&
221 verify_head $c1 &&
222 verify_mergeheads $c2 $c3
223 '
225 test_debug 'git log --graph --decorate --oneline --all'
227 test_expect_success 'merge c0 with c1 (squash)' '
228 git reset --hard c0 &&
229 git merge --squash c1 &&
230 verify_merge file result.1 &&
231 verify_head $c0 &&
232 verify_no_mergehead &&
233 test_cmp squash.1 .git/SQUASH_MSG
234 '
236 test_debug 'git log --graph --decorate --oneline --all'
238 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
239 git reset --hard c0 &&
240 git merge --squash --ff-only c1 &&
241 verify_merge file result.1 &&
242 verify_head $c0 &&
243 verify_no_mergehead &&
244 test_cmp squash.1 .git/SQUASH_MSG
245 '
247 test_debug 'git log --graph --decorate --oneline --all'
249 test_expect_success 'merge c1 with c2 (squash)' '
250 git reset --hard c1 &&
251 git merge --squash c2 &&
252 verify_merge file result.1-5 &&
253 verify_head $c1 &&
254 verify_no_mergehead &&
255 test_cmp squash.1-5 .git/SQUASH_MSG
256 '
258 test_debug 'git log --graph --decorate --oneline --all'
260 test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
261 git reset --hard c1 &&
262 test_must_fail git merge --squash --ff-only c2
263 '
265 test_debug 'git log --graph --decorate --oneline --all'
267 test_expect_success 'merge c1 with c2 and c3 (squash)' '
268 git reset --hard c1 &&
269 git merge --squash c2 c3 &&
270 verify_merge file result.1-5-9 &&
271 verify_head $c1 &&
272 verify_no_mergehead &&
273 test_cmp squash.1-5-9 .git/SQUASH_MSG
274 '
276 test_debug 'git log --graph --decorate --oneline --all'
278 test_expect_success 'merge c1 with c2 (no-commit in config)' '
279 git reset --hard c1 &&
280 git config branch.master.mergeoptions "--no-commit" &&
281 git merge c2 &&
282 verify_merge file result.1-5 &&
283 verify_head $c1 &&
284 verify_mergeheads $c2
285 '
287 test_debug 'git log --graph --decorate --oneline --all'
289 test_expect_success 'merge c1 with c2 (squash in config)' '
290 git reset --hard c1 &&
291 git config branch.master.mergeoptions "--squash" &&
292 git merge c2 &&
293 verify_merge file result.1-5 &&
294 verify_head $c1 &&
295 verify_no_mergehead &&
296 test_cmp squash.1-5 .git/SQUASH_MSG
297 '
299 test_debug 'git log --graph --decorate --oneline --all'
301 test_expect_success 'override config option -n with --summary' '
302 git reset --hard c1 &&
303 git config branch.master.mergeoptions "-n" &&
304 test_tick &&
305 git merge --summary c2 >diffstat.txt &&
306 verify_merge file result.1-5 msg.1-5 &&
307 verify_parents $c1 $c2 &&
308 if ! grep "^ file | *2 +-$" diffstat.txt
309 then
310 echo "[OOPS] diffstat was not generated with --summary"
311 false
312 fi
313 '
315 test_expect_success 'override config option -n with --stat' '
316 git reset --hard c1 &&
317 git config branch.master.mergeoptions "-n" &&
318 test_tick &&
319 git merge --stat c2 >diffstat.txt &&
320 verify_merge file result.1-5 msg.1-5 &&
321 verify_parents $c1 $c2 &&
322 if ! grep "^ file | *2 +-$" diffstat.txt
323 then
324 echo "[OOPS] diffstat was not generated with --stat"
325 false
326 fi
327 '
329 test_debug 'git log --graph --decorate --oneline --all'
331 test_expect_success 'override config option --stat' '
332 git reset --hard c1 &&
333 git config branch.master.mergeoptions "--stat" &&
334 test_tick &&
335 git merge -n c2 >diffstat.txt &&
336 verify_merge file result.1-5 msg.1-5 &&
337 verify_parents $c1 $c2 &&
338 if grep "^ file | *2 +-$" diffstat.txt
339 then
340 echo "[OOPS] diffstat was generated"
341 false
342 fi
343 '
345 test_debug 'git log --graph --decorate --oneline --all'
347 test_expect_success 'merge c1 with c2 (override --no-commit)' '
348 git reset --hard c1 &&
349 git config branch.master.mergeoptions "--no-commit" &&
350 test_tick &&
351 git merge --commit c2 &&
352 verify_merge file result.1-5 msg.1-5 &&
353 verify_parents $c1 $c2
354 '
356 test_debug 'git log --graph --decorate --oneline --all'
358 test_expect_success 'merge c1 with c2 (override --squash)' '
359 git reset --hard c1 &&
360 git config branch.master.mergeoptions "--squash" &&
361 test_tick &&
362 git merge --no-squash c2 &&
363 verify_merge file result.1-5 msg.1-5 &&
364 verify_parents $c1 $c2
365 '
367 test_debug 'git log --graph --decorate --oneline --all'
369 test_expect_success 'merge c0 with c1 (no-ff)' '
370 git reset --hard c0 &&
371 git config branch.master.mergeoptions "" &&
372 test_tick &&
373 git merge --no-ff c1 &&
374 verify_merge file result.1 &&
375 verify_parents $c0 $c1
376 '
378 test_debug 'git log --graph --decorate --oneline --all'
380 test_expect_success 'combining --squash and --no-ff is refused' '
381 test_must_fail git merge --squash --no-ff c1 &&
382 test_must_fail git merge --no-ff --squash c1
383 '
385 test_expect_success 'combining --ff-only and --no-ff is refused' '
386 test_must_fail git merge --ff-only --no-ff c1 &&
387 test_must_fail git merge --no-ff --ff-only c1
388 '
390 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
391 git reset --hard c0 &&
392 git config branch.master.mergeoptions "--no-ff" &&
393 git merge --ff c1 &&
394 verify_merge file result.1 &&
395 verify_head $c1
396 '
398 test_expect_success 'merge log message' '
399 git reset --hard c0 &&
400 git merge --no-log c2 &&
401 git show -s --pretty=format:%b HEAD >msg.act &&
402 test_cmp msg.nolog msg.act &&
404 git merge --log c3 &&
405 git show -s --pretty=format:%b HEAD >msg.act &&
406 test_cmp msg.log msg.act &&
408 git reset --hard HEAD^ &&
409 git config merge.log yes &&
410 git merge c3 &&
411 git show -s --pretty=format:%b HEAD >msg.act &&
412 test_cmp msg.log msg.act
413 '
415 test_debug 'git log --graph --decorate --oneline --all'
417 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
418 git reset --hard c1 &&
419 git config branch.master.mergeoptions "" &&
420 test_tick &&
421 git merge c0 c2 c0 c1 &&
422 verify_merge file result.1-5 &&
423 verify_parents $c1 $c2
424 '
426 test_debug 'git log --graph --decorate --oneline --all'
428 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
429 git reset --hard c1 &&
430 git config branch.master.mergeoptions "" &&
431 test_tick &&
432 git merge c0 c2 c0 c1 &&
433 verify_merge file result.1-5 &&
434 verify_parents $c1 $c2
435 '
437 test_debug 'git log --graph --decorate --oneline --all'
439 test_expect_success 'merge c1 with c1 and c2' '
440 git reset --hard c1 &&
441 git config branch.master.mergeoptions "" &&
442 test_tick &&
443 git merge c1 c2 &&
444 verify_merge file result.1-5 &&
445 verify_parents $c1 $c2
446 '
448 test_debug 'git log --graph --decorate --oneline --all'
450 test_expect_success 'merge fast-forward in a dirty tree' '
451 git reset --hard c0 &&
452 mv file file1 &&
453 cat file1 >file &&
454 rm -f file1 &&
455 git merge c2
456 '
458 test_debug 'git log --graph --decorate --oneline --all'
460 test_expect_success 'in-index merge' '
461 git reset --hard c0 &&
462 git merge --no-ff -s resolve c1 >out &&
463 grep "Wonderful." out &&
464 verify_parents $c0 $c1
465 '
467 test_debug 'git log --graph --decorate --oneline --all'
469 test_expect_success 'refresh the index before merging' '
470 git reset --hard c1 &&
471 cp file file.n && mv -f file.n file &&
472 git merge c3
473 '
475 cat >expected.branch <<\EOF
476 Merge branch 'c5-branch' (early part)
477 EOF
478 cat >expected.tag <<\EOF
479 Merge commit 'c5~1'
480 EOF
482 test_expect_success 'merge early part of c2' '
483 git reset --hard c3 &&
484 echo c4 >c4.c &&
485 git add c4.c &&
486 git commit -m c4 &&
487 git tag c4 &&
488 echo c5 >c5.c &&
489 git add c5.c &&
490 git commit -m c5 &&
491 git tag c5 &&
492 git reset --hard c3 &&
493 echo c6 >c6.c &&
494 git add c6.c &&
495 git commit -m c6 &&
496 git tag c6 &&
497 git branch -f c5-branch c5 &&
498 git merge c5-branch~1 &&
499 git show -s --pretty=format:%s HEAD >actual.branch &&
500 git reset --keep HEAD^ &&
501 git merge c5~1 &&
502 git show -s --pretty=format:%s HEAD >actual.tag &&
503 test_cmp expected.branch actual.branch &&
504 test_cmp expected.tag actual.tag
505 '
507 test_debug 'git log --graph --decorate --oneline --all'
509 test_expect_success 'merge --no-ff --no-commit && commit' '
510 git reset --hard c0 &&
511 git merge --no-ff --no-commit c1 &&
512 EDITOR=: git commit &&
513 verify_parents $c0 $c1
514 '
516 test_debug 'git log --graph --decorate --oneline --all'
518 test_expect_success 'amending no-ff merge commit' '
519 EDITOR=: git commit --amend &&
520 verify_parents $c0 $c1
521 '
523 test_debug 'git log --graph --decorate --oneline --all'
525 test_done