3 # Copyright (c) 2007 Lars Hjemli
6 test_description='git merge
8 Testing basic merge operations/option parsing.
17 * [master] Merge commit 'c1'
19 - [master] Merge commit 'c1'
26 +++++++* [c0] commit 0
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 &&
41 echo "Merge commit '\''c2'\''" >msg.1-5 &&
42 echo "Merge commit '\''c2'\''; commit '\''c3'\''" >msg.1-5-9 &&
44 echo "Squashed commit of the following:" &&
46 git log --no-merges ^HEAD c1
49 echo "Squashed commit of the following:" &&
51 git log --no-merges ^HEAD c2
54 echo "Squashed commit of the following:" &&
56 git log --no-merges ^HEAD c2 c3
60 echo "* commit '\''c3'\'':" &&
68 git update-index --refresh &&
69 git diff --exit-code &&
72 git show -s --pretty=format:%s HEAD >msg.act &&
78 echo "$1" >head.expected &&
79 git rev-parse HEAD >head.actual &&
80 test_cmp head.expected head.actual
84 printf "%s\n" "$@" >parents.expected &&
89 git rev-parse HEAD^$i >>parents.actual &&
93 test_cmp parents.expected parents.actual
97 printf "%s\n" "$@" >mergehead.expected &&
98 test_cmp mergehead.expected .git/MERGE_HEAD
101 verify_no_mergehead() {
102 ! test -e .git/MERGE_HEAD
106 test_expect_success 'setup' '
109 git commit -m "commit 0" &&
111 c0=$(git rev-parse HEAD) &&
115 git commit -m "commit 1" &&
117 c1=$(git rev-parse HEAD) &&
118 git reset --hard "$c0" &&
122 git commit -m "commit 2" &&
124 c2=$(git rev-parse HEAD) &&
125 git reset --hard "$c0" &&
129 git commit -m "commit 3" &&
131 c3=$(git rev-parse HEAD)
132 git reset --hard "$c0" &&
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
147 test_expect_success 'reject non-strategy with a git-merge-foo name' '
148 test_must_fail git merge -s index c1
151 test_expect_success 'merge c0 with c1' '
152 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
154 git reset --hard c0 &&
156 verify_merge file result.1 &&
159 git reflog -1 >reflog.actual &&
160 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
161 test_cmp reflog.expected reflog.fuzzy
164 test_debug 'git log --graph --decorate --oneline --all'
166 test_expect_success 'merge c0 with c1 with --ff-only' '
167 git reset --hard c0 &&
168 git merge --ff-only c1 &&
169 git merge --ff-only HEAD c0 c1 &&
170 verify_merge file result.1 &&
174 test_debug 'git log --graph --decorate --oneline --all'
176 test_expect_success 'merge from unborn branch' '
177 git checkout -f master &&
178 test_might_fail git branch -D kid &&
180 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
182 git checkout --orphan kid &&
183 test_when_finished "git checkout -f master" &&
186 git merge --ff-only c1 &&
187 verify_merge file result.1 &&
190 git reflog -1 >reflog.actual &&
191 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
192 test_cmp reflog.expected reflog.fuzzy
195 test_debug 'git log --graph --decorate --oneline --all'
197 test_expect_success 'merge c1 with c2' '
198 git reset --hard c1 &&
201 verify_merge file result.1-5 msg.1-5 &&
202 verify_parents $c1 $c2
205 test_debug 'git log --graph --decorate --oneline --all'
207 test_expect_success 'merge c1 with c2 and c3' '
208 git reset --hard c1 &&
211 verify_merge file result.1-5-9 msg.1-5-9 &&
212 verify_parents $c1 $c2 $c3
215 test_debug 'git log --graph --decorate --oneline --all'
217 test_expect_success 'failing merges with --ff-only' '
218 git reset --hard c1 &&
220 test_must_fail git merge --ff-only c2 &&
221 test_must_fail git merge --ff-only c3 &&
222 test_must_fail git merge --ff-only c2 c3
225 test_expect_success 'merge c0 with c1 (no-commit)' '
226 git reset --hard c0 &&
227 git merge --no-commit c1 &&
228 verify_merge file result.1 &&
232 test_debug 'git log --graph --decorate --oneline --all'
234 test_expect_success 'merge c1 with c2 (no-commit)' '
235 git reset --hard c1 &&
236 git merge --no-commit c2 &&
237 verify_merge file result.1-5 &&
239 verify_mergeheads $c2
242 test_debug 'git log --graph --decorate --oneline --all'
244 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
245 git reset --hard c1 &&
246 git merge --no-commit c2 c3 &&
247 verify_merge file result.1-5-9 &&
249 verify_mergeheads $c2 $c3
252 test_debug 'git log --graph --decorate --oneline --all'
254 test_expect_success 'merge c0 with c1 (squash)' '
255 git reset --hard c0 &&
256 git merge --squash c1 &&
257 verify_merge file result.1 &&
259 verify_no_mergehead &&
260 test_cmp squash.1 .git/SQUASH_MSG
263 test_debug 'git log --graph --decorate --oneline --all'
265 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
266 git reset --hard c0 &&
267 git merge --squash --ff-only c1 &&
268 verify_merge file result.1 &&
270 verify_no_mergehead &&
271 test_cmp squash.1 .git/SQUASH_MSG
274 test_debug 'git log --graph --decorate --oneline --all'
276 test_expect_success 'merge c1 with c2 (squash)' '
277 git reset --hard c1 &&
278 git merge --squash c2 &&
279 verify_merge file result.1-5 &&
281 verify_no_mergehead &&
282 test_cmp squash.1-5 .git/SQUASH_MSG
285 test_debug 'git log --graph --decorate --oneline --all'
287 test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
288 git reset --hard c1 &&
289 test_must_fail git merge --squash --ff-only c2
292 test_debug 'git log --graph --decorate --oneline --all'
294 test_expect_success 'merge c1 with c2 and c3 (squash)' '
295 git reset --hard c1 &&
296 git merge --squash c2 c3 &&
297 verify_merge file result.1-5-9 &&
299 verify_no_mergehead &&
300 test_cmp squash.1-5-9 .git/SQUASH_MSG
303 test_debug 'git log --graph --decorate --oneline --all'
305 test_expect_success 'merge c1 with c2 (no-commit in config)' '
306 git reset --hard c1 &&
307 git config branch.master.mergeoptions "--no-commit" &&
309 verify_merge file result.1-5 &&
311 verify_mergeheads $c2
314 test_debug 'git log --graph --decorate --oneline --all'
316 test_expect_success 'merge c1 with c2 (squash in config)' '
317 git reset --hard c1 &&
318 git config branch.master.mergeoptions "--squash" &&
320 verify_merge file result.1-5 &&
322 verify_no_mergehead &&
323 test_cmp squash.1-5 .git/SQUASH_MSG
326 test_debug 'git log --graph --decorate --oneline --all'
328 test_expect_success 'override config option -n with --summary' '
329 git reset --hard c1 &&
330 git config branch.master.mergeoptions "-n" &&
332 git merge --summary c2 >diffstat.txt &&
333 verify_merge file result.1-5 msg.1-5 &&
334 verify_parents $c1 $c2 &&
335 if ! grep "^ file | *2 +-$" diffstat.txt
337 echo "[OOPS] diffstat was not generated with --summary"
342 test_expect_success 'override config option -n with --stat' '
343 git reset --hard c1 &&
344 git config branch.master.mergeoptions "-n" &&
346 git merge --stat c2 >diffstat.txt &&
347 verify_merge file result.1-5 msg.1-5 &&
348 verify_parents $c1 $c2 &&
349 if ! grep "^ file | *2 +-$" diffstat.txt
351 echo "[OOPS] diffstat was not generated with --stat"
356 test_debug 'git log --graph --decorate --oneline --all'
358 test_expect_success 'override config option --stat' '
359 git reset --hard c1 &&
360 git config branch.master.mergeoptions "--stat" &&
362 git merge -n c2 >diffstat.txt &&
363 verify_merge file result.1-5 msg.1-5 &&
364 verify_parents $c1 $c2 &&
365 if grep "^ file | *2 +-$" diffstat.txt
367 echo "[OOPS] diffstat was generated"
372 test_debug 'git log --graph --decorate --oneline --all'
374 test_expect_success 'merge c1 with c2 (override --no-commit)' '
375 git reset --hard c1 &&
376 git config branch.master.mergeoptions "--no-commit" &&
378 git merge --commit c2 &&
379 verify_merge file result.1-5 msg.1-5 &&
380 verify_parents $c1 $c2
383 test_debug 'git log --graph --decorate --oneline --all'
385 test_expect_success 'merge c1 with c2 (override --squash)' '
386 git reset --hard c1 &&
387 git config branch.master.mergeoptions "--squash" &&
389 git merge --no-squash c2 &&
390 verify_merge file result.1-5 msg.1-5 &&
391 verify_parents $c1 $c2
394 test_debug 'git log --graph --decorate --oneline --all'
396 test_expect_success 'merge c0 with c1 (no-ff)' '
397 git reset --hard c0 &&
398 git config branch.master.mergeoptions "" &&
400 git merge --no-ff c1 &&
401 verify_merge file result.1 &&
402 verify_parents $c0 $c1
405 test_debug 'git log --graph --decorate --oneline --all'
407 test_expect_success 'combining --squash and --no-ff is refused' '
408 test_must_fail git merge --squash --no-ff c1 &&
409 test_must_fail git merge --no-ff --squash c1
412 test_expect_success 'combining --ff-only and --no-ff is refused' '
413 test_must_fail git merge --ff-only --no-ff c1 &&
414 test_must_fail git merge --no-ff --ff-only c1
417 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
418 git reset --hard c0 &&
419 git config branch.master.mergeoptions "--no-ff" &&
421 verify_merge file result.1 &&
425 test_expect_success 'merge log message' '
426 git reset --hard c0 &&
427 git merge --no-log c2 &&
428 git show -s --pretty=format:%b HEAD >msg.act &&
429 test_cmp msg.nolog msg.act &&
431 git merge --log c3 &&
432 git show -s --pretty=format:%b HEAD >msg.act &&
433 test_cmp msg.log msg.act &&
435 git reset --hard HEAD^ &&
436 git config merge.log yes &&
438 git show -s --pretty=format:%b HEAD >msg.act &&
439 test_cmp msg.log msg.act
442 test_debug 'git log --graph --decorate --oneline --all'
444 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
445 git reset --hard c1 &&
446 git config branch.master.mergeoptions "" &&
448 git merge c0 c2 c0 c1 &&
449 verify_merge file result.1-5 &&
450 verify_parents $c1 $c2
453 test_debug 'git log --graph --decorate --oneline --all'
455 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
456 git reset --hard c1 &&
457 git config branch.master.mergeoptions "" &&
459 git merge c0 c2 c0 c1 &&
460 verify_merge file result.1-5 &&
461 verify_parents $c1 $c2
464 test_debug 'git log --graph --decorate --oneline --all'
466 test_expect_success 'merge c1 with c1 and c2' '
467 git reset --hard c1 &&
468 git config branch.master.mergeoptions "" &&
471 verify_merge file result.1-5 &&
472 verify_parents $c1 $c2
475 test_debug 'git log --graph --decorate --oneline --all'
477 test_expect_success 'merge fast-forward in a dirty tree' '
478 git reset --hard c0 &&
485 test_debug 'git log --graph --decorate --oneline --all'
487 test_expect_success 'in-index merge' '
488 git reset --hard c0 &&
489 git merge --no-ff -s resolve c1 >out &&
490 grep "Wonderful." out &&
491 verify_parents $c0 $c1
494 test_debug 'git log --graph --decorate --oneline --all'
496 test_expect_success 'refresh the index before merging' '
497 git reset --hard c1 &&
498 cp file file.n && mv -f file.n file &&
502 cat >expected.branch <<\EOF
503 Merge branch 'c5-branch' (early part)
505 cat >expected.tag <<\EOF
509 test_expect_success 'merge early part of c2' '
510 git reset --hard c3 &&
519 git reset --hard c3 &&
524 git branch -f c5-branch c5 &&
525 git merge c5-branch~1 &&
526 git show -s --pretty=format:%s HEAD >actual.branch &&
527 git reset --keep HEAD^ &&
529 git show -s --pretty=format:%s HEAD >actual.tag &&
530 test_cmp expected.branch actual.branch &&
531 test_cmp expected.tag actual.tag
534 test_debug 'git log --graph --decorate --oneline --all'
536 test_expect_success 'merge --no-ff --no-commit && commit' '
537 git reset --hard c0 &&
538 git merge --no-ff --no-commit c1 &&
539 EDITOR=: git commit &&
540 verify_parents $c0 $c1
543 test_debug 'git log --graph --decorate --oneline --all'
545 test_expect_success 'amending no-ff merge commit' '
546 EDITOR=: git commit --amend &&
547 verify_parents $c0 $c1
550 test_debug 'git log --graph --decorate --oneline --all'