Skip to content

Commit

Permalink
#5662: Implement git reset --hard when aborting a merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 22, 2021
1 parent dbe535e commit eea0c0b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
24 changes: 24 additions & 0 deletions plugins/vcs/Repository.cpp
Expand Up @@ -405,6 +405,30 @@ bool Repository::mergeIsInProgress()
return state == GIT_REPOSITORY_STATE_MERGE;
}

void Repository::abortMerge()
{
if (!mergeIsInProgress())
{
return;
}

auto head = getHead();

git_oid targetOid;
auto error = git_reference_name_to_id(&targetOid, _repository, head->getName().c_str());
GitException::ThrowOnError(error);

git_object* target;
error = git_object_lookup(&target, _repository, &targetOid, GIT_OBJECT_COMMIT);
GitException::ThrowOnError(error);

git_checkout_options checkoutOptions = GIT_CHECKOUT_OPTIONS_INIT;
checkoutOptions.checkout_strategy = GIT_CHECKOUT_FORCE;

error = git_reset(_repository, target, GIT_RESET_HARD, &checkoutOptions);
GitException::ThrowOnError(error);
}

Commit::Ptr Repository::findMergeBase(const Reference& first, const Reference& second)
{
git_oid firstOid;
Expand Down
2 changes: 2 additions & 0 deletions plugins/vcs/Repository.h
Expand Up @@ -71,6 +71,8 @@ class Repository final
bool isReadyForMerge();
bool mergeIsInProgress();

void abortMerge();

Index::Ptr getIndex();

// Finds a common ancestor of the two refs, to base a merge operation on
Expand Down
14 changes: 12 additions & 2 deletions plugins/vcs/ui/VcsStatus.cpp
Expand Up @@ -198,10 +198,20 @@ void VcsStatus::onMapEvent(IMap::MapEvent ev)
// Ask the user whether to cancel the git merge status
if (wxutil::Messagebox::Show(_("Cancel Merge Operation?"),
_("You've aborted the map merge. Do you want to abort the ongoing git merge operation too?\n"
"This will reset the repository to the state it had before the merge was started."),
"This will perform a hard reset in the repository to the state it had before the merge was started.\n\n"
"Important: All uncommitted changes in the working tree will be lost!"),
::ui::IDialog::MessageType::MESSAGE_ASK) == ::ui::IDialog::RESULT_YES)
{
// TODO
try
{
_repository->abortMerge();
}
catch (git::GitException& ex)
{
wxutil::Messagebox::ShowError(ex.what());
}

analyseRemoteStatus(_repository);
}
}
else if (ev == IMap::MapMergeOperationFinished && _repository && _repository->mergeIsInProgress())
Expand Down

0 comments on commit eea0c0b

Please sign in to comment.