Skip to content

Commit 24ba8b7

Browse files
newrengitster
authored andcommitted
merge-resolve: abort if index does not match HEAD
As noted in commit 9822175 ("Ensure index matches head before invoking merge machinery, round N", 2019-08-17), we have had a very long history of problems with failing to enforce the requirement that index matches HEAD when starting a merge. One of the commits referenced in the long tale of issues arising from lax enforcement of this requirement was commit 55f39cf ("merge: fix misleading pre-merge check documentation", 2018-06-30), which tried to document the requirement and noted there were some exceptions. As mentioned in that commit message, the `resolve` strategy was the one strategy that did not have an explicit index matching HEAD check, and the reason it didn't was that I wasn't able to discover any cases where the implementation would fail to catch the problem and abort, and didn't want to introduce unnecessary performance overhead of adding another check. Well, today I discovered a testcase where the implementation does not catch the problem and so an explicit check is needed. Add a testcase that previously would have failed, and update git-merge-resolve.sh to have an explicit check. Note that the code is copied from 3ec62ad ("merge-octopus: abort if index does not match HEAD", 2016-04-09), so that we reuse the same message and avoid making translators need to translate some new message. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 11f4290 commit 24ba8b7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

git-merge-resolve.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
#
66
# Resolve two trees, using enhanced multi-base read-tree.
77

8+
. git-sh-setup
9+
10+
# Abort if index does not match HEAD
11+
if ! git diff-index --quiet --cached HEAD --
12+
then
13+
gettextln "Error: Your local changes to the following files would be overwritten by merge"
14+
git diff-index --cached --name-only HEAD -- | sed -e 's/^/ /'
15+
exit 2
16+
fi
17+
818
# The first parameters up to -- are merge bases; the rest are heads.
919
bases= head= remotes= sep_seen=
1020
for arg

t/t6424-merge-unrelated-index-changes.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ test_expect_success 'resolve, non-trivial' '
114114
test_path_is_missing .git/MERGE_HEAD
115115
'
116116

117+
test_expect_success 'resolve, non-trivial, related file removed' '
118+
git reset --hard &&
119+
git checkout B^0 &&
120+
121+
git rm a &&
122+
test_path_is_missing a &&
123+
124+
test_must_fail git merge -s resolve D^0 &&
125+
126+
test_path_is_missing a &&
127+
test_path_is_missing .git/MERGE_HEAD
128+
'
129+
117130
test_expect_success 'recursive' '
118131
git reset --hard &&
119132
git checkout B^0 &&

0 commit comments

Comments
 (0)