3 test_description
='recursive merge corner cases involving criss-cross merges'
7 get_clean_checkout
() {
23 test_expect_success
'setup basic criss-cross + rename with no modifications' '
24 ten="0 1 2 3 4 5 6 7 8 9" &&
27 echo line $i in a sample file
31 echo line $i in another sample file
34 test_tick && git commit -m initial &&
39 test_tick && git commit -m R1 &&
43 test_tick && git commit -m L1 &&
46 test_tick && git merge -s ours R1 &&
50 test_tick && git merge -s ours L1 &&
54 test_expect_success
'merge simple rename+criss-cross with no modifications' '
58 test_must_fail git merge -s recursive R2^0 &&
60 test 2 = $(git ls-files -s | wc -l) &&
61 test 2 = $(git ls-files -u | wc -l) &&
62 test 2 = $(git ls-files -o | wc -l) &&
64 test $(git rev-parse :2:three) = $(git rev-parse L2:three) &&
65 test $(git rev-parse :3:three) = $(git rev-parse R2:three) &&
67 test $(git rev-parse L2:three) = $(git hash-object three~HEAD) &&
68 test $(git rev-parse R2:three) = $(git hash-object three~R2^0)
72 # Same as before, but modify L1 slightly:
83 test_expect_success
'setup criss-cross + rename merges with basic modification' '
89 ten="0 1 2 3 4 5 6 7 8 9"
92 echo line $i in a sample file
96 echo line $i in another sample file
99 test_tick && git commit -m initial &&
102 git checkout -b R1 &&
106 test_tick && git commit -m R1 &&
110 test_tick && git commit -m L1 &&
113 test_tick && git merge -s ours R1 &&
117 test_tick && git merge -s ours L1 &&
121 test_expect_success
'merge criss-cross + rename merges with basic modification' '
125 test_must_fail git merge -s recursive R2^0 &&
127 test 2 = $(git ls-files -s | wc -l) &&
128 test 2 = $(git ls-files -u | wc -l) &&
129 test 2 = $(git ls-files -o | wc -l) &&
131 test $(git rev-parse :2:three) = $(git rev-parse L2:three) &&
132 test $(git rev-parse :3:three) = $(git rev-parse R2:three) &&
134 test $(git rev-parse L2:three) = $(git hash-object three~HEAD) &&
135 test $(git rev-parse R2:three) = $(git hash-object three~R2^0)
139 # For the next test, we start with three commits in two lines of development
140 # which setup a rename/add conflict:
141 # Commit A: File 'a' exists
142 # Commit B: Rename 'a' -> 'new_a'
143 # Commit C: Modify 'a', create different 'new_a'
144 # Later, two different people merge and resolve differently:
145 # Commit D: Merge B & C, ignoring separately created 'new_a'
146 # Commit E: Merge B & C making use of some piece of secondary 'new_a'
147 # Finally, someone goes to merge D & E. Does git detect the conflict?
158 test_expect_success
'setup differently handled merges of rename/add conflict' '
164 printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
166 test_tick && git commit -m A &&
171 echo "other content" >>new_a &&
173 test_tick && git commit -m C &&
177 test_tick && git commit -m B &&
180 test_must_fail git merge C &&
182 test_tick && git commit -m D &&
186 test_must_fail git merge B &&
187 rm new_a~HEAD new_a &&
188 printf "Incorrectly merged content" >>new_a &&
190 test_tick && git commit -m E &&
194 test_expect_success
'git detects differently handled merges conflict' '
198 git merge -s recursive E^0 && {
199 echo "BAD: should have conflicted"
200 test "Incorrectly merged content" = "$(cat new_a)" &&
201 echo "BAD: Silently accepted wrong content"
205 test 3 = $(git ls-files -s | wc -l) &&
206 test 3 = $(git ls-files -u | wc -l) &&
207 test 0 = $(git ls-files -o | wc -l) &&
209 test $(git rev-parse :2:new_a) = $(git rev-parse D:new_a) &&
210 test $(git rev-parse :3:new_a) = $(git rev-parse E:new_a) &&
212 git cat-file -p B:new_a >>merged &&
213 git cat-file -p C:new_a >>merge-me &&
215 test_must_fail git merge-file \
216 -L "Temporary merge branch 2" \
218 -L "Temporary merge branch 1" \
219 merged empty merge-me &&
220 sed -e "s/^\([<=>]\)/\1\1\1/" merged >merged-internal &&
221 test $(git rev-parse :1:new_a) = $(git hash-object merged-internal)
225 # criss-cross + modify/delete:
235 # Commit A: file with contents 'A\n'
236 # Commit B: file with contents 'B\n'
237 # Commit C: file not present
238 # Commit D: file with contents 'B\n'
239 # Commit E: file not present
241 # Merging commits D & E should result in modify/delete conflict.
243 test_expect_success
'setup criss-cross + modify/delete resolved differently' '
267 test_must_fail git merge C &&
275 test_must_fail git merge B &&
282 test_expect_success
'git detects conflict merging criss-cross+modify/delete' '
285 test_must_fail git merge -s recursive E^0 &&
287 test 2 -eq $(git ls-files -s | wc -l) &&
288 test 2 -eq $(git ls-files -u | wc -l) &&
290 test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
291 test $(git rev-parse :2:file) = $(git rev-parse B:file)
294 test_expect_success
'git detects conflict merging criss-cross+modify/delete, reverse direction' '
298 test_must_fail git merge -s recursive D^0 &&
300 test 2 -eq $(git ls-files -s | wc -l) &&
301 test 2 -eq $(git ls-files -u | wc -l) &&
303 test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
304 test $(git rev-parse :3:file) = $(git rev-parse B:file)
308 # criss-cross + modify/modify with very contrived file contents:
318 # Commit A: file with contents 'A\n'
319 # Commit B: file with contents 'B\n'
320 # Commit C: file with contents 'C\n'
321 # Commit D: file with contents 'D\n'
322 # Commit E: file with contents:
323 # <<<<<<< Temporary merge branch 1
327 # >>>>>>> Temporary merge branch 2
329 # Now, when we merge commits D & E, does git detect the conflict?
331 test_expect_success
'setup differently handled merges of content conflict' '
355 test_must_fail git merge C &&
363 test_must_fail git merge B &&
365 <<<<<<< Temporary merge branch 1
369 >>>>>>> Temporary merge branch 2
377 test_expect_failure
'git detects conflict w/ criss-cross+contrived resolution' '
380 test_must_fail git merge -s recursive E^0 &&
382 test 3 -eq $(git ls-files -s | wc -l) &&
383 test 3 -eq $(git ls-files -u | wc -l) &&
384 test 0 -eq $(git ls-files -o | wc -l) &&
386 test $(git rev-parse :2:file) = $(git rev-parse D:file) &&
387 test $(git rev-parse :3:file) = $(git rev-parse E:file)
391 # criss-cross + d/f conflict via add/add:
392 # Commit A: Neither file 'a' nor directory 'a/' exists.
393 # Commit B: Introduce 'a'
394 # Commit C: Introduce 'a/file'
395 # Commit D: Merge B & C, keeping 'a' and deleting 'a/'
397 # Two different later cases:
398 # Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
399 # Commit E2: Merge B & C, deleting 'a' but keeping a slightly modified 'a/file'
409 # Merging D & E1 requires we first create a virtual merge base X from
410 # merging A & B in memory. Now, if X could keep both 'a' and 'a/file' in
411 # the index, then the merge of D & E1 could be resolved cleanly with both
412 # 'a' and 'a/file' removed. Since git does not currently allow creating
413 # such a tree, the best we can do is have X contain both 'a~<unique>' and
414 # 'a/file' resulting in the merge of D and E1 having a rename/delete
415 # conflict for 'a'. (Although this merge appears to be unsolvable with git
416 # currently, git could do a lot better than it currently does with these
417 # d/f conflicts, which is the purpose of this test.)
419 # Merge of D & E2 has similar issues for path 'a', but should always result
420 # in a modify/delete conflict for path 'a/file'.
422 # We run each merge in both directions, to check for directional issues
423 # with D/F conflict handling.
426 test_expect_success
'setup differently handled merges of directory/file conflict' '
453 test_must_fail git merge C &&
463 test_must_fail git merge B &&
473 test_must_fail git merge B &&
476 printf "10\n11\n" >a/file &&
483 test_expect_success
'merge of D & E1 fails but has appropriate contents' '
484 get_clean_checkout D^0 &&
486 test_must_fail git merge -s recursive E1^0 &&
488 test 2 -eq $(git ls-files -s | wc -l) &&
489 test 1 -eq $(git ls-files -u | wc -l) &&
490 test 0 -eq $(git ls-files -o | wc -l) &&
492 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
493 test $(git rev-parse :2:a) = $(git rev-parse B:a)
496 test_expect_success
'merge of E1 & D fails but has appropriate contents' '
497 get_clean_checkout E1^0 &&
499 test_must_fail git merge -s recursive D^0 &&
501 test 2 -eq $(git ls-files -s | wc -l) &&
502 test 1 -eq $(git ls-files -u | wc -l) &&
503 test 0 -eq $(git ls-files -o | wc -l) &&
505 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
506 test $(git rev-parse :3:a) = $(git rev-parse B:a)
509 test_expect_success
'merge of D & E2 fails but has appropriate contents' '
510 get_clean_checkout D^0 &&
512 test_must_fail git merge -s recursive E2^0 &&
514 test 4 -eq $(git ls-files -s | wc -l) &&
515 test 3 -eq $(git ls-files -u | wc -l) &&
516 test 1 -eq $(git ls-files -o | wc -l) &&
518 test $(git rev-parse :2:a) = $(git rev-parse B:a) &&
519 test $(git rev-parse :3:a/file) = $(git rev-parse E2:a/file) &&
520 test $(git rev-parse :1:a/file) = $(git rev-parse C:a/file) &&
521 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
526 test_expect_success
'merge of E2 & D fails but has appropriate contents' '
527 get_clean_checkout E2^0 &&
529 test_must_fail git merge -s recursive D^0 &&
531 test 4 -eq $(git ls-files -s | wc -l) &&
532 test 3 -eq $(git ls-files -u | wc -l) &&
533 test 1 -eq $(git ls-files -o | wc -l) &&
535 test $(git rev-parse :3:a) = $(git rev-parse B:a) &&
536 test $(git rev-parse :2:a/file) = $(git rev-parse E2:a/file) &&
537 test $(git rev-parse :1:a/file) = $(git rev-parse C:a/file)
538 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
544 # criss-cross with rename/rename(1to2)/modify followed by
545 # rename/rename(2to1)/modify:
555 # Commit A: new file: a
556 # Commit B: rename a->b, modifying by adding a line
557 # Commit C: rename a->c
558 # Commit D: merge B&C, resolving conflict by keeping contents in newname
559 # Commit E: merge B&C, resolving conflict similar to D but adding another line
561 # There is a conflict merging B & C, but one of filename not of file
562 # content. Whoever created D and E chose specific resolutions for that
563 # conflict resolution. Now, since: (1) there is no content conflict
564 # merging B & C, (2) D does not modify that merged content further, and (3)
565 # both D & E resolve the name conflict in the same way, the modification to
566 # newname in E should not cause any conflicts when it is merged with D.
567 # (Note that this can be accomplished by having the virtual merge base have
568 # the merged contents of b and c stored in a file named a, which seems like
569 # the most logical choice anyway.)
571 # Comment from Junio: I do not necessarily agree with the choice "a", but
572 # it feels sound to say "B and C do not agree what the final pathname
573 # should be, but we know this content was derived from the common A:a so we
574 # use one path whose name is arbitrary in the virtual merge base X between
575 # D and E" and then further let the rename detection to notice that that
576 # arbitrary path gets renamed between X-D to "newname" and X-E also to
577 # "newname" to resolve it as both sides renaming it to the same new
578 # name. It is akin to what we do at the content level, i.e. "B and C do not
579 # agree what the final contents should be, so we leave the conflict marker
580 # but that may cancel out at the final merge stage".
582 test_expect_success
'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
589 printf "1\n2\n3\n4\n5\n6\n" >a &&
594 git checkout -b B A &&
600 git checkout -b C A &&
604 git checkout -q B^0 &&
605 git merge --no-commit -s ours C^0 &&
607 git commit -m "Merge commit C^0 into HEAD" &&
610 git checkout -q C^0 &&
611 git merge --no-commit -s ours B^0 &&
613 printf "7\n8\n" >>newname &&
615 git commit -m "Merge commit B^0 into HEAD" &&
619 test_expect_success
'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
622 git merge -s recursive E^0 &&
624 test 1 -eq $(git ls-files -s | wc -l) &&
625 test 0 -eq $(git ls-files -u | wc -l) &&
626 test 0 -eq $(git ls-files -o | wc -l) &&
628 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
632 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
642 # Commit A: new file: a
643 # Commit B: rename a->b
644 # Commit C: rename a->c, add different a
645 # Commit D: merge B&C, keeping b&c and (new) a modified at beginning
646 # Commit E: merge B&C, keeping b&c and (new) a modified at end
648 # Merging commits D & E should result in no conflict; doing so correctly
649 # requires getting the virtual merge base (from merging B&C) right, handling
650 # renaming carefully (both in the virtual merge base and later), and getting
651 # content merge handled.
653 test_expect_success
'setup criss-cross + rename/rename/add + modify/modify' '
659 printf "lots\nof\nwords\nand\ncontent\n" >a &&
664 git checkout -b B A &&
668 git checkout -b C A &&
670 printf "2\n3\n4\n5\n6\n7\n" >a &&
675 git merge --no-commit -s ours C^0 &&
676 git checkout C -- a c &&
682 git commit -m "Merge commit C^0 into HEAD" &&
686 git merge --no-commit -s ours B^0 &&
687 git checkout B -- b &&
690 git commit -m "Merge commit B^0 into HEAD" &&
694 test_expect_failure
'detect rename/rename/add-source for virtual merge-base' '
697 git merge -s recursive E^0 &&
699 test 3 -eq $(git ls-files -s | wc -l) &&
700 test 0 -eq $(git ls-files -u | wc -l) &&
701 test 0 -eq $(git ls-files -o | wc -l) &&
703 test $(git rev-parse HEAD:b) = $(git rev-parse A:a) &&
704 test $(git rev-parse HEAD:c) = $(git rev-parse A:a) &&
705 test "$(cat a)" = "$(printf "1\n2\n3\n4\n5\n6\n7\n8\n")"
709 # criss-cross with rename/rename(1to2)/add-dest + simple modify:
719 # Commit A: new file: a
720 # Commit B: rename a->b, add c
721 # Commit C: rename a->c
722 # Commit D: merge B&C, keeping A:a and B:c
723 # Commit E: merge B&C, keeping A:a and slightly modified c from B
725 # Merging commits D & E should result in no conflict. The virtual merge
726 # base of B & C needs to not delete B:c for that to work, though...
728 test_expect_success
'setup criss-cross+rename/rename/add-dest + simple modify' '
739 git checkout -b B A &&
741 printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
745 git checkout -b C A &&
750 git merge --no-commit -s ours C^0 &&
752 git commit -m "D is like B but renames b back to a" &&
756 git merge --no-commit -s ours C^0 &&
760 git commit -m "E like D but has mod in c" &&
764 test_expect_success
'virtual merge base handles rename/rename(1to2)/add-dest' '
767 git merge -s recursive E^0 &&
769 test 2 -eq $(git ls-files -s | wc -l) &&
770 test 0 -eq $(git ls-files -u | wc -l) &&
771 test 0 -eq $(git ls-files -o | wc -l) &&
773 test $(git rev-parse HEAD:a) = $(git rev-parse A:a) &&
774 test $(git rev-parse HEAD:c) = $(git rev-parse E:c)