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 &&
541 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
550 git cat-file -p B:a >a2 &&
556 git merge -s ours -m E1 B^0 &&
560 git merge -s ours -m E2 B^0 &&
561 test_write_lines a b c d e f g h >a/file &&
563 git commit --amend -C HEAD &&
567 test_must_fail git merge B^0 &&
568 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
575 test_write_lines 1 2 3 4 5 6 7 8 >a &&
581 test_must_fail git merge B^0 &&
582 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
591 test_write_lines 1 2 3 4 5 6 7 8 >a2 &&
598 test_expect_success 'merge of D1 & E1 fails but has appropriate contents' '
599 test_when_finished "git -C directory-file reset --hard" &&
600 test_when_finished "git -C directory-file clean -fdqx" &&
606 test_must_fail git merge -s recursive E1^0 &&
608 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
610 git ls-files -s >out &&
611 test_line_count = 3 out &&
612 git ls-files -u >out &&
613 test_line_count = 2 out &&
614 git ls-files -o >out &&
615 test_line_count = 1 out &&
617 git rev-parse >expect \
618 A:ignore-me B:a D1:a &&
619 git rev-parse >actual \
620 :0:ignore-me :1:a :2:a &&
621 test_cmp expect actual
623 git ls-files -s >out &&
624 test_line_count = 2 out &&
625 git ls-files -u >out &&
626 test_line_count = 1 out &&
627 git ls-files -o >out &&
628 test_line_count = 1 out &&
630 git rev-parse >expect \
632 git rev-parse >actual \
634 test_cmp expect actual
639 test_expect_success 'merge of E1 & D1 fails but has appropriate contents' '
640 test_when_finished "git -C directory-file reset --hard" &&
641 test_when_finished "git -C directory-file clean -fdqx" &&
647 test_must_fail git merge -s recursive D1^0 &&
649 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
651 git ls-files -s >out &&
652 test_line_count = 3 out &&
653 git ls-files -u >out &&
654 test_line_count = 2 out &&
655 git ls-files -o >out &&
656 test_line_count = 1 out &&
658 git rev-parse >expect \
659 A:ignore-me B:a D1:a &&
660 git rev-parse >actual \
661 :0:ignore-me :1:a :3:a &&
662 test_cmp expect actual
664 git ls-files -s >out &&
665 test_line_count = 2 out &&
666 git ls-files -u >out &&
667 test_line_count = 1 out &&
668 git ls-files -o >out &&
669 test_line_count = 1 out &&
671 git rev-parse >expect \
673 git rev-parse >actual \
675 test_cmp expect actual
680 test_expect_success 'merge of D1 & E2 fails but has appropriate contents' '
681 test_when_finished "git -C directory-file reset --hard" &&
682 test_when_finished "git -C directory-file clean -fdqx" &&
688 test_must_fail git merge -s recursive E2^0 &&
690 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
692 git ls-files -s >out &&
693 test_line_count = 5 out &&
694 git ls-files -u >out &&
695 test_line_count = 4 out &&
696 git ls-files -o >out &&
697 test_line_count = 1 out &&
699 git rev-parse >expect \
700 B:a D1:a E2:a/file C:a/file A:ignore-me &&
701 git rev-parse >actual \
702 :1:a~HEAD :2:a~HEAD :3:a/file :1:a/file :0:ignore-me
704 git ls-files -s >out &&
705 test_line_count = 4 out &&
706 git ls-files -u >out &&
707 test_line_count = 3 out &&
708 git ls-files -o >out &&
709 test_line_count = 2 out &&
711 git rev-parse >expect \
712 B:a E2:a/file C:a/file A:ignore-me &&
713 git rev-parse >actual \
714 :2:a :3:a/file :1:a/file :0:ignore-me
716 test_cmp expect actual &&
718 test_path_is_file a~HEAD
722 test_expect_success 'merge of E2 & D1 fails but has appropriate contents' '
723 test_when_finished "git -C directory-file reset --hard" &&
724 test_when_finished "git -C directory-file clean -fdqx" &&
730 test_must_fail git merge -s recursive D1^0 &&
732 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
734 git ls-files -s >out &&
735 test_line_count = 5 out &&
736 git ls-files -u >out &&
737 test_line_count = 4 out &&
738 git ls-files -o >out &&
739 test_line_count = 1 out &&
741 git rev-parse >expect \
742 B:a D1:a E2:a/file C:a/file A:ignore-me &&
743 git rev-parse >actual \
744 :1:a~D1^0 :3:a~D1^0 :2:a/file :1:a/file :0:ignore-me
746 git ls-files -s >out &&
747 test_line_count = 4 out &&
748 git ls-files -u >out &&
749 test_line_count = 3 out &&
750 git ls-files -o >out &&
751 test_line_count = 2 out &&
753 git rev-parse >expect \
754 B:a E2:a/file C:a/file A:ignore-me &&
755 git rev-parse >actual \
756 :3:a :2:a/file :1:a/file :0:ignore-me
758 test_cmp expect actual &&
760 test_path_is_file a~D1^0
764 test_expect_success 'merge of D1 & E3 succeeds' '
765 test_when_finished "git -C directory-file reset --hard" &&
766 test_when_finished "git -C directory-file clean -fdqx" &&
772 git merge -s recursive E3^0 &&
774 git ls-files -s >out &&
775 test_line_count = 2 out &&
776 git ls-files -u >out &&
777 test_line_count = 0 out &&
778 git ls-files -o >out &&
779 test_line_count = 1 out &&
781 git rev-parse >expect \
783 git rev-parse >actual \
785 test_cmp expect actual
789 test_expect_merge_algorithm failure success 'merge of D1 & E4 puts merge of a and a2 in both a and a2' '
790 test_when_finished "git -C directory-file reset --hard" &&
791 test_when_finished "git -C directory-file clean -fdqx" &&
797 test_must_fail git merge -s recursive E4^0 &&
799 git ls-files -s >out &&
800 test_line_count = 4 out &&
801 git ls-files -u >out &&
802 test_line_count = 3 out &&
803 git ls-files -o >out &&
804 test_line_count = 1 out &&
806 git rev-parse >expect \
807 A:ignore-me B:a E4:a2 E4:a2 &&
808 git rev-parse >actual \
809 :0:ignore-me :1:a~Temporary\ merge\ branch\ 2 :2:a :3:a2 &&
810 test_cmp expect actual
814 test_expect_failure 'merge of D2 & E4 merges a2s & reports conflict for a/file' '
815 test_when_finished "git -C directory-file reset --hard" &&
816 test_when_finished "git -C directory-file clean -fdqx" &&
822 test_must_fail git merge -s recursive E4^0 &&
824 git ls-files -s >out &&
825 test_line_count = 3 out &&
826 git ls-files -u >out &&
827 test_line_count = 1 out &&
828 git ls-files -o >out &&
829 test_line_count = 1 out &&
831 git rev-parse >expect \
832 A:ignore-me E4:a2 D2:a/file &&
833 git rev-parse >actual \
834 :0:ignore-me :0:a2 :2:a/file &&
835 test_cmp expect actual
840 # criss-cross with rename/rename(1to2)/modify followed by
841 # rename/rename(2to1)/modify:
851 # Commit A: new file: a
852 # Commit B: rename a->b, modifying by adding a line
853 # Commit C: rename a->c
854 # Commit D: merge B&C, resolving conflict by keeping contents in newname
855 # Commit E: merge B&C, resolving conflict similar to D but adding another line
857 # There is a conflict merging B & C, but one of filename not of file
858 # content. Whoever created D and E chose specific resolutions for that
859 # conflict resolution. Now, since: (1) there is no content conflict
860 # merging B & C, (2) D does not modify that merged content further, and (3)
861 # both D & E resolve the name conflict in the same way, the modification to
862 # newname in E should not cause any conflicts when it is merged with D.
863 # (Note that this can be accomplished by having the virtual merge base have
864 # the merged contents of b and c stored in a file named a, which seems like
865 # the most logical choice anyway.)
867 # Comment from Junio: I do not necessarily agree with the choice "a", but
868 # it feels sound to say "B and C do not agree what the final pathname
869 # should be, but we know this content was derived from the common A:a so we
870 # use one path whose name is arbitrary in the virtual merge base X between
871 # D and E" and then further let the rename detection to notice that that
872 # arbitrary path gets renamed between X-D to "newname" and X-E also to
873 # "newname" to resolve it as both sides renaming it to the same new
874 # name. It is akin to what we do at the content level, i.e. "B and C do not
875 # agree what the final contents should be, so we leave the conflict marker
876 # but that may cancel out at the final merge stage".
878 test_expect_success 'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
879 test_create_repo rename-squared-squared &&
881 cd rename-squared-squared &&
883 printf "1\n2\n3\n4\n5\n6\n" >a &&
888 git checkout -b B A &&
894 git checkout -b C A &&
898 git checkout -q B^0 &&
899 git merge --no-commit -s ours C^0 &&
901 git commit -m "Merge commit C^0 into HEAD" &&
904 git checkout -q C^0 &&
905 git merge --no-commit -s ours B^0 &&
907 printf "7\n8\n" >>newname &&
909 git commit -m "Merge commit B^0 into HEAD" &&
914 test_expect_success 'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
916 cd rename-squared-squared &&
920 git merge -s recursive E^0 &&
922 git ls-files -s >out &&
923 test_line_count = 1 out &&
924 git ls-files -u >out &&
925 test_line_count = 0 out &&
926 git ls-files -o >out &&
927 test_line_count = 1 out &&
929 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
934 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
944 # Commit A: new file: a
945 # Commit B: rename a->b
946 # Commit C: rename a->c, add different a
947 # Commit D: merge B&C, keeping b&c and (new) a modified at beginning
948 # Commit E: merge B&C, keeping b&c and (new) a modified at end
950 # Merging commits D & E should result in no conflict; doing so correctly
951 # requires getting the virtual merge base (from merging B&C) right, handling
952 # renaming carefully (both in the virtual merge base and later), and getting
953 # content merge handled.
955 test_expect_success 'setup criss-cross + rename/rename/add-source + modify/modify' '
956 test_create_repo rename-rename-add-source &&
958 cd rename-rename-add-source &&
960 printf "lots\nof\nwords\nand\ncontent\n" >a &&
965 git checkout -b B A &&
969 git checkout -b C A &&
971 printf "2\n3\n4\n5\n6\n7\n" >a &&
976 git merge --no-commit -s ours C^0 &&
977 git checkout C -- a c &&
983 git commit -m "Merge commit C^0 into HEAD" &&
987 git merge --no-commit -s ours B^0 &&
988 git checkout B -- b &&
991 git commit -m "Merge commit B^0 into HEAD" &&
996 test_expect_failure 'detect rename/rename/add-source for virtual merge-base' '
998 cd rename-rename-add-source &&
1002 git merge -s recursive E^0 &&
1004 git ls-files -s >out &&
1005 test_line_count = 3 out &&
1006 git ls-files -u >out &&
1007 test_line_count = 0 out &&
1008 git ls-files -o >out &&
1009 test_line_count = 1 out &&
1011 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >correct &&
1012 git rev-parse >expect \
1015 git rev-parse >actual \
1017 git hash-object >>actual \
1019 test_cmp expect actual
1024 # criss-cross with rename/rename(1to2)/add-dest + simple modify:
1034 # Commit A: new file: a
1035 # Commit B: rename a->b, add c
1036 # Commit C: rename a->c
1037 # Commit D: merge B&C, keeping A:a and B:c
1038 # Commit E: merge B&C, keeping A:a and slightly modified c from B
1040 # Merging commits D & E should result in no conflict. The virtual merge
1041 # base of B & C needs to not delete B:c for that to work, though...
1043 test_expect_success 'setup criss-cross+rename/rename/add-dest + simple modify' '
1044 test_create_repo rename-rename-add-dest &&
1046 cd rename-rename-add-dest &&
1053 git checkout -b B A &&
1055 printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
1059 git checkout -b C A &&
1064 git merge --no-commit -s ours C^0 &&
1066 git commit -m "D is like B but renames b back to a" &&
1070 git merge --no-commit -s ours C^0 &&
1074 git commit -m "E like D but has mod in c" &&
1079 test_expect_success 'virtual merge base handles rename/rename(1to2)/add-dest' '
1081 cd rename-rename-add-dest &&
1085 git merge -s recursive E^0 &&
1087 git ls-files -s >out &&
1088 test_line_count = 2 out &&
1089 git ls-files -u >out &&
1090 test_line_count = 0 out &&
1091 git ls-files -o >out &&
1092 test_line_count = 1 out &&
1094 git rev-parse >expect \
1096 git rev-parse >actual \
1098 test_cmp expect actual
1103 # criss-cross with modify/modify on a symlink:
1113 # Commit A: simple simlink fickle->lagoon
1114 # Commit B: redirect fickle->disneyland
1115 # Commit C: redirect fickle->home
1116 # Commit D: merge B&C, resolving in favor of B
1117 # Commit E: merge B&C, resolving in favor of C
1119 # This is an obvious modify/modify conflict for the symlink 'fickle'. Can
1122 test_expect_success 'setup symlink modify/modify' '
1123 test_create_repo symlink-modify-modify &&
1125 cd symlink-modify-modify &&
1127 test_ln_s_add lagoon fickle &&
1131 git checkout -b B A &&
1133 test_ln_s_add disneyland fickle &&
1136 git checkout -b C A &&
1138 test_ln_s_add home fickle &&
1142 git checkout -q B^0 &&
1143 git merge -s ours -m D C^0 &&
1146 git checkout -q C^0 &&
1147 git merge -s ours -m E B^0 &&
1152 test_expect_merge_algorithm failure success 'check symlink modify/modify' '
1154 cd symlink-modify-modify &&
1158 test_must_fail git merge -s recursive E^0 &&
1160 git ls-files -s >out &&
1161 test_line_count = 3 out &&
1162 git ls-files -u >out &&
1163 test_line_count = 3 out &&
1164 git ls-files -o >out &&
1165 test_line_count = 1 out
1170 # criss-cross with add/add of a symlink:
1180 # Commit A: No symlink or path exists yet
1181 # Commit B: set up symlink: fickle->disneyland
1182 # Commit C: set up symlink: fickle->home
1183 # Commit D: merge B&C, resolving in favor of B
1184 # Commit E: merge B&C, resolving in favor of C
1186 # This is an obvious add/add conflict for the symlink 'fickle'. Can
1189 test_expect_success 'setup symlink add/add' '
1190 test_create_repo symlink-add-add &&
1192 cd symlink-add-add &&
1199 git checkout -b B A &&
1200 test_ln_s_add disneyland fickle &&
1203 git checkout -b C A &&
1204 test_ln_s_add home fickle &&
1208 git checkout -q B^0 &&
1209 git merge -s ours -m D C^0 &&
1212 git checkout -q C^0 &&
1213 git merge -s ours -m E B^0 &&
1218 test_expect_merge_algorithm failure success 'check symlink add/add' '
1220 cd symlink-add-add &&
1224 test_must_fail git merge -s recursive E^0 &&
1226 git ls-files -s >out &&
1227 test_line_count = 3 out &&
1228 git ls-files -u >out &&
1229 test_line_count = 2 out &&
1230 git ls-files -o >out &&
1231 test_line_count = 1 out
1236 # criss-cross with modify/modify on a submodule:
1246 # Commit A: simple submodule repo
1247 # Commit B: update repo
1248 # Commit C: update repo differently
1249 # Commit D: merge B&C, resolving in favor of B
1250 # Commit E: merge B&C, resolving in favor of C
1252 # This is an obvious modify/modify conflict for the submodule 'repo'. Can
1255 test_expect_success 'setup submodule modify/modify' '
1256 test_create_repo submodule-modify-modify &&
1258 cd submodule-modify-modify &&
1260 test_create_repo submod &&
1268 git checkout -b B A &&
1274 git checkout -b C A &&
1281 git -C submod reset --hard A &&
1286 git checkout -b B A &&
1287 git -C submod reset --hard B &&
1291 git checkout -b C A &&
1292 git -C submod reset --hard C &&
1296 git checkout -q B^0 &&
1297 git merge -s ours -m D C^0 &&
1300 git checkout -q C^0 &&
1301 git merge -s ours -m E B^0 &&
1306 test_expect_merge_algorithm failure success 'check submodule modify/modify' '
1308 cd submodule-modify-modify &&
1312 test_must_fail git merge -s recursive E^0 &&
1314 git ls-files -s >out &&
1315 test_line_count = 3 out &&
1316 git ls-files -u >out &&
1317 test_line_count = 3 out &&
1318 git ls-files -o >out &&
1319 test_line_count = 1 out
1324 # criss-cross with add/add on a submodule:
1334 # Commit A: nothing of note
1335 # Commit B: introduce submodule repo
1336 # Commit C: introduce submodule repo at different commit
1337 # Commit D: merge B&C, resolving in favor of B
1338 # Commit E: merge B&C, resolving in favor of C
1340 # This is an obvious add/add conflict for the submodule 'repo'. Can
1343 test_expect_success 'setup submodule add/add' '
1344 test_create_repo submodule-add-add &&
1346 cd submodule-add-add &&
1348 test_create_repo submod &&
1356 git checkout -b B A &&
1362 git checkout -b C A &&
1369 touch irrelevant-file &&
1370 git add irrelevant-file &&
1374 git checkout -b B A &&
1375 git -C submod reset --hard B &&
1379 git checkout -b C A &&
1380 git -C submod reset --hard C &&
1384 git checkout -q B^0 &&
1385 git merge -s ours -m D C^0 &&
1388 git checkout -q C^0 &&
1389 git merge -s ours -m E B^0 &&
1394 test_expect_merge_algorithm failure success 'check submodule add/add' '
1396 cd submodule-add-add &&
1400 test_must_fail git merge -s recursive E^0 &&
1402 git ls-files -s >out &&
1403 test_line_count = 3 out &&
1404 git ls-files -u >out &&
1405 test_line_count = 2 out &&
1406 git ls-files -o >out &&
1407 test_line_count = 1 out
1412 # criss-cross with conflicting entry types:
1422 # Commit A: nothing of note
1423 # Commit B: introduce submodule 'path'
1424 # Commit C: introduce symlink 'path'
1425 # Commit D: merge B&C, resolving in favor of B
1426 # Commit E: merge B&C, resolving in favor of C
1428 # This is an obvious add/add conflict for 'path'. Can git detect it?
1430 test_expect_success 'setup conflicting entry types (submodule vs symlink)' '
1431 test_create_repo submodule-symlink-add-add &&
1433 cd submodule-symlink-add-add &&
1435 test_create_repo path &&
1444 touch irrelevant-file &&
1445 git add irrelevant-file &&
1449 git checkout -b B A &&
1450 git -C path reset --hard B &&
1454 git checkout -b C A &&
1456 test_ln_s_add irrelevant-file path &&
1459 git checkout -q B^0 &&
1460 git merge -s ours -m D C^0 &&
1463 git checkout -q C^0 &&
1464 git merge -s ours -m E B^0 &&
1469 test_expect_merge_algorithm failure success 'check conflicting entry types (submodule vs symlink)' '
1471 cd submodule-symlink-add-add &&
1475 test_must_fail git merge -s recursive E^0 &&
1477 git ls-files -s >out &&
1478 test_line_count = 3 out &&
1479 git ls-files -u >out &&
1480 test_line_count = 2 out &&
1481 git ls-files -o >out &&
1482 test_line_count = 1 out
1487 # criss-cross with regular files that have conflicting modes:
1497 # Commit A: nothing of note
1498 # Commit B: introduce file source_me.bash, not executable
1499 # Commit C: introduce file source_me.bash, executable
1500 # Commit D: merge B&C, resolving in favor of B
1501 # Commit E: merge B&C, resolving in favor of C
1503 # This is an obvious add/add mode conflict. Can git detect it?
1505 test_expect_success 'setup conflicting modes for regular file' '
1506 test_create_repo regular-file-mode-conflict &&
1508 cd regular-file-mode-conflict &&
1510 touch irrelevant-file &&
1511 git add irrelevant-file &&
1515 git checkout -b B A &&
1516 echo "command_to_run" >source_me.bash &&
1517 git add source_me.bash &&
1520 git checkout -b C A &&
1521 echo "command_to_run" >source_me.bash &&
1522 git add source_me.bash &&
1523 test_chmod +x source_me.bash &&
1526 git checkout -q B^0 &&
1527 git merge -s ours -m D C^0 &&
1530 git checkout -q C^0 &&
1531 git merge -s ours -m E B^0 &&
1536 test_expect_failure 'check conflicting modes for regular file' '
1538 cd regular-file-mode-conflict &&
1542 test_must_fail git merge -s recursive E^0 &&
1544 git ls-files -s >out &&
1545 test_line_count = 3 out &&
1546 git ls-files -u >out &&
1547 test_line_count = 2 out &&
1548 git ls-files -o >out &&
1549 test_line_count = 1 out
1561 # master has two files, named 'b' and 'a'
1562 # branches L1 and R1 both modify each of the two files in conflicting ways
1564 # L2 is a merge of R1 into L1; more on it later.
1565 # R2 is a merge of L1 into R1; more on it later.
1567 # X is an auto-generated merge-base used when merging L2 and R2.
1568 # since X is a merge of L1 and R1, it has conflicting versions of each file
1570 # More about L2 and R2:
1571 # - both resolve the conflicts in 'b' and 'a' differently
1572 # - L2 renames 'b' to 'm'
1573 # - R2 renames 'a' to 'm'
1575 # In the end, in file 'm' we have four different conflicting files (from
1576 # two versions of 'b' and two of 'a'). In addition, if
1577 # merge.conflictstyle is diff3, then the base version also has
1578 # conflict markers of its own, leading to a total of three levels of
1579 # conflict markers. This is a pretty weird corner case, but we just want
1580 # to ensure that we handle it as well as practical.
1582 test_expect_success 'setup nested conflicts' '
1583 test_create_repo nested_conflicts &&
1585 cd nested_conflicts &&
1587 # Create some related files now
1588 for i in $(test_seq 1 10)
1590 echo Random base content line $i
1602 test_write_lines b b_L1 >>b_L1 &&
1603 test_write_lines b b_R1 >>b_R1 &&
1604 test_write_lines b b_L2 >>b_L2 &&
1605 test_write_lines b b_R2 >>b_R2 &&
1606 test_write_lines a a_L1 >>a_L1 &&
1607 test_write_lines a a_R1 >>a_R1 &&
1608 test_write_lines a a_L2 >>a_L2 &&
1609 test_write_lines a a_R2 >>a_R2 &&
1611 # Setup original commit (or merge-base), consisting of
1612 # files named "b" and "a"
1618 test_tick && git commit -m initial &&
1623 # Handle the left side
1628 test_tick && git commit -m "version L1 of files" &&
1631 # Handle the right side
1636 test_tick && git commit -m "version R1 of files" &&
1639 # Create first merge on left side
1641 test_must_fail git merge R1 &&
1646 test_tick && git commit -m "left merge, rename b->m" &&
1649 # Create first merge on right side
1651 test_must_fail git merge L1 &&
1656 test_tick && git commit -m "right merge, rename a->m" &&
1661 test_expect_success 'check nested conflicts' '
1663 cd nested_conflicts &&
1666 MASTER=$(git rev-parse --short master) &&
1667 git checkout L2^0 &&
1669 # Merge must fail; there is a conflict
1670 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R2^0 &&
1672 # Make sure the index has the right number of entries
1673 git ls-files -s >out &&
1674 test_line_count = 2 out &&
1675 git ls-files -u >out &&
1676 test_line_count = 2 out &&
1677 # Ensure we have the correct number of untracked files
1678 git ls-files -o >out &&
1679 test_line_count = 1 out &&
1681 # Create a and b from virtual merge base X
1682 git cat-file -p master:a >base &&
1683 git cat-file -p L1:a >ours &&
1684 git cat-file -p R1:a >theirs &&
1685 test_must_fail git merge-file --diff3 \
1686 -L "Temporary merge branch 1" \
1688 -L "Temporary merge branch 2" \
1692 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_a &&
1694 git cat-file -p master:b >base &&
1695 git cat-file -p L1:b >ours &&
1696 git cat-file -p R1:b >theirs &&
1697 test_must_fail git merge-file --diff3 \
1698 -L "Temporary merge branch 1" \
1700 -L "Temporary merge branch 2" \
1704 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_b &&
1706 # Compare :2:m to expected values
1707 git cat-file -p L2:m >ours &&
1708 git cat-file -p R2:b >theirs &&
1709 test_must_fail git merge-file --diff3 \
1711 -L "merged common ancestors:b" \
1716 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_2 &&
1717 git cat-file -p :2:m >actual &&
1718 test_cmp m_stage_2 actual &&
1720 # Compare :3:m to expected values
1721 git cat-file -p L2:a >ours &&
1722 git cat-file -p R2:m >theirs &&
1723 test_must_fail git merge-file --diff3 \
1725 -L "merged common ancestors:a" \
1730 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_3 &&
1731 git cat-file -p :3:m >actual &&
1732 test_cmp m_stage_3 actual &&
1734 # Compare m to expected contents
1736 cp m_stage_2 expected_final_m &&
1737 test_must_fail git merge-file --diff3 \
1739 -L "merged common ancestors" \
1744 test_cmp expected_final_m m
1756 # master has one file named 'content'
1757 # branches L1 and R1 both modify each of the two files in conflicting ways
1759 # L<n> (n>1) is a merge of R<n-1> into L<n-1>
1760 # R<n> (n>1) is a merge of L<n-1> into R<n-1>
1761 # L<n> and R<n> resolve the conflicts differently.
1763 # X<n> is an auto-generated merge-base used when merging L<n+1> and R<n+1>.
1764 # By construction, X1 has conflict markers due to conflicting versions.
1765 # X2, due to using merge.conflictstyle=3, has nested conflict markers.
1767 # So, merging R3 into L3 using merge.conflictstyle=3 should show the
1768 # nested conflict markers from X2 in the base version -- that means we
1769 # have three levels of conflict markers. Can we distinguish all three?
1771 test_expect_success 'setup virtual merge base with nested conflicts' '
1772 test_create_repo virtual_merge_base_has_nested_conflicts &&
1774 cd virtual_merge_base_has_nested_conflicts &&
1776 # Create some related files now
1777 for i in $(test_seq 1 10)
1779 echo Random base content line $i
1782 # Setup original commit
1784 test_tick && git commit -m initial &&
1791 echo left >>content &&
1793 test_tick && git commit -m "version L1 of content" &&
1798 echo right >>content &&
1800 test_tick && git commit -m "version R1 of content" &&
1805 test_must_fail git -c merge.conflictstyle=diff3 merge R1 &&
1806 git checkout L1 content &&
1807 test_tick && git commit -m "version L2 of content" &&
1812 test_must_fail git -c merge.conflictstyle=diff3 merge L1 &&
1813 git checkout R1 content &&
1814 test_tick && git commit -m "version R2 of content" &&
1819 test_must_fail git -c merge.conflictstyle=diff3 merge R2 &&
1820 git checkout L1 content &&
1821 test_tick && git commit -m "version L3 of content" &&
1826 test_must_fail git -c merge.conflictstyle=diff3 merge L2 &&
1827 git checkout R1 content &&
1828 test_tick && git commit -m "version R3 of content" &&
1833 test_expect_success 'check virtual merge base with nested conflicts' '
1835 cd virtual_merge_base_has_nested_conflicts &&
1837 MASTER=$(git rev-parse --short master) &&
1838 git checkout L3^0 &&
1840 # Merge must fail; there is a conflict
1841 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R3^0 &&
1843 # Make sure the index has the right number of entries
1844 git ls-files -s >out &&
1845 test_line_count = 3 out &&
1846 git ls-files -u >out &&
1847 test_line_count = 3 out &&
1848 # Ensure we have the correct number of untracked files
1849 git ls-files -o >out &&
1850 test_line_count = 1 out &&
1852 # Compare :[23]:content to expected values
1853 git rev-parse L1:content R1:content >expect &&
1854 git rev-parse :2:content :3:content >actual &&
1855 test_cmp expect actual &&
1857 # Imitate X1 merge base, except without long enough conflict
1858 # markers because a subsequent sed will modify them. Put
1860 git cat-file -p master:content >base &&
1861 git cat-file -p L:content >left &&
1862 git cat-file -p R:content >right &&
1863 cp left merged-once &&
1864 test_must_fail git merge-file --diff3 \
1865 -L "Temporary merge branch 1" \
1867 -L "Temporary merge branch 2" \
1871 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-once >vmb &&
1873 # Imitate X2 merge base, overwriting vmb. Note that we
1874 # extend both sets of conflict markers to make them longer
1875 # with the sed command.
1876 cp left merged-twice &&
1877 test_must_fail git merge-file --diff3 \
1878 -L "Temporary merge branch 1" \
1879 -L "merged common ancestors" \
1880 -L "Temporary merge branch 2" \
1884 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-twice >vmb &&
1886 # Compare :1:content to expected value
1887 git cat-file -p :1:content >actual &&
1888 test_cmp vmb actual &&
1890 # Determine expected content in final outer merge, compare to
1891 # what the merge generated.
1892 cp -f left expect &&
1893 test_must_fail git merge-file --diff3 \
1894 -L "HEAD" -L "merged common ancestors" -L "R3^0" \
1896 test_cmp expect content