diff options
| author | Elijah Newren <newren@gmail.com> | 2022-07-23 01:53:13 +0000 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2022-07-22 21:45:23 -0700 |
| commit | e4cdfe84a0d2ac560bf0a2ab0e9beade5f71a844 (patch) | |
| tree | eb84466dc1d5c3e19d79bb82590de82dda4d0b0c /builtin/merge.c | |
| parent | 24ba8b70c9965ea40fc1192cf17cd09394cb8350 (diff) | |
merge: abort if index does not match HEAD for trivial merges
As noted in the last commit and the links therein (especially commit
9822175d2b ("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.
The "trivial merge" logic in builtin/merge.c is yet another such case
we previously missed. Add a check for it to ensure it aborts if the
index does not match HEAD, and add a testcase where this fix is needed.
Note that the fix here would also incidentally be an alternative fix
for the testcase added in the last patch, but the fix in the last patch
is still needed when multiple merge strategies are in use, so tweak the
testcase from the previous commit so that it continues to exercise the
codepath added in the last commit.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/merge.c')
| -rw-r--r-- | builtin/merge.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/builtin/merge.c b/builtin/merge.c index 23170f2d2a..b43876f68e 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -1599,6 +1599,21 @@ int cmd_merge(int argc, const char **argv, const char *prefix) */ refresh_cache(REFRESH_QUIET); if (allow_trivial && fast_forward != FF_ONLY) { + /* + * Must first ensure that index matches HEAD before + * attempting a trivial merge. + */ + struct tree *head_tree = get_commit_tree(head_commit); + struct strbuf sb = STRBUF_INIT; + + if (repo_index_has_changes(the_repository, head_tree, + &sb)) { + error(_("Your local changes to the following files would be overwritten by merge:\n %s"), + sb.buf); + strbuf_release(&sb); + return 2; + } + /* See if it is really trivial. */ git_committer_info(IDENT_STRICT); printf(_("Trying really trivial in-index merge...\n")); |
