t/perf: avoid unnecessary test_export() recursion
[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 6b, Same rename done on both sides
1281 #   (Related to testcases 6c and 8e)
1282 #   Commit O: z/{b,c}
1283 #   Commit A: y/{b,c}
1284 #   Commit B: y/{b,c}, z/d
1285 #   Expected: y/{b,c}, z/d
1286 #   Note: If we did directory rename detection here, we'd move z/d into y/,
1287 #         but B did that rename and still decided to put the file into z/,
1288 #         so we probably shouldn't apply directory rename detection for it.
1289
1290 test_setup_6b () {
1291         test_create_repo 6b &&
1292         (
1293                 cd 6b &&
1294
1295                 mkdir z &&
1296                 echo b >z/b &&
1297                 echo c >z/c &&
1298                 git add z &&
1299                 test_tick &&
1300                 git commit -m "O" &&
1301
1302                 git branch O &&
1303                 git branch A &&
1304                 git branch B &&
1305
1306                 git checkout A &&
1307                 git mv z y &&
1308                 test_tick &&
1309                 git commit -m "A" &&
1310
1311                 git checkout B &&
1312                 git mv z y &&
1313                 mkdir z &&
1314                 echo d >z/d &&
1315                 git add z/d &&
1316                 test_tick &&
1317                 git commit -m "B"
1318         )
1319 }
1320
1321 test_expect_success '6b: Same rename done on both sides' '
1322         test_setup_6b &&
1323         (
1324                 cd 6b &&
1325
1326                 git checkout A^0 &&
1327
1328                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
1329
1330                 git ls-files -s >out &&
1331                 test_line_count = 3 out &&
1332                 git ls-files -u >out &&
1333                 test_line_count = 0 out &&
1334                 git ls-files -o >out &&
1335                 test_line_count = 1 out &&
1336
1337                 git rev-parse >actual \
1338                         HEAD:y/b HEAD:y/c HEAD:z/d &&
1339                 git rev-parse >expect \
1340                         O:z/b    O:z/c    B:z/d &&
1341                 test_cmp expect actual
1342         )
1343 '
1344
1345 # Testcase 6c, Rename only done on same side
1346 #   (Related to testcases 6b and 8e)
1347 #   Commit O: z/{b,c}
1348 #   Commit A: z/{b,c} (no change)
1349 #   Commit B: y/{b,c}, z/d
1350 #   Expected: y/{b,c}, z/d
1351 #   NOTE: Seems obvious, but just checking that the implementation doesn't
1352 #         "accidentally detect a rename" and give us y/{b,c,d}.
1353
1354 test_setup_6c () {
1355         test_create_repo 6c &&
1356         (
1357                 cd 6c &&
1358
1359                 mkdir z &&
1360                 echo b >z/b &&
1361                 echo c >z/c &&
1362                 git add z &&
1363                 test_tick &&
1364                 git commit -m "O" &&
1365
1366                 git branch O &&
1367                 git branch A &&
1368                 git branch B &&
1369
1370                 git checkout A &&
1371                 test_tick &&
1372                 git commit --allow-empty -m "A" &&
1373
1374                 git checkout B &&
1375                 git mv z y &&
1376                 mkdir z &&
1377                 echo d >z/d &&
1378                 git add z/d &&
1379                 test_tick &&
1380                 git commit -m "B"
1381         )
1382 }
1383
1384 test_expect_success '6c: Rename only done on same side' '
1385         test_setup_6c &&
1386         (
1387                 cd 6c &&
1388
1389                 git checkout A^0 &&
1390
1391                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
1392
1393                 git ls-files -s >out &&
1394                 test_line_count = 3 out &&
1395                 git ls-files -u >out &&
1396                 test_line_count = 0 out &&
1397                 git ls-files -o >out &&
1398                 test_line_count = 1 out &&
1399
1400                 git rev-parse >actual \
1401                         HEAD:y/b HEAD:y/c HEAD:z/d &&
1402                 git rev-parse >expect \
1403                         O:z/b    O:z/c    B:z/d &&
1404                 test_cmp expect actual
1405         )
1406 '
1407
1408 # Testcase 6d, We don't always want transitive renaming
1409 #   (Related to testcase 1c)
1410 #   Commit O: z/{b,c}, x/d
1411 #   Commit A: z/{b,c}, x/d (no change)
1412 #   Commit B: y/{b,c}, z/d
1413 #   Expected: y/{b,c}, z/d
1414 #   NOTE: Again, this seems obvious but just checking that the implementation
1415 #         doesn't "accidentally detect a rename" and give us y/{b,c,d}.
1416
1417 test_setup_6d () {
1418         test_create_repo 6d &&
1419         (
1420                 cd 6d &&
1421
1422                 mkdir z &&
1423                 echo b >z/b &&
1424                 echo c >z/c &&
1425                 mkdir x &&
1426                 echo d >x/d &&
1427                 git add z x &&
1428                 test_tick &&
1429                 git commit -m "O" &&
1430
1431                 git branch O &&
1432                 git branch A &&
1433                 git branch B &&
1434
1435                 git checkout A &&
1436                 test_tick &&
1437                 git commit --allow-empty -m "A" &&
1438
1439                 git checkout B &&
1440                 git mv z y &&
1441                 git mv x z &&
1442                 test_tick &&
1443                 git commit -m "B"
1444         )
1445 }
1446
1447 test_expect_success '6d: We do not always want transitive renaming' '
1448         test_setup_6d &&
1449         (
1450                 cd 6d &&
1451
1452                 git checkout A^0 &&
1453
1454                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
1455
1456                 git ls-files -s >out &&
1457                 test_line_count = 3 out &&
1458                 git ls-files -u >out &&
1459                 test_line_count = 0 out &&
1460                 git ls-files -o >out &&
1461                 test_line_count = 1 out &&
1462
1463                 git rev-parse >actual \
1464                         HEAD:y/b HEAD:y/c HEAD:z/d &&
1465                 git rev-parse >expect \
1466                         O:z/b    O:z/c    O:x/d &&
1467                 test_cmp expect actual
1468         )
1469 '
1470
1471 # Testcase 6e, Add/add from one-side
1472 #   Commit O: z/{b,c}
1473 #   Commit A: z/{b,c} (no change)
1474 #   Commit B: y/{b,c,d_1}, z/d_2
1475 #   Expected: y/{b,c,d_1}, z/d_2
1476 #   NOTE: Again, this seems obvious but just checking that the implementation
1477 #         doesn't "accidentally detect a rename" and give us y/{b,c} +
1478 #         add/add conflict on y/d_1 vs y/d_2.
1479
1480 test_setup_6e () {
1481         test_create_repo 6e &&
1482         (
1483                 cd 6e &&
1484
1485                 mkdir z &&
1486                 echo b >z/b &&
1487                 echo c >z/c &&
1488                 git add z &&
1489                 test_tick &&
1490                 git commit -m "O" &&
1491
1492                 git branch O &&
1493                 git branch A &&
1494                 git branch B &&
1495
1496                 git checkout A &&
1497                 test_tick &&
1498                 git commit --allow-empty -m "A" &&
1499
1500                 git checkout B &&
1501                 git mv z y &&
1502                 echo d1 > y/d &&
1503                 mkdir z &&
1504                 echo d2 > z/d &&
1505                 git add y/d z/d &&
1506                 test_tick &&
1507                 git commit -m "B"
1508         )
1509 }
1510
1511 test_expect_success '6e: Add/add from one side' '
1512         test_setup_6e &&
1513         (
1514                 cd 6e &&
1515
1516                 git checkout A^0 &&
1517
1518                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
1519
1520                 git ls-files -s >out &&
1521                 test_line_count = 4 out &&
1522                 git ls-files -u >out &&
1523                 test_line_count = 0 out &&
1524                 git ls-files -o >out &&
1525                 test_line_count = 1 out &&
1526
1527                 git rev-parse >actual \
1528                         HEAD:y/b HEAD:y/c HEAD:y/d HEAD:z/d &&
1529                 git rev-parse >expect \
1530                         O:z/b    O:z/c    B:y/d    B:z/d &&
1531                 test_cmp expect actual
1532         )
1533 '
1534
1535 ###########################################################################
1536 # Rules suggested by section 6:
1537 #
1538 #   Only apply implicit directory renames to directories if the other
1539 #   side of history is the one doing the renaming.
1540 ###########################################################################
1541
1542
1543 ###########################################################################
1544 # SECTION 7: More involved Edge/Corner cases
1545 #
1546 # The ruleset we have generated in the above sections seems to provide
1547 # well-defined merges.  But can we find edge/corner cases that either (a)
1548 # are harder for users to understand, or (b) have a resolution that is
1549 # non-intuitive or suboptimal?
1550 #
1551 # The testcases in this section dive into cases that I've tried to craft in
1552 # a way to find some that might be surprising to users or difficult for
1553 # them to understand (the next section will look at non-intuitive or
1554 # suboptimal merge results).  Some of the testcases are similar to ones
1555 # from past sections, but have been simplified to try to highlight error
1556 # messages using a "modified" path (due to the directory rename).  Are
1557 # users okay with these?
1558 #
1559 # In my opinion, testcases that are difficult to understand from this
1560 # section is due to difficulty in the testcase rather than the directory
1561 # renaming (similar to how t6042 and t6036 have difficult resolutions due
1562 # to the problem setup itself being complex).  And I don't think the
1563 # error messages are a problem.
1564 #
1565 # On the other hand, the testcases in section 8 worry me slightly more...
1566 ###########################################################################
1567
1568 # Testcase 7a, rename-dir vs. rename-dir (NOT split evenly) PLUS add-other-file
1569 #   Commit O: z/{b,c}
1570 #   Commit A: y/{b,c}
1571 #   Commit B: w/b, x/c, z/d
1572 #   Expected: y/d, CONFLICT(rename/rename for both z/b and z/c)
1573 #   NOTE: There's a rename of z/ here, y/ has more renames, so z/d -> y/d.
1574
1575 test_setup_7a () {
1576         test_create_repo 7a &&
1577         (
1578                 cd 7a &&
1579
1580                 mkdir z &&
1581                 echo b >z/b &&
1582                 echo c >z/c &&
1583                 git add z &&
1584                 test_tick &&
1585                 git commit -m "O" &&
1586
1587                 git branch O &&
1588                 git branch A &&
1589                 git branch B &&
1590
1591                 git checkout A &&
1592                 git mv z y &&
1593                 test_tick &&
1594                 git commit -m "A" &&
1595
1596                 git checkout B &&
1597                 mkdir w &&
1598                 mkdir x &&
1599                 git mv z/b w/ &&
1600                 git mv z/c x/ &&
1601                 echo d > z/d &&
1602                 git add z/d &&
1603                 test_tick &&
1604                 git commit -m "B"
1605         )
1606 }
1607
1608 test_expect_success '7a: rename-dir vs. rename-dir (NOT split evenly) PLUS add-other-file' '
1609         test_setup_7a &&
1610         (
1611                 cd 7a &&
1612
1613                 git checkout A^0 &&
1614
1615                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1616                 test_i18ngrep "CONFLICT (rename/rename).*z/b.*y/b.*w/b" out &&
1617                 test_i18ngrep "CONFLICT (rename/rename).*z/c.*y/c.*x/c" out &&
1618
1619                 git ls-files -s >out &&
1620                 test_line_count = 7 out &&
1621                 git ls-files -u >out &&
1622                 test_line_count = 6 out &&
1623                 git ls-files -o >out &&
1624                 test_line_count = 1 out &&
1625
1626                 git rev-parse >actual \
1627                         :1:z/b :2:y/b :3:w/b :1:z/c :2:y/c :3:x/c :0:y/d &&
1628                 git rev-parse >expect \
1629                          O:z/b  O:z/b  O:z/b  O:z/c  O:z/c  O:z/c  B:z/d &&
1630                 test_cmp expect actual &&
1631
1632                 git hash-object >actual \
1633                         y/b   w/b   y/c   x/c &&
1634                 git rev-parse >expect \
1635                         O:z/b O:z/b O:z/c O:z/c &&
1636                 test_cmp expect actual
1637         )
1638 '
1639
1640 # Testcase 7b, rename/rename(2to1), but only due to transitive rename
1641 #   (Related to testcase 1d)
1642 #   Commit O: z/{b,c},     x/d_1, w/d_2
1643 #   Commit A: y/{b,c,d_2}, x/d_1
1644 #   Commit B: z/{b,c,d_1},        w/d_2
1645 #   Expected: y/{b,c}, CONFLICT(rename/rename(2to1): x/d_1, w/d_2 -> y_d)
1646
1647 test_setup_7b () {
1648         test_create_repo 7b &&
1649         (
1650                 cd 7b &&
1651
1652                 mkdir z &&
1653                 mkdir x &&
1654                 mkdir w &&
1655                 echo b >z/b &&
1656                 echo c >z/c &&
1657                 echo d1 > x/d &&
1658                 echo d2 > w/d &&
1659                 git add z x w &&
1660                 test_tick &&
1661                 git commit -m "O" &&
1662
1663                 git branch O &&
1664                 git branch A &&
1665                 git branch B &&
1666
1667                 git checkout A &&
1668                 git mv z y &&
1669                 git mv w/d y/ &&
1670                 test_tick &&
1671                 git commit -m "A" &&
1672
1673                 git checkout B &&
1674                 git mv x/d z/ &&
1675                 rmdir x &&
1676                 test_tick &&
1677                 git commit -m "B"
1678         )
1679 }
1680
1681 test_expect_success '7b: rename/rename(2to1), but only due to transitive rename' '
1682         test_setup_7b &&
1683         (
1684                 cd 7b &&
1685
1686                 git checkout A^0 &&
1687
1688                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1689                 test_i18ngrep "CONFLICT (\(.*\)/\1)" out &&
1690
1691                 git ls-files -s >out &&
1692                 test_line_count = 4 out &&
1693                 git ls-files -u >out &&
1694                 test_line_count = 2 out &&
1695                 git ls-files -o >out &&
1696                 test_line_count = 1 out &&
1697
1698                 git rev-parse >actual \
1699                         :0:y/b :0:y/c :2:y/d :3:y/d &&
1700                 git rev-parse >expect \
1701                          O:z/b  O:z/c  O:w/d  O:x/d &&
1702                 test_cmp expect actual &&
1703
1704                 # Test that the two-way merge in y/d is as expected
1705                 git cat-file -p :2:y/d >expect &&
1706                 git cat-file -p :3:y/d >other &&
1707                 >empty &&
1708                 test_must_fail git merge-file \
1709                         -L "HEAD" \
1710                         -L "" \
1711                         -L "B^0" \
1712                         expect empty other &&
1713                 test_cmp expect y/d
1714         )
1715 '
1716
1717 # Testcase 7c, rename/rename(1to...2or3); transitive rename may add complexity
1718 #   (Related to testcases 3b and 5c)
1719 #   Commit O: z/{b,c}, x/d
1720 #   Commit A: y/{b,c}, w/d
1721 #   Commit B: z/{b,c,d}
1722 #   Expected: y/{b,c}, CONFLICT(x/d -> w/d vs. y/d)
1723 #   NOTE: z/ was renamed to y/ so we do want to report
1724 #         neither CONFLICT(x/d -> w/d vs. z/d)
1725 #         nor CONFLiCT x/d -> w/d vs. y/d vs. z/d)
1726
1727 test_setup_7c () {
1728         test_create_repo 7c &&
1729         (
1730                 cd 7c &&
1731
1732                 mkdir z &&
1733                 echo b >z/b &&
1734                 echo c >z/c &&
1735                 mkdir x &&
1736                 echo d >x/d &&
1737                 git add z x &&
1738                 test_tick &&
1739                 git commit -m "O" &&
1740
1741                 git branch O &&
1742                 git branch A &&
1743                 git branch B &&
1744
1745                 git checkout A &&
1746                 git mv z y &&
1747                 git mv x w &&
1748                 test_tick &&
1749                 git commit -m "A" &&
1750
1751                 git checkout B &&
1752                 git mv x/d z/ &&
1753                 rmdir x &&
1754                 test_tick &&
1755                 git commit -m "B"
1756         )
1757 }
1758
1759 test_expect_success '7c: rename/rename(1to...2or3); transitive rename may add complexity' '
1760         test_setup_7c &&
1761         (
1762                 cd 7c &&
1763
1764                 git checkout A^0 &&
1765
1766                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1767                 test_i18ngrep "CONFLICT (rename/rename).*x/d.*w/d.*y/d" out &&
1768
1769                 git ls-files -s >out &&
1770                 test_line_count = 5 out &&
1771                 git ls-files -u >out &&
1772                 test_line_count = 3 out &&
1773                 git ls-files -o >out &&
1774                 test_line_count = 1 out &&
1775
1776                 git rev-parse >actual \
1777                         :0:y/b :0:y/c :1:x/d :2:w/d :3:y/d &&
1778                 git rev-parse >expect \
1779                          O:z/b  O:z/c  O:x/d  O:x/d  O:x/d &&
1780                 test_cmp expect actual
1781         )
1782 '
1783
1784 # Testcase 7d, transitive rename involved in rename/delete; how is it reported?
1785 #   (Related somewhat to testcases 5b and 8d)
1786 #   Commit O: z/{b,c}, x/d
1787 #   Commit A: y/{b,c}
1788 #   Commit B: z/{b,c,d}
1789 #   Expected: y/{b,c}, CONFLICT(delete x/d vs rename to y/d)
1790 #   NOTE: z->y so NOT CONFLICT(delete x/d vs rename to z/d)
1791
1792 test_setup_7d () {
1793         test_create_repo 7d &&
1794         (
1795                 cd 7d &&
1796
1797                 mkdir z &&
1798                 echo b >z/b &&
1799                 echo c >z/c &&
1800                 mkdir x &&
1801                 echo d >x/d &&
1802                 git add z x &&
1803                 test_tick &&
1804                 git commit -m "O" &&
1805
1806                 git branch O &&
1807                 git branch A &&
1808                 git branch B &&
1809
1810                 git checkout A &&
1811                 git mv z y &&
1812                 git rm -rf x &&
1813                 test_tick &&
1814                 git commit -m "A" &&
1815
1816                 git checkout B &&
1817                 git mv x/d z/ &&
1818                 rmdir x &&
1819                 test_tick &&
1820                 git commit -m "B"
1821         )
1822 }
1823
1824 test_expect_success '7d: transitive rename involved in rename/delete; how is it reported?' '
1825         test_setup_7d &&
1826         (
1827                 cd 7d &&
1828
1829                 git checkout A^0 &&
1830
1831                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1832                 test_i18ngrep "CONFLICT (rename/delete).*x/d.*y/d" out &&
1833
1834                 git ls-files -s >out &&
1835                 test_line_count = 3 out &&
1836                 git ls-files -u >out &&
1837                 test_line_count = 1 out &&
1838                 git ls-files -o >out &&
1839                 test_line_count = 1 out &&
1840
1841                 git rev-parse >actual \
1842                         :0:y/b :0:y/c :3:y/d &&
1843                 git rev-parse >expect \
1844                          O:z/b  O:z/c  O:x/d &&
1845                 test_cmp expect actual
1846         )
1847 '
1848
1849 # Testcase 7e, transitive rename in rename/delete AND dirs in the way
1850 #   (Very similar to 'both rename source and destination involved in D/F conflict' from t6022-merge-rename.sh)
1851 #   (Also related to testcases 9c and 9d)
1852 #   Commit O: z/{b,c},     x/d_1
1853 #   Commit A: y/{b,c,d/g}, x/d/f
1854 #   Commit B: z/{b,c,d_1}
1855 #   Expected: rename/delete(x/d_1->y/d_1 vs. None) + D/F conflict on y/d
1856 #             y/{b,c,d/g}, y/d_1~B^0, x/d/f
1857
1858 #   NOTE: The main path of interest here is d_1 and where it ends up, but
1859 #         this is actually a case that has two potential directory renames
1860 #         involved and D/F conflict(s), so it makes sense to walk through
1861 #         each step.
1862 #
1863 #         Commit A renames z/ -> y/.  Thus everything that B adds to z/
1864 #         should be instead moved to y/.  This gives us the D/F conflict on
1865 #         y/d because x/d_1 -> z/d_1 -> y/d_1 conflicts with y/d/g.
1866 #
1867 #         Further, commit B renames x/ -> z/, thus everything A adds to x/
1868 #         should instead be moved to z/...BUT we removed z/ and renamed it
1869 #         to y/, so maybe everything should move not from x/ to z/, but
1870 #         from x/ to z/ to y/.  Doing so might make sense from the logic so
1871 #         far, but note that commit A had both an x/ and a y/; it did the
1872 #         renaming of z/ to y/ and created x/d/f and it clearly made these
1873 #         things separate, so it doesn't make much sense to push these
1874 #         together.  Doing so is what I'd call a doubly transitive rename;
1875 #         see testcases 9c and 9d for further discussion of this issue and
1876 #         how it's resolved.
1877
1878 test_setup_7e () {
1879         test_create_repo 7e &&
1880         (
1881                 cd 7e &&
1882
1883                 mkdir z &&
1884                 echo b >z/b &&
1885                 echo c >z/c &&
1886                 mkdir x &&
1887                 echo d1 >x/d &&
1888                 git add z x &&
1889                 test_tick &&
1890                 git commit -m "O" &&
1891
1892                 git branch O &&
1893                 git branch A &&
1894                 git branch B &&
1895
1896                 git checkout A &&
1897                 git mv z y &&
1898                 git rm x/d &&
1899                 mkdir -p x/d &&
1900                 mkdir -p y/d &&
1901                 echo f >x/d/f &&
1902                 echo g >y/d/g &&
1903                 git add x/d/f y/d/g &&
1904                 test_tick &&
1905                 git commit -m "A" &&
1906
1907                 git checkout B &&
1908                 git mv x/d z/ &&
1909                 rmdir x &&
1910                 test_tick &&
1911                 git commit -m "B"
1912         )
1913 }
1914
1915 test_expect_success '7e: transitive rename in rename/delete AND dirs in the way' '
1916         test_setup_7e &&
1917         (
1918                 cd 7e &&
1919
1920                 git checkout A^0 &&
1921
1922                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
1923                 test_i18ngrep "CONFLICT (rename/delete).*x/d.*y/d" out &&
1924
1925                 git ls-files -s >out &&
1926                 test_line_count = 5 out &&
1927                 git ls-files -u >out &&
1928                 test_line_count = 1 out &&
1929                 git ls-files -o >out &&
1930                 test_line_count = 2 out &&
1931
1932                 git rev-parse >actual \
1933                         :0:x/d/f :0:y/d/g :0:y/b :0:y/c :3:y/d &&
1934                 git rev-parse >expect \
1935                          A:x/d/f  A:y/d/g  O:z/b  O:z/c  O:x/d &&
1936                 test_cmp expect actual &&
1937
1938                 git hash-object y/d~B^0 >actual &&
1939                 git rev-parse O:x/d >expect &&
1940                 test_cmp expect actual
1941         )
1942 '
1943
1944 ###########################################################################
1945 # SECTION 8: Suboptimal merges
1946 #
1947 # As alluded to in the last section, the ruleset we have built up for
1948 # detecting directory renames unfortunately has some special cases where it
1949 # results in slightly suboptimal or non-intuitive behavior.  This section
1950 # explores these cases.
1951 #
1952 # To be fair, we already had non-intuitive or suboptimal behavior for most
1953 # of these cases in git before introducing implicit directory rename
1954 # detection, but it'd be nice if there was a modified ruleset out there
1955 # that handled these cases a bit better.
1956 ###########################################################################
1957
1958 # Testcase 8a, Dual-directory rename, one into the others' way
1959 #   Commit O. x/{a,b},   y/{c,d}
1960 #   Commit A. x/{a,b,e}, y/{c,d,f}
1961 #   Commit B. y/{a,b},   z/{c,d}
1962 #
1963 # Possible Resolutions:
1964 #   w/o dir-rename detection: y/{a,b,f},   z/{c,d},   x/e
1965 #   Currently expected:       y/{a,b,e,f}, z/{c,d}
1966 #   Optimal:                  y/{a,b,e},   z/{c,d,f}
1967 #
1968 # Note: Both x and y got renamed and it'd be nice to detect both, and we do
1969 # better with directory rename detection than git did without, but the
1970 # simple rule from section 5 prevents me from handling this as optimally as
1971 # we potentially could.
1972
1973 test_setup_8a () {
1974         test_create_repo 8a &&
1975         (
1976                 cd 8a &&
1977
1978                 mkdir x &&
1979                 mkdir y &&
1980                 echo a >x/a &&
1981                 echo b >x/b &&
1982                 echo c >y/c &&
1983                 echo d >y/d &&
1984                 git add x y &&
1985                 test_tick &&
1986                 git commit -m "O" &&
1987
1988                 git branch O &&
1989                 git branch A &&
1990                 git branch B &&
1991
1992                 git checkout A &&
1993                 echo e >x/e &&
1994                 echo f >y/f &&
1995                 git add x/e y/f &&
1996                 test_tick &&
1997                 git commit -m "A" &&
1998
1999                 git checkout B &&
2000                 git mv y z &&
2001                 git mv x y &&
2002                 test_tick &&
2003                 git commit -m "B"
2004         )
2005 }
2006
2007 test_expect_success '8a: Dual-directory rename, one into the others way' '
2008         test_setup_8a &&
2009         (
2010                 cd 8a &&
2011
2012                 git checkout A^0 &&
2013
2014                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2015
2016                 git ls-files -s >out &&
2017                 test_line_count = 6 out &&
2018                 git ls-files -u >out &&
2019                 test_line_count = 0 out &&
2020                 git ls-files -o >out &&
2021                 test_line_count = 1 out &&
2022
2023                 git rev-parse >actual \
2024                         HEAD:y/a HEAD:y/b HEAD:y/e HEAD:y/f HEAD:z/c HEAD:z/d &&
2025                 git rev-parse >expect \
2026                         O:x/a    O:x/b    A:x/e    A:y/f    O:y/c    O:y/d &&
2027                 test_cmp expect actual
2028         )
2029 '
2030
2031 # Testcase 8b, Dual-directory rename, one into the others' way, with conflicting filenames
2032 #   Commit O. x/{a_1,b_1},     y/{a_2,b_2}
2033 #   Commit A. x/{a_1,b_1,e_1}, y/{a_2,b_2,e_2}
2034 #   Commit B. y/{a_1,b_1},     z/{a_2,b_2}
2035 #
2036 #   w/o dir-rename detection: y/{a_1,b_1,e_2}, z/{a_2,b_2}, x/e_1
2037 #   Currently expected:       <same>
2038 #   Scary:                    y/{a_1,b_1},     z/{a_2,b_2}, CONFLICT(add/add, e_1 vs. e_2)
2039 #   Optimal:                  y/{a_1,b_1,e_1}, z/{a_2,b_2,e_2}
2040 #
2041 # Note: Very similar to 8a, except instead of 'e' and 'f' in directories x and
2042 # y, both are named 'e'.  Without directory rename detection, neither file
2043 # moves directories.  Implement directory rename detection suboptimally, and
2044 # you get an add/add conflict, but both files were added in commit A, so this
2045 # is an add/add conflict where one side of history added both files --
2046 # something we can't represent in the index.  Obviously, we'd prefer the last
2047 # resolution, but our previous rules are too coarse to allow it.  Using both
2048 # the rules from section 4 and section 5 save us from the Scary resolution,
2049 # making us fall back to pre-directory-rename-detection behavior for both
2050 # e_1 and e_2.
2051
2052 test_setup_8b () {
2053         test_create_repo 8b &&
2054         (
2055                 cd 8b &&
2056
2057                 mkdir x &&
2058                 mkdir y &&
2059                 echo a1 >x/a &&
2060                 echo b1 >x/b &&
2061                 echo a2 >y/a &&
2062                 echo b2 >y/b &&
2063                 git add x y &&
2064                 test_tick &&
2065                 git commit -m "O" &&
2066
2067                 git branch O &&
2068                 git branch A &&
2069                 git branch B &&
2070
2071                 git checkout A &&
2072                 echo e1 >x/e &&
2073                 echo e2 >y/e &&
2074                 git add x/e y/e &&
2075                 test_tick &&
2076                 git commit -m "A" &&
2077
2078                 git checkout B &&
2079                 git mv y z &&
2080                 git mv x y &&
2081                 test_tick &&
2082                 git commit -m "B"
2083         )
2084 }
2085
2086 test_expect_success '8b: Dual-directory rename, one into the others way, with conflicting filenames' '
2087         test_setup_8b &&
2088         (
2089                 cd 8b &&
2090
2091                 git checkout A^0 &&
2092
2093                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2094
2095                 git ls-files -s >out &&
2096                 test_line_count = 6 out &&
2097                 git ls-files -u >out &&
2098                 test_line_count = 0 out &&
2099                 git ls-files -o >out &&
2100                 test_line_count = 1 out &&
2101
2102                 git rev-parse >actual \
2103                         HEAD:y/a HEAD:y/b HEAD:z/a HEAD:z/b HEAD:x/e HEAD:y/e &&
2104                 git rev-parse >expect \
2105                         O:x/a    O:x/b    O:y/a    O:y/b    A:x/e    A:y/e &&
2106                 test_cmp expect actual
2107         )
2108 '
2109
2110 # Testcase 8c, modify/delete or rename+modify/delete?
2111 #   (Related to testcases 5b, 8d, and 9h)
2112 #   Commit O: z/{b,c,d}
2113 #   Commit A: y/{b,c}
2114 #   Commit B: z/{b,c,d_modified,e}
2115 #   Expected: y/{b,c,e}, CONFLICT(modify/delete: on z/d)
2116 #
2117 #   Note: It could easily be argued that the correct resolution here is
2118 #         y/{b,c,e}, CONFLICT(rename/delete: z/d -> y/d vs deleted)
2119 #         and that the modified version of d should be present in y/ after
2120 #         the merge, just marked as conflicted.  Indeed, I previously did
2121 #         argue that.  But applying directory renames to the side of
2122 #         history where a file is merely modified results in spurious
2123 #         rename/rename(1to2) conflicts -- see testcase 9h.  See also
2124 #         notes in 8d.
2125
2126 test_setup_8c () {
2127         test_create_repo 8c &&
2128         (
2129                 cd 8c &&
2130
2131                 mkdir z &&
2132                 echo b >z/b &&
2133                 echo c >z/c &&
2134                 test_seq 1 10 >z/d &&
2135                 git add z &&
2136                 test_tick &&
2137                 git commit -m "O" &&
2138
2139                 git branch O &&
2140                 git branch A &&
2141                 git branch B &&
2142
2143                 git checkout A &&
2144                 git rm z/d &&
2145                 git mv z y &&
2146                 test_tick &&
2147                 git commit -m "A" &&
2148
2149                 git checkout B &&
2150                 echo 11 >z/d &&
2151                 test_chmod +x z/d &&
2152                 echo e >z/e &&
2153                 git add z/d z/e &&
2154                 test_tick &&
2155                 git commit -m "B"
2156         )
2157 }
2158
2159 test_expect_success '8c: modify/delete or rename+modify/delete' '
2160         test_setup_8c &&
2161         (
2162                 cd 8c &&
2163
2164                 git checkout A^0 &&
2165
2166                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2167                 test_i18ngrep "CONFLICT (modify/delete).* z/d" out &&
2168
2169                 git ls-files -s >out &&
2170                 test_line_count = 5 out &&
2171                 git ls-files -u >out &&
2172                 test_line_count = 2 out &&
2173                 git ls-files -o >out &&
2174                 test_line_count = 1 out &&
2175
2176                 git rev-parse >actual \
2177                         :0:y/b :0:y/c :0:y/e :1:z/d :3:z/d &&
2178                 git rev-parse >expect \
2179                          O:z/b  O:z/c  B:z/e  O:z/d  B:z/d &&
2180                 test_cmp expect actual &&
2181
2182                 test_must_fail git rev-parse :2:z/d &&
2183                 git ls-files -s z/d | grep ^100755 &&
2184                 test_path_is_file z/d &&
2185                 test_path_is_missing y/d
2186         )
2187 '
2188
2189 # Testcase 8d, rename/delete...or not?
2190 #   (Related to testcase 5b; these may appear slightly inconsistent to users;
2191 #    Also related to testcases 7d and 7e)
2192 #   Commit O: z/{b,c,d}
2193 #   Commit A: y/{b,c}
2194 #   Commit B: z/{b,c,d,e}
2195 #   Expected: y/{b,c,e}
2196 #
2197 #   Note: It would also be somewhat reasonable to resolve this as
2198 #             y/{b,c,e}, CONFLICT(rename/delete: x/d -> y/d or deleted)
2199 #
2200 #   In this case, I'm leaning towards: commit A was the one that deleted z/d
2201 #   and it did the rename of z to y, so the two "conflicts" (rename vs.
2202 #   delete) are both coming from commit A, which is illogical.  Conflicts
2203 #   during merging are supposed to be about opposite sides doing things
2204 #   differently.
2205
2206 test_setup_8d () {
2207         test_create_repo 8d &&
2208         (
2209                 cd 8d &&
2210
2211                 mkdir z &&
2212                 echo b >z/b &&
2213                 echo c >z/c &&
2214                 test_seq 1 10 >z/d &&
2215                 git add z &&
2216                 test_tick &&
2217                 git commit -m "O" &&
2218
2219                 git branch O &&
2220                 git branch A &&
2221                 git branch B &&
2222
2223                 git checkout A &&
2224                 git rm z/d &&
2225                 git mv z y &&
2226                 test_tick &&
2227                 git commit -m "A" &&
2228
2229                 git checkout B &&
2230                 echo e >z/e &&
2231                 git add z/e &&
2232                 test_tick &&
2233                 git commit -m "B"
2234         )
2235 }
2236
2237 test_expect_success '8d: rename/delete...or not?' '
2238         test_setup_8d &&
2239         (
2240                 cd 8d &&
2241
2242                 git checkout A^0 &&
2243
2244                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2245
2246                 git ls-files -s >out &&
2247                 test_line_count = 3 out &&
2248
2249                 git rev-parse >actual \
2250                         HEAD:y/b HEAD:y/c HEAD:y/e &&
2251                 git rev-parse >expect \
2252                         O:z/b    O:z/c    B:z/e &&
2253                 test_cmp expect actual
2254         )
2255 '
2256
2257 # Testcase 8e, Both sides rename, one side adds to original directory
2258 #   Commit O: z/{b,c}
2259 #   Commit A: y/{b,c}
2260 #   Commit B: w/{b,c}, z/d
2261 #
2262 # Possible Resolutions:
2263 #   if z not considered renamed: z/d, CONFLICT(z/b -> y/b vs. w/b),
2264 #                                     CONFLICT(z/c -> y/c vs. w/c)
2265 #   if z->y rename considered:   y/d, CONFLICT(z/b -> y/b vs. w/b),
2266 #                                     CONFLICT(z/c -> y/c vs. w/c)
2267 #   Optimal:                     ??
2268 #
2269 # Notes: In commit A, directory z got renamed to y.  In commit B, directory z
2270 #        did NOT get renamed; the directory is still present; instead it is
2271 #        considered to have just renamed a subset of paths in directory z
2272 #        elsewhere.  However, this is much like testcase 6b (where commit B
2273 #        moves all the original paths out of z/ but opted to keep d
2274 #        within z/).  This makes it hard to judge where d should end up.
2275 #
2276 #        It's possible that users would get confused about this, but what
2277 #        should we do instead?  It's not at all clear to me whether z/d or
2278 #        y/d or something else is a better resolution here, and other cases
2279 #        start getting really tricky, so I just picked one.
2280
2281 test_setup_8e () {
2282         test_create_repo 8e &&
2283         (
2284                 cd 8e &&
2285
2286                 mkdir z &&
2287                 echo b >z/b &&
2288                 echo c >z/c &&
2289                 git add z &&
2290                 test_tick &&
2291                 git commit -m "O" &&
2292
2293                 git branch O &&
2294                 git branch A &&
2295                 git branch B &&
2296
2297                 git checkout A &&
2298                 git mv z y &&
2299                 test_tick &&
2300                 git commit -m "A" &&
2301
2302                 git checkout B &&
2303                 git mv z w &&
2304                 mkdir z &&
2305                 echo d >z/d &&
2306                 git add z/d &&
2307                 test_tick &&
2308                 git commit -m "B"
2309         )
2310 }
2311
2312 test_expect_success '8e: Both sides rename, one side adds to original directory' '
2313         test_setup_8e &&
2314         (
2315                 cd 8e &&
2316
2317                 git checkout A^0 &&
2318
2319                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
2320                 test_i18ngrep CONFLICT.*rename/rename.*z/c.*y/c.*w/c out &&
2321                 test_i18ngrep CONFLICT.*rename/rename.*z/b.*y/b.*w/b out &&
2322
2323                 git ls-files -s >out &&
2324                 test_line_count = 7 out &&
2325                 git ls-files -u >out &&
2326                 test_line_count = 6 out &&
2327                 git ls-files -o >out &&
2328                 test_line_count = 2 out &&
2329
2330                 git rev-parse >actual \
2331                         :1:z/b :2:y/b :3:w/b :1:z/c :2:y/c :3:w/c :0:y/d &&
2332                 git rev-parse >expect \
2333                          O:z/b  O:z/b  O:z/b  O:z/c  O:z/c  O:z/c  B:z/d &&
2334                 test_cmp expect actual &&
2335
2336                 git hash-object >actual \
2337                         y/b   w/b   y/c   w/c &&
2338                 git rev-parse >expect \
2339                         O:z/b O:z/b O:z/c O:z/c &&
2340                 test_cmp expect actual &&
2341
2342                 test_path_is_missing z/b &&
2343                 test_path_is_missing z/c
2344         )
2345 '
2346
2347 ###########################################################################
2348 # SECTION 9: Other testcases
2349 #
2350 # This section consists of miscellaneous testcases I thought of during
2351 # the implementation which round out the testing.
2352 ###########################################################################
2353
2354 # Testcase 9a, Inner renamed directory within outer renamed directory
2355 #   (Related to testcase 1f)
2356 #   Commit O: z/{b,c,d/{e,f,g}}
2357 #   Commit A: y/{b,c}, x/w/{e,f,g}
2358 #   Commit B: z/{b,c,d/{e,f,g,h},i}
2359 #   Expected: y/{b,c,i}, x/w/{e,f,g,h}
2360 #   NOTE: The only reason this one is interesting is because when a directory
2361 #         is split into multiple other directories, we determine by the weight
2362 #         of which one had the most paths going to it.  A naive implementation
2363 #         of that could take the new file in commit B at z/i to x/w/i or x/i.
2364
2365 test_setup_9a () {
2366         test_create_repo 9a &&
2367         (
2368                 cd 9a &&
2369
2370                 mkdir -p z/d &&
2371                 echo b >z/b &&
2372                 echo c >z/c &&
2373                 echo e >z/d/e &&
2374                 echo f >z/d/f &&
2375                 echo g >z/d/g &&
2376                 git add z &&
2377                 test_tick &&
2378                 git commit -m "O" &&
2379
2380                 git branch O &&
2381                 git branch A &&
2382                 git branch B &&
2383
2384                 git checkout A &&
2385                 mkdir x &&
2386                 git mv z/d x/w &&
2387                 git mv z y &&
2388                 test_tick &&
2389                 git commit -m "A" &&
2390
2391                 git checkout B &&
2392                 echo h >z/d/h &&
2393                 echo i >z/i &&
2394                 git add z &&
2395                 test_tick &&
2396                 git commit -m "B"
2397         )
2398 }
2399
2400 test_expect_success '9a: Inner renamed directory within outer renamed directory' '
2401         test_setup_9a &&
2402         (
2403                 cd 9a &&
2404
2405                 git checkout A^0 &&
2406
2407                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2408
2409                 git ls-files -s >out &&
2410                 test_line_count = 7 out &&
2411                 git ls-files -u >out &&
2412                 test_line_count = 0 out &&
2413                 git ls-files -o >out &&
2414                 test_line_count = 1 out &&
2415
2416                 git rev-parse >actual \
2417                         HEAD:y/b HEAD:y/c HEAD:y/i &&
2418                 git rev-parse >expect \
2419                         O:z/b    O:z/c    B:z/i &&
2420                 test_cmp expect actual &&
2421
2422                 git rev-parse >actual \
2423                         HEAD:x/w/e HEAD:x/w/f HEAD:x/w/g HEAD:x/w/h &&
2424                 git rev-parse >expect \
2425                         O:z/d/e    O:z/d/f    O:z/d/g    B:z/d/h &&
2426                 test_cmp expect actual
2427         )
2428 '
2429
2430 # Testcase 9b, Transitive rename with content merge
2431 #   (Related to testcase 1c)
2432 #   Commit O: z/{b,c},   x/d_1
2433 #   Commit A: y/{b,c},   x/d_2
2434 #   Commit B: z/{b,c,d_3}
2435 #   Expected: y/{b,c,d_merged}
2436
2437 test_setup_9b () {
2438         test_create_repo 9b &&
2439         (
2440                 cd 9b &&
2441
2442                 mkdir z &&
2443                 echo b >z/b &&
2444                 echo c >z/c &&
2445                 mkdir x &&
2446                 test_seq 1 10 >x/d &&
2447                 git add z x &&
2448                 test_tick &&
2449                 git commit -m "O" &&
2450
2451                 git branch O &&
2452                 git branch A &&
2453                 git branch B &&
2454
2455                 git checkout A &&
2456                 git mv z y &&
2457                 test_seq 1 11 >x/d &&
2458                 git add x/d &&
2459                 test_tick &&
2460                 git commit -m "A" &&
2461
2462                 git checkout B &&
2463                 test_seq 0 10 >x/d &&
2464                 git mv x/d z/d &&
2465                 git add z/d &&
2466                 test_tick &&
2467                 git commit -m "B"
2468         )
2469 }
2470
2471 test_expect_success '9b: Transitive rename with content merge' '
2472         test_setup_9b &&
2473         (
2474                 cd 9b &&
2475
2476                 git checkout A^0 &&
2477
2478                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2479
2480                 git ls-files -s >out &&
2481                 test_line_count = 3 out &&
2482
2483                 test_seq 0 11 >expected &&
2484                 test_cmp expected y/d &&
2485                 git add expected &&
2486                 git rev-parse >actual \
2487                         HEAD:y/b HEAD:y/c HEAD:y/d &&
2488                 git rev-parse >expect \
2489                         O:z/b    O:z/c    :0:expected &&
2490                 test_cmp expect actual &&
2491                 test_must_fail git rev-parse HEAD:x/d &&
2492                 test_must_fail git rev-parse HEAD:z/d &&
2493                 test_path_is_missing z/d &&
2494
2495                 test $(git rev-parse HEAD:y/d) != $(git rev-parse O:x/d) &&
2496                 test $(git rev-parse HEAD:y/d) != $(git rev-parse A:x/d) &&
2497                 test $(git rev-parse HEAD:y/d) != $(git rev-parse B:z/d)
2498         )
2499 '
2500
2501 # Testcase 9c, Doubly transitive rename?
2502 #   (Related to testcase 1c, 7e, and 9d)
2503 #   Commit O: z/{b,c},     x/{d,e},    w/f
2504 #   Commit A: y/{b,c},     x/{d,e,f,g}
2505 #   Commit B: z/{b,c,d,e},             w/f
2506 #   Expected: y/{b,c,d,e}, x/{f,g}
2507 #
2508 #   NOTE: x/f and x/g may be slightly confusing here.  The rename from w/f to
2509 #         x/f is clear.  Let's look beyond that.  Here's the logic:
2510 #            Commit B renamed x/ -> z/
2511 #            Commit A renamed z/ -> y/
2512 #         So, we could possibly further rename x/f to z/f to y/f, a doubly
2513 #         transient rename.  However, where does it end?  We can chain these
2514 #         indefinitely (see testcase 9d).  What if there is a D/F conflict
2515 #         at z/f/ or y/f/?  Or just another file conflict at one of those
2516 #         paths?  In the case of an N-long chain of transient renamings,
2517 #         where do we "abort" the rename at?  Can the user make sense of
2518 #         the resulting conflict and resolve it?
2519 #
2520 #         To avoid this confusion I use the simple rule that if the other side
2521 #         of history did a directory rename to a path that your side renamed
2522 #         away, then ignore that particular rename from the other side of
2523 #         history for any implicit directory renames.
2524
2525 test_setup_9c () {
2526         test_create_repo 9c &&
2527         (
2528                 cd 9c &&
2529
2530                 mkdir z &&
2531                 echo b >z/b &&
2532                 echo c >z/c &&
2533                 mkdir x &&
2534                 echo d >x/d &&
2535                 echo e >x/e &&
2536                 mkdir w &&
2537                 echo f >w/f &&
2538                 git add z x w &&
2539                 test_tick &&
2540                 git commit -m "O" &&
2541
2542                 git branch O &&
2543                 git branch A &&
2544                 git branch B &&
2545
2546                 git checkout A &&
2547                 git mv z y &&
2548                 git mv w/f x/ &&
2549                 echo g >x/g &&
2550                 git add x/g &&
2551                 test_tick &&
2552                 git commit -m "A" &&
2553
2554                 git checkout B &&
2555                 git mv x/d z/d &&
2556                 git mv x/e z/e &&
2557                 test_tick &&
2558                 git commit -m "B"
2559         )
2560 }
2561
2562 test_expect_success '9c: Doubly transitive rename?' '
2563         test_setup_9c &&
2564         (
2565                 cd 9c &&
2566
2567                 git checkout A^0 &&
2568
2569                 git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2570                 test_i18ngrep "WARNING: Avoiding applying x -> z rename to x/f" out &&
2571
2572                 git ls-files -s >out &&
2573                 test_line_count = 6 out &&
2574                 git ls-files -o >out &&
2575                 test_line_count = 1 out &&
2576
2577                 git rev-parse >actual \
2578                         HEAD:y/b HEAD:y/c HEAD:y/d HEAD:y/e HEAD:x/f HEAD:x/g &&
2579                 git rev-parse >expect \
2580                         O:z/b    O:z/c    O:x/d    O:x/e    O:w/f    A:x/g &&
2581                 test_cmp expect actual
2582         )
2583 '
2584
2585 # Testcase 9d, N-fold transitive rename?
2586 #   (Related to testcase 9c...and 1c and 7e)
2587 #   Commit O: z/a, y/b, x/c, w/d, v/e, u/f
2588 #   Commit A:  y/{a,b},  w/{c,d},  u/{e,f}
2589 #   Commit B: z/{a,t}, x/{b,c}, v/{d,e}, u/f
2590 #   Expected: <see NOTE first>
2591 #
2592 #   NOTE: z/ -> y/ (in commit A)
2593 #         y/ -> x/ (in commit B)
2594 #         x/ -> w/ (in commit A)
2595 #         w/ -> v/ (in commit B)
2596 #         v/ -> u/ (in commit A)
2597 #         So, if we add a file to z, say z/t, where should it end up?  In u?
2598 #         What if there's another file or directory named 't' in one of the
2599 #         intervening directories and/or in u itself?  Also, shouldn't the
2600 #         same logic that places 't' in u/ also move ALL other files to u/?
2601 #         What if there are file or directory conflicts in any of them?  If
2602 #         we attempted to do N-way (N-fold? N-ary? N-uple?) transitive renames
2603 #         like this, would the user have any hope of understanding any
2604 #         conflicts or how their working tree ended up?  I think not, so I'm
2605 #         ruling out N-ary transitive renames for N>1.
2606 #
2607 #   Therefore our expected result is:
2608 #     z/t, y/a, x/b, w/c, u/d, u/e, u/f
2609 #   The reason that v/d DOES get transitively renamed to u/d is that u/ isn't
2610 #   renamed somewhere.  A slightly sub-optimal result, but it uses fairly
2611 #   simple rules that are consistent with what we need for all the other
2612 #   testcases and simplifies things for the user.
2613
2614 test_setup_9d () {
2615         test_create_repo 9d &&
2616         (
2617                 cd 9d &&
2618
2619                 mkdir z y x w v u &&
2620                 echo a >z/a &&
2621                 echo b >y/b &&
2622                 echo c >x/c &&
2623                 echo d >w/d &&
2624                 echo e >v/e &&
2625                 echo f >u/f &&
2626                 git add z y x w v u &&
2627                 test_tick &&
2628                 git commit -m "O" &&
2629
2630                 git branch O &&
2631                 git branch A &&
2632                 git branch B &&
2633
2634                 git checkout A &&
2635                 git mv z/a y/ &&
2636                 git mv x/c w/ &&
2637                 git mv v/e u/ &&
2638                 test_tick &&
2639                 git commit -m "A" &&
2640
2641                 git checkout B &&
2642                 echo t >z/t &&
2643                 git mv y/b x/ &&
2644                 git mv w/d v/ &&
2645                 git add z/t &&
2646                 test_tick &&
2647                 git commit -m "B"
2648         )
2649 }
2650
2651 test_expect_success '9d: N-way transitive rename?' '
2652         test_setup_9d &&
2653         (
2654                 cd 9d &&
2655
2656                 git checkout A^0 &&
2657
2658                 git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2659                 test_i18ngrep "WARNING: Avoiding applying z -> y rename to z/t" out &&
2660                 test_i18ngrep "WARNING: Avoiding applying y -> x rename to y/a" out &&
2661                 test_i18ngrep "WARNING: Avoiding applying x -> w rename to x/b" out &&
2662                 test_i18ngrep "WARNING: Avoiding applying w -> v rename to w/c" out &&
2663
2664                 git ls-files -s >out &&
2665                 test_line_count = 7 out &&
2666                 git ls-files -o >out &&
2667                 test_line_count = 1 out &&
2668
2669                 git rev-parse >actual \
2670                         HEAD:z/t \
2671                         HEAD:y/a HEAD:x/b HEAD:w/c \
2672                         HEAD:u/d HEAD:u/e HEAD:u/f &&
2673                 git rev-parse >expect \
2674                         B:z/t    \
2675                         O:z/a    O:y/b    O:x/c    \
2676                         O:w/d    O:v/e    A:u/f &&
2677                 test_cmp expect actual
2678         )
2679 '
2680
2681 # Testcase 9e, N-to-1 whammo
2682 #   (Related to testcase 9c...and 1c and 7e)
2683 #   Commit O: dir1/{a,b}, dir2/{d,e}, dir3/{g,h}, dirN/{j,k}
2684 #   Commit A: dir1/{a,b,c,yo}, dir2/{d,e,f,yo}, dir3/{g,h,i,yo}, dirN/{j,k,l,yo}
2685 #   Commit B: combined/{a,b,d,e,g,h,j,k}
2686 #   Expected: combined/{a,b,c,d,e,f,g,h,i,j,k,l}, CONFLICT(Nto1) warnings,
2687 #             dir1/yo, dir2/yo, dir3/yo, dirN/yo
2688
2689 test_setup_9e () {
2690         test_create_repo 9e &&
2691         (
2692                 cd 9e &&
2693
2694                 mkdir dir1 dir2 dir3 dirN &&
2695                 echo a >dir1/a &&
2696                 echo b >dir1/b &&
2697                 echo d >dir2/d &&
2698                 echo e >dir2/e &&
2699                 echo g >dir3/g &&
2700                 echo h >dir3/h &&
2701                 echo j >dirN/j &&
2702                 echo k >dirN/k &&
2703                 git add dir* &&
2704                 test_tick &&
2705                 git commit -m "O" &&
2706
2707                 git branch O &&
2708                 git branch A &&
2709                 git branch B &&
2710
2711                 git checkout A &&
2712                 echo c  >dir1/c &&
2713                 echo yo >dir1/yo &&
2714                 echo f  >dir2/f &&
2715                 echo yo >dir2/yo &&
2716                 echo i  >dir3/i &&
2717                 echo yo >dir3/yo &&
2718                 echo l  >dirN/l &&
2719                 echo yo >dirN/yo &&
2720                 git add dir* &&
2721                 test_tick &&
2722                 git commit -m "A" &&
2723
2724                 git checkout B &&
2725                 git mv dir1 combined &&
2726                 git mv dir2/* combined/ &&
2727                 git mv dir3/* combined/ &&
2728                 git mv dirN/* combined/ &&
2729                 test_tick &&
2730                 git commit -m "B"
2731         )
2732 }
2733
2734 test_expect_success C_LOCALE_OUTPUT '9e: N-to-1 whammo' '
2735         test_setup_9e &&
2736         (
2737                 cd 9e &&
2738
2739                 git checkout A^0 &&
2740
2741                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2742                 grep "CONFLICT (implicit dir rename): Cannot map more than one path to combined/yo" out >error_line &&
2743                 grep -q dir1/yo error_line &&
2744                 grep -q dir2/yo error_line &&
2745                 grep -q dir3/yo error_line &&
2746                 grep -q dirN/yo error_line &&
2747
2748                 git ls-files -s >out &&
2749                 test_line_count = 16 out &&
2750                 git ls-files -u >out &&
2751                 test_line_count = 0 out &&
2752                 git ls-files -o >out &&
2753                 test_line_count = 2 out &&
2754
2755                 git rev-parse >actual \
2756                         :0:combined/a :0:combined/b :0:combined/c \
2757                         :0:combined/d :0:combined/e :0:combined/f \
2758                         :0:combined/g :0:combined/h :0:combined/i \
2759                         :0:combined/j :0:combined/k :0:combined/l &&
2760                 git rev-parse >expect \
2761                          O:dir1/a      O:dir1/b      A:dir1/c \
2762                          O:dir2/d      O:dir2/e      A:dir2/f \
2763                          O:dir3/g      O:dir3/h      A:dir3/i \
2764                          O:dirN/j      O:dirN/k      A:dirN/l &&
2765                 test_cmp expect actual &&
2766
2767                 git rev-parse >actual \
2768                         :0:dir1/yo :0:dir2/yo :0:dir3/yo :0:dirN/yo &&
2769                 git rev-parse >expect \
2770                          A:dir1/yo  A:dir2/yo  A:dir3/yo  A:dirN/yo &&
2771                 test_cmp expect actual
2772         )
2773 '
2774
2775 # Testcase 9f, Renamed directory that only contained immediate subdirs
2776 #   (Related to testcases 1e & 9g)
2777 #   Commit O: goal/{a,b}/$more_files
2778 #   Commit A: priority/{a,b}/$more_files
2779 #   Commit B: goal/{a,b}/$more_files, goal/c
2780 #   Expected: priority/{a,b}/$more_files, priority/c
2781
2782 test_setup_9f () {
2783         test_create_repo 9f &&
2784         (
2785                 cd 9f &&
2786
2787                 mkdir -p goal/a &&
2788                 mkdir -p goal/b &&
2789                 echo foo >goal/a/foo &&
2790                 echo bar >goal/b/bar &&
2791                 echo baz >goal/b/baz &&
2792                 git add goal &&
2793                 test_tick &&
2794                 git commit -m "O" &&
2795
2796                 git branch O &&
2797                 git branch A &&
2798                 git branch B &&
2799
2800                 git checkout A &&
2801                 git mv goal/ priority &&
2802                 test_tick &&
2803                 git commit -m "A" &&
2804
2805                 git checkout B &&
2806                 echo c >goal/c &&
2807                 git add goal/c &&
2808                 test_tick &&
2809                 git commit -m "B"
2810         )
2811 }
2812
2813 test_expect_success '9f: Renamed directory that only contained immediate subdirs' '
2814         test_setup_9f &&
2815         (
2816                 cd 9f &&
2817
2818                 git checkout A^0 &&
2819
2820                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2821
2822                 git ls-files -s >out &&
2823                 test_line_count = 4 out &&
2824
2825                 git rev-parse >actual \
2826                         HEAD:priority/a/foo \
2827                         HEAD:priority/b/bar \
2828                         HEAD:priority/b/baz \
2829                         HEAD:priority/c &&
2830                 git rev-parse >expect \
2831                         O:goal/a/foo \
2832                         O:goal/b/bar \
2833                         O:goal/b/baz \
2834                         B:goal/c &&
2835                 test_cmp expect actual &&
2836                 test_must_fail git rev-parse HEAD:goal/c
2837         )
2838 '
2839
2840 # Testcase 9g, Renamed directory that only contained immediate subdirs, immediate subdirs renamed
2841 #   (Related to testcases 1e & 9f)
2842 #   Commit O: goal/{a,b}/$more_files
2843 #   Commit A: priority/{alpha,bravo}/$more_files
2844 #   Commit B: goal/{a,b}/$more_files, goal/c
2845 #   Expected: priority/{alpha,bravo}/$more_files, priority/c
2846 # We currently fail this test because the directory renames we detect are
2847 #   goal/a/ -> priority/alpha/
2848 #   goal/b/ -> priority/bravo/
2849 # We do not detect
2850 #   goal/   -> priority/
2851 # because of no files found within goal/, and the fact that "a" != "alpha"
2852 # and "b" != "bravo".  But I'm not sure it's really a failure given that
2853 # viewpoint...
2854
2855 test_setup_9g () {
2856         test_create_repo 9g &&
2857         (
2858                 cd 9g &&
2859
2860                 mkdir -p goal/a &&
2861                 mkdir -p goal/b &&
2862                 echo foo >goal/a/foo &&
2863                 echo bar >goal/b/bar &&
2864                 echo baz >goal/b/baz &&
2865                 git add goal &&
2866                 test_tick &&
2867                 git commit -m "O" &&
2868
2869                 git branch O &&
2870                 git branch A &&
2871                 git branch B &&
2872
2873                 git checkout A &&
2874                 mkdir priority &&
2875                 git mv goal/a/ priority/alpha &&
2876                 git mv goal/b/ priority/beta &&
2877                 rmdir goal/ &&
2878                 test_tick &&
2879                 git commit -m "A" &&
2880
2881                 git checkout B &&
2882                 echo c >goal/c &&
2883                 git add goal/c &&
2884                 test_tick &&
2885                 git commit -m "B"
2886         )
2887 }
2888
2889 test_expect_failure '9g: Renamed directory that only contained immediate subdirs, immediate subdirs renamed' '
2890         test_setup_9g &&
2891         (
2892                 cd 9g &&
2893
2894                 git checkout A^0 &&
2895
2896                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2897
2898                 git ls-files -s >out &&
2899                 test_line_count = 4 out &&
2900
2901                 git rev-parse >actual \
2902                         HEAD:priority/alpha/foo \
2903                         HEAD:priority/beta/bar  \
2904                         HEAD:priority/beta/baz  \
2905                         HEAD:priority/c &&
2906                 git rev-parse >expect \
2907                         O:goal/a/foo \
2908                         O:goal/b/bar \
2909                         O:goal/b/baz \
2910                         B:goal/c &&
2911                 test_cmp expect actual &&
2912                 test_must_fail git rev-parse HEAD:goal/c
2913         )
2914 '
2915
2916 # Testcase 9h, Avoid implicit rename if involved as source on other side
2917 #   (Extremely closely related to testcase 3a)
2918 #   Commit O: z/{b,c,d_1}
2919 #   Commit A: z/{b,c,d_2}
2920 #   Commit B: y/{b,c}, x/d_1
2921 #   Expected: y/{b,c}, x/d_2
2922 #   NOTE: If we applied the z/ -> y/ rename to z/d, then we'd end up with
2923 #         a rename/rename(1to2) conflict (z/d -> y/d vs. x/d)
2924 test_setup_9h () {
2925         test_create_repo 9h &&
2926         (
2927                 cd 9h &&
2928
2929                 mkdir z &&
2930                 echo b >z/b &&
2931                 echo c >z/c &&
2932                 printf "1\n2\n3\n4\n5\n6\n7\n8\nd\n" >z/d &&
2933                 git add z &&
2934                 test_tick &&
2935                 git commit -m "O" &&
2936
2937                 git branch O &&
2938                 git branch A &&
2939                 git branch B &&
2940
2941                 git checkout A &&
2942                 test_tick &&
2943                 echo more >>z/d &&
2944                 git add z/d &&
2945                 git commit -m "A" &&
2946
2947                 git checkout B &&
2948                 mkdir y &&
2949                 mkdir x &&
2950                 git mv z/b y/ &&
2951                 git mv z/c y/ &&
2952                 git mv z/d x/ &&
2953                 rmdir z &&
2954                 test_tick &&
2955                 git commit -m "B"
2956         )
2957 }
2958
2959 test_expect_success '9h: Avoid dir rename on merely modified path' '
2960         test_setup_9h &&
2961         (
2962                 cd 9h &&
2963
2964                 git checkout A^0 &&
2965
2966                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2967
2968                 git ls-files -s >out &&
2969                 test_line_count = 3 out &&
2970
2971                 git rev-parse >actual \
2972                         HEAD:y/b HEAD:y/c HEAD:x/d &&
2973                 git rev-parse >expect \
2974                         O:z/b    O:z/c    A:z/d &&
2975                 test_cmp expect actual
2976         )
2977 '
2978
2979 ###########################################################################
2980 # Rules suggested by section 9:
2981 #
2982 #   If the other side of history did a directory rename to a path that your
2983 #   side renamed away, then ignore that particular rename from the other
2984 #   side of history for any implicit directory renames.
2985 ###########################################################################
2986
2987 ###########################################################################
2988 # SECTION 10: Handling untracked files
2989 #
2990 # unpack_trees(), upon which the recursive merge algorithm is based, aborts
2991 # the operation if untracked or dirty files would be deleted or overwritten
2992 # by the merge.  Unfortunately, unpack_trees() does not understand renames,
2993 # and if it doesn't abort, then it muddies up the working directory before
2994 # we even get to the point of detecting renames, so we need some special
2995 # handling, at least in the case of directory renames.
2996 ###########################################################################
2997
2998 # Testcase 10a, Overwrite untracked: normal rename/delete
2999 #   Commit O: z/{b,c_1}
3000 #   Commit A: z/b + untracked z/c + untracked z/d
3001 #   Commit B: z/{b,d_1}
3002 #   Expected: Aborted Merge +
3003 #       ERROR_MSG(untracked working tree files would be overwritten by merge)
3004
3005 test_setup_10a () {
3006         test_create_repo 10a &&
3007         (
3008                 cd 10a &&
3009
3010                 mkdir z &&
3011                 echo b >z/b &&
3012                 echo c >z/c &&
3013                 git add z &&
3014                 test_tick &&
3015                 git commit -m "O" &&
3016
3017                 git branch O &&
3018                 git branch A &&
3019                 git branch B &&
3020
3021                 git checkout A &&
3022                 git rm z/c &&
3023                 test_tick &&
3024                 git commit -m "A" &&
3025
3026                 git checkout B &&
3027                 git mv z/c z/d &&
3028                 test_tick &&
3029                 git commit -m "B"
3030         )
3031 }
3032
3033 test_expect_success '10a: Overwrite untracked with normal rename/delete' '
3034         test_setup_10a &&
3035         (
3036                 cd 10a &&
3037
3038                 git checkout A^0 &&
3039                 echo very >z/c &&
3040                 echo important >z/d &&
3041
3042                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3043                 test_i18ngrep "The following untracked working tree files would be overwritten by merge" err &&
3044
3045                 git ls-files -s >out &&
3046                 test_line_count = 1 out &&
3047                 git ls-files -o >out &&
3048                 test_line_count = 4 out &&
3049
3050                 echo very >expect &&
3051                 test_cmp expect z/c &&
3052
3053                 echo important >expect &&
3054                 test_cmp expect z/d &&
3055
3056                 git rev-parse HEAD:z/b >actual &&
3057                 git rev-parse O:z/b >expect &&
3058                 test_cmp expect actual
3059         )
3060 '
3061
3062 # Testcase 10b, Overwrite untracked: dir rename + delete
3063 #   Commit O: z/{b,c_1}
3064 #   Commit A: y/b + untracked y/{c,d,e}
3065 #   Commit B: z/{b,d_1,e}
3066 #   Expected: Failed Merge; y/b + untracked y/c + untracked y/d on disk +
3067 #             z/c_1 -> z/d_1 rename recorded at stage 3 for y/d +
3068 #       ERROR_MSG(refusing to lose untracked file at 'y/d')
3069
3070 test_setup_10b () {
3071         test_create_repo 10b &&
3072         (
3073                 cd 10b &&
3074
3075                 mkdir z &&
3076                 echo b >z/b &&
3077                 echo c >z/c &&
3078                 git add z &&
3079                 test_tick &&
3080                 git commit -m "O" &&
3081
3082                 git branch O &&
3083                 git branch A &&
3084                 git branch B &&
3085
3086                 git checkout A &&
3087                 git rm z/c &&
3088                 git mv z/ y/ &&
3089                 test_tick &&
3090                 git commit -m "A" &&
3091
3092                 git checkout B &&
3093                 git mv z/c z/d &&
3094                 echo e >z/e &&
3095                 git add z/e &&
3096                 test_tick &&
3097                 git commit -m "B"
3098         )
3099 }
3100
3101 test_expect_success '10b: Overwrite untracked with dir rename + delete' '
3102         test_setup_10b &&
3103         (
3104                 cd 10b &&
3105
3106                 git checkout A^0 &&
3107                 echo very >y/c &&
3108                 echo important >y/d &&
3109                 echo contents >y/e &&
3110
3111                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3112                 test_i18ngrep "CONFLICT (rename/delete).*Version B\^0 of y/d left in tree at y/d~B\^0" out &&
3113                 test_i18ngrep "Error: Refusing to lose untracked file at y/e; writing to y/e~B\^0 instead" out &&
3114
3115                 git ls-files -s >out &&
3116                 test_line_count = 3 out &&
3117                 git ls-files -u >out &&
3118                 test_line_count = 2 out &&
3119                 git ls-files -o >out &&
3120                 test_line_count = 5 out &&
3121
3122                 git rev-parse >actual \
3123                         :0:y/b :3:y/d :3:y/e &&
3124                 git rev-parse >expect \
3125                         O:z/b  O:z/c  B:z/e &&
3126                 test_cmp expect actual &&
3127
3128                 echo very >expect &&
3129                 test_cmp expect y/c &&
3130
3131                 echo important >expect &&
3132                 test_cmp expect y/d &&
3133
3134                 echo contents >expect &&
3135                 test_cmp expect y/e
3136         )
3137 '
3138
3139 # Testcase 10c, Overwrite untracked: dir rename/rename(1to2)
3140 #   Commit O: z/{a,b}, x/{c,d}
3141 #   Commit A: y/{a,b}, w/c, x/d + different untracked y/c
3142 #   Commit B: z/{a,b,c}, x/d
3143 #   Expected: Failed Merge; y/{a,b} + x/d + untracked y/c +
3144 #             CONFLICT(rename/rename) x/c -> w/c vs y/c +
3145 #             y/c~B^0 +
3146 #             ERROR_MSG(Refusing to lose untracked file at y/c)
3147
3148 test_setup_10c () {
3149         test_create_repo 10c_$1 &&
3150         (
3151                 cd 10c_$1 &&
3152
3153                 mkdir z x &&
3154                 echo a >z/a &&
3155                 echo b >z/b &&
3156                 echo c >x/c &&
3157                 echo d >x/d &&
3158                 git add z x &&
3159                 test_tick &&
3160                 git commit -m "O" &&
3161
3162                 git branch O &&
3163                 git branch A &&
3164                 git branch B &&
3165
3166                 git checkout A &&
3167                 mkdir w &&
3168                 git mv x/c w/c &&
3169                 git mv z/ y/ &&
3170                 test_tick &&
3171                 git commit -m "A" &&
3172
3173                 git checkout B &&
3174                 git mv x/c z/ &&
3175                 test_tick &&
3176                 git commit -m "B"
3177         )
3178 }
3179
3180 test_expect_success '10c1: Overwrite untracked with dir rename/rename(1to2)' '
3181         test_setup_10c 1 &&
3182         (
3183                 cd 10c_1 &&
3184
3185                 git checkout A^0 &&
3186                 echo important >y/c &&
3187
3188                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3189                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3190                 test_i18ngrep "Refusing to lose untracked file at y/c; adding as y/c~B\^0 instead" out &&
3191
3192                 git ls-files -s >out &&
3193                 test_line_count = 6 out &&
3194                 git ls-files -u >out &&
3195                 test_line_count = 3 out &&
3196                 git ls-files -o >out &&
3197                 test_line_count = 3 out &&
3198
3199                 git rev-parse >actual \
3200                         :0:y/a :0:y/b :0:x/d :1:x/c :2:w/c :3:y/c &&
3201                 git rev-parse >expect \
3202                          O:z/a  O:z/b  O:x/d  O:x/c  O:x/c  O:x/c &&
3203                 test_cmp expect actual &&
3204
3205                 git hash-object y/c~B^0 >actual &&
3206                 git rev-parse O:x/c >expect &&
3207                 test_cmp expect actual &&
3208
3209                 echo important >expect &&
3210                 test_cmp expect y/c
3211         )
3212 '
3213
3214 test_expect_success '10c2: Overwrite untracked with dir rename/rename(1to2), other direction' '
3215         test_setup_10c 2 &&
3216         (
3217                 cd 10c_2 &&
3218
3219                 git reset --hard &&
3220                 git clean -fdqx &&
3221
3222                 git checkout B^0 &&
3223                 mkdir y &&
3224                 echo important >y/c &&
3225
3226                 test_must_fail git -c merge.directoryRenames=true merge -s recursive A^0 >out 2>err &&
3227                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3228                 test_i18ngrep "Refusing to lose untracked file at y/c; adding as y/c~HEAD instead" out &&
3229
3230                 git ls-files -s >out &&
3231                 test_line_count = 6 out &&
3232                 git ls-files -u >out &&
3233                 test_line_count = 3 out &&
3234                 git ls-files -o >out &&
3235                 test_line_count = 3 out &&
3236
3237                 git rev-parse >actual \
3238                         :0:y/a :0:y/b :0:x/d :1:x/c :3:w/c :2:y/c &&
3239                 git rev-parse >expect \
3240                          O:z/a  O:z/b  O:x/d  O:x/c  O:x/c  O:x/c &&
3241                 test_cmp expect actual &&
3242
3243                 git hash-object y/c~HEAD >actual &&
3244                 git rev-parse O:x/c >expect &&
3245                 test_cmp expect actual &&
3246
3247                 echo important >expect &&
3248                 test_cmp expect y/c
3249         )
3250 '
3251
3252 # Testcase 10d, Delete untracked w/ dir rename/rename(2to1)
3253 #   Commit O: z/{a,b,c_1},        x/{d,e,f_2}
3254 #   Commit A: y/{a,b},            x/{d,e,f_2,wham_1} + untracked y/wham
3255 #   Commit B: z/{a,b,c_1,wham_2}, y/{d,e}
3256 #   Expected: Failed Merge; y/{a,b,d,e} + untracked y/{wham,wham~merged}+
3257 #             CONFLICT(rename/rename) z/c_1 vs x/f_2 -> y/wham
3258 #             ERROR_MSG(Refusing to lose untracked file at y/wham)
3259
3260 test_setup_10d () {
3261         test_create_repo 10d &&
3262         (
3263                 cd 10d &&
3264
3265                 mkdir z x &&
3266                 echo a >z/a &&
3267                 echo b >z/b &&
3268                 echo c >z/c &&
3269                 echo d >x/d &&
3270                 echo e >x/e &&
3271                 echo f >x/f &&
3272                 git add z x &&
3273                 test_tick &&
3274                 git commit -m "O" &&
3275
3276                 git branch O &&
3277                 git branch A &&
3278                 git branch B &&
3279
3280                 git checkout A &&
3281                 git mv z/c x/wham &&
3282                 git mv z/ y/ &&
3283                 test_tick &&
3284                 git commit -m "A" &&
3285
3286                 git checkout B &&
3287                 git mv x/f z/wham &&
3288                 git mv x/ y/ &&
3289                 test_tick &&
3290                 git commit -m "B"
3291         )
3292 }
3293
3294 test_expect_success '10d: Delete untracked with dir rename/rename(2to1)' '
3295         test_setup_10d &&
3296         (
3297                 cd 10d &&
3298
3299                 git checkout A^0 &&
3300                 echo important >y/wham &&
3301
3302                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3303                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3304                 test_i18ngrep "Refusing to lose untracked file at y/wham" out &&
3305
3306                 git ls-files -s >out &&
3307                 test_line_count = 6 out &&
3308                 git ls-files -u >out &&
3309                 test_line_count = 2 out &&
3310                 git ls-files -o >out &&
3311                 test_line_count = 3 out &&
3312
3313                 git rev-parse >actual \
3314                         :0:y/a :0:y/b :0:y/d :0:y/e :2:y/wham :3:y/wham &&
3315                 git rev-parse >expect \
3316                          O:z/a  O:z/b  O:x/d  O:x/e  O:z/c     O:x/f &&
3317                 test_cmp expect actual &&
3318
3319                 test_must_fail git rev-parse :1:y/wham &&
3320
3321                 echo important >expect &&
3322                 test_cmp expect y/wham &&
3323
3324                 # Test that the two-way merge in y/wham~merged is as expected
3325                 git cat-file -p :2:y/wham >expect &&
3326                 git cat-file -p :3:y/wham >other &&
3327                 >empty &&
3328                 test_must_fail git merge-file \
3329                         -L "HEAD" \
3330                         -L "" \
3331                         -L "B^0" \
3332                         expect empty other &&
3333                 test_cmp expect y/wham~merged
3334         )
3335 '
3336
3337 # Testcase 10e, Does git complain about untracked file that's not in the way?
3338 #   Commit O: z/{a,b}
3339 #   Commit A: y/{a,b} + untracked z/c
3340 #   Commit B: z/{a,b,c}
3341 #   Expected: y/{a,b,c} + untracked z/c
3342
3343 test_setup_10e () {
3344         test_create_repo 10e &&
3345         (
3346                 cd 10e &&
3347
3348                 mkdir z &&
3349                 echo a >z/a &&
3350                 echo b >z/b &&
3351                 git add z &&
3352                 test_tick &&
3353                 git commit -m "O" &&
3354
3355                 git branch O &&
3356                 git branch A &&
3357                 git branch B &&
3358
3359                 git checkout A &&
3360                 git mv z/ y/ &&
3361                 test_tick &&
3362                 git commit -m "A" &&
3363
3364                 git checkout B &&
3365                 echo c >z/c &&
3366                 git add z/c &&
3367                 test_tick &&
3368                 git commit -m "B"
3369         )
3370 }
3371
3372 test_expect_failure '10e: Does git complain about untracked file that is not really in the way?' '
3373         test_setup_10e &&
3374         (
3375                 cd 10e &&
3376
3377                 git checkout A^0 &&
3378                 mkdir z &&
3379                 echo random >z/c &&
3380
3381                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3382                 test_i18ngrep ! "following untracked working tree files would be overwritten by merge" err &&
3383
3384                 git ls-files -s >out &&
3385                 test_line_count = 3 out &&
3386                 git ls-files -u >out &&
3387                 test_line_count = 0 out &&
3388                 git ls-files -o >out &&
3389                 test_line_count = 3 out &&
3390
3391                 git rev-parse >actual \
3392                         :0:y/a :0:y/b :0:y/c &&
3393                 git rev-parse >expect \
3394                          O:z/a  O:z/b  B:z/c &&
3395                 test_cmp expect actual &&
3396
3397                 echo random >expect &&
3398                 test_cmp expect z/c
3399         )
3400 '
3401
3402 ###########################################################################
3403 # SECTION 11: Handling dirty (not up-to-date) files
3404 #
3405 # unpack_trees(), upon which the recursive merge algorithm is based, aborts
3406 # the operation if untracked or dirty files would be deleted or overwritten
3407 # by the merge.  Unfortunately, unpack_trees() does not understand renames,
3408 # and if it doesn't abort, then it muddies up the working directory before
3409 # we even get to the point of detecting renames, so we need some special
3410 # handling.  This was true even of normal renames, but there are additional
3411 # codepaths that need special handling with directory renames.  Add
3412 # testcases for both renamed-by-directory-rename-detection and standard
3413 # rename cases.
3414 ###########################################################################
3415
3416 # Testcase 11a, Avoid losing dirty contents with simple rename
3417 #   Commit O: z/{a,b_v1},
3418 #   Commit A: z/{a,c_v1}, and z/c_v1 has uncommitted mods
3419 #   Commit B: z/{a,b_v2}
3420 #   Expected: ERROR_MSG(Refusing to lose dirty file at z/c) +
3421 #             z/a, staged version of z/c has sha1sum matching B:z/b_v2,
3422 #             z/c~HEAD with contents of B:z/b_v2,
3423 #             z/c with uncommitted mods on top of A:z/c_v1
3424
3425 test_setup_11a () {
3426         test_create_repo 11a &&
3427         (
3428                 cd 11a &&
3429
3430                 mkdir z &&
3431                 echo a >z/a &&
3432                 test_seq 1 10 >z/b &&
3433                 git add z &&
3434                 test_tick &&
3435                 git commit -m "O" &&
3436
3437                 git branch O &&
3438                 git branch A &&
3439                 git branch B &&
3440
3441                 git checkout A &&
3442                 git mv z/b z/c &&
3443                 test_tick &&
3444                 git commit -m "A" &&
3445
3446                 git checkout B &&
3447                 echo 11 >>z/b &&
3448                 git add z/b &&
3449                 test_tick &&
3450                 git commit -m "B"
3451         )
3452 }
3453
3454 test_expect_success '11a: Avoid losing dirty contents with simple rename' '
3455         test_setup_11a &&
3456         (
3457                 cd 11a &&
3458
3459                 git checkout A^0 &&
3460                 echo stuff >>z/c &&
3461
3462                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3463                 test_i18ngrep "Refusing to lose dirty file at z/c" out &&
3464
3465                 test_seq 1 10 >expected &&
3466                 echo stuff >>expected &&
3467                 test_cmp expected z/c &&
3468
3469                 git ls-files -s >out &&
3470                 test_line_count = 2 out &&
3471                 git ls-files -u >out &&
3472                 test_line_count = 1 out &&
3473                 git ls-files -o >out &&
3474                 test_line_count = 4 out &&
3475
3476                 git rev-parse >actual \
3477                         :0:z/a :2:z/c &&
3478                 git rev-parse >expect \
3479                          O:z/a  B:z/b &&
3480                 test_cmp expect actual &&
3481
3482                 git hash-object z/c~HEAD >actual &&
3483                 git rev-parse B:z/b >expect &&
3484                 test_cmp expect actual
3485         )
3486 '
3487
3488 # Testcase 11b, Avoid losing dirty file involved in directory rename
3489 #   Commit O: z/a,         x/{b,c_v1}
3490 #   Commit A: z/{a,c_v1},  x/b,       and z/c_v1 has uncommitted mods
3491 #   Commit B: y/a,         x/{b,c_v2}
3492 #   Expected: y/{a,c_v2}, x/b, z/c_v1 with uncommitted mods untracked,
3493 #             ERROR_MSG(Refusing to lose dirty file at z/c)
3494
3495
3496 test_setup_11b () {
3497         test_create_repo 11b &&
3498         (
3499                 cd 11b &&
3500
3501                 mkdir z x &&
3502                 echo a >z/a &&
3503                 echo b >x/b &&
3504                 test_seq 1 10 >x/c &&
3505                 git add z x &&
3506                 test_tick &&
3507                 git commit -m "O" &&
3508
3509                 git branch O &&
3510                 git branch A &&
3511                 git branch B &&
3512
3513                 git checkout A &&
3514                 git mv x/c z/c &&
3515                 test_tick &&
3516                 git commit -m "A" &&
3517
3518                 git checkout B &&
3519                 git mv z y &&
3520                 echo 11 >>x/c &&
3521                 git add x/c &&
3522                 test_tick &&
3523                 git commit -m "B"
3524         )
3525 }
3526
3527 test_expect_success '11b: Avoid losing dirty file involved in directory rename' '
3528         test_setup_11b &&
3529         (
3530                 cd 11b &&
3531
3532                 git checkout A^0 &&
3533                 echo stuff >>z/c &&
3534
3535                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3536                 test_i18ngrep "Refusing to lose dirty file at z/c" out &&
3537
3538                 grep -q stuff z/c &&
3539                 test_seq 1 10 >expected &&
3540                 echo stuff >>expected &&
3541                 test_cmp expected z/c &&
3542
3543                 git ls-files -s >out &&
3544                 test_line_count = 3 out &&
3545                 git ls-files -u >out &&
3546                 test_line_count = 0 out &&
3547                 git ls-files -m >out &&
3548                 test_line_count = 0 out &&
3549                 git ls-files -o >out &&
3550                 test_line_count = 4 out &&
3551
3552                 git rev-parse >actual \
3553                         :0:x/b :0:y/a :0:y/c &&
3554                 git rev-parse >expect \
3555                          O:x/b  O:z/a  B:x/c &&
3556                 test_cmp expect actual &&
3557
3558                 git hash-object y/c >actual &&
3559                 git rev-parse B:x/c >expect &&
3560                 test_cmp expect actual
3561         )
3562 '
3563
3564 # Testcase 11c, Avoid losing not-up-to-date with rename + D/F conflict
3565 #   Commit O: y/a,         x/{b,c_v1}
3566 #   Commit A: y/{a,c_v1},  x/b,       and y/c_v1 has uncommitted mods
3567 #   Commit B: y/{a,c/d},   x/{b,c_v2}
3568 #   Expected: Abort_msg("following files would be overwritten by merge") +
3569 #             y/c left untouched (still has uncommitted mods)
3570
3571 test_setup_11c () {
3572         test_create_repo 11c &&
3573         (
3574                 cd 11c &&
3575
3576                 mkdir y x &&
3577                 echo a >y/a &&
3578                 echo b >x/b &&
3579                 test_seq 1 10 >x/c &&
3580                 git add y x &&
3581                 test_tick &&
3582                 git commit -m "O" &&
3583
3584                 git branch O &&
3585                 git branch A &&
3586                 git branch B &&
3587
3588                 git checkout A &&
3589                 git mv x/c y/c &&
3590                 test_tick &&
3591                 git commit -m "A" &&
3592
3593                 git checkout B &&
3594                 mkdir y/c &&
3595                 echo d >y/c/d &&
3596                 echo 11 >>x/c &&
3597                 git add x/c y/c/d &&
3598                 test_tick &&
3599                 git commit -m "B"
3600         )
3601 }
3602
3603 test_expect_success '11c: Avoid losing not-uptodate with rename + D/F conflict' '
3604         test_setup_11c &&
3605         (
3606                 cd 11c &&
3607
3608                 git checkout A^0 &&
3609                 echo stuff >>y/c &&
3610
3611                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3612                 test_i18ngrep "following files would be overwritten by merge" err &&
3613
3614                 grep -q stuff y/c &&
3615                 test_seq 1 10 >expected &&
3616                 echo stuff >>expected &&
3617                 test_cmp expected y/c &&
3618
3619                 git ls-files -s >out &&
3620                 test_line_count = 3 out &&
3621                 git ls-files -u >out &&
3622                 test_line_count = 0 out &&
3623                 git ls-files -m >out &&
3624                 test_line_count = 1 out &&
3625                 git ls-files -o >out &&
3626                 test_line_count = 3 out
3627         )
3628 '
3629
3630 # Testcase 11d, Avoid losing not-up-to-date with rename + D/F conflict
3631 #   Commit O: z/a,         x/{b,c_v1}
3632 #   Commit A: z/{a,c_v1},  x/b,       and z/c_v1 has uncommitted mods
3633 #   Commit B: y/{a,c/d},   x/{b,c_v2}
3634 #   Expected: D/F: y/c_v2 vs y/c/d) +
3635 #             Warning_Msg("Refusing to lose dirty file at z/c) +
3636 #             y/{a,c~HEAD,c/d}, x/b, now-untracked z/c_v1 with uncommitted mods
3637
3638 test_setup_11d () {
3639         test_create_repo 11d &&
3640         (
3641                 cd 11d &&
3642
3643                 mkdir z x &&
3644                 echo a >z/a &&
3645                 echo b >x/b &&
3646                 test_seq 1 10 >x/c &&
3647                 git add z x &&
3648                 test_tick &&
3649                 git commit -m "O" &&
3650
3651                 git branch O &&
3652                 git branch A &&
3653                 git branch B &&
3654
3655                 git checkout A &&
3656                 git mv x/c z/c &&
3657                 test_tick &&
3658                 git commit -m "A" &&
3659
3660                 git checkout B &&
3661                 git mv z y &&
3662                 mkdir y/c &&
3663                 echo d >y/c/d &&
3664                 echo 11 >>x/c &&
3665                 git add x/c y/c/d &&
3666                 test_tick &&
3667                 git commit -m "B"
3668         )
3669 }
3670
3671 test_expect_success '11d: Avoid losing not-uptodate with rename + D/F conflict' '
3672         test_setup_11d &&
3673         (
3674                 cd 11d &&
3675
3676                 git checkout A^0 &&
3677                 echo stuff >>z/c &&
3678
3679                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3680                 test_i18ngrep "Refusing to lose dirty file at z/c" out &&
3681
3682                 grep -q stuff z/c &&
3683                 test_seq 1 10 >expected &&
3684                 echo stuff >>expected &&
3685                 test_cmp expected z/c &&
3686
3687                 git ls-files -s >out &&
3688                 test_line_count = 4 out &&
3689                 git ls-files -u >out &&
3690                 test_line_count = 1 out &&
3691                 git ls-files -o >out &&
3692                 test_line_count = 5 out &&
3693
3694                 git rev-parse >actual \
3695                         :0:x/b :0:y/a :0:y/c/d :3:y/c &&
3696                 git rev-parse >expect \
3697                          O:x/b  O:z/a  B:y/c/d  B:x/c &&
3698                 test_cmp expect actual &&
3699
3700                 git hash-object y/c~HEAD >actual &&
3701                 git rev-parse B:x/c >expect &&
3702                 test_cmp expect actual
3703         )
3704 '
3705
3706 # Testcase 11e, Avoid deleting not-up-to-date with dir rename/rename(1to2)/add
3707 #   Commit O: z/{a,b},      x/{c_1,d}
3708 #   Commit A: y/{a,b,c_2},  x/d, w/c_1, and y/c_2 has uncommitted mods
3709 #   Commit B: z/{a,b,c_1},  x/d
3710 #   Expected: Failed Merge; y/{a,b} + x/d +
3711 #             CONFLICT(rename/rename) x/c_1 -> w/c_1 vs y/c_1 +
3712 #             ERROR_MSG(Refusing to lose dirty file at y/c)
3713 #             y/c~B^0 has O:x/c_1 contents
3714 #             y/c~HEAD has A:y/c_2 contents
3715 #             y/c has dirty file from before merge
3716
3717 test_setup_11e () {
3718         test_create_repo 11e &&
3719         (
3720                 cd 11e &&
3721
3722                 mkdir z x &&
3723                 echo a >z/a &&
3724                 echo b >z/b &&
3725                 echo c >x/c &&
3726                 echo d >x/d &&
3727                 git add z x &&
3728                 test_tick &&
3729                 git commit -m "O" &&
3730
3731                 git branch O &&
3732                 git branch A &&
3733                 git branch B &&
3734
3735                 git checkout A &&
3736                 git mv z/ y/ &&
3737                 echo different >y/c &&
3738                 mkdir w &&
3739                 git mv x/c w/ &&
3740                 git add y/c &&
3741                 test_tick &&
3742                 git commit -m "A" &&
3743
3744                 git checkout B &&
3745                 git mv x/c z/ &&
3746                 test_tick &&
3747                 git commit -m "B"
3748         )
3749 }
3750
3751 test_expect_success '11e: Avoid deleting not-uptodate with dir rename/rename(1to2)/add' '
3752         test_setup_11e &&
3753         (
3754                 cd 11e &&
3755
3756                 git checkout A^0 &&
3757                 echo mods >>y/c &&
3758
3759                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3760                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3761                 test_i18ngrep "Refusing to lose dirty file at y/c" out &&
3762
3763                 git ls-files -s >out &&
3764                 test_line_count = 7 out &&
3765                 git ls-files -u >out &&
3766                 test_line_count = 4 out &&
3767                 git ls-files -o >out &&
3768                 test_line_count = 3 out &&
3769
3770                 echo different >expected &&
3771                 echo mods >>expected &&
3772                 test_cmp expected y/c &&
3773
3774                 git rev-parse >actual \
3775                         :0:y/a :0:y/b :0:x/d :1:x/c :2:w/c :2:y/c :3:y/c &&
3776                 git rev-parse >expect \
3777                          O:z/a  O:z/b  O:x/d  O:x/c  O:x/c  A:y/c  O:x/c &&
3778                 test_cmp expect actual &&
3779
3780                 # See if y/c~merged has expected contents; requires manually
3781                 # doing the expected file merge
3782                 git cat-file -p A:y/c >c1 &&
3783                 git cat-file -p B:z/c >c2 &&
3784                 >empty &&
3785                 test_must_fail git merge-file \
3786                         -L "HEAD" \
3787                         -L "" \
3788                         -L "B^0" \
3789                         c1 empty c2 &&
3790                 test_cmp c1 y/c~merged
3791         )
3792 '
3793
3794 # Testcase 11f, Avoid deleting not-up-to-date w/ dir rename/rename(2to1)
3795 #   Commit O: z/{a,b},        x/{c_1,d_2}
3796 #   Commit A: y/{a,b,wham_1}, x/d_2, except y/wham has uncommitted mods
3797 #   Commit B: z/{a,b,wham_2}, x/c_1
3798 #   Expected: Failed Merge; y/{a,b} + untracked y/{wham~merged} +
3799 #             y/wham with dirty changes from before merge +
3800 #             CONFLICT(rename/rename) x/c vs x/d -> y/wham
3801 #             ERROR_MSG(Refusing to lose dirty file at y/wham)
3802
3803 test_setup_11f () {
3804         test_create_repo 11f &&
3805         (
3806                 cd 11f &&
3807
3808                 mkdir z x &&
3809                 echo a >z/a &&
3810                 echo b >z/b &&
3811                 test_seq 1 10 >x/c &&
3812                 echo d >x/d &&
3813                 git add z x &&
3814                 test_tick &&
3815                 git commit -m "O" &&
3816
3817                 git branch O &&
3818                 git branch A &&
3819                 git branch B &&
3820
3821                 git checkout A &&
3822                 git mv z/ y/ &&
3823                 git mv x/c y/wham &&
3824                 test_tick &&
3825                 git commit -m "A" &&
3826
3827                 git checkout B &&
3828                 git mv x/d z/wham &&
3829                 test_tick &&
3830                 git commit -m "B"
3831         )
3832 }
3833
3834 test_expect_success '11f: Avoid deleting not-uptodate with dir rename/rename(2to1)' '
3835         test_setup_11f &&
3836         (
3837                 cd 11f &&
3838
3839                 git checkout A^0 &&
3840                 echo important >>y/wham &&
3841
3842                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3843                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3844                 test_i18ngrep "Refusing to lose dirty file at y/wham" out &&
3845
3846                 git ls-files -s >out &&
3847                 test_line_count = 4 out &&
3848                 git ls-files -u >out &&
3849                 test_line_count = 2 out &&
3850                 git ls-files -o >out &&
3851                 test_line_count = 3 out &&
3852
3853                 test_seq 1 10 >expected &&
3854                 echo important >>expected &&
3855                 test_cmp expected y/wham &&
3856
3857                 test_must_fail git rev-parse :1:y/wham &&
3858
3859                 git rev-parse >actual \
3860                         :0:y/a :0:y/b :2:y/wham :3:y/wham &&
3861                 git rev-parse >expect \
3862                          O:z/a  O:z/b  O:x/c     O:x/d &&
3863                 test_cmp expect actual &&
3864
3865                 # Test that the two-way merge in y/wham~merged is as expected
3866                 git cat-file -p :2:y/wham >expect &&
3867                 git cat-file -p :3:y/wham >other &&
3868                 >empty &&
3869                 test_must_fail git merge-file \
3870                         -L "HEAD" \
3871                         -L "" \
3872                         -L "B^0" \
3873                         expect empty other &&
3874                 test_cmp expect y/wham~merged
3875         )
3876 '
3877
3878 ###########################################################################
3879 # SECTION 12: Everything else
3880 #
3881 # Tests suggested by others.  Tests added after implementation completed
3882 # and submitted.  Grab bag.
3883 ###########################################################################
3884
3885 # Testcase 12a, Moving one directory hierarchy into another
3886 #   (Related to testcase 9a)
3887 #   Commit O: node1/{leaf1,leaf2}, node2/{leaf3,leaf4}
3888 #   Commit A: node1/{leaf1,leaf2,node2/{leaf3,leaf4}}
3889 #   Commit B: node1/{leaf1,leaf2,leaf5}, node2/{leaf3,leaf4,leaf6}
3890 #   Expected: node1/{leaf1,leaf2,leaf5,node2/{leaf3,leaf4,leaf6}}
3891
3892 test_setup_12a () {
3893         test_create_repo 12a &&
3894         (
3895                 cd 12a &&
3896
3897                 mkdir -p node1 node2 &&
3898                 echo leaf1 >node1/leaf1 &&
3899                 echo leaf2 >node1/leaf2 &&
3900                 echo leaf3 >node2/leaf3 &&
3901                 echo leaf4 >node2/leaf4 &&
3902                 git add node1 node2 &&
3903                 test_tick &&
3904                 git commit -m "O" &&
3905
3906                 git branch O &&
3907                 git branch A &&
3908                 git branch B &&
3909
3910                 git checkout A &&
3911                 git mv node2/ node1/ &&
3912                 test_tick &&
3913                 git commit -m "A" &&
3914
3915                 git checkout B &&
3916                 echo leaf5 >node1/leaf5 &&
3917                 echo leaf6 >node2/leaf6 &&
3918                 git add node1 node2 &&
3919                 test_tick &&
3920                 git commit -m "B"
3921         )
3922 }
3923
3924 test_expect_success '12a: Moving one directory hierarchy into another' '
3925         test_setup_12a &&
3926         (
3927                 cd 12a &&
3928
3929                 git checkout A^0 &&
3930
3931                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
3932
3933                 git ls-files -s >out &&
3934                 test_line_count = 6 out &&
3935
3936                 git rev-parse >actual \
3937                         HEAD:node1/leaf1 HEAD:node1/leaf2 HEAD:node1/leaf5 \
3938                         HEAD:node1/node2/leaf3 \
3939                         HEAD:node1/node2/leaf4 \
3940                         HEAD:node1/node2/leaf6 &&
3941                 git rev-parse >expect \
3942                         O:node1/leaf1    O:node1/leaf2    B:node1/leaf5 \
3943                         O:node2/leaf3 \
3944                         O:node2/leaf4 \
3945                         B:node2/leaf6 &&
3946                 test_cmp expect actual
3947         )
3948 '
3949
3950 # Testcase 12b, Moving two directory hierarchies into each other
3951 #   (Related to testcases 1c and 12c)
3952 #   Commit O: node1/{leaf1, leaf2}, node2/{leaf3, leaf4}
3953 #   Commit A: node1/{leaf1, leaf2, node2/{leaf3, leaf4}}
3954 #   Commit B: node2/{leaf3, leaf4, node1/{leaf1, leaf2}}
3955 #   Expected: node1/node2/node1/{leaf1, leaf2},
3956 #             node2/node1/node2/{leaf3, leaf4}
3957 #   NOTE: Without directory renames, we would expect
3958 #                   node2/node1/{leaf1, leaf2},
3959 #                   node1/node2/{leaf3, leaf4}
3960 #         with directory rename detection, we note that
3961 #             commit A renames node2/ -> node1/node2/
3962 #             commit B renames node1/ -> node2/node1/
3963 #         therefore, applying those directory renames to the initial result
3964 #         (making all four paths experience a transitive renaming), yields
3965 #         the expected result.
3966 #
3967 #         You may ask, is it weird to have two directories rename each other?
3968 #         To which, I can do no more than shrug my shoulders and say that
3969 #         even simple rules give weird results when given weird inputs.
3970
3971 test_setup_12b () {
3972         test_create_repo 12b &&
3973         (
3974                 cd 12b &&
3975
3976                 mkdir -p node1 node2 &&
3977                 echo leaf1 >node1/leaf1 &&
3978                 echo leaf2 >node1/leaf2 &&
3979                 echo leaf3 >node2/leaf3 &&
3980                 echo leaf4 >node2/leaf4 &&
3981                 git add node1 node2 &&
3982                 test_tick &&
3983                 git commit -m "O" &&
3984
3985                 git branch O &&
3986                 git branch A &&
3987                 git branch B &&
3988
3989                 git checkout A &&
3990                 git mv node2/ node1/ &&
3991                 test_tick &&
3992                 git commit -m "A" &&
3993
3994                 git checkout B &&
3995                 git mv node1/ node2/ &&
3996                 test_tick &&
3997                 git commit -m "B"
3998         )
3999 }
4000
4001 test_expect_success '12b: Moving two directory hierarchies into each other' '
4002         test_setup_12b &&
4003         (
4004                 cd 12b &&
4005
4006                 git checkout A^0 &&
4007
4008                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4009
4010                 git ls-files -s >out &&
4011                 test_line_count = 4 out &&
4012
4013                 git rev-parse >actual \
4014                         HEAD:node1/node2/node1/leaf1 \
4015                         HEAD:node1/node2/node1/leaf2 \
4016                         HEAD:node2/node1/node2/leaf3 \
4017                         HEAD:node2/node1/node2/leaf4 &&
4018                 git rev-parse >expect \
4019                         O:node1/leaf1 \
4020                         O:node1/leaf2 \
4021                         O:node2/leaf3 \
4022                         O:node2/leaf4 &&
4023                 test_cmp expect actual
4024         )
4025 '
4026
4027 # Testcase 12c, Moving two directory hierarchies into each other w/ content merge
4028 #   (Related to testcase 12b)
4029 #   Commit O: node1/{       leaf1_1, leaf2_1}, node2/{leaf3_1, leaf4_1}
4030 #   Commit A: node1/{       leaf1_2, leaf2_2,  node2/{leaf3_2, leaf4_2}}
4031 #   Commit B: node2/{node1/{leaf1_3, leaf2_3},        leaf3_3, leaf4_3}
4032 #   Expected: Content merge conflicts for each of:
4033 #               node1/node2/node1/{leaf1, leaf2},
4034 #               node2/node1/node2/{leaf3, leaf4}
4035 #   NOTE: This is *exactly* like 12c, except that every path is modified on
4036 #         each side of the merge.
4037
4038 test_setup_12c () {
4039         test_create_repo 12c &&
4040         (
4041                 cd 12c &&
4042
4043                 mkdir -p node1 node2 &&
4044                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf1\n" >node1/leaf1 &&
4045                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf2\n" >node1/leaf2 &&
4046                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf3\n" >node2/leaf3 &&
4047                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf4\n" >node2/leaf4 &&
4048                 git add node1 node2 &&
4049                 test_tick &&
4050                 git commit -m "O" &&
4051
4052                 git branch O &&
4053                 git branch A &&
4054                 git branch B &&
4055
4056                 git checkout A &&
4057                 git mv node2/ node1/ &&
4058                 for i in `git ls-files`; do echo side A >>$i; done &&
4059                 git add -u &&
4060                 test_tick &&
4061                 git commit -m "A" &&
4062
4063                 git checkout B &&
4064                 git mv node1/ node2/ &&
4065                 for i in `git ls-files`; do echo side B >>$i; done &&
4066                 git add -u &&
4067                 test_tick &&
4068                 git commit -m "B"
4069         )
4070 }
4071
4072 test_expect_success '12c: Moving one directory hierarchy into another w/ content merge' '
4073         test_setup_12c &&
4074         (
4075                 cd 12c &&
4076
4077                 git checkout A^0 &&
4078
4079                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 &&
4080
4081                 git ls-files -u >out &&
4082                 test_line_count = 12 out &&
4083
4084                 git rev-parse >actual \
4085                         :1:node1/node2/node1/leaf1 \
4086                         :1:node1/node2/node1/leaf2 \
4087                         :1:node2/node1/node2/leaf3 \
4088                         :1:node2/node1/node2/leaf4 \
4089                         :2:node1/node2/node1/leaf1 \
4090                         :2:node1/node2/node1/leaf2 \
4091                         :2:node2/node1/node2/leaf3 \
4092                         :2:node2/node1/node2/leaf4 \
4093                         :3:node1/node2/node1/leaf1 \
4094                         :3:node1/node2/node1/leaf2 \
4095                         :3:node2/node1/node2/leaf3 \
4096                         :3:node2/node1/node2/leaf4 &&
4097                 git rev-parse >expect \
4098                         O:node1/leaf1 \
4099                         O:node1/leaf2 \
4100                         O:node2/leaf3 \
4101                         O:node2/leaf4 \
4102                         A:node1/leaf1 \
4103                         A:node1/leaf2 \
4104                         A:node1/node2/leaf3 \
4105                         A:node1/node2/leaf4 \
4106                         B:node2/node1/leaf1 \
4107                         B:node2/node1/leaf2 \
4108                         B:node2/leaf3 \
4109                         B:node2/leaf4 &&
4110                 test_cmp expect actual
4111         )
4112 '
4113
4114 # Testcase 12d, Rename/merge of subdirectory into the root
4115 #   Commit O: a/b/subdir/foo
4116 #   Commit A: subdir/foo
4117 #   Commit B: a/b/subdir/foo, a/b/bar
4118 #   Expected: subdir/foo, bar
4119
4120 test_setup_12d () {
4121         test_create_repo 12d &&
4122         (
4123                 cd 12d &&
4124
4125                 mkdir -p a/b/subdir &&
4126                 test_commit a/b/subdir/foo &&
4127
4128                 git branch O &&
4129                 git branch A &&
4130                 git branch B &&
4131
4132                 git checkout A &&
4133                 mkdir subdir &&
4134                 git mv a/b/subdir/foo.t subdir/foo.t &&
4135                 test_tick &&
4136                 git commit -m "A" &&
4137
4138                 git checkout B &&
4139                 test_commit a/b/bar
4140         )
4141 }
4142
4143 test_expect_success '12d: Rename/merge subdir into the root, variant 1' '
4144         test_setup_12d &&
4145         (
4146                 cd 12d &&
4147
4148                 git checkout A^0 &&
4149
4150                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4151
4152                 git ls-files -s >out &&
4153                 test_line_count = 2 out &&
4154
4155                 git rev-parse >actual \
4156                         HEAD:subdir/foo.t   HEAD:bar.t &&
4157                 git rev-parse >expect \
4158                         O:a/b/subdir/foo.t  B:a/b/bar.t &&
4159                 test_cmp expect actual &&
4160
4161                 git hash-object bar.t >actual &&
4162                 git rev-parse B:a/b/bar.t >expect &&
4163                 test_cmp expect actual &&
4164
4165                 test_must_fail git rev-parse HEAD:a/b/subdir/foo.t &&
4166                 test_must_fail git rev-parse HEAD:a/b/bar.t &&
4167                 test_path_is_missing a/ &&
4168                 test_path_is_file bar.t
4169         )
4170 '
4171
4172 # Testcase 12e, Rename/merge of subdirectory into the root
4173 #   Commit O: a/b/foo
4174 #   Commit A: foo
4175 #   Commit B: a/b/foo, a/b/bar
4176 #   Expected: foo, bar
4177
4178 test_setup_12e () {
4179         test_create_repo 12e &&
4180         (
4181                 cd 12e &&
4182
4183                 mkdir -p a/b &&
4184                 test_commit a/b/foo &&
4185
4186                 git branch O &&
4187                 git branch A &&
4188                 git branch B &&
4189
4190                 git checkout A &&
4191                 mkdir subdir &&
4192                 git mv a/b/foo.t foo.t &&
4193                 test_tick &&
4194                 git commit -m "A" &&
4195
4196                 git checkout B &&
4197                 test_commit a/b/bar
4198         )
4199 }
4200
4201 test_expect_success '12e: Rename/merge subdir into the root, variant 2' '
4202         test_setup_12e &&
4203         (
4204                 cd 12e &&
4205
4206                 git checkout A^0 &&
4207
4208                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4209
4210                 git ls-files -s >out &&
4211                 test_line_count = 2 out &&
4212
4213                 git rev-parse >actual \
4214                         HEAD:foo.t   HEAD:bar.t &&
4215                 git rev-parse >expect \
4216                         O:a/b/foo.t  B:a/b/bar.t &&
4217                 test_cmp expect actual &&
4218
4219                 git hash-object bar.t >actual &&
4220                 git rev-parse B:a/b/bar.t >expect &&
4221                 test_cmp expect actual &&
4222
4223                 test_must_fail git rev-parse HEAD:a/b/foo.t &&
4224                 test_must_fail git rev-parse HEAD:a/b/bar.t &&
4225                 test_path_is_missing a/ &&
4226                 test_path_is_file bar.t
4227         )
4228 '
4229
4230 ###########################################################################
4231 # SECTION 13: Checking informational and conflict messages
4232 #
4233 # A year after directory rename detection became the default, it was
4234 # instead decided to report conflicts on the pathname on the basis that
4235 # some users may expect the new files added or moved into a directory to
4236 # be unrelated to all the other files in that directory, and thus that
4237 # directory rename detection is unexpected.  Test that the messages printed
4238 # match our expectation.
4239 ###########################################################################
4240
4241 # Testcase 13a, Basic directory rename with newly added files
4242 #   Commit O: z/{b,c}
4243 #   Commit A: y/{b,c}
4244 #   Commit B: z/{b,c,d,e/f}
4245 #   Expected: y/{b,c,d,e/f}, with notices/conflicts for both y/d and y/e/f
4246
4247 test_setup_13a () {
4248         test_create_repo 13a_$1 &&
4249         (
4250                 cd 13a_$1 &&
4251
4252                 mkdir z &&
4253                 echo b >z/b &&
4254                 echo c >z/c &&
4255                 git add z &&
4256                 test_tick &&
4257                 git commit -m "O" &&
4258
4259                 git branch O &&
4260                 git branch A &&
4261                 git branch B &&
4262
4263                 git checkout A &&
4264                 git mv z y &&
4265                 test_tick &&
4266                 git commit -m "A" &&
4267
4268                 git checkout B &&
4269                 echo d >z/d &&
4270                 mkdir z/e &&
4271                 echo f >z/e/f &&
4272                 git add z/d z/e/f &&
4273                 test_tick &&
4274                 git commit -m "B"
4275         )
4276 }
4277
4278 test_expect_success '13a(conflict): messages for newly added files' '
4279         test_setup_13a conflict &&
4280         (
4281                 cd 13a_conflict &&
4282
4283                 git checkout A^0 &&
4284
4285                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4286
4287                 test_i18ngrep CONFLICT..file.location.*z/e/f.added.in.B^0.*y/e/f out &&
4288                 test_i18ngrep CONFLICT..file.location.*z/d.added.in.B^0.*y/d out &&
4289
4290                 git ls-files >paths &&
4291                 ! grep z/ paths &&
4292                 grep "y/[de]" paths &&
4293
4294                 test_path_is_missing z/d &&
4295                 test_path_is_file    y/d &&
4296                 test_path_is_missing z/e/f &&
4297                 test_path_is_file    y/e/f
4298         )
4299 '
4300
4301 test_expect_success '13a(info): messages for newly added files' '
4302         test_setup_13a info &&
4303         (
4304                 cd 13a_info &&
4305
4306                 git reset --hard &&
4307                 git checkout A^0 &&
4308
4309                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4310
4311                 test_i18ngrep Path.updated:.*z/e/f.added.in.B^0.*y/e/f out &&
4312                 test_i18ngrep Path.updated:.*z/d.added.in.B^0.*y/d out &&
4313
4314                 git ls-files >paths &&
4315                 ! grep z/ paths &&
4316                 grep "y/[de]" paths &&
4317
4318                 test_path_is_missing z/d &&
4319                 test_path_is_file    y/d &&
4320                 test_path_is_missing z/e/f &&
4321                 test_path_is_file    y/e/f
4322         )
4323 '
4324
4325 # Testcase 13b, Transitive rename with conflicted content merge and default
4326 #               "conflict" setting
4327 #   (Related to testcase 1c, 9b)
4328 #   Commit O: z/{b,c},   x/d_1
4329 #   Commit A: y/{b,c},   x/d_2
4330 #   Commit B: z/{b,c,d_3}
4331 #   Expected: y/{b,c,d_merged}, with two conflict messages for y/d,
4332 #             one about content, and one about file location
4333
4334 test_setup_13b () {
4335         test_create_repo 13b_$1 &&
4336         (
4337                 cd 13b_$1 &&
4338
4339                 mkdir x &&
4340                 mkdir z &&
4341                 test_seq 1 10 >x/d &&
4342                 echo b >z/b &&
4343                 echo c >z/c &&
4344                 git add x z &&
4345                 test_tick &&
4346                 git commit -m "O" &&
4347
4348                 git branch O &&
4349                 git branch A &&
4350                 git branch B &&
4351
4352                 git checkout A &&
4353                 git mv z y &&
4354                 echo 11 >>x/d &&
4355                 git add x/d &&
4356                 test_tick &&
4357                 git commit -m "A" &&
4358
4359                 git checkout B &&
4360                 echo eleven >>x/d &&
4361                 git mv x/d z/d &&
4362                 git add z/d &&
4363                 test_tick &&
4364                 git commit -m "B"
4365         )
4366 }
4367
4368 test_expect_success '13b(conflict): messages for transitive rename with conflicted content' '
4369         test_setup_13b conflict &&
4370         (
4371                 cd 13b_conflict &&
4372
4373                 git checkout A^0 &&
4374
4375                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4376
4377                 test_i18ngrep CONFLICT.*content.*Merge.conflict.in.y/d out &&
4378                 test_i18ngrep CONFLICT..file.location.*x/d.renamed.to.z/d.*moved.to.y/d out &&
4379
4380                 git ls-files >paths &&
4381                 ! grep z/ paths &&
4382                 grep "y/d" paths &&
4383
4384                 test_path_is_missing z/d &&
4385                 test_path_is_file    y/d
4386         )
4387 '
4388
4389 test_expect_success '13b(info): messages for transitive rename with conflicted content' '
4390         test_setup_13b info &&
4391         (
4392                 cd 13b_info &&
4393
4394                 git reset --hard &&
4395                 git checkout A^0 &&
4396
4397                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4398
4399                 test_i18ngrep CONFLICT.*content.*Merge.conflict.in.y/d out &&
4400                 test_i18ngrep Path.updated:.*x/d.renamed.to.z/d.in.B^0.*moving.it.to.y/d out &&
4401
4402                 git ls-files >paths &&
4403                 ! grep z/ paths &&
4404                 grep "y/d" paths &&
4405
4406                 test_path_is_missing z/d &&
4407                 test_path_is_file    y/d
4408         )
4409 '
4410
4411 # Testcase 13c, Rename/rename(1to1) due to directory rename
4412 #   Commit O: z/{b,c},   x/{d,e}
4413 #   Commit A: y/{b,c,d}, x/e
4414 #   Commit B: z/{b,c,d}, x/e
4415 #   Expected: y/{b,c,d}, x/e, with info or conflict messages for d
4416 #             A: renamed x/d -> z/d; B: renamed z/ -> y/ AND renamed x/d to y/d
4417 #             One could argue A had partial knowledge of what was done with
4418 #             d and B had full knowledge, but that's a slippery slope as
4419 #             shown in testcase 13d.
4420
4421 test_setup_13c () {
4422         test_create_repo 13c_$1 &&
4423         (
4424                 cd 13c_$1 &&
4425
4426                 mkdir x &&
4427                 mkdir z &&
4428                 test_seq 1 10 >x/d &&
4429                 echo e >x/e &&
4430                 echo b >z/b &&
4431                 echo c >z/c &&
4432                 git add x z &&
4433                 test_tick &&
4434                 git commit -m "O" &&
4435
4436                 git branch O &&
4437                 git branch A &&
4438                 git branch B &&
4439
4440                 git checkout A &&
4441                 git mv z y &&
4442                 git mv x/d y/ &&
4443                 test_tick &&
4444                 git commit -m "A" &&
4445
4446                 git checkout B &&
4447                 git mv x/d z/d &&
4448                 git add z/d &&
4449                 test_tick &&
4450                 git commit -m "B"
4451         )
4452 }
4453
4454 test_expect_success '13c(conflict): messages for rename/rename(1to1) via transitive rename' '
4455         test_setup_13c conflict &&
4456         (
4457                 cd 13c_conflict &&
4458
4459                 git checkout A^0 &&
4460
4461                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4462
4463                 test_i18ngrep CONFLICT..file.location.*x/d.renamed.to.z/d.*moved.to.y/d out &&
4464
4465                 git ls-files >paths &&
4466                 ! grep z/ paths &&
4467                 grep "y/d" paths &&
4468
4469                 test_path_is_missing z/d &&
4470                 test_path_is_file    y/d
4471         )
4472 '
4473
4474 test_expect_success '13c(info): messages for rename/rename(1to1) via transitive rename' '
4475         test_setup_13c info &&
4476         (
4477                 cd 13c_info &&
4478
4479                 git reset --hard &&
4480                 git checkout A^0 &&
4481
4482                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4483
4484                 test_i18ngrep Path.updated:.*x/d.renamed.to.z/d.in.B^0.*moving.it.to.y/d out &&
4485
4486                 git ls-files >paths &&
4487                 ! grep z/ paths &&
4488                 grep "y/d" paths &&
4489
4490                 test_path_is_missing z/d &&
4491                 test_path_is_file    y/d
4492         )
4493 '
4494
4495 # Testcase 13d, Rename/rename(1to1) due to directory rename on both sides
4496 #   Commit O: a/{z,y}, b/x,     c/w
4497 #   Commit A: a/z,     b/{y,x}, d/w
4498 #   Commit B: a/z,     d/x,     c/{y,w}
4499 #   Expected: a/z, d/{y,x,w} with no file location conflict for x
4500 #             Easy cases:
4501 #               * z is always in a; so it stays in a.
4502 #               * x starts in b, only modified on one side to move into d/
4503 #               * w starts in c, only modified on one side to move into d/
4504 #             Hard case:
4505 #               * A renames a/y to b/y, and B renames b/->d/ => a/y -> d/y
4506 #               * B renames a/y to c/y, and A renames c/->d/ => a/y -> d/y
4507 #               No conflict in where a/y ends up, so put it in d/y.
4508
4509 test_setup_13d () {
4510         test_create_repo 13d_$1 &&
4511         (
4512                 cd 13d_$1 &&
4513
4514                 mkdir a &&
4515                 mkdir b &&
4516                 mkdir c &&
4517                 echo z >a/z &&
4518                 echo y >a/y &&
4519                 echo x >b/x &&
4520                 echo w >c/w &&
4521                 git add a b c &&
4522                 test_tick &&
4523                 git commit -m "O" &&
4524
4525                 git branch O &&
4526                 git branch A &&
4527                 git branch B &&
4528
4529                 git checkout A &&
4530                 git mv a/y b/ &&
4531                 git mv c/ d/ &&
4532                 test_tick &&
4533                 git commit -m "A" &&
4534
4535                 git checkout B &&
4536                 git mv a/y c/ &&
4537                 git mv b/ d/ &&
4538                 test_tick &&
4539                 git commit -m "B"
4540         )
4541 }
4542
4543 test_expect_success '13d(conflict): messages for rename/rename(1to1) via dual transitive rename' '
4544         test_setup_13d conflict &&
4545         (
4546                 cd 13d_conflict &&
4547
4548                 git checkout A^0 &&
4549
4550                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4551
4552                 test_i18ngrep CONFLICT..file.location.*a/y.renamed.to.b/y.*moved.to.d/y out &&
4553                 test_i18ngrep CONFLICT..file.location.*a/y.renamed.to.c/y.*moved.to.d/y out &&
4554
4555                 git ls-files >paths &&
4556                 ! grep b/ paths &&
4557                 ! grep c/ paths &&
4558                 grep "d/y" paths &&
4559
4560                 test_path_is_missing b/y &&
4561                 test_path_is_missing c/y &&
4562                 test_path_is_file    d/y
4563         )
4564 '
4565
4566 test_expect_success '13d(info): messages for rename/rename(1to1) via dual transitive rename' '
4567         test_setup_13d info &&
4568         (
4569                 cd 13d_info &&
4570
4571                 git reset --hard &&
4572                 git checkout A^0 &&
4573
4574                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4575
4576                 test_i18ngrep Path.updated.*a/y.renamed.to.b/y.*moving.it.to.d/y out &&
4577                 test_i18ngrep Path.updated.*a/y.renamed.to.c/y.*moving.it.to.d/y out &&
4578
4579                 git ls-files >paths &&
4580                 ! grep b/ paths &&
4581                 ! grep c/ paths &&
4582                 grep "d/y" paths &&
4583
4584                 test_path_is_missing b/y &&
4585                 test_path_is_missing c/y &&
4586                 test_path_is_file    d/y
4587         )
4588 '
4589
4590 # Testcase 13e, directory rename in virtual merge base
4591 #
4592 # This testcase has a slightly different setup than all the above cases, in
4593 # order to include a recursive case:
4594 #
4595 #      A   C
4596 #      o - o
4597 #     / \ / \
4598 #  O o   X   ?
4599 #     \ / \ /
4600 #      o   o
4601 #      B   D
4602 #
4603 #   Commit O: a/{z,y}
4604 #   Commit A: b/{z,y}
4605 #   Commit B: a/{z,y,x}
4606 #   Commit C: b/{z,y,x}
4607 #   Commit D: b/{z,y}, a/x
4608 #   Expected: b/{z,y,x}  (sort of; see below for why this might not be expected)
4609 #
4610 #   NOTES: 'X' represents a virtual merge base.  With the default of
4611 #          directory rename detection yielding conflicts, merging A and B
4612 #          results in a conflict complaining about whether 'x' should be
4613 #          under 'a/' or 'b/'.  However, when creating the virtual merge
4614 #          base 'X', since virtual merge bases need to be written out as a
4615 #          tree, we cannot have a conflict, so some resolution has to be
4616 #          picked.
4617 #
4618 #          In choosing the right resolution, it's worth noting here that
4619 #          commits C & D are merges of A & B that choose different
4620 #          locations for 'x' (i.e. they resolve the conflict differently),
4621 #          and so it would be nice when merging C & D if git could detect
4622 #          this difference of opinion and report a conflict.  But the only
4623 #          way to do so that I can think of would be to have the virtual
4624 #          merge base place 'x' in some directory other than either 'a/' or
4625 #          'b/', which seems a little weird -- especially since it'd result
4626 #          in a rename/rename(1to2) conflict with a source path that never
4627 #          existed in any version.
4628 #
4629 #          So, for now, when directory rename detection is set to
4630 #          'conflict' just avoid doing directory rename detection at all in
4631 #          the recursive case.  This will not allow us to detect a conflict
4632 #          in the outer merge for this special kind of setup, but it at
4633 #          least avoids hitting a BUG().
4634 #
4635 test_setup_13e () {
4636         test_create_repo 13e &&
4637         (
4638                 cd 13e &&
4639
4640                 mkdir a &&
4641                 echo z >a/z &&
4642                 echo y >a/y &&
4643                 git add a &&
4644                 test_tick &&
4645                 git commit -m "O" &&
4646
4647                 git branch O &&
4648                 git branch A &&
4649                 git branch B &&
4650
4651                 git checkout A &&
4652                 git mv a/ b/ &&
4653                 test_tick &&
4654                 git commit -m "A" &&
4655
4656                 git checkout B &&
4657                 echo x >a/x &&
4658                 git add a &&
4659                 test_tick &&
4660                 git commit -m "B" &&
4661
4662                 git branch C A &&
4663                 git branch D B &&
4664
4665                 git checkout C &&
4666                 test_must_fail git -c merge.directoryRenames=conflict merge B &&
4667                 git add b/x &&
4668                 test_tick &&
4669                 git commit -m "C" &&
4670
4671
4672                 git checkout D &&
4673                 test_must_fail git -c merge.directoryRenames=conflict merge A &&
4674                 git add b/x &&
4675                 mkdir a &&
4676                 git mv b/x a/x &&
4677                 test_tick &&
4678                 git commit -m "D"
4679         )
4680 }
4681
4682 test_expect_success '13e: directory rename detection in recursive case' '
4683         test_setup_13e &&
4684         (
4685                 cd 13e &&
4686
4687                 git checkout --quiet D^0 &&
4688
4689                 git -c merge.directoryRenames=conflict merge -s recursive C^0 >out 2>err &&
4690
4691                 test_i18ngrep ! CONFLICT out &&
4692                 test_i18ngrep ! BUG: err &&
4693                 test_i18ngrep ! core.dumped err &&
4694                 test_must_be_empty err &&
4695
4696                 git ls-files >paths &&
4697                 ! grep a/x paths &&
4698                 grep b/x paths
4699         )
4700 '
4701
4702 test_done