1#!/bin/sh
2
3test_description='recursive merge corner cases involving criss-cross merges'
4
5. ./test-lib.sh
6
7get_clean_checkout () {
8 git reset --hard &&
9 git clean -fdqx &&
10 git checkout "$1"
11}
12
13#
14# L1 L2
15# o---o
16# / \ / \
17# o X ?
18# \ / \ /
19# o---o
20# R1 R2
21#
22
23test_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 &&
35
36 git branch L1 &&
37 git checkout -b R1 &&
38 git mv one three &&
39 test_tick && git commit -m R1 &&
40
41 git checkout L1 &&
42 git mv two three &&
43 test_tick && git commit -m L1 &&
44
45 git checkout L1^0 &&
46 test_tick && git merge -s ours R1 &&
47 git tag L2 &&
48
49 git checkout R1^0 &&
50 test_tick && git merge -s ours L1 &&
51 git tag R2
52'
53
54test_expect_success 'merge simple rename+criss-cross with no modifications' '
55 git reset --hard &&
56 git checkout L2^0 &&
57
58 test_must_fail git merge -s recursive R2^0 &&
59
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) &&
63
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) &&
68
69 cp two merged &&
70 >empty &&
71 test_must_fail git merge-file \
72 -L "Temporary merge branch 2" \
73 -L "" \
74 -L "Temporary merge branch 1" \
75 merged empty one &&
76 test $(git rev-parse :1:three) = $(git hash-object merged)
77'
78
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#
90
91test_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 &&
96
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 &&
108
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 &&
115
116 git checkout L1 &&
117 git mv two three &&
118 test_tick && git commit -m L1 &&
119
120 git checkout L1^0 &&
121 test_tick && git merge -s ours R1 &&
122 git tag L2 &&
123
124 git checkout R1^0 &&
125 test_tick && git merge -s ours L1 &&
126 git tag R2
127'
128
129test_expect_success 'merge criss-cross + rename merges with basic modification' '
130 git reset --hard &&
131 git checkout L2^0 &&
132
133 test_must_fail git merge -s recursive R2^0 &&
134
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) &&
138
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) &&
143
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 2" \
149 -L "" \
150 -L "Temporary merge branch 1" \
151 merged empty merge-me &&
152 test $(git rev-parse :1:three) = $(git hash-object merged)
153'
154
155#
156# For the next test, we start with three commits in two lines of development
157# which setup a rename/add conflict:
158# Commit A: File 'a' exists
159# Commit B: Rename 'a' -> 'new_a'
160# Commit C: Modify 'a', create different 'new_a'
161# Later, two different people merge and resolve differently:
162# Commit D: Merge B & C, ignoring separately created 'new_a'
163# Commit E: Merge B & C making use of some piece of secondary 'new_a'
164# Finally, someone goes to merge D & E. Does git detect the conflict?
165#
166# B D
167# o---o
168# / \ / \
169# A o X ? F
170# \ / \ /
171# o---o
172# C E
173#
174
175test_expect_success 'setup differently handled merges of rename/add conflict' '
176 git rm -rf . &&
177 git clean -fdqx &&
178 rm -rf .git &&
179 git init &&
180
181 printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
182 git add a &&
183 test_tick && git commit -m A &&
184
185 git branch B &&
186 git checkout -b C &&
187 echo 10 >>a &&
188 echo "other content" >>new_a &&
189 git add a new_a &&
190 test_tick && git commit -m C &&
191
192 git checkout B &&
193 git mv a new_a &&
194 test_tick && git commit -m B &&
195
196 git checkout B^0 &&
197 test_must_fail git merge C &&
198 git clean -f &&
199 test_tick && git commit -m D &&
200 git tag D &&
201
202 git checkout C^0 &&
203 test_must_fail git merge B &&
204 rm new_a~HEAD new_a &&
205 printf "Incorrectly merged content" >>new_a &&
206 git add -u &&
207 test_tick && git commit -m E &&
208 git tag E
209'
210
211test_expect_success 'git detects differently handled merges conflict' '
212 git reset --hard &&
213 git checkout D^0 &&
214
215 git merge -s recursive E^0 && {
216 echo "BAD: should have conflicted"
217 test "Incorrectly merged content" = "$(cat new_a)" &&
218 echo "BAD: Silently accepted wrong content"
219 return 1
220 }
221
222 test 3 = $(git ls-files -s | wc -l) &&
223 test 3 = $(git ls-files -u | wc -l) &&
224 test 0 = $(git ls-files -o | wc -l) &&
225
226 test $(git rev-parse :2:new_a) = $(git rev-parse D:new_a) &&
227 test $(git rev-parse :3:new_a) = $(git rev-parse E:new_a) &&
228
229 git cat-file -p B:new_a >>merged &&
230 git cat-file -p C:new_a >>merge-me &&
231 >empty &&
232 test_must_fail git merge-file \
233 -L "Temporary merge branch 2" \
234 -L "" \
235 -L "Temporary merge branch 1" \
236 merged empty merge-me &&
237 test $(git rev-parse :1:new_a) = $(git hash-object merged)
238'
239
240#
241# criss-cross + modify/delete:
242#
243# B D
244# o---o
245# / \ / \
246# A o X ? F
247# \ / \ /
248# o---o
249# C E
250#
251# Commit A: file with contents 'A\n'
252# Commit B: file with contents 'B\n'
253# Commit C: file not present
254# Commit D: file with contents 'B\n'
255# Commit E: file not present
256#
257# Merging commits D & E should result in modify/delete conflict.
258
259test_expect_success 'setup criss-cross + modify/delete resolved differently' '
260 git rm -rf . &&
261 git clean -fdqx &&
262 rm -rf .git &&
263 git init &&
264
265 echo A >file &&
266 git add file &&
267 test_tick &&
268 git commit -m A &&
269
270 git branch B &&
271 git checkout -b C &&
272 git rm file &&
273 test_tick &&
274 git commit -m C &&
275
276 git checkout B &&
277 echo B >file &&
278 git add file &&
279 test_tick &&
280 git commit -m B &&
281
282 git checkout B^0 &&
283 test_must_fail git merge C &&
284 echo B >file &&
285 git add file &&
286 test_tick &&
287 git commit -m D &&
288 git tag D &&
289
290 git checkout C^0 &&
291 test_must_fail git merge B &&
292 git rm file &&
293 test_tick &&
294 git commit -m E &&
295 git tag E
296'
297
298test_expect_failure 'git detects conflict merging criss-cross+modify/delete' '
299 git checkout D^0 &&
300
301 test_must_fail git merge -s recursive E^0 &&
302
303 test 2 -eq $(git ls-files -s | wc -l) &&
304 test 2 -eq $(git ls-files -u | wc -l) &&
305
306 test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
307 test $(git rev-parse :2:file) = $(git rev-parse B:file)
308'
309
310test_expect_failure 'git detects conflict merging criss-cross+modify/delete, reverse direction' '
311 git reset --hard &&
312 git checkout E^0 &&
313
314 test_must_fail git merge -s recursive D^0 &&
315
316 test 2 -eq $(git ls-files -s | wc -l) &&
317 test 2 -eq $(git ls-files -u | wc -l) &&
318
319 test $(git rev-parse :1:file) = $(git rev-parse master:file) &&
320 test $(git rev-parse :3:file) = $(git rev-parse B:file)
321'
322
323#
324# criss-cross + modify/modify with very contrived file contents:
325#
326# B D
327# o---o
328# / \ / \
329# A o X ? F
330# \ / \ /
331# o---o
332# C E
333#
334# Commit A: file with contents 'A\n'
335# Commit B: file with contents 'B\n'
336# Commit C: file with contents 'C\n'
337# Commit D: file with contents 'D\n'
338# Commit E: file with contents:
339# <<<<<<< Temporary merge branch 1
340# C
341# =======
342# B
343# >>>>>>> Temporary merge branch 2
344#
345# Now, when we merge commits D & E, does git detect the conflict?
346
347test_expect_success 'setup differently handled merges of content conflict' '
348 git clean -fdqx &&
349 rm -rf .git &&
350 git init &&
351
352 echo A >file &&
353 git add file &&
354 test_tick &&
355 git commit -m A &&
356
357 git branch B &&
358 git checkout -b C &&
359 echo C >file &&
360 git add file &&
361 test_tick &&
362 git commit -m C &&
363
364 git checkout B &&
365 echo B >file &&
366 git add file &&
367 test_tick &&
368 git commit -m B &&
369
370 git checkout B^0 &&
371 test_must_fail git merge C &&
372 echo D >file &&
373 git add file &&
374 test_tick &&
375 git commit -m D &&
376 git tag D &&
377
378 git checkout C^0 &&
379 test_must_fail git merge B &&
380 cat <<EOF >file &&
381<<<<<<< Temporary merge branch 1
382C
383=======
384B
385>>>>>>> Temporary merge branch 2
386EOF
387 git add file &&
388 test_tick &&
389 git commit -m E &&
390 git tag E
391'
392
393test_expect_failure 'git detects conflict w/ criss-cross+contrived resolution' '
394 git checkout D^0 &&
395
396 test_must_fail git merge -s recursive E^0 &&
397
398 test 3 -eq $(git ls-files -s | wc -l) &&
399 test 3 -eq $(git ls-files -u | wc -l) &&
400 test 0 -eq $(git ls-files -o | wc -l) &&
401
402 test $(git rev-parse :2:file) = $(git rev-parse D:file) &&
403 test $(git rev-parse :3:file) = $(git rev-parse E:file)
404'
405
406#
407# criss-cross + d/f conflict via add/add:
408# Commit A: Neither file 'a' nor directory 'a/' exist.
409# Commit B: Introduce 'a'
410# Commit C: Introduce 'a/file'
411# Commit D: Merge B & C, keeping 'a' and deleting 'a/'
412#
413# Two different later cases:
414# Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
415# Commit E2: Merge B & C, deleting 'a' but keeping a slightly modified 'a/file'
416#
417# B D
418# o---o
419# / \ / \
420# A o X ? F
421# \ / \ /
422# o---o
423# C E1 or E2
424#
425# Merging D & E1 requires we first create a virtual merge base X from
426# merging A & B in memory. Now, if X could keep both 'a' and 'a/file' in
427# the index, then the merge of D & E1 could be resolved cleanly with both
428# 'a' and 'a/file' removed. Since git does not currently allow creating
429# such a tree, the best we can do is have X contain both 'a~<unique>' and
430# 'a/file' resulting in the merge of D and E1 having a rename/delete
431# conflict for 'a'. (Although this merge appears to be unsolvable with git
432# currently, git could do a lot better than it currently does with these
433# d/f conflicts, which is the purpose of this test.)
434#
435# Merge of D & E2 has similar issues for path 'a', but should always result
436# in a modify/delete conflict for path 'a/file'.
437#
438# We run each merge in both directions, to check for directional issues
439# with D/F conflict handling.
440#
441
442test_expect_success 'setup differently handled merges of directory/file conflict' '
443 git rm -rf . &&
444 git clean -fdqx &&
445 rm -rf .git &&
446 git init &&
447
448 >ignore-me &&
449 git add ignore-me &&
450 test_tick &&
451 git commit -m A &&
452 git tag A &&
453
454 git branch B &&
455 git checkout -b C &&
456 mkdir a &&
457 echo 10 >a/file &&
458 git add a/file &&
459 test_tick &&
460 git commit -m C &&
461
462 git checkout B &&
463 echo 5 >a &&
464 git add a &&
465 test_tick &&
466 git commit -m B &&
467
468 git checkout B^0 &&
469 test_must_fail git merge C &&
470 git clean -f &&
471 rm -rf a/ &&
472 echo 5 >a &&
473 git add a &&
474 test_tick &&
475 git commit -m D &&
476 git tag D &&
477
478 git checkout C^0 &&
479 test_must_fail git merge B &&
480 git clean -f &&
481 git rm --cached a &&
482 echo 10 >a/file &&
483 git add a/file &&
484 test_tick &&
485 git commit -m E1 &&
486 git tag E1 &&
487
488 git checkout C^0 &&
489 test_must_fail git merge B &&
490 git clean -f &&
491 git rm --cached a &&
492 printf "10\n11\n" >a/file &&
493 git add a/file &&
494 test_tick &&
495 git commit -m E2 &&
496 git tag E2
497'
498
499test_expect_failure 'merge of D & E1 fails but has appropriate contents' '
500 get_clean_checkout D^0 &&
501
502 test_must_fail git merge -s recursive E1^0 &&
503
504 test 2 -eq $(git ls-files -s | wc -l) &&
505 test 1 -eq $(git ls-files -u | wc -l) &&
506 test 0 -eq $(git ls-files -o | wc -l) &&
507
508 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
509 test $(git rev-parse :2:a) = $(git rev-parse B:a)
510'
511
512test_expect_success 'merge of E1 & D fails but has appropriate contents' '
513 get_clean_checkout E1^0 &&
514
515 test_must_fail git merge -s recursive D^0 &&
516
517 test 2 -eq $(git ls-files -s | wc -l) &&
518 test 1 -eq $(git ls-files -u | wc -l) &&
519 test 0 -eq $(git ls-files -o | wc -l) &&
520
521 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
522 test $(git rev-parse :3:a) = $(git rev-parse B:a)
523'
524
525test_expect_success 'merge of D & E2 fails but has appropriate contents' '
526 get_clean_checkout D^0 &&
527
528 test_must_fail git merge -s recursive E2^0 &&
529
530 test 4 -eq $(git ls-files -s | wc -l) &&
531 test 3 -eq $(git ls-files -u | wc -l) &&
532 test 1 -eq $(git ls-files -o | wc -l) &&
533
534 test $(git rev-parse :2:a) = $(git rev-parse B:a) &&
535 test $(git rev-parse :3:a/file) = $(git rev-parse E2:a/file) &&
536 test $(git rev-parse :1:a/file) = $(git rev-parse C:a/file) &&
537 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
538
539 test -f a~HEAD
540'
541
542test_expect_success 'merge of E2 & D fails but has appropriate contents' '
543 get_clean_checkout E2^0 &&
544
545 test_must_fail git merge -s recursive D^0 &&
546
547 test 4 -eq $(git ls-files -s | wc -l) &&
548 test 3 -eq $(git ls-files -u | wc -l) &&
549 test 1 -eq $(git ls-files -o | wc -l) &&
550
551 test $(git rev-parse :3:a) = $(git rev-parse B:a) &&
552 test $(git rev-parse :2:a/file) = $(git rev-parse E2:a/file) &&
553 test $(git rev-parse :1:a/file) = $(git rev-parse C:a/file)
554 test $(git rev-parse :0:ignore-me) = $(git rev-parse A:ignore-me) &&
555
556 test -f a~D^0
557'
558
559#
560# criss-cross with rename/rename(1to2)/modify followed by
561# rename/rename(2to1)/modify:
562#
563# B D
564# o---o
565# / \ / \
566# A o X ? F
567# \ / \ /
568# o---o
569# C E
570#
571# Commit A: new file: a
572# Commit B: rename a->b, modifying by adding a line
573# Commit C: rename a->c
574# Commit D: merge B&C, resolving conflict by keeping contents in newname
575# Commit E: merge B&C, resolving conflict similar to D but adding another line
576#
577# There is a conflict merging B & C, but one of filename not of file
578# content. Whoever created D and E chose specific resolutions for that
579# conflict resolution. Now, since: (1) there is no content conflict
580# merging B & C, (2) D does not modify that merged content further, and (3)
581# both D & E resolve the name conflict in the same way, the modification to
582# newname in E should not cause any conflicts when it is merged with D.
583# (Note that this can be accomplished by having the virtual merge base have
584# the merged contents of b and c stored in a file named a, which seems like
585# the most logical choice anyway.)
586#
587# Comment from Junio: I do not necessarily agree with the choice "a", but
588# it feels sound to say "B and C do not agree what the final pathname
589# should be, but we know this content was derived from the common A:a so we
590# use one path whose name is arbitrary in the virtual merge base X between
591# D and E" and then further let the rename detection to notice that that
592# arbitrary path gets renamed between X-D to "newname" and X-E also to
593# "newname" to resolve it as both sides renaming it to the same new
594# name. It is akin to what we do at the content level, i.e. "B and C do not
595# agree what the final contents should be, so we leave the conflict marker
596# but that may cancel out at the final merge stage".
597
598test_expect_success 'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
599 git reset --hard &&
600 git rm -rf . &&
601 git clean -fdqx &&
602 rm -rf .git &&
603 git init &&
604
605 printf "1\n2\n3\n4\n5\n6\n" >a &&
606 git add a &&
607 git commit -m A &&
608 git tag A &&
609
610 git checkout -b B A &&
611 git mv a b &&
612 echo 7 >>b &&
613 git add -u &&
614 git commit -m B &&
615
616 git checkout -b C A &&
617 git mv a c &&
618 git commit -m C &&
619
620 git checkout -q B^0 &&
621 git merge --no-commit -s ours C^0 &&
622 git mv b newname &&
623 git commit -m "Merge commit C^0 into HEAD" &&
624 git tag D &&
625
626 git checkout -q C^0 &&
627 git merge --no-commit -s ours B^0 &&
628 git mv c newname &&
629 printf "7\n8\n" >>newname &&
630 git add -u &&
631 git commit -m "Merge commit B^0 into HEAD" &&
632 git tag E
633'
634
635test_expect_failure 'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
636 git checkout D^0 &&
637
638 git merge -s recursive E^0 &&
639
640 test 1 -eq $(git ls-files -s | wc -l) &&
641 test 0 -eq $(git ls-files -u | wc -l) &&
642 test 0 -eq $(git ls-files -o | wc -l) &&
643
644 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
645'
646
647#
648# criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
649#
650# B D
651# o---o
652# / \ / \
653# A o X ? F
654# \ / \ /
655# o---o
656# C E
657#
658# Commit A: new file: a
659# Commit B: rename a->b
660# Commit C: rename a->c, add different a
661# Commit D: merge B&C, keeping b&c and (new) a modified at beginning
662# Commit E: merge B&C, keeping b&c and (new) a modified at end
663#
664# Merging commits D & E should result in no conflict; doing so correctly
665# requires getting the virtual merge base (from merging B&C) right, handling
666# renaming carefully (both in the virtual merge base and later), and getting
667# content merge handled.
668
669test_expect_success 'setup criss-cross + rename/rename/add + modify/modify' '
670 git rm -rf . &&
671 git clean -fdqx &&
672 rm -rf .git &&
673 git init &&
674
675 printf "lots\nof\nwords\nand\ncontent\n" >a &&
676 git add a &&
677 git commit -m A &&
678 git tag A &&
679
680 git checkout -b B A &&
681 git mv a b &&
682 git commit -m B &&
683
684 git checkout -b C A &&
685 git mv a c &&
686 printf "2\n3\n4\n5\n6\n7\n" >a &&
687 git add a &&
688 git commit -m C &&
689
690 git checkout B^0 &&
691 git merge --no-commit -s ours C^0 &&
692 git checkout C -- a c &&
693 mv a old_a &&
694 echo 1 >a &&
695 cat old_a >>a &&
696 rm old_a &&
697 git add -u &&
698 git commit -m "Merge commit C^0 into HEAD" &&
699 git tag D &&
700
701 git checkout C^0 &&
702 git merge --no-commit -s ours B^0 &&
703 git checkout B -- b &&
704 echo 8 >>a &&
705 git add -u &&
706 git commit -m "Merge commit B^0 into HEAD" &&
707 git tag E
708'
709
710test_expect_failure 'detect rename/rename/add-source for virtual merge-base' '
711 git checkout D^0 &&
712
713 git merge -s recursive E^0 &&
714
715 test 3 -eq $(git ls-files -s | wc -l) &&
716 test 0 -eq $(git ls-files -u | wc -l) &&
717 test 0 -eq $(git ls-files -o | wc -l) &&
718
719 test $(git rev-parse HEAD:b) = $(git rev-parse A:a) &&
720 test $(git rev-parse HEAD:c) = $(git rev-parse A:a) &&
721 test "$(cat a)" = "$(printf "1\n2\n3\n4\n5\n6\n7\n8\n")"
722'
723
724test_done