c665acdd0a128667a93c57a7942c5bd7a44b2d8b
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 printf '%s\n' 1 2 3 4 5 6 7 8 9 >file
32 printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >file.1
33 printf '%s\n' 1 2 3 4 '5 X' 6 7 8 9 >file.5
34 printf '%s\n' 1 2 3 4 5 6 7 8 '9 X' >file.9
35 printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >result.1
36 printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
37 printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
39 create_merge_msgs () {
40 echo "Merge commit 'c2'" >msg.1-5 &&
41 echo "Merge commit 'c2'; commit 'c3'" >msg.1-5-9 &&
42 {
43 echo "Squashed commit of the following:" &&
44 echo &&
45 git log --no-merges ^HEAD c1
46 } >squash.1 &&
47 {
48 echo "Squashed commit of the following:" &&
49 echo &&
50 git log --no-merges ^HEAD c2
51 } >squash.1-5 &&
52 {
53 echo "Squashed commit of the following:" &&
54 echo &&
55 git log --no-merges ^HEAD c2 c3
56 } >squash.1-5-9 &&
57 echo >msg.nolog &&
58 {
59 echo "* commit 'c3':" &&
60 echo " commit 3" &&
61 echo
62 } >msg.log
63 }
65 verify_merge () {
66 test_cmp "$2" "$1" &&
67 git update-index --refresh &&
68 git diff --exit-code &&
69 if test -n "$3"
70 then
71 git show -s --pretty=format:%s HEAD >msg.act &&
72 test_cmp "$3" msg.act
73 fi
74 }
76 verify_head () {
77 echo "$1" >head.expected &&
78 git rev-parse HEAD >head.actual &&
79 test_cmp head.expected head.actual
80 }
82 verify_parents () {
83 printf '%s\n' "$@" >parents.expected &&
84 >parents.actual &&
85 i=1 &&
86 while test $i -le $#
87 do
88 git rev-parse HEAD^$i >>parents.actual &&
89 i=$(expr $i + 1) ||
90 return 1
91 done &&
92 test_cmp parents.expected parents.actual
93 }
95 verify_mergeheads () {
96 printf '%s\n' "$@" >mergehead.expected &&
97 test_cmp mergehead.expected .git/MERGE_HEAD
98 }
100 verify_no_mergehead () {
101 ! test -e .git/MERGE_HEAD
102 }
104 test_expect_success 'setup' '
105 git add file &&
106 test_tick &&
107 git commit -m "commit 0" &&
108 git tag c0 &&
109 c0=$(git rev-parse HEAD) &&
110 cp file.1 file &&
111 git add file &&
112 test_tick &&
113 git commit -m "commit 1" &&
114 git tag c1 &&
115 c1=$(git rev-parse HEAD) &&
116 git reset --hard "$c0" &&
117 cp file.5 file &&
118 git add file &&
119 test_tick &&
120 git commit -m "commit 2" &&
121 git tag c2 &&
122 c2=$(git rev-parse HEAD) &&
123 git reset --hard "$c0" &&
124 cp file.9 file &&
125 git add file &&
126 test_tick &&
127 git commit -m "commit 3" &&
128 git tag c3 &&
129 c3=$(git rev-parse HEAD)
130 git reset --hard "$c0" &&
131 create_merge_msgs
132 '
134 test_debug 'git log --graph --decorate --oneline --all'
136 test_expect_success 'test option parsing' '
137 test_must_fail git merge -$ c1 &&
138 test_must_fail git merge --no-such c1 &&
139 test_must_fail git merge -s foobar c1 &&
140 test_must_fail git merge -s=foobar c1 &&
141 test_must_fail git merge -m &&
142 test_must_fail git merge
143 '
145 test_expect_success 'merge -h with invalid index' '
146 mkdir broken &&
147 (
148 cd broken &&
149 git init &&
150 >.git/index &&
151 test_expect_code 129 git merge -h 2>usage
152 ) &&
153 grep "[Uu]sage: git merge" broken/usage
154 '
156 test_expect_success 'reject non-strategy with a git-merge-foo name' '
157 test_must_fail git merge -s index c1
158 '
160 test_expect_success 'merge c0 with c1' '
161 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
163 git reset --hard c0 &&
164 git merge c1 &&
165 verify_merge file result.1 &&
166 verify_head "$c1" &&
168 git reflog -1 >reflog.actual &&
169 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
170 test_cmp reflog.expected reflog.fuzzy
171 '
173 test_debug 'git log --graph --decorate --oneline --all'
175 test_expect_success 'merge c0 with c1 with --ff-only' '
176 git reset --hard c0 &&
177 git merge --ff-only c1 &&
178 git merge --ff-only HEAD c0 c1 &&
179 verify_merge file result.1 &&
180 verify_head "$c1"
181 '
183 test_debug 'git log --graph --decorate --oneline --all'
185 test_expect_success 'merge from unborn branch' '
186 git checkout -f master &&
187 test_might_fail git branch -D kid &&
189 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
191 git checkout --orphan kid &&
192 test_when_finished "git checkout -f master" &&
193 git rm -fr . &&
194 test_tick &&
195 git merge --ff-only c1 &&
196 verify_merge file result.1 &&
197 verify_head "$c1" &&
199 git reflog -1 >reflog.actual &&
200 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
201 test_cmp reflog.expected reflog.fuzzy
202 '
204 test_debug 'git log --graph --decorate --oneline --all'
206 test_expect_success 'merge c1 with c2' '
207 git reset --hard c1 &&
208 test_tick &&
209 git merge c2 &&
210 verify_merge file result.1-5 msg.1-5 &&
211 verify_parents $c1 $c2
212 '
214 test_debug 'git log --graph --decorate --oneline --all'
216 test_expect_success 'merge c1 with c2 and c3' '
217 git reset --hard c1 &&
218 test_tick &&
219 git merge c2 c3 &&
220 verify_merge file result.1-5-9 msg.1-5-9 &&
221 verify_parents $c1 $c2 $c3
222 '
224 test_debug 'git log --graph --decorate --oneline --all'
226 test_expect_success 'failing merges with --ff-only' '
227 git reset --hard c1 &&
228 test_tick &&
229 test_must_fail git merge --ff-only c2 &&
230 test_must_fail git merge --ff-only c3 &&
231 test_must_fail git merge --ff-only c2 c3
232 '
234 test_expect_success 'merge c0 with c1 (no-commit)' '
235 git reset --hard c0 &&
236 git merge --no-commit c1 &&
237 verify_merge file result.1 &&
238 verify_head $c1
239 '
241 test_debug 'git log --graph --decorate --oneline --all'
243 test_expect_success 'merge c1 with c2 (no-commit)' '
244 git reset --hard c1 &&
245 git merge --no-commit c2 &&
246 verify_merge file result.1-5 &&
247 verify_head $c1 &&
248 verify_mergeheads $c2
249 '
251 test_debug 'git log --graph --decorate --oneline --all'
253 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
254 git reset --hard c1 &&
255 git merge --no-commit c2 c3 &&
256 verify_merge file result.1-5-9 &&
257 verify_head $c1 &&
258 verify_mergeheads $c2 $c3
259 '
261 test_debug 'git log --graph --decorate --oneline --all'
263 test_expect_success 'merge c0 with c1 (squash)' '
264 git reset --hard c0 &&
265 git merge --squash c1 &&
266 verify_merge file result.1 &&
267 verify_head $c0 &&
268 verify_no_mergehead &&
269 test_cmp squash.1 .git/SQUASH_MSG
270 '
272 test_debug 'git log --graph --decorate --oneline --all'
274 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
275 git reset --hard c0 &&
276 git merge --squash --ff-only c1 &&
277 verify_merge file result.1 &&
278 verify_head $c0 &&
279 verify_no_mergehead &&
280 test_cmp squash.1 .git/SQUASH_MSG
281 '
283 test_debug 'git log --graph --decorate --oneline --all'
285 test_expect_success 'merge c1 with c2 (squash)' '
286 git reset --hard c1 &&
287 git merge --squash c2 &&
288 verify_merge file result.1-5 &&
289 verify_head $c1 &&
290 verify_no_mergehead &&
291 test_cmp squash.1-5 .git/SQUASH_MSG
292 '
294 test_debug 'git log --graph --decorate --oneline --all'
296 test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
297 git reset --hard c1 &&
298 test_must_fail git merge --squash --ff-only c2
299 '
301 test_debug 'git log --graph --decorate --oneline --all'
303 test_expect_success 'merge c1 with c2 and c3 (squash)' '
304 git reset --hard c1 &&
305 git merge --squash c2 c3 &&
306 verify_merge file result.1-5-9 &&
307 verify_head $c1 &&
308 verify_no_mergehead &&
309 test_cmp squash.1-5-9 .git/SQUASH_MSG
310 '
312 test_debug 'git log --graph --decorate --oneline --all'
314 test_expect_success 'merge c1 with c2 (no-commit in config)' '
315 git reset --hard c1 &&
316 git config branch.master.mergeoptions "--no-commit" &&
317 git merge c2 &&
318 verify_merge file result.1-5 &&
319 verify_head $c1 &&
320 verify_mergeheads $c2
321 '
323 test_debug 'git log --graph --decorate --oneline --all'
325 test_expect_success 'merge c1 with c2 (squash in config)' '
326 git reset --hard c1 &&
327 git config branch.master.mergeoptions "--squash" &&
328 git merge c2 &&
329 verify_merge file result.1-5 &&
330 verify_head $c1 &&
331 verify_no_mergehead &&
332 test_cmp squash.1-5 .git/SQUASH_MSG
333 '
335 test_debug 'git log --graph --decorate --oneline --all'
337 test_expect_success 'override config option -n with --summary' '
338 git reset --hard c1 &&
339 git config branch.master.mergeoptions "-n" &&
340 test_tick &&
341 git merge --summary c2 >diffstat.txt &&
342 verify_merge file result.1-5 msg.1-5 &&
343 verify_parents $c1 $c2 &&
344 if ! grep "^ file | *2 +-$" diffstat.txt
345 then
346 echo "[OOPS] diffstat was not generated with --summary"
347 false
348 fi
349 '
351 test_expect_success 'override config option -n with --stat' '
352 git reset --hard c1 &&
353 git config branch.master.mergeoptions "-n" &&
354 test_tick &&
355 git merge --stat c2 >diffstat.txt &&
356 verify_merge file result.1-5 msg.1-5 &&
357 verify_parents $c1 $c2 &&
358 if ! grep "^ file | *2 +-$" diffstat.txt
359 then
360 echo "[OOPS] diffstat was not generated with --stat"
361 false
362 fi
363 '
365 test_debug 'git log --graph --decorate --oneline --all'
367 test_expect_success 'override config option --stat' '
368 git reset --hard c1 &&
369 git config branch.master.mergeoptions "--stat" &&
370 test_tick &&
371 git merge -n c2 >diffstat.txt &&
372 verify_merge file result.1-5 msg.1-5 &&
373 verify_parents $c1 $c2 &&
374 if grep "^ file | *2 +-$" diffstat.txt
375 then
376 echo "[OOPS] diffstat was generated"
377 false
378 fi
379 '
381 test_debug 'git log --graph --decorate --oneline --all'
383 test_expect_success 'merge c1 with c2 (override --no-commit)' '
384 git reset --hard c1 &&
385 git config branch.master.mergeoptions "--no-commit" &&
386 test_tick &&
387 git merge --commit c2 &&
388 verify_merge file result.1-5 msg.1-5 &&
389 verify_parents $c1 $c2
390 '
392 test_debug 'git log --graph --decorate --oneline --all'
394 test_expect_success 'merge c1 with c2 (override --squash)' '
395 git reset --hard c1 &&
396 git config branch.master.mergeoptions "--squash" &&
397 test_tick &&
398 git merge --no-squash c2 &&
399 verify_merge file result.1-5 msg.1-5 &&
400 verify_parents $c1 $c2
401 '
403 test_debug 'git log --graph --decorate --oneline --all'
405 test_expect_success 'merge c0 with c1 (no-ff)' '
406 git reset --hard c0 &&
407 git config branch.master.mergeoptions "" &&
408 test_tick &&
409 git merge --no-ff c1 &&
410 verify_merge file result.1 &&
411 verify_parents $c0 $c1
412 '
414 test_debug 'git log --graph --decorate --oneline --all'
416 test_expect_success 'combining --squash and --no-ff is refused' '
417 test_must_fail git merge --squash --no-ff c1 &&
418 test_must_fail git merge --no-ff --squash c1
419 '
421 test_expect_success 'combining --ff-only and --no-ff is refused' '
422 test_must_fail git merge --ff-only --no-ff c1 &&
423 test_must_fail git merge --no-ff --ff-only c1
424 '
426 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
427 git reset --hard c0 &&
428 git config branch.master.mergeoptions "--no-ff" &&
429 git merge --ff c1 &&
430 verify_merge file result.1 &&
431 verify_head $c1
432 '
434 test_expect_success 'merge log message' '
435 git reset --hard c0 &&
436 git merge --no-log c2 &&
437 git show -s --pretty=format:%b HEAD >msg.act &&
438 test_cmp msg.nolog msg.act &&
440 git merge --log c3 &&
441 git show -s --pretty=format:%b HEAD >msg.act &&
442 test_cmp msg.log msg.act &&
444 git reset --hard HEAD^ &&
445 git config merge.log yes &&
446 git merge c3 &&
447 git show -s --pretty=format:%b HEAD >msg.act &&
448 test_cmp msg.log msg.act
449 '
451 test_debug 'git log --graph --decorate --oneline --all'
453 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
454 git reset --hard c1 &&
455 git config branch.master.mergeoptions "" &&
456 test_tick &&
457 git merge c0 c2 c0 c1 &&
458 verify_merge file result.1-5 &&
459 verify_parents $c1 $c2
460 '
462 test_debug 'git log --graph --decorate --oneline --all'
464 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
465 git reset --hard c1 &&
466 git config branch.master.mergeoptions "" &&
467 test_tick &&
468 git merge c0 c2 c0 c1 &&
469 verify_merge file result.1-5 &&
470 verify_parents $c1 $c2
471 '
473 test_debug 'git log --graph --decorate --oneline --all'
475 test_expect_success 'merge c1 with c1 and c2' '
476 git reset --hard c1 &&
477 git config branch.master.mergeoptions "" &&
478 test_tick &&
479 git merge c1 c2 &&
480 verify_merge file result.1-5 &&
481 verify_parents $c1 $c2
482 '
484 test_debug 'git log --graph --decorate --oneline --all'
486 test_expect_success 'merge fast-forward in a dirty tree' '
487 git reset --hard c0 &&
488 mv file file1 &&
489 cat file1 >file &&
490 rm -f file1 &&
491 git merge c2
492 '
494 test_debug 'git log --graph --decorate --oneline --all'
496 test_expect_success C_LOCALE_OUTPUT 'in-index merge' '
497 git reset --hard c0 &&
498 git merge --no-ff -s resolve c1 >out &&
499 grep "Wonderful." out &&
500 verify_parents $c0 $c1
501 '
503 test_debug 'git log --graph --decorate --oneline --all'
505 test_expect_success 'refresh the index before merging' '
506 git reset --hard c1 &&
507 cp file file.n && mv -f file.n file &&
508 git merge c3
509 '
511 cat >expected.branch <<\EOF
512 Merge branch 'c5-branch' (early part)
513 EOF
514 cat >expected.tag <<\EOF
515 Merge commit 'c5~1'
516 EOF
518 test_expect_success 'merge early part of c2' '
519 git reset --hard c3 &&
520 echo c4 >c4.c &&
521 git add c4.c &&
522 git commit -m c4 &&
523 git tag c4 &&
524 echo c5 >c5.c &&
525 git add c5.c &&
526 git commit -m c5 &&
527 git tag c5 &&
528 git reset --hard c3 &&
529 echo c6 >c6.c &&
530 git add c6.c &&
531 git commit -m c6 &&
532 git tag c6 &&
533 git branch -f c5-branch c5 &&
534 git merge c5-branch~1 &&
535 git show -s --pretty=format:%s HEAD >actual.branch &&
536 git reset --keep HEAD^ &&
537 git merge c5~1 &&
538 git show -s --pretty=format:%s HEAD >actual.tag &&
539 test_cmp expected.branch actual.branch &&
540 test_cmp expected.tag actual.tag
541 '
543 test_debug 'git log --graph --decorate --oneline --all'
545 test_expect_success 'merge --no-ff --no-commit && commit' '
546 git reset --hard c0 &&
547 git merge --no-ff --no-commit c1 &&
548 EDITOR=: git commit &&
549 verify_parents $c0 $c1
550 '
552 test_debug 'git log --graph --decorate --oneline --all'
554 test_expect_success 'amending no-ff merge commit' '
555 EDITOR=: git commit --amend &&
556 verify_parents $c0 $c1
557 '
559 test_debug 'git log --graph --decorate --oneline --all'
561 test_done