Skip to content

Commit 65170c0

Browse files
newrengitster
authored andcommitted
merge-recursive: avoid incorporating uncommitted changes in a merge
builtin/merge.c contains this important requirement for merge strategies: /* * At this point, we need a real merge. No matter what strategy * we use, it would operate on the index, possibly affecting the * working tree, and when resolved cleanly, have the desired * tree in the index -- this means that the index must be in * sync with the head commit. The strategies are responsible * to ensure this. */ merge-recursive does not do this check directly, instead it relies on unpack_trees() to do it. However, merge_trees() has a special check for the merge branch exactly matching the merge base; when it detects that situation, it returns early without calling unpack_trees(), because it knows that the HEAD commit already has the correct result. Unfortunately, it didn't check that the index matched HEAD, so after it returned, the outer logic ended up creating a merge commit that included something other than HEAD. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b101793 commit 65170c0

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

merge-recursive.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,13 @@ int merge_trees(struct merge_options *o,
19521952
}
19531953

19541954
if (oid_eq(&common->object.oid, &merge->object.oid)) {
1955+
struct strbuf sb = STRBUF_INIT;
1956+
1957+
if (index_has_changes(&sb)) {
1958+
err(o, _("Dirty index: cannot merge (dirty: %s)"),
1959+
sb.buf);
1960+
return 0;
1961+
}
19551962
output(o, 0, _("Already up to date!"));
19561963
*result = head;
19571964
return 1;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ test_expect_success 'recursive' '
112112
test_must_fail git merge -s recursive C^0
113113
'
114114

115-
test_expect_failure 'recursive, when merge branch matches merge base' '
115+
test_expect_success 'recursive, when merge branch matches merge base' '
116116
git reset --hard &&
117117
git checkout B^0 &&
118118

0 commit comments

Comments
 (0)