merge-ort: fix incorrect file handling

We have multiple bugs here -- accidental silent file deletion, accidental silent file retention for files that should be deleted, and incorrect number of entries left in the index. The series merged at commit d3b88be1b450 (Merge branch 'en/merge-dir-rename-corner-case-fix', 2021-07-16) introduced testcase 12i-12k in t6423 which checked for rename-to-self cases, and fixed bugs that merge-ort and merge-recursive had with these testcases. At the time, I noted that merge-ort had one bug for these cases, while merge-recursive had two. It turns out that merge-ort did in fact have another bug, but the "relevant renames" optimizations were masking it. If we modify testcase 12i from t6423 to modify the file in the commit that renames it (but only modify it enough that it can still be detected as a rename), then we can trigger silent deletion of the file. Tweak testcase 12i slightly to make the file in question have more than one line in it. This leaves the testcase intact other than changing the initial contents of this one file. The purpose of this tweak is to minimize the changes between this testcase and a new one that we want to add. Then duplicate testcase 12i as 12i2, changing it so that it adds a single line to the file in question when it is renamed; testcase 12i2 then serves as a testcase for this merge-ort bug that I previously overlooked. Further, commit 98a1a00d5301 (t6423: add a testcase causing a failed assertion in process_renames, 2025-03-06), fixed an issue with rename-to-self but added a new testcase, 12n, that only checked for whether the merge ran to completion. A few commits ago, we modified this test to check for the number of entries in the index -- but noted that the number was wrong. And we also noted a silently-keep-instead-of-delete bug at the same time in the new testcase 12n2. In summary, we have the following bugs with rename-to-self cases: * silent deletion of file expected to be kept (t6423 testcase 12i2) * silent retention of file expected to be removed (t6423 testcase 12n2) * wrong number of extries left in the index (t6423 testcase 12n) All of these bugs arise because in a rename-to-self case, when we have a rename A->B, both A and B name the same file. The code in process_renames() assumes A & B are different, and tries to move the higher order stages and file contents so that they are associated just with the new path, but the assumptions of A & B being different can cause A to be deleted when it's not supposed to be or mark B as resolved and kept in place when it's supposed to be deleted. Since A & B are already the same path in the rename-to-self case, simply skip the steps in process_renames() for such files to fix these bugs. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Aug 6, 2025 at 23:15 UTC 885ffe538bf616b9e9df8f2bf2dc67606200ebb8
2 files changed +80 -3
merge-ort.c
+14
@@ -2878,6 +2878,20 @@ static int process_renames(struct merge_options *opt,
2878 newinfo = new_ent->value;
2879 }
2880
2881 + /*
2882 + * Directory renames can result in rename-to-self; the code
2883 + * below assumes we have A->B with different A & B, and tries
2884 + * to move all entries to path B. If A & B are the same path,
2885 + * the logic can get confused, so skip further processing when
2886 + * A & B are already the same path.
2887 + *
2888 + * As a reminder, we can avoid strcmp here because all paths
2889 + * are interned in opt->priv->paths; see the comment above
2890 + * "paths" in struct merge_options_internal.
2891 + */
2892 + if (oldpath == newpath)
2893 + continue;
2894 +
2895 /*
2896 * If pair->one->path isn't in opt->priv->paths, that means
2897 * that either directory rename detection removed that
t/t6423-merge-rename-directories.sh
+66 -3
@@ -4731,7 +4731,7 @@ test_setup_12i () {
4731
4732 mkdir -p source/subdir &&
4733 echo foo >source/subdir/foo &&
4734 - echo bar >source/bar &&
4734 + printf "%d\n" 1 2 3 4 5 6 7 >source/bar &&
4735 echo baz >source/baz &&
4736 git add source &&
4737 git commit -m orig &&
@@ -4778,6 +4778,69 @@ test_expect_success '12i: Directory rename causes rename-to-self' '
4778 )
4779 '
4780
4781 +# Testcase 12i2, Identical to 12i except that source/subdir/bar modified on unrenamed side
4782 +# Commit O: source/{subdir/foo, bar, baz_1}
4783 +# Commit A: source/{foo, bar_2, baz_1}
4784 +# Commit B: source/{subdir/{foo, bar}, baz_2}
4785 +# Expected: source/{foo, bar, baz_2}, with conflicts on
4786 +# source/bar vs. source/subdir/bar
4787 +
4788 +test_setup_12i2 () {
4789 + git init 12i2 &&
4790 + (
4791 + cd 12i2 &&
4792 +
4793 + mkdir -p source/subdir &&
4794 + echo foo >source/subdir/foo &&
4795 + printf "%d\n" 1 2 3 4 5 6 7 >source/bar &&
4796 + echo baz >source/baz &&
4797 + git add source &&
4798 + git commit -m orig &&
4799 +
4800 + git branch O &&
4801 + git branch A &&
4802 + git branch B &&
4803 +
4804 + git switch A &&
4805 + git mv source/subdir/foo source/foo &&
4806 + echo 8 >> source/bar &&
4807 + git add source/bar &&
4808 + git commit -m A &&
4809 +
4810 + git switch B &&
4811 + git mv source/bar source/subdir/bar &&
4812 + echo more baz >>source/baz &&
4813 + git add source/baz &&
4814 + git commit -m B
4815 + )
4816 +}
4817 +
4818 +test_expect_success '12i2: Directory rename causes rename-to-self' '
4819 + test_setup_12i2 &&
4820 + (
4821 + cd 12i2 &&
4822 +
4823 + git checkout A^0 &&
4824 +
4825 + test_must_fail git -c merge.directoryRenames=conflict merge -s recursive B^0 &&
4826 +
4827 + test_path_is_missing source/subdir &&
4828 + test_path_is_file source/bar &&
4829 + test_path_is_file source/baz &&
4830 +
4831 + git ls-files >actual &&
4832 + uniq <actual >tracked &&
4833 + test_line_count = 3 tracked &&
4834 +
4835 + git status --porcelain -uno >actual &&
4836 + cat >expect <<-\EOF &&
4837 + UU source/bar
4838 + M source/baz
4839 + EOF
4840 + test_cmp expect actual
4841 + )
4842 +'
4843 +
4844 # Testcase 12j, Directory rename to root causes rename-to-self
4845 # Commit O: {subdir/foo, bar, baz_1}
4846 # Commit A: {foo, bar, baz_1}
@@ -5106,7 +5169,7 @@ test_setup_12n () {
5169 )
5170 }
5171
5109 -test_expect_failure '12n: Directory rename transitively makes rename back to self' '
5172 +test_expect_success '12n: Directory rename transitively makes rename back to self' '
5173 test_setup_12n &&
5174 (
5175 cd 12n &&
@@ -5166,7 +5229,7 @@ test_setup_12n2 () {
5229 )
5230 }
5231
5169 -test_expect_failure '12n2: Directory rename transitively makes rename back to self' '
5232 +test_expect_success '12n2: Directory rename transitively makes rename back to self' '
5233 test_setup_12n2 &&
5234 (
5235 cd 12n2 &&