Skip to content

Commit fa5838c

Browse files
newrengitster
authored andcommitted
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 d3b88be (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, but which doesn't change how it operates. Make this change to otherwise minimize the changes between this testcase and a new one that we want to add. Then duplicate the testcase as 12i2, changing it so that it adds a single line to the file in question when it is renamed, as a testcase for this bug. Further, commit 98a1a00 (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. Fix to merge-ort to prevent multiple 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) Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8c6fe0b commit fa5838c

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

merge-ort.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,17 @@ static int process_renames(struct merge_options *opt,
28732873
newinfo = new_ent->value;
28742874
}
28752875

2876+
/*
2877+
* Directory renames can result in rename-to-self, which we
2878+
* want to skip so we don't mark oldpath for deletion.
2879+
*
2880+
* Note that we can avoid strcmp here because of prior
2881+
* diligence in apply_directory_rename_modifications() to
2882+
* ensure we reused existing paths from opt->priv->paths.
2883+
*/
2884+
if (oldpath == newpath)
2885+
continue;
2886+
28762887
/*
28772888
* If pair->one->path isn't in opt->priv->paths, that means
28782889
* that either directory rename detection removed that

t/t6423-merge-rename-directories.sh

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4731,7 +4731,7 @@ test_setup_12i () {
47314731

47324732
mkdir -p source/subdir &&
47334733
echo foo >source/subdir/foo &&
4734-
echo bar >source/bar &&
4734+
printf "%d\n" 1 2 3 4 5 6 7 >source/bar &&
47354735
echo baz >source/baz &&
47364736
git add source &&
47374737
git commit -m orig &&
@@ -4778,6 +4778,69 @@ test_expect_success '12i: Directory rename causes rename-to-self' '
47784778
)
47794779
'
47804780

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+
47814844
# Testcase 12j, Directory rename to root causes rename-to-self
47824845
# Commit O: {subdir/foo, bar, baz_1}
47834846
# Commit A: {foo, bar, baz_1}
@@ -5106,7 +5169,7 @@ test_setup_12n () {
51065169
)
51075170
}
51085171

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' '
51105173
test_setup_12n &&
51115174
(
51125175
cd 12n &&
@@ -5166,7 +5229,7 @@ test_setup_12n2 () {
51665229
)
51675230
}
51685231

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' '
51705233
test_setup_12n2 &&
51715234
(
51725235
cd 12n2 &&

0 commit comments

Comments
 (0)