Merge branch 'jk/sideband-more-error-checking'
[git] / t / t6423-merge-rename-directories.sh
1 #!/bin/sh
2
3 test_description="recursive merge with directory renames"
4 # includes checking of many corner cases, with a similar methodology to:
5 #   t6042: corner cases with renames but not criss-cross merges
6 #   t6036: corner cases with both renames and criss-cross merges
7 #
8 # The setup for all of them, pictorially, is:
9 #
10 #      A
11 #      o
12 #     / \
13 #  O o   ?
14 #     \ /
15 #      o
16 #      B
17 #
18 # To help make it easier to follow the flow of tests, they have been
19 # divided into sections and each test will start with a quick explanation
20 # of what commits O, A, and B contain.
21 #
22 # Notation:
23 #    z/{b,c}   means  files z/b and z/c both exist
24 #    x/d_1     means  file x/d exists with content d1.  (Purpose of the
25 #                     underscore notation is to differentiate different
26 #                     files that might be renamed into each other's paths.)
27
28 . ./test-lib.sh
29
30
31 ###########################################################################
32 # SECTION 1: Basic cases we should be able to handle
33 ###########################################################################
34
35 # Testcase 1a, Basic directory rename.
36 #   Commit O: z/{b,c}
37 #   Commit A: y/{b,c}
38 #   Commit B: z/{b,c,d,e/f}
39 #   Expected: y/{b,c,d,e/f}
40
41 test_setup_1a () {
42         test_create_repo 1a &&
43         (
44                 cd 1a &&
45
46                 mkdir z &&
47                 echo b >z/b &&
48                 echo c >z/c &&
49                 git add z &&
50                 test_tick &&
51                 git commit -m "O" &&
52
53                 git branch O &&
54                 git branch A &&
55                 git branch B &&
56
57                 git checkout A &&
58                 git mv z y &&
59                 test_tick &&
60                 git commit -m "A" &&
61
62                 git checkout B &&
63                 echo d >z/d &&
64                 mkdir z/e &&
65                 echo f >z/e/f &&
66                 git add z/d z/e/f &&
67                 test_tick &&
68                 git commit -m "B"
69         )
70 }
71
72 test_expect_success '1a: Simple directory rename detection' '
73         test_setup_1a &&
74         (
75                 cd 1a &&
76
77                 git checkout A^0 &&
78
79                 git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
80
81                 git ls-files -s >out &&
82                 test_line_count = 4 out &&
83
84                 git rev-parse >actual \
85                         HEAD:y/b HEAD:y/c HEAD:y/d HEAD:y/e/f &&
86                 git rev-parse >expect \
87                         O:z/b    O:z/c    B:z/d    B:z/e/f &&
88                 test_cmp expect actual &&
89
90                 git hash-object y/d >actual &&
91                 git rev-parse B:z/d >expect &&
92                 test_cmp expect actual &&
93
94                 test_must_fail git rev-parse HEAD:z/d &&
95                 test_must_fail git rev-parse HEAD:z/e/f &&
96                 test_path_is_missing z/d &&
97                 test_path_is_missing z/e/f
98         )
99 '
100
101 # Testcase 1b, Merge a directory with another
102 #   Commit O: z/{b,c},   y/d
103 #   Commit A: z/{b,c,e}, y/d
104 #   Commit B: y/{b,c,d}
105 #   Expected: y/{b,c,d,e}
106
107 test_setup_1b () {
108         test_create_repo 1b &&
109         (
110                 cd 1b &&
111
112                 mkdir z &&
113                 echo b >z/b &&
114                 echo c >z/c &&
115                 mkdir y &&
116                 echo d >y/d &&
117                 git add z y &&
118                 test_tick &&
119                 git commit -m "O" &&
120
121                 git branch O &&
122                 git branch A &&
123                 git branch B &&
124
125                 git checkout A &&
126                 echo e >z/e &&
127                 git add z/e &&
128                 test_tick &&
129                 git commit -m "A" &&
130
131                 git checkout B &&
132                 git mv z/b y &&
133                 git mv z/c y &&
134                 rmdir z &&
135                 test_tick &&
136                 git commit -m "B"
137         )
138 }
139
140 test_expect_success '1b: Merge a directory with another' '
141         test_setup_1b &&
142         (
143                 cd 1b &&
144
145                 git checkout A^0 &&
146
147                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
148
149                 git ls-files -s >out &&
150                 test_line_count = 4 out &&
151
152                 git rev-parse >actual \
153                         HEAD:y/b HEAD:y/c HEAD:y/d HEAD:y/e &&
154                 git rev-parse >expect \
155                         O:z/b    O:z/c    O:y/d    A:z/e &&
156                 test_cmp expect actual &&
157                 test_must_fail git rev-parse HEAD:z/e
158         )
159 '
160
161 # Testcase 1c, Transitive renaming
162 #   (Related to testcases 3a and 6d -- when should a transitive rename apply?)
163 #   (Related to testcases 9c and 9d -- can transitivity repeat?)
164 #   (Related to testcase 12b -- joint-transitivity?)
165 #   Commit O: z/{b,c},   x/d
166 #   Commit A: y/{b,c},   x/d
167 #   Commit B: z/{b,c,d}
168 #   Expected: y/{b,c,d}  (because x/d -> z/d -> y/d)
169
170 test_setup_1c () {
171         test_create_repo 1c &&
172         (
173                 cd 1c &&
174
175                 mkdir z &&
176                 echo b >z/b &&
177                 echo c >z/c &&
178                 mkdir x &&
179                 echo d >x/d &&
180                 git add z x &&
181                 test_tick &&
182                 git commit -m "O" &&
183
184                 git branch O &&
185                 git branch A &&
186                 git branch B &&
187
188                 git checkout A &&
189                 git mv z y &&
190                 test_tick &&
191                 git commit -m "A" &&
192
193                 git checkout B &&
194                 git mv x/d z/d &&
195                 test_tick &&
196                 git commit -m "B"
197         )
198 }
199
200 test_expect_success '1c: Transitive renaming' '
201         test_setup_1c &&
202         (
203                 cd 1c &&
204
205                 git checkout A^0 &&
206
207                 git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
208
209                 git ls-files -s >out &&
210                 test_line_count = 3 out &&
211
212                 git rev-parse >actual \
213                         HEAD:y/b HEAD:y/c HEAD:y/d &&
214                 git rev-parse >expect \
215                         O:z/b    O:z/c    O:x/d &&
216                 test_cmp expect actual &&
217                 test_must_fail git rev-parse HEAD:x/d &&
218                 test_must_fail git rev-parse HEAD:z/d &&
219                 test_path_is_missing z/d
220         )
221 '
222
223 # Testcase 1d, Directory renames (merging two directories into one new one)
224 #              cause a rename/rename(2to1) conflict
225 #   (Related to testcases 1c and 7b)
226 #   Commit O. z/{b,c},        y/{d,e}
227 #   Commit A. x/{b,c},        y/{d,e,m,wham_1}
228 #   Commit B. z/{b,c,n,wham_2}, x/{d,e}
229 #   Expected: x/{b,c,d,e,m,n}, CONFLICT:(y/wham_1 & z/wham_2 -> x/wham)
230 #   Note: y/m & z/n should definitely move into x.  By the same token, both
231 #         y/wham_1 & z/wham_2 should too...giving us a conflict.
232
233 test_setup_1d () {
234         test_create_repo 1d &&
235         (
236                 cd 1d &&
237
238                 mkdir z &&
239                 echo b >z/b &&
240                 echo c >z/c &&
241                 mkdir y &&
242                 echo d >y/d &&
243                 echo e >y/e &&
244                 git add z y &&
245                 test_tick &&
246                 git commit -m "O" &&
247
248                 git branch O &&
249                 git branch A &&
250                 git branch B &&
251
252                 git checkout A &&
253                 git mv z x &&
254                 echo m >y/m &&
255                 echo wham1 >y/wham &&
256                 git add y &&
257                 test_tick &&
258                 git commit -m "A" &&
259
260                 git checkout B &&
261                 git mv y x &&
262                 echo n >z/n &&
263                 echo wham2 >z/wham &&
264                 git add z &&
265                 test_tick &&
266                 git commit -m "B"
267         )
268 }
269
270 test_expect_success '1d: Directory renames cause a rename/rename(2to1) conflict' '
271         test_setup_1d &&
272         (
273                 cd 1d &&
274
275                 git checkout A^0 &&
276
277                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
278                 test_i18ngrep "CONFLICT (\(.*\)/\1)" out &&
279
280                 git ls-files -s >out &&
281                 test_line_count = 8 out &&
282                 git ls-files -u >out &&
283                 test_line_count = 2 out &&
284                 git ls-files -o >out &&
285                 test_line_count = 1 out &&
286
287                 git rev-parse >actual \
288                         :0:x/b :0:x/c :0:x/d :0:x/e :0:x/m :0:x/n &&
289                 git rev-parse >expect \
290                          O:z/b  O:z/c  O:y/d  O:y/e  A:y/m  B:z/n &&
291                 test_cmp expect actual &&
292
293                 test_must_fail git rev-parse :0:x/wham &&
294                 git rev-parse >actual \
295                         :2:x/wham :3:x/wham &&
296                 git rev-parse >expect \
297                          A:y/wham  B:z/wham &&
298                 test_cmp expect actual &&
299
300                 # Test that the two-way merge in x/wham is as expected
301                 git cat-file -p :2:x/wham >expect &&
302                 git cat-file -p :3:x/wham >other &&
303                 >empty &&
304                 test_must_fail git merge-file \
305                         -L "HEAD" \
306                         -L "" \
307                         -L "B^0" \
308                         expect empty other &&
309                 test_cmp expect x/wham
310         )
311 '
312
313 # Testcase 1e, Renamed directory, with all filenames being renamed too
314 #   (Related to testcases 9f & 9g)
315 #   Commit O: z/{oldb,oldc}
316 #   Commit A: y/{newb,newc}
317 #   Commit B: z/{oldb,oldc,d}
318 #   Expected: y/{newb,newc,d}
319
320 test_setup_1e () {
321         test_create_repo 1e &&
322         (
323                 cd 1e &&
324
325                 mkdir z &&
326                 echo b >z/oldb &&
327                 echo c >z/oldc &&
328                 git add z &&
329                 test_tick &&
330                 git commit -m "O" &&
331
332                 git branch O &&
333                 git branch A &&
334                 git branch B &&
335
336                 git checkout A &&
337                 mkdir y &&
338                 git mv z/oldb y/newb &&
339                 git mv z/oldc y/newc &&
340                 test_tick &&
341                 git commit -m "A" &&
342
343                 git checkout B &&
344                 echo d >z/d &&
345                 git add z/d &&
346                 test_tick &&
347                 git commit -m "B"
348         )
349 }
350
351 test_expect_success '1e: Renamed directory, with all files being renamed too' '
352         test_setup_1e &&
353         (
354                 cd 1e &&
355
356                 git checkout A^0 &&
357
358                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
359
360                 git ls-files -s >out &&
361                 test_line_count = 3 out &&
362
363                 git rev-parse >actual \
364                         HEAD:y/newb HEAD:y/newc HEAD:y/d &&
365                 git rev-parse >expect \
366                         O:z/oldb    O:z/oldc    B:z/d &&
367                 test_cmp expect actual &&
368                 test_must_fail git rev-parse HEAD:z/d
369         )
370 '
371
372 # Testcase 1f, Split a directory into two other directories
373 #   (Related to testcases 3a, all of section 2, and all of section 4)
374 #   Commit O: z/{b,c,d,e,f}
375 #   Commit A: z/{b,c,d,e,f,g}
376 #   Commit B: y/{b,c}, x/{d,e,f}
377 #   Expected: y/{b,c}, x/{d,e,f,g}
378
379 test_setup_1f () {
380         test_create_repo 1f &&
381         (
382                 cd 1f &&
383
384                 mkdir z &&
385                 echo b >z/b &&
386                 echo c >z/c &&
387                 echo d >z/d &&
388                 echo e >z/e &&
389                 echo f >z/f &&
390                 git add z &&
391                 test_tick &&
392                 git commit -m "O" &&
393
394                 git branch O &&
395                 git branch A &&
396                 git branch B &&
397
398                 git checkout A &&
399                 echo g >z/g &&
400                 git add z/g &&
401                 test_tick &&
402                 git commit -m "A" &&
403
404                 git checkout B &&
405                 mkdir y &&
406                 mkdir x &&
407                 git mv z/b y/ &&
408                 git mv z/c y/ &&
409                 git mv z/d x/ &&
410                 git mv z/e x/ &&
411                 git mv z/f x/ &&
412                 rmdir z &&
413                 test_tick &&
414                 git commit -m "B"
415         )
416 }
417
418 test_expect_success '1f: Split a directory into two other directories' '
419         test_setup_1f &&
420         (
421                 cd 1f &&
422
423                 git checkout A^0 &&
424
425                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
426
427                 git ls-files -s >out &&
428                 test_line_count = 6 out &&
429
430                 git rev-parse >actual \
431                         HEAD:y/b HEAD:y/c HEAD:x/d HEAD:x/e HEAD:x/f HEAD:x/g &&
432                 git rev-parse >expect \
433                         O:z/b    O:z/c    O:z/d    O:z/e    O:z/f    A:z/g &&
434                 test_cmp expect actual &&
435                 test_path_is_missing z/g &&
436                 test_must_fail git rev-parse HEAD:z/g
437         )
438 '
439
440 ###########################################################################
441 # Rules suggested by testcases in section 1:
442 #
443 #   We should still detect the directory rename even if it wasn't just
444 #   the directory renamed, but the files within it. (see 1b)
445 #
446 #   If renames split a directory into two or more others, the directory
447 #   with the most renames, "wins" (see 1c).  However, see the testcases
448 #   in section 2, plus testcases 3a and 4a.
449 ###########################################################################
450
451
452 ###########################################################################
453 # SECTION 2: Split into multiple directories, with equal number of paths
454 #
455 # Explore the splitting-a-directory rules a bit; what happens in the
456 # edge cases?
457 #
458 # Note that there is a closely related case of a directory not being
459 # split on either side of history, but being renamed differently on
460 # each side.  See testcase 8e for that.
461 ###########################################################################
462
463 # Testcase 2a, Directory split into two on one side, with equal numbers of paths
464 #   Commit O: z/{b,c}
465 #   Commit A: y/b, w/c
466 #   Commit B: z/{b,c,d}
467 #   Expected: y/b, w/c, z/d, with warning about z/ -> (y/ vs. w/) conflict
468 test_setup_2a () {
469         test_create_repo 2a &&
470         (
471                 cd 2a &&
472
473                 mkdir z &&
474                 echo b >z/b &&
475                 echo c >z/c &&
476                 git add z &&
477                 test_tick &&
478                 git commit -m "O" &&
479
480                 git branch O &&
481                 git branch A &&
482                 git branch B &&
483
484                 git checkout A &&
485                 mkdir y &&
486                 mkdir w &&
487                 git mv z/b y/ &&
488                 git mv z/c w/ &&
489                 test_tick &&
490                 git commit -m "A" &&
491
492                 git checkout B &&
493                 echo d >z/d &&
494                 git add z/d &&
495                 test_tick &&
496                 git commit -m "B"
497         )
498 }
499
500 test_expect_success '2a: Directory split into two on one side, with equal numbers of paths' '
501         test_setup_2a &&
502         (
503                 cd 2a &&
504
505                 git checkout A^0 &&
506
507                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
508                 test_i18ngrep "CONFLICT.*directory rename split" out &&
509
510                 git ls-files -s >out &&
511                 test_line_count = 3 out &&
512                 git ls-files -u >out &&
513                 test_line_count = 0 out &&
514                 git ls-files -o >out &&
515                 test_line_count = 1 out &&
516
517                 git rev-parse >actual \
518                         :0:y/b :0:w/c :0:z/d &&
519                 git rev-parse >expect \
520                          O:z/b  O:z/c  B:z/d &&
521                 test_cmp expect actual
522         )
523 '
524
525 # Testcase 2b, Directory split into two on one side, with equal numbers of paths
526 #   Commit O: z/{b,c}
527 #   Commit A: y/b, w/c
528 #   Commit B: z/{b,c}, x/d
529 #   Expected: y/b, w/c, x/d; No warning about z/ -> (y/ vs. w/) conflict
530 test_setup_2b () {
531         test_create_repo 2b &&
532         (
533                 cd 2b &&
534
535                 mkdir z &&
536                 echo b >z/b &&
537                 echo c >z/c &&
538                 git add z &&
539                 test_tick &&
540                 git commit -m "O" &&
541
542                 git branch O &&
543                 git branch A &&
544                 git branch B &&
545
546                 git checkout A &&
547                 mkdir y &&
548                 mkdir w &&
549                 git mv z/b y/ &&
550                 git mv z/c w/ &&
551                 test_tick &&
552                 git commit -m "A" &&
553
554                 git checkout B &&
555                 mkdir x &&
556                 echo d >x/d &&
557                 git add x/d &&
558                 test_tick &&
559                 git commit -m "B"
560         )
561 }
562
563 test_expect_success '2b: Directory split into two on one side, with equal numbers of paths' '
564         test_setup_2b &&
565         (
566                 cd 2b &&
567
568                 git checkout A^0 &&
569
570                 git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
571
572                 git ls-files -s >out &&
573                 test_line_count = 3 out &&
574                 git ls-files -u >out &&
575                 test_line_count = 0 out &&
576                 git ls-files -o >out &&
577                 test_line_count = 1 out &&
578
579                 git rev-parse >actual \
580                         :0:y/b :0:w/c :0:x/d &&
581                 git rev-parse >expect \
582                          O:z/b  O:z/c  B:x/d &&
583                 test_cmp expect actual &&
584                 test_i18ngrep ! "CONFLICT.*directory rename split" out
585         )
586 '
587
588 ###########################################################################
589 # Rules suggested by section 2:
590 #
591 #   None; the rule was already covered in section 1.  These testcases are
592 #   here just to make sure the conflict resolution and necessary warning
593 #   messages are handled correctly.
594 ###########################################################################
595
596
597 ###########################################################################
598 # SECTION 3: Path in question is the source path for some rename already
599 #
600 # Combining cases from Section 1 and trying to handle them could lead to
601 # directory renaming detection being over-applied.  So, this section
602 # provides some good testcases to check that the implementation doesn't go
603 # too far.
604 ###########################################################################
605
606 # Testcase 3a, Avoid implicit rename if involved as source on other side
607 #   (Related to testcases 1c, 1f, and 9h)
608 #   Commit O: z/{b,c,d}
609 #   Commit A: z/{b,c,d} (no change)
610 #   Commit B: y/{b,c}, x/d
611 #   Expected: y/{b,c}, x/d
612 test_setup_3a () {
613         test_create_repo 3a &&
614         (
615                 cd 3a &&
616
617                 mkdir z &&
618                 echo b >z/b &&
619                 echo c >z/c &&
620                 echo d >z/d &&
621                 git add z &&
622                 test_tick &&
623                 git commit -m "O" &&
624
625                 git branch O &&
626                 git branch A &&
627                 git branch B &&
628
629                 git checkout A &&
630                 test_tick &&
631                 git commit --allow-empty -m "A" &&
632
633                 git checkout B &&
634                 mkdir y &&
635                 mkdir x &&
636                 git mv z/b y/ &&
637                 git mv z/c y/ &&
638                 git mv z/d x/ &&
639                 rmdir z &&
640                 test_tick &&
641                 git commit -m "B"
642         )
643 }
644
645 test_expect_success '3a: Avoid implicit rename if involved as source on other side' '
646         test_setup_3a &&
647         (
648                 cd 3a &&
649
650                 git checkout A^0 &&
651
652                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
653
654                 git ls-files -s >out &&
655                 test_line_count = 3 out &&
656
657                 git rev-parse >actual \
658                         HEAD:y/b HEAD:y/c HEAD:x/d &&
659                 git rev-parse >expect \
660                         O:z/b    O:z/c    O:z/d &&
661                 test_cmp expect actual
662         )
663 '
664
665 # Testcase 3b, Avoid implicit rename if involved as source on other side
666 #   (Related to testcases 5c and 7c, also kind of 1e and 1f)
667 #   Commit O: z/{b,c,d}
668 #   Commit A: y/{b,c}, x/d
669 #   Commit B: z/{b,c}, w/d
670 #   Expected: y/{b,c}, CONFLICT:(z/d -> x/d vs. w/d)
671 #   NOTE: We're particularly checking that since z/d is already involved as
672 #         a source in a file rename on the same side of history, that we don't
673 #         get it involved in directory rename detection.  If it were, we might
674 #         end up with CONFLICT:(z/d -> y/d vs. x/d vs. w/d), i.e. a
675 #         rename/rename/rename(1to3) conflict, which is just weird.
676 test_setup_3b () {
677         test_create_repo 3b &&
678         (
679                 cd 3b &&
680
681                 mkdir z &&
682                 echo b >z/b &&
683                 echo c >z/c &&
684                 echo d >z/d &&
685                 git add z &&
686                 test_tick &&
687                 git commit -m "O" &&
688
689                 git branch O &&
690                 git branch A &&
691                 git branch B &&
692
693                 git checkout A &&
694                 mkdir y &&
695                 mkdir x &&
696                 git mv z/b y/ &&
697                 git mv z/c y/ &&
698                 git mv z/d x/ &&
699                 rmdir z &&
700                 test_tick &&
701                 git commit -m "A" &&
702
703                 git checkout B &&
704                 mkdir w &&
705                 git mv z/d w/ &&
706                 test_tick &&
707                 git commit -m "B"
708         )
709 }
710
711 test_expect_success '3b: Avoid implicit rename if involved as source on current side' '
712         test_setup_3b &&
713         (
714                 cd 3b &&
715
716                 git checkout A^0 &&
717
718                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
719                 test_i18ngrep CONFLICT.*rename/rename.*z/d.*x/d.*w/d out &&
720                 test_i18ngrep ! CONFLICT.*rename/rename.*y/d out &&
721
722                 git ls-files -s >out &&
723                 test_line_count = 5 out &&
724                 git ls-files -u >out &&
725                 test_line_count = 3 out &&
726                 git ls-files -o >out &&
727                 test_line_count = 1 out &&
728
729                 git rev-parse >actual \
730                         :0:y/b :0:y/c :1:z/d :2:x/d :3:w/d &&
731                 git rev-parse >expect \
732                          O:z/b  O:z/c  O:z/d  O:z/d  O:z/d &&
733                 test_cmp expect actual &&
734
735                 test_path_is_missing z/d &&
736                 git hash-object >actual \
737                         x/d   w/d &&
738                 git rev-parse >expect \
739                         O:z/d O:z/d &&
740                 test_cmp expect actual
741         )
742 '
743
744 ###########################################################################
745 # Rules suggested by section 3:
746 #
747 #   Avoid directory-rename-detection for a path, if that path is the source
748 #   of a rename on either side of a merge.
749 ###########################################################################
750
751
752 ###########################################################################
753 # SECTION 4: Partially renamed directory; still exists on both sides of merge
754 #
755 # What if we were to attempt to do directory rename detection when someone
756 # "mostly" moved a directory but still left some files around, or,
757 # equivalently, fully renamed a directory in one commit and then recreated
758 # that directory in a later commit adding some new files and then tried to
759 # merge?
760 #
761 # It's hard to divine user intent in these cases, because you can make an
762 # argument that, depending on the intermediate history of the side being
763 # merged, that some users will want files in that directory to
764 # automatically be detected and renamed, while users with a different
765 # intermediate history wouldn't want that rename to happen.
766 #
767 # I think that it is best to simply not have directory rename detection
768 # apply to such cases.  My reasoning for this is four-fold: (1) it's
769 # easiest for users in general to figure out what happened if we don't
770 # apply directory rename detection in any such case, (2) it's an easy rule
771 # to explain ["We don't do directory rename detection if the directory
772 # still exists on both sides of the merge"], (3) we can get some hairy
773 # edge/corner cases that would be really confusing and possibly not even
774 # representable in the index if we were to even try, and [related to 3] (4)
775 # attempting to resolve this issue of divining user intent by examining
776 # intermediate history goes against the spirit of three-way merges and is a
777 # path towards crazy corner cases that are far more complex than what we're
778 # already dealing with.
779 #
780 # Note that the wording of the rule ("We don't do directory rename
781 # detection if the directory still exists on both sides of the merge.")
782 # also excludes "renaming" of a directory into a subdirectory of itself
783 # (e.g. /some/dir/* -> /some/dir/subdir/*).  It may be possible to carve
784 # out an exception for "renaming"-beneath-itself cases without opening
785 # weird edge/corner cases for other partial directory renames, but for now
786 # we are keeping the rule simple.
787 #
788 # This section contains a test for a partially-renamed-directory case.
789 ###########################################################################
790
791 # Testcase 4a, Directory split, with original directory still present
792 #   (Related to testcase 1f)
793 #   Commit O: z/{b,c,d,e}
794 #   Commit A: y/{b,c,d}, z/e
795 #   Commit B: z/{b,c,d,e,f}
796 #   Expected: y/{b,c,d}, z/{e,f}
797 #   NOTE: Even though most files from z moved to y, we don't want f to follow.
798
799 test_setup_4a () {
800         test_create_repo 4a &&
801         (
802                 cd 4a &&
803
804                 mkdir z &&
805                 echo b >z/b &&
806                 echo c >z/c &&
807                 echo d >z/d &&
808                 echo e >z/e &&
809                 git add z &&
810                 test_tick &&
811                 git commit -m "O" &&
812
813                 git branch O &&
814                 git branch A &&
815                 git branch B &&
816
817                 git checkout A &&
818                 mkdir y &&
819                 git mv z/b y/ &&
820                 git mv z/c y/ &&
821                 git mv z/d y/ &&
822                 test_tick &&
823                 git commit -m "A" &&
824
825                 git checkout B &&
826                 echo f >z/f &&
827                 git add z/f &&
828                 test_tick &&
829                 git commit -m "B"
830         )
831 }
832
833 test_expect_success '4a: Directory split, with original directory still present' '
834         test_setup_4a &&
835         (
836                 cd 4a &&
837
838                 git checkout A^0 &&
839
840                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
841
842                 git ls-files -s >out &&
843                 test_line_count = 5 out &&
844                 git ls-files -u >out &&
845                 test_line_count = 0 out &&
846                 git ls-files -o >out &&
847                 test_line_count = 1 out &&
848
849                 git rev-parse >actual \
850                         HEAD:y/b HEAD:y/c HEAD:y/d HEAD:z/e HEAD:z/f &&
851                 git rev-parse >expect \
852                         O:z/b    O:z/c    O:z/d    O:z/e    B:z/f &&
853                 test_cmp expect actual
854         )
855 '
856
857 ###########################################################################
858 # Rules suggested by section 4:
859 #
860 #   Directory-rename-detection should be turned off for any directories (as
861 #   a source for renames) that exist on both sides of the merge.  (The "as
862 #   a source for renames" clarification is due to cases like 1c where
863 #   the target directory exists on both sides and we do want the rename
864 #   detection.)  But, sadly, see testcase 8b.
865 ###########################################################################
866
867
868 ###########################################################################
869 # SECTION 5: Files/directories in the way of subset of to-be-renamed paths
870 #
871 # Implicitly renaming files due to a detected directory rename could run
872 # into problems if there are files or directories in the way of the paths
873 # we want to rename.  Explore such cases in this section.
874 ###########################################################################
875
876 # Testcase 5a, Merge directories, other side adds files to original and target
877 #   Commit O: z/{b,c},       y/d
878 #   Commit A: z/{b,c,e_1,f}, y/{d,e_2}
879 #   Commit B: y/{b,c,d}
880 #   Expected: z/e_1, y/{b,c,d,e_2,f} + CONFLICT warning
881 #   NOTE: While directory rename detection is active here causing z/f to
882 #         become y/f, we did not apply this for z/e_1 because that would
883 #         give us an add/add conflict for y/e_1 vs y/e_2.  This problem with
884 #         this add/add, is that both versions of y/e are from the same side
885 #         of history, giving us no way to represent this conflict in the
886 #         index.
887
888 test_setup_5a () {
889         test_create_repo 5a &&
890         (
891                 cd 5a &&
892
893                 mkdir z &&
894                 echo b >z/b &&
895                 echo c >z/c &&
896                 mkdir y &&
897                 echo d >y/d &&
898                 git add z y &&
899                 test_tick &&
900                 git commit -m "O" &&
901
902                 git branch O &&
903                 git branch A &&
904                 git branch B &&
905
906                 git checkout A &&
907                 echo e1 >z/e &&
908                 echo f >z/f &&
909                 echo e2 >y/e &&
910                 git add z/e z/f y/e &&
911                 test_tick &&
912                 git commit -m "A" &&
913
914                 git checkout B &&
915                 git mv z/b y/ &&
916                 git mv z/c y/ &&
917                 rmdir z &&
918                 test_tick &&
919                 git commit -m "B"
920         )
921 }
922
923 test_expect_success '5a: Merge directories, other side adds files to original and target' '
924         test_setup_5a &&
925         (
926                 cd 5a &&
927
928                 git checkout A^0 &&
929
930                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
931                 test_i18ngrep "CONFLICT.*implicit dir rename" out &&
932
933                 git ls-files -s >out &&
934                 test_line_count = 6 out &&
935                 git ls-files -u >out &&
936                 test_line_count = 0 out &&
937                 git ls-files -o >out &&
938                 test_line_count = 1 out &&
939
940                 git rev-parse >actual \
941                         :0:y/b :0:y/c :0:y/d :0:y/e :0:z/e :0:y/f &&
942                 git rev-parse >expect \
943                          O:z/b  O:z/c  O:y/d  A:y/e  A:z/e  A:z/f &&
944                 test_cmp expect actual
945         )
946 '
947
948 # Testcase 5b, Rename/delete in order to get add/add/add conflict
949 #   (Related to testcase 8d; these may appear slightly inconsistent to users;
950 #    Also related to testcases 7d and 7e)
951 #   Commit O: z/{b,c,d_1}
952 #   Commit A: y/{b,c,d_2}
953 #   Commit B: z/{b,c,d_1,e}, y/d_3
954 #   Expected: y/{b,c,e}, CONFLICT(add/add: y/d_2 vs. y/d_3)
955 #   NOTE: If z/d_1 in commit B were to be involved in dir rename detection, as
956 #         we normally would since z/ is being renamed to y/, then this would be
957 #         a rename/delete (z/d_1 -> y/d_1 vs. deleted) AND an add/add/add
958 #         conflict of y/d_1 vs. y/d_2 vs. y/d_3.  Add/add/add is not
959 #         representable in the index, so the existence of y/d_3 needs to
960 #         cause us to bail on directory rename detection for that path, falling
961 #         back to git behavior without the directory rename detection.
962
963 test_setup_5b () {
964         test_create_repo 5b &&
965         (
966                 cd 5b &&
967
968                 mkdir z &&
969                 echo b >z/b &&
970                 echo c >z/c &&
971                 echo d1 >z/d &&
972                 git add z &&
973                 test_tick &&
974                 git commit -m "O" &&
975
976                 git branch O &&
977                 git branch A &&
978                 git branch B &&
979
980                 git checkout A &&
981                 git rm z/d &&
982                 git mv z y &&
983                 echo d2 >y/d &&
984                 git add y/d &&
985                 test_tick &&
986                 git commit -m "A" &&
987
988                 git checkout B &&
989                 mkdir y &&
990                 echo d3 >y/d &&
991                 echo e >z/e &&
992                 git add y/d z/e &&
993                 test_tick &&
994                 git commit -m "B"
995         )
996 }
997
998 test_expect_success '5b: Rename/delete in order to get add/add/add conflict' '
999         test_setup_5b &&
1000         (
1001                 cd 5b &&
1002
1003                 git checkout A^0 &&
1004
1005                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1006                 test_i18ngrep "CONFLICT (add/add).* y/d" out &&
1007
1008                 git ls-files -s >out &&
1009                 test_line_count = 5 out &&
1010                 git ls-files -u >out &&
1011                 test_line_count = 2 out &&
1012                 git ls-files -o >out &&
1013                 test_line_count = 1 out &&
1014
1015                 git rev-parse >actual \
1016                         :0:y/b :0:y/c :0:y/e :2:y/d :3:y/d &&
1017                 git rev-parse >expect \
1018                          O:z/b  O:z/c  B:z/e  A:y/d  B:y/d &&
1019                 test_cmp expect actual &&
1020
1021                 test_must_fail git rev-parse :1:y/d &&
1022                 test_path_is_file y/d
1023         )
1024 '
1025
1026 # Testcase 5c, Transitive rename would cause rename/rename/rename/add/add/add
1027 #   (Directory rename detection would result in transitive rename vs.
1028 #    rename/rename(1to2) and turn it into a rename/rename(1to3).  Further,
1029 #    rename paths conflict with separate adds on the other side)
1030 #   (Related to testcases 3b and 7c)
1031 #   Commit O: z/{b,c}, x/d_1
1032 #   Commit A: y/{b,c,d_2}, w/d_1
1033 #   Commit B: z/{b,c,d_1,e}, w/d_3, y/d_4
1034 #   Expected: A mess, but only a rename/rename(1to2)/add/add mess.  Use the
1035 #             presence of y/d_4 in B to avoid doing transitive rename of
1036 #             x/d_1 -> z/d_1 -> y/d_1, so that the only paths we have at
1037 #             y/d are y/d_2 and y/d_4.  We still do the move from z/e to y/e,
1038 #             though, because it doesn't have anything in the way.
1039
1040 test_setup_5c () {
1041         test_create_repo 5c &&
1042         (
1043                 cd 5c &&
1044
1045                 mkdir z &&
1046                 echo b >z/b &&
1047                 echo c >z/c &&
1048                 mkdir x &&
1049                 echo d1 >x/d &&
1050                 git add z x &&
1051                 test_tick &&
1052                 git commit -m "O" &&
1053
1054                 git branch O &&
1055                 git branch A &&
1056                 git branch B &&
1057
1058                 git checkout A &&
1059                 git mv z y &&
1060                 echo d2 >y/d &&
1061                 git add y/d &&
1062                 git mv x w &&
1063                 test_tick &&
1064                 git commit -m "A" &&
1065
1066                 git checkout B &&
1067                 git mv x/d z/ &&
1068                 mkdir w &&
1069                 mkdir y &&
1070                 echo d3 >w/d &&
1071                 echo d4 >y/d &&
1072                 echo e >z/e &&
1073                 git add w/ y/ z/e &&
1074                 test_tick &&
1075                 git commit -m "B"
1076         )
1077 }
1078
1079 test_expect_success '5c: Transitive rename would cause rename/rename/rename/add/add/add' '
1080         test_setup_5c &&
1081         (
1082                 cd 5c &&
1083
1084                 git checkout A^0 &&
1085
1086                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1087                 test_i18ngrep "CONFLICT (rename/rename).*x/d.*w/d.*z/d" out &&
1088                 test_i18ngrep "CONFLICT (add/add).* y/d" out &&
1089
1090                 git ls-files -s >out &&
1091                 test_line_count = 9 out &&
1092                 git ls-files -u >out &&
1093                 test_line_count = 6 out &&
1094                 git ls-files -o >out &&
1095                 test_line_count = 1 out &&
1096
1097                 git rev-parse >actual \
1098                         :0:y/b :0:y/c :0:y/e &&
1099                 git rev-parse >expect \
1100                          O:z/b  O:z/c  B:z/e &&
1101                 test_cmp expect actual &&
1102
1103                 test_must_fail git rev-parse :1:y/d &&
1104                 git rev-parse >actual \
1105                         :2:w/d :3:w/d :1:x/d :2:y/d :3:y/d :3:z/d &&
1106                 git rev-parse >expect \
1107                          O:x/d  B:w/d  O:x/d  A:y/d  B:y/d  O:x/d &&
1108                 test_cmp expect actual &&
1109
1110                 git hash-object >actual \
1111                         z/d &&
1112                 git rev-parse >expect \
1113                         O:x/d &&
1114                 test_cmp expect actual &&
1115                 test_path_is_missing x/d &&
1116                 test_path_is_file y/d &&
1117                 grep -q "<<<<" y/d  # conflict markers should be present
1118         )
1119 '
1120
1121 # Testcase 5d, Directory/file/file conflict due to directory rename
1122 #   Commit O: z/{b,c}
1123 #   Commit A: y/{b,c,d_1}
1124 #   Commit B: z/{b,c,d_2,f}, y/d/e
1125 #   Expected: y/{b,c,d/e,f}, z/d_2, CONFLICT(file/directory), y/d_1~HEAD
1126 #   Note: The fact that y/d/ exists in B makes us bail on directory rename
1127 #         detection for z/d_2, but that doesn't prevent us from applying the
1128 #         directory rename detection for z/f -> y/f.
1129
1130 test_setup_5d () {
1131         test_create_repo 5d &&
1132         (
1133                 cd 5d &&
1134
1135                 mkdir z &&
1136                 echo b >z/b &&
1137                 echo c >z/c &&
1138                 git add z &&
1139                 test_tick &&
1140                 git commit -m "O" &&
1141
1142                 git branch O &&
1143                 git branch A &&
1144                 git branch B &&
1145
1146                 git checkout A &&
1147                 git mv z y &&
1148                 echo d1 >y/d &&
1149                 git add y/d &&
1150                 test_tick &&
1151                 git commit -m "A" &&
1152
1153                 git checkout B &&
1154                 mkdir -p y/d &&
1155                 echo e >y/d/e &&
1156                 echo d2 >z/d &&
1157                 echo f >z/f &&
1158                 git add y/d/e z/d z/f &&
1159                 test_tick &&
1160                 git commit -m "B"
1161         )
1162 }
1163
1164 test_expect_success '5d: Directory/file/file conflict due to directory rename' '
1165         test_setup_5d &&
1166         (
1167                 cd 5d &&
1168
1169                 git checkout A^0 &&
1170
1171                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1172                 test_i18ngrep "CONFLICT (file/directory).*y/d" out &&
1173
1174                 git ls-files -s >out &&
1175                 test_line_count = 6 out &&
1176                 git ls-files -u >out &&
1177                 test_line_count = 1 out &&
1178                 git ls-files -o >out &&
1179                 test_line_count = 2 out &&
1180
1181                 git rev-parse >actual \
1182                         :0:y/b :0:y/c :0:z/d :0:y/f :2:y/d :0:y/d/e &&
1183                 git rev-parse >expect \
1184                          O:z/b  O:z/c  B:z/d  B:z/f  A:y/d  B:y/d/e &&
1185                 test_cmp expect actual &&
1186
1187                 git hash-object y/d~HEAD >actual &&
1188                 git rev-parse A:y/d >expect &&
1189                 test_cmp expect actual
1190         )
1191 '
1192
1193 ###########################################################################
1194 # Rules suggested by section 5:
1195 #
1196 #   If a subset of to-be-renamed files have a file or directory in the way,
1197 #   "turn off" the directory rename for those specific sub-paths, falling
1198 #   back to old handling.  But, sadly, see testcases 8a and 8b.
1199 ###########################################################################
1200
1201
1202 ###########################################################################
1203 # SECTION 6: Same side of the merge was the one that did the rename
1204 #
1205 # It may sound obvious that you only want to apply implicit directory
1206 # renames to directories if the _other_ side of history did the renaming.
1207 # If you did make an implementation that didn't explicitly enforce this
1208 # rule, the majority of cases that would fall under this section would
1209 # also be solved by following the rules from the above sections.  But
1210 # there are still a few that stick out, so this section covers them just
1211 # to make sure we also get them right.
1212 ###########################################################################
1213
1214 # Testcase 6a, Tricky rename/delete
1215 #   Commit O: z/{b,c,d}
1216 #   Commit A: z/b
1217 #   Commit B: y/{b,c}, z/d
1218 #   Expected: y/b, CONFLICT(rename/delete, z/c -> y/c vs. NULL)
1219 #   Note: We're just checking here that the rename of z/b and z/c to put
1220 #         them under y/ doesn't accidentally catch z/d and make it look like
1221 #         it is also involved in a rename/delete conflict.
1222
1223 test_setup_6a () {
1224         test_create_repo 6a &&
1225         (
1226                 cd 6a &&
1227
1228                 mkdir z &&
1229                 echo b >z/b &&
1230                 echo c >z/c &&
1231                 echo d >z/d &&
1232                 git add z &&
1233                 test_tick &&
1234                 git commit -m "O" &&
1235
1236                 git branch O &&
1237                 git branch A &&
1238                 git branch B &&
1239
1240                 git checkout A &&
1241                 git rm z/c &&
1242                 git rm z/d &&
1243                 test_tick &&
1244                 git commit -m "A" &&
1245
1246                 git checkout B &&
1247                 mkdir y &&
1248                 git mv z/b y/ &&
1249                 git mv z/c y/ &&
1250                 test_tick &&
1251                 git commit -m "B"
1252         )
1253 }
1254
1255 test_expect_success '6a: Tricky rename/delete' '
1256         test_setup_6a &&
1257         (
1258                 cd 6a &&
1259
1260                 git checkout A^0 &&
1261
1262                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1263                 test_i18ngrep "CONFLICT (rename/delete).*z/c.*y/c" out &&
1264
1265                 git ls-files -s >out &&
1266                 test_line_count = 2 out &&
1267                 git ls-files -u >out &&
1268                 test_line_count = 1 out &&
1269                 git ls-files -o >out &&
1270                 test_line_count = 1 out &&
1271
1272                 git rev-parse >actual \
1273                         :0:y/b :3:y/c &&
1274                 git rev-parse >expect \
1275                          O:z/b  O:z/c &&
1276                 test_cmp expect actual
1277         )
1278 '
1279
1280 # Testcase 6b1, Same rename done on both sides
1281 #   (Related to testcase 6b2 and 8e)
1282 #   Commit O: z/{b,c,d,e}
1283 #   Commit A: y/{b,c,d}, x/e
1284 #   Commit B: y/{b,c,d}, z/{e,f}
1285 #   Expected: y/{b,c,d,f}, x/e
1286 #   Note: Directory rename detection says A renamed z/ -> y/ (3 paths renamed
1287 #         to y/ and only 1 renamed to x/), therefore the new file 'z/f' in B
1288 #         should be moved to 'y/f'.
1289 #
1290 #         This is a bit of an edge case where any behavior might surprise users,
1291 #         whether that is treating A as renaming z/ -> y/, treating A as renaming
1292 #         z/ -> x/, or treating A as not doing any directory rename.  However, I
1293 #         think this answer is the least confusing and most consistent with the
1294 #         rules elsewhere.
1295 #
1296 #         A note about z/ -> x/, since it may not be clear how that could come
1297 #         about: If we were to ignore files renamed by both sides
1298 #         (i.e. z/{b,c,d}), as directory rename detection did in git-2.18 thru
1299 #         at least git-2.28, then we would note there are no renames from z/ to
1300 #         y/ and one rename from z/ to x/ and thus come to the conclusion that
1301 #         A renamed z/ -> x/.  This seems more confusing for end users than a
1302 #         rename of z/ to y/, it makes directory rename detection behavior
1303 #         harder for them to predict.  As such, we modified the rule, changed
1304 #         the behavior on testcases 6b2 and 8e, and introduced this 6b1 testcase.
1305
1306 test_setup_6b1 () {
1307         test_create_repo 6b1 &&
1308         (
1309                 cd 6b1 &&
1310
1311                 mkdir z &&
1312                 echo b >z/b &&
1313                 echo c >z/c &&
1314                 echo d >z/d &&
1315                 echo e >z/e &&
1316                 git add z &&
1317                 test_tick &&
1318                 git commit -m "O" &&
1319
1320                 git branch O &&
1321                 git branch A &&
1322                 git branch B &&
1323
1324                 git checkout A &&
1325                 git mv z y &&
1326                 mkdir x &&
1327                 git mv y/e x/e &&
1328                 test_tick &&
1329                 git commit -m "A" &&
1330
1331                 git checkout B &&
1332                 git mv z y &&
1333                 mkdir z &&
1334                 git mv y/e z/e &&
1335                 echo f >z/f &&
1336                 git add z/f &&
1337                 test_tick &&
1338                 git commit -m "B"
1339         )
1340 }
1341
1342 test_expect_failure '6b1: Same renames done on both sides, plus another rename' '
1343         test_setup_6b1 &&
1344         (
1345                 cd 6b1 &&
1346
1347                 git checkout A^0 &&
1348
1349                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
1350
1351                 git ls-files -s >out &&
1352                 test_line_count = 5 out &&
1353                 git ls-files -u >out &&
1354                 test_line_count = 0 out &&
1355                 git ls-files -o >out &&
1356                 test_line_count = 1 out &&
1357
1358                 git rev-parse >actual \
1359                         HEAD:y/b HEAD:y/c HEAD:y/d HEAD:x/e HEAD:y/f &&
1360                 git rev-parse >expect \
1361                         O:z/b    O:z/c    O:z/d    O:z/e    B:z/f &&
1362                 test_cmp expect actual
1363         )
1364 '
1365
1366 # Testcase 6b2, Same rename done on both sides
1367 #   (Related to testcases 6c and 8e)
1368 #   Commit O: z/{b,c}
1369 #   Commit A: y/{b,c}
1370 #   Commit B: y/{b,c}, z/d
1371 #   Expected: y/{b,c,d}
1372 #   Alternate: y/{b,c}, z/d
1373 #   Note: Directory rename detection says A renamed z/ -> y/, therefore the new
1374 #         file 'z/d' in B should be moved to 'y/d'.
1375 #
1376 #         We could potentially ignore the renames of z/{b,c} on side A since
1377 #         those were renamed on both sides.  However, it's a bit of a corner
1378 #         case because what if there was also a z/e that side A moved to x/e
1379 #         and side B left alone?  If we used the "ignore renames done on both
1380 #         sides" logic, then we'd compute that A renamed z/ -> x/, and move
1381 #         z/d to x/d.  That seems more surprising and uglier than allowing
1382 #         the z/ -> y/ rename.
1383
1384 test_setup_6b2 () {
1385         test_create_repo 6b2 &&
1386         (
1387                 cd 6b2 &&
1388
1389                 mkdir z &&
1390                 echo b >z/b &&
1391                 echo c >z/c &&
1392                 git add z &&
1393                 test_tick &&
1394                 git commit -m "O" &&
1395
1396                 git branch O &&
1397                 git branch A &&
1398                 git branch B &&
1399
1400                 git checkout A &&
1401                 git mv z y &&
1402                 test_tick &&
1403                 git commit -m "A" &&
1404
1405                 git checkout B &&
1406                 git mv z y &&
1407                 mkdir z &&
1408                 echo d >z/d &&
1409                 git add z/d &&
1410                 test_tick &&
1411                 git commit -m "B"
1412         )
1413 }
1414
1415 test_expect_failure '6b2: Same rename done on both sides' '
1416         test_setup_6b2 &&
1417         (
1418                 cd 6b2 &&
1419
1420                 git checkout A^0 &&
1421
1422                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
1423
1424                 git ls-files -s >out &&
1425                 test_line_count = 3 out &&
1426                 git ls-files -u >out &&
1427                 test_line_count = 0 out &&
1428                 git ls-files -o >out &&
1429                 test_line_count = 1 out &&
1430
1431                 git rev-parse >actual \
1432                         HEAD:y/b HEAD:y/c HEAD:y/d &&
1433                 git rev-parse >expect \
1434                         O:z/b    O:z/c    B:z/d &&
1435                 test_cmp expect actual
1436         )
1437 '
1438
1439 # Testcase 6c, Rename only done on same side
1440 #   (Related to testcases 6b1, 6b2, and 8e)
1441 #   Commit O: z/{b,c}
1442 #   Commit A: z/{b,c} (no change)
1443 #   Commit B: y/{b,c}, z/d
1444 #   Expected: y/{b,c}, z/d
1445 #   NOTE: Seems obvious, but just checking that the implementation doesn't
1446 #         "accidentally detect a rename" and give us y/{b,c,d}.
1447
1448 test_setup_6c () {
1449         test_create_repo 6c &&
1450         (
1451                 cd 6c &&
1452
1453                 mkdir z &&
1454                 echo b >z/b &&
1455                 echo c >z/c &&
1456                 git add z &&
1457                 test_tick &&
1458                 git commit -m "O" &&
1459
1460                 git branch O &&
1461                 git branch A &&
1462                 git branch B &&
1463
1464                 git checkout A &&
1465                 test_tick &&
1466                 git commit --allow-empty -m "A" &&
1467
1468                 git checkout B &&
1469                 git mv z y &&
1470                 mkdir z &&
1471                 echo d >z/d &&
1472                 git add z/d &&
1473                 test_tick &&
1474                 git commit -m "B"
1475         )
1476 }
1477
1478 test_expect_success '6c: Rename only done on same side' '
1479         test_setup_6c &&
1480         (
1481                 cd 6c &&
1482
1483                 git checkout A^0 &&
1484
1485                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
1486
1487                 git ls-files -s >out &&
1488                 test_line_count = 3 out &&
1489                 git ls-files -u >out &&
1490                 test_line_count = 0 out &&
1491                 git ls-files -o >out &&
1492                 test_line_count = 1 out &&
1493
1494                 git rev-parse >actual \
1495                         HEAD:y/b HEAD:y/c HEAD:z/d &&
1496                 git rev-parse >expect \
1497                         O:z/b    O:z/c    B:z/d &&
1498                 test_cmp expect actual
1499         )
1500 '
1501
1502 # Testcase 6d, We don't always want transitive renaming
1503 #   (Related to testcase 1c)
1504 #   Commit O: z/{b,c}, x/d
1505 #   Commit A: z/{b,c}, x/d (no change)
1506 #   Commit B: y/{b,c}, z/d
1507 #   Expected: y/{b,c}, z/d
1508 #   NOTE: Again, this seems obvious but just checking that the implementation
1509 #         doesn't "accidentally detect a rename" and give us y/{b,c,d}.
1510
1511 test_setup_6d () {
1512         test_create_repo 6d &&
1513         (
1514                 cd 6d &&
1515
1516                 mkdir z &&
1517                 echo b >z/b &&
1518                 echo c >z/c &&
1519                 mkdir x &&
1520                 echo d >x/d &&
1521                 git add z x &&
1522                 test_tick &&
1523                 git commit -m "O" &&
1524
1525                 git branch O &&
1526                 git branch A &&
1527                 git branch B &&
1528
1529                 git checkout A &&
1530                 test_tick &&
1531                 git commit --allow-empty -m "A" &&
1532
1533                 git checkout B &&
1534                 git mv z y &&
1535                 git mv x z &&
1536                 test_tick &&
1537                 git commit -m "B"
1538         )
1539 }
1540
1541 test_expect_success '6d: We do not always want transitive renaming' '
1542         test_setup_6d &&
1543         (
1544                 cd 6d &&
1545
1546                 git checkout A^0 &&
1547
1548                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
1549
1550                 git ls-files -s >out &&
1551                 test_line_count = 3 out &&
1552                 git ls-files -u >out &&
1553                 test_line_count = 0 out &&
1554                 git ls-files -o >out &&
1555                 test_line_count = 1 out &&
1556
1557                 git rev-parse >actual \
1558                         HEAD:y/b HEAD:y/c HEAD:z/d &&
1559                 git rev-parse >expect \
1560                         O:z/b    O:z/c    O:x/d &&
1561                 test_cmp expect actual
1562         )
1563 '
1564
1565 # Testcase 6e, Add/add from one-side
1566 #   Commit O: z/{b,c}
1567 #   Commit A: z/{b,c} (no change)
1568 #   Commit B: y/{b,c,d_1}, z/d_2
1569 #   Expected: y/{b,c,d_1}, z/d_2
1570 #   NOTE: Again, this seems obvious but just checking that the implementation
1571 #         doesn't "accidentally detect a rename" and give us y/{b,c} +
1572 #         add/add conflict on y/d_1 vs y/d_2.
1573
1574 test_setup_6e () {
1575         test_create_repo 6e &&
1576         (
1577                 cd 6e &&
1578
1579                 mkdir z &&
1580                 echo b >z/b &&
1581                 echo c >z/c &&
1582                 git add z &&
1583                 test_tick &&
1584                 git commit -m "O" &&
1585
1586                 git branch O &&
1587                 git branch A &&
1588                 git branch B &&
1589
1590                 git checkout A &&
1591                 test_tick &&
1592                 git commit --allow-empty -m "A" &&
1593
1594                 git checkout B &&
1595                 git mv z y &&
1596                 echo d1 > y/d &&
1597                 mkdir z &&
1598                 echo d2 > z/d &&
1599                 git add y/d z/d &&
1600                 test_tick &&
1601                 git commit -m "B"
1602         )
1603 }
1604
1605 test_expect_success '6e: Add/add from one side' '
1606         test_setup_6e &&
1607         (
1608                 cd 6e &&
1609
1610                 git checkout A^0 &&
1611
1612                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
1613
1614                 git ls-files -s >out &&
1615                 test_line_count = 4 out &&
1616                 git ls-files -u >out &&
1617                 test_line_count = 0 out &&
1618                 git ls-files -o >out &&
1619                 test_line_count = 1 out &&
1620
1621                 git rev-parse >actual \
1622                         HEAD:y/b HEAD:y/c HEAD:y/d HEAD:z/d &&
1623                 git rev-parse >expect \
1624                         O:z/b    O:z/c    B:y/d    B:z/d &&
1625                 test_cmp expect actual
1626         )
1627 '
1628
1629 ###########################################################################
1630 # Rules suggested by section 6:
1631 #
1632 #   Only apply implicit directory renames to directories if the other
1633 #   side of history is the one doing the renaming.
1634 ###########################################################################
1635
1636
1637 ###########################################################################
1638 # SECTION 7: More involved Edge/Corner cases
1639 #
1640 # The ruleset we have generated in the above sections seems to provide
1641 # well-defined merges.  But can we find edge/corner cases that either (a)
1642 # are harder for users to understand, or (b) have a resolution that is
1643 # non-intuitive or suboptimal?
1644 #
1645 # The testcases in this section dive into cases that I've tried to craft in
1646 # a way to find some that might be surprising to users or difficult for
1647 # them to understand (the next section will look at non-intuitive or
1648 # suboptimal merge results).  Some of the testcases are similar to ones
1649 # from past sections, but have been simplified to try to highlight error
1650 # messages using a "modified" path (due to the directory rename).  Are
1651 # users okay with these?
1652 #
1653 # In my opinion, testcases that are difficult to understand from this
1654 # section is due to difficulty in the testcase rather than the directory
1655 # renaming (similar to how t6042 and t6036 have difficult resolutions due
1656 # to the problem setup itself being complex).  And I don't think the
1657 # error messages are a problem.
1658 #
1659 # On the other hand, the testcases in section 8 worry me slightly more...
1660 ###########################################################################
1661
1662 # Testcase 7a, rename-dir vs. rename-dir (NOT split evenly) PLUS add-other-file
1663 #   Commit O: z/{b,c}
1664 #   Commit A: y/{b,c}
1665 #   Commit B: w/b, x/c, z/d
1666 #   Expected: y/d, CONFLICT(rename/rename for both z/b and z/c)
1667 #   NOTE: There's a rename of z/ here, y/ has more renames, so z/d -> y/d.
1668
1669 test_setup_7a () {
1670         test_create_repo 7a &&
1671         (
1672                 cd 7a &&
1673
1674                 mkdir z &&
1675                 echo b >z/b &&
1676                 echo c >z/c &&
1677                 git add z &&
1678                 test_tick &&
1679                 git commit -m "O" &&
1680
1681                 git branch O &&
1682                 git branch A &&
1683                 git branch B &&
1684
1685                 git checkout A &&
1686                 git mv z y &&
1687                 test_tick &&
1688                 git commit -m "A" &&
1689
1690                 git checkout B &&
1691                 mkdir w &&
1692                 mkdir x &&
1693                 git mv z/b w/ &&
1694                 git mv z/c x/ &&
1695                 echo d > z/d &&
1696                 git add z/d &&
1697                 test_tick &&
1698                 git commit -m "B"
1699         )
1700 }
1701
1702 test_expect_success '7a: rename-dir vs. rename-dir (NOT split evenly) PLUS add-other-file' '
1703         test_setup_7a &&
1704         (
1705                 cd 7a &&
1706
1707                 git checkout A^0 &&
1708
1709                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1710                 test_i18ngrep "CONFLICT (rename/rename).*z/b.*y/b.*w/b" out &&
1711                 test_i18ngrep "CONFLICT (rename/rename).*z/c.*y/c.*x/c" out &&
1712
1713                 git ls-files -s >out &&
1714                 test_line_count = 7 out &&
1715                 git ls-files -u >out &&
1716                 test_line_count = 6 out &&
1717                 git ls-files -o >out &&
1718                 test_line_count = 1 out &&
1719
1720                 git rev-parse >actual \
1721                         :1:z/b :2:y/b :3:w/b :1:z/c :2:y/c :3:x/c :0:y/d &&
1722                 git rev-parse >expect \
1723                          O:z/b  O:z/b  O:z/b  O:z/c  O:z/c  O:z/c  B:z/d &&
1724                 test_cmp expect actual &&
1725
1726                 git hash-object >actual \
1727                         y/b   w/b   y/c   x/c &&
1728                 git rev-parse >expect \
1729                         O:z/b O:z/b O:z/c O:z/c &&
1730                 test_cmp expect actual
1731         )
1732 '
1733
1734 # Testcase 7b, rename/rename(2to1), but only due to transitive rename
1735 #   (Related to testcase 1d)
1736 #   Commit O: z/{b,c},     x/d_1, w/d_2
1737 #   Commit A: y/{b,c,d_2}, x/d_1
1738 #   Commit B: z/{b,c,d_1},        w/d_2
1739 #   Expected: y/{b,c}, CONFLICT(rename/rename(2to1): x/d_1, w/d_2 -> y_d)
1740
1741 test_setup_7b () {
1742         test_create_repo 7b &&
1743         (
1744                 cd 7b &&
1745
1746                 mkdir z &&
1747                 mkdir x &&
1748                 mkdir w &&
1749                 echo b >z/b &&
1750                 echo c >z/c &&
1751                 echo d1 > x/d &&
1752                 echo d2 > w/d &&
1753                 git add z x w &&
1754                 test_tick &&
1755                 git commit -m "O" &&
1756
1757                 git branch O &&
1758                 git branch A &&
1759                 git branch B &&
1760
1761                 git checkout A &&
1762                 git mv z y &&
1763                 git mv w/d y/ &&
1764                 test_tick &&
1765                 git commit -m "A" &&
1766
1767                 git checkout B &&
1768                 git mv x/d z/ &&
1769                 rmdir x &&
1770                 test_tick &&
1771                 git commit -m "B"
1772         )
1773 }
1774
1775 test_expect_success '7b: rename/rename(2to1), but only due to transitive rename' '
1776         test_setup_7b &&
1777         (
1778                 cd 7b &&
1779
1780                 git checkout A^0 &&
1781
1782                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1783                 test_i18ngrep "CONFLICT (\(.*\)/\1)" out &&
1784
1785                 git ls-files -s >out &&
1786                 test_line_count = 4 out &&
1787                 git ls-files -u >out &&
1788                 test_line_count = 2 out &&
1789                 git ls-files -o >out &&
1790                 test_line_count = 1 out &&
1791
1792                 git rev-parse >actual \
1793                         :0:y/b :0:y/c :2:y/d :3:y/d &&
1794                 git rev-parse >expect \
1795                          O:z/b  O:z/c  O:w/d  O:x/d &&
1796                 test_cmp expect actual &&
1797
1798                 # Test that the two-way merge in y/d is as expected
1799                 git cat-file -p :2:y/d >expect &&
1800                 git cat-file -p :3:y/d >other &&
1801                 >empty &&
1802                 test_must_fail git merge-file \
1803                         -L "HEAD" \
1804                         -L "" \
1805                         -L "B^0" \
1806                         expect empty other &&
1807                 test_cmp expect y/d
1808         )
1809 '
1810
1811 # Testcase 7c, rename/rename(1to...2or3); transitive rename may add complexity
1812 #   (Related to testcases 3b and 5c)
1813 #   Commit O: z/{b,c}, x/d
1814 #   Commit A: y/{b,c}, w/d
1815 #   Commit B: z/{b,c,d}
1816 #   Expected: y/{b,c}, CONFLICT(x/d -> w/d vs. y/d)
1817 #   NOTE: z/ was renamed to y/ so we do want to report
1818 #         neither CONFLICT(x/d -> w/d vs. z/d)
1819 #         nor CONFLiCT x/d -> w/d vs. y/d vs. z/d)
1820
1821 test_setup_7c () {
1822         test_create_repo 7c &&
1823         (
1824                 cd 7c &&
1825
1826                 mkdir z &&
1827                 echo b >z/b &&
1828                 echo c >z/c &&
1829                 mkdir x &&
1830                 echo d >x/d &&
1831                 git add z x &&
1832                 test_tick &&
1833                 git commit -m "O" &&
1834
1835                 git branch O &&
1836                 git branch A &&
1837                 git branch B &&
1838
1839                 git checkout A &&
1840                 git mv z y &&
1841                 git mv x w &&
1842                 test_tick &&
1843                 git commit -m "A" &&
1844
1845                 git checkout B &&
1846                 git mv x/d z/ &&
1847                 rmdir x &&
1848                 test_tick &&
1849                 git commit -m "B"
1850         )
1851 }
1852
1853 test_expect_success '7c: rename/rename(1to...2or3); transitive rename may add complexity' '
1854         test_setup_7c &&
1855         (
1856                 cd 7c &&
1857
1858                 git checkout A^0 &&
1859
1860                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1861                 test_i18ngrep "CONFLICT (rename/rename).*x/d.*w/d.*y/d" out &&
1862
1863                 git ls-files -s >out &&
1864                 test_line_count = 5 out &&
1865                 git ls-files -u >out &&
1866                 test_line_count = 3 out &&
1867                 git ls-files -o >out &&
1868                 test_line_count = 1 out &&
1869
1870                 git rev-parse >actual \
1871                         :0:y/b :0:y/c :1:x/d :2:w/d :3:y/d &&
1872                 git rev-parse >expect \
1873                          O:z/b  O:z/c  O:x/d  O:x/d  O:x/d &&
1874                 test_cmp expect actual
1875         )
1876 '
1877
1878 # Testcase 7d, transitive rename involved in rename/delete; how is it reported?
1879 #   (Related somewhat to testcases 5b and 8d)
1880 #   Commit O: z/{b,c}, x/d
1881 #   Commit A: y/{b,c}
1882 #   Commit B: z/{b,c,d}
1883 #   Expected: y/{b,c}, CONFLICT(delete x/d vs rename to y/d)
1884 #   NOTE: z->y so NOT CONFLICT(delete x/d vs rename to z/d)
1885
1886 test_setup_7d () {
1887         test_create_repo 7d &&
1888         (
1889                 cd 7d &&
1890
1891                 mkdir z &&
1892                 echo b >z/b &&
1893                 echo c >z/c &&
1894                 mkdir x &&
1895                 echo d >x/d &&
1896                 git add z x &&
1897                 test_tick &&
1898                 git commit -m "O" &&
1899
1900                 git branch O &&
1901                 git branch A &&
1902                 git branch B &&
1903
1904                 git checkout A &&
1905                 git mv z y &&
1906                 git rm -rf x &&
1907                 test_tick &&
1908                 git commit -m "A" &&
1909
1910                 git checkout B &&
1911                 git mv x/d z/ &&
1912                 rmdir x &&
1913                 test_tick &&
1914                 git commit -m "B"
1915         )
1916 }
1917
1918 test_expect_success '7d: transitive rename involved in rename/delete; how is it reported?' '
1919         test_setup_7d &&
1920         (
1921                 cd 7d &&
1922
1923                 git checkout A^0 &&
1924
1925                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1926                 test_i18ngrep "CONFLICT (rename/delete).*x/d.*y/d" out &&
1927
1928                 git ls-files -s >out &&
1929                 test_line_count = 3 out &&
1930                 git ls-files -u >out &&
1931                 test_line_count = 1 out &&
1932                 git ls-files -o >out &&
1933                 test_line_count = 1 out &&
1934
1935                 git rev-parse >actual \
1936                         :0:y/b :0:y/c :3:y/d &&
1937                 git rev-parse >expect \
1938                          O:z/b  O:z/c  O:x/d &&
1939                 test_cmp expect actual
1940         )
1941 '
1942
1943 # Testcase 7e, transitive rename in rename/delete AND dirs in the way
1944 #   (Very similar to 'both rename source and destination involved in D/F conflict' from t6022-merge-rename.sh)
1945 #   (Also related to testcases 9c and 9d)
1946 #   Commit O: z/{b,c},     x/d_1
1947 #   Commit A: y/{b,c,d/g}, x/d/f
1948 #   Commit B: z/{b,c,d_1}
1949 #   Expected: rename/delete(x/d_1->y/d_1 vs. None) + D/F conflict on y/d
1950 #             y/{b,c,d/g}, y/d_1~B^0, x/d/f
1951
1952 #   NOTE: The main path of interest here is d_1 and where it ends up, but
1953 #         this is actually a case that has two potential directory renames
1954 #         involved and D/F conflict(s), so it makes sense to walk through
1955 #         each step.
1956 #
1957 #         Commit A renames z/ -> y/.  Thus everything that B adds to z/
1958 #         should be instead moved to y/.  This gives us the D/F conflict on
1959 #         y/d because x/d_1 -> z/d_1 -> y/d_1 conflicts with y/d/g.
1960 #
1961 #         Further, commit B renames x/ -> z/, thus everything A adds to x/
1962 #         should instead be moved to z/...BUT we removed z/ and renamed it
1963 #         to y/, so maybe everything should move not from x/ to z/, but
1964 #         from x/ to z/ to y/.  Doing so might make sense from the logic so
1965 #         far, but note that commit A had both an x/ and a y/; it did the
1966 #         renaming of z/ to y/ and created x/d/f and it clearly made these
1967 #         things separate, so it doesn't make much sense to push these
1968 #         together.  Doing so is what I'd call a doubly transitive rename;
1969 #         see testcases 9c and 9d for further discussion of this issue and
1970 #         how it's resolved.
1971
1972 test_setup_7e () {
1973         test_create_repo 7e &&
1974         (
1975                 cd 7e &&
1976
1977                 mkdir z &&
1978                 echo b >z/b &&
1979                 echo c >z/c &&
1980                 mkdir x &&
1981                 echo d1 >x/d &&
1982                 git add z x &&
1983                 test_tick &&
1984                 git commit -m "O" &&
1985
1986                 git branch O &&
1987                 git branch A &&
1988                 git branch B &&
1989
1990                 git checkout A &&
1991                 git mv z y &&
1992                 git rm x/d &&
1993                 mkdir -p x/d &&
1994                 mkdir -p y/d &&
1995                 echo f >x/d/f &&
1996                 echo g >y/d/g &&
1997                 git add x/d/f y/d/g &&
1998                 test_tick &&
1999                 git commit -m "A" &&
2000
2001                 git checkout B &&
2002                 git mv x/d z/ &&
2003                 rmdir x &&
2004                 test_tick &&
2005                 git commit -m "B"
2006         )
2007 }
2008
2009 test_expect_success '7e: transitive rename in rename/delete AND dirs in the way' '
2010         test_setup_7e &&
2011         (
2012                 cd 7e &&
2013
2014                 git checkout A^0 &&
2015
2016                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2017                 test_i18ngrep "CONFLICT (rename/delete).*x/d.*y/d" out &&
2018
2019                 git ls-files -s >out &&
2020                 test_line_count = 5 out &&
2021                 git ls-files -u >out &&
2022                 test_line_count = 1 out &&
2023                 git ls-files -o >out &&
2024                 test_line_count = 2 out &&
2025
2026                 git rev-parse >actual \
2027                         :0:x/d/f :0:y/d/g :0:y/b :0:y/c :3:y/d &&
2028                 git rev-parse >expect \
2029                          A:x/d/f  A:y/d/g  O:z/b  O:z/c  O:x/d &&
2030                 test_cmp expect actual &&
2031
2032                 git hash-object y/d~B^0 >actual &&
2033                 git rev-parse O:x/d >expect &&
2034                 test_cmp expect actual
2035         )
2036 '
2037
2038 ###########################################################################
2039 # SECTION 8: Suboptimal merges
2040 #
2041 # As alluded to in the last section, the ruleset we have built up for
2042 # detecting directory renames unfortunately has some special cases where it
2043 # results in slightly suboptimal or non-intuitive behavior.  This section
2044 # explores these cases.
2045 #
2046 # To be fair, we already had non-intuitive or suboptimal behavior for most
2047 # of these cases in git before introducing implicit directory rename
2048 # detection, but it'd be nice if there was a modified ruleset out there
2049 # that handled these cases a bit better.
2050 ###########################################################################
2051
2052 # Testcase 8a, Dual-directory rename, one into the others' way
2053 #   Commit O. x/{a,b},   y/{c,d}
2054 #   Commit A. x/{a,b,e}, y/{c,d,f}
2055 #   Commit B. y/{a,b},   z/{c,d}
2056 #
2057 # Possible Resolutions:
2058 #   w/o dir-rename detection: y/{a,b,f},   z/{c,d},   x/e
2059 #   Currently expected:       y/{a,b,e,f}, z/{c,d}
2060 #   Optimal:                  y/{a,b,e},   z/{c,d,f}
2061 #
2062 # Note: Both x and y got renamed and it'd be nice to detect both, and we do
2063 # better with directory rename detection than git did without, but the
2064 # simple rule from section 5 prevents me from handling this as optimally as
2065 # we potentially could.
2066
2067 test_setup_8a () {
2068         test_create_repo 8a &&
2069         (
2070                 cd 8a &&
2071
2072                 mkdir x &&
2073                 mkdir y &&
2074                 echo a >x/a &&
2075                 echo b >x/b &&
2076                 echo c >y/c &&
2077                 echo d >y/d &&
2078                 git add x y &&
2079                 test_tick &&
2080                 git commit -m "O" &&
2081
2082                 git branch O &&
2083                 git branch A &&
2084                 git branch B &&
2085
2086                 git checkout A &&
2087                 echo e >x/e &&
2088                 echo f >y/f &&
2089                 git add x/e y/f &&
2090                 test_tick &&
2091                 git commit -m "A" &&
2092
2093                 git checkout B &&
2094                 git mv y z &&
2095                 git mv x y &&
2096                 test_tick &&
2097                 git commit -m "B"
2098         )
2099 }
2100
2101 test_expect_success '8a: Dual-directory rename, one into the others way' '
2102         test_setup_8a &&
2103         (
2104                 cd 8a &&
2105
2106                 git checkout A^0 &&
2107
2108                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2109
2110                 git ls-files -s >out &&
2111                 test_line_count = 6 out &&
2112                 git ls-files -u >out &&
2113                 test_line_count = 0 out &&
2114                 git ls-files -o >out &&
2115                 test_line_count = 1 out &&
2116
2117                 git rev-parse >actual \
2118                         HEAD:y/a HEAD:y/b HEAD:y/e HEAD:y/f HEAD:z/c HEAD:z/d &&
2119                 git rev-parse >expect \
2120                         O:x/a    O:x/b    A:x/e    A:y/f    O:y/c    O:y/d &&
2121                 test_cmp expect actual
2122         )
2123 '
2124
2125 # Testcase 8b, Dual-directory rename, one into the others' way, with conflicting filenames
2126 #   Commit O. x/{a_1,b_1},     y/{a_2,b_2}
2127 #   Commit A. x/{a_1,b_1,e_1}, y/{a_2,b_2,e_2}
2128 #   Commit B. y/{a_1,b_1},     z/{a_2,b_2}
2129 #
2130 #   w/o dir-rename detection: y/{a_1,b_1,e_2}, z/{a_2,b_2}, x/e_1
2131 #   Currently expected:       <same>
2132 #   Scary:                    y/{a_1,b_1},     z/{a_2,b_2}, CONFLICT(add/add, e_1 vs. e_2)
2133 #   Optimal:                  y/{a_1,b_1,e_1}, z/{a_2,b_2,e_2}
2134 #
2135 # Note: Very similar to 8a, except instead of 'e' and 'f' in directories x and
2136 # y, both are named 'e'.  Without directory rename detection, neither file
2137 # moves directories.  Implement directory rename detection suboptimally, and
2138 # you get an add/add conflict, but both files were added in commit A, so this
2139 # is an add/add conflict where one side of history added both files --
2140 # something we can't represent in the index.  Obviously, we'd prefer the last
2141 # resolution, but our previous rules are too coarse to allow it.  Using both
2142 # the rules from section 4 and section 5 save us from the Scary resolution,
2143 # making us fall back to pre-directory-rename-detection behavior for both
2144 # e_1 and e_2.
2145
2146 test_setup_8b () {
2147         test_create_repo 8b &&
2148         (
2149                 cd 8b &&
2150
2151                 mkdir x &&
2152                 mkdir y &&
2153                 echo a1 >x/a &&
2154                 echo b1 >x/b &&
2155                 echo a2 >y/a &&
2156                 echo b2 >y/b &&
2157                 git add x y &&
2158                 test_tick &&
2159                 git commit -m "O" &&
2160
2161                 git branch O &&
2162                 git branch A &&
2163                 git branch B &&
2164
2165                 git checkout A &&
2166                 echo e1 >x/e &&
2167                 echo e2 >y/e &&
2168                 git add x/e y/e &&
2169                 test_tick &&
2170                 git commit -m "A" &&
2171
2172                 git checkout B &&
2173                 git mv y z &&
2174                 git mv x y &&
2175                 test_tick &&
2176                 git commit -m "B"
2177         )
2178 }
2179
2180 test_expect_success '8b: Dual-directory rename, one into the others way, with conflicting filenames' '
2181         test_setup_8b &&
2182         (
2183                 cd 8b &&
2184
2185                 git checkout A^0 &&
2186
2187                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2188
2189                 git ls-files -s >out &&
2190                 test_line_count = 6 out &&
2191                 git ls-files -u >out &&
2192                 test_line_count = 0 out &&
2193                 git ls-files -o >out &&
2194                 test_line_count = 1 out &&
2195
2196                 git rev-parse >actual \
2197                         HEAD:y/a HEAD:y/b HEAD:z/a HEAD:z/b HEAD:x/e HEAD:y/e &&
2198                 git rev-parse >expect \
2199                         O:x/a    O:x/b    O:y/a    O:y/b    A:x/e    A:y/e &&
2200                 test_cmp expect actual
2201         )
2202 '
2203
2204 # Testcase 8c, modify/delete or rename+modify/delete?
2205 #   (Related to testcases 5b, 8d, and 9h)
2206 #   Commit O: z/{b,c,d}
2207 #   Commit A: y/{b,c}
2208 #   Commit B: z/{b,c,d_modified,e}
2209 #   Expected: y/{b,c,e}, CONFLICT(modify/delete: on z/d)
2210 #
2211 #   Note: It could easily be argued that the correct resolution here is
2212 #         y/{b,c,e}, CONFLICT(rename/delete: z/d -> y/d vs deleted)
2213 #         and that the modified version of d should be present in y/ after
2214 #         the merge, just marked as conflicted.  Indeed, I previously did
2215 #         argue that.  But applying directory renames to the side of
2216 #         history where a file is merely modified results in spurious
2217 #         rename/rename(1to2) conflicts -- see testcase 9h.  See also
2218 #         notes in 8d.
2219
2220 test_setup_8c () {
2221         test_create_repo 8c &&
2222         (
2223                 cd 8c &&
2224
2225                 mkdir z &&
2226                 echo b >z/b &&
2227                 echo c >z/c &&
2228                 test_seq 1 10 >z/d &&
2229                 git add z &&
2230                 test_tick &&
2231                 git commit -m "O" &&
2232
2233                 git branch O &&
2234                 git branch A &&
2235                 git branch B &&
2236
2237                 git checkout A &&
2238                 git rm z/d &&
2239                 git mv z y &&
2240                 test_tick &&
2241                 git commit -m "A" &&
2242
2243                 git checkout B &&
2244                 echo 11 >z/d &&
2245                 test_chmod +x z/d &&
2246                 echo e >z/e &&
2247                 git add z/d z/e &&
2248                 test_tick &&
2249                 git commit -m "B"
2250         )
2251 }
2252
2253 test_expect_success '8c: modify/delete or rename+modify/delete' '
2254         test_setup_8c &&
2255         (
2256                 cd 8c &&
2257
2258                 git checkout A^0 &&
2259
2260                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2261                 test_i18ngrep "CONFLICT (modify/delete).* z/d" out &&
2262
2263                 git ls-files -s >out &&
2264                 test_line_count = 5 out &&
2265                 git ls-files -u >out &&
2266                 test_line_count = 2 out &&
2267                 git ls-files -o >out &&
2268                 test_line_count = 1 out &&
2269
2270                 git rev-parse >actual \
2271                         :0:y/b :0:y/c :0:y/e :1:z/d :3:z/d &&
2272                 git rev-parse >expect \
2273                          O:z/b  O:z/c  B:z/e  O:z/d  B:z/d &&
2274                 test_cmp expect actual &&
2275
2276                 test_must_fail git rev-parse :2:z/d &&
2277                 git ls-files -s z/d | grep ^100755 &&
2278                 test_path_is_file z/d &&
2279                 test_path_is_missing y/d
2280         )
2281 '
2282
2283 # Testcase 8d, rename/delete...or not?
2284 #   (Related to testcase 5b; these may appear slightly inconsistent to users;
2285 #    Also related to testcases 7d and 7e)
2286 #   Commit O: z/{b,c,d}
2287 #   Commit A: y/{b,c}
2288 #   Commit B: z/{b,c,d,e}
2289 #   Expected: y/{b,c,e}
2290 #
2291 #   Note: It would also be somewhat reasonable to resolve this as
2292 #             y/{b,c,e}, CONFLICT(rename/delete: x/d -> y/d or deleted)
2293 #
2294 #   In this case, I'm leaning towards: commit A was the one that deleted z/d
2295 #   and it did the rename of z to y, so the two "conflicts" (rename vs.
2296 #   delete) are both coming from commit A, which is illogical.  Conflicts
2297 #   during merging are supposed to be about opposite sides doing things
2298 #   differently.
2299
2300 test_setup_8d () {
2301         test_create_repo 8d &&
2302         (
2303                 cd 8d &&
2304
2305                 mkdir z &&
2306                 echo b >z/b &&
2307                 echo c >z/c &&
2308                 test_seq 1 10 >z/d &&
2309                 git add z &&
2310                 test_tick &&
2311                 git commit -m "O" &&
2312
2313                 git branch O &&
2314                 git branch A &&
2315                 git branch B &&
2316
2317                 git checkout A &&
2318                 git rm z/d &&
2319                 git mv z y &&
2320                 test_tick &&
2321                 git commit -m "A" &&
2322
2323                 git checkout B &&
2324                 echo e >z/e &&
2325                 git add z/e &&
2326                 test_tick &&
2327                 git commit -m "B"
2328         )
2329 }
2330
2331 test_expect_success '8d: rename/delete...or not?' '
2332         test_setup_8d &&
2333         (
2334                 cd 8d &&
2335
2336                 git checkout A^0 &&
2337
2338                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2339
2340                 git ls-files -s >out &&
2341                 test_line_count = 3 out &&
2342
2343                 git rev-parse >actual \
2344                         HEAD:y/b HEAD:y/c HEAD:y/e &&
2345                 git rev-parse >expect \
2346                         O:z/b    O:z/c    B:z/e &&
2347                 test_cmp expect actual
2348         )
2349 '
2350
2351 # Testcase 8e, Both sides rename, one side adds to original directory
2352 #   Commit O: z/{b,c}
2353 #   Commit A: y/{b,c}
2354 #   Commit B: w/{b,c}, z/d
2355 #
2356 # Possible Resolutions:
2357 #   if z not considered renamed: z/d, CONFLICT(z/b -> y/b vs. w/b),
2358 #                                     CONFLICT(z/c -> y/c vs. w/c)
2359 #   if z->y rename considered:   y/d, CONFLICT(z/b -> y/b vs. w/b),
2360 #                                     CONFLICT(z/c -> y/c vs. w/c)
2361 #   Optimal:                     ??
2362 #
2363 # Notes: In commit A, directory z got renamed to y.  In commit B, directory z
2364 #        did NOT get renamed; the directory is still present; instead it is
2365 #        considered to have just renamed a subset of paths in directory z
2366 #        elsewhere.  This is much like testcase 6b2 (where commit B moves all
2367 #        the original paths out of z/ but opted to keep d within z/).
2368 #
2369 #        It was not clear in the past what should be done with this testcase;
2370 #        in fact, I noted that I "just picked one" previously.  However,
2371 #        following the new logic for testcase 6b2, we should take the rename
2372 #        and move z/d to y/d.
2373 #
2374 #        6b1, 6b2, and this case are definitely somewhat fuzzy in terms of
2375 #        whether they are optimal for end users, but (a) the default for
2376 #        directory rename detection is to mark these all as conflicts
2377 #        anyway, (b) it feels like this is less prone to higher order corner
2378 #        case confusion, and (c) the current algorithm requires less global
2379 #        knowledge (i.e. less coupling in the algorithm between renames done
2380 #        on both sides) which thus means users are better able to predict
2381 #        the behavior, and predict it without computing as many details.
2382
2383 test_setup_8e () {
2384         test_create_repo 8e &&
2385         (
2386                 cd 8e &&
2387
2388                 mkdir z &&
2389                 echo b >z/b &&
2390                 echo c >z/c &&
2391                 git add z &&
2392                 test_tick &&
2393                 git commit -m "O" &&
2394
2395                 git branch O &&
2396                 git branch A &&
2397                 git branch B &&
2398
2399                 git checkout A &&
2400                 git mv z y &&
2401                 test_tick &&
2402                 git commit -m "A" &&
2403
2404                 git checkout B &&
2405                 git mv z w &&
2406                 mkdir z &&
2407                 echo d >z/d &&
2408                 git add z/d &&
2409                 test_tick &&
2410                 git commit -m "B"
2411         )
2412 }
2413
2414 test_expect_success '8e: Both sides rename, one side adds to original directory' '
2415         test_setup_8e &&
2416         (
2417                 cd 8e &&
2418
2419                 git checkout A^0 &&
2420
2421                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
2422                 test_i18ngrep CONFLICT.*rename/rename.*z/c.*y/c.*w/c out &&
2423                 test_i18ngrep CONFLICT.*rename/rename.*z/b.*y/b.*w/b out &&
2424
2425                 git ls-files -s >out &&
2426                 test_line_count = 7 out &&
2427                 git ls-files -u >out &&
2428                 test_line_count = 6 out &&
2429                 git ls-files -o >out &&
2430                 test_line_count = 2 out &&
2431
2432                 git rev-parse >actual \
2433                         :1:z/b :2:y/b :3:w/b :1:z/c :2:y/c :3:w/c :0:y/d &&
2434                 git rev-parse >expect \
2435                          O:z/b  O:z/b  O:z/b  O:z/c  O:z/c  O:z/c  B:z/d &&
2436                 test_cmp expect actual &&
2437
2438                 git hash-object >actual \
2439                         y/b   w/b   y/c   w/c &&
2440                 git rev-parse >expect \
2441                         O:z/b O:z/b O:z/c O:z/c &&
2442                 test_cmp expect actual &&
2443
2444                 test_path_is_missing z/b &&
2445                 test_path_is_missing z/c
2446         )
2447 '
2448
2449 ###########################################################################
2450 # SECTION 9: Other testcases
2451 #
2452 # This section consists of miscellaneous testcases I thought of during
2453 # the implementation which round out the testing.
2454 ###########################################################################
2455
2456 # Testcase 9a, Inner renamed directory within outer renamed directory
2457 #   (Related to testcase 1f)
2458 #   Commit O: z/{b,c,d/{e,f,g}}
2459 #   Commit A: y/{b,c}, x/w/{e,f,g}
2460 #   Commit B: z/{b,c,d/{e,f,g,h},i}
2461 #   Expected: y/{b,c,i}, x/w/{e,f,g,h}
2462 #   NOTE: The only reason this one is interesting is because when a directory
2463 #         is split into multiple other directories, we determine by the weight
2464 #         of which one had the most paths going to it.  A naive implementation
2465 #         of that could take the new file in commit B at z/i to x/w/i or x/i.
2466
2467 test_setup_9a () {
2468         test_create_repo 9a &&
2469         (
2470                 cd 9a &&
2471
2472                 mkdir -p z/d &&
2473                 echo b >z/b &&
2474                 echo c >z/c &&
2475                 echo e >z/d/e &&
2476                 echo f >z/d/f &&
2477                 echo g >z/d/g &&
2478                 git add z &&
2479                 test_tick &&
2480                 git commit -m "O" &&
2481
2482                 git branch O &&
2483                 git branch A &&
2484                 git branch B &&
2485
2486                 git checkout A &&
2487                 mkdir x &&
2488                 git mv z/d x/w &&
2489                 git mv z y &&
2490                 test_tick &&
2491                 git commit -m "A" &&
2492
2493                 git checkout B &&
2494                 echo h >z/d/h &&
2495                 echo i >z/i &&
2496                 git add z &&
2497                 test_tick &&
2498                 git commit -m "B"
2499         )
2500 }
2501
2502 test_expect_success '9a: Inner renamed directory within outer renamed directory' '
2503         test_setup_9a &&
2504         (
2505                 cd 9a &&
2506
2507                 git checkout A^0 &&
2508
2509                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2510
2511                 git ls-files -s >out &&
2512                 test_line_count = 7 out &&
2513                 git ls-files -u >out &&
2514                 test_line_count = 0 out &&
2515                 git ls-files -o >out &&
2516                 test_line_count = 1 out &&
2517
2518                 git rev-parse >actual \
2519                         HEAD:y/b HEAD:y/c HEAD:y/i &&
2520                 git rev-parse >expect \
2521                         O:z/b    O:z/c    B:z/i &&
2522                 test_cmp expect actual &&
2523
2524                 git rev-parse >actual \
2525                         HEAD:x/w/e HEAD:x/w/f HEAD:x/w/g HEAD:x/w/h &&
2526                 git rev-parse >expect \
2527                         O:z/d/e    O:z/d/f    O:z/d/g    B:z/d/h &&
2528                 test_cmp expect actual
2529         )
2530 '
2531
2532 # Testcase 9b, Transitive rename with content merge
2533 #   (Related to testcase 1c)
2534 #   Commit O: z/{b,c},   x/d_1
2535 #   Commit A: y/{b,c},   x/d_2
2536 #   Commit B: z/{b,c,d_3}
2537 #   Expected: y/{b,c,d_merged}
2538
2539 test_setup_9b () {
2540         test_create_repo 9b &&
2541         (
2542                 cd 9b &&
2543
2544                 mkdir z &&
2545                 echo b >z/b &&
2546                 echo c >z/c &&
2547                 mkdir x &&
2548                 test_seq 1 10 >x/d &&
2549                 git add z x &&
2550                 test_tick &&
2551                 git commit -m "O" &&
2552
2553                 git branch O &&
2554                 git branch A &&
2555                 git branch B &&
2556
2557                 git checkout A &&
2558                 git mv z y &&
2559                 test_seq 1 11 >x/d &&
2560                 git add x/d &&
2561                 test_tick &&
2562                 git commit -m "A" &&
2563
2564                 git checkout B &&
2565                 test_seq 0 10 >x/d &&
2566                 git mv x/d z/d &&
2567                 git add z/d &&
2568                 test_tick &&
2569                 git commit -m "B"
2570         )
2571 }
2572
2573 test_expect_success '9b: Transitive rename with content merge' '
2574         test_setup_9b &&
2575         (
2576                 cd 9b &&
2577
2578                 git checkout A^0 &&
2579
2580                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2581
2582                 git ls-files -s >out &&
2583                 test_line_count = 3 out &&
2584
2585                 test_seq 0 11 >expected &&
2586                 test_cmp expected y/d &&
2587                 git add expected &&
2588                 git rev-parse >actual \
2589                         HEAD:y/b HEAD:y/c HEAD:y/d &&
2590                 git rev-parse >expect \
2591                         O:z/b    O:z/c    :0:expected &&
2592                 test_cmp expect actual &&
2593                 test_must_fail git rev-parse HEAD:x/d &&
2594                 test_must_fail git rev-parse HEAD:z/d &&
2595                 test_path_is_missing z/d &&
2596
2597                 test $(git rev-parse HEAD:y/d) != $(git rev-parse O:x/d) &&
2598                 test $(git rev-parse HEAD:y/d) != $(git rev-parse A:x/d) &&
2599                 test $(git rev-parse HEAD:y/d) != $(git rev-parse B:z/d)
2600         )
2601 '
2602
2603 # Testcase 9c, Doubly transitive rename?
2604 #   (Related to testcase 1c, 7e, and 9d)
2605 #   Commit O: z/{b,c},     x/{d,e},    w/f
2606 #   Commit A: y/{b,c},     x/{d,e,f,g}
2607 #   Commit B: z/{b,c,d,e},             w/f
2608 #   Expected: y/{b,c,d,e}, x/{f,g}
2609 #
2610 #   NOTE: x/f and x/g may be slightly confusing here.  The rename from w/f to
2611 #         x/f is clear.  Let's look beyond that.  Here's the logic:
2612 #            Commit B renamed x/ -> z/
2613 #            Commit A renamed z/ -> y/
2614 #         So, we could possibly further rename x/f to z/f to y/f, a doubly
2615 #         transient rename.  However, where does it end?  We can chain these
2616 #         indefinitely (see testcase 9d).  What if there is a D/F conflict
2617 #         at z/f/ or y/f/?  Or just another file conflict at one of those
2618 #         paths?  In the case of an N-long chain of transient renamings,
2619 #         where do we "abort" the rename at?  Can the user make sense of
2620 #         the resulting conflict and resolve it?
2621 #
2622 #         To avoid this confusion I use the simple rule that if the other side
2623 #         of history did a directory rename to a path that your side renamed
2624 #         away, then ignore that particular rename from the other side of
2625 #         history for any implicit directory renames.
2626
2627 test_setup_9c () {
2628         test_create_repo 9c &&
2629         (
2630                 cd 9c &&
2631
2632                 mkdir z &&
2633                 echo b >z/b &&
2634                 echo c >z/c &&
2635                 mkdir x &&
2636                 echo d >x/d &&
2637                 echo e >x/e &&
2638                 mkdir w &&
2639                 echo f >w/f &&
2640                 git add z x w &&
2641                 test_tick &&
2642                 git commit -m "O" &&
2643
2644                 git branch O &&
2645                 git branch A &&
2646                 git branch B &&
2647
2648                 git checkout A &&
2649                 git mv z y &&
2650                 git mv w/f x/ &&
2651                 echo g >x/g &&
2652                 git add x/g &&
2653                 test_tick &&
2654                 git commit -m "A" &&
2655
2656                 git checkout B &&
2657                 git mv x/d z/d &&
2658                 git mv x/e z/e &&
2659                 test_tick &&
2660                 git commit -m "B"
2661         )
2662 }
2663
2664 test_expect_success '9c: Doubly transitive rename?' '
2665         test_setup_9c &&
2666         (
2667                 cd 9c &&
2668
2669                 git checkout A^0 &&
2670
2671                 git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2672                 test_i18ngrep "WARNING: Avoiding applying x -> z rename to x/f" out &&
2673
2674                 git ls-files -s >out &&
2675                 test_line_count = 6 out &&
2676                 git ls-files -o >out &&
2677                 test_line_count = 1 out &&
2678
2679                 git rev-parse >actual \
2680                         HEAD:y/b HEAD:y/c HEAD:y/d HEAD:y/e HEAD:x/f HEAD:x/g &&
2681                 git rev-parse >expect \
2682                         O:z/b    O:z/c    O:x/d    O:x/e    O:w/f    A:x/g &&
2683                 test_cmp expect actual
2684         )
2685 '
2686
2687 # Testcase 9d, N-fold transitive rename?
2688 #   (Related to testcase 9c...and 1c and 7e)
2689 #   Commit O: z/a, y/b, x/c, w/d, v/e, u/f
2690 #   Commit A:  y/{a,b},  w/{c,d},  u/{e,f}
2691 #   Commit B: z/{a,t}, x/{b,c}, v/{d,e}, u/f
2692 #   Expected: <see NOTE first>
2693 #
2694 #   NOTE: z/ -> y/ (in commit A)
2695 #         y/ -> x/ (in commit B)
2696 #         x/ -> w/ (in commit A)
2697 #         w/ -> v/ (in commit B)
2698 #         v/ -> u/ (in commit A)
2699 #         So, if we add a file to z, say z/t, where should it end up?  In u?
2700 #         What if there's another file or directory named 't' in one of the
2701 #         intervening directories and/or in u itself?  Also, shouldn't the
2702 #         same logic that places 't' in u/ also move ALL other files to u/?
2703 #         What if there are file or directory conflicts in any of them?  If
2704 #         we attempted to do N-way (N-fold? N-ary? N-uple?) transitive renames
2705 #         like this, would the user have any hope of understanding any
2706 #         conflicts or how their working tree ended up?  I think not, so I'm
2707 #         ruling out N-ary transitive renames for N>1.
2708 #
2709 #   Therefore our expected result is:
2710 #     z/t, y/a, x/b, w/c, u/d, u/e, u/f
2711 #   The reason that v/d DOES get transitively renamed to u/d is that u/ isn't
2712 #   renamed somewhere.  A slightly sub-optimal result, but it uses fairly
2713 #   simple rules that are consistent with what we need for all the other
2714 #   testcases and simplifies things for the user.
2715
2716 test_setup_9d () {
2717         test_create_repo 9d &&
2718         (
2719                 cd 9d &&
2720
2721                 mkdir z y x w v u &&
2722                 echo a >z/a &&
2723                 echo b >y/b &&
2724                 echo c >x/c &&
2725                 echo d >w/d &&
2726                 echo e >v/e &&
2727                 echo f >u/f &&
2728                 git add z y x w v u &&
2729                 test_tick &&
2730                 git commit -m "O" &&
2731
2732                 git branch O &&
2733                 git branch A &&
2734                 git branch B &&
2735
2736                 git checkout A &&
2737                 git mv z/a y/ &&
2738                 git mv x/c w/ &&
2739                 git mv v/e u/ &&
2740                 test_tick &&
2741                 git commit -m "A" &&
2742
2743                 git checkout B &&
2744                 echo t >z/t &&
2745                 git mv y/b x/ &&
2746                 git mv w/d v/ &&
2747                 git add z/t &&
2748                 test_tick &&
2749                 git commit -m "B"
2750         )
2751 }
2752
2753 test_expect_success '9d: N-way transitive rename?' '
2754         test_setup_9d &&
2755         (
2756                 cd 9d &&
2757
2758                 git checkout A^0 &&
2759
2760                 git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2761                 test_i18ngrep "WARNING: Avoiding applying z -> y rename to z/t" out &&
2762                 test_i18ngrep "WARNING: Avoiding applying y -> x rename to y/a" out &&
2763                 test_i18ngrep "WARNING: Avoiding applying x -> w rename to x/b" out &&
2764                 test_i18ngrep "WARNING: Avoiding applying w -> v rename to w/c" out &&
2765
2766                 git ls-files -s >out &&
2767                 test_line_count = 7 out &&
2768                 git ls-files -o >out &&
2769                 test_line_count = 1 out &&
2770
2771                 git rev-parse >actual \
2772                         HEAD:z/t \
2773                         HEAD:y/a HEAD:x/b HEAD:w/c \
2774                         HEAD:u/d HEAD:u/e HEAD:u/f &&
2775                 git rev-parse >expect \
2776                         B:z/t    \
2777                         O:z/a    O:y/b    O:x/c    \
2778                         O:w/d    O:v/e    A:u/f &&
2779                 test_cmp expect actual
2780         )
2781 '
2782
2783 # Testcase 9e, N-to-1 whammo
2784 #   (Related to testcase 9c...and 1c and 7e)
2785 #   Commit O: dir1/{a,b}, dir2/{d,e}, dir3/{g,h}, dirN/{j,k}
2786 #   Commit A: dir1/{a,b,c,yo}, dir2/{d,e,f,yo}, dir3/{g,h,i,yo}, dirN/{j,k,l,yo}
2787 #   Commit B: combined/{a,b,d,e,g,h,j,k}
2788 #   Expected: combined/{a,b,c,d,e,f,g,h,i,j,k,l}, CONFLICT(Nto1) warnings,
2789 #             dir1/yo, dir2/yo, dir3/yo, dirN/yo
2790
2791 test_setup_9e () {
2792         test_create_repo 9e &&
2793         (
2794                 cd 9e &&
2795
2796                 mkdir dir1 dir2 dir3 dirN &&
2797                 echo a >dir1/a &&
2798                 echo b >dir1/b &&
2799                 echo d >dir2/d &&
2800                 echo e >dir2/e &&
2801                 echo g >dir3/g &&
2802                 echo h >dir3/h &&
2803                 echo j >dirN/j &&
2804                 echo k >dirN/k &&
2805                 git add dir* &&
2806                 test_tick &&
2807                 git commit -m "O" &&
2808
2809                 git branch O &&
2810                 git branch A &&
2811                 git branch B &&
2812
2813                 git checkout A &&
2814                 echo c  >dir1/c &&
2815                 echo yo >dir1/yo &&
2816                 echo f  >dir2/f &&
2817                 echo yo >dir2/yo &&
2818                 echo i  >dir3/i &&
2819                 echo yo >dir3/yo &&
2820                 echo l  >dirN/l &&
2821                 echo yo >dirN/yo &&
2822                 git add dir* &&
2823                 test_tick &&
2824                 git commit -m "A" &&
2825
2826                 git checkout B &&
2827                 git mv dir1 combined &&
2828                 git mv dir2/* combined/ &&
2829                 git mv dir3/* combined/ &&
2830                 git mv dirN/* combined/ &&
2831                 test_tick &&
2832                 git commit -m "B"
2833         )
2834 }
2835
2836 test_expect_success C_LOCALE_OUTPUT '9e: N-to-1 whammo' '
2837         test_setup_9e &&
2838         (
2839                 cd 9e &&
2840
2841                 git checkout A^0 &&
2842
2843                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2844                 grep "CONFLICT (implicit dir rename): Cannot map more than one path to combined/yo" out >error_line &&
2845                 grep -q dir1/yo error_line &&
2846                 grep -q dir2/yo error_line &&
2847                 grep -q dir3/yo error_line &&
2848                 grep -q dirN/yo error_line &&
2849
2850                 git ls-files -s >out &&
2851                 test_line_count = 16 out &&
2852                 git ls-files -u >out &&
2853                 test_line_count = 0 out &&
2854                 git ls-files -o >out &&
2855                 test_line_count = 2 out &&
2856
2857                 git rev-parse >actual \
2858                         :0:combined/a :0:combined/b :0:combined/c \
2859                         :0:combined/d :0:combined/e :0:combined/f \
2860                         :0:combined/g :0:combined/h :0:combined/i \
2861                         :0:combined/j :0:combined/k :0:combined/l &&
2862                 git rev-parse >expect \
2863                          O:dir1/a      O:dir1/b      A:dir1/c \
2864                          O:dir2/d      O:dir2/e      A:dir2/f \
2865                          O:dir3/g      O:dir3/h      A:dir3/i \
2866                          O:dirN/j      O:dirN/k      A:dirN/l &&
2867                 test_cmp expect actual &&
2868
2869                 git rev-parse >actual \
2870                         :0:dir1/yo :0:dir2/yo :0:dir3/yo :0:dirN/yo &&
2871                 git rev-parse >expect \
2872                          A:dir1/yo  A:dir2/yo  A:dir3/yo  A:dirN/yo &&
2873                 test_cmp expect actual
2874         )
2875 '
2876
2877 # Testcase 9f, Renamed directory that only contained immediate subdirs
2878 #   (Related to testcases 1e & 9g)
2879 #   Commit O: goal/{a,b}/$more_files
2880 #   Commit A: priority/{a,b}/$more_files
2881 #   Commit B: goal/{a,b}/$more_files, goal/c
2882 #   Expected: priority/{a,b}/$more_files, priority/c
2883
2884 test_setup_9f () {
2885         test_create_repo 9f &&
2886         (
2887                 cd 9f &&
2888
2889                 mkdir -p goal/a &&
2890                 mkdir -p goal/b &&
2891                 echo foo >goal/a/foo &&
2892                 echo bar >goal/b/bar &&
2893                 echo baz >goal/b/baz &&
2894                 git add goal &&
2895                 test_tick &&
2896                 git commit -m "O" &&
2897
2898                 git branch O &&
2899                 git branch A &&
2900                 git branch B &&
2901
2902                 git checkout A &&
2903                 git mv goal/ priority &&
2904                 test_tick &&
2905                 git commit -m "A" &&
2906
2907                 git checkout B &&
2908                 echo c >goal/c &&
2909                 git add goal/c &&
2910                 test_tick &&
2911                 git commit -m "B"
2912         )
2913 }
2914
2915 test_expect_success '9f: Renamed directory that only contained immediate subdirs' '
2916         test_setup_9f &&
2917         (
2918                 cd 9f &&
2919
2920                 git checkout A^0 &&
2921
2922                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2923
2924                 git ls-files -s >out &&
2925                 test_line_count = 4 out &&
2926
2927                 git rev-parse >actual \
2928                         HEAD:priority/a/foo \
2929                         HEAD:priority/b/bar \
2930                         HEAD:priority/b/baz \
2931                         HEAD:priority/c &&
2932                 git rev-parse >expect \
2933                         O:goal/a/foo \
2934                         O:goal/b/bar \
2935                         O:goal/b/baz \
2936                         B:goal/c &&
2937                 test_cmp expect actual &&
2938                 test_must_fail git rev-parse HEAD:goal/c
2939         )
2940 '
2941
2942 # Testcase 9g, Renamed directory that only contained immediate subdirs, immediate subdirs renamed
2943 #   (Related to testcases 1e & 9f)
2944 #   Commit O: goal/{a,b}/$more_files
2945 #   Commit A: priority/{alpha,bravo}/$more_files
2946 #   Commit B: goal/{a,b}/$more_files, goal/c
2947 #   Expected: priority/{alpha,bravo}/$more_files, priority/c
2948 # We currently fail this test because the directory renames we detect are
2949 #   goal/a/ -> priority/alpha/
2950 #   goal/b/ -> priority/bravo/
2951 # We do not detect
2952 #   goal/   -> priority/
2953 # because of no files found within goal/, and the fact that "a" != "alpha"
2954 # and "b" != "bravo".  But I'm not sure it's really a failure given that
2955 # viewpoint...
2956
2957 test_setup_9g () {
2958         test_create_repo 9g &&
2959         (
2960                 cd 9g &&
2961
2962                 mkdir -p goal/a &&
2963                 mkdir -p goal/b &&
2964                 echo foo >goal/a/foo &&
2965                 echo bar >goal/b/bar &&
2966                 echo baz >goal/b/baz &&
2967                 git add goal &&
2968                 test_tick &&
2969                 git commit -m "O" &&
2970
2971                 git branch O &&
2972                 git branch A &&
2973                 git branch B &&
2974
2975                 git checkout A &&
2976                 mkdir priority &&
2977                 git mv goal/a/ priority/alpha &&
2978                 git mv goal/b/ priority/beta &&
2979                 rmdir goal/ &&
2980                 test_tick &&
2981                 git commit -m "A" &&
2982
2983                 git checkout B &&
2984                 echo c >goal/c &&
2985                 git add goal/c &&
2986                 test_tick &&
2987                 git commit -m "B"
2988         )
2989 }
2990
2991 test_expect_failure '9g: Renamed directory that only contained immediate subdirs, immediate subdirs renamed' '
2992         test_setup_9g &&
2993         (
2994                 cd 9g &&
2995
2996                 git checkout A^0 &&
2997
2998                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2999
3000                 git ls-files -s >out &&
3001                 test_line_count = 4 out &&
3002
3003                 git rev-parse >actual \
3004                         HEAD:priority/alpha/foo \
3005                         HEAD:priority/beta/bar  \
3006                         HEAD:priority/beta/baz  \
3007                         HEAD:priority/c &&
3008                 git rev-parse >expect \
3009                         O:goal/a/foo \
3010                         O:goal/b/bar \
3011                         O:goal/b/baz \
3012                         B:goal/c &&
3013                 test_cmp expect actual &&
3014                 test_must_fail git rev-parse HEAD:goal/c
3015         )
3016 '
3017
3018 # Testcase 9h, Avoid implicit rename if involved as source on other side
3019 #   (Extremely closely related to testcase 3a)
3020 #   Commit O: z/{b,c,d_1}
3021 #   Commit A: z/{b,c,d_2}
3022 #   Commit B: y/{b,c}, x/d_1
3023 #   Expected: y/{b,c}, x/d_2
3024 #   NOTE: If we applied the z/ -> y/ rename to z/d, then we'd end up with
3025 #         a rename/rename(1to2) conflict (z/d -> y/d vs. x/d)
3026 test_setup_9h () {
3027         test_create_repo 9h &&
3028         (
3029                 cd 9h &&
3030
3031                 mkdir z &&
3032                 echo b >z/b &&
3033                 echo c >z/c &&
3034                 printf "1\n2\n3\n4\n5\n6\n7\n8\nd\n" >z/d &&
3035                 git add z &&
3036                 test_tick &&
3037                 git commit -m "O" &&
3038
3039                 git branch O &&
3040                 git branch A &&
3041                 git branch B &&
3042
3043                 git checkout A &&
3044                 test_tick &&
3045                 echo more >>z/d &&
3046                 git add z/d &&
3047                 git commit -m "A" &&
3048
3049                 git checkout B &&
3050                 mkdir y &&
3051                 mkdir x &&
3052                 git mv z/b y/ &&
3053                 git mv z/c y/ &&
3054                 git mv z/d x/ &&
3055                 rmdir z &&
3056                 test_tick &&
3057                 git commit -m "B"
3058         )
3059 }
3060
3061 test_expect_success '9h: Avoid dir rename on merely modified path' '
3062         test_setup_9h &&
3063         (
3064                 cd 9h &&
3065
3066                 git checkout A^0 &&
3067
3068                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
3069
3070                 git ls-files -s >out &&
3071                 test_line_count = 3 out &&
3072
3073                 git rev-parse >actual \
3074                         HEAD:y/b HEAD:y/c HEAD:x/d &&
3075                 git rev-parse >expect \
3076                         O:z/b    O:z/c    A:z/d &&
3077                 test_cmp expect actual
3078         )
3079 '
3080
3081 ###########################################################################
3082 # Rules suggested by section 9:
3083 #
3084 #   If the other side of history did a directory rename to a path that your
3085 #   side renamed away, then ignore that particular rename from the other
3086 #   side of history for any implicit directory renames.
3087 ###########################################################################
3088
3089 ###########################################################################
3090 # SECTION 10: Handling untracked files
3091 #
3092 # unpack_trees(), upon which the recursive merge algorithm is based, aborts
3093 # the operation if untracked or dirty files would be deleted or overwritten
3094 # by the merge.  Unfortunately, unpack_trees() does not understand renames,
3095 # and if it doesn't abort, then it muddies up the working directory before
3096 # we even get to the point of detecting renames, so we need some special
3097 # handling, at least in the case of directory renames.
3098 ###########################################################################
3099
3100 # Testcase 10a, Overwrite untracked: normal rename/delete
3101 #   Commit O: z/{b,c_1}
3102 #   Commit A: z/b + untracked z/c + untracked z/d
3103 #   Commit B: z/{b,d_1}
3104 #   Expected: Aborted Merge +
3105 #       ERROR_MSG(untracked working tree files would be overwritten by merge)
3106
3107 test_setup_10a () {
3108         test_create_repo 10a &&
3109         (
3110                 cd 10a &&
3111
3112                 mkdir z &&
3113                 echo b >z/b &&
3114                 echo c >z/c &&
3115                 git add z &&
3116                 test_tick &&
3117                 git commit -m "O" &&
3118
3119                 git branch O &&
3120                 git branch A &&
3121                 git branch B &&
3122
3123                 git checkout A &&
3124                 git rm z/c &&
3125                 test_tick &&
3126                 git commit -m "A" &&
3127
3128                 git checkout B &&
3129                 git mv z/c z/d &&
3130                 test_tick &&
3131                 git commit -m "B"
3132         )
3133 }
3134
3135 test_expect_success '10a: Overwrite untracked with normal rename/delete' '
3136         test_setup_10a &&
3137         (
3138                 cd 10a &&
3139
3140                 git checkout A^0 &&
3141                 echo very >z/c &&
3142                 echo important >z/d &&
3143
3144                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3145                 test_i18ngrep "The following untracked working tree files would be overwritten by merge" err &&
3146
3147                 git ls-files -s >out &&
3148                 test_line_count = 1 out &&
3149                 git ls-files -o >out &&
3150                 test_line_count = 4 out &&
3151
3152                 echo very >expect &&
3153                 test_cmp expect z/c &&
3154
3155                 echo important >expect &&
3156                 test_cmp expect z/d &&
3157
3158                 git rev-parse HEAD:z/b >actual &&
3159                 git rev-parse O:z/b >expect &&
3160                 test_cmp expect actual
3161         )
3162 '
3163
3164 # Testcase 10b, Overwrite untracked: dir rename + delete
3165 #   Commit O: z/{b,c_1}
3166 #   Commit A: y/b + untracked y/{c,d,e}
3167 #   Commit B: z/{b,d_1,e}
3168 #   Expected: Failed Merge; y/b + untracked y/c + untracked y/d on disk +
3169 #             z/c_1 -> z/d_1 rename recorded at stage 3 for y/d +
3170 #       ERROR_MSG(refusing to lose untracked file at 'y/d')
3171
3172 test_setup_10b () {
3173         test_create_repo 10b &&
3174         (
3175                 cd 10b &&
3176
3177                 mkdir z &&
3178                 echo b >z/b &&
3179                 echo c >z/c &&
3180                 git add z &&
3181                 test_tick &&
3182                 git commit -m "O" &&
3183
3184                 git branch O &&
3185                 git branch A &&
3186                 git branch B &&
3187
3188                 git checkout A &&
3189                 git rm z/c &&
3190                 git mv z/ y/ &&
3191                 test_tick &&
3192                 git commit -m "A" &&
3193
3194                 git checkout B &&
3195                 git mv z/c z/d &&
3196                 echo e >z/e &&
3197                 git add z/e &&
3198                 test_tick &&
3199                 git commit -m "B"
3200         )
3201 }
3202
3203 test_expect_success '10b: Overwrite untracked with dir rename + delete' '
3204         test_setup_10b &&
3205         (
3206                 cd 10b &&
3207
3208                 git checkout A^0 &&
3209                 echo very >y/c &&
3210                 echo important >y/d &&
3211                 echo contents >y/e &&
3212
3213                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3214                 test_i18ngrep "CONFLICT (rename/delete).*Version B\^0 of y/d left in tree at y/d~B\^0" out &&
3215                 test_i18ngrep "Error: Refusing to lose untracked file at y/e; writing to y/e~B\^0 instead" out &&
3216
3217                 git ls-files -s >out &&
3218                 test_line_count = 3 out &&
3219                 git ls-files -u >out &&
3220                 test_line_count = 2 out &&
3221                 git ls-files -o >out &&
3222                 test_line_count = 5 out &&
3223
3224                 git rev-parse >actual \
3225                         :0:y/b :3:y/d :3:y/e &&
3226                 git rev-parse >expect \
3227                         O:z/b  O:z/c  B:z/e &&
3228                 test_cmp expect actual &&
3229
3230                 echo very >expect &&
3231                 test_cmp expect y/c &&
3232
3233                 echo important >expect &&
3234                 test_cmp expect y/d &&
3235
3236                 echo contents >expect &&
3237                 test_cmp expect y/e
3238         )
3239 '
3240
3241 # Testcase 10c, Overwrite untracked: dir rename/rename(1to2)
3242 #   Commit O: z/{a,b}, x/{c,d}
3243 #   Commit A: y/{a,b}, w/c, x/d + different untracked y/c
3244 #   Commit B: z/{a,b,c}, x/d
3245 #   Expected: Failed Merge; y/{a,b} + x/d + untracked y/c +
3246 #             CONFLICT(rename/rename) x/c -> w/c vs y/c +
3247 #             y/c~B^0 +
3248 #             ERROR_MSG(Refusing to lose untracked file at y/c)
3249
3250 test_setup_10c () {
3251         test_create_repo 10c_$1 &&
3252         (
3253                 cd 10c_$1 &&
3254
3255                 mkdir z x &&
3256                 echo a >z/a &&
3257                 echo b >z/b &&
3258                 echo c >x/c &&
3259                 echo d >x/d &&
3260                 git add z x &&
3261                 test_tick &&
3262                 git commit -m "O" &&
3263
3264                 git branch O &&
3265                 git branch A &&
3266                 git branch B &&
3267
3268                 git checkout A &&
3269                 mkdir w &&
3270                 git mv x/c w/c &&
3271                 git mv z/ y/ &&
3272                 test_tick &&
3273                 git commit -m "A" &&
3274
3275                 git checkout B &&
3276                 git mv x/c z/ &&
3277                 test_tick &&
3278                 git commit -m "B"
3279         )
3280 }
3281
3282 test_expect_success '10c1: Overwrite untracked with dir rename/rename(1to2)' '
3283         test_setup_10c 1 &&
3284         (
3285                 cd 10c_1 &&
3286
3287                 git checkout A^0 &&
3288                 echo important >y/c &&
3289
3290                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3291                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3292                 test_i18ngrep "Refusing to lose untracked file at y/c; adding as y/c~B\^0 instead" out &&
3293
3294                 git ls-files -s >out &&
3295                 test_line_count = 6 out &&
3296                 git ls-files -u >out &&
3297                 test_line_count = 3 out &&
3298                 git ls-files -o >out &&
3299                 test_line_count = 3 out &&
3300
3301                 git rev-parse >actual \
3302                         :0:y/a :0:y/b :0:x/d :1:x/c :2:w/c :3:y/c &&
3303                 git rev-parse >expect \
3304                          O:z/a  O:z/b  O:x/d  O:x/c  O:x/c  O:x/c &&
3305                 test_cmp expect actual &&
3306
3307                 git hash-object y/c~B^0 >actual &&
3308                 git rev-parse O:x/c >expect &&
3309                 test_cmp expect actual &&
3310
3311                 echo important >expect &&
3312                 test_cmp expect y/c
3313         )
3314 '
3315
3316 test_expect_success '10c2: Overwrite untracked with dir rename/rename(1to2), other direction' '
3317         test_setup_10c 2 &&
3318         (
3319                 cd 10c_2 &&
3320
3321                 git reset --hard &&
3322                 git clean -fdqx &&
3323
3324                 git checkout B^0 &&
3325                 mkdir y &&
3326                 echo important >y/c &&
3327
3328                 test_must_fail git -c merge.directoryRenames=true merge -s recursive A^0 >out 2>err &&
3329                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3330                 test_i18ngrep "Refusing to lose untracked file at y/c; adding as y/c~HEAD instead" out &&
3331
3332                 git ls-files -s >out &&
3333                 test_line_count = 6 out &&
3334                 git ls-files -u >out &&
3335                 test_line_count = 3 out &&
3336                 git ls-files -o >out &&
3337                 test_line_count = 3 out &&
3338
3339                 git rev-parse >actual \
3340                         :0:y/a :0:y/b :0:x/d :1:x/c :3:w/c :2:y/c &&
3341                 git rev-parse >expect \
3342                          O:z/a  O:z/b  O:x/d  O:x/c  O:x/c  O:x/c &&
3343                 test_cmp expect actual &&
3344
3345                 git hash-object y/c~HEAD >actual &&
3346                 git rev-parse O:x/c >expect &&
3347                 test_cmp expect actual &&
3348
3349                 echo important >expect &&
3350                 test_cmp expect y/c
3351         )
3352 '
3353
3354 # Testcase 10d, Delete untracked w/ dir rename/rename(2to1)
3355 #   Commit O: z/{a,b,c_1},        x/{d,e,f_2}
3356 #   Commit A: y/{a,b},            x/{d,e,f_2,wham_1} + untracked y/wham
3357 #   Commit B: z/{a,b,c_1,wham_2}, y/{d,e}
3358 #   Expected: Failed Merge; y/{a,b,d,e} + untracked y/{wham,wham~merged}+
3359 #             CONFLICT(rename/rename) z/c_1 vs x/f_2 -> y/wham
3360 #             ERROR_MSG(Refusing to lose untracked file at y/wham)
3361
3362 test_setup_10d () {
3363         test_create_repo 10d &&
3364         (
3365                 cd 10d &&
3366
3367                 mkdir z x &&
3368                 echo a >z/a &&
3369                 echo b >z/b &&
3370                 echo c >z/c &&
3371                 echo d >x/d &&
3372                 echo e >x/e &&
3373                 echo f >x/f &&
3374                 git add z x &&
3375                 test_tick &&
3376                 git commit -m "O" &&
3377
3378                 git branch O &&
3379                 git branch A &&
3380                 git branch B &&
3381
3382                 git checkout A &&
3383                 git mv z/c x/wham &&
3384                 git mv z/ y/ &&
3385                 test_tick &&
3386                 git commit -m "A" &&
3387
3388                 git checkout B &&
3389                 git mv x/f z/wham &&
3390                 git mv x/ y/ &&
3391                 test_tick &&
3392                 git commit -m "B"
3393         )
3394 }
3395
3396 test_expect_success '10d: Delete untracked with dir rename/rename(2to1)' '
3397         test_setup_10d &&
3398         (
3399                 cd 10d &&
3400
3401                 git checkout A^0 &&
3402                 echo important >y/wham &&
3403
3404                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3405                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3406                 test_i18ngrep "Refusing to lose untracked file at y/wham" out &&
3407
3408                 git ls-files -s >out &&
3409                 test_line_count = 6 out &&
3410                 git ls-files -u >out &&
3411                 test_line_count = 2 out &&
3412                 git ls-files -o >out &&
3413                 test_line_count = 3 out &&
3414
3415                 git rev-parse >actual \
3416                         :0:y/a :0:y/b :0:y/d :0:y/e :2:y/wham :3:y/wham &&
3417                 git rev-parse >expect \
3418                          O:z/a  O:z/b  O:x/d  O:x/e  O:z/c     O:x/f &&
3419                 test_cmp expect actual &&
3420
3421                 test_must_fail git rev-parse :1:y/wham &&
3422
3423                 echo important >expect &&
3424                 test_cmp expect y/wham &&
3425
3426                 # Test that the two-way merge in y/wham~merged is as expected
3427                 git cat-file -p :2:y/wham >expect &&
3428                 git cat-file -p :3:y/wham >other &&
3429                 >empty &&
3430                 test_must_fail git merge-file \
3431                         -L "HEAD" \
3432                         -L "" \
3433                         -L "B^0" \
3434                         expect empty other &&
3435                 test_cmp expect y/wham~merged
3436         )
3437 '
3438
3439 # Testcase 10e, Does git complain about untracked file that's not in the way?
3440 #   Commit O: z/{a,b}
3441 #   Commit A: y/{a,b} + untracked z/c
3442 #   Commit B: z/{a,b,c}
3443 #   Expected: y/{a,b,c} + untracked z/c
3444
3445 test_setup_10e () {
3446         test_create_repo 10e &&
3447         (
3448                 cd 10e &&
3449
3450                 mkdir z &&
3451                 echo a >z/a &&
3452                 echo b >z/b &&
3453                 git add z &&
3454                 test_tick &&
3455                 git commit -m "O" &&
3456
3457                 git branch O &&
3458                 git branch A &&
3459                 git branch B &&
3460
3461                 git checkout A &&
3462                 git mv z/ y/ &&
3463                 test_tick &&
3464                 git commit -m "A" &&
3465
3466                 git checkout B &&
3467                 echo c >z/c &&
3468                 git add z/c &&
3469                 test_tick &&
3470                 git commit -m "B"
3471         )
3472 }
3473
3474 test_expect_failure '10e: Does git complain about untracked file that is not really in the way?' '
3475         test_setup_10e &&
3476         (
3477                 cd 10e &&
3478
3479                 git checkout A^0 &&
3480                 mkdir z &&
3481                 echo random >z/c &&
3482
3483                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3484                 test_i18ngrep ! "following untracked working tree files would be overwritten by merge" err &&
3485
3486                 git ls-files -s >out &&
3487                 test_line_count = 3 out &&
3488                 git ls-files -u >out &&
3489                 test_line_count = 0 out &&
3490                 git ls-files -o >out &&
3491                 test_line_count = 3 out &&
3492
3493                 git rev-parse >actual \
3494                         :0:y/a :0:y/b :0:y/c &&
3495                 git rev-parse >expect \
3496                          O:z/a  O:z/b  B:z/c &&
3497                 test_cmp expect actual &&
3498
3499                 echo random >expect &&
3500                 test_cmp expect z/c
3501         )
3502 '
3503
3504 ###########################################################################
3505 # SECTION 11: Handling dirty (not up-to-date) files
3506 #
3507 # unpack_trees(), upon which the recursive merge algorithm is based, aborts
3508 # the operation if untracked or dirty files would be deleted or overwritten
3509 # by the merge.  Unfortunately, unpack_trees() does not understand renames,
3510 # and if it doesn't abort, then it muddies up the working directory before
3511 # we even get to the point of detecting renames, so we need some special
3512 # handling.  This was true even of normal renames, but there are additional
3513 # codepaths that need special handling with directory renames.  Add
3514 # testcases for both renamed-by-directory-rename-detection and standard
3515 # rename cases.
3516 ###########################################################################
3517
3518 # Testcase 11a, Avoid losing dirty contents with simple rename
3519 #   Commit O: z/{a,b_v1},
3520 #   Commit A: z/{a,c_v1}, and z/c_v1 has uncommitted mods
3521 #   Commit B: z/{a,b_v2}
3522 #   Expected: ERROR_MSG(Refusing to lose dirty file at z/c) +
3523 #             z/a, staged version of z/c has sha1sum matching B:z/b_v2,
3524 #             z/c~HEAD with contents of B:z/b_v2,
3525 #             z/c with uncommitted mods on top of A:z/c_v1
3526
3527 test_setup_11a () {
3528         test_create_repo 11a &&
3529         (
3530                 cd 11a &&
3531
3532                 mkdir z &&
3533                 echo a >z/a &&
3534                 test_seq 1 10 >z/b &&
3535                 git add z &&
3536                 test_tick &&
3537                 git commit -m "O" &&
3538
3539                 git branch O &&
3540                 git branch A &&
3541                 git branch B &&
3542
3543                 git checkout A &&
3544                 git mv z/b z/c &&
3545                 test_tick &&
3546                 git commit -m "A" &&
3547
3548                 git checkout B &&
3549                 echo 11 >>z/b &&
3550                 git add z/b &&
3551                 test_tick &&
3552                 git commit -m "B"
3553         )
3554 }
3555
3556 test_expect_success '11a: Avoid losing dirty contents with simple rename' '
3557         test_setup_11a &&
3558         (
3559                 cd 11a &&
3560
3561                 git checkout A^0 &&
3562                 echo stuff >>z/c &&
3563
3564                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3565                 test_i18ngrep "Refusing to lose dirty file at z/c" out &&
3566
3567                 test_seq 1 10 >expected &&
3568                 echo stuff >>expected &&
3569                 test_cmp expected z/c &&
3570
3571                 git ls-files -s >out &&
3572                 test_line_count = 2 out &&
3573                 git ls-files -u >out &&
3574                 test_line_count = 1 out &&
3575                 git ls-files -o >out &&
3576                 test_line_count = 4 out &&
3577
3578                 git rev-parse >actual \
3579                         :0:z/a :2:z/c &&
3580                 git rev-parse >expect \
3581                          O:z/a  B:z/b &&
3582                 test_cmp expect actual &&
3583
3584                 git hash-object z/c~HEAD >actual &&
3585                 git rev-parse B:z/b >expect &&
3586                 test_cmp expect actual
3587         )
3588 '
3589
3590 # Testcase 11b, Avoid losing dirty file involved in directory rename
3591 #   Commit O: z/a,         x/{b,c_v1}
3592 #   Commit A: z/{a,c_v1},  x/b,       and z/c_v1 has uncommitted mods
3593 #   Commit B: y/a,         x/{b,c_v2}
3594 #   Expected: y/{a,c_v2}, x/b, z/c_v1 with uncommitted mods untracked,
3595 #             ERROR_MSG(Refusing to lose dirty file at z/c)
3596
3597
3598 test_setup_11b () {
3599         test_create_repo 11b &&
3600         (
3601                 cd 11b &&
3602
3603                 mkdir z x &&
3604                 echo a >z/a &&
3605                 echo b >x/b &&
3606                 test_seq 1 10 >x/c &&
3607                 git add z x &&
3608                 test_tick &&
3609                 git commit -m "O" &&
3610
3611                 git branch O &&
3612                 git branch A &&
3613                 git branch B &&
3614
3615                 git checkout A &&
3616                 git mv x/c z/c &&
3617                 test_tick &&
3618                 git commit -m "A" &&
3619
3620                 git checkout B &&
3621                 git mv z y &&
3622                 echo 11 >>x/c &&
3623                 git add x/c &&
3624                 test_tick &&
3625                 git commit -m "B"
3626         )
3627 }
3628
3629 test_expect_success '11b: Avoid losing dirty file involved in directory rename' '
3630         test_setup_11b &&
3631         (
3632                 cd 11b &&
3633
3634                 git checkout A^0 &&
3635                 echo stuff >>z/c &&
3636
3637                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3638                 test_i18ngrep "Refusing to lose dirty file at z/c" out &&
3639
3640                 grep -q stuff z/c &&
3641                 test_seq 1 10 >expected &&
3642                 echo stuff >>expected &&
3643                 test_cmp expected z/c &&
3644
3645                 git ls-files -s >out &&
3646                 test_line_count = 3 out &&
3647                 git ls-files -u >out &&
3648                 test_line_count = 0 out &&
3649                 git ls-files -m >out &&
3650                 test_line_count = 0 out &&
3651                 git ls-files -o >out &&
3652                 test_line_count = 4 out &&
3653
3654                 git rev-parse >actual \
3655                         :0:x/b :0:y/a :0:y/c &&
3656                 git rev-parse >expect \
3657                          O:x/b  O:z/a  B:x/c &&
3658                 test_cmp expect actual &&
3659
3660                 git hash-object y/c >actual &&
3661                 git rev-parse B:x/c >expect &&
3662                 test_cmp expect actual
3663         )
3664 '
3665
3666 # Testcase 11c, Avoid losing not-up-to-date with rename + D/F conflict
3667 #   Commit O: y/a,         x/{b,c_v1}
3668 #   Commit A: y/{a,c_v1},  x/b,       and y/c_v1 has uncommitted mods
3669 #   Commit B: y/{a,c/d},   x/{b,c_v2}
3670 #   Expected: Abort_msg("following files would be overwritten by merge") +
3671 #             y/c left untouched (still has uncommitted mods)
3672
3673 test_setup_11c () {
3674         test_create_repo 11c &&
3675         (
3676                 cd 11c &&
3677
3678                 mkdir y x &&
3679                 echo a >y/a &&
3680                 echo b >x/b &&
3681                 test_seq 1 10 >x/c &&
3682                 git add y x &&
3683                 test_tick &&
3684                 git commit -m "O" &&
3685
3686                 git branch O &&
3687                 git branch A &&
3688                 git branch B &&
3689
3690                 git checkout A &&
3691                 git mv x/c y/c &&
3692                 test_tick &&
3693                 git commit -m "A" &&
3694
3695                 git checkout B &&
3696                 mkdir y/c &&
3697                 echo d >y/c/d &&
3698                 echo 11 >>x/c &&
3699                 git add x/c y/c/d &&
3700                 test_tick &&
3701                 git commit -m "B"
3702         )
3703 }
3704
3705 test_expect_success '11c: Avoid losing not-uptodate with rename + D/F conflict' '
3706         test_setup_11c &&
3707         (
3708                 cd 11c &&
3709
3710                 git checkout A^0 &&
3711                 echo stuff >>y/c &&
3712
3713                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3714                 test_i18ngrep "following files would be overwritten by merge" err &&
3715
3716                 grep -q stuff y/c &&
3717                 test_seq 1 10 >expected &&
3718                 echo stuff >>expected &&
3719                 test_cmp expected y/c &&
3720
3721                 git ls-files -s >out &&
3722                 test_line_count = 3 out &&
3723                 git ls-files -u >out &&
3724                 test_line_count = 0 out &&
3725                 git ls-files -m >out &&
3726                 test_line_count = 1 out &&
3727                 git ls-files -o >out &&
3728                 test_line_count = 3 out
3729         )
3730 '
3731
3732 # Testcase 11d, Avoid losing not-up-to-date with rename + D/F conflict
3733 #   Commit O: z/a,         x/{b,c_v1}
3734 #   Commit A: z/{a,c_v1},  x/b,       and z/c_v1 has uncommitted mods
3735 #   Commit B: y/{a,c/d},   x/{b,c_v2}
3736 #   Expected: D/F: y/c_v2 vs y/c/d) +
3737 #             Warning_Msg("Refusing to lose dirty file at z/c) +
3738 #             y/{a,c~HEAD,c/d}, x/b, now-untracked z/c_v1 with uncommitted mods
3739
3740 test_setup_11d () {
3741         test_create_repo 11d &&
3742         (
3743                 cd 11d &&
3744
3745                 mkdir z x &&
3746                 echo a >z/a &&
3747                 echo b >x/b &&
3748                 test_seq 1 10 >x/c &&
3749                 git add z x &&
3750                 test_tick &&
3751                 git commit -m "O" &&
3752
3753                 git branch O &&
3754                 git branch A &&
3755                 git branch B &&
3756
3757                 git checkout A &&
3758                 git mv x/c z/c &&
3759                 test_tick &&
3760                 git commit -m "A" &&
3761
3762                 git checkout B &&
3763                 git mv z y &&
3764                 mkdir y/c &&
3765                 echo d >y/c/d &&
3766                 echo 11 >>x/c &&
3767                 git add x/c y/c/d &&
3768                 test_tick &&
3769                 git commit -m "B"
3770         )
3771 }
3772
3773 test_expect_success '11d: Avoid losing not-uptodate with rename + D/F conflict' '
3774         test_setup_11d &&
3775         (
3776                 cd 11d &&
3777
3778                 git checkout A^0 &&
3779                 echo stuff >>z/c &&
3780
3781                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3782                 test_i18ngrep "Refusing to lose dirty file at z/c" out &&
3783
3784                 grep -q stuff z/c &&
3785                 test_seq 1 10 >expected &&
3786                 echo stuff >>expected &&
3787                 test_cmp expected z/c &&
3788
3789                 git ls-files -s >out &&
3790                 test_line_count = 4 out &&
3791                 git ls-files -u >out &&
3792                 test_line_count = 1 out &&
3793                 git ls-files -o >out &&
3794                 test_line_count = 5 out &&
3795
3796                 git rev-parse >actual \
3797                         :0:x/b :0:y/a :0:y/c/d :3:y/c &&
3798                 git rev-parse >expect \
3799                          O:x/b  O:z/a  B:y/c/d  B:x/c &&
3800                 test_cmp expect actual &&
3801
3802                 git hash-object y/c~HEAD >actual &&
3803                 git rev-parse B:x/c >expect &&
3804                 test_cmp expect actual
3805         )
3806 '
3807
3808 # Testcase 11e, Avoid deleting not-up-to-date with dir rename/rename(1to2)/add
3809 #   Commit O: z/{a,b},      x/{c_1,d}
3810 #   Commit A: y/{a,b,c_2},  x/d, w/c_1, and y/c_2 has uncommitted mods
3811 #   Commit B: z/{a,b,c_1},  x/d
3812 #   Expected: Failed Merge; y/{a,b} + x/d +
3813 #             CONFLICT(rename/rename) x/c_1 -> w/c_1 vs y/c_1 +
3814 #             ERROR_MSG(Refusing to lose dirty file at y/c)
3815 #             y/c~B^0 has O:x/c_1 contents
3816 #             y/c~HEAD has A:y/c_2 contents
3817 #             y/c has dirty file from before merge
3818
3819 test_setup_11e () {
3820         test_create_repo 11e &&
3821         (
3822                 cd 11e &&
3823
3824                 mkdir z x &&
3825                 echo a >z/a &&
3826                 echo b >z/b &&
3827                 echo c >x/c &&
3828                 echo d >x/d &&
3829                 git add z x &&
3830                 test_tick &&
3831                 git commit -m "O" &&
3832
3833                 git branch O &&
3834                 git branch A &&
3835                 git branch B &&
3836
3837                 git checkout A &&
3838                 git mv z/ y/ &&
3839                 echo different >y/c &&
3840                 mkdir w &&
3841                 git mv x/c w/ &&
3842                 git add y/c &&
3843                 test_tick &&
3844                 git commit -m "A" &&
3845
3846                 git checkout B &&
3847                 git mv x/c z/ &&
3848                 test_tick &&
3849                 git commit -m "B"
3850         )
3851 }
3852
3853 test_expect_success '11e: Avoid deleting not-uptodate with dir rename/rename(1to2)/add' '
3854         test_setup_11e &&
3855         (
3856                 cd 11e &&
3857
3858                 git checkout A^0 &&
3859                 echo mods >>y/c &&
3860
3861                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3862                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3863                 test_i18ngrep "Refusing to lose dirty file at y/c" out &&
3864
3865                 git ls-files -s >out &&
3866                 test_line_count = 7 out &&
3867                 git ls-files -u >out &&
3868                 test_line_count = 4 out &&
3869                 git ls-files -o >out &&
3870                 test_line_count = 3 out &&
3871
3872                 echo different >expected &&
3873                 echo mods >>expected &&
3874                 test_cmp expected y/c &&
3875
3876                 git rev-parse >actual \
3877                         :0:y/a :0:y/b :0:x/d :1:x/c :2:w/c :2:y/c :3:y/c &&
3878                 git rev-parse >expect \
3879                          O:z/a  O:z/b  O:x/d  O:x/c  O:x/c  A:y/c  O:x/c &&
3880                 test_cmp expect actual &&
3881
3882                 # See if y/c~merged has expected contents; requires manually
3883                 # doing the expected file merge
3884                 git cat-file -p A:y/c >c1 &&
3885                 git cat-file -p B:z/c >c2 &&
3886                 >empty &&
3887                 test_must_fail git merge-file \
3888                         -L "HEAD" \
3889                         -L "" \
3890                         -L "B^0" \
3891                         c1 empty c2 &&
3892                 test_cmp c1 y/c~merged
3893         )
3894 '
3895
3896 # Testcase 11f, Avoid deleting not-up-to-date w/ dir rename/rename(2to1)
3897 #   Commit O: z/{a,b},        x/{c_1,d_2}
3898 #   Commit A: y/{a,b,wham_1}, x/d_2, except y/wham has uncommitted mods
3899 #   Commit B: z/{a,b,wham_2}, x/c_1
3900 #   Expected: Failed Merge; y/{a,b} + untracked y/{wham~merged} +
3901 #             y/wham with dirty changes from before merge +
3902 #             CONFLICT(rename/rename) x/c vs x/d -> y/wham
3903 #             ERROR_MSG(Refusing to lose dirty file at y/wham)
3904
3905 test_setup_11f () {
3906         test_create_repo 11f &&
3907         (
3908                 cd 11f &&
3909
3910                 mkdir z x &&
3911                 echo a >z/a &&
3912                 echo b >z/b &&
3913                 test_seq 1 10 >x/c &&
3914                 echo d >x/d &&
3915                 git add z x &&
3916                 test_tick &&
3917                 git commit -m "O" &&
3918
3919                 git branch O &&
3920                 git branch A &&
3921                 git branch B &&
3922
3923                 git checkout A &&
3924                 git mv z/ y/ &&
3925                 git mv x/c y/wham &&
3926                 test_tick &&
3927                 git commit -m "A" &&
3928
3929                 git checkout B &&
3930                 git mv x/d z/wham &&
3931                 test_tick &&
3932                 git commit -m "B"
3933         )
3934 }
3935
3936 test_expect_success '11f: Avoid deleting not-uptodate with dir rename/rename(2to1)' '
3937         test_setup_11f &&
3938         (
3939                 cd 11f &&
3940
3941                 git checkout A^0 &&
3942                 echo important >>y/wham &&
3943
3944                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3945                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3946                 test_i18ngrep "Refusing to lose dirty file at y/wham" out &&
3947
3948                 git ls-files -s >out &&
3949                 test_line_count = 4 out &&
3950                 git ls-files -u >out &&
3951                 test_line_count = 2 out &&
3952                 git ls-files -o >out &&
3953                 test_line_count = 3 out &&
3954
3955                 test_seq 1 10 >expected &&
3956                 echo important >>expected &&
3957                 test_cmp expected y/wham &&
3958
3959                 test_must_fail git rev-parse :1:y/wham &&
3960
3961                 git rev-parse >actual \
3962                         :0:y/a :0:y/b :2:y/wham :3:y/wham &&
3963                 git rev-parse >expect \
3964                          O:z/a  O:z/b  O:x/c     O:x/d &&
3965                 test_cmp expect actual &&
3966
3967                 # Test that the two-way merge in y/wham~merged is as expected
3968                 git cat-file -p :2:y/wham >expect &&
3969                 git cat-file -p :3:y/wham >other &&
3970                 >empty &&
3971                 test_must_fail git merge-file \
3972                         -L "HEAD" \
3973                         -L "" \
3974                         -L "B^0" \
3975                         expect empty other &&
3976                 test_cmp expect y/wham~merged
3977         )
3978 '
3979
3980 ###########################################################################
3981 # SECTION 12: Everything else
3982 #
3983 # Tests suggested by others.  Tests added after implementation completed
3984 # and submitted.  Grab bag.
3985 ###########################################################################
3986
3987 # Testcase 12a, Moving one directory hierarchy into another
3988 #   (Related to testcase 9a)
3989 #   Commit O: node1/{leaf1,leaf2}, node2/{leaf3,leaf4}
3990 #   Commit A: node1/{leaf1,leaf2,node2/{leaf3,leaf4}}
3991 #   Commit B: node1/{leaf1,leaf2,leaf5}, node2/{leaf3,leaf4,leaf6}
3992 #   Expected: node1/{leaf1,leaf2,leaf5,node2/{leaf3,leaf4,leaf6}}
3993
3994 test_setup_12a () {
3995         test_create_repo 12a &&
3996         (
3997                 cd 12a &&
3998
3999                 mkdir -p node1 node2 &&
4000                 echo leaf1 >node1/leaf1 &&
4001                 echo leaf2 >node1/leaf2 &&
4002                 echo leaf3 >node2/leaf3 &&
4003                 echo leaf4 >node2/leaf4 &&
4004                 git add node1 node2 &&
4005                 test_tick &&
4006                 git commit -m "O" &&
4007
4008                 git branch O &&
4009                 git branch A &&
4010                 git branch B &&
4011
4012                 git checkout A &&
4013                 git mv node2/ node1/ &&
4014                 test_tick &&
4015                 git commit -m "A" &&
4016
4017                 git checkout B &&
4018                 echo leaf5 >node1/leaf5 &&
4019                 echo leaf6 >node2/leaf6 &&
4020                 git add node1 node2 &&
4021                 test_tick &&
4022                 git commit -m "B"
4023         )
4024 }
4025
4026 test_expect_success '12a: Moving one directory hierarchy into another' '
4027         test_setup_12a &&
4028         (
4029                 cd 12a &&
4030
4031                 git checkout A^0 &&
4032
4033                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4034
4035                 git ls-files -s >out &&
4036                 test_line_count = 6 out &&
4037
4038                 git rev-parse >actual \
4039                         HEAD:node1/leaf1 HEAD:node1/leaf2 HEAD:node1/leaf5 \
4040                         HEAD:node1/node2/leaf3 \
4041                         HEAD:node1/node2/leaf4 \
4042                         HEAD:node1/node2/leaf6 &&
4043                 git rev-parse >expect \
4044                         O:node1/leaf1    O:node1/leaf2    B:node1/leaf5 \
4045                         O:node2/leaf3 \
4046                         O:node2/leaf4 \
4047                         B:node2/leaf6 &&
4048                 test_cmp expect actual
4049         )
4050 '
4051
4052 # Testcase 12b1, Moving two directory hierarchies into each other
4053 #   (Related to testcases 1c and 12c)
4054 #   Commit O: node1/{leaf1, leaf2}, node2/{leaf3, leaf4}
4055 #   Commit A: node1/{leaf1, leaf2, node2/{leaf3, leaf4}}
4056 #   Commit B: node2/{leaf3, leaf4, node1/{leaf1, leaf2}}
4057 #   Expected: node1/node2/{leaf3, leaf4}
4058 #             node2/node1/{leaf1, leaf2}
4059 #   NOTE: If there were new files added to the old node1/ or node2/ directories,
4060 #         then we would need to detect renames for those directories and would
4061 #         find that:
4062 #             commit A renames node2/ -> node1/node2/
4063 #             commit B renames node1/ -> node2/node1/
4064 #         Applying those directory renames to the initial result (making all
4065 #         four paths experience a transitive renaming), yields
4066 #             node1/node2/node1/{leaf1, leaf2}
4067 #             node2/node1/node2/{leaf3, leaf4}
4068 #         as the result.  It may be really weird to have two directories
4069 #         rename each other, but simple rules give weird results when given
4070 #         weird inputs.  HOWEVER, the "If" at the beginning of those NOTE was
4071 #         false; there were no new files added and thus there is no directory
4072 #         rename detection to perform.  As such, we just have simple renames
4073 #         and the expected answer is:
4074 #             node1/node2/{leaf3, leaf4}
4075 #             node2/node1/{leaf1, leaf2}
4076
4077 test_setup_12b1 () {
4078         test_create_repo 12b1 &&
4079         (
4080                 cd 12b1 &&
4081
4082                 mkdir -p node1 node2 &&
4083                 echo leaf1 >node1/leaf1 &&
4084                 echo leaf2 >node1/leaf2 &&
4085                 echo leaf3 >node2/leaf3 &&
4086                 echo leaf4 >node2/leaf4 &&
4087                 git add node1 node2 &&
4088                 test_tick &&
4089                 git commit -m "O" &&
4090
4091                 git branch O &&
4092                 git branch A &&
4093                 git branch B &&
4094
4095                 git checkout A &&
4096                 git mv node2/ node1/ &&
4097                 test_tick &&
4098                 git commit -m "A" &&
4099
4100                 git checkout B &&
4101                 git mv node1/ node2/ &&
4102                 test_tick &&
4103                 git commit -m "B"
4104         )
4105 }
4106
4107 test_expect_failure '12b1: Moving two directory hierarchies into each other' '
4108         test_setup_12b1 &&
4109         (
4110                 cd 12b1 &&
4111
4112                 git checkout A^0 &&
4113
4114                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4115
4116                 git ls-files -s >out &&
4117                 test_line_count = 4 out &&
4118
4119                 git rev-parse >actual \
4120                         HEAD:node2/node1/leaf1 \
4121                         HEAD:node2/node1/leaf2 \
4122                         HEAD:node1/node2/leaf3 \
4123                         HEAD:node1/node2/leaf4 &&
4124                 git rev-parse >expect \
4125                         O:node1/leaf1 \
4126                         O:node1/leaf2 \
4127                         O:node2/leaf3 \
4128                         O:node2/leaf4 &&
4129                 test_cmp expect actual
4130         )
4131 '
4132
4133 # Testcase 12b2, Moving two directory hierarchies into each other
4134 #   (Related to testcases 1c and 12c)
4135 #   Commit O: node1/{leaf1, leaf2}, node2/{leaf3, leaf4}
4136 #   Commit A: node1/{leaf1, leaf2, leaf5, node2/{leaf3, leaf4}}
4137 #   Commit B: node2/{leaf3, leaf4, leaf6, node1/{leaf1, leaf2}}
4138 #   Expected: node1/node2/{node1/{leaf1, leaf2}, leaf6}
4139 #             node2/node1/{node2/{leaf3, leaf4}, leaf5}
4140 #   NOTE: Without directory renames, we would expect
4141 #             A: node2/leaf3 -> node1/node2/leaf3
4142 #             A: node2/leaf1 -> node1/node2/leaf4
4143 #             A: Adds           node1/leaf5
4144 #             B: node1/leaf1 -> node2/node1/leaf1
4145 #             B: node1/leaf2 -> node2/node1/leaf2
4146 #             B: Adds           node2/leaf6
4147 #         with directory rename detection, we note that
4148 #             commit A renames node2/ -> node1/node2/
4149 #             commit B renames node1/ -> node2/node1/
4150 #         therefore, applying A's directory rename to the paths added in B gives:
4151 #             B: node1/leaf1 -> node1/node2/node1/leaf1
4152 #             B: node1/leaf2 -> node1/node2/node1/leaf2
4153 #             B: Adds           node1/node2/leaf6
4154 #         and applying B's directory rename to the paths added in A gives:
4155 #             A: node2/leaf3 -> node2/node1/node2/leaf3
4156 #             A: node2/leaf1 -> node2/node1/node2/leaf4
4157 #             A: Adds           node2/node1/leaf5
4158 #         resulting in the expected
4159 #             node1/node2/{node1/{leaf1, leaf2}, leaf6}
4160 #             node2/node1/{node2/{leaf3, leaf4}, leaf5}
4161 #
4162 #         You may ask, is it weird to have two directories rename each other?
4163 #         To which, I can do no more than shrug my shoulders and say that
4164 #         even simple rules give weird results when given weird inputs.
4165
4166 test_setup_12b2 () {
4167         test_create_repo 12b2 &&
4168         (
4169                 cd 12b2 &&
4170
4171                 mkdir -p node1 node2 &&
4172                 echo leaf1 >node1/leaf1 &&
4173                 echo leaf2 >node1/leaf2 &&
4174                 echo leaf3 >node2/leaf3 &&
4175                 echo leaf4 >node2/leaf4 &&
4176                 git add node1 node2 &&
4177                 test_tick &&
4178                 git commit -m "O" &&
4179
4180                 git branch O &&
4181                 git branch A &&
4182                 git branch B &&
4183
4184                 git checkout A &&
4185                 git mv node2/ node1/ &&
4186                 echo leaf5 >node1/leaf5 &&
4187                 git add node1/leaf5 &&
4188                 test_tick &&
4189                 git commit -m "A" &&
4190
4191                 git checkout B &&
4192                 git mv node1/ node2/ &&
4193                 echo leaf6 >node2/leaf6 &&
4194                 git add node2/leaf6 &&
4195                 test_tick &&
4196                 git commit -m "B"
4197         )
4198 }
4199
4200 test_expect_success '12b2: Moving two directory hierarchies into each other' '
4201         test_setup_12b2 &&
4202         (
4203                 cd 12b2 &&
4204
4205                 git checkout A^0 &&
4206
4207                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4208
4209                 git ls-files -s >out &&
4210                 test_line_count = 6 out &&
4211
4212                 git rev-parse >actual \
4213                         HEAD:node1/node2/node1/leaf1 \
4214                         HEAD:node1/node2/node1/leaf2 \
4215                         HEAD:node2/node1/node2/leaf3 \
4216                         HEAD:node2/node1/node2/leaf4 \
4217                         HEAD:node2/node1/leaf5       \
4218                         HEAD:node1/node2/leaf6       &&
4219                 git rev-parse >expect \
4220                         O:node1/leaf1 \
4221                         O:node1/leaf2 \
4222                         O:node2/leaf3 \
4223                         O:node2/leaf4 \
4224                         A:node1/leaf5 \
4225                         B:node2/leaf6 &&
4226                 test_cmp expect actual
4227         )
4228 '
4229
4230 # Testcase 12c1, Moving two directory hierarchies into each other w/ content merge
4231 #   (Related to testcase 12b)
4232 #   Commit O: node1/{       leaf1_1, leaf2_1}, node2/{leaf3_1, leaf4_1}
4233 #   Commit A: node1/{       leaf1_2, leaf2_2,  node2/{leaf3_2, leaf4_2}}
4234 #   Commit B: node2/{node1/{leaf1_3, leaf2_3},        leaf3_3, leaf4_3}
4235 #   Expected: Content merge conflicts for each of:
4236 #               node1/node2/node1/{leaf1, leaf2},
4237 #               node2/node1/node2/{leaf3, leaf4}
4238 #   NOTE: This is *exactly* like 12b1, except that every path is modified on
4239 #         each side of the merge.
4240
4241 test_setup_12c1 () {
4242         test_create_repo 12c1 &&
4243         (
4244                 cd 12c1 &&
4245
4246                 mkdir -p node1 node2 &&
4247                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf1\n" >node1/leaf1 &&
4248                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf2\n" >node1/leaf2 &&
4249                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf3\n" >node2/leaf3 &&
4250                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf4\n" >node2/leaf4 &&
4251                 git add node1 node2 &&
4252                 test_tick &&
4253                 git commit -m "O" &&
4254
4255                 git branch O &&
4256                 git branch A &&
4257                 git branch B &&
4258
4259                 git checkout A &&
4260                 git mv node2/ node1/ &&
4261                 for i in `git ls-files`; do echo side A >>$i; done &&
4262                 git add -u &&
4263                 test_tick &&
4264                 git commit -m "A" &&
4265
4266                 git checkout B &&
4267                 git mv node1/ node2/ &&
4268                 for i in `git ls-files`; do echo side B >>$i; done &&
4269                 git add -u &&
4270                 test_tick &&
4271                 git commit -m "B"
4272         )
4273 }
4274
4275 test_expect_failure '12c1: Moving one directory hierarchy into another w/ content merge' '
4276         test_setup_12c1 &&
4277         (
4278                 cd 12c1 &&
4279
4280                 git checkout A^0 &&
4281
4282                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 &&
4283
4284                 git ls-files -u >out &&
4285                 test_line_count = 12 out &&
4286
4287                 git rev-parse >actual \
4288                         :1:node2/node1/leaf1 \
4289                         :1:node2/node1/leaf2 \
4290                         :1:node1/node2/leaf3 \
4291                         :1:node1/node2/leaf4 \
4292                         :2:node2/node1/leaf1 \
4293                         :2:node2/node1/leaf2 \
4294                         :2:node1/node2/leaf3 \
4295                         :2:node1/node2/leaf4 \
4296                         :3:node2/node1/leaf1 \
4297                         :3:node2/node1/leaf2 \
4298                         :3:node1/node2/leaf3 \
4299                         :3:node1/node2/leaf4 &&
4300                 git rev-parse >expect \
4301                         O:node1/leaf1 \
4302                         O:node1/leaf2 \
4303                         O:node2/leaf3 \
4304                         O:node2/leaf4 \
4305                         A:node1/leaf1 \
4306                         A:node1/leaf2 \
4307                         A:node1/node2/leaf3 \
4308                         A:node1/node2/leaf4 \
4309                         B:node2/node1/leaf1 \
4310                         B:node2/node1/leaf2 \
4311                         B:node2/leaf3 \
4312                         B:node2/leaf4 &&
4313                 test_cmp expect actual
4314         )
4315 '
4316
4317 # Testcase 12c2, Moving two directory hierarchies into each other w/ content merge
4318 #   (Related to testcase 12b)
4319 #   Commit O: node1/{       leaf1_1, leaf2_1}, node2/{leaf3_1, leaf4_1}
4320 #   Commit A: node1/{       leaf1_2, leaf2_2,  node2/{leaf3_2, leaf4_2}, leaf5}
4321 #   Commit B: node2/{node1/{leaf1_3, leaf2_3},        leaf3_3, leaf4_3,  leaf6}
4322 #   Expected: Content merge conflicts for each of:
4323 #               node1/node2/node1/{leaf1, leaf2}
4324 #               node2/node1/node2/{leaf3, leaf4}
4325 #             plus
4326 #               node2/node1/leaf5
4327 #               node1/node2/leaf6
4328 #   NOTE: This is *exactly* like 12b2, except that every path from O is modified
4329 #         on each side of the merge.
4330
4331 test_setup_12c2 () {
4332         test_create_repo 12c2 &&
4333         (
4334                 cd 12c2 &&
4335
4336                 mkdir -p node1 node2 &&
4337                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf1\n" >node1/leaf1 &&
4338                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf2\n" >node1/leaf2 &&
4339                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf3\n" >node2/leaf3 &&
4340                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf4\n" >node2/leaf4 &&
4341                 git add node1 node2 &&
4342                 test_tick &&
4343                 git commit -m "O" &&
4344
4345                 git branch O &&
4346                 git branch A &&
4347                 git branch B &&
4348
4349                 git checkout A &&
4350                 git mv node2/ node1/ &&
4351                 for i in `git ls-files`; do echo side A >>$i; done &&
4352                 git add -u &&
4353                 echo leaf5 >node1/leaf5 &&
4354                 git add node1/leaf5 &&
4355                 test_tick &&
4356                 git commit -m "A" &&
4357
4358                 git checkout B &&
4359                 git mv node1/ node2/ &&
4360                 for i in `git ls-files`; do echo side B >>$i; done &&
4361                 git add -u &&
4362                 echo leaf6 >node2/leaf6 &&
4363                 git add node2/leaf6 &&
4364                 test_tick &&
4365                 git commit -m "B"
4366         )
4367 }
4368
4369 test_expect_success '12c2: Moving one directory hierarchy into another w/ content merge' '
4370         test_setup_12c2 &&
4371         (
4372                 cd 12c2 &&
4373
4374                 git checkout A^0 &&
4375
4376                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 &&
4377
4378                 git ls-files -s >out &&
4379                 test_line_count = 14 out &&
4380                 git ls-files -u >out &&
4381                 test_line_count = 12 out &&
4382
4383                 git rev-parse >actual \
4384                         :1:node1/node2/node1/leaf1 \
4385                         :1:node1/node2/node1/leaf2 \
4386                         :1:node2/node1/node2/leaf3 \
4387                         :1:node2/node1/node2/leaf4 \
4388                         :2:node1/node2/node1/leaf1 \
4389                         :2:node1/node2/node1/leaf2 \
4390                         :2:node2/node1/node2/leaf3 \
4391                         :2:node2/node1/node2/leaf4 \
4392                         :3:node1/node2/node1/leaf1 \
4393                         :3:node1/node2/node1/leaf2 \
4394                         :3:node2/node1/node2/leaf3 \
4395                         :3:node2/node1/node2/leaf4 \
4396                         :0:node2/node1/leaf5       \
4397                         :0:node1/node2/leaf6       &&
4398                 git rev-parse >expect \
4399                         O:node1/leaf1 \
4400                         O:node1/leaf2 \
4401                         O:node2/leaf3 \
4402                         O:node2/leaf4 \
4403                         A:node1/leaf1 \
4404                         A:node1/leaf2 \
4405                         A:node1/node2/leaf3 \
4406                         A:node1/node2/leaf4 \
4407                         B:node2/node1/leaf1 \
4408                         B:node2/node1/leaf2 \
4409                         B:node2/leaf3 \
4410                         B:node2/leaf4 \
4411                         A:node1/leaf5 \
4412                         B:node2/leaf6 &&
4413                 test_cmp expect actual
4414         )
4415 '
4416
4417 # Testcase 12d, Rename/merge of subdirectory into the root
4418 #   Commit O: a/b/subdir/foo
4419 #   Commit A: subdir/foo
4420 #   Commit B: a/b/subdir/foo, a/b/bar
4421 #   Expected: subdir/foo, bar
4422
4423 test_setup_12d () {
4424         test_create_repo 12d &&
4425         (
4426                 cd 12d &&
4427
4428                 mkdir -p a/b/subdir &&
4429                 test_commit a/b/subdir/foo &&
4430
4431                 git branch O &&
4432                 git branch A &&
4433                 git branch B &&
4434
4435                 git checkout A &&
4436                 mkdir subdir &&
4437                 git mv a/b/subdir/foo.t subdir/foo.t &&
4438                 test_tick &&
4439                 git commit -m "A" &&
4440
4441                 git checkout B &&
4442                 test_commit a/b/bar
4443         )
4444 }
4445
4446 test_expect_success '12d: Rename/merge subdir into the root, variant 1' '
4447         test_setup_12d &&
4448         (
4449                 cd 12d &&
4450
4451                 git checkout A^0 &&
4452
4453                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4454
4455                 git ls-files -s >out &&
4456                 test_line_count = 2 out &&
4457
4458                 git rev-parse >actual \
4459                         HEAD:subdir/foo.t   HEAD:bar.t &&
4460                 git rev-parse >expect \
4461                         O:a/b/subdir/foo.t  B:a/b/bar.t &&
4462                 test_cmp expect actual &&
4463
4464                 git hash-object bar.t >actual &&
4465                 git rev-parse B:a/b/bar.t >expect &&
4466                 test_cmp expect actual &&
4467
4468                 test_must_fail git rev-parse HEAD:a/b/subdir/foo.t &&
4469                 test_must_fail git rev-parse HEAD:a/b/bar.t &&
4470                 test_path_is_missing a/ &&
4471                 test_path_is_file bar.t
4472         )
4473 '
4474
4475 # Testcase 12e, Rename/merge of subdirectory into the root
4476 #   Commit O: a/b/foo
4477 #   Commit A: foo
4478 #   Commit B: a/b/foo, a/b/bar
4479 #   Expected: foo, bar
4480
4481 test_setup_12e () {
4482         test_create_repo 12e &&
4483         (
4484                 cd 12e &&
4485
4486                 mkdir -p a/b &&
4487                 test_commit a/b/foo &&
4488
4489                 git branch O &&
4490                 git branch A &&
4491                 git branch B &&
4492
4493                 git checkout A &&
4494                 mkdir subdir &&
4495                 git mv a/b/foo.t foo.t &&
4496                 test_tick &&
4497                 git commit -m "A" &&
4498
4499                 git checkout B &&
4500                 test_commit a/b/bar
4501         )
4502 }
4503
4504 test_expect_success '12e: Rename/merge subdir into the root, variant 2' '
4505         test_setup_12e &&
4506         (
4507                 cd 12e &&
4508
4509                 git checkout A^0 &&
4510
4511                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4512
4513                 git ls-files -s >out &&
4514                 test_line_count = 2 out &&
4515
4516                 git rev-parse >actual \
4517                         HEAD:foo.t   HEAD:bar.t &&
4518                 git rev-parse >expect \
4519                         O:a/b/foo.t  B:a/b/bar.t &&
4520                 test_cmp expect actual &&
4521
4522                 git hash-object bar.t >actual &&
4523                 git rev-parse B:a/b/bar.t >expect &&
4524                 test_cmp expect actual &&
4525
4526                 test_must_fail git rev-parse HEAD:a/b/foo.t &&
4527                 test_must_fail git rev-parse HEAD:a/b/bar.t &&
4528                 test_path_is_missing a/ &&
4529                 test_path_is_file bar.t
4530         )
4531 '
4532
4533 # Testcase 12f, Rebase of patches with big directory rename
4534 #   Commit O:
4535 #              dir/subdir/{a,b,c,d,e_O,Makefile_TOP_O}
4536 #              dir/subdir/tweaked/{f,g,h,Makefile_SUB_O}
4537 #              dir/unchanged/<LOTS OF FILES>
4538 #   Commit A:
4539 #     (Remove f & g, move e into newsubdir, rename dir/->folder/, modify files)
4540 #              folder/subdir/{a,b,c,d,Makefile_TOP_A}
4541 #              folder/subdir/newsubdir/e_A
4542 #              folder/subdir/tweaked/{h,Makefile_SUB_A}
4543 #              folder/unchanged/<LOTS OF FILES>
4544 #   Commit B1:
4545 #     (add newfile.{c,py}, modify underscored files)
4546 #              dir/{a,b,c,d,e_B1,Makefile_TOP_B1,newfile.c}
4547 #              dir/tweaked/{f,g,h,Makefile_SUB_B1,newfile.py}
4548 #              dir/unchanged/<LOTS OF FILES>
4549 #   Commit B2:
4550 #     (Modify e further, add newfile.rs)
4551 #              dir/{a,b,c,d,e_B2,Makefile_TOP_B1,newfile.c,newfile.rs}
4552 #              dir/tweaked/{f,g,h,Makefile_SUB_B1,newfile.py}
4553 #              dir/unchanged/<LOTS OF FILES>
4554 #   Expected:
4555 #          B1-picked:
4556 #              folder/subdir/{a,b,c,d,Makefile_TOP_Merge1,newfile.c}
4557 #              folder/subdir/newsubdir/e_Merge1
4558 #              folder/subdir/tweaked/{h,Makefile_SUB_Merge1,newfile.py}
4559 #              folder/unchanged/<LOTS OF FILES>
4560 #          B2-picked:
4561 #              folder/subdir/{a,b,c,d,Makefile_TOP_Merge1,newfile.c,newfile.rs}
4562 #              folder/subdir/newsubdir/e_Merge2
4563 #              folder/subdir/tweaked/{h,Makefile_SUB_Merge1,newfile.py}
4564 #              folder/unchanged/<LOTS OF FILES>
4565 #
4566 # Notes: This testcase happens to exercise lots of the
4567 #        optimization-specific codepaths in merge-ort, and also
4568 #        demonstrated a failing of the directory rename detection algorithm
4569 #        in merge-recursive; newfile.c should not get pushed into
4570 #        folder/subdir/newsubdir/, yet merge-recursive put it there because
4571 #        the rename of dir/subdir/{a,b,c,d} -> folder/subdir/{a,b,c,d}
4572 #        looks like
4573 #            dir/ -> folder/,
4574 #        whereas the rename of dir/subdir/e -> folder/subdir/newsubdir/e
4575 #        looks like
4576 #            dir/subdir/ -> folder/subdir/newsubdir/
4577 #        and if we note that newfile.c is found in dir/subdir/, we might
4578 #        overlook the dir/ -> folder/ rule that has more weight.
4579
4580 test_setup_12f () {
4581         test_create_repo 12f &&
4582         (
4583                 cd 12f &&
4584
4585                 mkdir -p dir/unchanged &&
4586                 mkdir -p dir/subdir/tweaked &&
4587                 echo a >dir/subdir/a &&
4588                 echo b >dir/subdir/b &&
4589                 echo c >dir/subdir/c &&
4590                 echo d >dir/subdir/d &&
4591                 test_seq 1 10 >dir/subdir/e &&
4592                 test_seq 10 20 >dir/subdir/Makefile &&
4593                 echo f >dir/subdir/tweaked/f &&
4594                 echo g >dir/subdir/tweaked/g &&
4595                 echo h >dir/subdir/tweaked/h &&
4596                 test_seq 20 30 >dir/subdir/tweaked/Makefile &&
4597                 for i in `test_seq 1 88`; do
4598                         echo content $i >dir/unchanged/file_$i
4599                 done &&
4600                 git add . &&
4601                 git commit -m "O" &&
4602
4603                 git branch O &&
4604                 git branch A &&
4605                 git branch B &&
4606
4607                 git switch A &&
4608                 git rm dir/subdir/tweaked/f dir/subdir/tweaked/g &&
4609                 test_seq 2 10 >dir/subdir/e &&
4610                 test_seq 11 20 >dir/subdir/Makefile &&
4611                 test_seq 21 30 >dir/subdir/tweaked/Makefile &&
4612                 mkdir dir/subdir/newsubdir &&
4613                 git mv dir/subdir/e dir/subdir/newsubdir/ &&
4614                 git mv dir folder &&
4615                 git add . &&
4616                 git commit -m "A" &&
4617
4618                 git switch B &&
4619                 mkdir dir/subdir/newsubdir/ &&
4620                 echo c code >dir/subdir/newfile.c &&
4621                 echo python code >dir/subdir/newsubdir/newfile.py &&
4622                 test_seq 1 11 >dir/subdir/e &&
4623                 test_seq 10 21 >dir/subdir/Makefile &&
4624                 test_seq 20 31 >dir/subdir/tweaked/Makefile &&
4625                 git add . &&
4626                 git commit -m "B1" &&
4627
4628                 echo rust code >dir/subdir/newfile.rs &&
4629                 test_seq 1 12 >dir/subdir/e &&
4630                 git add . &&
4631                 git commit -m "B2"
4632         )
4633 }
4634
4635 test_expect_failure '12f: Trivial directory resolve, caching, all kinds of fun' '
4636         test_setup_12f &&
4637         (
4638                 cd 12f &&
4639
4640                 git checkout A^0 &&
4641                 git branch Bmod B &&
4642
4643                 git -c merge.directoryRenames=true rebase A Bmod &&
4644
4645                 echo Checking the pick of B1... &&
4646
4647                 test_must_fail git rev-parse Bmod~1:dir &&
4648
4649                 git ls-tree -r Bmod~1 >out &&
4650                 test_line_count = 98 out &&
4651
4652                 git diff --name-status A Bmod~1 >actual &&
4653                 q_to_tab >expect <<-\EOF &&
4654                 MQfolder/subdir/Makefile
4655                 AQfolder/subdir/newfile.c
4656                 MQfolder/subdir/newsubdir/e
4657                 AQfolder/subdir/newsubdir/newfile.py
4658                 MQfolder/subdir/tweaked/Makefile
4659                 EOF
4660                 test_cmp expect actual &&
4661
4662                 # Three-way merged files
4663                 test_seq  2 11 >e_Merge1 &&
4664                 test_seq 11 21 >Makefile_TOP &&
4665                 test_seq 21 31 >Makefile_SUB &&
4666                 git hash-object >expect      \
4667                         e_Merge1             \
4668                         Makefile_TOP         \
4669                         Makefile_SUB         &&
4670                 git rev-parse >actual              \
4671                         Bmod~1:folder/subdir/newsubdir/e     \
4672                         Bmod~1:folder/subdir/Makefile        \
4673                         Bmod~1:folder/subdir/tweaked/Makefile &&
4674                 test_cmp expect actual &&
4675
4676                 # New files showed up at the right location with right contents
4677                 git rev-parse >expect                \
4678                         B~1:dir/subdir/newfile.c            \
4679                         B~1:dir/subdir/newsubdir/newfile.py &&
4680                 git rev-parse >actual                      \
4681                         Bmod~1:folder/subdir/newfile.c            \
4682                         Bmod~1:folder/subdir/newsubdir/newfile.py &&
4683                 test_cmp expect actual &&
4684
4685                 # Removed files
4686                 test_path_is_missing folder/subdir/tweaked/f &&
4687                 test_path_is_missing folder/subdir/tweaked/g &&
4688
4689                 # Unchanged files or directories
4690                 git rev-parse >actual        \
4691                         Bmod~1:folder/subdir/a          \
4692                         Bmod~1:folder/subdir/b          \
4693                         Bmod~1:folder/subdir/c          \
4694                         Bmod~1:folder/subdir/d          \
4695                         Bmod~1:folder/unchanged         \
4696                         Bmod~1:folder/subdir/tweaked/h &&
4697                 git rev-parse >expect          \
4698                         O:dir/subdir/a         \
4699                         O:dir/subdir/b         \
4700                         O:dir/subdir/c         \
4701                         O:dir/subdir/d         \
4702                         O:dir/unchanged        \
4703                         O:dir/subdir/tweaked/h &&
4704                 test_cmp expect actual &&
4705
4706                 echo Checking the pick of B2... &&
4707
4708                 test_must_fail git rev-parse Bmod:dir &&
4709
4710                 git ls-tree -r Bmod >out &&
4711                 test_line_count = 99 out &&
4712
4713                 git diff --name-status Bmod~1 Bmod >actual &&
4714                 q_to_tab >expect <<-\EOF &&
4715                 AQfolder/subdir/newfile.rs
4716                 MQfolder/subdir/newsubdir/e
4717                 EOF
4718                 test_cmp expect actual &&
4719
4720                 # Three-way merged file
4721                 test_seq  2 12 >e_Merge2 &&
4722                 git hash-object e_Merge2 >expect &&
4723                 git rev-parse Bmod:folder/subdir/newsubdir/e >actual &&
4724                 test_cmp expect actual
4725         )
4726 '
4727
4728 ###########################################################################
4729 # SECTION 13: Checking informational and conflict messages
4730 #
4731 # A year after directory rename detection became the default, it was
4732 # instead decided to report conflicts on the pathname on the basis that
4733 # some users may expect the new files added or moved into a directory to
4734 # be unrelated to all the other files in that directory, and thus that
4735 # directory rename detection is unexpected.  Test that the messages printed
4736 # match our expectation.
4737 ###########################################################################
4738
4739 # Testcase 13a, Basic directory rename with newly added files
4740 #   Commit O: z/{b,c}
4741 #   Commit A: y/{b,c}
4742 #   Commit B: z/{b,c,d,e/f}
4743 #   Expected: y/{b,c,d,e/f}, with notices/conflicts for both y/d and y/e/f
4744
4745 test_setup_13a () {
4746         test_create_repo 13a_$1 &&
4747         (
4748                 cd 13a_$1 &&
4749
4750                 mkdir z &&
4751                 echo b >z/b &&
4752                 echo c >z/c &&
4753                 git add z &&
4754                 test_tick &&
4755                 git commit -m "O" &&
4756
4757                 git branch O &&
4758                 git branch A &&
4759                 git branch B &&
4760
4761                 git checkout A &&
4762                 git mv z y &&
4763                 test_tick &&
4764                 git commit -m "A" &&
4765
4766                 git checkout B &&
4767                 echo d >z/d &&
4768                 mkdir z/e &&
4769                 echo f >z/e/f &&
4770                 git add z/d z/e/f &&
4771                 test_tick &&
4772                 git commit -m "B"
4773         )
4774 }
4775
4776 test_expect_success '13a(conflict): messages for newly added files' '
4777         test_setup_13a conflict &&
4778         (
4779                 cd 13a_conflict &&
4780
4781                 git checkout A^0 &&
4782
4783                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4784
4785                 test_i18ngrep CONFLICT..file.location.*z/e/f.added.in.B^0.*y/e/f out &&
4786                 test_i18ngrep CONFLICT..file.location.*z/d.added.in.B^0.*y/d out &&
4787
4788                 git ls-files >paths &&
4789                 ! grep z/ paths &&
4790                 grep "y/[de]" paths &&
4791
4792                 test_path_is_missing z/d &&
4793                 test_path_is_file    y/d &&
4794                 test_path_is_missing z/e/f &&
4795                 test_path_is_file    y/e/f
4796         )
4797 '
4798
4799 test_expect_success '13a(info): messages for newly added files' '
4800         test_setup_13a info &&
4801         (
4802                 cd 13a_info &&
4803
4804                 git reset --hard &&
4805                 git checkout A^0 &&
4806
4807                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4808
4809                 test_i18ngrep Path.updated:.*z/e/f.added.in.B^0.*y/e/f out &&
4810                 test_i18ngrep Path.updated:.*z/d.added.in.B^0.*y/d out &&
4811
4812                 git ls-files >paths &&
4813                 ! grep z/ paths &&
4814                 grep "y/[de]" paths &&
4815
4816                 test_path_is_missing z/d &&
4817                 test_path_is_file    y/d &&
4818                 test_path_is_missing z/e/f &&
4819                 test_path_is_file    y/e/f
4820         )
4821 '
4822
4823 # Testcase 13b, Transitive rename with conflicted content merge and default
4824 #               "conflict" setting
4825 #   (Related to testcase 1c, 9b)
4826 #   Commit O: z/{b,c},   x/d_1
4827 #   Commit A: y/{b,c},   x/d_2
4828 #   Commit B: z/{b,c,d_3}
4829 #   Expected: y/{b,c,d_merged}, with two conflict messages for y/d,
4830 #             one about content, and one about file location
4831
4832 test_setup_13b () {
4833         test_create_repo 13b_$1 &&
4834         (
4835                 cd 13b_$1 &&
4836
4837                 mkdir x &&
4838                 mkdir z &&
4839                 test_seq 1 10 >x/d &&
4840                 echo b >z/b &&
4841                 echo c >z/c &&
4842                 git add x z &&
4843                 test_tick &&
4844                 git commit -m "O" &&
4845
4846                 git branch O &&
4847                 git branch A &&
4848                 git branch B &&
4849
4850                 git checkout A &&
4851                 git mv z y &&
4852                 echo 11 >>x/d &&
4853                 git add x/d &&
4854                 test_tick &&
4855                 git commit -m "A" &&
4856
4857                 git checkout B &&
4858                 echo eleven >>x/d &&
4859                 git mv x/d z/d &&
4860                 git add z/d &&
4861                 test_tick &&
4862                 git commit -m "B"
4863         )
4864 }
4865
4866 test_expect_success '13b(conflict): messages for transitive rename with conflicted content' '
4867         test_setup_13b conflict &&
4868         (
4869                 cd 13b_conflict &&
4870
4871                 git checkout A^0 &&
4872
4873                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4874
4875                 test_i18ngrep CONFLICT.*content.*Merge.conflict.in.y/d out &&
4876                 test_i18ngrep CONFLICT..file.location.*x/d.renamed.to.z/d.*moved.to.y/d out &&
4877
4878                 git ls-files >paths &&
4879                 ! grep z/ paths &&
4880                 grep "y/d" paths &&
4881
4882                 test_path_is_missing z/d &&
4883                 test_path_is_file    y/d
4884         )
4885 '
4886
4887 test_expect_success '13b(info): messages for transitive rename with conflicted content' '
4888         test_setup_13b info &&
4889         (
4890                 cd 13b_info &&
4891
4892                 git reset --hard &&
4893                 git checkout A^0 &&
4894
4895                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4896
4897                 test_i18ngrep CONFLICT.*content.*Merge.conflict.in.y/d out &&
4898                 test_i18ngrep Path.updated:.*x/d.renamed.to.z/d.in.B^0.*moving.it.to.y/d out &&
4899
4900                 git ls-files >paths &&
4901                 ! grep z/ paths &&
4902                 grep "y/d" paths &&
4903
4904                 test_path_is_missing z/d &&
4905                 test_path_is_file    y/d
4906         )
4907 '
4908
4909 # Testcase 13c, Rename/rename(1to1) due to directory rename
4910 #   Commit O: z/{b,c},   x/{d,e}
4911 #   Commit A: y/{b,c,d}, x/e
4912 #   Commit B: z/{b,c,d}, x/e
4913 #   Expected: y/{b,c,d}, x/e, with info or conflict messages for d
4914 #             A: renamed x/d -> z/d; B: renamed z/ -> y/ AND renamed x/d to y/d
4915 #             One could argue A had partial knowledge of what was done with
4916 #             d and B had full knowledge, but that's a slippery slope as
4917 #             shown in testcase 13d.
4918
4919 test_setup_13c () {
4920         test_create_repo 13c_$1 &&
4921         (
4922                 cd 13c_$1 &&
4923
4924                 mkdir x &&
4925                 mkdir z &&
4926                 test_seq 1 10 >x/d &&
4927                 echo e >x/e &&
4928                 echo b >z/b &&
4929                 echo c >z/c &&
4930                 git add x z &&
4931                 test_tick &&
4932                 git commit -m "O" &&
4933
4934                 git branch O &&
4935                 git branch A &&
4936                 git branch B &&
4937
4938                 git checkout A &&
4939                 git mv z y &&
4940                 git mv x/d y/ &&
4941                 test_tick &&
4942                 git commit -m "A" &&
4943
4944                 git checkout B &&
4945                 git mv x/d z/d &&
4946                 git add z/d &&
4947                 test_tick &&
4948                 git commit -m "B"
4949         )
4950 }
4951
4952 test_expect_success '13c(conflict): messages for rename/rename(1to1) via transitive rename' '
4953         test_setup_13c conflict &&
4954         (
4955                 cd 13c_conflict &&
4956
4957                 git checkout A^0 &&
4958
4959                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4960
4961                 test_i18ngrep CONFLICT..file.location.*x/d.renamed.to.z/d.*moved.to.y/d out &&
4962
4963                 git ls-files >paths &&
4964                 ! grep z/ paths &&
4965                 grep "y/d" paths &&
4966
4967                 test_path_is_missing z/d &&
4968                 test_path_is_file    y/d
4969         )
4970 '
4971
4972 test_expect_success '13c(info): messages for rename/rename(1to1) via transitive rename' '
4973         test_setup_13c info &&
4974         (
4975                 cd 13c_info &&
4976
4977                 git reset --hard &&
4978                 git checkout A^0 &&
4979
4980                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4981
4982                 test_i18ngrep Path.updated:.*x/d.renamed.to.z/d.in.B^0.*moving.it.to.y/d out &&
4983
4984                 git ls-files >paths &&
4985                 ! grep z/ paths &&
4986                 grep "y/d" paths &&
4987
4988                 test_path_is_missing z/d &&
4989                 test_path_is_file    y/d
4990         )
4991 '
4992
4993 # Testcase 13d, Rename/rename(1to1) due to directory rename on both sides
4994 #   Commit O: a/{z,y}, b/x,     c/w
4995 #   Commit A: a/z,     b/{y,x}, d/w
4996 #   Commit B: a/z,     d/x,     c/{y,w}
4997 #   Expected: a/z, d/{y,x,w} with no file location conflict for x
4998 #             Easy cases:
4999 #               * z is always in a; so it stays in a.
5000 #               * x starts in b, only modified on one side to move into d/
5001 #               * w starts in c, only modified on one side to move into d/
5002 #             Hard case:
5003 #               * A renames a/y to b/y, and B renames b/->d/ => a/y -> d/y
5004 #               * B renames a/y to c/y, and A renames c/->d/ => a/y -> d/y
5005 #               No conflict in where a/y ends up, so put it in d/y.
5006
5007 test_setup_13d () {
5008         test_create_repo 13d_$1 &&
5009         (
5010                 cd 13d_$1 &&
5011
5012                 mkdir a &&
5013                 mkdir b &&
5014                 mkdir c &&
5015                 echo z >a/z &&
5016                 echo y >a/y &&
5017                 echo x >b/x &&
5018                 echo w >c/w &&
5019                 git add a b c &&
5020                 test_tick &&
5021                 git commit -m "O" &&
5022
5023                 git branch O &&
5024                 git branch A &&
5025                 git branch B &&
5026
5027                 git checkout A &&
5028                 git mv a/y b/ &&
5029                 git mv c/ d/ &&
5030                 test_tick &&
5031                 git commit -m "A" &&
5032
5033                 git checkout B &&
5034                 git mv a/y c/ &&
5035                 git mv b/ d/ &&
5036                 test_tick &&
5037                 git commit -m "B"
5038         )
5039 }
5040
5041 test_expect_success '13d(conflict): messages for rename/rename(1to1) via dual transitive rename' '
5042         test_setup_13d conflict &&
5043         (
5044                 cd 13d_conflict &&
5045
5046                 git checkout A^0 &&
5047
5048                 test_must_fail git merge -s recursive B^0 >out 2>err &&
5049
5050                 test_i18ngrep CONFLICT..file.location.*a/y.renamed.to.b/y.*moved.to.d/y out &&
5051                 test_i18ngrep CONFLICT..file.location.*a/y.renamed.to.c/y.*moved.to.d/y out &&
5052
5053                 git ls-files >paths &&
5054                 ! grep b/ paths &&
5055                 ! grep c/ paths &&
5056                 grep "d/y" paths &&
5057
5058                 test_path_is_missing b/y &&
5059                 test_path_is_missing c/y &&
5060                 test_path_is_file    d/y
5061         )
5062 '
5063
5064 test_expect_success '13d(info): messages for rename/rename(1to1) via dual transitive rename' '
5065         test_setup_13d info &&
5066         (
5067                 cd 13d_info &&
5068
5069                 git reset --hard &&
5070                 git checkout A^0 &&
5071
5072                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
5073
5074                 test_i18ngrep Path.updated.*a/y.renamed.to.b/y.*moving.it.to.d/y out &&
5075                 test_i18ngrep Path.updated.*a/y.renamed.to.c/y.*moving.it.to.d/y out &&
5076
5077                 git ls-files >paths &&
5078                 ! grep b/ paths &&
5079                 ! grep c/ paths &&
5080                 grep "d/y" paths &&
5081
5082                 test_path_is_missing b/y &&
5083                 test_path_is_missing c/y &&
5084                 test_path_is_file    d/y
5085         )
5086 '
5087
5088 # Testcase 13e, directory rename in virtual merge base
5089 #
5090 # This testcase has a slightly different setup than all the above cases, in
5091 # order to include a recursive case:
5092 #
5093 #      A   C
5094 #      o - o
5095 #     / \ / \
5096 #  O o   X   ?
5097 #     \ / \ /
5098 #      o   o
5099 #      B   D
5100 #
5101 #   Commit O: a/{z,y}
5102 #   Commit A: b/{z,y}
5103 #   Commit B: a/{z,y,x}
5104 #   Commit C: b/{z,y,x}
5105 #   Commit D: b/{z,y}, a/x
5106 #   Expected: b/{z,y,x}  (sort of; see below for why this might not be expected)
5107 #
5108 #   NOTES: 'X' represents a virtual merge base.  With the default of
5109 #          directory rename detection yielding conflicts, merging A and B
5110 #          results in a conflict complaining about whether 'x' should be
5111 #          under 'a/' or 'b/'.  However, when creating the virtual merge
5112 #          base 'X', since virtual merge bases need to be written out as a
5113 #          tree, we cannot have a conflict, so some resolution has to be
5114 #          picked.
5115 #
5116 #          In choosing the right resolution, it's worth noting here that
5117 #          commits C & D are merges of A & B that choose different
5118 #          locations for 'x' (i.e. they resolve the conflict differently),
5119 #          and so it would be nice when merging C & D if git could detect
5120 #          this difference of opinion and report a conflict.  But the only
5121 #          way to do so that I can think of would be to have the virtual
5122 #          merge base place 'x' in some directory other than either 'a/' or
5123 #          'b/', which seems a little weird -- especially since it'd result
5124 #          in a rename/rename(1to2) conflict with a source path that never
5125 #          existed in any version.
5126 #
5127 #          So, for now, when directory rename detection is set to
5128 #          'conflict' just avoid doing directory rename detection at all in
5129 #          the recursive case.  This will not allow us to detect a conflict
5130 #          in the outer merge for this special kind of setup, but it at
5131 #          least avoids hitting a BUG().
5132 #
5133 test_setup_13e () {
5134         test_create_repo 13e &&
5135         (
5136                 cd 13e &&
5137
5138                 mkdir a &&
5139                 echo z >a/z &&
5140                 echo y >a/y &&
5141                 git add a &&
5142                 test_tick &&
5143                 git commit -m "O" &&
5144
5145                 git branch O &&
5146                 git branch A &&
5147                 git branch B &&
5148
5149                 git checkout A &&
5150                 git mv a/ b/ &&
5151                 test_tick &&
5152                 git commit -m "A" &&
5153
5154                 git checkout B &&
5155                 echo x >a/x &&
5156                 git add a &&
5157                 test_tick &&
5158                 git commit -m "B" &&
5159
5160                 git branch C A &&
5161                 git branch D B &&
5162
5163                 git checkout C &&
5164                 test_must_fail git -c merge.directoryRenames=conflict merge B &&
5165                 git add b/x &&
5166                 test_tick &&
5167                 git commit -m "C" &&
5168
5169
5170                 git checkout D &&
5171                 test_must_fail git -c merge.directoryRenames=conflict merge A &&
5172                 git add b/x &&
5173                 mkdir a &&
5174                 git mv b/x a/x &&
5175                 test_tick &&
5176                 git commit -m "D"
5177         )
5178 }
5179
5180 test_expect_success '13e: directory rename detection in recursive case' '
5181         test_setup_13e &&
5182         (
5183                 cd 13e &&
5184
5185                 git checkout --quiet D^0 &&
5186
5187                 git -c merge.directoryRenames=conflict merge -s recursive C^0 >out 2>err &&
5188
5189                 test_i18ngrep ! CONFLICT out &&
5190                 test_i18ngrep ! BUG: err &&
5191                 test_i18ngrep ! core.dumped err &&
5192                 test_must_be_empty err &&
5193
5194                 git ls-files >paths &&
5195                 ! grep a/x paths &&
5196                 grep b/x paths
5197         )
5198 '
5199
5200 test_done