3 test_description='Merge-recursive merging renames'
7 sed -e "$1" <"$2" >"$2.x" &&
11 test_expect_success 'setup' '
13 a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
14 b bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
15 c cccccccccccccccccccccccccccccccccccccccccccccccc
16 d dddddddddddddddddddddddddddddddddddddddddddddddd
17 e eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
18 f ffffffffffffffffffffffffffffffffffffffffffffffff
19 g gggggggggggggggggggggggggggggggggggggggggggggggg
20 h hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
21 i iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
22 j jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
23 k kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
24 l llllllllllllllllllllllllllllllllllllllllllllllll
25 m mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
26 n nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
27 o oooooooooooooooooooooooooooooooooooooooooooooooo
31 A AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
32 B BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
33 C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
34 D DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
35 E EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
36 F FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
37 G GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
38 H HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
39 I IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
40 J JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
41 K KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
42 L LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
43 M MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
44 N NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
45 O OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
49 git commit -m "initial has A and M" &&
55 git branch change+rename &&
57 sed -e "/^g /s/.*/g : master changes a line/" <A >A+ &&
59 git commit -a -m "master updates A" &&
61 git checkout yellow &&
63 git commit -a -m "yellow removes M" &&
66 sed -e "/^g /s/.*/g : white changes a line/" <A >B &&
67 sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
69 git update-index --add --remove A B M N &&
70 git commit -m "white renames A->B, M->N" &&
73 sed -e "/^g /s/.*/g : red changes a line/" <A >B &&
74 sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
76 git update-index --add --remove A B M N &&
77 git commit -m "red renames A->B, M->N" &&
80 sed -e "/^g /s/.*/g : blue changes a line/" <A >C &&
81 sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
83 git update-index --add --remove A C M N &&
84 git commit -m "blue renames A->C, M->N" &&
86 git checkout change &&
87 sed -e "/^g /s/.*/g : changed line/" <A >A+ &&
89 git commit -q -a -m "changed" &&
91 git checkout change+rename &&
92 sed -e "/^g /s/.*/g : changed line/" <A >B &&
94 git update-index --add B &&
95 git commit -q -a -m "changed and renamed" &&
100 test_expect_success 'pull renaming branch into unrenaming one' \
103 test_expect_code 1 git pull . white &&
105 git ls-files -u B >b.stages &&
106 test_line_count = 3 b.stages &&
107 git ls-files -s N >n.stages &&
108 test_line_count = 1 n.stages &&
112 }" B | grep master &&
113 git diff --exit-code white N
116 test_expect_success 'pull renaming branch into another renaming one' \
121 test_expect_code 1 git pull . white &&
122 git ls-files -u B >b.stages &&
123 test_line_count = 3 b.stages &&
124 git ls-files -s N >n.stages &&
125 test_line_count = 1 n.stages &&
130 git diff --exit-code white N
133 test_expect_success 'pull unrenaming branch into renaming one' \
137 test_expect_code 1 git pull . master &&
138 git ls-files -u B >b.stages &&
139 test_line_count = 3 b.stages &&
140 git ls-files -s N >n.stages &&
141 test_line_count = 1 n.stages &&
146 git diff --exit-code white N
149 test_expect_success 'pull conflicting renames' \
153 test_expect_code 1 git pull . blue &&
154 git ls-files -u A >a.stages &&
155 test_line_count = 1 a.stages &&
156 git ls-files -u B >b.stages &&
157 test_line_count = 1 b.stages &&
158 git ls-files -u C >c.stages &&
159 test_line_count = 1 c.stages &&
160 git ls-files -s N >n.stages &&
161 test_line_count = 1 n.stages &&
166 git diff --exit-code white N
169 test_expect_success 'interference with untracked working tree file' '
172 echo >A this file should not matter &&
173 test_expect_code 1 git pull . white &&
177 test_expect_success 'interference with untracked working tree file' '
179 git checkout white &&
182 echo >A this file should not matter &&
183 test_expect_code 1 git pull . red &&
187 test_expect_success 'interference with untracked working tree file' '
190 git checkout -f master &&
194 test_path_is_missing M &&
195 git reset --hard anchor
198 test_expect_success 'updated working tree file should prevent the merge' '
201 git checkout -f master &&
204 echo >>M one line addition &&
206 test_expect_code 128 git pull . yellow &&
207 test_cmp M M.saved &&
211 test_expect_success 'updated working tree file should prevent the merge' '
214 git checkout -f master &&
217 echo >>M one line addition &&
219 git update-index M &&
220 test_expect_code 128 git pull . yellow &&
221 test_cmp M M.saved &&
225 test_expect_success 'interference with untracked working tree file' '
228 git checkout -f yellow &&
231 echo >M this file should not matter &&
233 test_path_is_file M &&
238 git reset --hard anchor
241 test_expect_success 'merge of identical changes in a renamed file' '
244 git checkout change+rename &&
246 test-tool chmtime --get -3600 B >old-mtime &&
247 GIT_MERGE_VERBOSITY=3 git merge change >out &&
249 test-tool chmtime --get B >new-mtime &&
250 test_cmp old-mtime new-mtime &&
252 git reset --hard HEAD^ &&
253 git checkout change &&
255 # A will be renamed to B; we check mtimes and file presence
256 test_path_is_missing B &&
257 test-tool chmtime --get -3600 A >old-mtime &&
258 GIT_MERGE_VERBOSITY=3 git merge change+rename >out &&
260 test_path_is_missing A &&
261 test-tool chmtime --get B >new-mtime &&
262 test $(cat old-mtime) -lt $(cat new-mtime)
265 test_expect_success 'setup for rename + d/f conflicts' '
267 git checkout --orphan dir-in-way &&
273 printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >sub/file &&
274 echo foo >dir/file-in-the-way &&
276 git commit -m "Common commit" &&
278 echo 11 >>sub/file &&
279 echo more >>dir/file-in-the-way &&
281 git commit -m "Commit to merge, with dir in the way" &&
283 git checkout -b dir-not-in-way &&
284 git reset --soft HEAD^ &&
286 git commit -m "Commit to merge, with dir removed" -- dir sub/file &&
288 git checkout -b renamed-file-has-no-conflicts dir-in-way~1 &&
291 printf "1\n2\n3\n4\n5555\n6\n7\n8\n9\n10\n" >dir &&
293 git commit -m "Independent change" &&
295 git checkout -b renamed-file-has-conflicts dir-in-way~1 &&
297 git mv sub/file dir &&
300 git commit -m "Conflicting change"
303 test_expect_success 'Rename+D/F conflict; renamed file merges + dir not in way' '
305 git checkout -q renamed-file-has-no-conflicts^0 &&
307 git merge --strategy=recursive dir-not-in-way &&
310 test_path_is_file dir &&
311 test_write_lines 1 2 3 4 5555 6 7 8 9 10 11 >expected &&
312 test_cmp expected dir
315 test_expect_success 'Rename+D/F conflict; renamed file merges but dir in way' '
318 git checkout -q renamed-file-has-no-conflicts^0 &&
319 test_must_fail git merge --strategy=recursive dir-in-way >output &&
321 test_i18ngrep "CONFLICT (modify/delete): dir/file-in-the-way" output &&
322 test_i18ngrep "Auto-merging dir" output &&
323 test_i18ngrep "Adding as dir~HEAD instead" output &&
325 test 3 -eq "$(git ls-files -u | wc -l)" &&
326 test 2 -eq "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
328 test_must_fail git diff --quiet &&
329 test_must_fail git diff --cached --quiet &&
331 test_path_is_file dir/file-in-the-way &&
332 test_path_is_file dir~HEAD &&
333 test_cmp expected dir~HEAD
336 test_expect_success 'Same as previous, but merged other way' '
339 git checkout -q dir-in-way^0 &&
340 test_must_fail git merge --strategy=recursive renamed-file-has-no-conflicts >output 2>errors &&
342 ! grep "error: refusing to lose untracked file at" errors &&
343 test_i18ngrep "CONFLICT (modify/delete): dir/file-in-the-way" output &&
344 test_i18ngrep "Auto-merging dir" output &&
345 test_i18ngrep "Adding as dir~renamed-file-has-no-conflicts instead" output &&
347 test 3 -eq "$(git ls-files -u | wc -l)" &&
348 test 2 -eq "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
350 test_must_fail git diff --quiet &&
351 test_must_fail git diff --cached --quiet &&
353 test_path_is_file dir/file-in-the-way &&
354 test_path_is_file dir~renamed-file-has-no-conflicts &&
355 test_cmp expected dir~renamed-file-has-no-conflicts
358 test_expect_success 'Rename+D/F conflict; renamed file cannot merge, dir not in way' '
361 git checkout -q renamed-file-has-conflicts^0 &&
362 test_must_fail git merge --strategy=recursive dir-not-in-way &&
364 test 3 -eq "$(git ls-files -u | wc -l)" &&
365 test 3 -eq "$(git ls-files -u dir | wc -l)" &&
367 test_must_fail git diff --quiet &&
368 test_must_fail git diff --cached --quiet &&
370 test_path_is_file dir &&
371 cat >expected <<-\EOF &&
386 >>>>>>> dir-not-in-way:sub/file
388 test_cmp expected dir
391 test_expect_success 'Rename+D/F conflict; renamed file cannot merge and dir in the way' '
392 modify s/dir-not-in-way/dir-in-way/ expected &&
396 git checkout -q renamed-file-has-conflicts^0 &&
397 test_must_fail git merge --strategy=recursive dir-in-way &&
399 test 5 -eq "$(git ls-files -u | wc -l)" &&
400 test 3 -eq "$(git ls-files -u dir | grep -v file-in-the-way | wc -l)" &&
401 test 2 -eq "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
403 test_must_fail git diff --quiet &&
404 test_must_fail git diff --cached --quiet &&
406 test_path_is_file dir/file-in-the-way &&
407 test_path_is_file dir~HEAD &&
408 test_cmp expected dir~HEAD
411 test_expect_success 'Same as previous, but merged other way' '
414 git checkout -q dir-in-way^0 &&
415 test_must_fail git merge --strategy=recursive renamed-file-has-conflicts &&
417 test 5 -eq "$(git ls-files -u | wc -l)" &&
418 test 3 -eq "$(git ls-files -u dir | grep -v file-in-the-way | wc -l)" &&
419 test 2 -eq "$(git ls-files -u dir/file-in-the-way | wc -l)" &&
421 test_must_fail git diff --quiet &&
422 test_must_fail git diff --cached --quiet &&
424 test_path_is_file dir/file-in-the-way &&
425 test_path_is_file dir~renamed-file-has-conflicts &&
426 cat >expected <<-\EOF &&
437 <<<<<<< HEAD:sub/file
441 >>>>>>> renamed-file-has-conflicts:dir
443 test_cmp expected dir~renamed-file-has-conflicts
446 test_expect_success 'setup both rename source and destination involved in D/F conflict' '
448 git checkout --orphan rename-dest &&
453 echo stuff >one/file &&
455 git commit -m "Common commit" &&
457 git mv one/file destdir &&
458 git commit -m "Renamed to destdir" &&
460 git checkout -b source-conflict HEAD~1 &&
463 touch one destdir/foo &&
465 git commit -m "Conflicts in the way"
468 test_expect_success 'both rename source and destination involved in D/F conflict' '
471 git checkout -q rename-dest^0 &&
472 test_must_fail git merge --strategy=recursive source-conflict &&
474 test 1 -eq "$(git ls-files -u | wc -l)" &&
476 test_must_fail git diff --quiet &&
478 test_path_is_file destdir/foo &&
479 test_path_is_file one &&
480 test_path_is_file destdir~HEAD &&
481 test "stuff" = "$(cat destdir~HEAD)"
484 test_expect_success 'setup pair rename to parent of other (D/F conflicts)' '
486 git checkout --orphan rename-two &&
492 echo stuff >one/file &&
493 echo other >two/file &&
495 git commit -m "Common commit" &&
498 git mv two/file one &&
499 git commit -m "Rename two/file -> one" &&
501 git checkout -b rename-one HEAD~1 &&
503 git mv one/file two &&
505 git commit -m "Rename one/file -> two"
508 test_expect_success 'pair rename to parent of other (D/F conflicts) w/ untracked dir' '
509 git checkout -q rename-one^0 &&
511 test_must_fail git merge --strategy=recursive rename-two &&
513 test 2 -eq "$(git ls-files -u | wc -l)" &&
514 test 1 -eq "$(git ls-files -u one | wc -l)" &&
515 test 1 -eq "$(git ls-files -u two | wc -l)" &&
517 test_must_fail git diff --quiet &&
519 test 4 -eq $(find . | grep -v .git | wc -l) &&
521 test_path_is_dir one &&
522 test_path_is_file one~rename-two &&
523 test_path_is_file two &&
524 test "other" = $(cat one~rename-two) &&
525 test "stuff" = $(cat two)
528 test_expect_success 'pair rename to parent of other (D/F conflicts) w/ clean start' '
531 test_must_fail git merge --strategy=recursive rename-two &&
533 test 2 -eq "$(git ls-files -u | wc -l)" &&
534 test 1 -eq "$(git ls-files -u one | wc -l)" &&
535 test 1 -eq "$(git ls-files -u two | wc -l)" &&
537 test_must_fail git diff --quiet &&
539 test 3 -eq $(find . | grep -v .git | wc -l) &&
541 test_path_is_file one &&
542 test_path_is_file two &&
543 test "other" = $(cat one) &&
544 test "stuff" = $(cat two)
547 test_expect_success 'setup rename of one file to two, with directories in the way' '
549 git checkout --orphan first-rename &&
553 echo stuff >original &&
555 git commit -m "Common commit" &&
560 git mv original one &&
561 git commit -m "Put two/file in the way, rename to one" &&
563 git checkout -b second-rename HEAD~1 &&
567 git mv original two &&
568 git commit -m "Put one/file in the way, rename to two"
571 test_expect_success 'check handling of differently renamed file with D/F conflicts' '
572 git checkout -q first-rename^0 &&
573 test_must_fail git merge --strategy=recursive second-rename &&
575 test 5 -eq "$(git ls-files -s | wc -l)" &&
576 test 3 -eq "$(git ls-files -u | wc -l)" &&
577 test 1 -eq "$(git ls-files -u one | wc -l)" &&
578 test 1 -eq "$(git ls-files -u two | wc -l)" &&
579 test 1 -eq "$(git ls-files -u original | wc -l)" &&
580 test 2 -eq "$(git ls-files -o | wc -l)" &&
582 test_path_is_file one/file &&
583 test_path_is_file two/file &&
584 test_path_is_file one~HEAD &&
585 test_path_is_file two~second-rename &&
586 test_path_is_missing original
589 test_expect_success 'setup rename one file to two; directories moving out of the way' '
591 git checkout --orphan first-rename-redo &&
595 echo stuff >original &&
597 touch one/file two/file &&
599 git commit -m "Common commit" &&
602 git mv original one &&
603 git commit -m "Rename to one" &&
605 git checkout -b second-rename-redo HEAD~1 &&
607 git mv original two &&
608 git commit -m "Rename to two"
611 test_expect_success 'check handling of differently renamed file with D/F conflicts' '
612 git checkout -q first-rename-redo^0 &&
613 test_must_fail git merge --strategy=recursive second-rename-redo &&
615 test 3 -eq "$(git ls-files -u | wc -l)" &&
616 test 1 -eq "$(git ls-files -u one | wc -l)" &&
617 test 1 -eq "$(git ls-files -u two | wc -l)" &&
618 test 1 -eq "$(git ls-files -u original | wc -l)" &&
619 test 0 -eq "$(git ls-files -o | wc -l)" &&
621 test_path_is_file one &&
622 test_path_is_file two &&
623 test_path_is_missing original
626 test_expect_success 'setup avoid unnecessary update, normal rename' '
628 git checkout --orphan avoid-unnecessary-update-1 &&
632 printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >original &&
634 git commit -m "Common commit" &&
636 git mv original rename &&
639 git commit -m "Renamed and modified" &&
641 git checkout -b merge-branch-1 HEAD~1 &&
642 echo "random content" >random-file &&
644 git commit -m "Random, unrelated changes"
647 test_expect_success 'avoid unnecessary update, normal rename' '
648 git checkout -q avoid-unnecessary-update-1^0 &&
649 test-tool chmtime --get -3600 rename >expect &&
650 git merge merge-branch-1 &&
651 test-tool chmtime --get rename >actual &&
652 test_cmp expect actual # "rename" should have stayed intact
655 test_expect_success 'setup to test avoiding unnecessary update, with D/F conflict' '
657 git checkout --orphan avoid-unnecessary-update-2 &&
662 printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >df/file &&
664 git commit -m "Common commit" &&
666 git mv df/file temp &&
671 git commit -m "Renamed and modified" &&
673 git checkout -b merge-branch-2 HEAD~1 &&
675 git add unrelated-change &&
676 git commit -m "Only unrelated changes"
679 test_expect_success 'avoid unnecessary update, with D/F conflict' '
680 git checkout -q avoid-unnecessary-update-2^0 &&
681 test-tool chmtime --get -3600 df >expect &&
682 git merge merge-branch-2 &&
683 test-tool chmtime --get df >actual &&
684 test_cmp expect actual # "df" should have stayed intact
687 test_expect_success 'setup avoid unnecessary update, dir->(file,nothing)' '
699 git checkout -b side &&
703 git checkout master &&
707 git commit -m "Add a newfile"
710 test_expect_success 'avoid unnecessary update, dir->(file,nothing)' '
711 git checkout -q master^0 &&
712 test-tool chmtime --get -3600 df >expect &&
714 test-tool chmtime --get df >actual &&
715 test_cmp expect actual # "df" should have stayed intact
718 test_expect_success 'setup avoid unnecessary update, modify/delete' '
729 git checkout -b side &&
731 git commit -m "Delete file" &&
733 git checkout master &&
736 git commit -m "Modify file"
739 test_expect_success 'avoid unnecessary update, modify/delete' '
740 git checkout -q master^0 &&
741 test-tool chmtime --get -3600 file >expect &&
742 test_must_fail git merge side &&
743 test-tool chmtime --get file >actual &&
744 test_cmp expect actual # "file" should have stayed intact
747 test_expect_success 'setup avoid unnecessary update, rename/add-dest' '
753 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >file &&
757 git checkout -b side &&
760 git commit -m "Add file copy" &&
762 git checkout master &&
763 git mv file newfile &&
764 git commit -m "Rename file"
767 test_expect_success 'avoid unnecessary update, rename/add-dest' '
768 git checkout -q master^0 &&
769 test-tool chmtime --get -3600 newfile >expect &&
771 test-tool chmtime --get newfile >actual &&
772 test_cmp expect actual # "file" should have stayed intact
775 test_expect_success 'setup merge of rename + small change' '
777 git checkout --orphan rename-plus-small-change &&
781 echo ORIGINAL >file &&
785 git commit -m Initial &&
786 git checkout -b rename_branch &&
787 git mv file renamed_file &&
788 git commit -m Rename &&
789 git checkout rename-plus-small-change &&
790 echo NEW-VERSION >file &&
791 git commit -a -m Reformat
794 test_expect_success 'merge rename + small change' '
795 git merge rename_branch &&
797 test 1 -eq $(git ls-files -s | wc -l) &&
798 test 0 -eq $(git ls-files -o | wc -l) &&
799 test $(git rev-parse HEAD:renamed_file) = $(git rev-parse HEAD~1:file)
802 test_expect_success 'setup for use of extended merge markers' '
808 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >original_file &&
809 git add original_file &&
812 git checkout -b rename &&
813 echo 9 >>original_file &&
814 git add original_file &&
815 git mv original_file renamed_file &&
818 git checkout master &&
819 echo 8.5 >>original_file &&
820 git add original_file &&
824 test_expect_success 'merge master into rename has correct extended markers' '
825 git checkout rename^0 &&
826 test_must_fail git merge -s recursive master^0 &&
828 cat >expected <<-\EOF &&
837 <<<<<<< HEAD:renamed_file
841 >>>>>>> master^0:original_file
843 test_cmp expected renamed_file
846 test_expect_success 'merge rename into master has correct extended markers' '
848 git checkout master^0 &&
849 test_must_fail git merge -s recursive rename^0 &&
851 cat >expected <<-\EOF &&
860 <<<<<<< HEAD:original_file
864 >>>>>>> rename^0:renamed_file
866 test_cmp expected renamed_file
869 test_expect_success 'setup spurious "refusing to lose untracked" message' '
876 printf "1\n2\n3\n4\n5\n6\n7\n8\n" >original_file &&
877 git add irrelevant_file original_file &&
880 git checkout -b rename &&
881 git mv original_file renamed_file &&
884 git checkout master &&
885 git rm original_file &&
889 test_expect_success 'no spurious "refusing to lose untracked" message' '
890 git checkout master^0 &&
891 test_must_fail git merge rename^0 2>errors.txt &&
892 ! grep "refusing to lose untracked file" errors.txt
895 test_expect_success 'do not follow renames for empty files' '
896 git checkout -f -b empty-base &&
899 git commit -m base &&
900 echo content >empty1 &&
902 git commit -m fill &&
903 git checkout -b empty-topic HEAD^ &&
904 git mv empty1 empty2 &&
905 git commit -m rename &&
906 test_must_fail git merge empty-base &&
907 test_must_be_empty empty2