Git fork

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>

authored by

Elijah Newren and committed by
Junio C Hamano
885ffe53 d3de9786

+80 -3
+14
merge-ort.c
··· 2879 2879 } 2880 2880 2881 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 + /* 2882 2896 * If pair->one->path isn't in opt->priv->paths, that means 2883 2897 * that either directory rename detection removed that 2884 2898 * path, or a parent directory of oldpath was resolved and
+66 -3
t/t6423-merge-rename-directories.sh
··· 4731 4731 4732 4732 mkdir -p source/subdir && 4733 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 4735 echo baz >source/baz && 4736 4736 git add source && 4737 4737 git commit -m orig && ··· 4756 4756 test_setup_12i && 4757 4757 ( 4758 4758 cd 12i && 4759 + 4760 + git checkout A^0 && 4761 + 4762 + test_must_fail git -c merge.directoryRenames=conflict merge -s recursive B^0 && 4763 + 4764 + test_path_is_missing source/subdir && 4765 + test_path_is_file source/bar && 4766 + test_path_is_file source/baz && 4767 + 4768 + git ls-files >actual && 4769 + uniq <actual >tracked && 4770 + test_line_count = 3 tracked && 4771 + 4772 + git status --porcelain -uno >actual && 4773 + cat >expect <<-\EOF && 4774 + UU source/bar 4775 + M source/baz 4776 + EOF 4777 + test_cmp expect actual 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 && 4759 4822 4760 4823 git checkout A^0 && 4761 4824 ··· 5106 5169 ) 5107 5170 } 5108 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' ' 5110 5173 test_setup_12n && 5111 5174 ( 5112 5175 cd 12n && ··· 5166 5229 ) 5167 5230 } 5168 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' ' 5170 5233 test_setup_12n2 && 5171 5234 ( 5172 5235 cd 12n2 &&