3 test_description='recursive merge corner cases involving criss-cross merges'
6 . "$TEST_DIRECTORY"/lib-merge.sh
18 test_expect_success 'setup basic criss-cross + rename with no modifications' '
19 test_create_repo basic-rename &&
23 ten="0 1 2 3 4 5 6 7 8 9" &&
26 echo line $i in a sample file
30 echo line $i in another sample file
33 test_tick && git commit -m initial &&
38 test_tick && git commit -m R1 &&
42 test_tick && git commit -m L1 &&
45 test_tick && git merge -s ours R1 &&
49 test_tick && git merge -s ours L1 &&
54 test_expect_success 'merge simple rename+criss-cross with no modifications' '
61 test_must_fail git merge -s recursive R2^0 &&
63 git ls-files -s >out &&
64 test_line_count = 5 out &&
65 git ls-files -u >out &&
66 test_line_count = 3 out &&
67 git ls-files -o >out &&
68 test_line_count = 1 out &&
70 git rev-parse >expect \
72 git rev-parse >actual \
74 test_cmp expect actual
79 # Same as before, but modify L1 slightly:
90 test_expect_success 'setup criss-cross + rename merges with basic modification' '
91 test_create_repo rename-modify &&
95 ten="0 1 2 3 4 5 6 7 8 9" &&
98 echo line $i in a sample file
102 echo line $i in another sample file
105 test_tick && git commit -m initial &&
108 git checkout -b R1 &&
112 test_tick && git commit -m R1 &&
116 test_tick && git commit -m L1 &&
119 test_tick && git merge -s ours R1 &&
123 test_tick && git merge -s ours L1 &&
128 test_expect_success 'merge criss-cross + rename merges with basic modification' '
134 test_must_fail git merge -s recursive R2^0 &&
136 git ls-files -s >out &&
137 test_line_count = 5 out &&
138 git ls-files -u >out &&
139 test_line_count = 3 out &&
140 git ls-files -o >out &&
141 test_line_count = 1 out &&
143 git rev-parse >expect \
145 git rev-parse >actual \
147 test_cmp expect actual
152 # For the next test, we start with three commits in two lines of development
153 # which setup a rename/add conflict:
154 # Commit A: File 'a' exists
155 # Commit B: Rename 'a' -> 'new_a'
156 # Commit C: Modify 'a', create different 'new_a'
157 # Later, two different people merge and resolve differently:
158 # Commit D: Merge B & C, ignoring separately created 'new_a'
159 # Commit E: Merge B & C making use of some piece of secondary 'new_a'
160 # Finally, someone goes to merge D & E. Does git detect the conflict?
171 test_expect_success 'setup differently handled merges of rename/add conflict' '
172 test_create_repo rename-add &&
176 printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
178 test_tick && git commit -m A &&
183 test_write_lines 0 1 2 3 4 5 6 7 foobar >new_a &&
185 test_tick && git commit -m C &&
189 test_tick && git commit -m B &&
192 test_must_fail git merge C &&
193 git show :2:new_a >new_a &&
195 test_tick && git commit -m D &&
199 test_must_fail git merge B &&
200 test_write_lines 0 1 2 3 4 5 6 7 bad_merge >new_a &&
202 test_tick && git commit -m E &&
207 test_expect_success 'git detects differently handled merges conflict' '
213 test_must_fail git merge -s recursive E^0 &&
215 git ls-files -s >out &&
216 test_line_count = 3 out &&
217 git ls-files -u >out &&
218 test_line_count = 3 out &&
219 git ls-files -o >out &&
220 test_line_count = 1 out &&
222 git cat-file -p C:new_a >ours &&
223 git cat-file -p C:a >theirs &&
225 test_must_fail git merge-file \
226 -L "Temporary merge branch 1" \
228 -L "Temporary merge branch 2" \
230 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
231 git hash-object ours-tweaked >expect &&
232 git rev-parse >>expect \
234 git rev-parse >actual \
235 :1:new_a :2:new_a :3:new_a &&
236 test_cmp expect actual &&
238 # Test that the two-way merge in new_a is as expected
239 git cat-file -p D:new_a >ours &&
240 git cat-file -p E:new_a >theirs &&
242 test_must_fail git merge-file \
247 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
248 git hash-object new_a >actual &&
249 git hash-object ours >expect &&
250 test_cmp expect actual
254 # Repeat the above testcase with precisely the same setup, other than with
255 # the two merge bases having different orderings of commit timestamps so
256 # that they are reversed in the order they are provided to merge-recursive,
257 # so that we can improve code coverage.
258 test_expect_success 'git detects differently handled merges conflict, swapped' '
262 # Difference #1: Do cleanup from previous testrun
266 # Difference #2: Change commit timestamps
267 btime=$(git log --no-walk --date=raw --format=%cd B | awk "{print \$1}") &&
268 ctime=$(git log --no-walk --date=raw --format=%cd C | awk "{print \$1}") &&
269 newctime=$(($btime+1)) &&
270 git fast-export --no-data --all | sed -e s/$ctime/$newctime/ | git fast-import --force --quiet &&
271 # End of most differences; rest is copy-paste of last test,
272 # other than swapping C:a and C:new_a due to order switch
275 test_must_fail git merge -s recursive E^0 &&
277 git ls-files -s >out &&
278 test_line_count = 3 out &&
279 git ls-files -u >out &&
280 test_line_count = 3 out &&
281 git ls-files -o >out &&
282 test_line_count = 1 out &&
284 git cat-file -p C:a >ours &&
285 git cat-file -p C:new_a >theirs &&
287 test_must_fail git merge-file \
288 -L "Temporary merge branch 1" \
290 -L "Temporary merge branch 2" \
292 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
293 git hash-object ours-tweaked >expect &&
294 git rev-parse >>expect \
296 git rev-parse >actual \
297 :1:new_a :2:new_a :3:new_a &&
298 test_cmp expect actual &&
300 # Test that the two-way merge in new_a is as expected
301 git cat-file -p D:new_a >ours &&
302 git cat-file -p E:new_a >theirs &&
304 test_must_fail git merge-file \
309 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
310 git hash-object new_a >actual &&
311 git hash-object ours >expect &&
312 test_cmp expect actual
317 # criss-cross + modify/delete:
327 # Commit A: file with contents 'A\n'
328 # Commit B: file with contents 'B\n'
329 # Commit C: file not present
330 # Commit D: file with contents 'B\n'
331 # Commit E: file not present
333 # Merging commits D & E should result in modify/delete conflict.
335 test_expect_success 'setup criss-cross + modify/delete resolved differently' '
336 test_create_repo modify-delete &&
358 test_must_fail git merge C &&
366 test_must_fail git merge B &&
374 test_expect_success 'git detects conflict merging criss-cross+modify/delete' '
380 test_must_fail git merge -s recursive E^0 &&
382 git ls-files -s >out &&
383 test_line_count = 2 out &&
384 git ls-files -u >out &&
385 test_line_count = 2 out &&
387 git rev-parse >expect \
388 master:file B:file &&
389 git rev-parse >actual \
391 test_cmp expect actual
395 test_expect_success 'git detects conflict merging criss-cross+modify/delete, reverse direction' '
402 test_must_fail git merge -s recursive D^0 &&
404 git ls-files -s >out &&
405 test_line_count = 2 out &&
406 git ls-files -u >out &&
407 test_line_count = 2 out &&
409 git rev-parse >expect \
410 master:file B:file &&
411 git rev-parse >actual \
413 test_cmp expect actual
417 # SORRY FOR THE SUPER LONG DESCRIPTION, BUT THIS NEXT ONE IS HAIRY
419 # criss-cross + d/f conflict via add/add:
420 # Commit A: Neither file 'a' nor directory 'a/' exists.
421 # Commit B: Introduce 'a'
422 # Commit C: Introduce 'a/file'
423 # Commit D1: Merge B & C, keeping 'a' and deleting 'a/'
424 # Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
434 # I'll describe D2, E2, & E3 (which are alternatives for D1 & E1) more below...
436 # Merging D1 & E1 requires we first create a virtual merge base X from
437 # merging A & B in memory. There are several possibilities for the merge-base:
438 # 1: Keep both 'a' and 'a/file' (assuming crazy filesystem allowing a tree
439 # with a directory and file at same path): results in merge of D1 & E1
440 # being clean with both files deleted. Bad (no conflict detected).
441 # 2: Keep 'a' but not 'a/file': Merging D1 & E1 is clean and matches E1. Bad.
442 # 3: Keep 'a/file' but not 'a': Merging D1 & E1 is clean and matches D1. Bad.
443 # 4: Keep neither file: Merging D1 & E1 reports the D/F add/add conflict.
445 # So 4 sounds good for this case, but if we were to merge D1 & E3, where E3
447 # Commit E3: Merge B & C, keeping modified a, and deleting a/
448 # then we'd get an add/add conflict for 'a', which seems suboptimal. A little
449 # creativity leads us to an alternate choice:
450 # 5: Keep 'a' as 'a~$UNIQUE' and a/file; results:
451 # Merge D1 & E1: rename/delete conflict for 'a'; a/file silently deleted
452 # Merge D1 & E3 is clean, as expected.
454 # So choice 5 at least provides some kind of conflict for the original case,
455 # and can merge cleanly as expected with D1 and E3. It also made things just
456 # slightly funny for merging D1 and E4, where E4 is defined as:
457 # Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
458 # in this case, we'll get a rename/rename(1to2) conflict because a~$UNIQUE
459 # gets renamed to 'a' in D1 and to 'a2' in E4. But that's better than having
460 # two files (both 'a' and 'a2') sitting around without the user being notified
461 # that we could detect they were related and need to be merged. Also, choice
462 # 5 makes the handling of 'a/file' seem suboptimal. What if we were to merge
463 # D2 and E4, where D2 is:
464 # Commit D2: Merge B & C, renaming 'a'->'a2', keeping 'a/file'
465 # This would result in a clean merge with 'a2' having three-way merged
466 # contents (good), and deleting 'a/' (bad) -- it doesn't detect the
467 # conflict in how the different sides treated a/file differently.
468 # Continuing down the creative route:
469 # 6: Keep 'a' as 'a~$UNIQUE1' and keep 'a/' as 'a~$UNIQUE2/'; results:
470 # Merge D1 & E1: rename/delete conflict for 'a' and each path under 'a/'.
471 # Merge D1 & E3: clean, as expected.
472 # Merge D1 & E4: rename/rename(1to2) conflict on 'a' vs 'a2'.
473 # Merge D2 & E4: clean for 'a2', rename/delete for a/file
475 # Choice 6 could cause rename detection to take longer (providing more targets
476 # that need to be searched). Also, the conflict message for each path under
477 # 'a/' might be annoying unless we can detect it at the directory level, print
478 # it once, and then suppress it for individual filepaths underneath.
481 # As of time of writing, git uses choice 5. Directory rename detection and
482 # rename detection performance improvements might make choice 6 a desirable
483 # improvement. But we can at least document where we fall short for now...
486 # Historically, this testcase also used:
487 # Commit E2: Merge B & C, deleting 'a' but keeping slightly modified 'a/file'
488 # The merge of D1 & E2 is very similar to D1 & E1 -- it has similar issues for
489 # path 'a', but should always result in a modify/delete conflict for path
490 # 'a/file'. These tests ran the two merges
493 # in both directions, to check for directional issues with D/F conflict
494 # handling. Later we added
498 # for good measure, though we only ran those one way because we had pretty
499 # good confidence in merge-recursive's directional handling of D/F issues.
501 # Just to summarize all the intermediate merge commits:
502 # Commit D1: Merge B & C, keeping a and deleting a/
503 # Commit D2: Merge B & C, renaming a->a2, keeping a/file
504 # Commit E1: Merge B & C, deleting a but keeping a/file
505 # Commit E2: Merge B & C, deleting a but keeping slightly modified a/file
506 # Commit E3: Merge B & C, keeping modified a, and deleting a/
507 # Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
510 test_expect_success 'setup differently handled merges of directory/file conflict' '
511 test_create_repo directory-file &&
524 test_write_lines a b c d e f g >a/file &&
530 test_write_lines 1 2 3 4 5 6 7 >a &&
536 git merge -s ours -m D1 C^0 &&
540 test_must_fail git merge C^0 &&
544 git cat-file -p B:a >a2 &&
550 git merge -s ours -m E1 B^0 &&
554 git merge -s ours -m E2 B^0 &&
555 test_write_lines a b c d e f g h >a/file &&
557 git commit --amend -C HEAD &&
561 test_must_fail git merge B^0 &&
564 test_write_lines 1 2 3 4 5 6 7 8 >a &&
570 test_must_fail git merge B^0 &&
574 test_write_lines 1 2 3 4 5 6 7 8 >a2 &&
581 test_expect_success 'merge of D1 & E1 fails but has appropriate contents' '
582 test_when_finished "git -C directory-file reset --hard" &&
583 test_when_finished "git -C directory-file clean -fdqx" &&
589 test_must_fail git merge -s recursive E1^0 &&
591 git ls-files -s >out &&
592 test_line_count = 2 out &&
593 git ls-files -u >out &&
594 test_line_count = 1 out &&
595 git ls-files -o >out &&
596 test_line_count = 1 out &&
598 git rev-parse >expect \
600 git rev-parse >actual \
602 test_cmp expect actual
606 test_expect_success 'merge of E1 & D1 fails but has appropriate contents' '
607 test_when_finished "git -C directory-file reset --hard" &&
608 test_when_finished "git -C directory-file clean -fdqx" &&
614 test_must_fail git merge -s recursive D1^0 &&
616 git ls-files -s >out &&
617 test_line_count = 2 out &&
618 git ls-files -u >out &&
619 test_line_count = 1 out &&
620 git ls-files -o >out &&
621 test_line_count = 1 out &&
623 git rev-parse >expect \
625 git rev-parse >actual \
627 test_cmp expect actual
631 test_expect_success 'merge of D1 & E2 fails but has appropriate contents' '
632 test_when_finished "git -C directory-file reset --hard" &&
633 test_when_finished "git -C directory-file clean -fdqx" &&
639 test_must_fail git merge -s recursive E2^0 &&
641 git ls-files -s >out &&
642 test_line_count = 4 out &&
643 git ls-files -u >out &&
644 test_line_count = 3 out &&
645 git ls-files -o >out &&
646 test_line_count = 2 out &&
648 git rev-parse >expect \
649 B:a E2:a/file C:a/file A:ignore-me &&
650 git rev-parse >actual \
651 :2:a :3:a/file :1:a/file :0:ignore-me &&
652 test_cmp expect actual &&
654 test_path_is_file a~HEAD
658 test_expect_success 'merge of E2 & D1 fails but has appropriate contents' '
659 test_when_finished "git -C directory-file reset --hard" &&
660 test_when_finished "git -C directory-file clean -fdqx" &&
666 test_must_fail git merge -s recursive D1^0 &&
668 git ls-files -s >out &&
669 test_line_count = 4 out &&
670 git ls-files -u >out &&
671 test_line_count = 3 out &&
672 git ls-files -o >out &&
673 test_line_count = 2 out &&
675 git rev-parse >expect \
676 B:a E2:a/file C:a/file A:ignore-me &&
677 git rev-parse >actual \
678 :3:a :2:a/file :1:a/file :0:ignore-me &&
679 test_cmp expect actual &&
681 test_path_is_file a~D1^0
685 test_expect_success 'merge of D1 & E3 succeeds' '
686 test_when_finished "git -C directory-file reset --hard" &&
687 test_when_finished "git -C directory-file clean -fdqx" &&
693 git merge -s recursive E3^0 &&
695 git ls-files -s >out &&
696 test_line_count = 2 out &&
697 git ls-files -u >out &&
698 test_line_count = 0 out &&
699 git ls-files -o >out &&
700 test_line_count = 1 out &&
702 git rev-parse >expect \
704 git rev-parse >actual \
706 test_cmp expect actual
710 test_expect_success 'merge of D1 & E4 notifies user a and a2 are related' '
711 test_when_finished "git -C directory-file reset --hard" &&
712 test_when_finished "git -C directory-file clean -fdqx" &&
718 test_must_fail git merge -s recursive E4^0 &&
720 git ls-files -s >out &&
721 test_line_count = 4 out &&
722 git ls-files -u >out &&
723 test_line_count = 3 out &&
724 git ls-files -o >out &&
725 test_line_count = 1 out &&
727 git rev-parse >expect \
728 A:ignore-me B:a D1:a E4:a2 &&
729 git rev-parse >actual \
730 :0:ignore-me :1:a~Temporary\ merge\ branch\ 2 :2:a :3:a2 &&
731 test_cmp expect actual
735 test_expect_failure 'merge of D2 & E4 merges a2s & reports conflict for a/file' '
736 test_when_finished "git -C directory-file reset --hard" &&
737 test_when_finished "git -C directory-file clean -fdqx" &&
743 test_must_fail git merge -s recursive E4^0 &&
745 git ls-files -s >out &&
746 test_line_count = 3 out &&
747 git ls-files -u >out &&
748 test_line_count = 1 out &&
749 git ls-files -o >out &&
750 test_line_count = 1 out &&
752 git rev-parse >expect \
753 A:ignore-me E4:a2 D2:a/file &&
754 git rev-parse >actual \
755 :0:ignore-me :0:a2 :2:a/file &&
756 test_cmp expect actual
761 # criss-cross with rename/rename(1to2)/modify followed by
762 # rename/rename(2to1)/modify:
772 # Commit A: new file: a
773 # Commit B: rename a->b, modifying by adding a line
774 # Commit C: rename a->c
775 # Commit D: merge B&C, resolving conflict by keeping contents in newname
776 # Commit E: merge B&C, resolving conflict similar to D but adding another line
778 # There is a conflict merging B & C, but one of filename not of file
779 # content. Whoever created D and E chose specific resolutions for that
780 # conflict resolution. Now, since: (1) there is no content conflict
781 # merging B & C, (2) D does not modify that merged content further, and (3)
782 # both D & E resolve the name conflict in the same way, the modification to
783 # newname in E should not cause any conflicts when it is merged with D.
784 # (Note that this can be accomplished by having the virtual merge base have
785 # the merged contents of b and c stored in a file named a, which seems like
786 # the most logical choice anyway.)
788 # Comment from Junio: I do not necessarily agree with the choice "a", but
789 # it feels sound to say "B and C do not agree what the final pathname
790 # should be, but we know this content was derived from the common A:a so we
791 # use one path whose name is arbitrary in the virtual merge base X between
792 # D and E" and then further let the rename detection to notice that that
793 # arbitrary path gets renamed between X-D to "newname" and X-E also to
794 # "newname" to resolve it as both sides renaming it to the same new
795 # name. It is akin to what we do at the content level, i.e. "B and C do not
796 # agree what the final contents should be, so we leave the conflict marker
797 # but that may cancel out at the final merge stage".
799 test_expect_success 'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
800 test_create_repo rename-squared-squared &&
802 cd rename-squared-squared &&
804 printf "1\n2\n3\n4\n5\n6\n" >a &&
809 git checkout -b B A &&
815 git checkout -b C A &&
819 git checkout -q B^0 &&
820 git merge --no-commit -s ours C^0 &&
822 git commit -m "Merge commit C^0 into HEAD" &&
825 git checkout -q C^0 &&
826 git merge --no-commit -s ours B^0 &&
828 printf "7\n8\n" >>newname &&
830 git commit -m "Merge commit B^0 into HEAD" &&
835 test_expect_success 'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
837 cd rename-squared-squared &&
841 git merge -s recursive E^0 &&
843 git ls-files -s >out &&
844 test_line_count = 1 out &&
845 git ls-files -u >out &&
846 test_line_count = 0 out &&
847 git ls-files -o >out &&
848 test_line_count = 1 out &&
850 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
855 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
865 # Commit A: new file: a
866 # Commit B: rename a->b
867 # Commit C: rename a->c, add different a
868 # Commit D: merge B&C, keeping b&c and (new) a modified at beginning
869 # Commit E: merge B&C, keeping b&c and (new) a modified at end
871 # Merging commits D & E should result in no conflict; doing so correctly
872 # requires getting the virtual merge base (from merging B&C) right, handling
873 # renaming carefully (both in the virtual merge base and later), and getting
874 # content merge handled.
876 test_expect_success 'setup criss-cross + rename/rename/add-source + modify/modify' '
877 test_create_repo rename-rename-add-source &&
879 cd rename-rename-add-source &&
881 printf "lots\nof\nwords\nand\ncontent\n" >a &&
886 git checkout -b B A &&
890 git checkout -b C A &&
892 printf "2\n3\n4\n5\n6\n7\n" >a &&
897 git merge --no-commit -s ours C^0 &&
898 git checkout C -- a c &&
904 git commit -m "Merge commit C^0 into HEAD" &&
908 git merge --no-commit -s ours B^0 &&
909 git checkout B -- b &&
912 git commit -m "Merge commit B^0 into HEAD" &&
917 test_expect_failure 'detect rename/rename/add-source for virtual merge-base' '
919 cd rename-rename-add-source &&
923 git merge -s recursive E^0 &&
925 git ls-files -s >out &&
926 test_line_count = 3 out &&
927 git ls-files -u >out &&
928 test_line_count = 0 out &&
929 git ls-files -o >out &&
930 test_line_count = 1 out &&
932 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >correct &&
933 git rev-parse >expect \
936 git rev-parse >actual \
938 git hash-object >>actual \
940 test_cmp expect actual
945 # criss-cross with rename/rename(1to2)/add-dest + simple modify:
955 # Commit A: new file: a
956 # Commit B: rename a->b, add c
957 # Commit C: rename a->c
958 # Commit D: merge B&C, keeping A:a and B:c
959 # Commit E: merge B&C, keeping A:a and slightly modified c from B
961 # Merging commits D & E should result in no conflict. The virtual merge
962 # base of B & C needs to not delete B:c for that to work, though...
964 test_expect_success 'setup criss-cross+rename/rename/add-dest + simple modify' '
965 test_create_repo rename-rename-add-dest &&
967 cd rename-rename-add-dest &&
974 git checkout -b B A &&
976 printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
980 git checkout -b C A &&
985 git merge --no-commit -s ours C^0 &&
987 git commit -m "D is like B but renames b back to a" &&
991 git merge --no-commit -s ours C^0 &&
995 git commit -m "E like D but has mod in c" &&
1000 test_expect_success 'virtual merge base handles rename/rename(1to2)/add-dest' '
1002 cd rename-rename-add-dest &&
1006 git merge -s recursive E^0 &&
1008 git ls-files -s >out &&
1009 test_line_count = 2 out &&
1010 git ls-files -u >out &&
1011 test_line_count = 0 out &&
1012 git ls-files -o >out &&
1013 test_line_count = 1 out &&
1015 git rev-parse >expect \
1017 git rev-parse >actual \
1019 test_cmp expect actual
1024 # criss-cross with modify/modify on a symlink:
1034 # Commit A: simple simlink fickle->lagoon
1035 # Commit B: redirect fickle->disneyland
1036 # Commit C: redirect fickle->home
1037 # Commit D: merge B&C, resolving in favor of B
1038 # Commit E: merge B&C, resolving in favor of C
1040 # This is an obvious modify/modify conflict for the symlink 'fickle'. Can
1043 test_expect_success 'setup symlink modify/modify' '
1044 test_create_repo symlink-modify-modify &&
1046 cd symlink-modify-modify &&
1048 test_ln_s_add lagoon fickle &&
1052 git checkout -b B A &&
1054 test_ln_s_add disneyland fickle &&
1057 git checkout -b C A &&
1059 test_ln_s_add home fickle &&
1063 git checkout -q B^0 &&
1064 git merge -s ours -m D C^0 &&
1067 git checkout -q C^0 &&
1068 git merge -s ours -m E B^0 &&
1073 test_expect_merge_algorithm failure success 'check symlink modify/modify' '
1075 cd symlink-modify-modify &&
1079 test_must_fail git merge -s recursive E^0 &&
1081 git ls-files -s >out &&
1082 test_line_count = 3 out &&
1083 git ls-files -u >out &&
1084 test_line_count = 3 out &&
1085 git ls-files -o >out &&
1086 test_line_count = 1 out
1091 # criss-cross with add/add of a symlink:
1101 # Commit A: No symlink or path exists yet
1102 # Commit B: set up symlink: fickle->disneyland
1103 # Commit C: set up symlink: fickle->home
1104 # Commit D: merge B&C, resolving in favor of B
1105 # Commit E: merge B&C, resolving in favor of C
1107 # This is an obvious add/add conflict for the symlink 'fickle'. Can
1110 test_expect_success 'setup symlink add/add' '
1111 test_create_repo symlink-add-add &&
1113 cd symlink-add-add &&
1120 git checkout -b B A &&
1121 test_ln_s_add disneyland fickle &&
1124 git checkout -b C A &&
1125 test_ln_s_add home fickle &&
1129 git checkout -q B^0 &&
1130 git merge -s ours -m D C^0 &&
1133 git checkout -q C^0 &&
1134 git merge -s ours -m E B^0 &&
1139 test_expect_merge_algorithm failure success 'check symlink add/add' '
1141 cd symlink-add-add &&
1145 test_must_fail git merge -s recursive E^0 &&
1147 git ls-files -s >out &&
1148 test_line_count = 3 out &&
1149 git ls-files -u >out &&
1150 test_line_count = 2 out &&
1151 git ls-files -o >out &&
1152 test_line_count = 1 out
1157 # criss-cross with modify/modify on a submodule:
1167 # Commit A: simple submodule repo
1168 # Commit B: update repo
1169 # Commit C: update repo differently
1170 # Commit D: merge B&C, resolving in favor of B
1171 # Commit E: merge B&C, resolving in favor of C
1173 # This is an obvious modify/modify conflict for the submodule 'repo'. Can
1176 test_expect_success 'setup submodule modify/modify' '
1177 test_create_repo submodule-modify-modify &&
1179 cd submodule-modify-modify &&
1181 test_create_repo submod &&
1189 git checkout -b B A &&
1195 git checkout -b C A &&
1202 git -C submod reset --hard A &&
1207 git checkout -b B A &&
1208 git -C submod reset --hard B &&
1212 git checkout -b C A &&
1213 git -C submod reset --hard C &&
1217 git checkout -q B^0 &&
1218 git merge -s ours -m D C^0 &&
1221 git checkout -q C^0 &&
1222 git merge -s ours -m E B^0 &&
1227 test_expect_merge_algorithm failure success 'check submodule modify/modify' '
1229 cd submodule-modify-modify &&
1233 test_must_fail git merge -s recursive E^0 &&
1235 git ls-files -s >out &&
1236 test_line_count = 3 out &&
1237 git ls-files -u >out &&
1238 test_line_count = 3 out &&
1239 git ls-files -o >out &&
1240 test_line_count = 1 out
1245 # criss-cross with add/add on a submodule:
1255 # Commit A: nothing of note
1256 # Commit B: introduce submodule repo
1257 # Commit C: introduce submodule repo at different commit
1258 # Commit D: merge B&C, resolving in favor of B
1259 # Commit E: merge B&C, resolving in favor of C
1261 # This is an obvious add/add conflict for the submodule 'repo'. Can
1264 test_expect_success 'setup submodule add/add' '
1265 test_create_repo submodule-add-add &&
1267 cd submodule-add-add &&
1269 test_create_repo submod &&
1277 git checkout -b B A &&
1283 git checkout -b C A &&
1290 touch irrelevant-file &&
1291 git add irrelevant-file &&
1295 git checkout -b B A &&
1296 git -C submod reset --hard B &&
1300 git checkout -b C A &&
1301 git -C submod reset --hard C &&
1305 git checkout -q B^0 &&
1306 git merge -s ours -m D C^0 &&
1309 git checkout -q C^0 &&
1310 git merge -s ours -m E B^0 &&
1315 test_expect_merge_algorithm failure success 'check submodule add/add' '
1317 cd submodule-add-add &&
1321 test_must_fail git merge -s recursive E^0 &&
1323 git ls-files -s >out &&
1324 test_line_count = 3 out &&
1325 git ls-files -u >out &&
1326 test_line_count = 2 out &&
1327 git ls-files -o >out &&
1328 test_line_count = 1 out
1333 # criss-cross with conflicting entry types:
1343 # Commit A: nothing of note
1344 # Commit B: introduce submodule 'path'
1345 # Commit C: introduce symlink 'path'
1346 # Commit D: merge B&C, resolving in favor of B
1347 # Commit E: merge B&C, resolving in favor of C
1349 # This is an obvious add/add conflict for 'path'. Can git detect it?
1351 test_expect_success 'setup conflicting entry types (submodule vs symlink)' '
1352 test_create_repo submodule-symlink-add-add &&
1354 cd submodule-symlink-add-add &&
1356 test_create_repo path &&
1365 touch irrelevant-file &&
1366 git add irrelevant-file &&
1370 git checkout -b B A &&
1371 git -C path reset --hard B &&
1375 git checkout -b C A &&
1377 test_ln_s_add irrelevant-file path &&
1380 git checkout -q B^0 &&
1381 git merge -s ours -m D C^0 &&
1384 git checkout -q C^0 &&
1385 git merge -s ours -m E B^0 &&
1390 test_expect_merge_algorithm failure success 'check conflicting entry types (submodule vs symlink)' '
1392 cd submodule-symlink-add-add &&
1396 test_must_fail git merge -s recursive E^0 &&
1398 git ls-files -s >out &&
1399 test_line_count = 3 out &&
1400 git ls-files -u >out &&
1401 test_line_count = 2 out &&
1402 git ls-files -o >out &&
1403 test_line_count = 1 out
1408 # criss-cross with regular files that have conflicting modes:
1418 # Commit A: nothing of note
1419 # Commit B: introduce file source_me.bash, not executable
1420 # Commit C: introduce file source_me.bash, executable
1421 # Commit D: merge B&C, resolving in favor of B
1422 # Commit E: merge B&C, resolving in favor of C
1424 # This is an obvious add/add mode conflict. Can git detect it?
1426 test_expect_success 'setup conflicting modes for regular file' '
1427 test_create_repo regular-file-mode-conflict &&
1429 cd regular-file-mode-conflict &&
1431 touch irrelevant-file &&
1432 git add irrelevant-file &&
1436 git checkout -b B A &&
1437 echo "command_to_run" >source_me.bash &&
1438 git add source_me.bash &&
1441 git checkout -b C A &&
1442 echo "command_to_run" >source_me.bash &&
1443 git add source_me.bash &&
1444 test_chmod +x source_me.bash &&
1447 git checkout -q B^0 &&
1448 git merge -s ours -m D C^0 &&
1451 git checkout -q C^0 &&
1452 git merge -s ours -m E B^0 &&
1457 test_expect_failure 'check conflicting modes for regular file' '
1459 cd regular-file-mode-conflict &&
1463 test_must_fail git merge -s recursive E^0 &&
1465 git ls-files -s >out &&
1466 test_line_count = 3 out &&
1467 git ls-files -u >out &&
1468 test_line_count = 2 out &&
1469 git ls-files -o >out &&
1470 test_line_count = 1 out
1482 # master has two files, named 'b' and 'a'
1483 # branches L1 and R1 both modify each of the two files in conflicting ways
1485 # L2 is a merge of R1 into L1; more on it later.
1486 # R2 is a merge of L1 into R1; more on it later.
1488 # X is an auto-generated merge-base used when merging L2 and R2.
1489 # since X is a merge of L1 and R1, it has conflicting versions of each file
1491 # More about L2 and R2:
1492 # - both resolve the conflicts in 'b' and 'a' differently
1493 # - L2 renames 'b' to 'm'
1494 # - R2 renames 'a' to 'm'
1496 # In the end, in file 'm' we have four different conflicting files (from
1497 # two versions of 'b' and two of 'a'). In addition, if
1498 # merge.conflictstyle is diff3, then the base version also has
1499 # conflict markers of its own, leading to a total of three levels of
1500 # conflict markers. This is a pretty weird corner case, but we just want
1501 # to ensure that we handle it as well as practical.
1503 test_expect_success 'setup nested conflicts' '
1504 test_create_repo nested_conflicts &&
1506 cd nested_conflicts &&
1508 # Create some related files now
1509 for i in $(test_seq 1 10)
1511 echo Random base content line $i
1523 test_write_lines b b_L1 >>b_L1 &&
1524 test_write_lines b b_R1 >>b_R1 &&
1525 test_write_lines b b_L2 >>b_L2 &&
1526 test_write_lines b b_R2 >>b_R2 &&
1527 test_write_lines a a_L1 >>a_L1 &&
1528 test_write_lines a a_R1 >>a_R1 &&
1529 test_write_lines a a_L2 >>a_L2 &&
1530 test_write_lines a a_R2 >>a_R2 &&
1532 # Setup original commit (or merge-base), consisting of
1533 # files named "b" and "a"
1539 test_tick && git commit -m initial &&
1544 # Handle the left side
1549 test_tick && git commit -m "version L1 of files" &&
1552 # Handle the right side
1557 test_tick && git commit -m "version R1 of files" &&
1560 # Create first merge on left side
1562 test_must_fail git merge R1 &&
1567 test_tick && git commit -m "left merge, rename b->m" &&
1570 # Create first merge on right side
1572 test_must_fail git merge L1 &&
1577 test_tick && git commit -m "right merge, rename a->m" &&
1582 test_expect_success 'check nested conflicts' '
1584 cd nested_conflicts &&
1587 MASTER=$(git rev-parse --short master) &&
1588 git checkout L2^0 &&
1590 # Merge must fail; there is a conflict
1591 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R2^0 &&
1593 # Make sure the index has the right number of entries
1594 git ls-files -s >out &&
1595 test_line_count = 2 out &&
1596 git ls-files -u >out &&
1597 test_line_count = 2 out &&
1598 # Ensure we have the correct number of untracked files
1599 git ls-files -o >out &&
1600 test_line_count = 1 out &&
1602 # Create a and b from virtual merge base X
1603 git cat-file -p master:a >base &&
1604 git cat-file -p L1:a >ours &&
1605 git cat-file -p R1:a >theirs &&
1606 test_must_fail git merge-file --diff3 \
1607 -L "Temporary merge branch 1" \
1609 -L "Temporary merge branch 2" \
1613 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_a &&
1615 git cat-file -p master:b >base &&
1616 git cat-file -p L1:b >ours &&
1617 git cat-file -p R1:b >theirs &&
1618 test_must_fail git merge-file --diff3 \
1619 -L "Temporary merge branch 1" \
1621 -L "Temporary merge branch 2" \
1625 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_b &&
1627 # Compare :2:m to expected values
1628 git cat-file -p L2:m >ours &&
1629 git cat-file -p R2:b >theirs &&
1630 test_must_fail git merge-file --diff3 \
1632 -L "merged common ancestors:b" \
1637 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_2 &&
1638 git cat-file -p :2:m >actual &&
1639 test_cmp m_stage_2 actual &&
1641 # Compare :3:m to expected values
1642 git cat-file -p L2:a >ours &&
1643 git cat-file -p R2:m >theirs &&
1644 test_must_fail git merge-file --diff3 \
1646 -L "merged common ancestors:a" \
1651 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_3 &&
1652 git cat-file -p :3:m >actual &&
1653 test_cmp m_stage_3 actual &&
1655 # Compare m to expected contents
1657 cp m_stage_2 expected_final_m &&
1658 test_must_fail git merge-file --diff3 \
1660 -L "merged common ancestors" \
1665 test_cmp expected_final_m m
1677 # master has one file named 'content'
1678 # branches L1 and R1 both modify each of the two files in conflicting ways
1680 # L<n> (n>1) is a merge of R<n-1> into L<n-1>
1681 # R<n> (n>1) is a merge of L<n-1> into R<n-1>
1682 # L<n> and R<n> resolve the conflicts differently.
1684 # X<n> is an auto-generated merge-base used when merging L<n+1> and R<n+1>.
1685 # By construction, X1 has conflict markers due to conflicting versions.
1686 # X2, due to using merge.conflictstyle=3, has nested conflict markers.
1688 # So, merging R3 into L3 using merge.conflictstyle=3 should show the
1689 # nested conflict markers from X2 in the base version -- that means we
1690 # have three levels of conflict markers. Can we distinguish all three?
1692 test_expect_success 'setup virtual merge base with nested conflicts' '
1693 test_create_repo virtual_merge_base_has_nested_conflicts &&
1695 cd virtual_merge_base_has_nested_conflicts &&
1697 # Create some related files now
1698 for i in $(test_seq 1 10)
1700 echo Random base content line $i
1703 # Setup original commit
1705 test_tick && git commit -m initial &&
1712 echo left >>content &&
1714 test_tick && git commit -m "version L1 of content" &&
1719 echo right >>content &&
1721 test_tick && git commit -m "version R1 of content" &&
1726 test_must_fail git -c merge.conflictstyle=diff3 merge R1 &&
1727 git checkout L1 content &&
1728 test_tick && git commit -m "version L2 of content" &&
1733 test_must_fail git -c merge.conflictstyle=diff3 merge L1 &&
1734 git checkout R1 content &&
1735 test_tick && git commit -m "version R2 of content" &&
1740 test_must_fail git -c merge.conflictstyle=diff3 merge R2 &&
1741 git checkout L1 content &&
1742 test_tick && git commit -m "version L3 of content" &&
1747 test_must_fail git -c merge.conflictstyle=diff3 merge L2 &&
1748 git checkout R1 content &&
1749 test_tick && git commit -m "version R3 of content" &&
1754 test_expect_success 'check virtual merge base with nested conflicts' '
1756 cd virtual_merge_base_has_nested_conflicts &&
1758 MASTER=$(git rev-parse --short master) &&
1759 git checkout L3^0 &&
1761 # Merge must fail; there is a conflict
1762 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R3^0 &&
1764 # Make sure the index has the right number of entries
1765 git ls-files -s >out &&
1766 test_line_count = 3 out &&
1767 git ls-files -u >out &&
1768 test_line_count = 3 out &&
1769 # Ensure we have the correct number of untracked files
1770 git ls-files -o >out &&
1771 test_line_count = 1 out &&
1773 # Compare :[23]:content to expected values
1774 git rev-parse L1:content R1:content >expect &&
1775 git rev-parse :2:content :3:content >actual &&
1776 test_cmp expect actual &&
1778 # Imitate X1 merge base, except without long enough conflict
1779 # markers because a subsequent sed will modify them. Put
1781 git cat-file -p master:content >base &&
1782 git cat-file -p L:content >left &&
1783 git cat-file -p R:content >right &&
1784 cp left merged-once &&
1785 test_must_fail git merge-file --diff3 \
1786 -L "Temporary merge branch 1" \
1788 -L "Temporary merge branch 2" \
1792 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-once >vmb &&
1794 # Imitate X2 merge base, overwriting vmb. Note that we
1795 # extend both sets of conflict markers to make them longer
1796 # with the sed command.
1797 cp left merged-twice &&
1798 test_must_fail git merge-file --diff3 \
1799 -L "Temporary merge branch 1" \
1800 -L "merged common ancestors" \
1801 -L "Temporary merge branch 2" \
1805 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-twice >vmb &&
1807 # Compare :1:content to expected value
1808 git cat-file -p :1:content >actual &&
1809 test_cmp vmb actual &&
1811 # Determine expected content in final outer merge, compare to
1812 # what the merge generated.
1813 cp -f left expect &&
1814 test_must_fail git merge-file --diff3 \
1815 -L "HEAD" -L "merged common ancestors" -L "R3^0" \
1817 test_cmp expect content