Code

314fdaeb187a4db9cfa18e6019494d315794405a
[git.git] / t / t6036-recursive-corner-cases.sh
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"
11 }
13 #
14 #  L1  L2
15 #   o---o
16 #  / \ / \
17 # o   X   ?
18 #  \ / \ /
19 #   o---o
20 #  R1  R2
21 #
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
26         do
27                 echo line $i in a sample file
28         done >one &&
29         for i in $ten
30         do
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
52 '
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 5 = $(git ls-files -s | wc -l) &&
61         test 3 = $(git ls-files -u | wc -l) &&
62         test 0 = $(git ls-files -o | wc -l) &&
64         test $(git rev-parse :0:one) = $(git rev-parse L2:one) &&
65         test $(git rev-parse :0:two) = $(git rev-parse R2:two) &&
66         test $(git rev-parse :2:three) = $(git rev-parse L2:three) &&
67         test $(git rev-parse :3:three) = $(git rev-parse R2:three) &&
69         cp one merged &&
70         >empty &&
71         test_must_fail git merge-file \
72                 -L "Temporary merge branch 1" \
73                 -L "" \
74                 -L "Temporary merge branch 2" \
75                 merged empty two &&
76         test $(git rev-parse :1:three) = $(git hash-object merged)
77 '
79 #
80 # Same as before, but modify L1 slightly:
81 #
82 #  L1m L2
83 #   o---o
84 #  / \ / \
85 # o   X   ?
86 #  \ / \ /
87 #   o---o
88 #  R1  R2
89 #
91 test_expect_success 'setup criss-cross + rename merges with basic modification' '
92         git rm -rf . &&
93         git clean -fdqx &&
94         rm -rf .git &&
95         git init &&
97         ten="0 1 2 3 4 5 6 7 8 9"
98         for i in $ten
99         do
100                 echo line $i in a sample file
101         done >one &&
102         for i in $ten
103         do
104                 echo line $i in another sample file
105         done >two &&
106         git add one two &&
107         test_tick && git commit -m initial &&
109         git branch L1 &&
110         git checkout -b R1 &&
111         git mv one three &&
112         echo more >>two &&
113         git add two &&
114         test_tick && git commit -m R1 &&
116         git checkout L1 &&
117         git mv two three &&
118         test_tick && git commit -m L1 &&
120         git checkout L1^0 &&
121         test_tick && git merge -s ours R1 &&
122         git tag L2 &&
124         git checkout R1^0 &&
125         test_tick && git merge -s ours L1 &&
126         git tag R2
129 test_expect_success 'merge criss-cross + rename merges with basic modification' '
130         git reset --hard &&
131         git checkout L2^0 &&
133         test_must_fail git merge -s recursive R2^0 &&
135         test 5 = $(git ls-files -s | wc -l) &&
136         test 3 = $(git ls-files -u | wc -l) &&
137         test 0 = $(git ls-files -o | wc -l) &&
139         test $(git rev-parse :0:one) = $(git rev-parse L2:one) &&
140         test $(git rev-parse :0:two) = $(git rev-parse R2:two) &&
141         test $(git rev-parse :2:three) = $(git rev-parse L2:three) &&
142         test $(git rev-parse :3:three) = $(git rev-parse R2:three) &&
144         head -n 10 two >merged &&
145         cp one merge-me &&
146         >empty &&
147         test_must_fail git merge-file \
148                 -L "Temporary merge branch 1" \
149                 -L "" \
150                 -L "Temporary merge branch 2" \
151                 merge-me empty merged &&
153         test $(git rev-parse :1:three) = $(git hash-object merge-me)
157 # For the next test, we start with three commits in two lines of development
158 # which setup a rename/add conflict:
159 #   Commit A: File 'a' exists
160 #   Commit B: Rename 'a' -> 'new_a'
161 #   Commit C: Modify 'a', create different 'new_a'
162 # Later, two different people merge and resolve differently:
163 #   Commit D: Merge B & C, ignoring separately created 'new_a'
164 #   Commit E: Merge B & C making use of some piece of secondary 'new_a'
165 # Finally, someone goes to merge D & E.  Does git detect the conflict?
167 #      B   D
168 #      o---o
169 #     / \ / \
170 #  A o   X   ? F
171 #     \ / \ /
172 #      o---o
173 #      C   E
176 test_expect_success 'setup differently handled merges of rename/add conflict' '
177         git rm -rf . &&
178         git clean -fdqx &&
179         rm -rf .git &&
180         git init &&
182         printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
183         git add a &&
184         test_tick && git commit -m A &&
186         git branch B &&
187         git checkout -b C &&
188         echo 10 >>a &&
189         echo "other content" >>new_a &&
190         git add a new_a &&
191         test_tick && git commit -m C &&
193         git checkout B &&
194         git mv a new_a &&
195         test_tick && git commit -m B &&
197         git checkout B^0 &&
198         test_must_fail git merge C &&
199         git clean -f &&
200         test_tick && git commit -m D &&
201         git tag D &&
203         git checkout C^0 &&
204         test_must_fail git merge B &&
205         rm new_a~HEAD new_a &&
206         printf "Incorrectly merged content" >>new_a &&
207         git add -u &&
208         test_tick && git commit -m E &&
209         git tag E
212 test_expect_success 'git detects differently handled merges conflict' '
213         git reset --hard &&
214         git checkout D^0 &&
216         git merge -s recursive E^0 && {
217                 echo "BAD: should have conflicted"
218                 test "Incorrectly merged content" = "$(cat new_a)" &&
219                         echo "BAD: Silently accepted wrong content"
220                 return 1
221         }
223         test 3 = $(git ls-files -s | wc -l) &&
224         test 3 = $(git ls-files -u | wc -l) &&
225         test 0 = $(git ls-files -o | wc -l) &&
227         test $(git rev-parse :2:new_a) = $(git rev-parse D:new_a) &&
228         test $(git rev-parse :3:new_a) = $(git rev-parse E:new_a) &&
230         git cat-file -p B:new_a >>merged &&
231         git cat-file -p C:new_a >>merge-me &&
232         >empty &&
233         test_must_fail git merge-file \
234                 -L "Temporary merge branch 2" \
235                 -L "" \
236                 -L "Temporary merge branch 1" \
237                 merged empty merge-me &&
238         test $(git rev-parse :1:new_a) = $(git hash-object merged)
242 # criss-cross + modify/delete:
244 #      B   D
245 #      o---o
246 #     / \ / \
247 #  A o   X   ? F
248 #     \ / \ /
249 #      o---o
250 #      C   E
252 #   Commit A: file with contents 'A\n'
253 #   Commit B: file with contents 'B\n'
254 #   Commit C: file not present
255 #   Commit D: file with contents 'B\n'
256 #   Commit E: file not present
258 # Merging commits D & E should result in modify/delete conflict.
260 test_expect_success 'setup criss-cross + modify/delete resolved differently' '
261         git rm -rf . &&
262         git clean -fdqx &&
263         rm -rf .git &&
264         git init &&
266         echo A >file &&
267         git add file &&
268         test_tick &&
269         git commit -m A &&
271         git branch B &&
272         git checkout -b C &&
273         git rm file &&
274         test_tick &&
275         git commit -m C &&
277         git checkout B &&
278         echo B >file &&
279         git add file &&
280         test_tick &&
281         git commit -m B &&
283         git checkout B^0 &&
284         test_must_fail git merge C &&
285         echo B >file &&
286         git add file &&
287         test_tick &&
288         git commit -m D &&
289         git tag D &&
291         git checkout C^0 &&
292         test_must_fail git merge B &&
293         git rm file &&
294         test_tick &&
295         git commit -m E &&
296         git tag E
299 test_expect_success 'git detects conflict merging criss-cross+modify/delete' '
300         git checkout D^0 &&
302         test_must_fail git merge -s recursive E^0 &&
304         test 2 -eq $(git ls-files -s | wc -l) &&
305         test 2 -eq $(git ls-files -u | wc -l) &&
307         test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
308         test $(git rev-parse :2:file) = $(git rev-parse B:file)
311 test_expect_success 'git detects conflict merging criss-cross+modify/delete, reverse direction' '
312         git reset --hard &&
313         git checkout E^0 &&
315         test_must_fail git merge -s recursive D^0 &&
317         test 2 -eq $(git ls-files -s | wc -l) &&
318         test 2 -eq $(git ls-files -u | wc -l) &&
320         test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
321         test $(git rev-parse :3:file) = $(git rev-parse B:file)
325 # criss-cross + modify/modify with very contrived file contents:
327 #      B   D
328 #      o---o
329 #     / \ / \
330 #  A o   X   ? F
331 #     \ / \ /
332 #      o---o
333 #      C   E
335 #   Commit A: file with contents 'A\n'
336 #   Commit B: file with contents 'B\n'
337 #   Commit C: file with contents 'C\n'
338 #   Commit D: file with contents 'D\n'
339 #   Commit E: file with contents:
340 #      <<<<<<< Temporary merge branch 1
341 #      C
342 #      =======
343 #      B
344 #      >>>>>>> Temporary merge branch 2
346 # Now, when we merge commits D & E, does git detect the conflict?
348 test_expect_success 'setup differently handled merges of content conflict' '
349         git clean -fdqx &&
350         rm -rf .git &&
351         git init &&
353         echo A >file &&
354         git add file &&
355         test_tick &&
356         git commit -m A &&
358         git branch B &&
359         git checkout -b C &&
360         echo C >file &&
361         git add file &&
362         test_tick &&
363         git commit -m C &&
365         git checkout B &&
366         echo B >file &&
367         git add file &&
368         test_tick &&
369         git commit -m B &&
371         git checkout B^0 &&
372         test_must_fail git merge C &&
373         echo D >file &&
374         git add file &&
375         test_tick &&
376         git commit -m D &&
377         git tag D &&
379         git checkout C^0 &&
380         test_must_fail git merge B &&
381         cat <<EOF >file &&
382 <<<<<<< Temporary merge branch 1
384 =======
386 >>>>>>> Temporary merge branch 2
387 EOF
388         git add file &&
389         test_tick &&
390         git commit -m E &&
391         git tag E
394 test_expect_failure 'git detects conflict w/ criss-cross+contrived resolution' '
395         git checkout D^0 &&
397         test_must_fail git merge -s recursive E^0 &&
399         test 3 -eq $(git ls-files -s | wc -l) &&
400         test 3 -eq $(git ls-files -u | wc -l) &&
401         test 0 -eq $(git ls-files -o | wc -l) &&
403         test $(git rev-parse :2:file) = $(git rev-parse D:file) &&
404         test $(git rev-parse :3:file) = $(git rev-parse E:file)
408 # criss-cross + d/f conflict via add/add:
409 #   Commit A: Neither file 'a' nor directory 'a/' exist.
410 #   Commit B: Introduce 'a'
411 #   Commit C: Introduce 'a/file'
412 #   Commit D: Merge B & C, keeping 'a' and deleting 'a/'
414 # Two different later cases:
415 #   Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
416 #   Commit E2: Merge B & C, deleting 'a' but keeping a slightly modified 'a/file'
418 #      B   D
419 #      o---o
420 #     / \ / \
421 #  A o   X   ? F
422 #     \ / \ /
423 #      o---o
424 #      C   E1 or E2
426 # Merging D & E1 requires we first create a virtual merge base X from
427 # merging A & B in memory.  Now, if X could keep both 'a' and 'a/file' in
428 # the index, then the merge of D & E1 could be resolved cleanly with both
429 # 'a' and 'a/file' removed.  Since git does not currently allow creating
430 # such a tree, the best we can do is have X contain both 'a~<unique>' and
431 # 'a/file' resulting in the merge of D and E1 having a rename/delete
432 # conflict for 'a'.  (Although this merge appears to be unsolvable with git
433 # currently, git could do a lot better than it currently does with these
434 # d/f conflicts, which is the purpose of this test.)
436 # Merge of D & E2 has similar issues for path 'a', but should always result
437 # in a modify/delete conflict for path 'a/file'.
439 # We run each merge in both directions, to check for directional issues
440 # with D/F conflict handling.
443 test_expect_success 'setup differently handled merges of directory/file conflict' '
444         git rm -rf . &&
445         git clean -fdqx &&
446         rm -rf .git &&
447         git init &&
449         >ignore-me &&
450         git add ignore-me &&
451         test_tick &&
452         git commit -m A &&
453         git tag A &&
455         git branch B &&
456         git checkout -b C &&
457         mkdir a &&
458         echo 10 >a/file &&
459         git add a/file &&
460         test_tick &&
461         git commit -m C &&
463         git checkout B &&
464         echo 5 >a &&
465         git add a &&
466         test_tick &&
467         git commit -m B &&
469         git checkout B^0 &&
470         test_must_fail git merge C &&
471         git clean -f &&
472         rm -rf a/ &&
473         echo 5 >a &&
474         git add a &&
475         test_tick &&
476         git commit -m D &&
477         git tag D &&
479         git checkout C^0 &&
480         test_must_fail git merge B &&
481         git clean -f &&
482         git rm --cached a &&
483         echo 10 >a/file &&
484         git add a/file &&
485         test_tick &&
486         git commit -m E1 &&
487         git tag E1 &&
489         git checkout C^0 &&
490         test_must_fail git merge B &&
491         git clean -f &&
492         git rm --cached a &&
493         printf "10\n11\n" >a/file &&
494         git add a/file &&
495         test_tick &&
496         git commit -m E2 &&
497         git tag E2
500 test_expect_success 'merge of D & E1 fails but has appropriate contents' '
501         get_clean_checkout D^0 &&
503         test_must_fail git merge -s recursive E1^0 &&
505         test 2 -eq $(git ls-files -s | wc -l) &&
506         test 1 -eq $(git ls-files -u | wc -l) &&
507         test 0 -eq $(git ls-files -o | wc -l) &&
509         test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
510         test $(git rev-parse :2:a) = $(git rev-parse B:a)
513 test_expect_success 'merge of E1 & D fails but has appropriate contents' '
514         get_clean_checkout E1^0 &&
516         test_must_fail git merge -s recursive D^0 &&
518         test 2 -eq $(git ls-files -s | wc -l) &&
519         test 1 -eq $(git ls-files -u | wc -l) &&
520         test 0 -eq $(git ls-files -o | wc -l) &&
522         test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
523         test $(git rev-parse :3:a) = $(git rev-parse B:a)
526 test_expect_success 'merge of D & E2 fails but has appropriate contents' '
527         get_clean_checkout D^0 &&
529         test_must_fail git merge -s recursive E2^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 :2:a) = $(git rev-parse B:a) &&
536         test $(git rev-parse :3: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~HEAD
543 test_expect_success 'merge of E2 & D fails but has appropriate contents' '
544         get_clean_checkout E2^0 &&
546         test_must_fail git merge -s recursive D^0 &&
548         test 4 -eq $(git ls-files -s | wc -l) &&
549         test 3 -eq $(git ls-files -u | wc -l) &&
550         test 1 -eq $(git ls-files -o | wc -l) &&
552         test $(git rev-parse :3:a) = $(git rev-parse B:a) &&
553         test $(git rev-parse :2:a/file) = $(git rev-parse E2:a/file) &&
554         test $(git rev-parse :1:a/file) = $(git rev-parse C:a/file)
555         test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
557         test -f a~D^0
561 # criss-cross with rename/rename(1to2)/modify followed by
562 # rename/rename(2to1)/modify:
564 #      B   D
565 #      o---o
566 #     / \ / \
567 #  A o   X   ? F
568 #     \ / \ /
569 #      o---o
570 #      C   E
572 #   Commit A: new file: a
573 #   Commit B: rename a->b, modifying by adding a line
574 #   Commit C: rename a->c
575 #   Commit D: merge B&C, resolving conflict by keeping contents in newname
576 #   Commit E: merge B&C, resolving conflict similar to D but adding another line
578 # There is a conflict merging B & C, but one of filename not of file
579 # content.  Whoever created D and E chose specific resolutions for that
580 # conflict resolution.  Now, since: (1) there is no content conflict
581 # merging B & C, (2) D does not modify that merged content further, and (3)
582 # both D & E resolve the name conflict in the same way, the modification to
583 # newname in E should not cause any conflicts when it is merged with D.
584 # (Note that this can be accomplished by having the virtual merge base have
585 # the merged contents of b and c stored in a file named a, which seems like
586 # the most logical choice anyway.)
588 # Comment from Junio: I do not necessarily agree with the choice "a", but
589 # it feels sound to say "B and C do not agree what the final pathname
590 # should be, but we know this content was derived from the common A:a so we
591 # use one path whose name is arbitrary in the virtual merge base X between
592 # D and E" and then further let the rename detection to notice that that
593 # arbitrary path gets renamed between X-D to "newname" and X-E also to
594 # "newname" to resolve it as both sides renaming it to the same new
595 # name. It is akin to what we do at the content level, i.e. "B and C do not
596 # agree what the final contents should be, so we leave the conflict marker
597 # but that may cancel out at the final merge stage".
599 test_expect_success 'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
600         git reset --hard &&
601         git rm -rf . &&
602         git clean -fdqx &&
603         rm -rf .git &&
604         git init &&
606         printf "1\n2\n3\n4\n5\n6\n" >a &&
607         git add a &&
608         git commit -m A &&
609         git tag A &&
611         git checkout -b B A &&
612         git mv a b &&
613         echo 7 >>b &&
614         git add -u &&
615         git commit -m B &&
617         git checkout -b C A &&
618         git mv a c &&
619         git commit -m C &&
621         git checkout -q B^0 &&
622         git merge --no-commit -s ours C^0 &&
623         git mv b newname &&
624         git commit -m "Merge commit C^0 into HEAD" &&
625         git tag D &&
627         git checkout -q C^0 &&
628         git merge --no-commit -s ours B^0 &&
629         git mv c newname &&
630         printf "7\n8\n" >>newname &&
631         git add -u &&
632         git commit -m "Merge commit B^0 into HEAD" &&
633         git tag E
636 test_expect_failure 'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
637         git checkout D^0 &&
639         git merge -s recursive E^0 &&
641         test 1 -eq $(git ls-files -s | wc -l) &&
642         test 0 -eq $(git ls-files -u | wc -l) &&
643         test 0 -eq $(git ls-files -o | wc -l) &&
645         test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
649 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
651 #      B   D
652 #      o---o
653 #     / \ / \
654 #  A o   X   ? F
655 #     \ / \ /
656 #      o---o
657 #      C   E
659 #   Commit A: new file: a
660 #   Commit B: rename a->b
661 #   Commit C: rename a->c, add different a
662 #   Commit D: merge B&C, keeping b&c and (new) a modified at beginning
663 #   Commit E: merge B&C, keeping b&c and (new) a modified at end
665 # Merging commits D & E should result in no conflict; doing so correctly
666 # requires getting the virtual merge base (from merging B&C) right, handling
667 # renaming carefully (both in the virtual merge base and later), and getting
668 # content merge handled.
670 test_expect_success 'setup criss-cross + rename/rename/add + modify/modify' '
671         git rm -rf . &&
672         git clean -fdqx &&
673         rm -rf .git &&
674         git init &&
676         printf "lots\nof\nwords\nand\ncontent\n" >a &&
677         git add a &&
678         git commit -m A &&
679         git tag A &&
681         git checkout -b B A &&
682         git mv a b &&
683         git commit -m B &&
685         git checkout -b C A &&
686         git mv a c &&
687         printf "2\n3\n4\n5\n6\n7\n" >a &&
688         git add a &&
689         git commit -m C &&
691         git checkout B^0 &&
692         git merge --no-commit -s ours C^0 &&
693         git checkout C -- a c &&
694         mv a old_a &&
695         echo 1 >a &&
696         cat old_a >>a &&
697         rm old_a &&
698         git add -u &&
699         git commit -m "Merge commit C^0 into HEAD" &&
700         git tag D &&
702         git checkout C^0 &&
703         git merge --no-commit -s ours B^0 &&
704         git checkout B -- b &&
705         echo 8 >>a &&
706         git add -u &&
707         git commit -m "Merge commit B^0 into HEAD" &&
708         git tag E
711 test_expect_failure 'detect rename/rename/add-source for virtual merge-base' '
712         git checkout D^0 &&
714         git merge -s recursive E^0 &&
716         test 3 -eq $(git ls-files -s | wc -l) &&
717         test 0 -eq $(git ls-files -u | wc -l) &&
718         test 0 -eq $(git ls-files -o | wc -l) &&
720         test $(git rev-parse HEAD:b) = $(git rev-parse A:a) &&
721         test $(git rev-parse HEAD:c) = $(git rev-parse A:a) &&
722         test "$(cat a)" = "$(printf "1\n2\n3\n4\n5\n6\n7\n8\n")"
725 test_done