ll-merge: use a longer conflict marker for internal merge
[git.git] / t / t6036-recursive-corner-cases.sh
blobcc1ee6aa7ff2fb4b87043c84368dca8a4f7a34f1
1 #!/bin/sh
3 test_description='recursive merge corner cases involving criss-cross merges'
5 . ./test-lib.sh
7 get_clean_checkout () {
8 git reset --hard &&
9 git clean -fdqx &&
10 git checkout "$1"
14 # L1 L2
15 # o---o
16 # / \ / \
17 # o X ?
18 # \ / \ /
19 # o---o
20 # R1 R2
23 test_expect_success 'setup basic criss-cross + rename with no modifications' '
24 ten="0 1 2 3 4 5 6 7 8 9" &&
25 for i in $ten
27 echo line $i in a sample file
28 done >one &&
29 for i in $ten
31 echo line $i in another sample file
32 done >two &&
33 git add one two &&
34 test_tick && git commit -m initial &&
36 git branch L1 &&
37 git checkout -b R1 &&
38 git mv one three &&
39 test_tick && git commit -m R1 &&
41 git checkout L1 &&
42 git mv two three &&
43 test_tick && git commit -m L1 &&
45 git checkout L1^0 &&
46 test_tick && git merge -s ours R1 &&
47 git tag L2 &&
49 git checkout R1^0 &&
50 test_tick && git merge -s ours L1 &&
51 git tag R2
54 test_expect_success 'merge simple rename+criss-cross with no modifications' '
55 git reset --hard &&
56 git checkout L2^0 &&
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:
74 # L1m L2
75 # o---o
76 # / \ / \
77 # o X ?
78 # \ / \ /
79 # o---o
80 # R1 R2
83 test_expect_success 'setup criss-cross + rename merges with basic modification' '
84 git rm -rf . &&
85 git clean -fdqx &&
86 rm -rf .git &&
87 git init &&
89 ten="0 1 2 3 4 5 6 7 8 9"
90 for i in $ten
92 echo line $i in a sample file
93 done >one &&
94 for i in $ten
96 echo line $i in another sample file
97 done >two &&
98 git add one two &&
99 test_tick && git commit -m initial &&
101 git branch L1 &&
102 git checkout -b R1 &&
103 git mv one three &&
104 echo more >>two &&
105 git add two &&
106 test_tick && git commit -m R1 &&
108 git checkout L1 &&
109 git mv two three &&
110 test_tick && git commit -m L1 &&
112 git checkout L1^0 &&
113 test_tick && git merge -s ours R1 &&
114 git tag L2 &&
116 git checkout R1^0 &&
117 test_tick && git merge -s ours L1 &&
118 git tag R2
121 test_expect_success 'merge criss-cross + rename merges with basic modification' '
122 git reset --hard &&
123 git checkout L2^0 &&
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?
149 # B D
150 # o---o
151 # / \ / \
152 # A o X ? F
153 # \ / \ /
154 # o---o
155 # C E
158 test_expect_success 'setup differently handled merges of rename/add conflict' '
159 git rm -rf . &&
160 git clean -fdqx &&
161 rm -rf .git &&
162 git init &&
164 printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
165 git add a &&
166 test_tick && git commit -m A &&
168 git branch B &&
169 git checkout -b C &&
170 echo 10 >>a &&
171 echo "other content" >>new_a &&
172 git add a new_a &&
173 test_tick && git commit -m C &&
175 git checkout B &&
176 git mv a new_a &&
177 test_tick && git commit -m B &&
179 git checkout B^0 &&
180 test_must_fail git merge C &&
181 git clean -f &&
182 test_tick && git commit -m D &&
183 git tag D &&
185 git checkout C^0 &&
186 test_must_fail git merge B &&
187 rm new_a~HEAD new_a &&
188 printf "Incorrectly merged content" >>new_a &&
189 git add -u &&
190 test_tick && git commit -m E &&
191 git tag E
194 test_expect_success 'git detects differently handled merges conflict' '
195 git reset --hard &&
196 git checkout D^0 &&
198 git merge -s recursive E^0 && {
199 echo "BAD: should have conflicted"
200 test "Incorrectly merged content" = "$(cat new_a)" &&
201 echo "BAD: Silently accepted wrong content"
202 return 1
205 test 3 = $(git ls-files -s | wc -l) &&
206 test 3 = $(git ls-files -u | wc -l) &&
207 test 0 = $(git ls-files -o | wc -l) &&
209 test $(git rev-parse :2:new_a) = $(git rev-parse D:new_a) &&
210 test $(git rev-parse :3:new_a) = $(git rev-parse E:new_a) &&
212 git cat-file -p B:new_a >>merged &&
213 git cat-file -p C:new_a >>merge-me &&
214 >empty &&
215 test_must_fail git merge-file \
216 -L "Temporary merge branch 2" \
217 -L "" \
218 -L "Temporary merge branch 1" \
219 merged empty merge-me &&
220 sed -e "s/^\([<=>]\)/\1\1\1/" merged >merged-internal &&
221 test $(git rev-parse :1:new_a) = $(git hash-object merged-internal)
225 # criss-cross + modify/delete:
227 # B D
228 # o---o
229 # / \ / \
230 # A o X ? F
231 # \ / \ /
232 # o---o
233 # C E
235 # Commit A: file with contents 'A\n'
236 # Commit B: file with contents 'B\n'
237 # Commit C: file not present
238 # Commit D: file with contents 'B\n'
239 # Commit E: file not present
241 # Merging commits D & E should result in modify/delete conflict.
243 test_expect_success 'setup criss-cross + modify/delete resolved differently' '
244 git rm -rf . &&
245 git clean -fdqx &&
246 rm -rf .git &&
247 git init &&
249 echo A >file &&
250 git add file &&
251 test_tick &&
252 git commit -m A &&
254 git branch B &&
255 git checkout -b C &&
256 git rm file &&
257 test_tick &&
258 git commit -m C &&
260 git checkout B &&
261 echo B >file &&
262 git add file &&
263 test_tick &&
264 git commit -m B &&
266 git checkout B^0 &&
267 test_must_fail git merge C &&
268 echo B >file &&
269 git add file &&
270 test_tick &&
271 git commit -m D &&
272 git tag D &&
274 git checkout C^0 &&
275 test_must_fail git merge B &&
276 git rm file &&
277 test_tick &&
278 git commit -m E &&
279 git tag E
282 test_expect_success 'git detects conflict merging criss-cross+modify/delete' '
283 git checkout D^0 &&
285 test_must_fail git merge -s recursive E^0 &&
287 test 2 -eq $(git ls-files -s | wc -l) &&
288 test 2 -eq $(git ls-files -u | wc -l) &&
290 test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
291 test $(git rev-parse :2:file) = $(git rev-parse B:file)
294 test_expect_success 'git detects conflict merging criss-cross+modify/delete, reverse direction' '
295 git reset --hard &&
296 git checkout E^0 &&
298 test_must_fail git merge -s recursive D^0 &&
300 test 2 -eq $(git ls-files -s | wc -l) &&
301 test 2 -eq $(git ls-files -u | wc -l) &&
303 test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
304 test $(git rev-parse :3:file) = $(git rev-parse B:file)
308 # criss-cross + modify/modify with very contrived file contents:
310 # B D
311 # o---o
312 # / \ / \
313 # A o X ? F
314 # \ / \ /
315 # o---o
316 # C E
318 # Commit A: file with contents 'A\n'
319 # Commit B: file with contents 'B\n'
320 # Commit C: file with contents 'C\n'
321 # Commit D: file with contents 'D\n'
322 # Commit E: file with contents:
323 # <<<<<<< Temporary merge branch 1
325 # =======
327 # >>>>>>> Temporary merge branch 2
329 # Now, when we merge commits D & E, does git detect the conflict?
331 test_expect_success 'setup differently handled merges of content conflict' '
332 git clean -fdqx &&
333 rm -rf .git &&
334 git init &&
336 echo A >file &&
337 git add file &&
338 test_tick &&
339 git commit -m A &&
341 git branch B &&
342 git checkout -b C &&
343 echo C >file &&
344 git add file &&
345 test_tick &&
346 git commit -m C &&
348 git checkout B &&
349 echo B >file &&
350 git add file &&
351 test_tick &&
352 git commit -m B &&
354 git checkout B^0 &&
355 test_must_fail git merge C &&
356 echo D >file &&
357 git add file &&
358 test_tick &&
359 git commit -m D &&
360 git tag D &&
362 git checkout C^0 &&
363 test_must_fail git merge B &&
364 cat <<EOF >file &&
365 <<<<<<< Temporary merge branch 1
367 =======
369 >>>>>>> Temporary merge branch 2
371 git add file &&
372 test_tick &&
373 git commit -m E &&
374 git tag E
377 test_expect_failure 'git detects conflict w/ criss-cross+contrived resolution' '
378 git checkout D^0 &&
380 test_must_fail git merge -s recursive E^0 &&
382 test 3 -eq $(git ls-files -s | wc -l) &&
383 test 3 -eq $(git ls-files -u | wc -l) &&
384 test 0 -eq $(git ls-files -o | wc -l) &&
386 test $(git rev-parse :2:file) = $(git rev-parse D:file) &&
387 test $(git rev-parse :3:file) = $(git rev-parse E:file)
391 # criss-cross + d/f conflict via add/add:
392 # Commit A: Neither file 'a' nor directory 'a/' exists.
393 # Commit B: Introduce 'a'
394 # Commit C: Introduce 'a/file'
395 # Commit D: Merge B & C, keeping 'a' and deleting 'a/'
397 # Two different later cases:
398 # Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
399 # Commit E2: Merge B & C, deleting 'a' but keeping a slightly modified 'a/file'
401 # B D
402 # o---o
403 # / \ / \
404 # A o X ? F
405 # \ / \ /
406 # o---o
407 # C E1 or E2
409 # Merging D & E1 requires we first create a virtual merge base X from
410 # merging A & B in memory. Now, if X could keep both 'a' and 'a/file' in
411 # the index, then the merge of D & E1 could be resolved cleanly with both
412 # 'a' and 'a/file' removed. Since git does not currently allow creating
413 # such a tree, the best we can do is have X contain both 'a~<unique>' and
414 # 'a/file' resulting in the merge of D and E1 having a rename/delete
415 # conflict for 'a'. (Although this merge appears to be unsolvable with git
416 # currently, git could do a lot better than it currently does with these
417 # d/f conflicts, which is the purpose of this test.)
419 # Merge of D & E2 has similar issues for path 'a', but should always result
420 # in a modify/delete conflict for path 'a/file'.
422 # We run each merge in both directions, to check for directional issues
423 # with D/F conflict handling.
426 test_expect_success 'setup differently handled merges of directory/file conflict' '
427 git rm -rf . &&
428 git clean -fdqx &&
429 rm -rf .git &&
430 git init &&
432 >ignore-me &&
433 git add ignore-me &&
434 test_tick &&
435 git commit -m A &&
436 git tag A &&
438 git branch B &&
439 git checkout -b C &&
440 mkdir a &&
441 echo 10 >a/file &&
442 git add a/file &&
443 test_tick &&
444 git commit -m C &&
446 git checkout B &&
447 echo 5 >a &&
448 git add a &&
449 test_tick &&
450 git commit -m B &&
452 git checkout B^0 &&
453 test_must_fail git merge C &&
454 git clean -f &&
455 rm -rf a/ &&
456 echo 5 >a &&
457 git add a &&
458 test_tick &&
459 git commit -m D &&
460 git tag D &&
462 git checkout C^0 &&
463 test_must_fail git merge B &&
464 git clean -f &&
465 git rm --cached a &&
466 echo 10 >a/file &&
467 git add a/file &&
468 test_tick &&
469 git commit -m E1 &&
470 git tag E1 &&
472 git checkout C^0 &&
473 test_must_fail git merge B &&
474 git clean -f &&
475 git rm --cached a &&
476 printf "10\n11\n" >a/file &&
477 git add a/file &&
478 test_tick &&
479 git commit -m E2 &&
480 git tag E2
483 test_expect_success 'merge of D & E1 fails but has appropriate contents' '
484 get_clean_checkout D^0 &&
486 test_must_fail git merge -s recursive E1^0 &&
488 test 2 -eq $(git ls-files -s | wc -l) &&
489 test 1 -eq $(git ls-files -u | wc -l) &&
490 test 0 -eq $(git ls-files -o | wc -l) &&
492 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
493 test $(git rev-parse :2:a) = $(git rev-parse B:a)
496 test_expect_success 'merge of E1 & D fails but has appropriate contents' '
497 get_clean_checkout E1^0 &&
499 test_must_fail git merge -s recursive D^0 &&
501 test 2 -eq $(git ls-files -s | wc -l) &&
502 test 1 -eq $(git ls-files -u | wc -l) &&
503 test 0 -eq $(git ls-files -o | wc -l) &&
505 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
506 test $(git rev-parse :3:a) = $(git rev-parse B:a)
509 test_expect_success 'merge of D & E2 fails but has appropriate contents' '
510 get_clean_checkout D^0 &&
512 test_must_fail git merge -s recursive E2^0 &&
514 test 4 -eq $(git ls-files -s | wc -l) &&
515 test 3 -eq $(git ls-files -u | wc -l) &&
516 test 1 -eq $(git ls-files -o | wc -l) &&
518 test $(git rev-parse :2:a) = $(git rev-parse B:a) &&
519 test $(git rev-parse :3:a/file) = $(git rev-parse E2:a/file) &&
520 test $(git rev-parse :1:a/file) = $(git rev-parse C:a/file) &&
521 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
523 test -f a~HEAD
526 test_expect_success 'merge of E2 & D fails but has appropriate contents' '
527 get_clean_checkout E2^0 &&
529 test_must_fail git merge -s recursive D^0 &&
531 test 4 -eq $(git ls-files -s | wc -l) &&
532 test 3 -eq $(git ls-files -u | wc -l) &&
533 test 1 -eq $(git ls-files -o | wc -l) &&
535 test $(git rev-parse :3:a) = $(git rev-parse B:a) &&
536 test $(git rev-parse :2:a/file) = $(git rev-parse E2:a/file) &&
537 test $(git rev-parse :1:a/file) = $(git rev-parse C:a/file)
538 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
540 test -f a~D^0
544 # criss-cross with rename/rename(1to2)/modify followed by
545 # rename/rename(2to1)/modify:
547 # B D
548 # o---o
549 # / \ / \
550 # A o X ? F
551 # \ / \ /
552 # o---o
553 # C E
555 # Commit A: new file: a
556 # Commit B: rename a->b, modifying by adding a line
557 # Commit C: rename a->c
558 # Commit D: merge B&C, resolving conflict by keeping contents in newname
559 # Commit E: merge B&C, resolving conflict similar to D but adding another line
561 # There is a conflict merging B & C, but one of filename not of file
562 # content. Whoever created D and E chose specific resolutions for that
563 # conflict resolution. Now, since: (1) there is no content conflict
564 # merging B & C, (2) D does not modify that merged content further, and (3)
565 # both D & E resolve the name conflict in the same way, the modification to
566 # newname in E should not cause any conflicts when it is merged with D.
567 # (Note that this can be accomplished by having the virtual merge base have
568 # the merged contents of b and c stored in a file named a, which seems like
569 # the most logical choice anyway.)
571 # Comment from Junio: I do not necessarily agree with the choice "a", but
572 # it feels sound to say "B and C do not agree what the final pathname
573 # should be, but we know this content was derived from the common A:a so we
574 # use one path whose name is arbitrary in the virtual merge base X between
575 # D and E" and then further let the rename detection to notice that that
576 # arbitrary path gets renamed between X-D to "newname" and X-E also to
577 # "newname" to resolve it as both sides renaming it to the same new
578 # name. It is akin to what we do at the content level, i.e. "B and C do not
579 # agree what the final contents should be, so we leave the conflict marker
580 # but that may cancel out at the final merge stage".
582 test_expect_success 'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
583 git reset --hard &&
584 git rm -rf . &&
585 git clean -fdqx &&
586 rm -rf .git &&
587 git init &&
589 printf "1\n2\n3\n4\n5\n6\n" >a &&
590 git add a &&
591 git commit -m A &&
592 git tag A &&
594 git checkout -b B A &&
595 git mv a b &&
596 echo 7 >>b &&
597 git add -u &&
598 git commit -m B &&
600 git checkout -b C A &&
601 git mv a c &&
602 git commit -m C &&
604 git checkout -q B^0 &&
605 git merge --no-commit -s ours C^0 &&
606 git mv b newname &&
607 git commit -m "Merge commit C^0 into HEAD" &&
608 git tag D &&
610 git checkout -q C^0 &&
611 git merge --no-commit -s ours B^0 &&
612 git mv c newname &&
613 printf "7\n8\n" >>newname &&
614 git add -u &&
615 git commit -m "Merge commit B^0 into HEAD" &&
616 git tag E
619 test_expect_success 'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
620 git checkout D^0 &&
622 git merge -s recursive E^0 &&
624 test 1 -eq $(git ls-files -s | wc -l) &&
625 test 0 -eq $(git ls-files -u | wc -l) &&
626 test 0 -eq $(git ls-files -o | wc -l) &&
628 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
632 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
634 # B D
635 # o---o
636 # / \ / \
637 # A o X ? F
638 # \ / \ /
639 # o---o
640 # C E
642 # Commit A: new file: a
643 # Commit B: rename a->b
644 # Commit C: rename a->c, add different a
645 # Commit D: merge B&C, keeping b&c and (new) a modified at beginning
646 # Commit E: merge B&C, keeping b&c and (new) a modified at end
648 # Merging commits D & E should result in no conflict; doing so correctly
649 # requires getting the virtual merge base (from merging B&C) right, handling
650 # renaming carefully (both in the virtual merge base and later), and getting
651 # content merge handled.
653 test_expect_success 'setup criss-cross + rename/rename/add + modify/modify' '
654 git rm -rf . &&
655 git clean -fdqx &&
656 rm -rf .git &&
657 git init &&
659 printf "lots\nof\nwords\nand\ncontent\n" >a &&
660 git add a &&
661 git commit -m A &&
662 git tag A &&
664 git checkout -b B A &&
665 git mv a b &&
666 git commit -m B &&
668 git checkout -b C A &&
669 git mv a c &&
670 printf "2\n3\n4\n5\n6\n7\n" >a &&
671 git add a &&
672 git commit -m C &&
674 git checkout B^0 &&
675 git merge --no-commit -s ours C^0 &&
676 git checkout C -- a c &&
677 mv a old_a &&
678 echo 1 >a &&
679 cat old_a >>a &&
680 rm old_a &&
681 git add -u &&
682 git commit -m "Merge commit C^0 into HEAD" &&
683 git tag D &&
685 git checkout C^0 &&
686 git merge --no-commit -s ours B^0 &&
687 git checkout B -- b &&
688 echo 8 >>a &&
689 git add -u &&
690 git commit -m "Merge commit B^0 into HEAD" &&
691 git tag E
694 test_expect_failure 'detect rename/rename/add-source for virtual merge-base' '
695 git checkout D^0 &&
697 git merge -s recursive E^0 &&
699 test 3 -eq $(git ls-files -s | wc -l) &&
700 test 0 -eq $(git ls-files -u | wc -l) &&
701 test 0 -eq $(git ls-files -o | wc -l) &&
703 test $(git rev-parse HEAD:b) = $(git rev-parse A:a) &&
704 test $(git rev-parse HEAD:c) = $(git rev-parse A:a) &&
705 test "$(cat a)" = "$(printf "1\n2\n3\n4\n5\n6\n7\n8\n")"
709 # criss-cross with rename/rename(1to2)/add-dest + simple modify:
711 # B D
712 # o---o
713 # / \ / \
714 # A o X ? F
715 # \ / \ /
716 # o---o
717 # C E
719 # Commit A: new file: a
720 # Commit B: rename a->b, add c
721 # Commit C: rename a->c
722 # Commit D: merge B&C, keeping A:a and B:c
723 # Commit E: merge B&C, keeping A:a and slightly modified c from B
725 # Merging commits D & E should result in no conflict. The virtual merge
726 # base of B & C needs to not delete B:c for that to work, though...
728 test_expect_success 'setup criss-cross+rename/rename/add-dest + simple modify' '
729 git rm -rf . &&
730 git clean -fdqx &&
731 rm -rf .git &&
732 git init &&
734 >a &&
735 git add a &&
736 git commit -m A &&
737 git tag A &&
739 git checkout -b B A &&
740 git mv a b &&
741 printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
742 git add c &&
743 git commit -m B &&
745 git checkout -b C A &&
746 git mv a c &&
747 git commit -m C &&
749 git checkout B^0 &&
750 git merge --no-commit -s ours C^0 &&
751 git mv b a &&
752 git commit -m "D is like B but renames b back to a" &&
753 git tag D &&
755 git checkout B^0 &&
756 git merge --no-commit -s ours C^0 &&
757 git mv b a &&
758 echo 8 >>c &&
759 git add c &&
760 git commit -m "E like D but has mod in c" &&
761 git tag E
764 test_expect_success 'virtual merge base handles rename/rename(1to2)/add-dest' '
765 git checkout D^0 &&
767 git merge -s recursive E^0 &&
769 test 2 -eq $(git ls-files -s | wc -l) &&
770 test 0 -eq $(git ls-files -u | wc -l) &&
771 test 0 -eq $(git ls-files -o | wc -l) &&
773 test $(git rev-parse HEAD:a) = $(git rev-parse A:a) &&
774 test $(git rev-parse HEAD:c) = $(git rev-parse E:c)
777 test_done