merge-recursive: don't detect renames of empty files

Merge-recursive detects renames so that if one side modifies "foo" and the other side moves it to "bar", the modification is applied to "bar". However, our rename detection is based on content analysis, it can be wrong (i.e., two files were not intended as a rename, but just happen to have the same or similar content). This is quite rare if the files actually contain content, since two unrelated files are unlikely to have exactly the same content. However, empty files present a problem, in that there is nothing to analyze. An uninteresting placeholder file with zero bytes may or may not be related to a placeholder file with another name. The result is that adding content to an empty file may cause confusion if the other side of a merge removed it; your content may end up in another random placeholder file that was added. Let's err on the side of caution and not consider empty files as renames. This will cause a modify/delete conflict on the merge, which will let the user sort it out themselves. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 22, 2012 at 18:52 UTC 4f7cb99ada26be5d86402a6e060f3ee16a672f16
2 files changed +17
merge-recursive.c
+1
@@ -485,6 +485,7 @@ static struct string_list *get_renames(struct merge_options *o,
485 renames = xcalloc(1, sizeof(struct string_list));
486 diff_setup(&opts);
487 DIFF_OPT_SET(&opts, RECURSIVE);
488 + DIFF_OPT_CLR(&opts, RENAME_EMPTY);
489 opts.detect_rename = DIFF_DETECT_RENAME;
490 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
491 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
t/t6022-merge-rename.sh
+16
@@ -884,4 +884,20 @@ test_expect_success 'no spurious "refusing to lose untracked" message' '
884 ! grep "refusing to lose untracked file" errors.txt
885 '
886
887 +test_expect_success 'do not follow renames for empty files' '
888 + git checkout -f -b empty-base &&
889 + >empty1 &&
890 + git add empty1 &&
891 + git commit -m base &&
892 + echo content >empty1 &&
893 + git add empty1 &&
894 + git commit -m fill &&
895 + git checkout -b empty-topic HEAD^ &&
896 + git mv empty1 empty2 &&
897 + git commit -m rename &&
898 + test_must_fail git merge empty-base &&
899 + >expect &&
900 + test_cmp expect empty2
901 +'
902 +
903 test_done