rebase --merge: optionally skip upstreamed commits
[git] / t / t6043-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 (rename/rename)" 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 (rename/rename)" 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 #   w/o dir-rename detection: z/d, CONFLICT(z/b -> y/b vs. w/b),
2264 #                                  CONFLICT(z/c -> y/c vs. w/c)
2265 #   Currently expected:       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.  Therefore, the directory rename done in commit A to z/
2273 #        applies to z/d and maps it to y/d.
2274 #
2275 #        It's possible that users would get confused about this, but what
2276 #        should we do instead?  Silently leaving at z/d seems just as bad or
2277 #        maybe even worse.  Perhaps we could print a big warning about z/d
2278 #        and how we're moving to y/d in this case, but when I started thinking
2279 #        about the ramifications of doing that, I didn't know how to rule out
2280 #        that opening other weird edge and corner cases so I just punted.
2281
2282 test_setup_8e () {
2283         test_create_repo 8e &&
2284         (
2285                 cd 8e &&
2286
2287                 mkdir z &&
2288                 echo b >z/b &&
2289                 echo c >z/c &&
2290                 git add z &&
2291                 test_tick &&
2292                 git commit -m "O" &&
2293
2294                 git branch O &&
2295                 git branch A &&
2296                 git branch B &&
2297
2298                 git checkout A &&
2299                 git mv z y &&
2300                 test_tick &&
2301                 git commit -m "A" &&
2302
2303                 git checkout B &&
2304                 git mv z w &&
2305                 mkdir z &&
2306                 echo d >z/d &&
2307                 git add z/d &&
2308                 test_tick &&
2309                 git commit -m "B"
2310         )
2311 }
2312
2313 test_expect_success '8e: Both sides rename, one side adds to original directory' '
2314         test_setup_8e &&
2315         (
2316                 cd 8e &&
2317
2318                 git checkout A^0 &&
2319
2320                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
2321                 test_i18ngrep CONFLICT.*rename/rename.*z/c.*y/c.*w/c out &&
2322                 test_i18ngrep CONFLICT.*rename/rename.*z/b.*y/b.*w/b out &&
2323
2324                 git ls-files -s >out &&
2325                 test_line_count = 7 out &&
2326                 git ls-files -u >out &&
2327                 test_line_count = 6 out &&
2328                 git ls-files -o >out &&
2329                 test_line_count = 2 out &&
2330
2331                 git rev-parse >actual \
2332                         :1:z/b :2:y/b :3:w/b :1:z/c :2:y/c :3:w/c :0:y/d &&
2333                 git rev-parse >expect \
2334                          O:z/b  O:z/b  O:z/b  O:z/c  O:z/c  O:z/c  B:z/d &&
2335                 test_cmp expect actual &&
2336
2337                 git hash-object >actual \
2338                         y/b   w/b   y/c   w/c &&
2339                 git rev-parse >expect \
2340                         O:z/b O:z/b O:z/c O:z/c &&
2341                 test_cmp expect actual &&
2342
2343                 test_path_is_missing z/b &&
2344                 test_path_is_missing z/c
2345         )
2346 '
2347
2348 ###########################################################################
2349 # SECTION 9: Other testcases
2350 #
2351 # This section consists of miscellaneous testcases I thought of during
2352 # the implementation which round out the testing.
2353 ###########################################################################
2354
2355 # Testcase 9a, Inner renamed directory within outer renamed directory
2356 #   (Related to testcase 1f)
2357 #   Commit O: z/{b,c,d/{e,f,g}}
2358 #   Commit A: y/{b,c}, x/w/{e,f,g}
2359 #   Commit B: z/{b,c,d/{e,f,g,h},i}
2360 #   Expected: y/{b,c,i}, x/w/{e,f,g,h}
2361 #   NOTE: The only reason this one is interesting is because when a directory
2362 #         is split into multiple other directories, we determine by the weight
2363 #         of which one had the most paths going to it.  A naive implementation
2364 #         of that could take the new file in commit B at z/i to x/w/i or x/i.
2365
2366 test_setup_9a () {
2367         test_create_repo 9a &&
2368         (
2369                 cd 9a &&
2370
2371                 mkdir -p z/d &&
2372                 echo b >z/b &&
2373                 echo c >z/c &&
2374                 echo e >z/d/e &&
2375                 echo f >z/d/f &&
2376                 echo g >z/d/g &&
2377                 git add z &&
2378                 test_tick &&
2379                 git commit -m "O" &&
2380
2381                 git branch O &&
2382                 git branch A &&
2383                 git branch B &&
2384
2385                 git checkout A &&
2386                 mkdir x &&
2387                 git mv z/d x/w &&
2388                 git mv z y &&
2389                 test_tick &&
2390                 git commit -m "A" &&
2391
2392                 git checkout B &&
2393                 echo h >z/d/h &&
2394                 echo i >z/i &&
2395                 git add z &&
2396                 test_tick &&
2397                 git commit -m "B"
2398         )
2399 }
2400
2401 test_expect_success '9a: Inner renamed directory within outer renamed directory' '
2402         test_setup_9a &&
2403         (
2404                 cd 9a &&
2405
2406                 git checkout A^0 &&
2407
2408                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2409
2410                 git ls-files -s >out &&
2411                 test_line_count = 7 out &&
2412                 git ls-files -u >out &&
2413                 test_line_count = 0 out &&
2414                 git ls-files -o >out &&
2415                 test_line_count = 1 out &&
2416
2417                 git rev-parse >actual \
2418                         HEAD:y/b HEAD:y/c HEAD:y/i &&
2419                 git rev-parse >expect \
2420                         O:z/b    O:z/c    B:z/i &&
2421                 test_cmp expect actual &&
2422
2423                 git rev-parse >actual \
2424                         HEAD:x/w/e HEAD:x/w/f HEAD:x/w/g HEAD:x/w/h &&
2425                 git rev-parse >expect \
2426                         O:z/d/e    O:z/d/f    O:z/d/g    B:z/d/h &&
2427                 test_cmp expect actual
2428         )
2429 '
2430
2431 # Testcase 9b, Transitive rename with content merge
2432 #   (Related to testcase 1c)
2433 #   Commit O: z/{b,c},   x/d_1
2434 #   Commit A: y/{b,c},   x/d_2
2435 #   Commit B: z/{b,c,d_3}
2436 #   Expected: y/{b,c,d_merged}
2437
2438 test_setup_9b () {
2439         test_create_repo 9b &&
2440         (
2441                 cd 9b &&
2442
2443                 mkdir z &&
2444                 echo b >z/b &&
2445                 echo c >z/c &&
2446                 mkdir x &&
2447                 test_seq 1 10 >x/d &&
2448                 git add z x &&
2449                 test_tick &&
2450                 git commit -m "O" &&
2451
2452                 git branch O &&
2453                 git branch A &&
2454                 git branch B &&
2455
2456                 git checkout A &&
2457                 git mv z y &&
2458                 test_seq 1 11 >x/d &&
2459                 git add x/d &&
2460                 test_tick &&
2461                 git commit -m "A" &&
2462
2463                 git checkout B &&
2464                 test_seq 0 10 >x/d &&
2465                 git mv x/d z/d &&
2466                 git add z/d &&
2467                 test_tick &&
2468                 git commit -m "B"
2469         )
2470 }
2471
2472 test_expect_success '9b: Transitive rename with content merge' '
2473         test_setup_9b &&
2474         (
2475                 cd 9b &&
2476
2477                 git checkout A^0 &&
2478
2479                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2480
2481                 git ls-files -s >out &&
2482                 test_line_count = 3 out &&
2483
2484                 test_seq 0 11 >expected &&
2485                 test_cmp expected y/d &&
2486                 git add expected &&
2487                 git rev-parse >actual \
2488                         HEAD:y/b HEAD:y/c HEAD:y/d &&
2489                 git rev-parse >expect \
2490                         O:z/b    O:z/c    :0:expected &&
2491                 test_cmp expect actual &&
2492                 test_must_fail git rev-parse HEAD:x/d &&
2493                 test_must_fail git rev-parse HEAD:z/d &&
2494                 test_path_is_missing z/d &&
2495
2496                 test $(git rev-parse HEAD:y/d) != $(git rev-parse O:x/d) &&
2497                 test $(git rev-parse HEAD:y/d) != $(git rev-parse A:x/d) &&
2498                 test $(git rev-parse HEAD:y/d) != $(git rev-parse B:z/d)
2499         )
2500 '
2501
2502 # Testcase 9c, Doubly transitive rename?
2503 #   (Related to testcase 1c, 7e, and 9d)
2504 #   Commit O: z/{b,c},     x/{d,e},    w/f
2505 #   Commit A: y/{b,c},     x/{d,e,f,g}
2506 #   Commit B: z/{b,c,d,e},             w/f
2507 #   Expected: y/{b,c,d,e}, x/{f,g}
2508 #
2509 #   NOTE: x/f and x/g may be slightly confusing here.  The rename from w/f to
2510 #         x/f is clear.  Let's look beyond that.  Here's the logic:
2511 #            Commit B renamed x/ -> z/
2512 #            Commit A renamed z/ -> y/
2513 #         So, we could possibly further rename x/f to z/f to y/f, a doubly
2514 #         transient rename.  However, where does it end?  We can chain these
2515 #         indefinitely (see testcase 9d).  What if there is a D/F conflict
2516 #         at z/f/ or y/f/?  Or just another file conflict at one of those
2517 #         paths?  In the case of an N-long chain of transient renamings,
2518 #         where do we "abort" the rename at?  Can the user make sense of
2519 #         the resulting conflict and resolve it?
2520 #
2521 #         To avoid this confusion I use the simple rule that if the other side
2522 #         of history did a directory rename to a path that your side renamed
2523 #         away, then ignore that particular rename from the other side of
2524 #         history for any implicit directory renames.
2525
2526 test_setup_9c () {
2527         test_create_repo 9c &&
2528         (
2529                 cd 9c &&
2530
2531                 mkdir z &&
2532                 echo b >z/b &&
2533                 echo c >z/c &&
2534                 mkdir x &&
2535                 echo d >x/d &&
2536                 echo e >x/e &&
2537                 mkdir w &&
2538                 echo f >w/f &&
2539                 git add z x w &&
2540                 test_tick &&
2541                 git commit -m "O" &&
2542
2543                 git branch O &&
2544                 git branch A &&
2545                 git branch B &&
2546
2547                 git checkout A &&
2548                 git mv z y &&
2549                 git mv w/f x/ &&
2550                 echo g >x/g &&
2551                 git add x/g &&
2552                 test_tick &&
2553                 git commit -m "A" &&
2554
2555                 git checkout B &&
2556                 git mv x/d z/d &&
2557                 git mv x/e z/e &&
2558                 test_tick &&
2559                 git commit -m "B"
2560         )
2561 }
2562
2563 test_expect_success '9c: Doubly transitive rename?' '
2564         test_setup_9c &&
2565         (
2566                 cd 9c &&
2567
2568                 git checkout A^0 &&
2569
2570                 git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2571                 test_i18ngrep "WARNING: Avoiding applying x -> z rename to x/f" out &&
2572
2573                 git ls-files -s >out &&
2574                 test_line_count = 6 out &&
2575                 git ls-files -o >out &&
2576                 test_line_count = 1 out &&
2577
2578                 git rev-parse >actual \
2579                         HEAD:y/b HEAD:y/c HEAD:y/d HEAD:y/e HEAD:x/f HEAD:x/g &&
2580                 git rev-parse >expect \
2581                         O:z/b    O:z/c    O:x/d    O:x/e    O:w/f    A:x/g &&
2582                 test_cmp expect actual
2583         )
2584 '
2585
2586 # Testcase 9d, N-fold transitive rename?
2587 #   (Related to testcase 9c...and 1c and 7e)
2588 #   Commit O: z/a, y/b, x/c, w/d, v/e, u/f
2589 #   Commit A:  y/{a,b},  w/{c,d},  u/{e,f}
2590 #   Commit B: z/{a,t}, x/{b,c}, v/{d,e}, u/f
2591 #   Expected: <see NOTE first>
2592 #
2593 #   NOTE: z/ -> y/ (in commit A)
2594 #         y/ -> x/ (in commit B)
2595 #         x/ -> w/ (in commit A)
2596 #         w/ -> v/ (in commit B)
2597 #         v/ -> u/ (in commit A)
2598 #         So, if we add a file to z, say z/t, where should it end up?  In u?
2599 #         What if there's another file or directory named 't' in one of the
2600 #         intervening directories and/or in u itself?  Also, shouldn't the
2601 #         same logic that places 't' in u/ also move ALL other files to u/?
2602 #         What if there are file or directory conflicts in any of them?  If
2603 #         we attempted to do N-way (N-fold? N-ary? N-uple?) transitive renames
2604 #         like this, would the user have any hope of understanding any
2605 #         conflicts or how their working tree ended up?  I think not, so I'm
2606 #         ruling out N-ary transitive renames for N>1.
2607 #
2608 #   Therefore our expected result is:
2609 #     z/t, y/a, x/b, w/c, u/d, u/e, u/f
2610 #   The reason that v/d DOES get transitively renamed to u/d is that u/ isn't
2611 #   renamed somewhere.  A slightly sub-optimal result, but it uses fairly
2612 #   simple rules that are consistent with what we need for all the other
2613 #   testcases and simplifies things for the user.
2614
2615 test_setup_9d () {
2616         test_create_repo 9d &&
2617         (
2618                 cd 9d &&
2619
2620                 mkdir z y x w v u &&
2621                 echo a >z/a &&
2622                 echo b >y/b &&
2623                 echo c >x/c &&
2624                 echo d >w/d &&
2625                 echo e >v/e &&
2626                 echo f >u/f &&
2627                 git add z y x w v u &&
2628                 test_tick &&
2629                 git commit -m "O" &&
2630
2631                 git branch O &&
2632                 git branch A &&
2633                 git branch B &&
2634
2635                 git checkout A &&
2636                 git mv z/a y/ &&
2637                 git mv x/c w/ &&
2638                 git mv v/e u/ &&
2639                 test_tick &&
2640                 git commit -m "A" &&
2641
2642                 git checkout B &&
2643                 echo t >z/t &&
2644                 git mv y/b x/ &&
2645                 git mv w/d v/ &&
2646                 git add z/t &&
2647                 test_tick &&
2648                 git commit -m "B"
2649         )
2650 }
2651
2652 test_expect_success '9d: N-way transitive rename?' '
2653         test_setup_9d &&
2654         (
2655                 cd 9d &&
2656
2657                 git checkout A^0 &&
2658
2659                 git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2660                 test_i18ngrep "WARNING: Avoiding applying z -> y rename to z/t" out &&
2661                 test_i18ngrep "WARNING: Avoiding applying y -> x rename to y/a" out &&
2662                 test_i18ngrep "WARNING: Avoiding applying x -> w rename to x/b" out &&
2663                 test_i18ngrep "WARNING: Avoiding applying w -> v rename to w/c" out &&
2664
2665                 git ls-files -s >out &&
2666                 test_line_count = 7 out &&
2667                 git ls-files -o >out &&
2668                 test_line_count = 1 out &&
2669
2670                 git rev-parse >actual \
2671                         HEAD:z/t \
2672                         HEAD:y/a HEAD:x/b HEAD:w/c \
2673                         HEAD:u/d HEAD:u/e HEAD:u/f &&
2674                 git rev-parse >expect \
2675                         B:z/t    \
2676                         O:z/a    O:y/b    O:x/c    \
2677                         O:w/d    O:v/e    A:u/f &&
2678                 test_cmp expect actual
2679         )
2680 '
2681
2682 # Testcase 9e, N-to-1 whammo
2683 #   (Related to testcase 9c...and 1c and 7e)
2684 #   Commit O: dir1/{a,b}, dir2/{d,e}, dir3/{g,h}, dirN/{j,k}
2685 #   Commit A: dir1/{a,b,c,yo}, dir2/{d,e,f,yo}, dir3/{g,h,i,yo}, dirN/{j,k,l,yo}
2686 #   Commit B: combined/{a,b,d,e,g,h,j,k}
2687 #   Expected: combined/{a,b,c,d,e,f,g,h,i,j,k,l}, CONFLICT(Nto1) warnings,
2688 #             dir1/yo, dir2/yo, dir3/yo, dirN/yo
2689
2690 test_setup_9e () {
2691         test_create_repo 9e &&
2692         (
2693                 cd 9e &&
2694
2695                 mkdir dir1 dir2 dir3 dirN &&
2696                 echo a >dir1/a &&
2697                 echo b >dir1/b &&
2698                 echo d >dir2/d &&
2699                 echo e >dir2/e &&
2700                 echo g >dir3/g &&
2701                 echo h >dir3/h &&
2702                 echo j >dirN/j &&
2703                 echo k >dirN/k &&
2704                 git add dir* &&
2705                 test_tick &&
2706                 git commit -m "O" &&
2707
2708                 git branch O &&
2709                 git branch A &&
2710                 git branch B &&
2711
2712                 git checkout A &&
2713                 echo c  >dir1/c &&
2714                 echo yo >dir1/yo &&
2715                 echo f  >dir2/f &&
2716                 echo yo >dir2/yo &&
2717                 echo i  >dir3/i &&
2718                 echo yo >dir3/yo &&
2719                 echo l  >dirN/l &&
2720                 echo yo >dirN/yo &&
2721                 git add dir* &&
2722                 test_tick &&
2723                 git commit -m "A" &&
2724
2725                 git checkout B &&
2726                 git mv dir1 combined &&
2727                 git mv dir2/* combined/ &&
2728                 git mv dir3/* combined/ &&
2729                 git mv dirN/* combined/ &&
2730                 test_tick &&
2731                 git commit -m "B"
2732         )
2733 }
2734
2735 test_expect_success C_LOCALE_OUTPUT '9e: N-to-1 whammo' '
2736         test_setup_9e &&
2737         (
2738                 cd 9e &&
2739
2740                 git checkout A^0 &&
2741
2742                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
2743                 grep "CONFLICT (implicit dir rename): Cannot map more than one path to combined/yo" out >error_line &&
2744                 grep -q dir1/yo error_line &&
2745                 grep -q dir2/yo error_line &&
2746                 grep -q dir3/yo error_line &&
2747                 grep -q dirN/yo error_line &&
2748
2749                 git ls-files -s >out &&
2750                 test_line_count = 16 out &&
2751                 git ls-files -u >out &&
2752                 test_line_count = 0 out &&
2753                 git ls-files -o >out &&
2754                 test_line_count = 2 out &&
2755
2756                 git rev-parse >actual \
2757                         :0:combined/a :0:combined/b :0:combined/c \
2758                         :0:combined/d :0:combined/e :0:combined/f \
2759                         :0:combined/g :0:combined/h :0:combined/i \
2760                         :0:combined/j :0:combined/k :0:combined/l &&
2761                 git rev-parse >expect \
2762                          O:dir1/a      O:dir1/b      A:dir1/c \
2763                          O:dir2/d      O:dir2/e      A:dir2/f \
2764                          O:dir3/g      O:dir3/h      A:dir3/i \
2765                          O:dirN/j      O:dirN/k      A:dirN/l &&
2766                 test_cmp expect actual &&
2767
2768                 git rev-parse >actual \
2769                         :0:dir1/yo :0:dir2/yo :0:dir3/yo :0:dirN/yo &&
2770                 git rev-parse >expect \
2771                          A:dir1/yo  A:dir2/yo  A:dir3/yo  A:dirN/yo &&
2772                 test_cmp expect actual
2773         )
2774 '
2775
2776 # Testcase 9f, Renamed directory that only contained immediate subdirs
2777 #   (Related to testcases 1e & 9g)
2778 #   Commit O: goal/{a,b}/$more_files
2779 #   Commit A: priority/{a,b}/$more_files
2780 #   Commit B: goal/{a,b}/$more_files, goal/c
2781 #   Expected: priority/{a,b}/$more_files, priority/c
2782
2783 test_setup_9f () {
2784         test_create_repo 9f &&
2785         (
2786                 cd 9f &&
2787
2788                 mkdir -p goal/a &&
2789                 mkdir -p goal/b &&
2790                 echo foo >goal/a/foo &&
2791                 echo bar >goal/b/bar &&
2792                 echo baz >goal/b/baz &&
2793                 git add goal &&
2794                 test_tick &&
2795                 git commit -m "O" &&
2796
2797                 git branch O &&
2798                 git branch A &&
2799                 git branch B &&
2800
2801                 git checkout A &&
2802                 git mv goal/ priority &&
2803                 test_tick &&
2804                 git commit -m "A" &&
2805
2806                 git checkout B &&
2807                 echo c >goal/c &&
2808                 git add goal/c &&
2809                 test_tick &&
2810                 git commit -m "B"
2811         )
2812 }
2813
2814 test_expect_success '9f: Renamed directory that only contained immediate subdirs' '
2815         test_setup_9f &&
2816         (
2817                 cd 9f &&
2818
2819                 git checkout A^0 &&
2820
2821                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2822
2823                 git ls-files -s >out &&
2824                 test_line_count = 4 out &&
2825
2826                 git rev-parse >actual \
2827                         HEAD:priority/a/foo \
2828                         HEAD:priority/b/bar \
2829                         HEAD:priority/b/baz \
2830                         HEAD:priority/c &&
2831                 git rev-parse >expect \
2832                         O:goal/a/foo \
2833                         O:goal/b/bar \
2834                         O:goal/b/baz \
2835                         B:goal/c &&
2836                 test_cmp expect actual &&
2837                 test_must_fail git rev-parse HEAD:goal/c
2838         )
2839 '
2840
2841 # Testcase 9g, Renamed directory that only contained immediate subdirs, immediate subdirs renamed
2842 #   (Related to testcases 1e & 9f)
2843 #   Commit O: goal/{a,b}/$more_files
2844 #   Commit A: priority/{alpha,bravo}/$more_files
2845 #   Commit B: goal/{a,b}/$more_files, goal/c
2846 #   Expected: priority/{alpha,bravo}/$more_files, priority/c
2847
2848 test_setup_9g () {
2849         test_create_repo 9g &&
2850         (
2851                 cd 9g &&
2852
2853                 mkdir -p goal/a &&
2854                 mkdir -p goal/b &&
2855                 echo foo >goal/a/foo &&
2856                 echo bar >goal/b/bar &&
2857                 echo baz >goal/b/baz &&
2858                 git add goal &&
2859                 test_tick &&
2860                 git commit -m "O" &&
2861
2862                 git branch O &&
2863                 git branch A &&
2864                 git branch B &&
2865
2866                 git checkout A &&
2867                 mkdir priority &&
2868                 git mv goal/a/ priority/alpha &&
2869                 git mv goal/b/ priority/beta &&
2870                 rmdir goal/ &&
2871                 test_tick &&
2872                 git commit -m "A" &&
2873
2874                 git checkout B &&
2875                 echo c >goal/c &&
2876                 git add goal/c &&
2877                 test_tick &&
2878                 git commit -m "B"
2879         )
2880 }
2881
2882 test_expect_failure '9g: Renamed directory that only contained immediate subdirs, immediate subdirs renamed' '
2883         (
2884                 cd 9g &&
2885
2886                 git checkout A^0 &&
2887
2888                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2889
2890                 git ls-files -s >out &&
2891                 test_line_count = 4 out &&
2892
2893                 git rev-parse >actual \
2894                         HEAD:priority/alpha/foo \
2895                         HEAD:priority/beta/bar  \
2896                         HEAD:priority/beta/baz  \
2897                         HEAD:priority/c &&
2898                 git rev-parse >expect \
2899                         O:goal/a/foo \
2900                         O:goal/b/bar \
2901                         O:goal/b/baz \
2902                         B:goal/c &&
2903                 test_cmp expect actual &&
2904                 test_must_fail git rev-parse HEAD:goal/c
2905         )
2906 '
2907
2908 # Testcase 9h, Avoid implicit rename if involved as source on other side
2909 #   (Extremely closely related to testcase 3a)
2910 #   Commit O: z/{b,c,d_1}
2911 #   Commit A: z/{b,c,d_2}
2912 #   Commit B: y/{b,c}, x/d_1
2913 #   Expected: y/{b,c}, x/d_2
2914 #   NOTE: If we applied the z/ -> y/ rename to z/d, then we'd end up with
2915 #         a rename/rename(1to2) conflict (z/d -> y/d vs. x/d)
2916 test_setup_9h () {
2917         test_create_repo 9h &&
2918         (
2919                 cd 9h &&
2920
2921                 mkdir z &&
2922                 echo b >z/b &&
2923                 echo c >z/c &&
2924                 printf "1\n2\n3\n4\n5\n6\n7\n8\nd\n" >z/d &&
2925                 git add z &&
2926                 test_tick &&
2927                 git commit -m "O" &&
2928
2929                 git branch O &&
2930                 git branch A &&
2931                 git branch B &&
2932
2933                 git checkout A &&
2934                 test_tick &&
2935                 echo more >>z/d &&
2936                 git add z/d &&
2937                 git commit -m "A" &&
2938
2939                 git checkout B &&
2940                 mkdir y &&
2941                 mkdir x &&
2942                 git mv z/b y/ &&
2943                 git mv z/c y/ &&
2944                 git mv z/d x/ &&
2945                 rmdir z &&
2946                 test_tick &&
2947                 git commit -m "B"
2948         )
2949 }
2950
2951 test_expect_success '9h: Avoid dir rename on merely modified path' '
2952         test_setup_9h &&
2953         (
2954                 cd 9h &&
2955
2956                 git checkout A^0 &&
2957
2958                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
2959
2960                 git ls-files -s >out &&
2961                 test_line_count = 3 out &&
2962
2963                 git rev-parse >actual \
2964                         HEAD:y/b HEAD:y/c HEAD:x/d &&
2965                 git rev-parse >expect \
2966                         O:z/b    O:z/c    A:z/d &&
2967                 test_cmp expect actual
2968         )
2969 '
2970
2971 ###########################################################################
2972 # Rules suggested by section 9:
2973 #
2974 #   If the other side of history did a directory rename to a path that your
2975 #   side renamed away, then ignore that particular rename from the other
2976 #   side of history for any implicit directory renames.
2977 ###########################################################################
2978
2979 ###########################################################################
2980 # SECTION 10: Handling untracked files
2981 #
2982 # unpack_trees(), upon which the recursive merge algorithm is based, aborts
2983 # the operation if untracked or dirty files would be deleted or overwritten
2984 # by the merge.  Unfortunately, unpack_trees() does not understand renames,
2985 # and if it doesn't abort, then it muddies up the working directory before
2986 # we even get to the point of detecting renames, so we need some special
2987 # handling, at least in the case of directory renames.
2988 ###########################################################################
2989
2990 # Testcase 10a, Overwrite untracked: normal rename/delete
2991 #   Commit O: z/{b,c_1}
2992 #   Commit A: z/b + untracked z/c + untracked z/d
2993 #   Commit B: z/{b,d_1}
2994 #   Expected: Aborted Merge +
2995 #       ERROR_MSG(untracked working tree files would be overwritten by merge)
2996
2997 test_setup_10a () {
2998         test_create_repo 10a &&
2999         (
3000                 cd 10a &&
3001
3002                 mkdir z &&
3003                 echo b >z/b &&
3004                 echo c >z/c &&
3005                 git add z &&
3006                 test_tick &&
3007                 git commit -m "O" &&
3008
3009                 git branch O &&
3010                 git branch A &&
3011                 git branch B &&
3012
3013                 git checkout A &&
3014                 git rm z/c &&
3015                 test_tick &&
3016                 git commit -m "A" &&
3017
3018                 git checkout B &&
3019                 git mv z/c z/d &&
3020                 test_tick &&
3021                 git commit -m "B"
3022         )
3023 }
3024
3025 test_expect_success '10a: Overwrite untracked with normal rename/delete' '
3026         test_setup_10a &&
3027         (
3028                 cd 10a &&
3029
3030                 git checkout A^0 &&
3031                 echo very >z/c &&
3032                 echo important >z/d &&
3033
3034                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3035                 test_i18ngrep "The following untracked working tree files would be overwritten by merge" err &&
3036
3037                 git ls-files -s >out &&
3038                 test_line_count = 1 out &&
3039                 git ls-files -o >out &&
3040                 test_line_count = 4 out &&
3041
3042                 echo very >expect &&
3043                 test_cmp expect z/c &&
3044
3045                 echo important >expect &&
3046                 test_cmp expect z/d &&
3047
3048                 git rev-parse HEAD:z/b >actual &&
3049                 git rev-parse O:z/b >expect &&
3050                 test_cmp expect actual
3051         )
3052 '
3053
3054 # Testcase 10b, Overwrite untracked: dir rename + delete
3055 #   Commit O: z/{b,c_1}
3056 #   Commit A: y/b + untracked y/{c,d,e}
3057 #   Commit B: z/{b,d_1,e}
3058 #   Expected: Failed Merge; y/b + untracked y/c + untracked y/d on disk +
3059 #             z/c_1 -> z/d_1 rename recorded at stage 3 for y/d +
3060 #       ERROR_MSG(refusing to lose untracked file at 'y/d')
3061
3062 test_setup_10b () {
3063         test_create_repo 10b &&
3064         (
3065                 cd 10b &&
3066
3067                 mkdir z &&
3068                 echo b >z/b &&
3069                 echo c >z/c &&
3070                 git add z &&
3071                 test_tick &&
3072                 git commit -m "O" &&
3073
3074                 git branch O &&
3075                 git branch A &&
3076                 git branch B &&
3077
3078                 git checkout A &&
3079                 git rm z/c &&
3080                 git mv z/ y/ &&
3081                 test_tick &&
3082                 git commit -m "A" &&
3083
3084                 git checkout B &&
3085                 git mv z/c z/d &&
3086                 echo e >z/e &&
3087                 git add z/e &&
3088                 test_tick &&
3089                 git commit -m "B"
3090         )
3091 }
3092
3093 test_expect_success '10b: Overwrite untracked with dir rename + delete' '
3094         test_setup_10b &&
3095         (
3096                 cd 10b &&
3097
3098                 git checkout A^0 &&
3099                 echo very >y/c &&
3100                 echo important >y/d &&
3101                 echo contents >y/e &&
3102
3103                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3104                 test_i18ngrep "CONFLICT (rename/delete).*Version B\^0 of y/d left in tree at y/d~B\^0" out &&
3105                 test_i18ngrep "Error: Refusing to lose untracked file at y/e; writing to y/e~B\^0 instead" out &&
3106
3107                 git ls-files -s >out &&
3108                 test_line_count = 3 out &&
3109                 git ls-files -u >out &&
3110                 test_line_count = 2 out &&
3111                 git ls-files -o >out &&
3112                 test_line_count = 5 out &&
3113
3114                 git rev-parse >actual \
3115                         :0:y/b :3:y/d :3:y/e &&
3116                 git rev-parse >expect \
3117                         O:z/b  O:z/c  B:z/e &&
3118                 test_cmp expect actual &&
3119
3120                 echo very >expect &&
3121                 test_cmp expect y/c &&
3122
3123                 echo important >expect &&
3124                 test_cmp expect y/d &&
3125
3126                 echo contents >expect &&
3127                 test_cmp expect y/e
3128         )
3129 '
3130
3131 # Testcase 10c, Overwrite untracked: dir rename/rename(1to2)
3132 #   Commit O: z/{a,b}, x/{c,d}
3133 #   Commit A: y/{a,b}, w/c, x/d + different untracked y/c
3134 #   Commit B: z/{a,b,c}, x/d
3135 #   Expected: Failed Merge; y/{a,b} + x/d + untracked y/c +
3136 #             CONFLICT(rename/rename) x/c -> w/c vs y/c +
3137 #             y/c~B^0 +
3138 #             ERROR_MSG(Refusing to lose untracked file at y/c)
3139
3140 test_setup_10c () {
3141         test_create_repo 10c_$1 &&
3142         (
3143                 cd 10c_$1 &&
3144
3145                 mkdir z x &&
3146                 echo a >z/a &&
3147                 echo b >z/b &&
3148                 echo c >x/c &&
3149                 echo d >x/d &&
3150                 git add z x &&
3151                 test_tick &&
3152                 git commit -m "O" &&
3153
3154                 git branch O &&
3155                 git branch A &&
3156                 git branch B &&
3157
3158                 git checkout A &&
3159                 mkdir w &&
3160                 git mv x/c w/c &&
3161                 git mv z/ y/ &&
3162                 test_tick &&
3163                 git commit -m "A" &&
3164
3165                 git checkout B &&
3166                 git mv x/c z/ &&
3167                 test_tick &&
3168                 git commit -m "B"
3169         )
3170 }
3171
3172 test_expect_success '10c1: Overwrite untracked with dir rename/rename(1to2)' '
3173         test_setup_10c 1 &&
3174         (
3175                 cd 10c_1 &&
3176
3177                 git checkout A^0 &&
3178                 echo important >y/c &&
3179
3180                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3181                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3182                 test_i18ngrep "Refusing to lose untracked file at y/c; adding as y/c~B\^0 instead" out &&
3183
3184                 git ls-files -s >out &&
3185                 test_line_count = 6 out &&
3186                 git ls-files -u >out &&
3187                 test_line_count = 3 out &&
3188                 git ls-files -o >out &&
3189                 test_line_count = 3 out &&
3190
3191                 git rev-parse >actual \
3192                         :0:y/a :0:y/b :0:x/d :1:x/c :2:w/c :3:y/c &&
3193                 git rev-parse >expect \
3194                          O:z/a  O:z/b  O:x/d  O:x/c  O:x/c  O:x/c &&
3195                 test_cmp expect actual &&
3196
3197                 git hash-object y/c~B^0 >actual &&
3198                 git rev-parse O:x/c >expect &&
3199                 test_cmp expect actual &&
3200
3201                 echo important >expect &&
3202                 test_cmp expect y/c
3203         )
3204 '
3205
3206 test_expect_success '10c2: Overwrite untracked with dir rename/rename(1to2), other direction' '
3207         test_setup_10c 2 &&
3208         (
3209                 cd 10c_2 &&
3210
3211                 git reset --hard &&
3212                 git clean -fdqx &&
3213
3214                 git checkout B^0 &&
3215                 mkdir y &&
3216                 echo important >y/c &&
3217
3218                 test_must_fail git -c merge.directoryRenames=true merge -s recursive A^0 >out 2>err &&
3219                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3220                 test_i18ngrep "Refusing to lose untracked file at y/c; adding as y/c~HEAD instead" out &&
3221
3222                 git ls-files -s >out &&
3223                 test_line_count = 6 out &&
3224                 git ls-files -u >out &&
3225                 test_line_count = 3 out &&
3226                 git ls-files -o >out &&
3227                 test_line_count = 3 out &&
3228
3229                 git rev-parse >actual \
3230                         :0:y/a :0:y/b :0:x/d :1:x/c :3:w/c :2:y/c &&
3231                 git rev-parse >expect \
3232                          O:z/a  O:z/b  O:x/d  O:x/c  O:x/c  O:x/c &&
3233                 test_cmp expect actual &&
3234
3235                 git hash-object y/c~HEAD >actual &&
3236                 git rev-parse O:x/c >expect &&
3237                 test_cmp expect actual &&
3238
3239                 echo important >expect &&
3240                 test_cmp expect y/c
3241         )
3242 '
3243
3244 # Testcase 10d, Delete untracked w/ dir rename/rename(2to1)
3245 #   Commit O: z/{a,b,c_1},        x/{d,e,f_2}
3246 #   Commit A: y/{a,b},            x/{d,e,f_2,wham_1} + untracked y/wham
3247 #   Commit B: z/{a,b,c_1,wham_2}, y/{d,e}
3248 #   Expected: Failed Merge; y/{a,b,d,e} + untracked y/{wham,wham~merged}+
3249 #             CONFLICT(rename/rename) z/c_1 vs x/f_2 -> y/wham
3250 #             ERROR_MSG(Refusing to lose untracked file at y/wham)
3251
3252 test_setup_10d () {
3253         test_create_repo 10d &&
3254         (
3255                 cd 10d &&
3256
3257                 mkdir z x &&
3258                 echo a >z/a &&
3259                 echo b >z/b &&
3260                 echo c >z/c &&
3261                 echo d >x/d &&
3262                 echo e >x/e &&
3263                 echo f >x/f &&
3264                 git add z x &&
3265                 test_tick &&
3266                 git commit -m "O" &&
3267
3268                 git branch O &&
3269                 git branch A &&
3270                 git branch B &&
3271
3272                 git checkout A &&
3273                 git mv z/c x/wham &&
3274                 git mv z/ y/ &&
3275                 test_tick &&
3276                 git commit -m "A" &&
3277
3278                 git checkout B &&
3279                 git mv x/f z/wham &&
3280                 git mv x/ y/ &&
3281                 test_tick &&
3282                 git commit -m "B"
3283         )
3284 }
3285
3286 test_expect_success '10d: Delete untracked with dir rename/rename(2to1)' '
3287         test_setup_10d &&
3288         (
3289                 cd 10d &&
3290
3291                 git checkout A^0 &&
3292                 echo important >y/wham &&
3293
3294                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3295                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3296                 test_i18ngrep "Refusing to lose untracked file at y/wham" out &&
3297
3298                 git ls-files -s >out &&
3299                 test_line_count = 6 out &&
3300                 git ls-files -u >out &&
3301                 test_line_count = 2 out &&
3302                 git ls-files -o >out &&
3303                 test_line_count = 3 out &&
3304
3305                 git rev-parse >actual \
3306                         :0:y/a :0:y/b :0:y/d :0:y/e :2:y/wham :3:y/wham &&
3307                 git rev-parse >expect \
3308                          O:z/a  O:z/b  O:x/d  O:x/e  O:z/c     O:x/f &&
3309                 test_cmp expect actual &&
3310
3311                 test_must_fail git rev-parse :1:y/wham &&
3312
3313                 echo important >expect &&
3314                 test_cmp expect y/wham &&
3315
3316                 # Test that the two-way merge in y/wham~merged is as expected
3317                 git cat-file -p :2:y/wham >expect &&
3318                 git cat-file -p :3:y/wham >other &&
3319                 >empty &&
3320                 test_must_fail git merge-file \
3321                         -L "HEAD" \
3322                         -L "" \
3323                         -L "B^0" \
3324                         expect empty other &&
3325                 test_cmp expect y/wham~merged
3326         )
3327 '
3328
3329 # Testcase 10e, Does git complain about untracked file that's not in the way?
3330 #   Commit O: z/{a,b}
3331 #   Commit A: y/{a,b} + untracked z/c
3332 #   Commit B: z/{a,b,c}
3333 #   Expected: y/{a,b,c} + untracked z/c
3334
3335 test_setup_10e () {
3336         test_create_repo 10e &&
3337         (
3338                 cd 10e &&
3339
3340                 mkdir z &&
3341                 echo a >z/a &&
3342                 echo b >z/b &&
3343                 git add z &&
3344                 test_tick &&
3345                 git commit -m "O" &&
3346
3347                 git branch O &&
3348                 git branch A &&
3349                 git branch B &&
3350
3351                 git checkout A &&
3352                 git mv z/ y/ &&
3353                 test_tick &&
3354                 git commit -m "A" &&
3355
3356                 git checkout B &&
3357                 echo c >z/c &&
3358                 git add z/c &&
3359                 test_tick &&
3360                 git commit -m "B"
3361         )
3362 }
3363
3364 test_expect_failure '10e: Does git complain about untracked file that is not really in the way?' '
3365         (
3366                 cd 10e &&
3367
3368                 git checkout A^0 &&
3369                 mkdir z &&
3370                 echo random >z/c &&
3371
3372                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3373                 test_i18ngrep ! "following untracked working tree files would be overwritten by merge" err &&
3374
3375                 git ls-files -s >out &&
3376                 test_line_count = 3 out &&
3377                 git ls-files -u >out &&
3378                 test_line_count = 0 out &&
3379                 git ls-files -o >out &&
3380                 test_line_count = 3 out &&
3381
3382                 git rev-parse >actual \
3383                         :0:y/a :0:y/b :0:y/c &&
3384                 git rev-parse >expect \
3385                          O:z/a  O:z/b  B:z/c &&
3386                 test_cmp expect actual &&
3387
3388                 echo random >expect &&
3389                 test_cmp expect z/c
3390         )
3391 '
3392
3393 ###########################################################################
3394 # SECTION 11: Handling dirty (not up-to-date) files
3395 #
3396 # unpack_trees(), upon which the recursive merge algorithm is based, aborts
3397 # the operation if untracked or dirty files would be deleted or overwritten
3398 # by the merge.  Unfortunately, unpack_trees() does not understand renames,
3399 # and if it doesn't abort, then it muddies up the working directory before
3400 # we even get to the point of detecting renames, so we need some special
3401 # handling.  This was true even of normal renames, but there are additional
3402 # codepaths that need special handling with directory renames.  Add
3403 # testcases for both renamed-by-directory-rename-detection and standard
3404 # rename cases.
3405 ###########################################################################
3406
3407 # Testcase 11a, Avoid losing dirty contents with simple rename
3408 #   Commit O: z/{a,b_v1},
3409 #   Commit A: z/{a,c_v1}, and z/c_v1 has uncommitted mods
3410 #   Commit B: z/{a,b_v2}
3411 #   Expected: ERROR_MSG(Refusing to lose dirty file at z/c) +
3412 #             z/a, staged version of z/c has sha1sum matching B:z/b_v2,
3413 #             z/c~HEAD with contents of B:z/b_v2,
3414 #             z/c with uncommitted mods on top of A:z/c_v1
3415
3416 test_setup_11a () {
3417         test_create_repo 11a &&
3418         (
3419                 cd 11a &&
3420
3421                 mkdir z &&
3422                 echo a >z/a &&
3423                 test_seq 1 10 >z/b &&
3424                 git add z &&
3425                 test_tick &&
3426                 git commit -m "O" &&
3427
3428                 git branch O &&
3429                 git branch A &&
3430                 git branch B &&
3431
3432                 git checkout A &&
3433                 git mv z/b z/c &&
3434                 test_tick &&
3435                 git commit -m "A" &&
3436
3437                 git checkout B &&
3438                 echo 11 >>z/b &&
3439                 git add z/b &&
3440                 test_tick &&
3441                 git commit -m "B"
3442         )
3443 }
3444
3445 test_expect_success '11a: Avoid losing dirty contents with simple rename' '
3446         test_setup_11a &&
3447         (
3448                 cd 11a &&
3449
3450                 git checkout A^0 &&
3451                 echo stuff >>z/c &&
3452
3453                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3454                 test_i18ngrep "Refusing to lose dirty file at z/c" out &&
3455
3456                 test_seq 1 10 >expected &&
3457                 echo stuff >>expected &&
3458                 test_cmp expected z/c &&
3459
3460                 git ls-files -s >out &&
3461                 test_line_count = 2 out &&
3462                 git ls-files -u >out &&
3463                 test_line_count = 1 out &&
3464                 git ls-files -o >out &&
3465                 test_line_count = 4 out &&
3466
3467                 git rev-parse >actual \
3468                         :0:z/a :2:z/c &&
3469                 git rev-parse >expect \
3470                          O:z/a  B:z/b &&
3471                 test_cmp expect actual &&
3472
3473                 git hash-object z/c~HEAD >actual &&
3474                 git rev-parse B:z/b >expect &&
3475                 test_cmp expect actual
3476         )
3477 '
3478
3479 # Testcase 11b, Avoid losing dirty file involved in directory rename
3480 #   Commit O: z/a,         x/{b,c_v1}
3481 #   Commit A: z/{a,c_v1},  x/b,       and z/c_v1 has uncommitted mods
3482 #   Commit B: y/a,         x/{b,c_v2}
3483 #   Expected: y/{a,c_v2}, x/b, z/c_v1 with uncommitted mods untracked,
3484 #             ERROR_MSG(Refusing to lose dirty file at z/c)
3485
3486
3487 test_setup_11b () {
3488         test_create_repo 11b &&
3489         (
3490                 cd 11b &&
3491
3492                 mkdir z x &&
3493                 echo a >z/a &&
3494                 echo b >x/b &&
3495                 test_seq 1 10 >x/c &&
3496                 git add z x &&
3497                 test_tick &&
3498                 git commit -m "O" &&
3499
3500                 git branch O &&
3501                 git branch A &&
3502                 git branch B &&
3503
3504                 git checkout A &&
3505                 git mv x/c z/c &&
3506                 test_tick &&
3507                 git commit -m "A" &&
3508
3509                 git checkout B &&
3510                 git mv z y &&
3511                 echo 11 >>x/c &&
3512                 git add x/c &&
3513                 test_tick &&
3514                 git commit -m "B"
3515         )
3516 }
3517
3518 test_expect_success '11b: Avoid losing dirty file involved in directory rename' '
3519         test_setup_11b &&
3520         (
3521                 cd 11b &&
3522
3523                 git checkout A^0 &&
3524                 echo stuff >>z/c &&
3525
3526                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3527                 test_i18ngrep "Refusing to lose dirty file at z/c" out &&
3528
3529                 grep -q stuff z/c &&
3530                 test_seq 1 10 >expected &&
3531                 echo stuff >>expected &&
3532                 test_cmp expected z/c &&
3533
3534                 git ls-files -s >out &&
3535                 test_line_count = 3 out &&
3536                 git ls-files -u >out &&
3537                 test_line_count = 0 out &&
3538                 git ls-files -m >out &&
3539                 test_line_count = 0 out &&
3540                 git ls-files -o >out &&
3541                 test_line_count = 4 out &&
3542
3543                 git rev-parse >actual \
3544                         :0:x/b :0:y/a :0:y/c &&
3545                 git rev-parse >expect \
3546                          O:x/b  O:z/a  B:x/c &&
3547                 test_cmp expect actual &&
3548
3549                 git hash-object y/c >actual &&
3550                 git rev-parse B:x/c >expect &&
3551                 test_cmp expect actual
3552         )
3553 '
3554
3555 # Testcase 11c, Avoid losing not-up-to-date with rename + D/F conflict
3556 #   Commit O: y/a,         x/{b,c_v1}
3557 #   Commit A: y/{a,c_v1},  x/b,       and y/c_v1 has uncommitted mods
3558 #   Commit B: y/{a,c/d},   x/{b,c_v2}
3559 #   Expected: Abort_msg("following files would be overwritten by merge") +
3560 #             y/c left untouched (still has uncommitted mods)
3561
3562 test_setup_11c () {
3563         test_create_repo 11c &&
3564         (
3565                 cd 11c &&
3566
3567                 mkdir y x &&
3568                 echo a >y/a &&
3569                 echo b >x/b &&
3570                 test_seq 1 10 >x/c &&
3571                 git add y x &&
3572                 test_tick &&
3573                 git commit -m "O" &&
3574
3575                 git branch O &&
3576                 git branch A &&
3577                 git branch B &&
3578
3579                 git checkout A &&
3580                 git mv x/c y/c &&
3581                 test_tick &&
3582                 git commit -m "A" &&
3583
3584                 git checkout B &&
3585                 mkdir y/c &&
3586                 echo d >y/c/d &&
3587                 echo 11 >>x/c &&
3588                 git add x/c y/c/d &&
3589                 test_tick &&
3590                 git commit -m "B"
3591         )
3592 }
3593
3594 test_expect_success '11c: Avoid losing not-uptodate with rename + D/F conflict' '
3595         test_setup_11c &&
3596         (
3597                 cd 11c &&
3598
3599                 git checkout A^0 &&
3600                 echo stuff >>y/c &&
3601
3602                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3603                 test_i18ngrep "following files would be overwritten by merge" err &&
3604
3605                 grep -q stuff y/c &&
3606                 test_seq 1 10 >expected &&
3607                 echo stuff >>expected &&
3608                 test_cmp expected y/c &&
3609
3610                 git ls-files -s >out &&
3611                 test_line_count = 3 out &&
3612                 git ls-files -u >out &&
3613                 test_line_count = 0 out &&
3614                 git ls-files -m >out &&
3615                 test_line_count = 1 out &&
3616                 git ls-files -o >out &&
3617                 test_line_count = 3 out
3618         )
3619 '
3620
3621 # Testcase 11d, Avoid losing not-up-to-date with rename + D/F conflict
3622 #   Commit O: z/a,         x/{b,c_v1}
3623 #   Commit A: z/{a,c_v1},  x/b,       and z/c_v1 has uncommitted mods
3624 #   Commit B: y/{a,c/d},   x/{b,c_v2}
3625 #   Expected: D/F: y/c_v2 vs y/c/d) +
3626 #             Warning_Msg("Refusing to lose dirty file at z/c) +
3627 #             y/{a,c~HEAD,c/d}, x/b, now-untracked z/c_v1 with uncommitted mods
3628
3629 test_setup_11d () {
3630         test_create_repo 11d &&
3631         (
3632                 cd 11d &&
3633
3634                 mkdir z x &&
3635                 echo a >z/a &&
3636                 echo b >x/b &&
3637                 test_seq 1 10 >x/c &&
3638                 git add z x &&
3639                 test_tick &&
3640                 git commit -m "O" &&
3641
3642                 git branch O &&
3643                 git branch A &&
3644                 git branch B &&
3645
3646                 git checkout A &&
3647                 git mv x/c z/c &&
3648                 test_tick &&
3649                 git commit -m "A" &&
3650
3651                 git checkout B &&
3652                 git mv z y &&
3653                 mkdir y/c &&
3654                 echo d >y/c/d &&
3655                 echo 11 >>x/c &&
3656                 git add x/c y/c/d &&
3657                 test_tick &&
3658                 git commit -m "B"
3659         )
3660 }
3661
3662 test_expect_success '11d: Avoid losing not-uptodate with rename + D/F conflict' '
3663         test_setup_11d &&
3664         (
3665                 cd 11d &&
3666
3667                 git checkout A^0 &&
3668                 echo stuff >>z/c &&
3669
3670                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3671                 test_i18ngrep "Refusing to lose dirty file at z/c" out &&
3672
3673                 grep -q stuff z/c &&
3674                 test_seq 1 10 >expected &&
3675                 echo stuff >>expected &&
3676                 test_cmp expected z/c &&
3677
3678                 git ls-files -s >out &&
3679                 test_line_count = 4 out &&
3680                 git ls-files -u >out &&
3681                 test_line_count = 1 out &&
3682                 git ls-files -o >out &&
3683                 test_line_count = 5 out &&
3684
3685                 git rev-parse >actual \
3686                         :0:x/b :0:y/a :0:y/c/d :3:y/c &&
3687                 git rev-parse >expect \
3688                          O:x/b  O:z/a  B:y/c/d  B:x/c &&
3689                 test_cmp expect actual &&
3690
3691                 git hash-object y/c~HEAD >actual &&
3692                 git rev-parse B:x/c >expect &&
3693                 test_cmp expect actual
3694         )
3695 '
3696
3697 # Testcase 11e, Avoid deleting not-up-to-date with dir rename/rename(1to2)/add
3698 #   Commit O: z/{a,b},      x/{c_1,d}
3699 #   Commit A: y/{a,b,c_2},  x/d, w/c_1, and y/c_2 has uncommitted mods
3700 #   Commit B: z/{a,b,c_1},  x/d
3701 #   Expected: Failed Merge; y/{a,b} + x/d +
3702 #             CONFLICT(rename/rename) x/c_1 -> w/c_1 vs y/c_1 +
3703 #             ERROR_MSG(Refusing to lose dirty file at y/c)
3704 #             y/c~B^0 has O:x/c_1 contents
3705 #             y/c~HEAD has A:y/c_2 contents
3706 #             y/c has dirty file from before merge
3707
3708 test_setup_11e () {
3709         test_create_repo 11e &&
3710         (
3711                 cd 11e &&
3712
3713                 mkdir z x &&
3714                 echo a >z/a &&
3715                 echo b >z/b &&
3716                 echo c >x/c &&
3717                 echo d >x/d &&
3718                 git add z x &&
3719                 test_tick &&
3720                 git commit -m "O" &&
3721
3722                 git branch O &&
3723                 git branch A &&
3724                 git branch B &&
3725
3726                 git checkout A &&
3727                 git mv z/ y/ &&
3728                 echo different >y/c &&
3729                 mkdir w &&
3730                 git mv x/c w/ &&
3731                 git add y/c &&
3732                 test_tick &&
3733                 git commit -m "A" &&
3734
3735                 git checkout B &&
3736                 git mv x/c z/ &&
3737                 test_tick &&
3738                 git commit -m "B"
3739         )
3740 }
3741
3742 test_expect_success '11e: Avoid deleting not-uptodate with dir rename/rename(1to2)/add' '
3743         test_setup_11e &&
3744         (
3745                 cd 11e &&
3746
3747                 git checkout A^0 &&
3748                 echo mods >>y/c &&
3749
3750                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3751                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3752                 test_i18ngrep "Refusing to lose dirty file at y/c" out &&
3753
3754                 git ls-files -s >out &&
3755                 test_line_count = 7 out &&
3756                 git ls-files -u >out &&
3757                 test_line_count = 4 out &&
3758                 git ls-files -o >out &&
3759                 test_line_count = 3 out &&
3760
3761                 echo different >expected &&
3762                 echo mods >>expected &&
3763                 test_cmp expected y/c &&
3764
3765                 git rev-parse >actual \
3766                         :0:y/a :0:y/b :0:x/d :1:x/c :2:w/c :2:y/c :3:y/c &&
3767                 git rev-parse >expect \
3768                          O:z/a  O:z/b  O:x/d  O:x/c  O:x/c  A:y/c  O:x/c &&
3769                 test_cmp expect actual &&
3770
3771                 # See if y/c~merged has expected contents; requires manually
3772                 # doing the expected file merge
3773                 git cat-file -p A:y/c >c1 &&
3774                 git cat-file -p B:z/c >c2 &&
3775                 >empty &&
3776                 test_must_fail git merge-file \
3777                         -L "HEAD" \
3778                         -L "" \
3779                         -L "B^0" \
3780                         c1 empty c2 &&
3781                 test_cmp c1 y/c~merged
3782         )
3783 '
3784
3785 # Testcase 11f, Avoid deleting not-up-to-date w/ dir rename/rename(2to1)
3786 #   Commit O: z/{a,b},        x/{c_1,d_2}
3787 #   Commit A: y/{a,b,wham_1}, x/d_2, except y/wham has uncommitted mods
3788 #   Commit B: z/{a,b,wham_2}, x/c_1
3789 #   Expected: Failed Merge; y/{a,b} + untracked y/{wham~merged} +
3790 #             y/wham with dirty changes from before merge +
3791 #             CONFLICT(rename/rename) x/c vs x/d -> y/wham
3792 #             ERROR_MSG(Refusing to lose dirty file at y/wham)
3793
3794 test_setup_11f () {
3795         test_create_repo 11f &&
3796         (
3797                 cd 11f &&
3798
3799                 mkdir z x &&
3800                 echo a >z/a &&
3801                 echo b >z/b &&
3802                 test_seq 1 10 >x/c &&
3803                 echo d >x/d &&
3804                 git add z x &&
3805                 test_tick &&
3806                 git commit -m "O" &&
3807
3808                 git branch O &&
3809                 git branch A &&
3810                 git branch B &&
3811
3812                 git checkout A &&
3813                 git mv z/ y/ &&
3814                 git mv x/c y/wham &&
3815                 test_tick &&
3816                 git commit -m "A" &&
3817
3818                 git checkout B &&
3819                 git mv x/d z/wham &&
3820                 test_tick &&
3821                 git commit -m "B"
3822         )
3823 }
3824
3825 test_expect_success '11f: Avoid deleting not-uptodate with dir rename/rename(2to1)' '
3826         test_setup_11f &&
3827         (
3828                 cd 11f &&
3829
3830                 git checkout A^0 &&
3831                 echo important >>y/wham &&
3832
3833                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
3834                 test_i18ngrep "CONFLICT (rename/rename)" out &&
3835                 test_i18ngrep "Refusing to lose dirty file at y/wham" out &&
3836
3837                 git ls-files -s >out &&
3838                 test_line_count = 4 out &&
3839                 git ls-files -u >out &&
3840                 test_line_count = 2 out &&
3841                 git ls-files -o >out &&
3842                 test_line_count = 3 out &&
3843
3844                 test_seq 1 10 >expected &&
3845                 echo important >>expected &&
3846                 test_cmp expected y/wham &&
3847
3848                 test_must_fail git rev-parse :1:y/wham &&
3849
3850                 git rev-parse >actual \
3851                         :0:y/a :0:y/b :2:y/wham :3:y/wham &&
3852                 git rev-parse >expect \
3853                          O:z/a  O:z/b  O:x/c     O:x/d &&
3854                 test_cmp expect actual &&
3855
3856                 # Test that the two-way merge in y/wham~merged is as expected
3857                 git cat-file -p :2:y/wham >expect &&
3858                 git cat-file -p :3:y/wham >other &&
3859                 >empty &&
3860                 test_must_fail git merge-file \
3861                         -L "HEAD" \
3862                         -L "" \
3863                         -L "B^0" \
3864                         expect empty other &&
3865                 test_cmp expect y/wham~merged
3866         )
3867 '
3868
3869 ###########################################################################
3870 # SECTION 12: Everything else
3871 #
3872 # Tests suggested by others.  Tests added after implementation completed
3873 # and submitted.  Grab bag.
3874 ###########################################################################
3875
3876 # Testcase 12a, Moving one directory hierarchy into another
3877 #   (Related to testcase 9a)
3878 #   Commit O: node1/{leaf1,leaf2}, node2/{leaf3,leaf4}
3879 #   Commit A: node1/{leaf1,leaf2,node2/{leaf3,leaf4}}
3880 #   Commit B: node1/{leaf1,leaf2,leaf5}, node2/{leaf3,leaf4,leaf6}
3881 #   Expected: node1/{leaf1,leaf2,leaf5,node2/{leaf3,leaf4,leaf6}}
3882
3883 test_setup_12a () {
3884         test_create_repo 12a &&
3885         (
3886                 cd 12a &&
3887
3888                 mkdir -p node1 node2 &&
3889                 echo leaf1 >node1/leaf1 &&
3890                 echo leaf2 >node1/leaf2 &&
3891                 echo leaf3 >node2/leaf3 &&
3892                 echo leaf4 >node2/leaf4 &&
3893                 git add node1 node2 &&
3894                 test_tick &&
3895                 git commit -m "O" &&
3896
3897                 git branch O &&
3898                 git branch A &&
3899                 git branch B &&
3900
3901                 git checkout A &&
3902                 git mv node2/ node1/ &&
3903                 test_tick &&
3904                 git commit -m "A" &&
3905
3906                 git checkout B &&
3907                 echo leaf5 >node1/leaf5 &&
3908                 echo leaf6 >node2/leaf6 &&
3909                 git add node1 node2 &&
3910                 test_tick &&
3911                 git commit -m "B"
3912         )
3913 }
3914
3915 test_expect_success '12a: Moving one directory hierarchy into another' '
3916         test_setup_12a &&
3917         (
3918                 cd 12a &&
3919
3920                 git checkout A^0 &&
3921
3922                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
3923
3924                 git ls-files -s >out &&
3925                 test_line_count = 6 out &&
3926
3927                 git rev-parse >actual \
3928                         HEAD:node1/leaf1 HEAD:node1/leaf2 HEAD:node1/leaf5 \
3929                         HEAD:node1/node2/leaf3 \
3930                         HEAD:node1/node2/leaf4 \
3931                         HEAD:node1/node2/leaf6 &&
3932                 git rev-parse >expect \
3933                         O:node1/leaf1    O:node1/leaf2    B:node1/leaf5 \
3934                         O:node2/leaf3 \
3935                         O:node2/leaf4 \
3936                         B:node2/leaf6 &&
3937                 test_cmp expect actual
3938         )
3939 '
3940
3941 # Testcase 12b, Moving two directory hierarchies into each other
3942 #   (Related to testcases 1c and 12c)
3943 #   Commit O: node1/{leaf1, leaf2}, node2/{leaf3, leaf4}
3944 #   Commit A: node1/{leaf1, leaf2, node2/{leaf3, leaf4}}
3945 #   Commit B: node2/{leaf3, leaf4, node1/{leaf1, leaf2}}
3946 #   Expected: node1/node2/node1/{leaf1, leaf2},
3947 #             node2/node1/node2/{leaf3, leaf4}
3948 #   NOTE: Without directory renames, we would expect
3949 #                   node2/node1/{leaf1, leaf2},
3950 #                   node1/node2/{leaf3, leaf4}
3951 #         with directory rename detection, we note that
3952 #             commit A renames node2/ -> node1/node2/
3953 #             commit B renames node1/ -> node2/node1/
3954 #         therefore, applying those directory renames to the initial result
3955 #         (making all four paths experience a transitive renaming), yields
3956 #         the expected result.
3957 #
3958 #         You may ask, is it weird to have two directories rename each other?
3959 #         To which, I can do no more than shrug my shoulders and say that
3960 #         even simple rules give weird results when given weird inputs.
3961
3962 test_setup_12b () {
3963         test_create_repo 12b &&
3964         (
3965                 cd 12b &&
3966
3967                 mkdir -p node1 node2 &&
3968                 echo leaf1 >node1/leaf1 &&
3969                 echo leaf2 >node1/leaf2 &&
3970                 echo leaf3 >node2/leaf3 &&
3971                 echo leaf4 >node2/leaf4 &&
3972                 git add node1 node2 &&
3973                 test_tick &&
3974                 git commit -m "O" &&
3975
3976                 git branch O &&
3977                 git branch A &&
3978                 git branch B &&
3979
3980                 git checkout A &&
3981                 git mv node2/ node1/ &&
3982                 test_tick &&
3983                 git commit -m "A" &&
3984
3985                 git checkout B &&
3986                 git mv node1/ node2/ &&
3987                 test_tick &&
3988                 git commit -m "B"
3989         )
3990 }
3991
3992 test_expect_success '12b: Moving two directory hierarchies into each other' '
3993         test_setup_12b &&
3994         (
3995                 cd 12b &&
3996
3997                 git checkout A^0 &&
3998
3999                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4000
4001                 git ls-files -s >out &&
4002                 test_line_count = 4 out &&
4003
4004                 git rev-parse >actual \
4005                         HEAD:node1/node2/node1/leaf1 \
4006                         HEAD:node1/node2/node1/leaf2 \
4007                         HEAD:node2/node1/node2/leaf3 \
4008                         HEAD:node2/node1/node2/leaf4 &&
4009                 git rev-parse >expect \
4010                         O:node1/leaf1 \
4011                         O:node1/leaf2 \
4012                         O:node2/leaf3 \
4013                         O:node2/leaf4 &&
4014                 test_cmp expect actual
4015         )
4016 '
4017
4018 # Testcase 12c, Moving two directory hierarchies into each other w/ content merge
4019 #   (Related to testcase 12b)
4020 #   Commit O: node1/{       leaf1_1, leaf2_1}, node2/{leaf3_1, leaf4_1}
4021 #   Commit A: node1/{       leaf1_2, leaf2_2,  node2/{leaf3_2, leaf4_2}}
4022 #   Commit B: node2/{node1/{leaf1_3, leaf2_3},        leaf3_3, leaf4_3}
4023 #   Expected: Content merge conflicts for each of:
4024 #               node1/node2/node1/{leaf1, leaf2},
4025 #               node2/node1/node2/{leaf3, leaf4}
4026 #   NOTE: This is *exactly* like 12c, except that every path is modified on
4027 #         each side of the merge.
4028
4029 test_setup_12c () {
4030         test_create_repo 12c &&
4031         (
4032                 cd 12c &&
4033
4034                 mkdir -p node1 node2 &&
4035                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf1\n" >node1/leaf1 &&
4036                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf2\n" >node1/leaf2 &&
4037                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf3\n" >node2/leaf3 &&
4038                 printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf4\n" >node2/leaf4 &&
4039                 git add node1 node2 &&
4040                 test_tick &&
4041                 git commit -m "O" &&
4042
4043                 git branch O &&
4044                 git branch A &&
4045                 git branch B &&
4046
4047                 git checkout A &&
4048                 git mv node2/ node1/ &&
4049                 for i in `git ls-files`; do echo side A >>$i; done &&
4050                 git add -u &&
4051                 test_tick &&
4052                 git commit -m "A" &&
4053
4054                 git checkout B &&
4055                 git mv node1/ node2/ &&
4056                 for i in `git ls-files`; do echo side B >>$i; done &&
4057                 git add -u &&
4058                 test_tick &&
4059                 git commit -m "B"
4060         )
4061 }
4062
4063 test_expect_success '12c: Moving one directory hierarchy into another w/ content merge' '
4064         test_setup_12c &&
4065         (
4066                 cd 12c &&
4067
4068                 git checkout A^0 &&
4069
4070                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 &&
4071
4072                 git ls-files -u >out &&
4073                 test_line_count = 12 out &&
4074
4075                 git rev-parse >actual \
4076                         :1:node1/node2/node1/leaf1 \
4077                         :1:node1/node2/node1/leaf2 \
4078                         :1:node2/node1/node2/leaf3 \
4079                         :1:node2/node1/node2/leaf4 \
4080                         :2:node1/node2/node1/leaf1 \
4081                         :2:node1/node2/node1/leaf2 \
4082                         :2:node2/node1/node2/leaf3 \
4083                         :2:node2/node1/node2/leaf4 \
4084                         :3:node1/node2/node1/leaf1 \
4085                         :3:node1/node2/node1/leaf2 \
4086                         :3:node2/node1/node2/leaf3 \
4087                         :3:node2/node1/node2/leaf4 &&
4088                 git rev-parse >expect \
4089                         O:node1/leaf1 \
4090                         O:node1/leaf2 \
4091                         O:node2/leaf3 \
4092                         O:node2/leaf4 \
4093                         A:node1/leaf1 \
4094                         A:node1/leaf2 \
4095                         A:node1/node2/leaf3 \
4096                         A:node1/node2/leaf4 \
4097                         B:node2/node1/leaf1 \
4098                         B:node2/node1/leaf2 \
4099                         B:node2/leaf3 \
4100                         B:node2/leaf4 &&
4101                 test_cmp expect actual
4102         )
4103 '
4104
4105 # Testcase 12d, Rename/merge of subdirectory into the root
4106 #   Commit O: a/b/subdir/foo
4107 #   Commit A: subdir/foo
4108 #   Commit B: a/b/subdir/foo, a/b/bar
4109 #   Expected: subdir/foo, bar
4110
4111 test_setup_12d () {
4112         test_create_repo 12d &&
4113         (
4114                 cd 12d &&
4115
4116                 mkdir -p a/b/subdir &&
4117                 test_commit a/b/subdir/foo &&
4118
4119                 git branch O &&
4120                 git branch A &&
4121                 git branch B &&
4122
4123                 git checkout A &&
4124                 mkdir subdir &&
4125                 git mv a/b/subdir/foo.t subdir/foo.t &&
4126                 test_tick &&
4127                 git commit -m "A" &&
4128
4129                 git checkout B &&
4130                 test_commit a/b/bar
4131         )
4132 }
4133
4134 test_expect_success '12d: Rename/merge subdir into the root, variant 1' '
4135         test_setup_12d &&
4136         (
4137                 cd 12d &&
4138
4139                 git checkout A^0 &&
4140
4141                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4142
4143                 git ls-files -s >out &&
4144                 test_line_count = 2 out &&
4145
4146                 git rev-parse >actual \
4147                         HEAD:subdir/foo.t   HEAD:bar.t &&
4148                 git rev-parse >expect \
4149                         O:a/b/subdir/foo.t  B:a/b/bar.t &&
4150                 test_cmp expect actual &&
4151
4152                 git hash-object bar.t >actual &&
4153                 git rev-parse B:a/b/bar.t >expect &&
4154                 test_cmp expect actual &&
4155
4156                 test_must_fail git rev-parse HEAD:a/b/subdir/foo.t &&
4157                 test_must_fail git rev-parse HEAD:a/b/bar.t &&
4158                 test_path_is_missing a/ &&
4159                 test_path_is_file bar.t
4160         )
4161 '
4162
4163 # Testcase 12e, Rename/merge of subdirectory into the root
4164 #   Commit O: a/b/foo
4165 #   Commit A: foo
4166 #   Commit B: a/b/foo, a/b/bar
4167 #   Expected: foo, bar
4168
4169 test_setup_12e () {
4170         test_create_repo 12e &&
4171         (
4172                 cd 12e &&
4173
4174                 mkdir -p a/b &&
4175                 test_commit a/b/foo &&
4176
4177                 git branch O &&
4178                 git branch A &&
4179                 git branch B &&
4180
4181                 git checkout A &&
4182                 mkdir subdir &&
4183                 git mv a/b/foo.t foo.t &&
4184                 test_tick &&
4185                 git commit -m "A" &&
4186
4187                 git checkout B &&
4188                 test_commit a/b/bar
4189         )
4190 }
4191
4192 test_expect_success '12e: Rename/merge subdir into the root, variant 2' '
4193         test_setup_12e &&
4194         (
4195                 cd 12e &&
4196
4197                 git checkout A^0 &&
4198
4199                 git -c merge.directoryRenames=true merge -s recursive B^0 &&
4200
4201                 git ls-files -s >out &&
4202                 test_line_count = 2 out &&
4203
4204                 git rev-parse >actual \
4205                         HEAD:foo.t   HEAD:bar.t &&
4206                 git rev-parse >expect \
4207                         O:a/b/foo.t  B:a/b/bar.t &&
4208                 test_cmp expect actual &&
4209
4210                 git hash-object bar.t >actual &&
4211                 git rev-parse B:a/b/bar.t >expect &&
4212                 test_cmp expect actual &&
4213
4214                 test_must_fail git rev-parse HEAD:a/b/foo.t &&
4215                 test_must_fail git rev-parse HEAD:a/b/bar.t &&
4216                 test_path_is_missing a/ &&
4217                 test_path_is_file bar.t
4218         )
4219 '
4220
4221 ###########################################################################
4222 # SECTION 13: Checking informational and conflict messages
4223 #
4224 # A year after directory rename detection became the default, it was
4225 # instead decided to report conflicts on the pathname on the basis that
4226 # some users may expect the new files added or moved into a directory to
4227 # be unrelated to all the other files in that directory, and thus that
4228 # directory rename detection is unexpected.  Test that the messages printed
4229 # match our expectation.
4230 ###########################################################################
4231
4232 # Testcase 13a, Basic directory rename with newly added files
4233 #   Commit O: z/{b,c}
4234 #   Commit A: y/{b,c}
4235 #   Commit B: z/{b,c,d,e/f}
4236 #   Expected: y/{b,c,d,e/f}, with notices/conflicts for both y/d and y/e/f
4237
4238 test_setup_13a () {
4239         test_create_repo 13a_$1 &&
4240         (
4241                 cd 13a_$1 &&
4242
4243                 mkdir z &&
4244                 echo b >z/b &&
4245                 echo c >z/c &&
4246                 git add z &&
4247                 test_tick &&
4248                 git commit -m "O" &&
4249
4250                 git branch O &&
4251                 git branch A &&
4252                 git branch B &&
4253
4254                 git checkout A &&
4255                 git mv z y &&
4256                 test_tick &&
4257                 git commit -m "A" &&
4258
4259                 git checkout B &&
4260                 echo d >z/d &&
4261                 mkdir z/e &&
4262                 echo f >z/e/f &&
4263                 git add z/d z/e/f &&
4264                 test_tick &&
4265                 git commit -m "B"
4266         )
4267 }
4268
4269 test_expect_success '13a(conflict): messages for newly added files' '
4270         test_setup_13a conflict &&
4271         (
4272                 cd 13a_conflict &&
4273
4274                 git checkout A^0 &&
4275
4276                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4277
4278                 test_i18ngrep CONFLICT..file.location.*z/e/f.added.in.B^0.*y/e/f out &&
4279                 test_i18ngrep CONFLICT..file.location.*z/d.added.in.B^0.*y/d out &&
4280
4281                 git ls-files >paths &&
4282                 ! grep z/ paths &&
4283                 grep "y/[de]" paths &&
4284
4285                 test_path_is_missing z/d &&
4286                 test_path_is_file    y/d &&
4287                 test_path_is_missing z/e/f &&
4288                 test_path_is_file    y/e/f
4289         )
4290 '
4291
4292 test_expect_success '13a(info): messages for newly added files' '
4293         test_setup_13a info &&
4294         (
4295                 cd 13a_info &&
4296
4297                 git reset --hard &&
4298                 git checkout A^0 &&
4299
4300                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4301
4302                 test_i18ngrep Path.updated:.*z/e/f.added.in.B^0.*y/e/f out &&
4303                 test_i18ngrep Path.updated:.*z/d.added.in.B^0.*y/d out &&
4304
4305                 git ls-files >paths &&
4306                 ! grep z/ paths &&
4307                 grep "y/[de]" paths &&
4308
4309                 test_path_is_missing z/d &&
4310                 test_path_is_file    y/d &&
4311                 test_path_is_missing z/e/f &&
4312                 test_path_is_file    y/e/f
4313         )
4314 '
4315
4316 # Testcase 13b, Transitive rename with conflicted content merge and default
4317 #               "conflict" setting
4318 #   (Related to testcase 1c, 9b)
4319 #   Commit O: z/{b,c},   x/d_1
4320 #   Commit A: y/{b,c},   x/d_2
4321 #   Commit B: z/{b,c,d_3}
4322 #   Expected: y/{b,c,d_merged}, with two conflict messages for y/d,
4323 #             one about content, and one about file location
4324
4325 test_setup_13b () {
4326         test_create_repo 13b_$1 &&
4327         (
4328                 cd 13b_$1 &&
4329
4330                 mkdir x &&
4331                 mkdir z &&
4332                 test_seq 1 10 >x/d &&
4333                 echo b >z/b &&
4334                 echo c >z/c &&
4335                 git add x z &&
4336                 test_tick &&
4337                 git commit -m "O" &&
4338
4339                 git branch O &&
4340                 git branch A &&
4341                 git branch B &&
4342
4343                 git checkout A &&
4344                 git mv z y &&
4345                 echo 11 >>x/d &&
4346                 git add x/d &&
4347                 test_tick &&
4348                 git commit -m "A" &&
4349
4350                 git checkout B &&
4351                 echo eleven >>x/d &&
4352                 git mv x/d z/d &&
4353                 git add z/d &&
4354                 test_tick &&
4355                 git commit -m "B"
4356         )
4357 }
4358
4359 test_expect_success '13b(conflict): messages for transitive rename with conflicted content' '
4360         test_setup_13b conflict &&
4361         (
4362                 cd 13b_conflict &&
4363
4364                 git checkout A^0 &&
4365
4366                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4367
4368                 test_i18ngrep CONFLICT.*content.*Merge.conflict.in.y/d out &&
4369                 test_i18ngrep CONFLICT..file.location.*x/d.renamed.to.z/d.*moved.to.y/d out &&
4370
4371                 git ls-files >paths &&
4372                 ! grep z/ paths &&
4373                 grep "y/d" paths &&
4374
4375                 test_path_is_missing z/d &&
4376                 test_path_is_file    y/d
4377         )
4378 '
4379
4380 test_expect_success '13b(info): messages for transitive rename with conflicted content' '
4381         test_setup_13b info &&
4382         (
4383                 cd 13b_info &&
4384
4385                 git reset --hard &&
4386                 git checkout A^0 &&
4387
4388                 test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4389
4390                 test_i18ngrep CONFLICT.*content.*Merge.conflict.in.y/d out &&
4391                 test_i18ngrep Path.updated:.*x/d.renamed.to.z/d.in.B^0.*moving.it.to.y/d out &&
4392
4393                 git ls-files >paths &&
4394                 ! grep z/ paths &&
4395                 grep "y/d" paths &&
4396
4397                 test_path_is_missing z/d &&
4398                 test_path_is_file    y/d
4399         )
4400 '
4401
4402 # Testcase 13c, Rename/rename(1to1) due to directory rename
4403 #   Commit O: z/{b,c},   x/{d,e}
4404 #   Commit A: y/{b,c,d}, x/e
4405 #   Commit B: z/{b,c,d}, x/e
4406 #   Expected: y/{b,c,d}, with info or conflict messages for d (
4407 #             A: renamed x/d -> z/d; B: renamed z/ -> y/ AND renamed x/d to y/d
4408 #             One could argue A had partial knowledge of what was done with
4409 #             d and B had full knowledge, but that's a slippery slope as
4410 #             shown in testcase 13d.
4411
4412 test_setup_13c () {
4413         test_create_repo 13c_$1 &&
4414         (
4415                 cd 13c_$1 &&
4416
4417                 mkdir x &&
4418                 mkdir z &&
4419                 test_seq 1 10 >x/d &&
4420                 echo e >x/e &&
4421                 echo b >z/b &&
4422                 echo c >z/c &&
4423                 git add x z &&
4424                 test_tick &&
4425                 git commit -m "O" &&
4426
4427                 git branch O &&
4428                 git branch A &&
4429                 git branch B &&
4430
4431                 git checkout A &&
4432                 git mv z y &&
4433                 git mv x/d y/ &&
4434                 test_tick &&
4435                 git commit -m "A" &&
4436
4437                 git checkout B &&
4438                 git mv x/d z/d &&
4439                 git add z/d &&
4440                 test_tick &&
4441                 git commit -m "B"
4442         )
4443 }
4444
4445 test_expect_success '13c(conflict): messages for rename/rename(1to1) via transitive rename' '
4446         test_setup_13c conflict &&
4447         (
4448                 cd 13c_conflict &&
4449
4450                 git checkout A^0 &&
4451
4452                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4453
4454                 test_i18ngrep CONFLICT..file.location.*x/d.renamed.to.z/d.*moved.to.y/d out &&
4455
4456                 git ls-files >paths &&
4457                 ! grep z/ paths &&
4458                 grep "y/d" paths &&
4459
4460                 test_path_is_missing z/d &&
4461                 test_path_is_file    y/d
4462         )
4463 '
4464
4465 test_expect_success '13c(info): messages for rename/rename(1to1) via transitive rename' '
4466         test_setup_13c info &&
4467         (
4468                 cd 13c_info &&
4469
4470                 git reset --hard &&
4471                 git checkout A^0 &&
4472
4473                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4474
4475                 test_i18ngrep Path.updated:.*x/d.renamed.to.z/d.in.B^0.*moving.it.to.y/d out &&
4476
4477                 git ls-files >paths &&
4478                 ! grep z/ paths &&
4479                 grep "y/d" paths &&
4480
4481                 test_path_is_missing z/d &&
4482                 test_path_is_file    y/d
4483         )
4484 '
4485
4486 # Testcase 13d, Rename/rename(1to1) due to directory rename on both sides
4487 #   Commit O: a/{z,y}, b/x,     c/w
4488 #   Commit A: a/z,     b/{y,x}, d/w
4489 #   Commit B: a/z,     d/x,     c/{y,w}
4490 #   Expected: a/z, d/{y,x,w} with no file location conflict for x
4491 #             Easy cases:
4492 #               * z is always in a; so it stays in a.
4493 #               * x starts in b, only modified on one side to move into d/
4494 #               * w starts in c, only modified on one side to move into d/
4495 #             Hard case:
4496 #               * A renames a/y to b/y, and B renames b/->d/ => a/y -> d/y
4497 #               * B renames a/y to c/y, and A renames c/->d/ => a/y -> d/y
4498 #               No conflict in where a/y ends up, so put it in d/y.
4499
4500 test_setup_13d () {
4501         test_create_repo 13d_$1 &&
4502         (
4503                 cd 13d_$1 &&
4504
4505                 mkdir a &&
4506                 mkdir b &&
4507                 mkdir c &&
4508                 echo z >a/z &&
4509                 echo y >a/y &&
4510                 echo x >b/x &&
4511                 echo w >c/w &&
4512                 git add a b c &&
4513                 test_tick &&
4514                 git commit -m "O" &&
4515
4516                 git branch O &&
4517                 git branch A &&
4518                 git branch B &&
4519
4520                 git checkout A &&
4521                 git mv a/y b/ &&
4522                 git mv c/ d/ &&
4523                 test_tick &&
4524                 git commit -m "A" &&
4525
4526                 git checkout B &&
4527                 git mv a/y c/ &&
4528                 git mv b/ d/ &&
4529                 test_tick &&
4530                 git commit -m "B"
4531         )
4532 }
4533
4534 test_expect_success '13d(conflict): messages for rename/rename(1to1) via dual transitive rename' '
4535         test_setup_13d conflict &&
4536         (
4537                 cd 13d_conflict &&
4538
4539                 git checkout A^0 &&
4540
4541                 test_must_fail git merge -s recursive B^0 >out 2>err &&
4542
4543                 test_i18ngrep CONFLICT..file.location.*a/y.renamed.to.b/y.*moved.to.d/y out &&
4544                 test_i18ngrep CONFLICT..file.location.*a/y.renamed.to.c/y.*moved.to.d/y out &&
4545
4546                 git ls-files >paths &&
4547                 ! grep b/ paths &&
4548                 ! grep c/ paths &&
4549                 grep "d/y" paths &&
4550
4551                 test_path_is_missing b/y &&
4552                 test_path_is_missing c/y &&
4553                 test_path_is_file    d/y
4554         )
4555 '
4556
4557 test_expect_success '13d(info): messages for rename/rename(1to1) via dual transitive rename' '
4558         test_setup_13d info &&
4559         (
4560                 cd 13d_info &&
4561
4562                 git reset --hard &&
4563                 git checkout A^0 &&
4564
4565                 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
4566
4567                 test_i18ngrep Path.updated.*a/y.renamed.to.b/y.*moving.it.to.d/y out &&
4568                 test_i18ngrep Path.updated.*a/y.renamed.to.c/y.*moving.it.to.d/y out &&
4569
4570                 git ls-files >paths &&
4571                 ! grep b/ paths &&
4572                 ! grep c/ paths &&
4573                 grep "d/y" paths &&
4574
4575                 test_path_is_missing b/y &&
4576                 test_path_is_missing c/y &&
4577                 test_path_is_file    d/y
4578         )
4579 '
4580
4581 # Testcase 13e, directory rename in virtual merge base
4582 #
4583 # This testcase has a slightly different setup than all the above cases, in
4584 # order to include a recursive case:
4585 #
4586 #      A   C
4587 #      o - o
4588 #     / \ / \
4589 #  O o   X   ?
4590 #     \ / \ /
4591 #      o   o
4592 #      B   D
4593 #
4594 #   Commit O: a/{z,y}
4595 #   Commit A: b/{z,y}
4596 #   Commit B: a/{z,y,x}
4597 #   Commit C: b/{z,y,x}
4598 #   Commit D: b/{z,y}, a/x
4599 #   Expected: b/{z,y,x}  (sort of; see below for why this might not be expected)
4600 #
4601 #   NOTES: 'X' represents a virtual merge base.  With the default of
4602 #          directory rename detection yielding conflicts, merging A and B
4603 #          results in a conflict complaining about whether 'x' should be
4604 #          under 'a/' or 'b/'.  However, when creating the virtual merge
4605 #          base 'X', since virtual merge bases need to be written out as a
4606 #          tree, we cannot have a conflict, so some resolution has to be
4607 #          picked.
4608 #
4609 #          In choosing the right resolution, it's worth noting here that
4610 #          commits C & D are merges of A & B that choose different
4611 #          locations for 'x' (i.e. they resolve the conflict differently),
4612 #          and so it would be nice when merging C & D if git could detect
4613 #          this difference of opinion and report a conflict.  But the only
4614 #          way to do so that I can think of would be to have the virtual
4615 #          merge base place 'x' in some directory other than either 'a/' or
4616 #          'b/', which seems a little weird -- especially since it'd result
4617 #          in a rename/rename(1to2) conflict with a source path that never
4618 #          existed in any version.
4619 #
4620 #          So, for now, when directory rename detection is set to
4621 #          'conflict' just avoid doing directory rename detection at all in
4622 #          the recursive case.  This will not allow us to detect a conflict
4623 #          in the outer merge for this special kind of setup, but it at
4624 #          least avoids hitting a BUG().
4625 #
4626 test_setup_13e () {
4627         test_create_repo 13e &&
4628         (
4629                 cd 13e &&
4630
4631                 mkdir a &&
4632                 echo z >a/z &&
4633                 echo y >a/y &&
4634                 git add a &&
4635                 test_tick &&
4636                 git commit -m "O" &&
4637
4638                 git branch O &&
4639                 git branch A &&
4640                 git branch B &&
4641
4642                 git checkout A &&
4643                 git mv a/ b/ &&
4644                 test_tick &&
4645                 git commit -m "A" &&
4646
4647                 git checkout B &&
4648                 echo x >a/x &&
4649                 git add a &&
4650                 test_tick &&
4651                 git commit -m "B" &&
4652
4653                 git branch C A &&
4654                 git branch D B &&
4655
4656                 git checkout C &&
4657                 test_must_fail git -c merge.directoryRenames=conflict merge B &&
4658                 git add b/x &&
4659                 test_tick &&
4660                 git commit -m "C" &&
4661
4662
4663                 git checkout D &&
4664                 test_must_fail git -c merge.directoryRenames=conflict merge A &&
4665                 git add b/x &&
4666                 mkdir a &&
4667                 git mv b/x a/x &&
4668                 test_tick &&
4669                 git commit -m "D"
4670         )
4671 }
4672
4673 test_expect_success '13e: directory rename detection in recursive case' '
4674         test_setup_13e &&
4675         (
4676                 cd 13e &&
4677
4678                 git checkout --quiet D^0 &&
4679
4680                 git -c merge.directoryRenames=conflict merge -s recursive C^0 >out 2>err &&
4681
4682                 test_i18ngrep ! CONFLICT out &&
4683                 test_i18ngrep ! BUG: err &&
4684                 test_i18ngrep ! core.dumped err &&
4685                 test_must_be_empty err &&
4686
4687                 git ls-files >paths &&
4688                 ! grep a/x paths &&
4689                 grep b/x paths
4690         )
4691 '
4692
4693 test_done