feat(commit): add binding and API method for git_revert (#30)

This commit is contained in:
Aleksey Kulikov 2021-12-23 11:40:29 +03:00 committed by GitHub
parent fda5173e7f
commit 74a20a9cf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 0 deletions

View file

@ -361,6 +361,21 @@ Pointer<git_tree> tree(Pointer<git_commit> commit) {
}
}
/// Reverts the given commit, producing changes in the index and working
/// directory.
///
/// Throws a [LibGit2Error] if error occured.
void revert({
required Pointer<git_repository> repoPointer,
required Pointer<git_commit> commitPointer,
}) {
final error = libgit2.git_revert(repoPointer, commitPointer, nullptr);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Reverts the given commit against the given "our" commit, producing an index
/// that reflects the result of the revert.
///

View file

@ -665,6 +665,17 @@ class Repository {
);
}
/// Reverts the given [commit], producing changes in the index and working
/// directory.
///
/// Throws a [LibGit2Error] if error occured.
void revert(Commit commit) {
commit_bindings.revert(
repoPointer: _repoPointer,
commitPointer: commit.pointer,
);
}
/// Finds a single object and intermediate reference (if there is one) by a
/// [spec] revision string.
///

View file

@ -64,6 +64,26 @@ void main() {
});
test('successfully reverts commit', () {
final commit = repo.lookupCommit(
repo['821ed6e80627b8769d170a293862f9fc60825226'],
);
final index = repo.index;
expect(index.find('dir/dir_file.txt'), true);
expect(File('${repo.workdir}dir/dir_file.txt').existsSync(), true);
repo.revert(commit);
expect(index.find('dir/dir_file.txt'), false);
expect(File('${repo.workdir}dir/dir_file.txt').existsSync(), false);
index.free();
commit.free();
});
test('throws when trying to revert and error occurs', () {
expect(() => repo.revert(Commit(nullptr)), throwsA(isA<LibGit2Error>()));
});
test('successfully reverts commit to provided commit', () {
final to = repo.lookupCommit(
repo['78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8'],
);
@ -72,9 +92,11 @@ void main() {
);
final index = repo.index;
expect(index.find('dir/dir_file.txt'), true);
expect(File('${repo.workdir}dir/dir_file.txt').existsSync(), true);
final revertIndex = repo.revertCommit(revertCommit: from, ourCommit: to);
expect(revertIndex.find('dir/dir_file.txt'), false);
expect(File('${repo.workdir}dir/dir_file.txt').existsSync(), true);
revertIndex.free();
index.free();