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
8 TEST_PASSES_SANITIZE_LEAK
=true
10 .
"$TEST_DIRECTORY"/lib-merge.sh
22 test_expect_success
'setup basic criss-cross + rename with no modifications' '
23 git init basic-rename &&
27 ten="0 1 2 3 4 5 6 7 8 9" &&
28 printf "line %d in a sample file\n" $ten >one &&
29 printf "line %d in another sample file\n" $ten >two &&
31 test_tick && git commit -m initial &&
36 test_tick && git commit -m R1 &&
40 test_tick && git commit -m L1 &&
43 test_tick && git merge -s ours R1 &&
47 test_tick && git merge -s ours L1 &&
52 test_expect_success
'merge simple rename+criss-cross with no modifications' '
59 test_must_fail git merge -s recursive R2^0 &&
61 git ls-files -s >out &&
62 test_line_count = 5 out &&
63 git ls-files -u >out &&
64 test_line_count = 3 out &&
65 git ls-files -o >out &&
66 test_line_count = 1 out &&
68 git rev-parse >expect \
70 git rev-parse >actual \
72 test_cmp expect actual
77 # Same as before, but modify L1 slightly:
88 test_expect_success
'setup criss-cross + rename merges with basic modification' '
89 git init rename-modify &&
93 ten="0 1 2 3 4 5 6 7 8 9" &&
94 printf "line %d in a sample file\n" $ten >one &&
95 printf "line %d in another sample file\n" $ten >two &&
97 test_tick && git commit -m initial &&
100 git checkout -b R1 &&
104 test_tick && git commit -m R1 &&
108 test_tick && git commit -m L1 &&
111 test_tick && git merge -s ours R1 &&
115 test_tick && git merge -s ours L1 &&
120 test_expect_success
'merge criss-cross + rename merges with basic modification' '
126 test_must_fail git merge -s recursive R2^0 &&
128 git ls-files -s >out &&
129 test_line_count = 5 out &&
130 git ls-files -u >out &&
131 test_line_count = 3 out &&
132 git ls-files -o >out &&
133 test_line_count = 1 out &&
135 git rev-parse >expect \
137 git rev-parse >actual \
139 test_cmp expect actual
144 # For the next test, we start with three commits in two lines of development
145 # which setup a rename/add conflict:
146 # Commit A: File 'a' exists
147 # Commit B: Rename 'a' -> 'new_a'
148 # Commit C: Modify 'a', create different 'new_a'
149 # Later, two different people merge and resolve differently:
150 # Commit D: Merge B & C, ignoring separately created 'new_a'
151 # Commit E: Merge B & C making use of some piece of secondary 'new_a'
152 # Finally, someone goes to merge D & E. Does git detect the conflict?
163 test_expect_success
'setup differently handled merges of rename/add conflict' '
164 git init rename-add &&
168 printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
170 test_tick && git commit -m A &&
175 test_write_lines 0 1 2 3 4 5 6 7 foobar >new_a &&
177 test_tick && git commit -m C &&
181 test_tick && git commit -m B &&
184 test_must_fail git merge C &&
185 git show :2:new_a >new_a &&
187 test_tick && git commit -m D &&
191 test_must_fail git merge B &&
192 test_write_lines 0 1 2 3 4 5 6 7 bad_merge >new_a &&
194 test_tick && git commit -m E &&
199 test_expect_success
'git detects differently handled merges conflict' '
205 test_must_fail git merge -s recursive E^0 &&
207 git ls-files -s >out &&
208 test_line_count = 3 out &&
209 git ls-files -u >out &&
210 test_line_count = 3 out &&
211 git ls-files -o >out &&
212 test_line_count = 1 out &&
214 git cat-file -p C:new_a >ours &&
215 git cat-file -p C:a >theirs &&
217 test_must_fail git merge-file \
218 -L "Temporary merge branch 1" \
220 -L "Temporary merge branch 2" \
222 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
223 git hash-object ours-tweaked >expect &&
224 git rev-parse >>expect \
226 git rev-parse >actual \
227 :1:new_a :2:new_a :3:new_a &&
228 test_cmp expect actual &&
230 # Test that the two-way merge in new_a is as expected
231 git cat-file -p D:new_a >ours &&
232 git cat-file -p E:new_a >theirs &&
234 test_must_fail git merge-file \
239 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
240 git hash-object new_a >actual &&
241 git hash-object ours >expect &&
242 test_cmp expect actual
246 # Repeat the above testcase with precisely the same setup, other than with
247 # the two merge bases having different orderings of commit timestamps so
248 # that they are reversed in the order they are provided to merge-recursive,
249 # so that we can improve code coverage.
250 test_expect_success
'git detects differently handled merges conflict, swapped' '
254 # Difference #1: Do cleanup from previous testrun
258 # Difference #2: Change commit timestamps
259 btime=$(git log --no-walk --date=raw --format=%cd B | awk "{print \$1}") &&
260 ctime=$(git log --no-walk --date=raw --format=%cd C | awk "{print \$1}") &&
261 newctime=$(($btime+1)) &&
262 git fast-export --no-data --all | sed -e s/$ctime/$newctime/ | git fast-import --force --quiet &&
263 # End of most differences; rest is copy-paste of last test,
264 # other than swapping C:a and C:new_a due to order switch
267 test_must_fail git merge -s recursive E^0 &&
269 git ls-files -s >out &&
270 test_line_count = 3 out &&
271 git ls-files -u >out &&
272 test_line_count = 3 out &&
273 git ls-files -o >out &&
274 test_line_count = 1 out &&
276 git cat-file -p C:a >ours &&
277 git cat-file -p C:new_a >theirs &&
279 test_must_fail git merge-file \
280 -L "Temporary merge branch 1" \
282 -L "Temporary merge branch 2" \
284 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
285 git hash-object ours-tweaked >expect &&
286 git rev-parse >>expect \
288 git rev-parse >actual \
289 :1:new_a :2:new_a :3:new_a &&
290 test_cmp expect actual &&
292 # Test that the two-way merge in new_a is as expected
293 git cat-file -p D:new_a >ours &&
294 git cat-file -p E:new_a >theirs &&
296 test_must_fail git merge-file \
301 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
302 git hash-object new_a >actual &&
303 git hash-object ours >expect &&
304 test_cmp expect actual
309 # criss-cross + modify/delete:
319 # Commit A: file with contents 'A\n'
320 # Commit B: file with contents 'B\n'
321 # Commit C: file not present
322 # Commit D: file with contents 'B\n'
323 # Commit E: file not present
325 # Merging commits D & E should result in modify/delete conflict.
327 test_expect_success
'setup criss-cross + modify/delete resolved differently' '
328 git init modify-delete &&
350 test_must_fail git merge C &&
358 test_must_fail git merge B &&
366 test_expect_success
'git detects conflict merging criss-cross+modify/delete' '
372 test_must_fail git merge -s recursive E^0 &&
374 git ls-files -s >out &&
375 test_line_count = 2 out &&
376 git ls-files -u >out &&
377 test_line_count = 2 out &&
379 git rev-parse >expect \
381 git rev-parse >actual \
383 test_cmp expect actual
387 test_expect_success
'git detects conflict merging criss-cross+modify/delete, reverse direction' '
394 test_must_fail git merge -s recursive D^0 &&
396 git ls-files -s >out &&
397 test_line_count = 2 out &&
398 git ls-files -u >out &&
399 test_line_count = 2 out &&
401 git rev-parse >expect \
403 git rev-parse >actual \
405 test_cmp expect actual
409 # SORRY FOR THE SUPER LONG DESCRIPTION, BUT THIS NEXT ONE IS HAIRY
411 # criss-cross + d/f conflict via add/add:
412 # Commit A: Neither file 'a' nor directory 'a/' exists.
413 # Commit B: Introduce 'a'
414 # Commit C: Introduce 'a/file'
415 # Commit D1: Merge B & C, keeping 'a' and deleting 'a/'
416 # Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
426 # I'll describe D2, E2, & E3 (which are alternatives for D1 & E1) more below...
428 # Merging D1 & E1 requires we first create a virtual merge base X from
429 # merging A & B in memory. There are several possibilities for the merge-base:
430 # 1: Keep both 'a' and 'a/file' (assuming crazy filesystem allowing a tree
431 # with a directory and file at same path): results in merge of D1 & E1
432 # being clean with both files deleted. Bad (no conflict detected).
433 # 2: Keep 'a' but not 'a/file': Merging D1 & E1 is clean and matches E1. Bad.
434 # 3: Keep 'a/file' but not 'a': Merging D1 & E1 is clean and matches D1. Bad.
435 # 4: Keep neither file: Merging D1 & E1 reports the D/F add/add conflict.
437 # So 4 sounds good for this case, but if we were to merge D1 & E3, where E3
439 # Commit E3: Merge B & C, keeping modified a, and deleting a/
440 # then we'd get an add/add conflict for 'a', which seems suboptimal. A little
441 # creativity leads us to an alternate choice:
442 # 5: Keep 'a' as 'a~$UNIQUE' and a/file; results:
443 # Merge D1 & E1: rename/delete conflict for 'a'; a/file silently deleted
444 # Merge D1 & E3 is clean, as expected.
446 # So choice 5 at least provides some kind of conflict for the original case,
447 # and can merge cleanly as expected with D1 and E3. It also made things just
448 # slightly funny for merging D1 and E4, where E4 is defined as:
449 # Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
450 # in this case, we'll get a rename/rename(1to2) conflict because a~$UNIQUE
451 # gets renamed to 'a' in D1 and to 'a2' in E4. But that's better than having
452 # two files (both 'a' and 'a2') sitting around without the user being notified
453 # that we could detect they were related and need to be merged. Also, choice
454 # 5 makes the handling of 'a/file' seem suboptimal. What if we were to merge
455 # D2 and E4, where D2 is:
456 # Commit D2: Merge B & C, renaming 'a'->'a2', keeping 'a/file'
457 # This would result in a clean merge with 'a2' having three-way merged
458 # contents (good), and deleting 'a/' (bad) -- it doesn't detect the
459 # conflict in how the different sides treated a/file differently.
460 # Continuing down the creative route:
461 # 6: Keep 'a' as 'a~$UNIQUE1' and keep 'a/' as 'a~$UNIQUE2/'; results:
462 # Merge D1 & E1: rename/delete conflict for 'a' and each path under 'a/'.
463 # Merge D1 & E3: clean, as expected.
464 # Merge D1 & E4: rename/rename(1to2) conflict on 'a' vs 'a2'.
465 # Merge D2 & E4: clean for 'a2', rename/delete for a/file
467 # Choice 6 could cause rename detection to take longer (providing more targets
468 # that need to be searched). Also, the conflict message for each path under
469 # 'a/' might be annoying unless we can detect it at the directory level, print
470 # it once, and then suppress it for individual filepaths underneath.
473 # As of time of writing, git uses choice 5. Directory rename detection and
474 # rename detection performance improvements might make choice 6 a desirable
475 # improvement. But we can at least document where we fall short for now...
478 # Historically, this testcase also used:
479 # Commit E2: Merge B & C, deleting 'a' but keeping slightly modified 'a/file'
480 # The merge of D1 & E2 is very similar to D1 & E1 -- it has similar issues for
481 # path 'a', but should always result in a modify/delete conflict for path
482 # 'a/file'. These tests ran the two merges
485 # in both directions, to check for directional issues with D/F conflict
486 # handling. Later we added
490 # for good measure, though we only ran those one way because we had pretty
491 # good confidence in merge-recursive's directional handling of D/F issues.
493 # Just to summarize all the intermediate merge commits:
494 # Commit D1: Merge B & C, keeping a and deleting a/
495 # Commit D2: Merge B & C, renaming a->a2, keeping a/file
496 # Commit E1: Merge B & C, deleting a but keeping a/file
497 # Commit E2: Merge B & C, deleting a but keeping slightly modified a/file
498 # Commit E3: Merge B & C, keeping modified a, and deleting a/
499 # Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
502 test_expect_success
'setup differently handled merges of directory/file conflict' '
503 git init directory-file &&
516 test_write_lines a b c d e f g >a/file &&
522 test_write_lines 1 2 3 4 5 6 7 >a &&
528 git merge -s ours -m D1 C^0 &&
532 test_must_fail git merge C^0 &&
533 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
542 git cat-file -p B:a >a2 &&
548 git merge -s ours -m E1 B^0 &&
552 git merge -s ours -m E2 B^0 &&
553 test_write_lines a b c d e f g h >a/file &&
555 git commit --amend -C HEAD &&
559 test_must_fail git merge B^0 &&
560 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
567 test_write_lines 1 2 3 4 5 6 7 8 >a &&
573 test_must_fail git merge B^0 &&
574 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
583 test_write_lines 1 2 3 4 5 6 7 8 >a2 &&
590 test_expect_success
'merge of D1 & E1 fails but has appropriate contents' '
591 test_when_finished "git -C directory-file reset --hard" &&
592 test_when_finished "git -C directory-file clean -fdqx" &&
598 test_must_fail git merge -s recursive E1^0 &&
600 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
602 git ls-files -s >out &&
603 test_line_count = 3 out &&
604 git ls-files -u >out &&
605 test_line_count = 2 out &&
606 git ls-files -o >out &&
607 test_line_count = 1 out &&
609 git rev-parse >expect \
610 A:ignore-me B:a D1:a &&
611 git rev-parse >actual \
612 :0:ignore-me :1:a :2:a &&
613 test_cmp expect actual
615 git ls-files -s >out &&
616 test_line_count = 2 out &&
617 git ls-files -u >out &&
618 test_line_count = 1 out &&
619 git ls-files -o >out &&
620 test_line_count = 1 out &&
622 git rev-parse >expect \
624 git rev-parse >actual \
626 test_cmp expect actual
631 test_expect_success
'merge of E1 & D1 fails but has appropriate contents' '
632 test_when_finished "git -C directory-file reset --hard" &&
633 test_when_finished "git -C directory-file clean -fdqx" &&
639 test_must_fail git merge -s recursive D1^0 &&
641 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
643 git ls-files -s >out &&
644 test_line_count = 3 out &&
645 git ls-files -u >out &&
646 test_line_count = 2 out &&
647 git ls-files -o >out &&
648 test_line_count = 1 out &&
650 git rev-parse >expect \
651 A:ignore-me B:a D1:a &&
652 git rev-parse >actual \
653 :0:ignore-me :1:a :3:a &&
654 test_cmp expect actual
656 git ls-files -s >out &&
657 test_line_count = 2 out &&
658 git ls-files -u >out &&
659 test_line_count = 1 out &&
660 git ls-files -o >out &&
661 test_line_count = 1 out &&
663 git rev-parse >expect \
665 git rev-parse >actual \
667 test_cmp expect actual
672 test_expect_success
'merge of D1 & E2 fails but has appropriate contents' '
673 test_when_finished "git -C directory-file reset --hard" &&
674 test_when_finished "git -C directory-file clean -fdqx" &&
680 test_must_fail git merge -s recursive E2^0 &&
682 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
684 git ls-files -s >out &&
685 test_line_count = 5 out &&
686 git ls-files -u >out &&
687 test_line_count = 4 out &&
688 git ls-files -o >out &&
689 test_line_count = 1 out &&
691 git rev-parse >expect \
692 B:a D1:a E2:a/file C:a/file A:ignore-me &&
693 git rev-parse >actual \
694 :1:a~HEAD :2:a~HEAD :3:a/file :1:a/file :0:ignore-me
696 git ls-files -s >out &&
697 test_line_count = 4 out &&
698 git ls-files -u >out &&
699 test_line_count = 3 out &&
700 git ls-files -o >out &&
701 test_line_count = 2 out &&
703 git rev-parse >expect \
704 B:a E2:a/file C:a/file A:ignore-me &&
705 git rev-parse >actual \
706 :2:a :3:a/file :1:a/file :0:ignore-me
708 test_cmp expect actual &&
710 test_path_is_file a~HEAD
714 test_expect_success
'merge of E2 & D1 fails but has appropriate contents' '
715 test_when_finished "git -C directory-file reset --hard" &&
716 test_when_finished "git -C directory-file clean -fdqx" &&
722 test_must_fail git merge -s recursive D1^0 &&
724 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
726 git ls-files -s >out &&
727 test_line_count = 5 out &&
728 git ls-files -u >out &&
729 test_line_count = 4 out &&
730 git ls-files -o >out &&
731 test_line_count = 1 out &&
733 git rev-parse >expect \
734 B:a D1:a E2:a/file C:a/file A:ignore-me &&
735 git rev-parse >actual \
736 :1:a~D1^0 :3:a~D1^0 :2:a/file :1:a/file :0:ignore-me
738 git ls-files -s >out &&
739 test_line_count = 4 out &&
740 git ls-files -u >out &&
741 test_line_count = 3 out &&
742 git ls-files -o >out &&
743 test_line_count = 2 out &&
745 git rev-parse >expect \
746 B:a E2:a/file C:a/file A:ignore-me &&
747 git rev-parse >actual \
748 :3:a :2:a/file :1:a/file :0:ignore-me
750 test_cmp expect actual &&
752 test_path_is_file a~D1^0
756 test_expect_success
'merge of D1 & E3 succeeds' '
757 test_when_finished "git -C directory-file reset --hard" &&
758 test_when_finished "git -C directory-file clean -fdqx" &&
764 git merge -s recursive E3^0 &&
766 git ls-files -s >out &&
767 test_line_count = 2 out &&
768 git ls-files -u >out &&
769 test_line_count = 0 out &&
770 git ls-files -o >out &&
771 test_line_count = 1 out &&
773 git rev-parse >expect \
775 git rev-parse >actual \
777 test_cmp expect actual
781 test_expect_merge_algorithm failure success
'merge of D1 & E4 puts merge of a and a2 in both a and a2' '
782 test_when_finished "git -C directory-file reset --hard" &&
783 test_when_finished "git -C directory-file clean -fdqx" &&
789 test_must_fail git merge -s recursive E4^0 &&
791 git ls-files -s >out &&
792 test_line_count = 4 out &&
793 git ls-files -u >out &&
794 test_line_count = 3 out &&
795 git ls-files -o >out &&
796 test_line_count = 1 out &&
798 git rev-parse >expect \
799 A:ignore-me B:a E4:a2 E4:a2 &&
800 git rev-parse >actual \
801 :0:ignore-me :1:a~Temporary\ merge\ branch\ 2 :2:a :3:a2 &&
802 test_cmp expect actual
806 test_expect_failure
'merge of D2 & E4 merges a2s & reports conflict for a/file' '
807 test_when_finished "git -C directory-file reset --hard" &&
808 test_when_finished "git -C directory-file clean -fdqx" &&
814 test_must_fail git merge -s recursive E4^0 &&
816 git ls-files -s >out &&
817 test_line_count = 3 out &&
818 git ls-files -u >out &&
819 test_line_count = 1 out &&
820 git ls-files -o >out &&
821 test_line_count = 1 out &&
823 git rev-parse >expect \
824 A:ignore-me E4:a2 D2:a/file &&
825 git rev-parse >actual \
826 :0:ignore-me :0:a2 :2:a/file &&
827 test_cmp expect actual
832 # criss-cross with rename/rename(1to2)/modify followed by
833 # rename/rename(2to1)/modify:
843 # Commit A: new file: a
844 # Commit B: rename a->b, modifying by adding a line
845 # Commit C: rename a->c
846 # Commit D: merge B&C, resolving conflict by keeping contents in newname
847 # Commit E: merge B&C, resolving conflict similar to D but adding another line
849 # There is a conflict merging B & C, but one of filename not of file
850 # content. Whoever created D and E chose specific resolutions for that
851 # conflict resolution. Now, since: (1) there is no content conflict
852 # merging B & C, (2) D does not modify that merged content further, and (3)
853 # both D & E resolve the name conflict in the same way, the modification to
854 # newname in E should not cause any conflicts when it is merged with D.
855 # (Note that this can be accomplished by having the virtual merge base have
856 # the merged contents of b and c stored in a file named a, which seems like
857 # the most logical choice anyway.)
859 # Comment from Junio: I do not necessarily agree with the choice "a", but
860 # it feels sound to say "B and C do not agree what the final pathname
861 # should be, but we know this content was derived from the common A:a so we
862 # use one path whose name is arbitrary in the virtual merge base X between
863 # D and E" and then further let the rename detection to notice that that
864 # arbitrary path gets renamed between X-D to "newname" and X-E also to
865 # "newname" to resolve it as both sides renaming it to the same new
866 # name. It is akin to what we do at the content level, i.e. "B and C do not
867 # agree what the final contents should be, so we leave the conflict marker
868 # but that may cancel out at the final merge stage".
870 test_expect_success
'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
871 git init rename-squared-squared &&
873 cd rename-squared-squared &&
875 printf "1\n2\n3\n4\n5\n6\n" >a &&
880 git checkout -b B A &&
886 git checkout -b C A &&
890 git checkout -q B^0 &&
891 git merge --no-commit -s ours C^0 &&
893 git commit -m "Merge commit C^0 into HEAD" &&
896 git checkout -q C^0 &&
897 git merge --no-commit -s ours B^0 &&
899 printf "7\n8\n" >>newname &&
901 git commit -m "Merge commit B^0 into HEAD" &&
906 test_expect_success
'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
908 cd rename-squared-squared &&
912 git merge -s recursive E^0 &&
914 git ls-files -s >out &&
915 test_line_count = 1 out &&
916 git ls-files -u >out &&
917 test_line_count = 0 out &&
918 git ls-files -o >out &&
919 test_line_count = 1 out &&
921 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
926 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
936 # Commit A: new file: a
937 # Commit B: rename a->b
938 # Commit C: rename a->c, add different a
939 # Commit D: merge B&C, keeping b&c and (new) a modified at beginning
940 # Commit E: merge B&C, keeping b&c and (new) a modified at end
942 # Merging commits D & E should result in no conflict; doing so correctly
943 # requires getting the virtual merge base (from merging B&C) right, handling
944 # renaming carefully (both in the virtual merge base and later), and getting
945 # content merge handled.
947 test_expect_success
'setup criss-cross + rename/rename/add-source + modify/modify' '
948 git init rename-rename-add-source &&
950 cd rename-rename-add-source &&
952 printf "lots\nof\nwords\nand\ncontent\n" >a &&
957 git checkout -b B A &&
961 git checkout -b C A &&
963 printf "2\n3\n4\n5\n6\n7\n" >a &&
968 git merge --no-commit -s ours C^0 &&
969 git checkout C -- a c &&
975 git commit -m "Merge commit C^0 into HEAD" &&
979 git merge --no-commit -s ours B^0 &&
980 git checkout B -- b &&
983 git commit -m "Merge commit B^0 into HEAD" &&
988 test_expect_failure
'detect rename/rename/add-source for virtual merge-base' '
990 cd rename-rename-add-source &&
994 git merge -s recursive E^0 &&
996 git ls-files -s >out &&
997 test_line_count = 3 out &&
998 git ls-files -u >out &&
999 test_line_count = 0 out &&
1000 git ls-files -o >out &&
1001 test_line_count = 1 out &&
1003 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >correct &&
1004 git rev-parse >expect \
1007 git rev-parse >actual \
1009 git hash-object >>actual \
1011 test_cmp expect actual
1016 # criss-cross with rename/rename(1to2)/add-dest + simple modify:
1026 # Commit A: new file: a
1027 # Commit B: rename a->b, add c
1028 # Commit C: rename a->c
1029 # Commit D: merge B&C, keeping A:a and B:c
1030 # Commit E: merge B&C, keeping A:a and slightly modified c from B
1032 # Merging commits D & E should result in no conflict. The virtual merge
1033 # base of B & C needs to not delete B:c for that to work, though...
1035 test_expect_success
'setup criss-cross+rename/rename/add-dest + simple modify' '
1036 git init rename-rename-add-dest &&
1038 cd rename-rename-add-dest &&
1045 git checkout -b B A &&
1047 printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
1051 git checkout -b C A &&
1056 git merge --no-commit -s ours C^0 &&
1058 git commit -m "D is like B but renames b back to a" &&
1062 git merge --no-commit -s ours C^0 &&
1066 git commit -m "E like D but has mod in c" &&
1071 test_expect_success
'virtual merge base handles rename/rename(1to2)/add-dest' '
1073 cd rename-rename-add-dest &&
1077 git merge -s recursive E^0 &&
1079 git ls-files -s >out &&
1080 test_line_count = 2 out &&
1081 git ls-files -u >out &&
1082 test_line_count = 0 out &&
1083 git ls-files -o >out &&
1084 test_line_count = 1 out &&
1086 git rev-parse >expect \
1088 git rev-parse >actual \
1090 test_cmp expect actual
1095 # criss-cross with modify/modify on a symlink:
1105 # Commit A: simple simlink fickle->lagoon
1106 # Commit B: redirect fickle->disneyland
1107 # Commit C: redirect fickle->home
1108 # Commit D: merge B&C, resolving in favor of B
1109 # Commit E: merge B&C, resolving in favor of C
1111 # This is an obvious modify/modify conflict for the symlink 'fickle'. Can
1114 test_expect_success
'setup symlink modify/modify' '
1115 git init symlink-modify-modify &&
1117 cd symlink-modify-modify &&
1119 test_ln_s_add lagoon fickle &&
1123 git checkout -b B A &&
1125 test_ln_s_add disneyland fickle &&
1128 git checkout -b C A &&
1130 test_ln_s_add home fickle &&
1134 git checkout -q B^0 &&
1135 git merge -s ours -m D C^0 &&
1138 git checkout -q C^0 &&
1139 git merge -s ours -m E B^0 &&
1144 test_expect_merge_algorithm failure success
'check symlink modify/modify' '
1146 cd symlink-modify-modify &&
1150 test_must_fail git merge -s recursive E^0 &&
1152 git ls-files -s >out &&
1153 test_line_count = 3 out &&
1154 git ls-files -u >out &&
1155 test_line_count = 3 out &&
1156 git ls-files -o >out &&
1157 test_line_count = 1 out
1162 # criss-cross with add/add of a symlink:
1172 # Commit A: No symlink or path exists yet
1173 # Commit B: set up symlink: fickle->disneyland
1174 # Commit C: set up symlink: fickle->home
1175 # Commit D: merge B&C, resolving in favor of B
1176 # Commit E: merge B&C, resolving in favor of C
1178 # This is an obvious add/add conflict for the symlink 'fickle'. Can
1181 test_expect_success
'setup symlink add/add' '
1182 git init symlink-add-add &&
1184 cd symlink-add-add &&
1191 git checkout -b B A &&
1192 test_ln_s_add disneyland fickle &&
1195 git checkout -b C A &&
1196 test_ln_s_add home fickle &&
1200 git checkout -q B^0 &&
1201 git merge -s ours -m D C^0 &&
1204 git checkout -q C^0 &&
1205 git merge -s ours -m E B^0 &&
1210 test_expect_merge_algorithm failure success
'check symlink add/add' '
1212 cd symlink-add-add &&
1216 test_must_fail git merge -s recursive E^0 &&
1218 git ls-files -s >out &&
1219 test_line_count = 3 out &&
1220 git ls-files -u >out &&
1221 test_line_count = 2 out &&
1222 git ls-files -o >out &&
1223 test_line_count = 1 out
1228 # criss-cross with modify/modify on a submodule:
1238 # Commit A: simple submodule repo
1239 # Commit B: update repo
1240 # Commit C: update repo differently
1241 # Commit D: merge B&C, resolving in favor of B
1242 # Commit E: merge B&C, resolving in favor of C
1244 # This is an obvious modify/modify conflict for the submodule 'repo'. Can
1247 test_expect_success
'setup submodule modify/modify' '
1248 git init submodule-modify-modify &&
1250 cd submodule-modify-modify &&
1260 git checkout -b B A &&
1266 git checkout -b C A &&
1273 git -C submod reset --hard A &&
1278 git checkout -b B A &&
1279 git -C submod reset --hard B &&
1283 git checkout -b C A &&
1284 git -C submod reset --hard C &&
1288 git checkout -q B^0 &&
1289 git merge -s ours -m D C^0 &&
1292 git checkout -q C^0 &&
1293 git merge -s ours -m E B^0 &&
1298 test_expect_merge_algorithm failure success
'check submodule modify/modify' '
1300 cd submodule-modify-modify &&
1304 test_must_fail git merge -s recursive E^0 &&
1306 git ls-files -s >out &&
1307 test_line_count = 3 out &&
1308 git ls-files -u >out &&
1309 test_line_count = 3 out &&
1310 git ls-files -o >out &&
1311 test_line_count = 1 out
1316 # criss-cross with add/add on a submodule:
1326 # Commit A: nothing of note
1327 # Commit B: introduce submodule repo
1328 # Commit C: introduce submodule repo at different commit
1329 # Commit D: merge B&C, resolving in favor of B
1330 # Commit E: merge B&C, resolving in favor of C
1332 # This is an obvious add/add conflict for the submodule 'repo'. Can
1335 test_expect_success
'setup submodule add/add' '
1336 git init submodule-add-add &&
1338 cd submodule-add-add &&
1348 git checkout -b B A &&
1354 git checkout -b C A &&
1361 touch irrelevant-file &&
1362 git add irrelevant-file &&
1366 git checkout -b B A &&
1367 git -C submod reset --hard B &&
1371 git checkout -b C A &&
1372 git -C submod reset --hard C &&
1376 git checkout -q B^0 &&
1377 git merge -s ours -m D C^0 &&
1380 git checkout -q C^0 &&
1381 git merge -s ours -m E B^0 &&
1386 test_expect_merge_algorithm failure success
'check submodule add/add' '
1388 cd submodule-add-add &&
1392 test_must_fail git merge -s recursive E^0 &&
1394 git ls-files -s >out &&
1395 test_line_count = 3 out &&
1396 git ls-files -u >out &&
1397 test_line_count = 2 out &&
1398 git ls-files -o >out &&
1399 test_line_count = 1 out
1404 # criss-cross with conflicting entry types:
1414 # Commit A: nothing of note
1415 # Commit B: introduce submodule 'path'
1416 # Commit C: introduce symlink 'path'
1417 # Commit D: merge B&C, resolving in favor of B
1418 # Commit E: merge B&C, resolving in favor of C
1420 # This is an obvious add/add conflict for 'path'. Can git detect it?
1422 test_expect_success
'setup conflicting entry types (submodule vs symlink)' '
1423 git init submodule-symlink-add-add &&
1425 cd submodule-symlink-add-add &&
1436 touch irrelevant-file &&
1437 git add irrelevant-file &&
1441 git checkout -b B A &&
1442 git -C path reset --hard B &&
1446 git checkout -b C A &&
1448 test_ln_s_add irrelevant-file path &&
1451 git checkout -q B^0 &&
1452 git merge -s ours -m D C^0 &&
1455 git checkout -q C^0 &&
1456 git merge -s ours -m E B^0 &&
1461 test_expect_merge_algorithm failure success
'check conflicting entry types (submodule vs symlink)' '
1463 cd submodule-symlink-add-add &&
1467 test_must_fail git merge -s recursive E^0 &&
1469 git ls-files -s >out &&
1470 test_line_count = 3 out &&
1471 git ls-files -u >out &&
1472 test_line_count = 2 out &&
1473 git ls-files -o >out &&
1474 test_line_count = 1 out
1479 # criss-cross with regular files that have conflicting modes:
1489 # Commit A: nothing of note
1490 # Commit B: introduce file source_me.bash, not executable
1491 # Commit C: introduce file source_me.bash, executable
1492 # Commit D: merge B&C, resolving in favor of B
1493 # Commit E: merge B&C, resolving in favor of C
1495 # This is an obvious add/add mode conflict. Can git detect it?
1497 test_expect_success
'setup conflicting modes for regular file' '
1498 git init regular-file-mode-conflict &&
1500 cd regular-file-mode-conflict &&
1502 touch irrelevant-file &&
1503 git add irrelevant-file &&
1507 git checkout -b B A &&
1508 echo "command_to_run" >source_me.bash &&
1509 git add source_me.bash &&
1512 git checkout -b C A &&
1513 echo "command_to_run" >source_me.bash &&
1514 git add source_me.bash &&
1515 test_chmod +x source_me.bash &&
1518 git checkout -q B^0 &&
1519 git merge -s ours -m D C^0 &&
1522 git checkout -q C^0 &&
1523 git merge -s ours -m E B^0 &&
1528 test_expect_failure
'check conflicting modes for regular file' '
1530 cd regular-file-mode-conflict &&
1534 test_must_fail git merge -s recursive E^0 &&
1536 git ls-files -s >out &&
1537 test_line_count = 3 out &&
1538 git ls-files -u >out &&
1539 test_line_count = 2 out &&
1540 git ls-files -o >out &&
1541 test_line_count = 1 out
1553 # main has two files, named 'b' and 'a'
1554 # branches L1 and R1 both modify each of the two files in conflicting ways
1556 # L2 is a merge of R1 into L1; more on it later.
1557 # R2 is a merge of L1 into R1; more on it later.
1559 # X is an auto-generated merge-base used when merging L2 and R2.
1560 # since X is a merge of L1 and R1, it has conflicting versions of each file
1562 # More about L2 and R2:
1563 # - both resolve the conflicts in 'b' and 'a' differently
1564 # - L2 renames 'b' to 'm'
1565 # - R2 renames 'a' to 'm'
1567 # In the end, in file 'm' we have four different conflicting files (from
1568 # two versions of 'b' and two of 'a'). In addition, if
1569 # merge.conflictstyle is diff3, then the base version also has
1570 # conflict markers of its own, leading to a total of three levels of
1571 # conflict markers. This is a pretty weird corner case, but we just want
1572 # to ensure that we handle it as well as practical.
1574 test_expect_success
'setup nested conflicts' '
1575 git init nested_conflicts &&
1577 cd nested_conflicts &&
1579 # Create some related files now
1580 printf "Random base content line %d\n" $(test_seq 1 10) >initial &&
1591 test_write_lines b b_L1 >>b_L1 &&
1592 test_write_lines b b_R1 >>b_R1 &&
1593 test_write_lines b b_L2 >>b_L2 &&
1594 test_write_lines b b_R2 >>b_R2 &&
1595 test_write_lines a a_L1 >>a_L1 &&
1596 test_write_lines a a_R1 >>a_R1 &&
1597 test_write_lines a a_L2 >>a_L2 &&
1598 test_write_lines a a_R2 >>a_R2 &&
1600 # Setup original commit (or merge-base), consisting of
1601 # files named "b" and "a"
1607 test_tick && git commit -m initial &&
1612 # Handle the left side
1617 test_tick && git commit -m "version L1 of files" &&
1620 # Handle the right side
1625 test_tick && git commit -m "version R1 of files" &&
1628 # Create first merge on left side
1630 test_must_fail git merge R1 &&
1635 test_tick && git commit -m "left merge, rename b->m" &&
1638 # Create first merge on right side
1640 test_must_fail git merge L1 &&
1645 test_tick && git commit -m "right merge, rename a->m" &&
1650 test_expect_success
'check nested conflicts' '
1652 cd nested_conflicts &&
1655 MAIN=$(git rev-parse --short main) &&
1656 git checkout L2^0 &&
1658 # Merge must fail; there is a conflict
1659 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R2^0 &&
1661 # Make sure the index has the right number of entries
1662 git ls-files -s >out &&
1663 test_line_count = 2 out &&
1664 git ls-files -u >out &&
1665 test_line_count = 2 out &&
1666 # Ensure we have the correct number of untracked files
1667 git ls-files -o >out &&
1668 test_line_count = 1 out &&
1670 # Create a and b from virtual merge base X
1671 git cat-file -p main:a >base &&
1672 git cat-file -p L1:a >ours &&
1673 git cat-file -p R1:a >theirs &&
1674 test_must_fail git merge-file --diff3 \
1675 -L "Temporary merge branch 1" \
1677 -L "Temporary merge branch 2" \
1681 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_a &&
1683 git cat-file -p main:b >base &&
1684 git cat-file -p L1:b >ours &&
1685 git cat-file -p R1:b >theirs &&
1686 test_must_fail git merge-file --diff3 \
1687 -L "Temporary merge branch 1" \
1689 -L "Temporary merge branch 2" \
1693 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_b &&
1695 # Compare :2:m to expected values
1696 git cat-file -p L2:m >ours &&
1697 git cat-file -p R2:b >theirs &&
1698 test_must_fail git merge-file --diff3 \
1700 -L "merged common ancestors:b" \
1705 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_2 &&
1706 git cat-file -p :2:m >actual &&
1707 test_cmp m_stage_2 actual &&
1709 # Compare :3:m to expected values
1710 git cat-file -p L2:a >ours &&
1711 git cat-file -p R2:m >theirs &&
1712 test_must_fail git merge-file --diff3 \
1714 -L "merged common ancestors:a" \
1719 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_3 &&
1720 git cat-file -p :3:m >actual &&
1721 test_cmp m_stage_3 actual &&
1723 # Compare m to expected contents
1725 cp m_stage_2 expected_final_m &&
1726 test_must_fail git merge-file --diff3 \
1728 -L "merged common ancestors" \
1733 test_cmp expected_final_m m
1745 # main has one file named 'content'
1746 # branches L1 and R1 both modify each of the two files in conflicting ways
1748 # L<n> (n>1) is a merge of R<n-1> into L<n-1>
1749 # R<n> (n>1) is a merge of L<n-1> into R<n-1>
1750 # L<n> and R<n> resolve the conflicts differently.
1752 # X<n> is an auto-generated merge-base used when merging L<n+1> and R<n+1>.
1753 # By construction, X1 has conflict markers due to conflicting versions.
1754 # X2, due to using merge.conflictstyle=3, has nested conflict markers.
1756 # So, merging R3 into L3 using merge.conflictstyle=3 should show the
1757 # nested conflict markers from X2 in the base version -- that means we
1758 # have three levels of conflict markers. Can we distinguish all three?
1760 test_expect_success
'setup virtual merge base with nested conflicts' '
1761 git init virtual_merge_base_has_nested_conflicts &&
1763 cd virtual_merge_base_has_nested_conflicts &&
1765 # Create some related files now
1766 printf "Random base content line %d\n" $(test_seq 1 10) >content &&
1768 # Setup original commit
1770 test_tick && git commit -m initial &&
1777 echo left >>content &&
1779 test_tick && git commit -m "version L1 of content" &&
1784 echo right >>content &&
1786 test_tick && git commit -m "version R1 of content" &&
1791 test_must_fail git -c merge.conflictstyle=diff3 merge R1 &&
1792 git checkout L1 content &&
1793 test_tick && git commit -m "version L2 of content" &&
1798 test_must_fail git -c merge.conflictstyle=diff3 merge L1 &&
1799 git checkout R1 content &&
1800 test_tick && git commit -m "version R2 of content" &&
1805 test_must_fail git -c merge.conflictstyle=diff3 merge R2 &&
1806 git checkout L1 content &&
1807 test_tick && git commit -m "version L3 of content" &&
1812 test_must_fail git -c merge.conflictstyle=diff3 merge L2 &&
1813 git checkout R1 content &&
1814 test_tick && git commit -m "version R3 of content" &&
1819 test_expect_success
'check virtual merge base with nested conflicts' '
1821 cd virtual_merge_base_has_nested_conflicts &&
1823 MAIN=$(git rev-parse --short main) &&
1824 git checkout L3^0 &&
1826 # Merge must fail; there is a conflict
1827 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R3^0 &&
1829 # Make sure the index has the right number of entries
1830 git ls-files -s >out &&
1831 test_line_count = 3 out &&
1832 git ls-files -u >out &&
1833 test_line_count = 3 out &&
1834 # Ensure we have the correct number of untracked files
1835 git ls-files -o >out &&
1836 test_line_count = 1 out &&
1838 # Compare :[23]:content to expected values
1839 git rev-parse L1:content R1:content >expect &&
1840 git rev-parse :2:content :3:content >actual &&
1841 test_cmp expect actual &&
1843 # Imitate X1 merge base, except without long enough conflict
1844 # markers because a subsequent sed will modify them. Put
1846 git cat-file -p main:content >base &&
1847 git cat-file -p L:content >left &&
1848 git cat-file -p R:content >right &&
1849 cp left merged-once &&
1850 test_must_fail git merge-file --diff3 \
1851 -L "Temporary merge branch 1" \
1853 -L "Temporary merge branch 2" \
1857 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-once >vmb &&
1859 # Imitate X2 merge base, overwriting vmb. Note that we
1860 # extend both sets of conflict markers to make them longer
1861 # with the sed command.
1862 cp left merged-twice &&
1863 test_must_fail git merge-file --diff3 \
1864 -L "Temporary merge branch 1" \
1865 -L "merged common ancestors" \
1866 -L "Temporary merge branch 2" \
1870 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-twice >vmb &&
1872 # Compare :1:content to expected value
1873 git cat-file -p :1:content >actual &&
1874 test_cmp vmb actual &&
1876 # Determine expected content in final outer merge, compare to
1877 # what the merge generated.
1878 cp -f left expect &&
1879 test_must_fail git merge-file --diff3 \
1880 -L "HEAD" -L "merged common ancestors" -L "R3^0" \
1882 test_cmp expect content