3 test_description='recursive merge corner cases involving criss-cross merges'
7 get_clean_checkout () {
23 test_expect_success 'setup basic criss-cross + rename with no modifications' '
24 ten="0 1 2 3 4 5 6 7 8 9" &&
27 echo line $i in a sample file
31 echo line $i in another sample file
34 test_tick && git commit -m initial &&
39 test_tick && git commit -m R1 &&
43 test_tick && git commit -m L1 &&
46 test_tick && git merge -s ours R1 &&
50 test_tick && git merge -s ours L1 &&
54 test_expect_success 'merge simple rename+criss-cross with no modifications' '
58 test_must_fail git merge -s recursive R2^0 &&
60 test 2 = $(git ls-files -s | wc -l) &&
61 test 2 = $(git ls-files -u | wc -l) &&
62 test 2 = $(git ls-files -o | wc -l) &&
64 test $(git rev-parse :2:three) = $(git rev-parse L2:three) &&
65 test $(git rev-parse :3:three) = $(git rev-parse R2:three) &&
67 test $(git rev-parse L2:three) = $(git hash-object three~HEAD) &&
68 test $(git rev-parse R2:three) = $(git hash-object three~R2^0)
72 # Same as before, but modify L1 slightly:
83 test_expect_success 'setup criss-cross + rename merges with basic modification' '
89 ten="0 1 2 3 4 5 6 7 8 9" &&
92 echo line $i in a sample file
96 echo line $i in another sample file
99 test_tick && git commit -m initial &&
102 git checkout -b R1 &&
106 test_tick && git commit -m R1 &&
110 test_tick && git commit -m L1 &&
113 test_tick && git merge -s ours R1 &&
117 test_tick && git merge -s ours L1 &&
121 test_expect_success 'merge criss-cross + rename merges with basic modification' '
125 test_must_fail git merge -s recursive R2^0 &&
127 test 2 = $(git ls-files -s | wc -l) &&
128 test 2 = $(git ls-files -u | wc -l) &&
129 test 2 = $(git ls-files -o | wc -l) &&
131 test $(git rev-parse :2:three) = $(git rev-parse L2:three) &&
132 test $(git rev-parse :3:three) = $(git rev-parse R2:three) &&
134 test $(git rev-parse L2:three) = $(git hash-object three~HEAD) &&
135 test $(git rev-parse R2:three) = $(git hash-object three~R2^0)
139 # For the next test, we start with three commits in two lines of development
140 # which setup a rename/add conflict:
141 # Commit A: File 'a' exists
142 # Commit B: Rename 'a' -> 'new_a'
143 # Commit C: Modify 'a', create different 'new_a'
144 # Later, two different people merge and resolve differently:
145 # Commit D: Merge B & C, ignoring separately created 'new_a'
146 # Commit E: Merge B & C making use of some piece of secondary 'new_a'
147 # Finally, someone goes to merge D & E. Does git detect the conflict?
158 test_expect_success 'setup differently handled merges of rename/add conflict' '
164 printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
166 test_tick && git commit -m A &&
171 echo "other content" >>new_a &&
173 test_tick && git commit -m C &&
177 test_tick && git commit -m B &&
180 test_must_fail git merge C &&
182 test_tick && git commit -m D &&
186 test_must_fail git merge B &&
187 rm new_a~HEAD new_a &&
188 printf "Incorrectly merged content" >>new_a &&
190 test_tick && git commit -m E &&
194 test_expect_success 'git detects differently handled merges conflict' '
198 test_must_fail git merge -s recursive E^0 &&
200 test 3 = $(git ls-files -s | wc -l) &&
201 test 3 = $(git ls-files -u | wc -l) &&
202 test 0 = $(git ls-files -o | wc -l) &&
204 test $(git rev-parse :2:new_a) = $(git rev-parse D:new_a) &&
205 test $(git rev-parse :3:new_a) = $(git rev-parse E:new_a) &&
207 git cat-file -p B:new_a >>merged &&
208 git cat-file -p C:new_a >>merge-me &&
210 test_must_fail git merge-file \
211 -L "Temporary merge branch 2" \
213 -L "Temporary merge branch 1" \
214 merged empty merge-me &&
215 sed -e "s/^\([<=>]\)/\1\1\1/" merged >merged-internal &&
216 test $(git rev-parse :1:new_a) = $(git hash-object merged-internal)
220 # criss-cross + modify/delete:
230 # Commit A: file with contents 'A\n'
231 # Commit B: file with contents 'B\n'
232 # Commit C: file not present
233 # Commit D: file with contents 'B\n'
234 # Commit E: file not present
236 # Merging commits D & E should result in modify/delete conflict.
238 test_expect_success 'setup criss-cross + modify/delete resolved differently' '
262 test_must_fail git merge C &&
270 test_must_fail git merge B &&
277 test_expect_success 'git detects conflict merging criss-cross+modify/delete' '
280 test_must_fail git merge -s recursive E^0 &&
282 test 2 -eq $(git ls-files -s | wc -l) &&
283 test 2 -eq $(git ls-files -u | wc -l) &&
285 test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
286 test $(git rev-parse :2:file) = $(git rev-parse B:file)
289 test_expect_success 'git detects conflict merging criss-cross+modify/delete, reverse direction' '
293 test_must_fail git merge -s recursive D^0 &&
295 test 2 -eq $(git ls-files -s | wc -l) &&
296 test 2 -eq $(git ls-files -u | wc -l) &&
298 test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
299 test $(git rev-parse :3:file) = $(git rev-parse B:file)
303 # criss-cross + d/f conflict via add/add:
304 # Commit A: Neither file 'a' nor directory 'a/' exists.
305 # Commit B: Introduce 'a'
306 # Commit C: Introduce 'a/file'
307 # Commit D: Merge B & C, keeping 'a' and deleting 'a/'
309 # Two different later cases:
310 # Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
311 # Commit E2: Merge B & C, deleting 'a' but keeping a slightly modified 'a/file'
321 # Merging D & E1 requires we first create a virtual merge base X from
322 # merging A & B in memory. Now, if X could keep both 'a' and 'a/file' in
323 # the index, then the merge of D & E1 could be resolved cleanly with both
324 # 'a' and 'a/file' removed. Since git does not currently allow creating
325 # such a tree, the best we can do is have X contain both 'a~<unique>' and
326 # 'a/file' resulting in the merge of D and E1 having a rename/delete
327 # conflict for 'a'. (Although this merge appears to be unsolvable with git
328 # currently, git could do a lot better than it currently does with these
329 # d/f conflicts, which is the purpose of this test.)
331 # Merge of D & E2 has similar issues for path 'a', but should always result
332 # in a modify/delete conflict for path 'a/file'.
334 # We run each merge in both directions, to check for directional issues
335 # with D/F conflict handling.
338 test_expect_success 'setup differently handled merges of directory/file conflict' '
365 test_must_fail git merge C &&
375 test_must_fail git merge B &&
385 test_must_fail git merge B &&
388 printf "10\n11\n" >a/file &&
395 test_expect_success 'merge of D & E1 fails but has appropriate contents' '
396 get_clean_checkout D^0 &&
398 test_must_fail git merge -s recursive E1^0 &&
400 test 2 -eq $(git ls-files -s | wc -l) &&
401 test 1 -eq $(git ls-files -u | wc -l) &&
402 test 0 -eq $(git ls-files -o | wc -l) &&
404 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
405 test $(git rev-parse :2:a) = $(git rev-parse B:a)
408 test_expect_success 'merge of E1 & D fails but has appropriate contents' '
409 get_clean_checkout E1^0 &&
411 test_must_fail git merge -s recursive D^0 &&
413 test 2 -eq $(git ls-files -s | wc -l) &&
414 test 1 -eq $(git ls-files -u | wc -l) &&
415 test 0 -eq $(git ls-files -o | wc -l) &&
417 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
418 test $(git rev-parse :3:a) = $(git rev-parse B:a)
421 test_expect_success 'merge of D & E2 fails but has appropriate contents' '
422 get_clean_checkout D^0 &&
424 test_must_fail git merge -s recursive E2^0 &&
426 test 4 -eq $(git ls-files -s | wc -l) &&
427 test 3 -eq $(git ls-files -u | wc -l) &&
428 test 1 -eq $(git ls-files -o | wc -l) &&
430 test $(git rev-parse :2:a) = $(git rev-parse B:a) &&
431 test $(git rev-parse :3:a/file) = $(git rev-parse E2:a/file) &&
432 test $(git rev-parse :1:a/file) = $(git rev-parse C:a/file) &&
433 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
438 test_expect_success 'merge of E2 & D fails but has appropriate contents' '
439 get_clean_checkout E2^0 &&
441 test_must_fail git merge -s recursive D^0 &&
443 test 4 -eq $(git ls-files -s | wc -l) &&
444 test 3 -eq $(git ls-files -u | wc -l) &&
445 test 1 -eq $(git ls-files -o | wc -l) &&
447 test $(git rev-parse :3:a) = $(git rev-parse B:a) &&
448 test $(git rev-parse :2:a/file) = $(git rev-parse E2:a/file) &&
449 test $(git rev-parse :1:a/file) = $(git rev-parse C:a/file) &&
450 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
456 # criss-cross with rename/rename(1to2)/modify followed by
457 # rename/rename(2to1)/modify:
467 # Commit A: new file: a
468 # Commit B: rename a->b, modifying by adding a line
469 # Commit C: rename a->c
470 # Commit D: merge B&C, resolving conflict by keeping contents in newname
471 # Commit E: merge B&C, resolving conflict similar to D but adding another line
473 # There is a conflict merging B & C, but one of filename not of file
474 # content. Whoever created D and E chose specific resolutions for that
475 # conflict resolution. Now, since: (1) there is no content conflict
476 # merging B & C, (2) D does not modify that merged content further, and (3)
477 # both D & E resolve the name conflict in the same way, the modification to
478 # newname in E should not cause any conflicts when it is merged with D.
479 # (Note that this can be accomplished by having the virtual merge base have
480 # the merged contents of b and c stored in a file named a, which seems like
481 # the most logical choice anyway.)
483 # Comment from Junio: I do not necessarily agree with the choice "a", but
484 # it feels sound to say "B and C do not agree what the final pathname
485 # should be, but we know this content was derived from the common A:a so we
486 # use one path whose name is arbitrary in the virtual merge base X between
487 # D and E" and then further let the rename detection to notice that that
488 # arbitrary path gets renamed between X-D to "newname" and X-E also to
489 # "newname" to resolve it as both sides renaming it to the same new
490 # name. It is akin to what we do at the content level, i.e. "B and C do not
491 # agree what the final contents should be, so we leave the conflict marker
492 # but that may cancel out at the final merge stage".
494 test_expect_success 'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
501 printf "1\n2\n3\n4\n5\n6\n" >a &&
506 git checkout -b B A &&
512 git checkout -b C A &&
516 git checkout -q B^0 &&
517 git merge --no-commit -s ours C^0 &&
519 git commit -m "Merge commit C^0 into HEAD" &&
522 git checkout -q C^0 &&
523 git merge --no-commit -s ours B^0 &&
525 printf "7\n8\n" >>newname &&
527 git commit -m "Merge commit B^0 into HEAD" &&
531 test_expect_success 'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
534 git merge -s recursive E^0 &&
536 test 1 -eq $(git ls-files -s | wc -l) &&
537 test 0 -eq $(git ls-files -u | wc -l) &&
538 test 0 -eq $(git ls-files -o | wc -l) &&
540 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
544 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
554 # Commit A: new file: a
555 # Commit B: rename a->b
556 # Commit C: rename a->c, add different a
557 # Commit D: merge B&C, keeping b&c and (new) a modified at beginning
558 # Commit E: merge B&C, keeping b&c and (new) a modified at end
560 # Merging commits D & E should result in no conflict; doing so correctly
561 # requires getting the virtual merge base (from merging B&C) right, handling
562 # renaming carefully (both in the virtual merge base and later), and getting
563 # content merge handled.
565 test_expect_success 'setup criss-cross + rename/rename/add + modify/modify' '
571 printf "lots\nof\nwords\nand\ncontent\n" >a &&
576 git checkout -b B A &&
580 git checkout -b C A &&
582 printf "2\n3\n4\n5\n6\n7\n" >a &&
587 git merge --no-commit -s ours C^0 &&
588 git checkout C -- a c &&
594 git commit -m "Merge commit C^0 into HEAD" &&
598 git merge --no-commit -s ours B^0 &&
599 git checkout B -- b &&
602 git commit -m "Merge commit B^0 into HEAD" &&
606 test_expect_failure 'detect rename/rename/add-source for virtual merge-base' '
609 git merge -s recursive E^0 &&
611 test 3 -eq $(git ls-files -s | wc -l) &&
612 test 0 -eq $(git ls-files -u | wc -l) &&
613 test 0 -eq $(git ls-files -o | wc -l) &&
615 test $(git rev-parse HEAD:b) = $(git rev-parse A:a) &&
616 test $(git rev-parse HEAD:c) = $(git rev-parse A:a) &&
617 test "$(cat a)" = "$(printf "1\n2\n3\n4\n5\n6\n7\n8\n")"
621 # criss-cross with rename/rename(1to2)/add-dest + simple modify:
631 # Commit A: new file: a
632 # Commit B: rename a->b, add c
633 # Commit C: rename a->c
634 # Commit D: merge B&C, keeping A:a and B:c
635 # Commit E: merge B&C, keeping A:a and slightly modified c from B
637 # Merging commits D & E should result in no conflict. The virtual merge
638 # base of B & C needs to not delete B:c for that to work, though...
640 test_expect_success 'setup criss-cross+rename/rename/add-dest + simple modify' '
651 git checkout -b B A &&
653 printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
657 git checkout -b C A &&
662 git merge --no-commit -s ours C^0 &&
664 git commit -m "D is like B but renames b back to a" &&
668 git merge --no-commit -s ours C^0 &&
672 git commit -m "E like D but has mod in c" &&
676 test_expect_success 'virtual merge base handles rename/rename(1to2)/add-dest' '
679 git merge -s recursive E^0 &&
681 test 2 -eq $(git ls-files -s | wc -l) &&
682 test 0 -eq $(git ls-files -u | wc -l) &&
683 test 0 -eq $(git ls-files -o | wc -l) &&
685 test $(git rev-parse HEAD:a) = $(git rev-parse A:a) &&
686 test $(git rev-parse HEAD:c) = $(git rev-parse E:c)