feat(commit): add ability to revert commit

This commit is contained in:
Aleksey Kulikov 2021-09-16 20:04:40 +03:00
parent cd9f38c2bd
commit b83fea9360
3 changed files with 72 additions and 0 deletions

View file

@ -191,6 +191,45 @@ Pointer<git_oid> tree(Pointer<git_commit> commit) {
return libgit2.git_commit_tree_id(commit);
}
/// Reverts the given commit against the given "our" commit, producing an index that
/// reflects the result of the revert.
///
/// The returned index must be freed explicitly with `free()`.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_index> revertCommit(
Pointer<git_repository> repo,
Pointer<git_commit> revertCommit,
Pointer<git_commit> ourCommit,
int mainline,
) {
final out = calloc<Pointer<git_index>>();
final opts = calloc<git_merge_options>();
final optsError =
libgit2.git_merge_options_init(opts, GIT_MERGE_OPTIONS_VERSION);
if (optsError < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
final error = libgit2.git_revert_commit(
out,
repo,
revertCommit,
ourCommit,
mainline,
opts,
);
calloc.free(opts);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Get the repository that contains the commit.
Pointer<git_repository> owner(Pointer<git_commit> commit) =>
libgit2.git_commit_owner(commit);

View file

@ -619,6 +619,24 @@ class Repository {
return Index(result);
}
/// Reverts the given commit against the given "our" commit, producing an index that
/// reflects the result of the revert.
///
/// [mainline] is parent of the [revertCommit] if it is a merge (i.e. 1, 2).
///
/// The returned index must be freed explicitly with `free()`.
///
/// Throws a [LibGit2Error] if error occured.
Index revertCommit(
{required Commit revertCommit, required Commit ourCommit, mainline = 0}) {
return Index(commit_bindings.revertCommit(
_repoPointer,
revertCommit.pointer,
ourCommit.pointer,
mainline,
));
}
/// Merges two trees, producing an index that reflects the result of the merge.
/// The index may be written as-is to the working directory or checked out. If the index
/// is to be converted to a tree, the caller should resolve any conflicts that arose as part