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