The second batch
[git] / t / t6416-recursive-corner-cases.sh
1 #!/bin/sh
2
3 test_description='recursive merge corner cases involving criss-cross merges'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-merge.sh
10
11 #
12 #  L1  L2
13 #   o---o
14 #  / \ / \
15 # o   X   ?
16 #  \ / \ /
17 #   o---o
18 #  R1  R2
19 #
20
21 test_expect_success 'setup basic criss-cross + rename with no modifications' '
22         test_create_repo basic-rename &&
23         (
24                 cd basic-rename &&
25
26                 ten="0 1 2 3 4 5 6 7 8 9" &&
27                 for i in $ten
28                 do
29                         echo line $i in a sample file
30                 done >one &&
31                 for i in $ten
32                 do
33                         echo line $i in another sample file
34                 done >two &&
35                 git add one two &&
36                 test_tick && git commit -m initial &&
37
38                 git branch L1 &&
39                 git checkout -b R1 &&
40                 git mv one three &&
41                 test_tick && git commit -m R1 &&
42
43                 git checkout L1 &&
44                 git mv two three &&
45                 test_tick && git commit -m L1 &&
46
47                 git checkout L1^0 &&
48                 test_tick && git merge -s ours R1 &&
49                 git tag L2 &&
50
51                 git checkout R1^0 &&
52                 test_tick && git merge -s ours L1 &&
53                 git tag R2
54         )
55 '
56
57 test_expect_success 'merge simple rename+criss-cross with no modifications' '
58         (
59                 cd basic-rename &&
60
61                 git reset --hard &&
62                 git checkout L2^0 &&
63
64                 test_must_fail git merge -s recursive R2^0 &&
65
66                 git ls-files -s >out &&
67                 test_line_count = 5 out &&
68                 git ls-files -u >out &&
69                 test_line_count = 3 out &&
70                 git ls-files -o >out &&
71                 test_line_count = 1 out &&
72
73                 git rev-parse >expect       \
74                         L2:three   R2:three &&
75                 git rev-parse   >actual     \
76                         :2:three   :3:three &&
77                 test_cmp expect actual
78         )
79 '
80
81 #
82 # Same as before, but modify L1 slightly:
83 #
84 #  L1m L2
85 #   o---o
86 #  / \ / \
87 # o   X   ?
88 #  \ / \ /
89 #   o---o
90 #  R1  R2
91 #
92
93 test_expect_success 'setup criss-cross + rename merges with basic modification' '
94         test_create_repo rename-modify &&
95         (
96                 cd rename-modify &&
97
98                 ten="0 1 2 3 4 5 6 7 8 9" &&
99                 for i in $ten
100                 do
101                         echo line $i in a sample file
102                 done >one &&
103                 for i in $ten
104                 do
105                         echo line $i in another sample file
106                 done >two &&
107                 git add one two &&
108                 test_tick && git commit -m initial &&
109
110                 git branch L1 &&
111                 git checkout -b R1 &&
112                 git mv one three &&
113                 echo more >>two &&
114                 git add two &&
115                 test_tick && git commit -m R1 &&
116
117                 git checkout L1 &&
118                 git mv two three &&
119                 test_tick && git commit -m L1 &&
120
121                 git checkout L1^0 &&
122                 test_tick && git merge -s ours R1 &&
123                 git tag L2 &&
124
125                 git checkout R1^0 &&
126                 test_tick && git merge -s ours L1 &&
127                 git tag R2
128         )
129 '
130
131 test_expect_success 'merge criss-cross + rename merges with basic modification' '
132         (
133                 cd rename-modify &&
134
135                 git checkout L2^0 &&
136
137                 test_must_fail git merge -s recursive R2^0 &&
138
139                 git ls-files -s >out &&
140                 test_line_count = 5 out &&
141                 git ls-files -u >out &&
142                 test_line_count = 3 out &&
143                 git ls-files -o >out &&
144                 test_line_count = 1 out &&
145
146                 git rev-parse >expect       \
147                         L2:three   R2:three &&
148                 git rev-parse   >actual     \
149                         :2:three   :3:three &&
150                 test_cmp expect actual
151         )
152 '
153
154 #
155 # For the next test, we start with three commits in two lines of development
156 # which setup a rename/add conflict:
157 #   Commit A: File 'a' exists
158 #   Commit B: Rename 'a' -> 'new_a'
159 #   Commit C: Modify 'a', create different 'new_a'
160 # Later, two different people merge and resolve differently:
161 #   Commit D: Merge B & C, ignoring separately created 'new_a'
162 #   Commit E: Merge B & C making use of some piece of secondary 'new_a'
163 # Finally, someone goes to merge D & E.  Does git detect the conflict?
164 #
165 #      B   D
166 #      o---o
167 #     / \ / \
168 #  A o   X   ? F
169 #     \ / \ /
170 #      o---o
171 #      C   E
172 #
173
174 test_expect_success 'setup differently handled merges of rename/add conflict' '
175         test_create_repo rename-add &&
176         (
177                 cd rename-add &&
178
179                 printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
180                 git add a &&
181                 test_tick && git commit -m A &&
182
183                 git branch B &&
184                 git checkout -b C &&
185                 echo 10 >>a &&
186                 test_write_lines 0 1 2 3 4 5 6 7 foobar >new_a &&
187                 git add a new_a &&
188                 test_tick && git commit -m C &&
189
190                 git checkout B &&
191                 git mv a new_a &&
192                 test_tick && git commit -m B &&
193
194                 git checkout B^0 &&
195                 test_must_fail git merge C &&
196                 git show :2:new_a >new_a &&
197                 git add new_a &&
198                 test_tick && git commit -m D &&
199                 git tag D &&
200
201                 git checkout C^0 &&
202                 test_must_fail git merge B &&
203                 test_write_lines 0 1 2 3 4 5 6 7 bad_merge >new_a &&
204                 git add -u &&
205                 test_tick && git commit -m E &&
206                 git tag E
207         )
208 '
209
210 test_expect_success 'git detects differently handled merges conflict' '
211         (
212                 cd rename-add &&
213
214                 git checkout D^0 &&
215
216                 test_must_fail git merge -s recursive E^0 &&
217
218                 git ls-files -s >out &&
219                 test_line_count = 3 out &&
220                 git ls-files -u >out &&
221                 test_line_count = 3 out &&
222                 git ls-files -o >out &&
223                 test_line_count = 1 out &&
224
225                 git cat-file -p C:new_a >ours &&
226                 git cat-file -p C:a >theirs &&
227                 >empty &&
228                 test_must_fail git merge-file \
229                         -L "Temporary merge branch 1" \
230                         -L "" \
231                         -L "Temporary merge branch 2" \
232                         ours empty theirs &&
233                 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
234                 git hash-object ours-tweaked >expect &&
235                 git rev-parse >>expect      \
236                                   D:new_a  E:new_a &&
237                 git rev-parse   >actual     \
238                         :1:new_a :2:new_a :3:new_a &&
239                 test_cmp expect actual &&
240
241                 # Test that the two-way merge in new_a is as expected
242                 git cat-file -p D:new_a >ours &&
243                 git cat-file -p E:new_a >theirs &&
244                 >empty &&
245                 test_must_fail git merge-file \
246                         -L "HEAD" \
247                         -L "" \
248                         -L "E^0" \
249                         ours empty theirs &&
250                 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
251                 git hash-object new_a >actual &&
252                 git hash-object ours  >expect &&
253                 test_cmp expect actual
254         )
255 '
256
257 # Repeat the above testcase with precisely the same setup, other than with
258 # the two merge bases having different orderings of commit timestamps so
259 # that they are reversed in the order they are provided to merge-recursive,
260 # so that we can improve code coverage.
261 test_expect_success 'git detects differently handled merges conflict, swapped' '
262         (
263                 cd rename-add &&
264
265                 # Difference #1: Do cleanup from previous testrun
266                 git reset --hard &&
267                 git clean -fdqx &&
268
269                 # Difference #2: Change commit timestamps
270                 btime=$(git log --no-walk --date=raw --format=%cd B | awk "{print \$1}") &&
271                 ctime=$(git log --no-walk --date=raw --format=%cd C | awk "{print \$1}") &&
272                 newctime=$(($btime+1)) &&
273                 git fast-export --no-data --all | sed -e s/$ctime/$newctime/ | git fast-import --force --quiet &&
274                 # End of most differences; rest is copy-paste of last test,
275                 # other than swapping C:a and C:new_a due to order switch
276
277                 git checkout D^0 &&
278                 test_must_fail git merge -s recursive E^0 &&
279
280                 git ls-files -s >out &&
281                 test_line_count = 3 out &&
282                 git ls-files -u >out &&
283                 test_line_count = 3 out &&
284                 git ls-files -o >out &&
285                 test_line_count = 1 out &&
286
287                 git cat-file -p C:a >ours &&
288                 git cat-file -p C:new_a >theirs &&
289                 >empty &&
290                 test_must_fail git merge-file \
291                         -L "Temporary merge branch 1" \
292                         -L "" \
293                         -L "Temporary merge branch 2" \
294                         ours empty theirs &&
295                 sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
296                 git hash-object ours-tweaked >expect &&
297                 git rev-parse >>expect      \
298                                   D:new_a  E:new_a &&
299                 git rev-parse   >actual     \
300                         :1:new_a :2:new_a :3:new_a &&
301                 test_cmp expect actual &&
302
303                 # Test that the two-way merge in new_a is as expected
304                 git cat-file -p D:new_a >ours &&
305                 git cat-file -p E:new_a >theirs &&
306                 >empty &&
307                 test_must_fail git merge-file \
308                         -L "HEAD" \
309                         -L "" \
310                         -L "E^0" \
311                         ours empty theirs &&
312                 sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
313                 git hash-object new_a >actual &&
314                 git hash-object ours  >expect &&
315                 test_cmp expect actual
316         )
317 '
318
319 #
320 # criss-cross + modify/delete:
321 #
322 #      B   D
323 #      o---o
324 #     / \ / \
325 #  A o   X   ? F
326 #     \ / \ /
327 #      o---o
328 #      C   E
329 #
330 #   Commit A: file with contents 'A\n'
331 #   Commit B: file with contents 'B\n'
332 #   Commit C: file not present
333 #   Commit D: file with contents 'B\n'
334 #   Commit E: file not present
335 #
336 # Merging commits D & E should result in modify/delete conflict.
337
338 test_expect_success 'setup criss-cross + modify/delete resolved differently' '
339         test_create_repo modify-delete &&
340         (
341                 cd modify-delete &&
342
343                 echo A >file &&
344                 git add file &&
345                 test_tick &&
346                 git commit -m A &&
347
348                 git branch B &&
349                 git checkout -b C &&
350                 git rm file &&
351                 test_tick &&
352                 git commit -m C &&
353
354                 git checkout B &&
355                 echo B >file &&
356                 git add file &&
357                 test_tick &&
358                 git commit -m B &&
359
360                 git checkout B^0 &&
361                 test_must_fail git merge C &&
362                 echo B >file &&
363                 git add file &&
364                 test_tick &&
365                 git commit -m D &&
366                 git tag D &&
367
368                 git checkout C^0 &&
369                 test_must_fail git merge B &&
370                 git rm file &&
371                 test_tick &&
372                 git commit -m E &&
373                 git tag E
374         )
375 '
376
377 test_expect_success 'git detects conflict merging criss-cross+modify/delete' '
378         (
379                 cd modify-delete &&
380
381                 git checkout D^0 &&
382
383                 test_must_fail git merge -s recursive E^0 &&
384
385                 git ls-files -s >out &&
386                 test_line_count = 2 out &&
387                 git ls-files -u >out &&
388                 test_line_count = 2 out &&
389
390                 git rev-parse >expect       \
391                         main:file    B:file &&
392                 git rev-parse   >actual      \
393                         :1:file      :2:file &&
394                 test_cmp expect actual
395         )
396 '
397
398 test_expect_success 'git detects conflict merging criss-cross+modify/delete, reverse direction' '
399         (
400                 cd modify-delete &&
401
402                 git reset --hard &&
403                 git checkout E^0 &&
404
405                 test_must_fail git merge -s recursive D^0 &&
406
407                 git ls-files -s >out &&
408                 test_line_count = 2 out &&
409                 git ls-files -u >out &&
410                 test_line_count = 2 out &&
411
412                 git rev-parse >expect       \
413                         main:file    B:file &&
414                 git rev-parse   >actual      \
415                         :1:file      :3:file &&
416                 test_cmp expect actual
417         )
418 '
419
420 #      SORRY FOR THE SUPER LONG DESCRIPTION, BUT THIS NEXT ONE IS HAIRY
421 #
422 # criss-cross + d/f conflict via add/add:
423 #   Commit A: Neither file 'a' nor directory 'a/' exists.
424 #   Commit B: Introduce 'a'
425 #   Commit C: Introduce 'a/file'
426 #   Commit D1: Merge B & C, keeping 'a'    and deleting 'a/'
427 #   Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
428 #
429 #      B   D1 or D2
430 #      o---o
431 #     / \ / \
432 #  A o   X   ? F
433 #     \ / \ /
434 #      o---o
435 #      C   E1 or E2 or E3
436 #
437 # I'll describe D2, E2, & E3 (which are alternatives for D1 & E1) more below...
438 #
439 # Merging D1 & E1 requires we first create a virtual merge base X from
440 # merging A & B in memory.  There are several possibilities for the merge-base:
441 #   1: Keep both 'a' and 'a/file' (assuming crazy filesystem allowing a tree
442 #      with a directory and file at same path): results in merge of D1 & E1
443 #      being clean with both files deleted.  Bad (no conflict detected).
444 #   2: Keep 'a' but not 'a/file': Merging D1 & E1 is clean and matches E1.  Bad.
445 #   3: Keep 'a/file' but not 'a': Merging D1 & E1 is clean and matches D1.  Bad.
446 #   4: Keep neither file: Merging D1 & E1 reports the D/F add/add conflict.
447 #
448 # So 4 sounds good for this case, but if we were to merge D1 & E3, where E3
449 # is defined as:
450 #   Commit E3: Merge B & C, keeping modified a, and deleting a/
451 # then we'd get an add/add conflict for 'a', which seems suboptimal.  A little
452 # creativity leads us to an alternate choice:
453 #   5: Keep 'a' as 'a~$UNIQUE' and a/file; results:
454 #        Merge D1 & E1: rename/delete conflict for 'a'; a/file silently deleted
455 #        Merge D1 & E3 is clean, as expected.
456 #
457 # So choice 5 at least provides some kind of conflict for the original case,
458 # and can merge cleanly as expected with D1 and E3.  It also made things just
459 # slightly funny for merging D1 and E4, where E4 is defined as:
460 #   Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
461 # in this case, we'll get a rename/rename(1to2) conflict because a~$UNIQUE
462 # gets renamed to 'a' in D1 and to 'a2' in E4.  But that's better than having
463 # two files (both 'a' and 'a2') sitting around without the user being notified
464 # that we could detect they were related and need to be merged.  Also, choice
465 # 5 makes the handling of 'a/file' seem suboptimal.  What if we were to merge
466 # D2 and E4, where D2 is:
467 #   Commit D2: Merge B & C, renaming 'a'->'a2', keeping 'a/file'
468 # This would result in a clean merge with 'a2' having three-way merged
469 # contents (good), and deleting 'a/' (bad) -- it doesn't detect the
470 # conflict in how the different sides treated a/file differently.
471 # Continuing down the creative route:
472 #   6: Keep 'a' as 'a~$UNIQUE1' and keep 'a/' as 'a~$UNIQUE2/'; results:
473 #        Merge D1 & E1: rename/delete conflict for 'a' and each path under 'a/'.
474 #        Merge D1 & E3: clean, as expected.
475 #        Merge D1 & E4: rename/rename(1to2) conflict on 'a' vs 'a2'.
476 #        Merge D2 & E4: clean for 'a2', rename/delete for a/file
477 #
478 # Choice 6 could cause rename detection to take longer (providing more targets
479 # that need to be searched).  Also, the conflict message for each path under
480 # 'a/' might be annoying unless we can detect it at the directory level, print
481 # it once, and then suppress it for individual filepaths underneath.
482 #
483 #
484 # As of time of writing, git uses choice 5.  Directory rename detection and
485 # rename detection performance improvements might make choice 6 a desirable
486 # improvement.  But we can at least document where we fall short for now...
487 #
488 #
489 # Historically, this testcase also used:
490 #   Commit E2: Merge B & C, deleting 'a' but keeping slightly modified 'a/file'
491 # The merge of D1 & E2 is very similar to D1 & E1 -- it has similar issues for
492 # path 'a', but should always result in a modify/delete conflict for path
493 # 'a/file'.  These tests ran the two merges
494 #   D1 & E1
495 #   D1 & E2
496 # in both directions, to check for directional issues with D/F conflict
497 # handling. Later we added
498 #   D1 & E3
499 #   D1 & E4
500 #   D2 & E4
501 # for good measure, though we only ran those one way because we had pretty
502 # good confidence in merge-recursive's directional handling of D/F issues.
503 #
504 # Just to summarize all the intermediate merge commits:
505 #   Commit D1: Merge B & C, keeping a    and deleting a/
506 #   Commit D2: Merge B & C, renaming a->a2, keeping a/file
507 #   Commit E1: Merge B & C, deleting a but keeping a/file
508 #   Commit E2: Merge B & C, deleting a but keeping slightly modified a/file
509 #   Commit E3: Merge B & C, keeping modified a, and deleting a/
510 #   Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
511 #
512
513 test_expect_success 'setup differently handled merges of directory/file conflict' '
514         test_create_repo directory-file &&
515         (
516                 cd directory-file &&
517
518                 >ignore-me &&
519                 git add ignore-me &&
520                 test_tick &&
521                 git commit -m A &&
522                 git tag A &&
523
524                 git branch B &&
525                 git checkout -b C &&
526                 mkdir a &&
527                 test_write_lines a b c d e f g >a/file &&
528                 git add a/file &&
529                 test_tick &&
530                 git commit -m C &&
531
532                 git checkout B &&
533                 test_write_lines 1 2 3 4 5 6 7 >a &&
534                 git add a &&
535                 test_tick &&
536                 git commit -m B &&
537
538                 git checkout B^0 &&
539                 git merge -s ours -m D1 C^0 &&
540                 git tag D1 &&
541
542                 git checkout B^0 &&
543                 test_must_fail git merge C^0 &&
544                 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
545                 then
546                         git rm -rf a/ &&
547                         git rm a~HEAD
548                 else
549                         git clean -fd &&
550                         git rm -rf a/ &&
551                         git rm a
552                 fi &&
553                 git cat-file -p B:a >a2 &&
554                 git add a2 &&
555                 git commit -m D2 &&
556                 git tag D2 &&
557
558                 git checkout C^0 &&
559                 git merge -s ours -m E1 B^0 &&
560                 git tag E1 &&
561
562                 git checkout C^0 &&
563                 git merge -s ours -m E2 B^0 &&
564                 test_write_lines a b c d e f g h >a/file &&
565                 git add a/file &&
566                 git commit --amend -C HEAD &&
567                 git tag E2 &&
568
569                 git checkout C^0 &&
570                 test_must_fail git merge B^0 &&
571                 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
572                 then
573                         git rm a~B^0
574                 else
575                         git clean -fd
576                 fi &&
577                 git rm -rf a/ &&
578                 test_write_lines 1 2 3 4 5 6 7 8 >a &&
579                 git add a &&
580                 git commit -m E3 &&
581                 git tag E3 &&
582
583                 git checkout C^0 &&
584                 test_must_fail git merge B^0 &&
585                 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
586                 then
587                         git rm -rf a/ &&
588                         git rm a~B^0
589                 else
590                         git clean -fd &&
591                         git rm -rf a/ &&
592                         git rm a
593                 fi &&
594                 test_write_lines 1 2 3 4 5 6 7 8 >a2 &&
595                 git add a2 &&
596                 git commit -m E4 &&
597                 git tag E4
598         )
599 '
600
601 test_expect_success 'merge of D1 & E1 fails but has appropriate contents' '
602         test_when_finished "git -C directory-file reset --hard" &&
603         test_when_finished "git -C directory-file clean -fdqx" &&
604         (
605                 cd directory-file &&
606
607                 git checkout D1^0 &&
608
609                 test_must_fail git merge -s recursive E1^0 &&
610
611                 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
612                 then
613                         git ls-files -s >out &&
614                         test_line_count = 3 out &&
615                         git ls-files -u >out &&
616                         test_line_count = 2 out &&
617                         git ls-files -o >out &&
618                         test_line_count = 1 out &&
619
620                         git rev-parse >expect    \
621                                 A:ignore-me  B:a  D1:a &&
622                         git rev-parse   >actual   \
623                                 :0:ignore-me :1:a :2:a &&
624                         test_cmp expect actual
625                 else
626                         git ls-files -s >out &&
627                         test_line_count = 2 out &&
628                         git ls-files -u >out &&
629                         test_line_count = 1 out &&
630                         git ls-files -o >out &&
631                         test_line_count = 1 out &&
632
633                         git rev-parse >expect    \
634                                 A:ignore-me  B:a &&
635                         git rev-parse   >actual   \
636                                 :0:ignore-me :2:a &&
637                         test_cmp expect actual
638                 fi
639         )
640 '
641
642 test_expect_success 'merge of E1 & D1 fails but has appropriate contents' '
643         test_when_finished "git -C directory-file reset --hard" &&
644         test_when_finished "git -C directory-file clean -fdqx" &&
645         (
646                 cd directory-file &&
647
648                 git checkout E1^0 &&
649
650                 test_must_fail git merge -s recursive D1^0 &&
651
652                 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
653                 then
654                         git ls-files -s >out &&
655                         test_line_count = 3 out &&
656                         git ls-files -u >out &&
657                         test_line_count = 2 out &&
658                         git ls-files -o >out &&
659                         test_line_count = 1 out &&
660
661                         git rev-parse >expect    \
662                                 A:ignore-me  B:a  D1:a &&
663                         git rev-parse   >actual   \
664                                 :0:ignore-me :1:a :3:a &&
665                         test_cmp expect actual
666                 else
667                         git ls-files -s >out &&
668                         test_line_count = 2 out &&
669                         git ls-files -u >out &&
670                         test_line_count = 1 out &&
671                         git ls-files -o >out &&
672                         test_line_count = 1 out &&
673
674                         git rev-parse >expect    \
675                                 A:ignore-me  B:a &&
676                         git rev-parse   >actual   \
677                                 :0:ignore-me :3:a &&
678                         test_cmp expect actual
679                 fi
680         )
681 '
682
683 test_expect_success 'merge of D1 & E2 fails but has appropriate contents' '
684         test_when_finished "git -C directory-file reset --hard" &&
685         test_when_finished "git -C directory-file clean -fdqx" &&
686         (
687                 cd directory-file &&
688
689                 git checkout D1^0 &&
690
691                 test_must_fail git merge -s recursive E2^0 &&
692
693                 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
694                 then
695                         git ls-files -s >out &&
696                         test_line_count = 5 out &&
697                         git ls-files -u >out &&
698                         test_line_count = 4 out &&
699                         git ls-files -o >out &&
700                         test_line_count = 1 out &&
701
702                         git rev-parse >expect    \
703                                 B:a       D1:a      E2:a/file  C:a/file   A:ignore-me &&
704                         git rev-parse   >actual   \
705                                 :1:a~HEAD :2:a~HEAD :3:a/file  :1:a/file  :0:ignore-me
706                 else
707                         git ls-files -s >out &&
708                         test_line_count = 4 out &&
709                         git ls-files -u >out &&
710                         test_line_count = 3 out &&
711                         git ls-files -o >out &&
712                         test_line_count = 2 out &&
713
714                         git rev-parse >expect    \
715                                 B:a    E2:a/file  C:a/file   A:ignore-me &&
716                         git rev-parse   >actual   \
717                                 :2:a   :3:a/file  :1:a/file  :0:ignore-me
718                 fi &&
719                 test_cmp expect actual &&
720
721                 test_path_is_file a~HEAD
722         )
723 '
724
725 test_expect_success 'merge of E2 & D1 fails but has appropriate contents' '
726         test_when_finished "git -C directory-file reset --hard" &&
727         test_when_finished "git -C directory-file clean -fdqx" &&
728         (
729                 cd directory-file &&
730
731                 git checkout E2^0 &&
732
733                 test_must_fail git merge -s recursive D1^0 &&
734
735                 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
736                 then
737                         git ls-files -s >out &&
738                         test_line_count = 5 out &&
739                         git ls-files -u >out &&
740                         test_line_count = 4 out &&
741                         git ls-files -o >out &&
742                         test_line_count = 1 out &&
743
744                         git rev-parse >expect    \
745                                 B:a       D1:a      E2:a/file  C:a/file   A:ignore-me &&
746                         git rev-parse   >actual   \
747                                 :1:a~D1^0 :3:a~D1^0 :2:a/file  :1:a/file  :0:ignore-me
748                 else
749                         git ls-files -s >out &&
750                         test_line_count = 4 out &&
751                         git ls-files -u >out &&
752                         test_line_count = 3 out &&
753                         git ls-files -o >out &&
754                         test_line_count = 2 out &&
755
756                         git rev-parse >expect    \
757                                 B:a   E2:a/file  C:a/file   A:ignore-me &&
758                         git rev-parse   >actual   \
759                                 :3:a  :2:a/file  :1:a/file  :0:ignore-me
760                 fi &&
761                 test_cmp expect actual &&
762
763                 test_path_is_file a~D1^0
764         )
765 '
766
767 test_expect_success 'merge of D1 & E3 succeeds' '
768         test_when_finished "git -C directory-file reset --hard" &&
769         test_when_finished "git -C directory-file clean -fdqx" &&
770         (
771                 cd directory-file &&
772
773                 git checkout D1^0 &&
774
775                 git merge -s recursive E3^0 &&
776
777                 git ls-files -s >out &&
778                 test_line_count = 2 out &&
779                 git ls-files -u >out &&
780                 test_line_count = 0 out &&
781                 git ls-files -o >out &&
782                 test_line_count = 1 out &&
783
784                 git rev-parse >expect    \
785                         A:ignore-me  E3:a &&
786                 git rev-parse   >actual   \
787                         :0:ignore-me :0:a &&
788                 test_cmp expect actual
789         )
790 '
791
792 test_expect_merge_algorithm failure success 'merge of D1 & E4 puts merge of a and a2 in both a and a2' '
793         test_when_finished "git -C directory-file reset --hard" &&
794         test_when_finished "git -C directory-file clean -fdqx" &&
795         (
796                 cd directory-file &&
797
798                 git checkout D1^0 &&
799
800                 test_must_fail git merge -s recursive E4^0 &&
801
802                 git ls-files -s >out &&
803                 test_line_count = 4 out &&
804                 git ls-files -u >out &&
805                 test_line_count = 3 out &&
806                 git ls-files -o >out &&
807                 test_line_count = 1 out &&
808
809                 git rev-parse >expect                  \
810                         A:ignore-me  B:a   E4:a2  E4:a2 &&
811                 git rev-parse   >actual                \
812                         :0:ignore-me :1:a~Temporary\ merge\ branch\ 2  :2:a  :3:a2 &&
813                 test_cmp expect actual
814         )
815 '
816
817 test_expect_failure 'merge of D2 & E4 merges a2s & reports conflict for a/file' '
818         test_when_finished "git -C directory-file reset --hard" &&
819         test_when_finished "git -C directory-file clean -fdqx" &&
820         (
821                 cd directory-file &&
822
823                 git checkout D2^0 &&
824
825                 test_must_fail git merge -s recursive E4^0 &&
826
827                 git ls-files -s >out &&
828                 test_line_count = 3 out &&
829                 git ls-files -u >out &&
830                 test_line_count = 1 out &&
831                 git ls-files -o >out &&
832                 test_line_count = 1 out &&
833
834                 git rev-parse >expect                 \
835                         A:ignore-me  E4:a2  D2:a/file &&
836                 git rev-parse   >actual               \
837                         :0:ignore-me :0:a2  :2:a/file &&
838                 test_cmp expect actual
839         )
840 '
841
842 #
843 # criss-cross with rename/rename(1to2)/modify followed by
844 # rename/rename(2to1)/modify:
845 #
846 #      B   D
847 #      o---o
848 #     / \ / \
849 #  A o   X   ? F
850 #     \ / \ /
851 #      o---o
852 #      C   E
853 #
854 #   Commit A: new file: a
855 #   Commit B: rename a->b, modifying by adding a line
856 #   Commit C: rename a->c
857 #   Commit D: merge B&C, resolving conflict by keeping contents in newname
858 #   Commit E: merge B&C, resolving conflict similar to D but adding another line
859 #
860 # There is a conflict merging B & C, but one of filename not of file
861 # content.  Whoever created D and E chose specific resolutions for that
862 # conflict resolution.  Now, since: (1) there is no content conflict
863 # merging B & C, (2) D does not modify that merged content further, and (3)
864 # both D & E resolve the name conflict in the same way, the modification to
865 # newname in E should not cause any conflicts when it is merged with D.
866 # (Note that this can be accomplished by having the virtual merge base have
867 # the merged contents of b and c stored in a file named a, which seems like
868 # the most logical choice anyway.)
869 #
870 # Comment from Junio: I do not necessarily agree with the choice "a", but
871 # it feels sound to say "B and C do not agree what the final pathname
872 # should be, but we know this content was derived from the common A:a so we
873 # use one path whose name is arbitrary in the virtual merge base X between
874 # D and E" and then further let the rename detection to notice that that
875 # arbitrary path gets renamed between X-D to "newname" and X-E also to
876 # "newname" to resolve it as both sides renaming it to the same new
877 # name. It is akin to what we do at the content level, i.e. "B and C do not
878 # agree what the final contents should be, so we leave the conflict marker
879 # but that may cancel out at the final merge stage".
880
881 test_expect_success 'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
882         test_create_repo rename-squared-squared &&
883         (
884                 cd rename-squared-squared &&
885
886                 printf "1\n2\n3\n4\n5\n6\n" >a &&
887                 git add a &&
888                 git commit -m A &&
889                 git tag A &&
890
891                 git checkout -b B A &&
892                 git mv a b &&
893                 echo 7 >>b &&
894                 git add -u &&
895                 git commit -m B &&
896
897                 git checkout -b C A &&
898                 git mv a c &&
899                 git commit -m C &&
900
901                 git checkout -q B^0 &&
902                 git merge --no-commit -s ours C^0 &&
903                 git mv b newname &&
904                 git commit -m "Merge commit C^0 into HEAD" &&
905                 git tag D &&
906
907                 git checkout -q C^0 &&
908                 git merge --no-commit -s ours B^0 &&
909                 git mv c newname &&
910                 printf "7\n8\n" >>newname &&
911                 git add -u &&
912                 git commit -m "Merge commit B^0 into HEAD" &&
913                 git tag E
914         )
915 '
916
917 test_expect_success 'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
918         (
919                 cd rename-squared-squared &&
920
921                 git checkout D^0 &&
922
923                 git merge -s recursive E^0 &&
924
925                 git ls-files -s >out &&
926                 test_line_count = 1 out &&
927                 git ls-files -u >out &&
928                 test_line_count = 0 out &&
929                 git ls-files -o >out &&
930                 test_line_count = 1 out &&
931
932                 test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
933         )
934 '
935
936 #
937 # criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
938 #
939 #      B   D
940 #      o---o
941 #     / \ / \
942 #  A o   X   ? F
943 #     \ / \ /
944 #      o---o
945 #      C   E
946 #
947 #   Commit A: new file: a
948 #   Commit B: rename a->b
949 #   Commit C: rename a->c, add different a
950 #   Commit D: merge B&C, keeping b&c and (new) a modified at beginning
951 #   Commit E: merge B&C, keeping b&c and (new) a modified at end
952 #
953 # Merging commits D & E should result in no conflict; doing so correctly
954 # requires getting the virtual merge base (from merging B&C) right, handling
955 # renaming carefully (both in the virtual merge base and later), and getting
956 # content merge handled.
957
958 test_expect_success 'setup criss-cross + rename/rename/add-source + modify/modify' '
959         test_create_repo rename-rename-add-source &&
960         (
961                 cd rename-rename-add-source &&
962
963                 printf "lots\nof\nwords\nand\ncontent\n" >a &&
964                 git add a &&
965                 git commit -m A &&
966                 git tag A &&
967
968                 git checkout -b B A &&
969                 git mv a b &&
970                 git commit -m B &&
971
972                 git checkout -b C A &&
973                 git mv a c &&
974                 printf "2\n3\n4\n5\n6\n7\n" >a &&
975                 git add a &&
976                 git commit -m C &&
977
978                 git checkout B^0 &&
979                 git merge --no-commit -s ours C^0 &&
980                 git checkout C -- a c &&
981                 mv a old_a &&
982                 echo 1 >a &&
983                 cat old_a >>a &&
984                 rm old_a &&
985                 git add -u &&
986                 git commit -m "Merge commit C^0 into HEAD" &&
987                 git tag D &&
988
989                 git checkout C^0 &&
990                 git merge --no-commit -s ours B^0 &&
991                 git checkout B -- b &&
992                 echo 8 >>a &&
993                 git add -u &&
994                 git commit -m "Merge commit B^0 into HEAD" &&
995                 git tag E
996         )
997 '
998
999 test_expect_failure 'detect rename/rename/add-source for virtual merge-base' '
1000         (
1001                 cd rename-rename-add-source &&
1002
1003                 git checkout D^0 &&
1004
1005                 git merge -s recursive E^0 &&
1006
1007                 git ls-files -s >out &&
1008                 test_line_count = 3 out &&
1009                 git ls-files -u >out &&
1010                 test_line_count = 0 out &&
1011                 git ls-files -o >out &&
1012                 test_line_count = 1 out &&
1013
1014                 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >correct &&
1015                 git rev-parse >expect \
1016                         A:a   A:a     \
1017                         correct       &&
1018                 git rev-parse   >actual  \
1019                         :0:b  :0:c       &&
1020                 git hash-object >>actual \
1021                         a                &&
1022                 test_cmp expect actual
1023         )
1024 '
1025
1026 #
1027 # criss-cross with rename/rename(1to2)/add-dest + simple modify:
1028 #
1029 #      B   D
1030 #      o---o
1031 #     / \ / \
1032 #  A o   X   ? F
1033 #     \ / \ /
1034 #      o---o
1035 #      C   E
1036 #
1037 #   Commit A: new file: a
1038 #   Commit B: rename a->b, add c
1039 #   Commit C: rename a->c
1040 #   Commit D: merge B&C, keeping A:a and B:c
1041 #   Commit E: merge B&C, keeping A:a and slightly modified c from B
1042 #
1043 # Merging commits D & E should result in no conflict.  The virtual merge
1044 # base of B & C needs to not delete B:c for that to work, though...
1045
1046 test_expect_success 'setup criss-cross+rename/rename/add-dest + simple modify' '
1047         test_create_repo rename-rename-add-dest &&
1048         (
1049                 cd rename-rename-add-dest &&
1050
1051                 >a &&
1052                 git add a &&
1053                 git commit -m A &&
1054                 git tag A &&
1055
1056                 git checkout -b B A &&
1057                 git mv a b &&
1058                 printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
1059                 git add c &&
1060                 git commit -m B &&
1061
1062                 git checkout -b C A &&
1063                 git mv a c &&
1064                 git commit -m C &&
1065
1066                 git checkout B^0 &&
1067                 git merge --no-commit -s ours C^0 &&
1068                 git mv b a &&
1069                 git commit -m "D is like B but renames b back to a" &&
1070                 git tag D &&
1071
1072                 git checkout B^0 &&
1073                 git merge --no-commit -s ours C^0 &&
1074                 git mv b a &&
1075                 echo 8 >>c &&
1076                 git add c &&
1077                 git commit -m "E like D but has mod in c" &&
1078                 git tag E
1079         )
1080 '
1081
1082 test_expect_success 'virtual merge base handles rename/rename(1to2)/add-dest' '
1083         (
1084                 cd rename-rename-add-dest &&
1085
1086                 git checkout D^0 &&
1087
1088                 git merge -s recursive E^0 &&
1089
1090                 git ls-files -s >out &&
1091                 test_line_count = 2 out &&
1092                 git ls-files -u >out &&
1093                 test_line_count = 0 out &&
1094                 git ls-files -o >out &&
1095                 test_line_count = 1 out &&
1096
1097                 git rev-parse >expect \
1098                         A:a   E:c     &&
1099                 git rev-parse   >actual \
1100                         :0:a  :0:c      &&
1101                 test_cmp expect actual
1102         )
1103 '
1104
1105 #
1106 # criss-cross with modify/modify on a symlink:
1107 #
1108 #      B   D
1109 #      o---o
1110 #     / \ / \
1111 #  A o   X   ? F
1112 #     \ / \ /
1113 #      o---o
1114 #      C   E
1115 #
1116 #   Commit A: simple simlink fickle->lagoon
1117 #   Commit B: redirect fickle->disneyland
1118 #   Commit C: redirect fickle->home
1119 #   Commit D: merge B&C, resolving in favor of B
1120 #   Commit E: merge B&C, resolving in favor of C
1121 #
1122 # This is an obvious modify/modify conflict for the symlink 'fickle'.  Can
1123 # git detect it?
1124
1125 test_expect_success 'setup symlink modify/modify' '
1126         test_create_repo symlink-modify-modify &&
1127         (
1128                 cd symlink-modify-modify &&
1129
1130                 test_ln_s_add lagoon fickle &&
1131                 git commit -m A &&
1132                 git tag A &&
1133
1134                 git checkout -b B A &&
1135                 git rm fickle &&
1136                 test_ln_s_add disneyland fickle &&
1137                 git commit -m B &&
1138
1139                 git checkout -b C A &&
1140                 git rm fickle &&
1141                 test_ln_s_add home fickle &&
1142                 git add fickle &&
1143                 git commit -m C &&
1144
1145                 git checkout -q B^0 &&
1146                 git merge -s ours -m D C^0 &&
1147                 git tag D &&
1148
1149                 git checkout -q C^0 &&
1150                 git merge -s ours -m E B^0 &&
1151                 git tag E
1152         )
1153 '
1154
1155 test_expect_merge_algorithm failure success 'check symlink modify/modify' '
1156         (
1157                 cd symlink-modify-modify &&
1158
1159                 git checkout D^0 &&
1160
1161                 test_must_fail git merge -s recursive E^0 &&
1162
1163                 git ls-files -s >out &&
1164                 test_line_count = 3 out &&
1165                 git ls-files -u >out &&
1166                 test_line_count = 3 out &&
1167                 git ls-files -o >out &&
1168                 test_line_count = 1 out
1169         )
1170 '
1171
1172 #
1173 # criss-cross with add/add of a symlink:
1174 #
1175 #      B   D
1176 #      o---o
1177 #     / \ / \
1178 #  A o   X   ? F
1179 #     \ / \ /
1180 #      o---o
1181 #      C   E
1182 #
1183 #   Commit A: No symlink or path exists yet
1184 #   Commit B: set up symlink: fickle->disneyland
1185 #   Commit C: set up symlink: fickle->home
1186 #   Commit D: merge B&C, resolving in favor of B
1187 #   Commit E: merge B&C, resolving in favor of C
1188 #
1189 # This is an obvious add/add conflict for the symlink 'fickle'.  Can
1190 # git detect it?
1191
1192 test_expect_success 'setup symlink add/add' '
1193         test_create_repo symlink-add-add &&
1194         (
1195                 cd symlink-add-add &&
1196
1197                 touch ignoreme &&
1198                 git add ignoreme &&
1199                 git commit -m A &&
1200                 git tag A &&
1201
1202                 git checkout -b B A &&
1203                 test_ln_s_add disneyland fickle &&
1204                 git commit -m B &&
1205
1206                 git checkout -b C A &&
1207                 test_ln_s_add home fickle &&
1208                 git add fickle &&
1209                 git commit -m C &&
1210
1211                 git checkout -q B^0 &&
1212                 git merge -s ours -m D C^0 &&
1213                 git tag D &&
1214
1215                 git checkout -q C^0 &&
1216                 git merge -s ours -m E B^0 &&
1217                 git tag E
1218         )
1219 '
1220
1221 test_expect_merge_algorithm failure success 'check symlink add/add' '
1222         (
1223                 cd symlink-add-add &&
1224
1225                 git checkout D^0 &&
1226
1227                 test_must_fail git merge -s recursive E^0 &&
1228
1229                 git ls-files -s >out &&
1230                 test_line_count = 3 out &&
1231                 git ls-files -u >out &&
1232                 test_line_count = 2 out &&
1233                 git ls-files -o >out &&
1234                 test_line_count = 1 out
1235         )
1236 '
1237
1238 #
1239 # criss-cross with modify/modify on a submodule:
1240 #
1241 #      B   D
1242 #      o---o
1243 #     / \ / \
1244 #  A o   X   ? F
1245 #     \ / \ /
1246 #      o---o
1247 #      C   E
1248 #
1249 #   Commit A: simple submodule repo
1250 #   Commit B: update repo
1251 #   Commit C: update repo differently
1252 #   Commit D: merge B&C, resolving in favor of B
1253 #   Commit E: merge B&C, resolving in favor of C
1254 #
1255 # This is an obvious modify/modify conflict for the submodule 'repo'.  Can
1256 # git detect it?
1257
1258 test_expect_success 'setup submodule modify/modify' '
1259         test_create_repo submodule-modify-modify &&
1260         (
1261                 cd submodule-modify-modify &&
1262
1263                 test_create_repo submod &&
1264                 (
1265                         cd submod &&
1266                         touch file-A &&
1267                         git add file-A &&
1268                         git commit -m A &&
1269                         git tag A &&
1270
1271                         git checkout -b B A &&
1272                         touch file-B &&
1273                         git add file-B &&
1274                         git commit -m B &&
1275                         git tag B &&
1276
1277                         git checkout -b C A &&
1278                         touch file-C &&
1279                         git add file-C &&
1280                         git commit -m C &&
1281                         git tag C
1282                 ) &&
1283
1284                 git -C submod reset --hard A &&
1285                 git add submod &&
1286                 git commit -m A &&
1287                 git tag A &&
1288
1289                 git checkout -b B A &&
1290                 git -C submod reset --hard B &&
1291                 git add submod &&
1292                 git commit -m B &&
1293
1294                 git checkout -b C A &&
1295                 git -C submod reset --hard C &&
1296                 git add submod &&
1297                 git commit -m C &&
1298
1299                 git checkout -q B^0 &&
1300                 git merge -s ours -m D C^0 &&
1301                 git tag D &&
1302
1303                 git checkout -q C^0 &&
1304                 git merge -s ours -m E B^0 &&
1305                 git tag E
1306         )
1307 '
1308
1309 test_expect_merge_algorithm failure success 'check submodule modify/modify' '
1310         (
1311                 cd submodule-modify-modify &&
1312
1313                 git checkout D^0 &&
1314
1315                 test_must_fail git merge -s recursive E^0 &&
1316
1317                 git ls-files -s >out &&
1318                 test_line_count = 3 out &&
1319                 git ls-files -u >out &&
1320                 test_line_count = 3 out &&
1321                 git ls-files -o >out &&
1322                 test_line_count = 1 out
1323         )
1324 '
1325
1326 #
1327 # criss-cross with add/add on a submodule:
1328 #
1329 #      B   D
1330 #      o---o
1331 #     / \ / \
1332 #  A o   X   ? F
1333 #     \ / \ /
1334 #      o---o
1335 #      C   E
1336 #
1337 #   Commit A: nothing of note
1338 #   Commit B: introduce submodule repo
1339 #   Commit C: introduce submodule repo at different commit
1340 #   Commit D: merge B&C, resolving in favor of B
1341 #   Commit E: merge B&C, resolving in favor of C
1342 #
1343 # This is an obvious add/add conflict for the submodule 'repo'.  Can
1344 # git detect it?
1345
1346 test_expect_success 'setup submodule add/add' '
1347         test_create_repo submodule-add-add &&
1348         (
1349                 cd submodule-add-add &&
1350
1351                 test_create_repo submod &&
1352                 (
1353                         cd submod &&
1354                         touch file-A &&
1355                         git add file-A &&
1356                         git commit -m A &&
1357                         git tag A &&
1358
1359                         git checkout -b B A &&
1360                         touch file-B &&
1361                         git add file-B &&
1362                         git commit -m B &&
1363                         git tag B &&
1364
1365                         git checkout -b C A &&
1366                         touch file-C &&
1367                         git add file-C &&
1368                         git commit -m C &&
1369                         git tag C
1370                 ) &&
1371
1372                 touch irrelevant-file &&
1373                 git add irrelevant-file &&
1374                 git commit -m A &&
1375                 git tag A &&
1376
1377                 git checkout -b B A &&
1378                 git -C submod reset --hard B &&
1379                 git add submod &&
1380                 git commit -m B &&
1381
1382                 git checkout -b C A &&
1383                 git -C submod reset --hard C &&
1384                 git add submod &&
1385                 git commit -m C &&
1386
1387                 git checkout -q B^0 &&
1388                 git merge -s ours -m D C^0 &&
1389                 git tag D &&
1390
1391                 git checkout -q C^0 &&
1392                 git merge -s ours -m E B^0 &&
1393                 git tag E
1394         )
1395 '
1396
1397 test_expect_merge_algorithm failure success 'check submodule add/add' '
1398         (
1399                 cd submodule-add-add &&
1400
1401                 git checkout D^0 &&
1402
1403                 test_must_fail git merge -s recursive E^0 &&
1404
1405                 git ls-files -s >out &&
1406                 test_line_count = 3 out &&
1407                 git ls-files -u >out &&
1408                 test_line_count = 2 out &&
1409                 git ls-files -o >out &&
1410                 test_line_count = 1 out
1411         )
1412 '
1413
1414 #
1415 # criss-cross with conflicting entry types:
1416 #
1417 #      B   D
1418 #      o---o
1419 #     / \ / \
1420 #  A o   X   ? F
1421 #     \ / \ /
1422 #      o---o
1423 #      C   E
1424 #
1425 #   Commit A: nothing of note
1426 #   Commit B: introduce submodule 'path'
1427 #   Commit C: introduce symlink 'path'
1428 #   Commit D: merge B&C, resolving in favor of B
1429 #   Commit E: merge B&C, resolving in favor of C
1430 #
1431 # This is an obvious add/add conflict for 'path'.  Can git detect it?
1432
1433 test_expect_success 'setup conflicting entry types (submodule vs symlink)' '
1434         test_create_repo submodule-symlink-add-add &&
1435         (
1436                 cd submodule-symlink-add-add &&
1437
1438                 test_create_repo path &&
1439                 (
1440                         cd path &&
1441                         touch file-B &&
1442                         git add file-B &&
1443                         git commit -m B &&
1444                         git tag B
1445                 ) &&
1446
1447                 touch irrelevant-file &&
1448                 git add irrelevant-file &&
1449                 git commit -m A &&
1450                 git tag A &&
1451
1452                 git checkout -b B A &&
1453                 git -C path reset --hard B &&
1454                 git add path &&
1455                 git commit -m B &&
1456
1457                 git checkout -b C A &&
1458                 rm -rf path/ &&
1459                 test_ln_s_add irrelevant-file path &&
1460                 git commit -m C &&
1461
1462                 git checkout -q B^0 &&
1463                 git merge -s ours -m D C^0 &&
1464                 git tag D &&
1465
1466                 git checkout -q C^0 &&
1467                 git merge -s ours -m E B^0 &&
1468                 git tag E
1469         )
1470 '
1471
1472 test_expect_merge_algorithm failure success 'check conflicting entry types (submodule vs symlink)' '
1473         (
1474                 cd submodule-symlink-add-add &&
1475
1476                 git checkout D^0 &&
1477
1478                 test_must_fail git merge -s recursive E^0 &&
1479
1480                 git ls-files -s >out &&
1481                 test_line_count = 3 out &&
1482                 git ls-files -u >out &&
1483                 test_line_count = 2 out &&
1484                 git ls-files -o >out &&
1485                 test_line_count = 1 out
1486         )
1487 '
1488
1489 #
1490 # criss-cross with regular files that have conflicting modes:
1491 #
1492 #      B   D
1493 #      o---o
1494 #     / \ / \
1495 #  A o   X   ? F
1496 #     \ / \ /
1497 #      o---o
1498 #      C   E
1499 #
1500 #   Commit A: nothing of note
1501 #   Commit B: introduce file source_me.bash, not executable
1502 #   Commit C: introduce file source_me.bash, executable
1503 #   Commit D: merge B&C, resolving in favor of B
1504 #   Commit E: merge B&C, resolving in favor of C
1505 #
1506 # This is an obvious add/add mode conflict.  Can git detect it?
1507
1508 test_expect_success 'setup conflicting modes for regular file' '
1509         test_create_repo regular-file-mode-conflict &&
1510         (
1511                 cd regular-file-mode-conflict &&
1512
1513                 touch irrelevant-file &&
1514                 git add irrelevant-file &&
1515                 git commit -m A &&
1516                 git tag A &&
1517
1518                 git checkout -b B A &&
1519                 echo "command_to_run" >source_me.bash &&
1520                 git add source_me.bash &&
1521                 git commit -m B &&
1522
1523                 git checkout -b C A &&
1524                 echo "command_to_run" >source_me.bash &&
1525                 git add source_me.bash &&
1526                 test_chmod +x source_me.bash &&
1527                 git commit -m C &&
1528
1529                 git checkout -q B^0 &&
1530                 git merge -s ours -m D C^0 &&
1531                 git tag D &&
1532
1533                 git checkout -q C^0 &&
1534                 git merge -s ours -m E B^0 &&
1535                 git tag E
1536         )
1537 '
1538
1539 test_expect_failure 'check conflicting modes for regular file' '
1540         (
1541                 cd regular-file-mode-conflict &&
1542
1543                 git checkout D^0 &&
1544
1545                 test_must_fail git merge -s recursive E^0 &&
1546
1547                 git ls-files -s >out &&
1548                 test_line_count = 3 out &&
1549                 git ls-files -u >out &&
1550                 test_line_count = 2 out &&
1551                 git ls-files -o >out &&
1552                 test_line_count = 1 out
1553         )
1554 '
1555
1556 # Setup:
1557 #          L1---L2
1558 #         /  \ /  \
1559 #     main    X    ?
1560 #         \  / \  /
1561 #          R1---R2
1562 #
1563 # Where:
1564 #   main has two files, named 'b' and 'a'
1565 #   branches L1 and R1 both modify each of the two files in conflicting ways
1566 #
1567 #   L2 is a merge of R1 into L1; more on it later.
1568 #   R2 is a merge of L1 into R1; more on it later.
1569 #
1570 #   X is an auto-generated merge-base used when merging L2 and R2.
1571 #   since X is a merge of L1 and R1, it has conflicting versions of each file
1572 #
1573 #   More about L2 and R2:
1574 #     - both resolve the conflicts in 'b' and 'a' differently
1575 #     - L2 renames 'b' to 'm'
1576 #     - R2 renames 'a' to 'm'
1577 #
1578 #   In the end, in file 'm' we have four different conflicting files (from
1579 #   two versions of 'b' and two of 'a').  In addition, if
1580 #   merge.conflictstyle is diff3, then the base version also has
1581 #   conflict markers of its own, leading to a total of three levels of
1582 #   conflict markers.  This is a pretty weird corner case, but we just want
1583 #   to ensure that we handle it as well as practical.
1584
1585 test_expect_success 'setup nested conflicts' '
1586         test_create_repo nested_conflicts &&
1587         (
1588                 cd nested_conflicts &&
1589
1590                 # Create some related files now
1591                 for i in $(test_seq 1 10)
1592                 do
1593                         echo Random base content line $i
1594                 done >initial &&
1595
1596                 cp initial b_L1 &&
1597                 cp initial b_R1 &&
1598                 cp initial b_L2 &&
1599                 cp initial b_R2 &&
1600                 cp initial a_L1 &&
1601                 cp initial a_R1 &&
1602                 cp initial a_L2 &&
1603                 cp initial a_R2 &&
1604
1605                 test_write_lines b b_L1 >>b_L1 &&
1606                 test_write_lines b b_R1 >>b_R1 &&
1607                 test_write_lines b b_L2 >>b_L2 &&
1608                 test_write_lines b b_R2 >>b_R2 &&
1609                 test_write_lines a a_L1 >>a_L1 &&
1610                 test_write_lines a a_R1 >>a_R1 &&
1611                 test_write_lines a a_L2 >>a_L2 &&
1612                 test_write_lines a a_R2 >>a_R2 &&
1613
1614                 # Setup original commit (or merge-base), consisting of
1615                 # files named "b" and "a"
1616                 cp initial b &&
1617                 cp initial a &&
1618                 echo b >>b &&
1619                 echo a >>a &&
1620                 git add b a &&
1621                 test_tick && git commit -m initial &&
1622
1623                 git branch L &&
1624                 git branch R &&
1625
1626                 # Handle the left side
1627                 git checkout L &&
1628                 mv -f b_L1 b &&
1629                 mv -f a_L1 a &&
1630                 git add b a &&
1631                 test_tick && git commit -m "version L1 of files" &&
1632                 git tag L1 &&
1633
1634                 # Handle the right side
1635                 git checkout R &&
1636                 mv -f b_R1 b &&
1637                 mv -f a_R1 a &&
1638                 git add b a &&
1639                 test_tick && git commit -m "version R1 of files" &&
1640                 git tag R1 &&
1641
1642                 # Create first merge on left side
1643                 git checkout L &&
1644                 test_must_fail git merge R1 &&
1645                 mv -f b_L2 b &&
1646                 mv -f a_L2 a &&
1647                 git add b a &&
1648                 git mv b m &&
1649                 test_tick && git commit -m "left merge, rename b->m" &&
1650                 git tag L2 &&
1651
1652                 # Create first merge on right side
1653                 git checkout R &&
1654                 test_must_fail git merge L1 &&
1655                 mv -f b_R2 b &&
1656                 mv -f a_R2 a &&
1657                 git add b a &&
1658                 git mv a m &&
1659                 test_tick && git commit -m "right merge, rename a->m" &&
1660                 git tag R2
1661         )
1662 '
1663
1664 test_expect_success 'check nested conflicts' '
1665         (
1666                 cd nested_conflicts &&
1667
1668                 git clean -f &&
1669                 MAIN=$(git rev-parse --short main) &&
1670                 git checkout L2^0 &&
1671
1672                 # Merge must fail; there is a conflict
1673                 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R2^0 &&
1674
1675                 # Make sure the index has the right number of entries
1676                 git ls-files -s >out &&
1677                 test_line_count = 2 out &&
1678                 git ls-files -u >out &&
1679                 test_line_count = 2 out &&
1680                 # Ensure we have the correct number of untracked files
1681                 git ls-files -o >out &&
1682                 test_line_count = 1 out &&
1683
1684                 # Create a and b from virtual merge base X
1685                 git cat-file -p main:a >base &&
1686                 git cat-file -p L1:a >ours &&
1687                 git cat-file -p R1:a >theirs &&
1688                 test_must_fail git merge-file --diff3 \
1689                         -L "Temporary merge branch 1" \
1690                         -L "$MAIN"  \
1691                         -L "Temporary merge branch 2" \
1692                         ours  \
1693                         base  \
1694                         theirs &&
1695                 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_a &&
1696
1697                 git cat-file -p main:b >base &&
1698                 git cat-file -p L1:b >ours &&
1699                 git cat-file -p R1:b >theirs &&
1700                 test_must_fail git merge-file --diff3 \
1701                         -L "Temporary merge branch 1" \
1702                         -L "$MAIN"  \
1703                         -L "Temporary merge branch 2" \
1704                         ours  \
1705                         base  \
1706                         theirs &&
1707                 sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_b &&
1708
1709                 # Compare :2:m to expected values
1710                 git cat-file -p L2:m >ours &&
1711                 git cat-file -p R2:b >theirs &&
1712                 test_must_fail git merge-file --diff3  \
1713                         -L "HEAD:m"                    \
1714                         -L "merged common ancestors:b" \
1715                         -L "R2^0:b"                    \
1716                         ours                           \
1717                         vmb_b                          \
1718                         theirs                         &&
1719                 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_2 &&
1720                 git cat-file -p :2:m >actual &&
1721                 test_cmp m_stage_2 actual &&
1722
1723                 # Compare :3:m to expected values
1724                 git cat-file -p L2:a >ours &&
1725                 git cat-file -p R2:m >theirs &&
1726                 test_must_fail git merge-file --diff3  \
1727                         -L "HEAD:a"                    \
1728                         -L "merged common ancestors:a" \
1729                         -L "R2^0:m"                    \
1730                         ours                           \
1731                         vmb_a                          \
1732                         theirs                         &&
1733                 sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_3 &&
1734                 git cat-file -p :3:m >actual &&
1735                 test_cmp m_stage_3 actual &&
1736
1737                 # Compare m to expected contents
1738                 >empty &&
1739                 cp m_stage_2 expected_final_m &&
1740                 test_must_fail git merge-file --diff3 \
1741                         -L "HEAD"                     \
1742                         -L "merged common ancestors"  \
1743                         -L "R2^0"                     \
1744                         expected_final_m              \
1745                         empty                         \
1746                         m_stage_3                     &&
1747                 test_cmp expected_final_m m
1748         )
1749 '
1750
1751 # Setup:
1752 #          L1---L2---L3
1753 #         /  \ /  \ /  \
1754 #     main    X1   X2   ?
1755 #         \  / \  / \  /
1756 #          R1---R2---R3
1757 #
1758 # Where:
1759 #   main has one file named 'content'
1760 #   branches L1 and R1 both modify each of the two files in conflicting ways
1761 #
1762 #   L<n> (n>1) is a merge of R<n-1> into L<n-1>
1763 #   R<n> (n>1) is a merge of L<n-1> into R<n-1>
1764 #   L<n> and R<n> resolve the conflicts differently.
1765 #
1766 #   X<n> is an auto-generated merge-base used when merging L<n+1> and R<n+1>.
1767 #   By construction, X1 has conflict markers due to conflicting versions.
1768 #   X2, due to using merge.conflictstyle=3, has nested conflict markers.
1769 #
1770 #   So, merging R3 into L3 using merge.conflictstyle=3 should show the
1771 #   nested conflict markers from X2 in the base version -- that means we
1772 #   have three levels of conflict markers.  Can we distinguish all three?
1773
1774 test_expect_success 'setup virtual merge base with nested conflicts' '
1775         test_create_repo virtual_merge_base_has_nested_conflicts &&
1776         (
1777                 cd virtual_merge_base_has_nested_conflicts &&
1778
1779                 # Create some related files now
1780                 for i in $(test_seq 1 10)
1781                 do
1782                         echo Random base content line $i
1783                 done >content &&
1784
1785                 # Setup original commit
1786                 git add content &&
1787                 test_tick && git commit -m initial &&
1788
1789                 git branch L &&
1790                 git branch R &&
1791
1792                 # Create L1
1793                 git checkout L &&
1794                 echo left >>content &&
1795                 git add content &&
1796                 test_tick && git commit -m "version L1 of content" &&
1797                 git tag L1 &&
1798
1799                 # Create R1
1800                 git checkout R &&
1801                 echo right >>content &&
1802                 git add content &&
1803                 test_tick && git commit -m "version R1 of content" &&
1804                 git tag R1 &&
1805
1806                 # Create L2
1807                 git checkout L &&
1808                 test_must_fail git -c merge.conflictstyle=diff3 merge R1 &&
1809                 git checkout L1 content &&
1810                 test_tick && git commit -m "version L2 of content" &&
1811                 git tag L2 &&
1812
1813                 # Create R2
1814                 git checkout R &&
1815                 test_must_fail git -c merge.conflictstyle=diff3 merge L1 &&
1816                 git checkout R1 content &&
1817                 test_tick && git commit -m "version R2 of content" &&
1818                 git tag R2 &&
1819
1820                 # Create L3
1821                 git checkout L &&
1822                 test_must_fail git -c merge.conflictstyle=diff3 merge R2 &&
1823                 git checkout L1 content &&
1824                 test_tick && git commit -m "version L3 of content" &&
1825                 git tag L3 &&
1826
1827                 # Create R3
1828                 git checkout R &&
1829                 test_must_fail git -c merge.conflictstyle=diff3 merge L2 &&
1830                 git checkout R1 content &&
1831                 test_tick && git commit -m "version R3 of content" &&
1832                 git tag R3
1833         )
1834 '
1835
1836 test_expect_success 'check virtual merge base with nested conflicts' '
1837         (
1838                 cd virtual_merge_base_has_nested_conflicts &&
1839
1840                 MAIN=$(git rev-parse --short main) &&
1841                 git checkout L3^0 &&
1842
1843                 # Merge must fail; there is a conflict
1844                 test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R3^0 &&
1845
1846                 # Make sure the index has the right number of entries
1847                 git ls-files -s >out &&
1848                 test_line_count = 3 out &&
1849                 git ls-files -u >out &&
1850                 test_line_count = 3 out &&
1851                 # Ensure we have the correct number of untracked files
1852                 git ls-files -o >out &&
1853                 test_line_count = 1 out &&
1854
1855                 # Compare :[23]:content to expected values
1856                 git rev-parse L1:content R1:content >expect &&
1857                 git rev-parse :2:content :3:content >actual &&
1858                 test_cmp expect actual &&
1859
1860                 # Imitate X1 merge base, except without long enough conflict
1861                 # markers because a subsequent sed will modify them.  Put
1862                 # result into vmb.
1863                 git cat-file -p main:content >base &&
1864                 git cat-file -p L:content >left &&
1865                 git cat-file -p R:content >right &&
1866                 cp left merged-once &&
1867                 test_must_fail git merge-file --diff3 \
1868                         -L "Temporary merge branch 1" \
1869                         -L "$MAIN"  \
1870                         -L "Temporary merge branch 2" \
1871                         merged-once \
1872                         base        \
1873                         right       &&
1874                 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-once >vmb &&
1875
1876                 # Imitate X2 merge base, overwriting vmb.  Note that we
1877                 # extend both sets of conflict markers to make them longer
1878                 # with the sed command.
1879                 cp left merged-twice &&
1880                 test_must_fail git merge-file --diff3 \
1881                         -L "Temporary merge branch 1" \
1882                         -L "merged common ancestors"  \
1883                         -L "Temporary merge branch 2" \
1884                         merged-twice \
1885                         vmb          \
1886                         right        &&
1887                 sed -e "s/^\([<|=>]\)/\1\1\1/" merged-twice >vmb &&
1888
1889                 # Compare :1:content to expected value
1890                 git cat-file -p :1:content >actual &&
1891                 test_cmp vmb actual &&
1892
1893                 # Determine expected content in final outer merge, compare to
1894                 # what the merge generated.
1895                 cp -f left expect &&
1896                 test_must_fail git merge-file --diff3                      \
1897                         -L "HEAD"  -L "merged common ancestors"  -L "R3^0" \
1898                         expect     vmb                           right     &&
1899                 test_cmp expect content
1900         )
1901 '
1902
1903 test_done