sitereactor
sitereactor

Reputation: 400

Abort git merge using libgit2sharp

In libgit2sharp I'm doing a fetch and merge for an upstream branch, and I want to be able to abort the merge similar to:

git merge --abort

when the MergeStatus is:

LibGit2Sharp.MergeStatus.Conflicts

Is this currently possible (using v.0.20.1)? If not, does anybody know what it would require to implement something like that?

Upvotes: 4

Views: 953

Answers (2)

Jason Haslam
Jason Haslam

Reputation: 2897

I've tried to emulate reset --merge by pre-filtering the paths passed in to reset --hard. I think that it has the same basic semantics as reset --merge. I'm doing something like this pseudocode:

// Gather list of files modified in the index.
git_diff *index_diff;
git_diff_tree_to_index(&index_diff, repo, current_head_tree, ...);
// Iterate over index_diff to produce list of changed files.

// Gather list of files modified between index and workdir.
git_diff *workdir_diff;
git_diff_index_to_workdir(&workdir_diff, ...);
// Iterate over workdir_diff looking for files in the index list.
// Any non-conflicted files that are already in the index list
// cause the whole process to be aborted with an error. Can't reset
// merged files that have unstaged changes.

// The files in the index list are the ones that need to be reset.
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy |= GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
opts.paths = index_list;
git_reset(repo, current_head_commit, GIT_RESET_HARD, opts);

Of course, that's probably not how it should be implemented in the library. There should probably be a checkout strategy that skips files that are modified in the workdir but not staged in the index. Then that could be combined with GIT_CHECKOUT_USE_OURS to throw away conflicted files.

Upvotes: 0

Edward Thomson
Edward Thomson

Reputation: 78673

The git-merge --abort command is shorthand for git reset --merge, and both a git-reset --merge and a git-reset --hard will abort the merge and clean up the repository, differing in how they update modified files.

LibGit2Sharp does not currently offer a reset parallel to the merge option. However if you use the Repository.Reset method with the ResetMode.Hard option your merge will be aborted (though with git-reset --hard semantics).

Upvotes: 8

Related Questions