3 test_description
='recursive merge corner cases involving criss-cross merges'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9 .
"$TEST_DIRECTORY"/lib-merge.sh
21 test_expect_success
'setup basic criss-cross + rename with no modifications' '
22 test_create_repo basic-rename &&
26 ten="0 1 2 3 4 5 6 7 8 9" &&
29 echo line $i in a sample file
33 echo line $i in another sample file
36 test_tick && git commit -m initial &&
41 test_tick && git commit -m R1 &&
45 test_tick && git commit -m L1 &&
48 test_tick && git merge -s ours R1 &&
52 test_tick && git merge -s ours L1 &&
57 test_expect_success
'merge simple rename+criss-cross with no modifications' '
64 test_must_fail git merge -s recursive R2^0 &&
66 git ls-files -s >out &&
67 test_line_count = 5 out &&
68 git ls-files -u >out &&
69 test_line_count = 3 out &&
70 git ls-files -o >out &&
71 test_line_count = 1 out &&
73 git rev-parse >expect \
75 git rev-parse >actual \
77 test_cmp expect actual
82 # Same as before, but modify L1 slightly:
93 test_expect_success
'setup criss-cross + rename merges with basic modification' '
94 test_create_repo rename-modify &&
98 ten="0 1 2 3 4 5 6 7 8 9" &&
101 echo line $i in a sample file
105 echo line $i in another sample file
108 test_tick && git commit -m initial &&
111 git checkout -b R1 &&
115 test_tick && git commit -m R1 &&
119 test_tick && git commit -m L1 &&
122 test_tick && git merge -s ours R1 &&
126 test_tick && git merge -s ours L1 &&
131 test_expect_success
'merge criss-cross + rename merges with basic modification' '
137 test_must_fail git merge -s recursive R2^0 &&
139 git ls-files -s >out &&
140 test_line_count = 5 out &&
141 git ls-files -u >out &&
142 test_line_count = 3 out &&
143 git ls-files -o >out &&
144 test_line_count = 1 out &&
146 git rev-parse >expect \
148 git rev-parse >actual \
150 test_cmp expect actual
155 # For the next test, we start with three commits in two lines of development
156 # which setup a rename/add conflict:
157 # Commit A: File 'a' exists
158 # Commit B: Rename 'a' -> 'new_a'
159 # Commit C: Modify 'a', create different 'new_a'
160 # Later, two different people merge and resolve differently:
161 # Commit D: Merge B & C, ignoring separately created 'new_a'
162 # Commit E: Merge B & C making use of some piece of secondary 'new_a'
163 # Finally, someone goes to merge D & E. Does git detect the conflict?
174 test_expect_success
'setup differently handled merges of rename/add conflict' '
175 test_create_repo rename-add &&
179 printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
181 test_tick && git commit -m A &&
186 test_write_lines 0 1 2 3 4 5 6 7 foobar >new_a &&
188 test_tick && git commit -m C &&
192 test_tick && git commit -m B &&
195 test_must_fail git merge C &&
196 git show :2:new_a >new_a &&
198 test_tick && git commit -m D &&
202 test_must_fail git merge B &&
203 test_write_lines 0 1 2 3 4 5 6 7 bad_merge >new_a &&
205 test_tick && git commit -m E &&
210 test_expect_success
'git detects differently handled merges conflict' '
216 test_must_fail git merge -s recursive E^0 &&
218 git ls-files -s >out &&
219 test_line_count = 3 out &&
220 git ls-files -u >out &&
221 test_line_count = 3 out &&
222 git ls-files -o >out &&
223 test_line_count = 1 out &&
225 git cat-file -p C:new_a >ours &&
226 git cat-file -p C:a >theirs &&
228 test_must_fail git merge-file \
229 -L "Temporary merge branch 1" \
231 -L "Temporary merge branch 2" \
233 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
234 git hash-object ours-tweaked >expect &&
235 git rev-parse >>expect \
237 git rev-parse >actual \
238 :1:new_a :2:new_a :3:new_a &&
239 test_cmp expect actual &&
241 # Test that the two-way merge in new_a is as expected
242 git cat-file -p D:new_a >ours &&
243 git cat-file -p E:new_a >theirs &&
245 test_must_fail git merge-file \
250 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
251 git hash-object new_a >actual &&
252 git hash-object ours >expect &&
253 test_cmp expect actual
257 # Repeat the above testcase with precisely the same setup, other than with
258 # the two merge bases having different orderings of commit timestamps so
259 # that they are reversed in the order they are provided to merge-recursive,
260 # so that we can improve code coverage.
261 test_expect_success
'git detects differently handled merges conflict, swapped' '
265 # Difference #1: Do cleanup from previous testrun
269 # Difference #2: Change commit timestamps
270 btime=$(git log --no-walk --date=raw --format=%cd B | awk "{print \$1}") &&
271 ctime=$(git log --no-walk --date=raw --format=%cd C | awk "{print \$1}") &&
272 newctime=$(($btime+1)) &&
273 git fast-export --no-data --all | sed -e s/$ctime/$newctime/ | git fast-import --force --quiet &&
274 # End of most differences; rest is copy-paste of last test,
275 # other than swapping C:a and C:new_a due to order switch
278 test_must_fail git merge -s recursive E^0 &&
280 git ls-files -s >out &&
281 test_line_count = 3 out &&
282 git ls-files -u >out &&
283 test_line_count = 3 out &&
284 git ls-files -o >out &&
285 test_line_count = 1 out &&
287 git cat-file -p C:a >ours &&
288 git cat-file -p C:new_a >theirs &&
290 test_must_fail git merge-file \
291 -L "Temporary merge branch 1" \
293 -L "Temporary merge branch 2" \
295 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
296 git hash-object ours-tweaked >expect &&
297 git rev-parse >>expect \
299 git rev-parse >actual \
300 :1:new_a :2:new_a :3:new_a &&
301 test_cmp expect actual &&
303 # Test that the two-way merge in new_a is as expected
304 git cat-file -p D:new_a >ours &&
305 git cat-file -p E:new_a >theirs &&
307 test_must_fail git merge-file \
312 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
313 git hash-object new_a >actual &&
314 git hash-object ours >expect &&
315 test_cmp expect actual
320 # criss-cross + modify/delete:
330 # Commit A: file with contents 'A\n'
331 # Commit B: file with contents 'B\n'
332 # Commit C: file not present
333 # Commit D: file with contents 'B\n'
334 # Commit E: file not present
336 # Merging commits D & E should result in modify/delete conflict.
338 test_expect_success
'setup criss-cross + modify/delete resolved differently' '
339 test_create_repo modify-delete &&
361 test_must_fail git merge C &&
369 test_must_fail git merge B &&
377 test_expect_success
'git detects conflict merging criss-cross+modify/delete' '
383 test_must_fail git merge -s recursive E^0 &&
385 git ls-files -s >out &&
386 test_line_count = 2 out &&
387 git ls-files -u >out &&
388 test_line_count = 2 out &&
390 git rev-parse >expect \
392 git rev-parse >actual \
394 test_cmp expect actual
398 test_expect_success
'git detects conflict merging criss-cross+modify/delete, reverse direction' '
405 test_must_fail git merge -s recursive D^0 &&
407 git ls-files -s >out &&
408 test_line_count = 2 out &&
409 git ls-files -u >out &&
410 test_line_count = 2 out &&
412 git rev-parse >expect \
414 git rev-parse >actual \
416 test_cmp expect actual
420 # SORRY FOR THE SUPER LONG DESCRIPTION, BUT THIS NEXT ONE IS HAIRY
422 # criss-cross + d/f conflict via add/add:
423 # Commit A: Neither file 'a' nor directory 'a/' exists.
424 # Commit B: Introduce 'a'
425 # Commit C: Introduce 'a/file'
426 # Commit D1: Merge B & C, keeping 'a' and deleting 'a/'
427 # Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
437 # I'll describe D2, E2, & E3 (which are alternatives for D1 & E1) more below...
439 # Merging D1 & E1 requires we first create a virtual merge base X from
440 # merging A & B in memory. There are several possibilities for the merge-base:
441 # 1: Keep both 'a' and 'a/file' (assuming crazy filesystem allowing a tree
442 # with a directory and file at same path): results in merge of D1 & E1
443 # being clean with both files deleted. Bad (no conflict detected).
444 # 2: Keep 'a' but not 'a/file': Merging D1 & E1 is clean and matches E1. Bad.
445 # 3: Keep 'a/file' but not 'a': Merging D1 & E1 is clean and matches D1. Bad.
446 # 4: Keep neither file: Merging D1 & E1 reports the D/F add/add conflict.
448 # So 4 sounds good for this case, but if we were to merge D1 & E3, where E3
450 # Commit E3: Merge B & C, keeping modified a, and deleting a/
451 # then we'd get an add/add conflict for 'a', which seems suboptimal. A little
452 # creativity leads us to an alternate choice:
453 # 5: Keep 'a' as 'a~$UNIQUE' and a/file; results:
454 # Merge D1 & E1: rename/delete conflict for 'a'; a/file silently deleted
455 # Merge D1 & E3 is clean, as expected.
457 # So choice 5 at least provides some kind of conflict for the original case,
458 # and can merge cleanly as expected with D1 and E3. It also made things just
459 # slightly funny for merging D1 and E4, where E4 is defined as:
460 # Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
461 # in this case, we'll get a rename/rename(1to2) conflict because a~$UNIQUE
462 # gets renamed to 'a' in D1 and to 'a2' in E4. But that's better than having
463 # two files (both 'a' and 'a2') sitting around without the user being notified
464 # that we could detect they were related and need to be merged. Also, choice
465 # 5 makes the handling of 'a/file' seem suboptimal. What if we were to merge
466 # D2 and E4, where D2 is:
467 # Commit D2: Merge B & C, renaming 'a'->'a2', keeping 'a/file'
468 # This would result in a clean merge with 'a2' having three-way merged
469 # contents (good), and deleting 'a/' (bad) -- it doesn't detect the
470 # conflict in how the different sides treated a/file differently.
471 # Continuing down the creative route:
472 # 6: Keep 'a' as 'a~$UNIQUE1' and keep 'a/' as 'a~$UNIQUE2/'; results:
473 # Merge D1 & E1: rename/delete conflict for 'a' and each path under 'a/'.
474 # Merge D1 & E3: clean, as expected.
475 # Merge D1 & E4: rename/rename(1to2) conflict on 'a' vs 'a2'.
476 # Merge D2 & E4: clean for 'a2', rename/delete for a/file
478 # Choice 6 could cause rename detection to take longer (providing more targets
479 # that need to be searched). Also, the conflict message for each path under
480 # 'a/' might be annoying unless we can detect it at the directory level, print
481 # it once, and then suppress it for individual filepaths underneath.
484 # As of time of writing, git uses choice 5. Directory rename detection and
485 # rename detection performance improvements might make choice 6 a desirable
486 # improvement. But we can at least document where we fall short for now...
489 # Historically, this testcase also used:
490 # Commit E2: Merge B & C, deleting 'a' but keeping slightly modified 'a/file'
491 # The merge of D1 & E2 is very similar to D1 & E1 -- it has similar issues for
492 # path 'a', but should always result in a modify/delete conflict for path
493 # 'a/file'. These tests ran the two merges
496 # in both directions, to check for directional issues with D/F conflict
497 # handling. Later we added
501 # for good measure, though we only ran those one way because we had pretty
502 # good confidence in merge-recursive's directional handling of D/F issues.
504 # Just to summarize all the intermediate merge commits:
505 # Commit D1: Merge B & C, keeping a and deleting a/
506 # Commit D2: Merge B & C, renaming a->a2, keeping a/file
507 # Commit E1: Merge B & C, deleting a but keeping a/file
508 # Commit E2: Merge B & C, deleting a but keeping slightly modified a/file
509 # Commit E3: Merge B & C, keeping modified a, and deleting a/
510 # Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
513 test_expect_success
'setup differently handled merges of directory/file conflict' '
514 test_create_repo directory-file &&
527 test_write_lines a b c d e f g >a/file &&
533 test_write_lines 1 2 3 4 5 6 7 >a &&
539 git merge -s ours -m D1 C^0 &&
543 test_must_fail git merge C^0 &&
544 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
553 git cat-file -p B:a >a2 &&
559 git merge -s ours -m E1 B^0 &&
563 git merge -s ours -m E2 B^0 &&
564 test_write_lines a b c d e f g h >a/file &&
566 git commit --amend -C HEAD &&
570 test_must_fail git merge B^0 &&
571 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
578 test_write_lines 1 2 3 4 5 6 7 8 >a &&
584 test_must_fail git merge B^0 &&
585 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
594 test_write_lines 1 2 3 4 5 6 7 8 >a2 &&
601 test_expect_success
'merge of D1 & E1 fails but has appropriate contents' '
602 test_when_finished "git -C directory-file reset --hard" &&
603 test_when_finished "git -C directory-file clean -fdqx" &&
609 test_must_fail git merge -s recursive E1^0 &&
611 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
613 git ls-files -s >out &&
614 test_line_count = 3 out &&
615 git ls-files -u >out &&
616 test_line_count = 2 out &&
617 git ls-files -o >out &&
618 test_line_count = 1 out &&
620 git rev-parse >expect \
621 A:ignore-me B:a D1:a &&
622 git rev-parse >actual \
623 :0:ignore-me :1:a :2:a &&
624 test_cmp expect actual
626 git ls-files -s >out &&
627 test_line_count = 2 out &&
628 git ls-files -u >out &&
629 test_line_count = 1 out &&
630 git ls-files -o >out &&
631 test_line_count = 1 out &&
633 git rev-parse >expect \
635 git rev-parse >actual \
637 test_cmp expect actual
642 test_expect_success
'merge of E1 & D1 fails but has appropriate contents' '
643 test_when_finished "git -C directory-file reset --hard" &&
644 test_when_finished "git -C directory-file clean -fdqx" &&
650 test_must_fail git merge -s recursive D1^0 &&
652 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
654 git ls-files -s >out &&
655 test_line_count = 3 out &&
656 git ls-files -u >out &&
657 test_line_count = 2 out &&
658 git ls-files -o >out &&
659 test_line_count = 1 out &&
661 git rev-parse >expect \
662 A:ignore-me B:a D1:a &&
663 git rev-parse >actual \
664 :0:ignore-me :1:a :3:a &&
665 test_cmp expect actual
667 git ls-files -s >out &&
668 test_line_count = 2 out &&
669 git ls-files -u >out &&
670 test_line_count = 1 out &&
671 git ls-files -o >out &&
672 test_line_count = 1 out &&
674 git rev-parse >expect \
676 git rev-parse >actual \
678 test_cmp expect actual
683 test_expect_success
'merge of D1 & E2 fails but has appropriate contents' '
684 test_when_finished "git -C directory-file reset --hard" &&
685 test_when_finished "git -C directory-file clean -fdqx" &&
691 test_must_fail git merge -s recursive E2^0 &&
693 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
695 git ls-files -s >out &&
696 test_line_count = 5 out &&
697 git ls-files -u >out &&
698 test_line_count = 4 out &&
699 git ls-files -o >out &&
700 test_line_count = 1 out &&
702 git rev-parse >expect \
703 B:a D1:a E2:a/file C:a/file A:ignore-me &&
704 git rev-parse >actual \
705 :1:a~HEAD :2:a~HEAD :3:a/file :1:a/file :0:ignore-me
707 git ls-files -s >out &&
708 test_line_count = 4 out &&
709 git ls-files -u >out &&
710 test_line_count = 3 out &&
711 git ls-files -o >out &&
712 test_line_count = 2 out &&
714 git rev-parse >expect \
715 B:a E2:a/file C:a/file A:ignore-me &&
716 git rev-parse >actual \
717 :2:a :3:a/file :1:a/file :0:ignore-me
719 test_cmp expect actual &&
721 test_path_is_file a~HEAD
725 test_expect_success
'merge of E2 & D1 fails but has appropriate contents' '
726 test_when_finished "git -C directory-file reset --hard" &&
727 test_when_finished "git -C directory-file clean -fdqx" &&
733 test_must_fail git merge -s recursive D1^0 &&
735 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
737 git ls-files -s >out &&
738 test_line_count = 5 out &&
739 git ls-files -u >out &&
740 test_line_count = 4 out &&
741 git ls-files -o >out &&
742 test_line_count = 1 out &&
744 git rev-parse >expect \
745 B:a D1:a E2:a/file C:a/file A:ignore-me &&
746 git rev-parse >actual \
747 :1:a~D1^0 :3:a~D1^0 :2:a/file :1:a/file :0:ignore-me
749 git ls-files -s >out &&
750 test_line_count = 4 out &&
751 git ls-files -u >out &&
752 test_line_count = 3 out &&
753 git ls-files -o >out &&
754 test_line_count = 2 out &&
756 git rev-parse >expect \
757 B:a E2:a/file C:a/file A:ignore-me &&
758 git rev-parse >actual \
759 :3:a :2:a/file :1:a/file :0:ignore-me
761 test_cmp expect actual &&
763 test_path_is_file a~D1^0
767 test_expect_success
'merge of D1 & E3 succeeds' '
768 test_when_finished "git -C directory-file reset --hard" &&
769 test_when_finished "git -C directory-file clean -fdqx" &&
775 git merge -s recursive E3^0 &&
777 git ls-files -s >out &&
778 test_line_count = 2 out &&
779 git ls-files -u >out &&
780 test_line_count = 0 out &&
781 git ls-files -o >out &&
782 test_line_count = 1 out &&
784 git rev-parse >expect \
786 git rev-parse >actual \
788 test_cmp expect actual
792 test_expect_merge_algorithm failure success
'merge of D1 & E4 puts merge of a and a2 in both a and a2' '
793 test_when_finished "git -C directory-file reset --hard" &&
794 test_when_finished "git -C directory-file clean -fdqx" &&
800 test_must_fail git merge -s recursive E4^0 &&
802 git ls-files -s >out &&
803 test_line_count = 4 out &&
804 git ls-files -u >out &&
805 test_line_count = 3 out &&
806 git ls-files -o >out &&
807 test_line_count = 1 out &&
809 git rev-parse >expect \
810 A:ignore-me B:a E4:a2 E4:a2 &&
811 git rev-parse >actual \
812 :0:ignore-me :1:a~Temporary\ merge\ branch\ 2 :2:a :3:a2 &&
813 test_cmp expect actual
817 test_expect_failure
'merge of D2 & E4 merges a2s & reports conflict for a/file' '
818 test_when_finished "git -C directory-file reset --hard" &&
819 test_when_finished "git -C directory-file clean -fdqx" &&
825 test_must_fail git merge -s recursive E4^0 &&
827 git ls-files -s >out &&
828 test_line_count = 3 out &&
829 git ls-files -u >out &&
830 test_line_count = 1 out &&
831 git ls-files -o >out &&
832 test_line_count = 1 out &&
834 git rev-parse >expect \
835 A:ignore-me E4:a2 D2:a/file &&
836 git rev-parse >actual \
837 :0:ignore-me :0:a2 :2:a/file &&
838 test_cmp expect actual
843 # criss-cross with rename/rename(1to2)/modify followed by
844 # rename/rename(2to1)/modify:
854 # Commit A: new file: a
855 # Commit B: rename a->b, modifying by adding a line
856 # Commit C: rename a->c
857 # Commit D: merge B&C, resolving conflict by keeping contents in newname
858 # Commit E: merge B&C, resolving conflict similar to D but adding another line
860 # There is a conflict merging B & C, but one of filename not of file
861 # content. Whoever created D and E chose specific resolutions for that
862 # conflict resolution. Now, since: (1) there is no content conflict
863 # merging B & C, (2) D does not modify that merged content further, and (3)
864 # both D & E resolve the name conflict in the same way, the modification to
865 # newname in E should not cause any conflicts when it is merged with D.
866 # (Note that this can be accomplished by having the virtual merge base have
867 # the merged contents of b and c stored in a file named a, which seems like
868 # the most logical choice anyway.)
870 # Comment from Junio: I do not necessarily agree with the choice "a", but
871 # it feels sound to say "B and C do not agree what the final pathname
872 # should be, but we know this content was derived from the common A:a so we
873 # use one path whose name is arbitrary in the virtual merge base X between
874 # D and E" and then further let the rename detection to notice that that
875 # arbitrary path gets renamed between X-D to "newname" and X-E also to
876 # "newname" to resolve it as both sides renaming it to the same new
877 # name. It is akin to what we do at the content level, i.e. "B and C do not
878 # agree what the final contents should be, so we leave the conflict marker
879 # but that may cancel out at the final merge stage".
881 test_expect_success
'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
882 test_create_repo rename-squared-squared &&
884 cd rename-squared-squared &&
886 printf "1\n2\n3\n4\n5\n6\n" >a &&
891 git checkout -b B A &&
897 git checkout -b C A &&
901 git checkout -q B^0 &&
902 git merge --no-commit -s ours C^0 &&
904 git commit -m "Merge commit C^0 into HEAD" &&
907 git checkout -q C^0 &&
908 git merge --no-commit -s ours B^0 &&
910 printf "7\n8\n" >>newname &&
912 git commit -m "Merge commit B^0 into HEAD" &&
917 test_expect_success
'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
919 cd rename-squared-squared &&
923 git merge -s recursive E^0 &&
925 git ls-files -s >out &&
926 test_line_count = 1 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 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
937 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
947 # Commit A: new file: a
948 # Commit B: rename a->b
949 # Commit C: rename a->c, add different a
950 # Commit D: merge B&C, keeping b&c and (new) a modified at beginning
951 # Commit E: merge B&C, keeping b&c and (new) a modified at end
953 # Merging commits D & E should result in no conflict; doing so correctly
954 # requires getting the virtual merge base (from merging B&C) right, handling
955 # renaming carefully (both in the virtual merge base and later), and getting
956 # content merge handled.
958 test_expect_success
'setup criss-cross + rename/rename/add-source + modify/modify' '
959 test_create_repo rename-rename-add-source &&
961 cd rename-rename-add-source &&
963 printf "lots\nof\nwords\nand\ncontent\n" >a &&
968 git checkout -b B A &&
972 git checkout -b C A &&
974 printf "2\n3\n4\n5\n6\n7\n" >a &&
979 git merge --no-commit -s ours C^0 &&
980 git checkout C -- a c &&
986 git commit -m "Merge commit C^0 into HEAD" &&
990 git merge --no-commit -s ours B^0 &&
991 git checkout B -- b &&
994 git commit -m "Merge commit B^0 into HEAD" &&
999 test_expect_failure
'detect rename/rename/add-source for virtual merge-base' '
1001 cd rename-rename-add-source &&
1005 git merge -s recursive E^0 &&
1007 git ls-files -s >out &&
1008 test_line_count = 3 out &&
1009 git ls-files -u >out &&
1010 test_line_count = 0 out &&
1011 git ls-files -o >out &&
1012 test_line_count = 1 out &&
1014 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >correct &&
1015 git rev-parse >expect \
1018 git rev-parse >actual \
1020 git hash-object >>actual \
1022 test_cmp expect actual
1027 # criss-cross with rename/rename(1to2)/add-dest + simple modify:
1037 # Commit A: new file: a
1038 # Commit B: rename a->b, add c
1039 # Commit C: rename a->c
1040 # Commit D: merge B&C, keeping A:a and B:c
1041 # Commit E: merge B&C, keeping A:a and slightly modified c from B
1043 # Merging commits D & E should result in no conflict. The virtual merge
1044 # base of B & C needs to not delete B:c for that to work, though...
1046 test_expect_success
'setup criss-cross+rename/rename/add-dest + simple modify' '
1047 test_create_repo rename-rename-add-dest &&
1049 cd rename-rename-add-dest &&
1056 git checkout -b B A &&
1058 printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
1062 git checkout -b C A &&
1067 git merge --no-commit -s ours C^0 &&
1069 git commit -m "D is like B but renames b back to a" &&
1073 git merge --no-commit -s ours C^0 &&
1077 git commit -m "E like D but has mod in c" &&
1082 test_expect_success
'virtual merge base handles rename/rename(1to2)/add-dest' '
1084 cd rename-rename-add-dest &&
1088 git merge -s recursive E^0 &&
1090 git ls-files -s >out &&
1091 test_line_count = 2 out &&
1092 git ls-files -u >out &&
1093 test_line_count = 0 out &&
1094 git ls-files -o >out &&
1095 test_line_count = 1 out &&
1097 git rev-parse >expect \
1099 git rev-parse >actual \
1101 test_cmp expect actual
1106 # criss-cross with modify/modify on a symlink:
1116 # Commit A: simple simlink fickle->lagoon
1117 # Commit B: redirect fickle->disneyland
1118 # Commit C: redirect fickle->home
1119 # Commit D: merge B&C, resolving in favor of B
1120 # Commit E: merge B&C, resolving in favor of C
1122 # This is an obvious modify/modify conflict for the symlink 'fickle'. Can
1125 test_expect_success
'setup symlink modify/modify' '
1126 test_create_repo symlink-modify-modify &&
1128 cd symlink-modify-modify &&
1130 test_ln_s_add lagoon fickle &&
1134 git checkout -b B A &&
1136 test_ln_s_add disneyland fickle &&
1139 git checkout -b C A &&
1141 test_ln_s_add home fickle &&
1145 git checkout -q B^0 &&
1146 git merge -s ours -m D C^0 &&
1149 git checkout -q C^0 &&
1150 git merge -s ours -m E B^0 &&
1155 test_expect_merge_algorithm failure success
'check symlink modify/modify' '
1157 cd symlink-modify-modify &&
1161 test_must_fail git merge -s recursive E^0 &&
1163 git ls-files -s >out &&
1164 test_line_count = 3 out &&
1165 git ls-files -u >out &&
1166 test_line_count = 3 out &&
1167 git ls-files -o >out &&
1168 test_line_count = 1 out
1173 # criss-cross with add/add of a symlink:
1183 # Commit A: No symlink or path exists yet
1184 # Commit B: set up symlink: fickle->disneyland
1185 # Commit C: set up symlink: fickle->home
1186 # Commit D: merge B&C, resolving in favor of B
1187 # Commit E: merge B&C, resolving in favor of C
1189 # This is an obvious add/add conflict for the symlink 'fickle'. Can
1192 test_expect_success
'setup symlink add/add' '
1193 test_create_repo symlink-add-add &&
1195 cd symlink-add-add &&
1202 git checkout -b B A &&
1203 test_ln_s_add disneyland fickle &&
1206 git checkout -b C A &&
1207 test_ln_s_add home fickle &&
1211 git checkout -q B^0 &&
1212 git merge -s ours -m D C^0 &&
1215 git checkout -q C^0 &&
1216 git merge -s ours -m E B^0 &&
1221 test_expect_merge_algorithm failure success
'check symlink add/add' '
1223 cd symlink-add-add &&
1227 test_must_fail git merge -s recursive E^0 &&
1229 git ls-files -s >out &&
1230 test_line_count = 3 out &&
1231 git ls-files -u >out &&
1232 test_line_count = 2 out &&
1233 git ls-files -o >out &&
1234 test_line_count = 1 out
1239 # criss-cross with modify/modify on a submodule:
1249 # Commit A: simple submodule repo
1250 # Commit B: update repo
1251 # Commit C: update repo differently
1252 # Commit D: merge B&C, resolving in favor of B
1253 # Commit E: merge B&C, resolving in favor of C
1255 # This is an obvious modify/modify conflict for the submodule 'repo'. Can
1258 test_expect_success
'setup submodule modify/modify' '
1259 test_create_repo submodule-modify-modify &&
1261 cd submodule-modify-modify &&
1263 test_create_repo submod &&
1271 git checkout -b B A &&
1277 git checkout -b C A &&
1284 git -C submod reset --hard A &&
1289 git checkout -b B A &&
1290 git -C submod reset --hard B &&
1294 git checkout -b C A &&
1295 git -C submod reset --hard C &&
1299 git checkout -q B^0 &&
1300 git merge -s ours -m D C^0 &&
1303 git checkout -q C^0 &&
1304 git merge -s ours -m E B^0 &&
1309 test_expect_merge_algorithm failure success
'check submodule modify/modify' '
1311 cd submodule-modify-modify &&
1315 test_must_fail git merge -s recursive E^0 &&
1317 git ls-files -s >out &&
1318 test_line_count = 3 out &&
1319 git ls-files -u >out &&
1320 test_line_count = 3 out &&
1321 git ls-files -o >out &&
1322 test_line_count = 1 out
1327 # criss-cross with add/add on a submodule:
1337 # Commit A: nothing of note
1338 # Commit B: introduce submodule repo
1339 # Commit C: introduce submodule repo at different commit
1340 # Commit D: merge B&C, resolving in favor of B
1341 # Commit E: merge B&C, resolving in favor of C
1343 # This is an obvious add/add conflict for the submodule 'repo'. Can
1346 test_expect_success
'setup submodule add/add' '
1347 test_create_repo submodule-add-add &&
1349 cd submodule-add-add &&
1351 test_create_repo submod &&
1359 git checkout -b B A &&
1365 git checkout -b C A &&
1372 touch irrelevant-file &&
1373 git add irrelevant-file &&
1377 git checkout -b B A &&
1378 git -C submod reset --hard B &&
1382 git checkout -b C A &&
1383 git -C submod reset --hard C &&
1387 git checkout -q B^0 &&
1388 git merge -s ours -m D C^0 &&
1391 git checkout -q C^0 &&
1392 git merge -s ours -m E B^0 &&
1397 test_expect_merge_algorithm failure success
'check submodule add/add' '
1399 cd submodule-add-add &&
1403 test_must_fail git merge -s recursive E^0 &&
1405 git ls-files -s >out &&
1406 test_line_count = 3 out &&
1407 git ls-files -u >out &&
1408 test_line_count = 2 out &&
1409 git ls-files -o >out &&
1410 test_line_count = 1 out
1415 # criss-cross with conflicting entry types:
1425 # Commit A: nothing of note
1426 # Commit B: introduce submodule 'path'
1427 # Commit C: introduce symlink 'path'
1428 # Commit D: merge B&C, resolving in favor of B
1429 # Commit E: merge B&C, resolving in favor of C
1431 # This is an obvious add/add conflict for 'path'. Can git detect it?
1433 test_expect_success
'setup conflicting entry types (submodule vs symlink)' '
1434 test_create_repo submodule-symlink-add-add &&
1436 cd submodule-symlink-add-add &&
1438 test_create_repo path &&
1447 touch irrelevant-file &&
1448 git add irrelevant-file &&
1452 git checkout -b B A &&
1453 git -C path reset --hard B &&
1457 git checkout -b C A &&
1459 test_ln_s_add irrelevant-file path &&
1462 git checkout -q B^0 &&
1463 git merge -s ours -m D C^0 &&
1466 git checkout -q C^0 &&
1467 git merge -s ours -m E B^0 &&
1472 test_expect_merge_algorithm failure success
'check conflicting entry types (submodule vs symlink)' '
1474 cd submodule-symlink-add-add &&
1478 test_must_fail git merge -s recursive E^0 &&
1480 git ls-files -s >out &&
1481 test_line_count = 3 out &&
1482 git ls-files -u >out &&
1483 test_line_count = 2 out &&
1484 git ls-files -o >out &&
1485 test_line_count = 1 out
1490 # criss-cross with regular files that have conflicting modes:
1500 # Commit A: nothing of note
1501 # Commit B: introduce file source_me.bash, not executable
1502 # Commit C: introduce file source_me.bash, executable
1503 # Commit D: merge B&C, resolving in favor of B
1504 # Commit E: merge B&C, resolving in favor of C
1506 # This is an obvious add/add mode conflict. Can git detect it?
1508 test_expect_success
'setup conflicting modes for regular file' '
1509 test_create_repo regular-file-mode-conflict &&
1511 cd regular-file-mode-conflict &&
1513 touch irrelevant-file &&
1514 git add irrelevant-file &&
1518 git checkout -b B A &&
1519 echo "command_to_run" >source_me.bash &&
1520 git add source_me.bash &&
1523 git checkout -b C A &&
1524 echo "command_to_run" >source_me.bash &&
1525 git add source_me.bash &&
1526 test_chmod +x source_me.bash &&
1529 git checkout -q B^0 &&
1530 git merge -s ours -m D C^0 &&
1533 git checkout -q C^0 &&
1534 git merge -s ours -m E B^0 &&
1539 test_expect_failure
'check conflicting modes for regular file' '
1541 cd regular-file-mode-conflict &&
1545 test_must_fail git merge -s recursive E^0 &&
1547 git ls-files -s >out &&
1548 test_line_count = 3 out &&
1549 git ls-files -u >out &&
1550 test_line_count = 2 out &&
1551 git ls-files -o >out &&
1552 test_line_count = 1 out
1564 # main has two files, named 'b' and 'a'
1565 # branches L1 and R1 both modify each of the two files in conflicting ways
1567 # L2 is a merge of R1 into L1; more on it later.
1568 # R2 is a merge of L1 into R1; more on it later.
1570 # X is an auto-generated merge-base used when merging L2 and R2.
1571 # since X is a merge of L1 and R1, it has conflicting versions of each file
1573 # More about L2 and R2:
1574 # - both resolve the conflicts in 'b' and 'a' differently
1575 # - L2 renames 'b' to 'm'
1576 # - R2 renames 'a' to 'm'
1578 # In the end, in file 'm' we have four different conflicting files (from
1579 # two versions of 'b' and two of 'a'). In addition, if
1580 # merge.conflictstyle is diff3, then the base version also has
1581 # conflict markers of its own, leading to a total of three levels of
1582 # conflict markers. This is a pretty weird corner case, but we just want
1583 # to ensure that we handle it as well as practical.
1585 test_expect_success
'setup nested conflicts' '
1586 test_create_repo nested_conflicts &&
1588 cd nested_conflicts &&
1590 # Create some related files now
1591 for i in $(test_seq 1 10)
1593 echo Random base content line $i
1605 test_write_lines b b_L1 >>b_L1 &&
1606 test_write_lines b b_R1 >>b_R1 &&
1607 test_write_lines b b_L2 >>b_L2 &&
1608 test_write_lines b b_R2 >>b_R2 &&
1609 test_write_lines a a_L1 >>a_L1 &&
1610 test_write_lines a a_R1 >>a_R1 &&
1611 test_write_lines a a_L2 >>a_L2 &&
1612 test_write_lines a a_R2 >>a_R2 &&
1614 # Setup original commit (or merge-base), consisting of
1615 # files named "b" and "a"
1621 test_tick && git commit -m initial &&
1626 # Handle the left side
1631 test_tick && git commit -m "version L1 of files" &&
1634 # Handle the right side
1639 test_tick && git commit -m "version R1 of files" &&
1642 # Create first merge on left side
1644 test_must_fail git merge R1 &&
1649 test_tick && git commit -m "left merge, rename b->m" &&
1652 # Create first merge on right side
1654 test_must_fail git merge L1 &&
1659 test_tick && git commit -m "right merge, rename a->m" &&
1664 test_expect_success
'check nested conflicts' '
1666 cd nested_conflicts &&
1669 MAIN=$(git rev-parse --short main) &&
1670 git checkout L2^0 &&
1672 # Merge must fail; there is a conflict
1673 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R2^0 &&
1675 # Make sure the index has the right number of entries
1676 git ls-files -s >out &&
1677 test_line_count = 2 out &&
1678 git ls-files -u >out &&
1679 test_line_count = 2 out &&
1680 # Ensure we have the correct number of untracked files
1681 git ls-files -o >out &&
1682 test_line_count = 1 out &&
1684 # Create a and b from virtual merge base X
1685 git cat-file -p main:a >base &&
1686 git cat-file -p L1:a >ours &&
1687 git cat-file -p R1:a >theirs &&
1688 test_must_fail git merge-file --diff3 \
1689 -L "Temporary merge branch 1" \
1691 -L "Temporary merge branch 2" \
1695 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_a &&
1697 git cat-file -p main:b >base &&
1698 git cat-file -p L1:b >ours &&
1699 git cat-file -p R1:b >theirs &&
1700 test_must_fail git merge-file --diff3 \
1701 -L "Temporary merge branch 1" \
1703 -L "Temporary merge branch 2" \
1707 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_b &&
1709 # Compare :2:m to expected values
1710 git cat-file -p L2:m >ours &&
1711 git cat-file -p R2:b >theirs &&
1712 test_must_fail git merge-file --diff3 \
1714 -L "merged common ancestors:b" \
1719 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_2 &&
1720 git cat-file -p :2:m >actual &&
1721 test_cmp m_stage_2 actual &&
1723 # Compare :3:m to expected values
1724 git cat-file -p L2:a >ours &&
1725 git cat-file -p R2:m >theirs &&
1726 test_must_fail git merge-file --diff3 \
1728 -L "merged common ancestors:a" \
1733 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_3 &&
1734 git cat-file -p :3:m >actual &&
1735 test_cmp m_stage_3 actual &&
1737 # Compare m to expected contents
1739 cp m_stage_2 expected_final_m &&
1740 test_must_fail git merge-file --diff3 \
1742 -L "merged common ancestors" \
1747 test_cmp expected_final_m m
1759 # main has one file named 'content'
1760 # branches L1 and R1 both modify each of the two files in conflicting ways
1762 # L<n> (n>1) is a merge of R<n-1> into L<n-1>
1763 # R<n> (n>1) is a merge of L<n-1> into R<n-1>
1764 # L<n> and R<n> resolve the conflicts differently.
1766 # X<n> is an auto-generated merge-base used when merging L<n+1> and R<n+1>.
1767 # By construction, X1 has conflict markers due to conflicting versions.
1768 # X2, due to using merge.conflictstyle=3, has nested conflict markers.
1770 # So, merging R3 into L3 using merge.conflictstyle=3 should show the
1771 # nested conflict markers from X2 in the base version -- that means we
1772 # have three levels of conflict markers. Can we distinguish all three?
1774 test_expect_success
'setup virtual merge base with nested conflicts' '
1775 test_create_repo virtual_merge_base_has_nested_conflicts &&
1777 cd virtual_merge_base_has_nested_conflicts &&
1779 # Create some related files now
1780 for i in $(test_seq 1 10)
1782 echo Random base content line $i
1785 # Setup original commit
1787 test_tick && git commit -m initial &&
1794 echo left >>content &&
1796 test_tick && git commit -m "version L1 of content" &&
1801 echo right >>content &&
1803 test_tick && git commit -m "version R1 of content" &&
1808 test_must_fail git -c merge.conflictstyle=diff3 merge R1 &&
1809 git checkout L1 content &&
1810 test_tick && git commit -m "version L2 of content" &&
1815 test_must_fail git -c merge.conflictstyle=diff3 merge L1 &&
1816 git checkout R1 content &&
1817 test_tick && git commit -m "version R2 of content" &&
1822 test_must_fail git -c merge.conflictstyle=diff3 merge R2 &&
1823 git checkout L1 content &&
1824 test_tick && git commit -m "version L3 of content" &&
1829 test_must_fail git -c merge.conflictstyle=diff3 merge L2 &&
1830 git checkout R1 content &&
1831 test_tick && git commit -m "version R3 of content" &&
1836 test_expect_success
'check virtual merge base with nested conflicts' '
1838 cd virtual_merge_base_has_nested_conflicts &&
1840 MAIN=$(git rev-parse --short main) &&
1841 git checkout L3^0 &&
1843 # Merge must fail; there is a conflict
1844 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R3^0 &&
1846 # Make sure the index has the right number of entries
1847 git ls-files -s >out &&
1848 test_line_count = 3 out &&
1849 git ls-files -u >out &&
1850 test_line_count = 3 out &&
1851 # Ensure we have the correct number of untracked files
1852 git ls-files -o >out &&
1853 test_line_count = 1 out &&
1855 # Compare :[23]:content to expected values
1856 git rev-parse L1:content R1:content >expect &&
1857 git rev-parse :2:content :3:content >actual &&
1858 test_cmp expect actual &&
1860 # Imitate X1 merge base, except without long enough conflict
1861 # markers because a subsequent sed will modify them. Put
1863 git cat-file -p main:content >base &&
1864 git cat-file -p L:content >left &&
1865 git cat-file -p R:content >right &&
1866 cp left merged-once &&
1867 test_must_fail git merge-file --diff3 \
1868 -L "Temporary merge branch 1" \
1870 -L "Temporary merge branch 2" \
1874 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-once >vmb &&
1876 # Imitate X2 merge base, overwriting vmb. Note that we
1877 # extend both sets of conflict markers to make them longer
1878 # with the sed command.
1879 cp left merged-twice &&
1880 test_must_fail git merge-file --diff3 \
1881 -L "Temporary merge branch 1" \
1882 -L "merged common ancestors" \
1883 -L "Temporary merge branch 2" \
1887 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-twice >vmb &&
1889 # Compare :1:content to expected value
1890 git cat-file -p :1:content >actual &&
1891 test_cmp vmb actual &&
1893 # Determine expected content in final outer merge, compare to
1894 # what the merge generated.
1895 cp -f left expect &&
1896 test_must_fail git merge-file --diff3 \
1897 -L "HEAD" -L "merged common ancestors" -L "R3^0" \
1899 test_cmp expect content