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); 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. /// Get the repository that contains the commit.
Pointer<git_repository> owner(Pointer<git_commit> commit) => Pointer<git_repository> owner(Pointer<git_commit> commit) =>
libgit2.git_commit_owner(commit); libgit2.git_commit_owner(commit);

View file

@ -619,6 +619,24 @@ class Repository {
return Index(result); 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. /// 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 /// 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 /// is to be converted to a tree, the caller should resolve any conflicts that arose as part

View file

@ -56,6 +56,21 @@ void main() {
commit.free(); commit.free();
}); });
test('successfully reverts commit', () {
final to = repo['78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8'] as Commit;
final from = repo['821ed6e80627b8769d170a293862f9fc60825226'] as Commit;
final index = repo.index;
expect(index.contains('dir/dir_file.txt'), true);
final revertIndex = repo.revertCommit(revertCommit: from, ourCommit: to);
expect(revertIndex.contains('dir/dir_file.txt'), false);
revertIndex.free();
index.free();
to.free();
from.free();
});
test('successfully creates commit', () { test('successfully creates commit', () {
final oid = Commit.create( final oid = Commit.create(
repo: repo, repo: repo,