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 git init basic-rename &&
26 ten="0 1 2 3 4 5 6 7 8 9" &&
27 printf "line %d in a sample file\n" $ten >one &&
28 printf "line %d in another sample file\n" $ten >two &&
30 test_tick && git commit -m initial &&
35 test_tick && git commit -m R1 &&
39 test_tick && git commit -m L1 &&
42 test_tick && git merge -s ours R1 &&
46 test_tick && git merge -s ours L1 &&
51 test_expect_success
'merge simple rename+criss-cross with no modifications' '
58 test_must_fail git merge -s recursive R2^0 &&
60 git ls-files -s >out &&
61 test_line_count = 5 out &&
62 git ls-files -u >out &&
63 test_line_count = 3 out &&
64 git ls-files -o >out &&
65 test_line_count = 1 out &&
67 git rev-parse >expect \
69 git rev-parse >actual \
71 test_cmp expect actual
76 # Same as before, but modify L1 slightly:
87 test_expect_success
'setup criss-cross + rename merges with basic modification' '
88 git init rename-modify &&
92 ten="0 1 2 3 4 5 6 7 8 9" &&
93 printf "line %d in a sample file\n" $ten >one &&
94 printf "line %d in another sample file\n" $ten >two &&
96 test_tick && git commit -m initial &&
103 test_tick && git commit -m R1 &&
107 test_tick && git commit -m L1 &&
110 test_tick && git merge -s ours R1 &&
114 test_tick && git merge -s ours L1 &&
119 test_expect_success
'merge criss-cross + rename merges with basic modification' '
125 test_must_fail git merge -s recursive R2^0 &&
127 git ls-files -s >out &&
128 test_line_count = 5 out &&
129 git ls-files -u >out &&
130 test_line_count = 3 out &&
131 git ls-files -o >out &&
132 test_line_count = 1 out &&
134 git rev-parse >expect \
136 git rev-parse >actual \
138 test_cmp expect actual
143 # For the next test, we start with three commits in two lines of development
144 # which setup a rename/add conflict:
145 # Commit A: File 'a' exists
146 # Commit B: Rename 'a' -> 'new_a'
147 # Commit C: Modify 'a', create different 'new_a'
148 # Later, two different people merge and resolve differently:
149 # Commit D: Merge B & C, ignoring separately created 'new_a'
150 # Commit E: Merge B & C making use of some piece of secondary 'new_a'
151 # Finally, someone goes to merge D & E. Does git detect the conflict?
162 test_expect_success
'setup differently handled merges of rename/add conflict' '
163 git init rename-add &&
167 printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
169 test_tick && git commit -m A &&
174 test_write_lines 0 1 2 3 4 5 6 7 foobar >new_a &&
176 test_tick && git commit -m C &&
180 test_tick && git commit -m B &&
183 test_must_fail git merge C &&
184 git show :2:new_a >new_a &&
186 test_tick && git commit -m D &&
190 test_must_fail git merge B &&
191 test_write_lines 0 1 2 3 4 5 6 7 bad_merge >new_a &&
193 test_tick && git commit -m E &&
198 test_expect_success
'git detects differently handled merges conflict' '
204 test_must_fail git merge -s recursive E^0 &&
206 git ls-files -s >out &&
207 test_line_count = 3 out &&
208 git ls-files -u >out &&
209 test_line_count = 3 out &&
210 git ls-files -o >out &&
211 test_line_count = 1 out &&
213 git cat-file -p C:new_a >ours &&
214 git cat-file -p C:a >theirs &&
216 test_must_fail git merge-file \
217 -L "Temporary merge branch 1" \
219 -L "Temporary merge branch 2" \
221 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
222 git hash-object ours-tweaked >expect &&
223 git rev-parse >>expect \
225 git rev-parse >actual \
226 :1:new_a :2:new_a :3:new_a &&
227 test_cmp expect actual &&
229 # Test that the two-way merge in new_a is as expected
230 git cat-file -p D:new_a >ours &&
231 git cat-file -p E:new_a >theirs &&
233 test_must_fail git merge-file \
238 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
239 git hash-object new_a >actual &&
240 git hash-object ours >expect &&
241 test_cmp expect actual
245 # Repeat the above testcase with precisely the same setup, other than with
246 # the two merge bases having different orderings of commit timestamps so
247 # that they are reversed in the order they are provided to merge-recursive,
248 # so that we can improve code coverage.
249 test_expect_success
'git detects differently handled merges conflict, swapped' '
253 # Difference #1: Do cleanup from previous testrun
257 # Difference #2: Change commit timestamps
258 btime=$(git log --no-walk --date=raw --format=%cd B | awk "{print \$1}") &&
259 ctime=$(git log --no-walk --date=raw --format=%cd C | awk "{print \$1}") &&
260 newctime=$(($btime+1)) &&
261 git fast-export --no-data --all | sed -e s/$ctime/$newctime/ | git fast-import --force --quiet &&
262 # End of most differences; rest is copy-paste of last test,
263 # other than swapping C:a and C:new_a due to order switch
266 test_must_fail git merge -s recursive E^0 &&
268 git ls-files -s >out &&
269 test_line_count = 3 out &&
270 git ls-files -u >out &&
271 test_line_count = 3 out &&
272 git ls-files -o >out &&
273 test_line_count = 1 out &&
275 git cat-file -p C:a >ours &&
276 git cat-file -p C:new_a >theirs &&
278 test_must_fail git merge-file \
279 -L "Temporary merge branch 1" \
281 -L "Temporary merge branch 2" \
283 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
284 git hash-object ours-tweaked >expect &&
285 git rev-parse >>expect \
287 git rev-parse >actual \
288 :1:new_a :2:new_a :3:new_a &&
289 test_cmp expect actual &&
291 # Test that the two-way merge in new_a is as expected
292 git cat-file -p D:new_a >ours &&
293 git cat-file -p E:new_a >theirs &&
295 test_must_fail git merge-file \
300 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
301 git hash-object new_a >actual &&
302 git hash-object ours >expect &&
303 test_cmp expect actual
308 # criss-cross + modify/delete:
318 # Commit A: file with contents 'A\n'
319 # Commit B: file with contents 'B\n'
320 # Commit C: file not present
321 # Commit D: file with contents 'B\n'
322 # Commit E: file not present
324 # Merging commits D & E should result in modify/delete conflict.
326 test_expect_success
'setup criss-cross + modify/delete resolved differently' '
327 git init modify-delete &&
349 test_must_fail git merge C &&
357 test_must_fail git merge B &&
365 test_expect_success
'git detects conflict merging criss-cross+modify/delete' '
371 test_must_fail git merge -s recursive E^0 &&
373 git ls-files -s >out &&
374 test_line_count = 2 out &&
375 git ls-files -u >out &&
376 test_line_count = 2 out &&
378 git rev-parse >expect \
380 git rev-parse >actual \
382 test_cmp expect actual
386 test_expect_success
'git detects conflict merging criss-cross+modify/delete, reverse direction' '
393 test_must_fail git merge -s recursive D^0 &&
395 git ls-files -s >out &&
396 test_line_count = 2 out &&
397 git ls-files -u >out &&
398 test_line_count = 2 out &&
400 git rev-parse >expect \
402 git rev-parse >actual \
404 test_cmp expect actual
408 # SORRY FOR THE SUPER LONG DESCRIPTION, BUT THIS NEXT ONE IS HAIRY
410 # criss-cross + d/f conflict via add/add:
411 # Commit A: Neither file 'a' nor directory 'a/' exists.
412 # Commit B: Introduce 'a'
413 # Commit C: Introduce 'a/file'
414 # Commit D1: Merge B & C, keeping 'a' and deleting 'a/'
415 # Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
425 # I'll describe D2, E2, & E3 (which are alternatives for D1 & E1) more below...
427 # Merging D1 & E1 requires we first create a virtual merge base X from
428 # merging A & B in memory. There are several possibilities for the merge-base:
429 # 1: Keep both 'a' and 'a/file' (assuming crazy filesystem allowing a tree
430 # with a directory and file at same path): results in merge of D1 & E1
431 # being clean with both files deleted. Bad (no conflict detected).
432 # 2: Keep 'a' but not 'a/file': Merging D1 & E1 is clean and matches E1. Bad.
433 # 3: Keep 'a/file' but not 'a': Merging D1 & E1 is clean and matches D1. Bad.
434 # 4: Keep neither file: Merging D1 & E1 reports the D/F add/add conflict.
436 # So 4 sounds good for this case, but if we were to merge D1 & E3, where E3
438 # Commit E3: Merge B & C, keeping modified a, and deleting a/
439 # then we'd get an add/add conflict for 'a', which seems suboptimal. A little
440 # creativity leads us to an alternate choice:
441 # 5: Keep 'a' as 'a~$UNIQUE' and a/file; results:
442 # Merge D1 & E1: rename/delete conflict for 'a'; a/file silently deleted
443 # Merge D1 & E3 is clean, as expected.
445 # So choice 5 at least provides some kind of conflict for the original case,
446 # and can merge cleanly as expected with D1 and E3. It also made things just
447 # slightly funny for merging D1 and E4, where E4 is defined as:
448 # Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
449 # in this case, we'll get a rename/rename(1to2) conflict because a~$UNIQUE
450 # gets renamed to 'a' in D1 and to 'a2' in E4. But that's better than having
451 # two files (both 'a' and 'a2') sitting around without the user being notified
452 # that we could detect they were related and need to be merged. Also, choice
453 # 5 makes the handling of 'a/file' seem suboptimal. What if we were to merge
454 # D2 and E4, where D2 is:
455 # Commit D2: Merge B & C, renaming 'a'->'a2', keeping 'a/file'
456 # This would result in a clean merge with 'a2' having three-way merged
457 # contents (good), and deleting 'a/' (bad) -- it doesn't detect the
458 # conflict in how the different sides treated a/file differently.
459 # Continuing down the creative route:
460 # 6: Keep 'a' as 'a~$UNIQUE1' and keep 'a/' as 'a~$UNIQUE2/'; results:
461 # Merge D1 & E1: rename/delete conflict for 'a' and each path under 'a/'.
462 # Merge D1 & E3: clean, as expected.
463 # Merge D1 & E4: rename/rename(1to2) conflict on 'a' vs 'a2'.
464 # Merge D2 & E4: clean for 'a2', rename/delete for a/file
466 # Choice 6 could cause rename detection to take longer (providing more targets
467 # that need to be searched). Also, the conflict message for each path under
468 # 'a/' might be annoying unless we can detect it at the directory level, print
469 # it once, and then suppress it for individual filepaths underneath.
472 # As of time of writing, git uses choice 5. Directory rename detection and
473 # rename detection performance improvements might make choice 6 a desirable
474 # improvement. But we can at least document where we fall short for now...
477 # Historically, this testcase also used:
478 # Commit E2: Merge B & C, deleting 'a' but keeping slightly modified 'a/file'
479 # The merge of D1 & E2 is very similar to D1 & E1 -- it has similar issues for
480 # path 'a', but should always result in a modify/delete conflict for path
481 # 'a/file'. These tests ran the two merges
484 # in both directions, to check for directional issues with D/F conflict
485 # handling. Later we added
489 # for good measure, though we only ran those one way because we had pretty
490 # good confidence in merge-recursive's directional handling of D/F issues.
492 # Just to summarize all the intermediate merge commits:
493 # Commit D1: Merge B & C, keeping a and deleting a/
494 # Commit D2: Merge B & C, renaming a->a2, keeping a/file
495 # Commit E1: Merge B & C, deleting a but keeping a/file
496 # Commit E2: Merge B & C, deleting a but keeping slightly modified a/file
497 # Commit E3: Merge B & C, keeping modified a, and deleting a/
498 # Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
501 test_expect_success
'setup differently handled merges of directory/file conflict' '
502 git init directory-file &&
515 test_write_lines a b c d e f g >a/file &&
521 test_write_lines 1 2 3 4 5 6 7 >a &&
527 git merge -s ours -m D1 C^0 &&
531 test_must_fail git merge C^0 &&
532 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
541 git cat-file -p B:a >a2 &&
547 git merge -s ours -m E1 B^0 &&
551 git merge -s ours -m E2 B^0 &&
552 test_write_lines a b c d e f g h >a/file &&
554 git commit --amend -C HEAD &&
558 test_must_fail git merge B^0 &&
559 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
566 test_write_lines 1 2 3 4 5 6 7 8 >a &&
572 test_must_fail git merge B^0 &&
573 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
582 test_write_lines 1 2 3 4 5 6 7 8 >a2 &&
589 test_expect_success
'merge of D1 & E1 fails but has appropriate contents' '
590 test_when_finished "git -C directory-file reset --hard" &&
591 test_when_finished "git -C directory-file clean -fdqx" &&
597 test_must_fail git merge -s recursive E1^0 &&
599 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
601 git ls-files -s >out &&
602 test_line_count = 3 out &&
603 git ls-files -u >out &&
604 test_line_count = 2 out &&
605 git ls-files -o >out &&
606 test_line_count = 1 out &&
608 git rev-parse >expect \
609 A:ignore-me B:a D1:a &&
610 git rev-parse >actual \
611 :0:ignore-me :1:a :2:a &&
612 test_cmp expect actual
614 git ls-files -s >out &&
615 test_line_count = 2 out &&
616 git ls-files -u >out &&
617 test_line_count = 1 out &&
618 git ls-files -o >out &&
619 test_line_count = 1 out &&
621 git rev-parse >expect \
623 git rev-parse >actual \
625 test_cmp expect actual
630 test_expect_success
'merge of E1 & D1 fails but has appropriate contents' '
631 test_when_finished "git -C directory-file reset --hard" &&
632 test_when_finished "git -C directory-file clean -fdqx" &&
638 test_must_fail git merge -s recursive D1^0 &&
640 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
642 git ls-files -s >out &&
643 test_line_count = 3 out &&
644 git ls-files -u >out &&
645 test_line_count = 2 out &&
646 git ls-files -o >out &&
647 test_line_count = 1 out &&
649 git rev-parse >expect \
650 A:ignore-me B:a D1:a &&
651 git rev-parse >actual \
652 :0:ignore-me :1:a :3:a &&
653 test_cmp expect actual
655 git ls-files -s >out &&
656 test_line_count = 2 out &&
657 git ls-files -u >out &&
658 test_line_count = 1 out &&
659 git ls-files -o >out &&
660 test_line_count = 1 out &&
662 git rev-parse >expect \
664 git rev-parse >actual \
666 test_cmp expect actual
671 test_expect_success
'merge of D1 & E2 fails but has appropriate contents' '
672 test_when_finished "git -C directory-file reset --hard" &&
673 test_when_finished "git -C directory-file clean -fdqx" &&
679 test_must_fail git merge -s recursive E2^0 &&
681 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
683 git ls-files -s >out &&
684 test_line_count = 5 out &&
685 git ls-files -u >out &&
686 test_line_count = 4 out &&
687 git ls-files -o >out &&
688 test_line_count = 1 out &&
690 git rev-parse >expect \
691 B:a D1:a E2:a/file C:a/file A:ignore-me &&
692 git rev-parse >actual \
693 :1:a~HEAD :2:a~HEAD :3:a/file :1:a/file :0:ignore-me
695 git ls-files -s >out &&
696 test_line_count = 4 out &&
697 git ls-files -u >out &&
698 test_line_count = 3 out &&
699 git ls-files -o >out &&
700 test_line_count = 2 out &&
702 git rev-parse >expect \
703 B:a E2:a/file C:a/file A:ignore-me &&
704 git rev-parse >actual \
705 :2:a :3:a/file :1:a/file :0:ignore-me
707 test_cmp expect actual &&
709 test_path_is_file a~HEAD
713 test_expect_success
'merge of E2 & D1 fails but has appropriate contents' '
714 test_when_finished "git -C directory-file reset --hard" &&
715 test_when_finished "git -C directory-file clean -fdqx" &&
721 test_must_fail git merge -s recursive D1^0 &&
723 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
725 git ls-files -s >out &&
726 test_line_count = 5 out &&
727 git ls-files -u >out &&
728 test_line_count = 4 out &&
729 git ls-files -o >out &&
730 test_line_count = 1 out &&
732 git rev-parse >expect \
733 B:a D1:a E2:a/file C:a/file A:ignore-me &&
734 git rev-parse >actual \
735 :1:a~D1^0 :3:a~D1^0 :2:a/file :1:a/file :0:ignore-me
737 git ls-files -s >out &&
738 test_line_count = 4 out &&
739 git ls-files -u >out &&
740 test_line_count = 3 out &&
741 git ls-files -o >out &&
742 test_line_count = 2 out &&
744 git rev-parse >expect \
745 B:a E2:a/file C:a/file A:ignore-me &&
746 git rev-parse >actual \
747 :3:a :2:a/file :1:a/file :0:ignore-me
749 test_cmp expect actual &&
751 test_path_is_file a~D1^0
755 test_expect_success
'merge of D1 & E3 succeeds' '
756 test_when_finished "git -C directory-file reset --hard" &&
757 test_when_finished "git -C directory-file clean -fdqx" &&
763 git merge -s recursive E3^0 &&
765 git ls-files -s >out &&
766 test_line_count = 2 out &&
767 git ls-files -u >out &&
768 test_line_count = 0 out &&
769 git ls-files -o >out &&
770 test_line_count = 1 out &&
772 git rev-parse >expect \
774 git rev-parse >actual \
776 test_cmp expect actual
780 test_expect_merge_algorithm failure success
'merge of D1 & E4 puts merge of a and a2 in both a and a2' '
781 test_when_finished "git -C directory-file reset --hard" &&
782 test_when_finished "git -C directory-file clean -fdqx" &&
788 test_must_fail git merge -s recursive E4^0 &&
790 git ls-files -s >out &&
791 test_line_count = 4 out &&
792 git ls-files -u >out &&
793 test_line_count = 3 out &&
794 git ls-files -o >out &&
795 test_line_count = 1 out &&
797 git rev-parse >expect \
798 A:ignore-me B:a E4:a2 E4:a2 &&
799 git rev-parse >actual \
800 :0:ignore-me :1:a~Temporary\ merge\ branch\ 2 :2:a :3:a2 &&
801 test_cmp expect actual
805 test_expect_failure
'merge of D2 & E4 merges a2s & reports conflict for a/file' '
806 test_when_finished "git -C directory-file reset --hard" &&
807 test_when_finished "git -C directory-file clean -fdqx" &&
813 test_must_fail git merge -s recursive E4^0 &&
815 git ls-files -s >out &&
816 test_line_count = 3 out &&
817 git ls-files -u >out &&
818 test_line_count = 1 out &&
819 git ls-files -o >out &&
820 test_line_count = 1 out &&
822 git rev-parse >expect \
823 A:ignore-me E4:a2 D2:a/file &&
824 git rev-parse >actual \
825 :0:ignore-me :0:a2 :2:a/file &&
826 test_cmp expect actual
831 # criss-cross with rename/rename(1to2)/modify followed by
832 # rename/rename(2to1)/modify:
842 # Commit A: new file: a
843 # Commit B: rename a->b, modifying by adding a line
844 # Commit C: rename a->c
845 # Commit D: merge B&C, resolving conflict by keeping contents in newname
846 # Commit E: merge B&C, resolving conflict similar to D but adding another line
848 # There is a conflict merging B & C, but one of filename not of file
849 # content. Whoever created D and E chose specific resolutions for that
850 # conflict resolution. Now, since: (1) there is no content conflict
851 # merging B & C, (2) D does not modify that merged content further, and (3)
852 # both D & E resolve the name conflict in the same way, the modification to
853 # newname in E should not cause any conflicts when it is merged with D.
854 # (Note that this can be accomplished by having the virtual merge base have
855 # the merged contents of b and c stored in a file named a, which seems like
856 # the most logical choice anyway.)
858 # Comment from Junio: I do not necessarily agree with the choice "a", but
859 # it feels sound to say "B and C do not agree what the final pathname
860 # should be, but we know this content was derived from the common A:a so we
861 # use one path whose name is arbitrary in the virtual merge base X between
862 # D and E" and then further let the rename detection to notice that that
863 # arbitrary path gets renamed between X-D to "newname" and X-E also to
864 # "newname" to resolve it as both sides renaming it to the same new
865 # name. It is akin to what we do at the content level, i.e. "B and C do not
866 # agree what the final contents should be, so we leave the conflict marker
867 # but that may cancel out at the final merge stage".
869 test_expect_success
'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
870 git init rename-squared-squared &&
872 cd rename-squared-squared &&
874 printf "1\n2\n3\n4\n5\n6\n" >a &&
879 git checkout -b B A &&
885 git checkout -b C A &&
889 git checkout -q B^0 &&
890 git merge --no-commit -s ours C^0 &&
892 git commit -m "Merge commit C^0 into HEAD" &&
895 git checkout -q C^0 &&
896 git merge --no-commit -s ours B^0 &&
898 printf "7\n8\n" >>newname &&
900 git commit -m "Merge commit B^0 into HEAD" &&
905 test_expect_success
'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
907 cd rename-squared-squared &&
911 git merge -s recursive E^0 &&
913 git ls-files -s >out &&
914 test_line_count = 1 out &&
915 git ls-files -u >out &&
916 test_line_count = 0 out &&
917 git ls-files -o >out &&
918 test_line_count = 1 out &&
920 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
925 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
935 # Commit A: new file: a
936 # Commit B: rename a->b
937 # Commit C: rename a->c, add different a
938 # Commit D: merge B&C, keeping b&c and (new) a modified at beginning
939 # Commit E: merge B&C, keeping b&c and (new) a modified at end
941 # Merging commits D & E should result in no conflict; doing so correctly
942 # requires getting the virtual merge base (from merging B&C) right, handling
943 # renaming carefully (both in the virtual merge base and later), and getting
944 # content merge handled.
946 test_expect_success
'setup criss-cross + rename/rename/add-source + modify/modify' '
947 git init rename-rename-add-source &&
949 cd rename-rename-add-source &&
951 printf "lots\nof\nwords\nand\ncontent\n" >a &&
956 git checkout -b B A &&
960 git checkout -b C A &&
962 printf "2\n3\n4\n5\n6\n7\n" >a &&
967 git merge --no-commit -s ours C^0 &&
968 git checkout C -- a c &&
974 git commit -m "Merge commit C^0 into HEAD" &&
978 git merge --no-commit -s ours B^0 &&
979 git checkout B -- b &&
982 git commit -m "Merge commit B^0 into HEAD" &&
987 test_expect_failure
'detect rename/rename/add-source for virtual merge-base' '
989 cd rename-rename-add-source &&
993 git merge -s recursive E^0 &&
995 git ls-files -s >out &&
996 test_line_count = 3 out &&
997 git ls-files -u >out &&
998 test_line_count = 0 out &&
999 git ls-files -o >out &&
1000 test_line_count = 1 out &&
1002 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >correct &&
1003 git rev-parse >expect \
1006 git rev-parse >actual \
1008 git hash-object >>actual \
1010 test_cmp expect actual
1015 # criss-cross with rename/rename(1to2)/add-dest + simple modify:
1025 # Commit A: new file: a
1026 # Commit B: rename a->b, add c
1027 # Commit C: rename a->c
1028 # Commit D: merge B&C, keeping A:a and B:c
1029 # Commit E: merge B&C, keeping A:a and slightly modified c from B
1031 # Merging commits D & E should result in no conflict. The virtual merge
1032 # base of B & C needs to not delete B:c for that to work, though...
1034 test_expect_success
'setup criss-cross+rename/rename/add-dest + simple modify' '
1035 git init rename-rename-add-dest &&
1037 cd rename-rename-add-dest &&
1044 git checkout -b B A &&
1046 printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
1050 git checkout -b C A &&
1055 git merge --no-commit -s ours C^0 &&
1057 git commit -m "D is like B but renames b back to a" &&
1061 git merge --no-commit -s ours C^0 &&
1065 git commit -m "E like D but has mod in c" &&
1070 test_expect_success
'virtual merge base handles rename/rename(1to2)/add-dest' '
1072 cd rename-rename-add-dest &&
1076 git merge -s recursive E^0 &&
1078 git ls-files -s >out &&
1079 test_line_count = 2 out &&
1080 git ls-files -u >out &&
1081 test_line_count = 0 out &&
1082 git ls-files -o >out &&
1083 test_line_count = 1 out &&
1085 git rev-parse >expect \
1087 git rev-parse >actual \
1089 test_cmp expect actual
1094 # criss-cross with modify/modify on a symlink:
1104 # Commit A: simple simlink fickle->lagoon
1105 # Commit B: redirect fickle->disneyland
1106 # Commit C: redirect fickle->home
1107 # Commit D: merge B&C, resolving in favor of B
1108 # Commit E: merge B&C, resolving in favor of C
1110 # This is an obvious modify/modify conflict for the symlink 'fickle'. Can
1113 test_expect_success
'setup symlink modify/modify' '
1114 git init symlink-modify-modify &&
1116 cd symlink-modify-modify &&
1118 test_ln_s_add lagoon fickle &&
1122 git checkout -b B A &&
1124 test_ln_s_add disneyland fickle &&
1127 git checkout -b C A &&
1129 test_ln_s_add home fickle &&
1133 git checkout -q B^0 &&
1134 git merge -s ours -m D C^0 &&
1137 git checkout -q C^0 &&
1138 git merge -s ours -m E B^0 &&
1143 test_expect_merge_algorithm failure success
'check symlink modify/modify' '
1145 cd symlink-modify-modify &&
1149 test_must_fail git merge -s recursive E^0 &&
1151 git ls-files -s >out &&
1152 test_line_count = 3 out &&
1153 git ls-files -u >out &&
1154 test_line_count = 3 out &&
1155 git ls-files -o >out &&
1156 test_line_count = 1 out
1161 # criss-cross with add/add of a symlink:
1171 # Commit A: No symlink or path exists yet
1172 # Commit B: set up symlink: fickle->disneyland
1173 # Commit C: set up symlink: fickle->home
1174 # Commit D: merge B&C, resolving in favor of B
1175 # Commit E: merge B&C, resolving in favor of C
1177 # This is an obvious add/add conflict for the symlink 'fickle'. Can
1180 test_expect_success
'setup symlink add/add' '
1181 git init symlink-add-add &&
1183 cd symlink-add-add &&
1190 git checkout -b B A &&
1191 test_ln_s_add disneyland fickle &&
1194 git checkout -b C A &&
1195 test_ln_s_add home fickle &&
1199 git checkout -q B^0 &&
1200 git merge -s ours -m D C^0 &&
1203 git checkout -q C^0 &&
1204 git merge -s ours -m E B^0 &&
1209 test_expect_merge_algorithm failure success
'check symlink add/add' '
1211 cd symlink-add-add &&
1215 test_must_fail git merge -s recursive E^0 &&
1217 git ls-files -s >out &&
1218 test_line_count = 3 out &&
1219 git ls-files -u >out &&
1220 test_line_count = 2 out &&
1221 git ls-files -o >out &&
1222 test_line_count = 1 out
1227 # criss-cross with modify/modify on a submodule:
1237 # Commit A: simple submodule repo
1238 # Commit B: update repo
1239 # Commit C: update repo differently
1240 # Commit D: merge B&C, resolving in favor of B
1241 # Commit E: merge B&C, resolving in favor of C
1243 # This is an obvious modify/modify conflict for the submodule 'repo'. Can
1246 test_expect_success
'setup submodule modify/modify' '
1247 git init submodule-modify-modify &&
1249 cd submodule-modify-modify &&
1259 git checkout -b B A &&
1265 git checkout -b C A &&
1272 git -C submod reset --hard A &&
1277 git checkout -b B A &&
1278 git -C submod reset --hard B &&
1282 git checkout -b C A &&
1283 git -C submod reset --hard C &&
1287 git checkout -q B^0 &&
1288 git merge -s ours -m D C^0 &&
1291 git checkout -q C^0 &&
1292 git merge -s ours -m E B^0 &&
1297 test_expect_merge_algorithm failure success
'check submodule modify/modify' '
1299 cd submodule-modify-modify &&
1303 test_must_fail git merge -s recursive E^0 &&
1305 git ls-files -s >out &&
1306 test_line_count = 3 out &&
1307 git ls-files -u >out &&
1308 test_line_count = 3 out &&
1309 git ls-files -o >out &&
1310 test_line_count = 1 out
1315 # criss-cross with add/add on a submodule:
1325 # Commit A: nothing of note
1326 # Commit B: introduce submodule repo
1327 # Commit C: introduce submodule repo at different commit
1328 # Commit D: merge B&C, resolving in favor of B
1329 # Commit E: merge B&C, resolving in favor of C
1331 # This is an obvious add/add conflict for the submodule 'repo'. Can
1334 test_expect_success
'setup submodule add/add' '
1335 git init submodule-add-add &&
1337 cd submodule-add-add &&
1347 git checkout -b B A &&
1353 git checkout -b C A &&
1360 touch irrelevant-file &&
1361 git add irrelevant-file &&
1365 git checkout -b B A &&
1366 git -C submod reset --hard B &&
1370 git checkout -b C A &&
1371 git -C submod reset --hard C &&
1375 git checkout -q B^0 &&
1376 git merge -s ours -m D C^0 &&
1379 git checkout -q C^0 &&
1380 git merge -s ours -m E B^0 &&
1385 test_expect_merge_algorithm failure success
'check submodule add/add' '
1387 cd submodule-add-add &&
1391 test_must_fail git merge -s recursive E^0 &&
1393 git ls-files -s >out &&
1394 test_line_count = 3 out &&
1395 git ls-files -u >out &&
1396 test_line_count = 2 out &&
1397 git ls-files -o >out &&
1398 test_line_count = 1 out
1403 # criss-cross with conflicting entry types:
1413 # Commit A: nothing of note
1414 # Commit B: introduce submodule 'path'
1415 # Commit C: introduce symlink 'path'
1416 # Commit D: merge B&C, resolving in favor of B
1417 # Commit E: merge B&C, resolving in favor of C
1419 # This is an obvious add/add conflict for 'path'. Can git detect it?
1421 test_expect_success
'setup conflicting entry types (submodule vs symlink)' '
1422 git init submodule-symlink-add-add &&
1424 cd submodule-symlink-add-add &&
1435 touch irrelevant-file &&
1436 git add irrelevant-file &&
1440 git checkout -b B A &&
1441 git -C path reset --hard B &&
1445 git checkout -b C A &&
1447 test_ln_s_add irrelevant-file path &&
1450 git checkout -q B^0 &&
1451 git merge -s ours -m D C^0 &&
1454 git checkout -q C^0 &&
1455 git merge -s ours -m E B^0 &&
1460 test_expect_merge_algorithm failure success
'check conflicting entry types (submodule vs symlink)' '
1462 cd submodule-symlink-add-add &&
1466 test_must_fail git merge -s recursive E^0 &&
1468 git ls-files -s >out &&
1469 test_line_count = 3 out &&
1470 git ls-files -u >out &&
1471 test_line_count = 2 out &&
1472 git ls-files -o >out &&
1473 test_line_count = 1 out
1478 # criss-cross with regular files that have conflicting modes:
1488 # Commit A: nothing of note
1489 # Commit B: introduce file source_me.bash, not executable
1490 # Commit C: introduce file source_me.bash, executable
1491 # Commit D: merge B&C, resolving in favor of B
1492 # Commit E: merge B&C, resolving in favor of C
1494 # This is an obvious add/add mode conflict. Can git detect it?
1496 test_expect_success
'setup conflicting modes for regular file' '
1497 git init regular-file-mode-conflict &&
1499 cd regular-file-mode-conflict &&
1501 touch irrelevant-file &&
1502 git add irrelevant-file &&
1506 git checkout -b B A &&
1507 echo "command_to_run" >source_me.bash &&
1508 git add source_me.bash &&
1511 git checkout -b C A &&
1512 echo "command_to_run" >source_me.bash &&
1513 git add source_me.bash &&
1514 test_chmod +x source_me.bash &&
1517 git checkout -q B^0 &&
1518 git merge -s ours -m D C^0 &&
1521 git checkout -q C^0 &&
1522 git merge -s ours -m E B^0 &&
1527 test_expect_failure
'check conflicting modes for regular file' '
1529 cd regular-file-mode-conflict &&
1533 test_must_fail git merge -s recursive E^0 &&
1535 git ls-files -s >out &&
1536 test_line_count = 3 out &&
1537 git ls-files -u >out &&
1538 test_line_count = 2 out &&
1539 git ls-files -o >out &&
1540 test_line_count = 1 out
1552 # main has two files, named 'b' and 'a'
1553 # branches L1 and R1 both modify each of the two files in conflicting ways
1555 # L2 is a merge of R1 into L1; more on it later.
1556 # R2 is a merge of L1 into R1; more on it later.
1558 # X is an auto-generated merge-base used when merging L2 and R2.
1559 # since X is a merge of L1 and R1, it has conflicting versions of each file
1561 # More about L2 and R2:
1562 # - both resolve the conflicts in 'b' and 'a' differently
1563 # - L2 renames 'b' to 'm'
1564 # - R2 renames 'a' to 'm'
1566 # In the end, in file 'm' we have four different conflicting files (from
1567 # two versions of 'b' and two of 'a'). In addition, if
1568 # merge.conflictstyle is diff3, then the base version also has
1569 # conflict markers of its own, leading to a total of three levels of
1570 # conflict markers. This is a pretty weird corner case, but we just want
1571 # to ensure that we handle it as well as practical.
1573 test_expect_success
'setup nested conflicts' '
1574 git init nested_conflicts &&
1576 cd nested_conflicts &&
1578 # Create some related files now
1579 printf "Random base content line %d\n" $(test_seq 1 10) >initial &&
1590 test_write_lines b b_L1 >>b_L1 &&
1591 test_write_lines b b_R1 >>b_R1 &&
1592 test_write_lines b b_L2 >>b_L2 &&
1593 test_write_lines b b_R2 >>b_R2 &&
1594 test_write_lines a a_L1 >>a_L1 &&
1595 test_write_lines a a_R1 >>a_R1 &&
1596 test_write_lines a a_L2 >>a_L2 &&
1597 test_write_lines a a_R2 >>a_R2 &&
1599 # Setup original commit (or merge-base), consisting of
1600 # files named "b" and "a"
1606 test_tick && git commit -m initial &&
1611 # Handle the left side
1616 test_tick && git commit -m "version L1 of files" &&
1619 # Handle the right side
1624 test_tick && git commit -m "version R1 of files" &&
1627 # Create first merge on left side
1629 test_must_fail git merge R1 &&
1634 test_tick && git commit -m "left merge, rename b->m" &&
1637 # Create first merge on right side
1639 test_must_fail git merge L1 &&
1644 test_tick && git commit -m "right merge, rename a->m" &&
1649 test_expect_success
'check nested conflicts' '
1651 cd nested_conflicts &&
1654 MAIN=$(git rev-parse --short main) &&
1655 git checkout L2^0 &&
1657 # Merge must fail; there is a conflict
1658 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R2^0 &&
1660 # Make sure the index has the right number of entries
1661 git ls-files -s >out &&
1662 test_line_count = 2 out &&
1663 git ls-files -u >out &&
1664 test_line_count = 2 out &&
1665 # Ensure we have the correct number of untracked files
1666 git ls-files -o >out &&
1667 test_line_count = 1 out &&
1669 # Create a and b from virtual merge base X
1670 git cat-file -p main:a >base &&
1671 git cat-file -p L1:a >ours &&
1672 git cat-file -p R1:a >theirs &&
1673 test_must_fail git merge-file --diff3 \
1674 -L "Temporary merge branch 1" \
1676 -L "Temporary merge branch 2" \
1680 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_a &&
1682 git cat-file -p main:b >base &&
1683 git cat-file -p L1:b >ours &&
1684 git cat-file -p R1:b >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_b &&
1694 # Compare :2:m to expected values
1695 git cat-file -p L2:m >ours &&
1696 git cat-file -p R2:b >theirs &&
1697 test_must_fail git merge-file --diff3 \
1699 -L "merged common ancestors:b" \
1704 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_2 &&
1705 git cat-file -p :2:m >actual &&
1706 test_cmp m_stage_2 actual &&
1708 # Compare :3:m to expected values
1709 git cat-file -p L2:a >ours &&
1710 git cat-file -p R2:m >theirs &&
1711 test_must_fail git merge-file --diff3 \
1713 -L "merged common ancestors:a" \
1718 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_3 &&
1719 git cat-file -p :3:m >actual &&
1720 test_cmp m_stage_3 actual &&
1722 # Compare m to expected contents
1724 cp m_stage_2 expected_final_m &&
1725 test_must_fail git merge-file --diff3 \
1727 -L "merged common ancestors" \
1732 test_cmp expected_final_m m
1744 # main has one file named 'content'
1745 # branches L1 and R1 both modify each of the two files in conflicting ways
1747 # L<n> (n>1) is a merge of R<n-1> into L<n-1>
1748 # R<n> (n>1) is a merge of L<n-1> into R<n-1>
1749 # L<n> and R<n> resolve the conflicts differently.
1751 # X<n> is an auto-generated merge-base used when merging L<n+1> and R<n+1>.
1752 # By construction, X1 has conflict markers due to conflicting versions.
1753 # X2, due to using merge.conflictstyle=3, has nested conflict markers.
1755 # So, merging R3 into L3 using merge.conflictstyle=3 should show the
1756 # nested conflict markers from X2 in the base version -- that means we
1757 # have three levels of conflict markers. Can we distinguish all three?
1759 test_expect_success
'setup virtual merge base with nested conflicts' '
1760 git init virtual_merge_base_has_nested_conflicts &&
1762 cd virtual_merge_base_has_nested_conflicts &&
1764 # Create some related files now
1765 printf "Random base content line %d\n" $(test_seq 1 10) >content &&
1767 # Setup original commit
1769 test_tick && git commit -m initial &&
1776 echo left >>content &&
1778 test_tick && git commit -m "version L1 of content" &&
1783 echo right >>content &&
1785 test_tick && git commit -m "version R1 of content" &&
1790 test_must_fail git -c merge.conflictstyle=diff3 merge R1 &&
1791 git checkout L1 content &&
1792 test_tick && git commit -m "version L2 of content" &&
1797 test_must_fail git -c merge.conflictstyle=diff3 merge L1 &&
1798 git checkout R1 content &&
1799 test_tick && git commit -m "version R2 of content" &&
1804 test_must_fail git -c merge.conflictstyle=diff3 merge R2 &&
1805 git checkout L1 content &&
1806 test_tick && git commit -m "version L3 of content" &&
1811 test_must_fail git -c merge.conflictstyle=diff3 merge L2 &&
1812 git checkout R1 content &&
1813 test_tick && git commit -m "version R3 of content" &&
1818 test_expect_success
'check virtual merge base with nested conflicts' '
1820 cd virtual_merge_base_has_nested_conflicts &&
1822 MAIN=$(git rev-parse --short main) &&
1823 git checkout L3^0 &&
1825 # Merge must fail; there is a conflict
1826 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R3^0 &&
1828 # Make sure the index has the right number of entries
1829 git ls-files -s >out &&
1830 test_line_count = 3 out &&
1831 git ls-files -u >out &&
1832 test_line_count = 3 out &&
1833 # Ensure we have the correct number of untracked files
1834 git ls-files -o >out &&
1835 test_line_count = 1 out &&
1837 # Compare :[23]:content to expected values
1838 git rev-parse L1:content R1:content >expect &&
1839 git rev-parse :2:content :3:content >actual &&
1840 test_cmp expect actual &&
1842 # Imitate X1 merge base, except without long enough conflict
1843 # markers because a subsequent sed will modify them. Put
1845 git cat-file -p main:content >base &&
1846 git cat-file -p L:content >left &&
1847 git cat-file -p R:content >right &&
1848 cp left merged-once &&
1849 test_must_fail git merge-file --diff3 \
1850 -L "Temporary merge branch 1" \
1852 -L "Temporary merge branch 2" \
1856 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-once >vmb &&
1858 # Imitate X2 merge base, overwriting vmb. Note that we
1859 # extend both sets of conflict markers to make them longer
1860 # with the sed command.
1861 cp left merged-twice &&
1862 test_must_fail git merge-file --diff3 \
1863 -L "Temporary merge branch 1" \
1864 -L "merged common ancestors" \
1865 -L "Temporary merge branch 2" \
1869 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-twice >vmb &&
1871 # Compare :1:content to expected value
1872 git cat-file -p :1:content >actual &&
1873 test_cmp vmb actual &&
1875 # Determine expected content in final outer merge, compare to
1876 # what the merge generated.
1877 cp -f left expect &&
1878 test_must_fail git merge-file --diff3 \
1879 -L "HEAD" -L "merged common ancestors" -L "R3^0" \
1881 test_cmp expect content