From 74a20a9cf2eea0c07b7d7e232d2c9e3246429c7a Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Thu, 23 Dec 2021 11:40:29 +0300 Subject: [PATCH] feat(commit): add binding and API method for git_revert (#30) --- lib/src/bindings/commit.dart | 15 +++++++++++++++ lib/src/repository.dart | 11 +++++++++++ test/commit_test.dart | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/lib/src/bindings/commit.dart b/lib/src/bindings/commit.dart index 1267c96..7a2cb69 100644 --- a/lib/src/bindings/commit.dart +++ b/lib/src/bindings/commit.dart @@ -361,6 +361,21 @@ Pointer tree(Pointer commit) { } } +/// Reverts the given commit, producing changes in the index and working +/// directory. +/// +/// Throws a [LibGit2Error] if error occured. +void revert({ + required Pointer repoPointer, + required Pointer 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. /// diff --git a/lib/src/repository.dart b/lib/src/repository.dart index 736dce0..29a78ab 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -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. /// diff --git a/test/commit_test.dart b/test/commit_test.dart index 4543498..8888cb9 100644 --- a/test/commit_test.dart +++ b/test/commit_test.dart @@ -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())); + }); + + 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();