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
'merge -h with invalid index' '
153 test_expect_code 129 git merge -h 2>usage
155 grep "[Uu]sage: git merge" broken/usage
158 test_expect_success
'reject non-strategy with a git-merge-foo name' '
159 test_must_fail git merge -s index c1
162 test_expect_success
'merge c0 with c1' '
163 echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
165 git reset --hard c0 &&
167 verify_merge file result.1 &&
170 git reflog -1 >reflog.actual &&
171 sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
172 test_cmp reflog.expected reflog.fuzzy
175 test_debug
'git log --graph --decorate --oneline --all'
177 test_expect_success
'merge c0 with c1 with --ff-only' '
178 git reset --hard c0 &&
179 git merge --ff-only c1 &&
180 git merge --ff-only HEAD c0 c1 &&
181 verify_merge file result.1 &&
185 test_debug
'git log --graph --decorate --oneline --all'
187 test_expect_success
'merge from unborn branch' '
188 git checkout -f master &&
189 test_might_fail git branch -D kid &&
191 echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
193 git checkout --orphan kid &&
194 test_when_finished "git checkout -f master" &&
197 git merge --ff-only c1 &&
198 verify_merge file result.1 &&
201 git reflog -1 >reflog.actual &&
202 sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
203 test_cmp reflog.expected reflog.fuzzy
206 test_debug
'git log --graph --decorate --oneline --all'
208 test_expect_success
'merge c1 with c2' '
209 git reset --hard c1 &&
212 verify_merge file result.1-5 msg.1-5 &&
213 verify_parents $c1 $c2
216 test_debug
'git log --graph --decorate --oneline --all'
218 test_expect_success
'merge c1 with c2 and c3' '
219 git reset --hard c1 &&
222 verify_merge file result.1-5-9 msg.1-5-9 &&
223 verify_parents $c1 $c2 $c3
226 test_debug
'git log --graph --decorate --oneline --all'
228 test_expect_success
'failing merges with --ff-only' '
229 git reset --hard c1 &&
231 test_must_fail git merge --ff-only c2 &&
232 test_must_fail git merge --ff-only c3 &&
233 test_must_fail git merge --ff-only c2 c3
236 test_expect_success
'merge c0 with c1 (no-commit)' '
237 git reset --hard c0 &&
238 git merge --no-commit c1 &&
239 verify_merge file result.1 &&
243 test_debug
'git log --graph --decorate --oneline --all'
245 test_expect_success
'merge c1 with c2 (no-commit)' '
246 git reset --hard c1 &&
247 git merge --no-commit c2 &&
248 verify_merge file result.1-5 &&
250 verify_mergeheads $c2
253 test_debug
'git log --graph --decorate --oneline --all'
255 test_expect_success
'merge c1 with c2 and c3 (no-commit)' '
256 git reset --hard c1 &&
257 git merge --no-commit c2 c3 &&
258 verify_merge file result.1-5-9 &&
260 verify_mergeheads $c2 $c3
263 test_debug
'git log --graph --decorate --oneline --all'
265 test_expect_success
'merge c0 with c1 (squash)' '
266 git reset --hard c0 &&
267 git merge --squash 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 c0 with c1 (squash, ff-only)' '
277 git reset --hard c0 &&
278 git merge --squash --ff-only c1 &&
279 verify_merge file result.1 &&
281 verify_no_mergehead &&
282 test_cmp squash.1 .git/SQUASH_MSG
285 test_debug
'git log --graph --decorate --oneline --all'
287 test_expect_success
'merge c1 with c2 (squash)' '
288 git reset --hard c1 &&
289 git merge --squash c2 &&
290 verify_merge file result.1-5 &&
292 verify_no_mergehead &&
293 test_cmp squash.1-5 .git/SQUASH_MSG
296 test_debug
'git log --graph --decorate --oneline --all'
298 test_expect_success
'unsuccesful merge of c1 with c2 (squash, ff-only)' '
299 git reset --hard c1 &&
300 test_must_fail git merge --squash --ff-only c2
303 test_debug
'git log --graph --decorate --oneline --all'
305 test_expect_success
'merge c1 with c2 and c3 (squash)' '
306 git reset --hard c1 &&
307 git merge --squash c2 c3 &&
308 verify_merge file result.1-5-9 &&
310 verify_no_mergehead &&
311 test_cmp squash.1-5-9 .git/SQUASH_MSG
314 test_debug
'git log --graph --decorate --oneline --all'
316 test_expect_success
'merge c1 with c2 (no-commit in config)' '
317 git reset --hard c1 &&
318 git config branch.master.mergeoptions "--no-commit" &&
320 verify_merge file result.1-5 &&
322 verify_mergeheads $c2
325 test_debug
'git log --graph --decorate --oneline --all'
327 test_expect_success
'merge c1 with c2 (log in config)' '
328 git config branch.master.mergeoptions "" &&
329 git reset --hard c1 &&
330 git merge --log c2 &&
331 git show -s --pretty=tformat:%s%n%b >expect &&
333 git config branch.master.mergeoptions --log &&
334 git reset --hard c1 &&
336 git show -s --pretty=tformat:%s%n%b >actual &&
338 test_cmp expect actual
341 test_expect_success
'merge c1 with c2 (log in config gets overridden)' '
343 git config --remove-section branch.master
344 git config --remove-section merge
346 git reset --hard c1 &&
348 git show -s --pretty=tformat:%s%n%b >expect &&
350 git config branch.master.mergeoptions "--no-log" &&
351 git config merge.log true &&
352 git reset --hard c1 &&
354 git show -s --pretty=tformat:%s%n%b >actual &&
356 test_cmp expect actual
359 test_expect_success
'merge c1 with c2 (squash in config)' '
360 git reset --hard c1 &&
361 git config branch.master.mergeoptions "--squash" &&
363 verify_merge file result.1-5 &&
365 verify_no_mergehead &&
366 test_cmp squash.1-5 .git/SQUASH_MSG
369 test_debug
'git log --graph --decorate --oneline --all'
371 test_expect_success
'override config option -n with --summary' '
372 git reset --hard c1 &&
373 git config branch.master.mergeoptions "-n" &&
375 git merge --summary c2 >diffstat.txt &&
376 verify_merge file result.1-5 msg.1-5 &&
377 verify_parents $c1 $c2 &&
378 if ! grep "^ file | *2 +-$" diffstat.txt
380 echo "[OOPS] diffstat was not generated with --summary"
385 test_expect_success
'override config option -n with --stat' '
386 git reset --hard c1 &&
387 git config branch.master.mergeoptions "-n" &&
389 git merge --stat c2 >diffstat.txt &&
390 verify_merge file result.1-5 msg.1-5 &&
391 verify_parents $c1 $c2 &&
392 if ! grep "^ file | *2 +-$" diffstat.txt
394 echo "[OOPS] diffstat was not generated with --stat"
399 test_debug
'git log --graph --decorate --oneline --all'
401 test_expect_success
'override config option --stat' '
402 git reset --hard c1 &&
403 git config branch.master.mergeoptions "--stat" &&
405 git merge -n c2 >diffstat.txt &&
406 verify_merge file result.1-5 msg.1-5 &&
407 verify_parents $c1 $c2 &&
408 if grep "^ file | *2 +-$" diffstat.txt
410 echo "[OOPS] diffstat was generated"
415 test_debug
'git log --graph --decorate --oneline --all'
417 test_expect_success
'merge c1 with c2 (override --no-commit)' '
418 git reset --hard c1 &&
419 git config branch.master.mergeoptions "--no-commit" &&
421 git merge --commit c2 &&
422 verify_merge file result.1-5 msg.1-5 &&
423 verify_parents $c1 $c2
426 test_debug
'git log --graph --decorate --oneline --all'
428 test_expect_success
'merge c1 with c2 (override --squash)' '
429 git reset --hard c1 &&
430 git config branch.master.mergeoptions "--squash" &&
432 git merge --no-squash c2 &&
433 verify_merge file result.1-5 msg.1-5 &&
434 verify_parents $c1 $c2
437 test_debug
'git log --graph --decorate --oneline --all'
439 test_expect_success
'merge c0 with c1 (no-ff)' '
440 git reset --hard c0 &&
441 git config branch.master.mergeoptions "" &&
443 git merge --no-ff c1 &&
444 verify_merge file result.1 &&
445 verify_parents $c0 $c1
448 test_debug
'git log --graph --decorate --oneline --all'
450 test_expect_success
'combining --squash and --no-ff is refused' '
451 test_must_fail git merge --squash --no-ff c1 &&
452 test_must_fail git merge --no-ff --squash c1
455 test_expect_success
'combining --ff-only and --no-ff is refused' '
456 test_must_fail git merge --ff-only --no-ff c1 &&
457 test_must_fail git merge --no-ff --ff-only c1
460 test_expect_success
'merge c0 with c1 (ff overrides no-ff)' '
461 git reset --hard c0 &&
462 git config branch.master.mergeoptions "--no-ff" &&
464 verify_merge file result.1 &&
468 test_expect_success
'merge log message' '
469 git reset --hard c0 &&
470 git merge --no-log c2 &&
471 git show -s --pretty=format:%b HEAD >msg.act &&
472 test_cmp msg.nolog msg.act &&
474 git merge --log c3 &&
475 git show -s --pretty=format:%b HEAD >msg.act &&
476 test_cmp msg.log msg.act &&
478 git reset --hard HEAD^ &&
479 git config merge.log yes &&
481 git show -s --pretty=format:%b HEAD >msg.act &&
482 test_cmp msg.log msg.act
485 test_debug
'git log --graph --decorate --oneline --all'
487 test_expect_success
'merge c1 with c0, c2, c0, and c1' '
488 git reset --hard c1 &&
489 git config branch.master.mergeoptions "" &&
491 git merge c0 c2 c0 c1 &&
492 verify_merge file result.1-5 &&
493 verify_parents $c1 $c2
496 test_debug
'git log --graph --decorate --oneline --all'
498 test_expect_success
'merge c1 with c0, c2, c0, and c1' '
499 git reset --hard c1 &&
500 git config branch.master.mergeoptions "" &&
502 git merge c0 c2 c0 c1 &&
503 verify_merge file result.1-5 &&
504 verify_parents $c1 $c2
507 test_debug
'git log --graph --decorate --oneline --all'
509 test_expect_success
'merge c1 with c1 and c2' '
510 git reset --hard c1 &&
511 git config branch.master.mergeoptions "" &&
514 verify_merge file result.1-5 &&
515 verify_parents $c1 $c2
518 test_debug
'git log --graph --decorate --oneline --all'
520 test_expect_success
'merge fast-forward in a dirty tree' '
521 git reset --hard c0 &&
528 test_debug
'git log --graph --decorate --oneline --all'
530 test_expect_success
'in-index merge' '
531 git reset --hard c0 &&
532 git merge --no-ff -s resolve c1 >out &&
533 test_i18ngrep "Wonderful." out &&
534 verify_parents $c0 $c1
537 test_debug
'git log --graph --decorate --oneline --all'
539 test_expect_success
'refresh the index before merging' '
540 git reset --hard c1 &&
541 cp file file.n && mv -f file.n file &&
545 cat >expected.branch
<<\EOF
546 Merge branch
'c5-branch' (early part
)
548 cat >expected.tag
<<\EOF
552 test_expect_success
'merge early part of c2' '
553 git reset --hard c3 &&
562 git reset --hard c3 &&
567 git branch -f c5-branch c5 &&
568 git merge c5-branch~1 &&
569 git show -s --pretty=format:%s HEAD >actual.branch &&
570 git reset --keep HEAD^ &&
572 git show -s --pretty=format:%s HEAD >actual.tag &&
573 test_cmp expected.branch actual.branch &&
574 test_cmp expected.tag actual.tag
577 test_debug
'git log --graph --decorate --oneline --all'
579 test_expect_success
'merge --no-ff --no-commit && commit' '
580 git reset --hard c0 &&
581 git merge --no-ff --no-commit c1 &&
582 EDITOR=: git commit &&
583 verify_parents $c0 $c1
586 test_debug
'git log --graph --decorate --oneline --all'
588 test_expect_success
'amending no-ff merge commit' '
589 EDITOR=: git commit --amend &&
590 verify_parents $c0 $c1
593 test_debug
'git log --graph --decorate --oneline --all'