From eb321f052f0435547a2f0cca24f70215126a2819 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Wed, 26 Jan 2022 11:16:17 +0300 Subject: [PATCH] refactor(repository)!: move `descendantOf()` method into Commit --- lib/src/bindings/graph.dart | 9 +-------- lib/src/commit.dart | 13 +++++++++++++ lib/src/repository.dart | 15 --------------- test/repository_test.dart | 31 +++++++------------------------ 4 files changed, 21 insertions(+), 47 deletions(-) diff --git a/lib/src/bindings/graph.dart b/lib/src/bindings/graph.dart index 231233b..62fd4e2 100644 --- a/lib/src/bindings/graph.dart +++ b/lib/src/bindings/graph.dart @@ -2,15 +2,12 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart'; import 'package:libgit2dart/src/bindings/libgit2_bindings.dart'; -import 'package:libgit2dart/src/error.dart'; import 'package:libgit2dart/src/util.dart'; /// Determine if a commit is the descendant of another commit. /// /// Note that a commit is not considered a descendant of itself, in contrast to /// `git merge-base --is-ancestor`. -/// -/// Throws a [LibGit2Error] if error occured. bool descendantOf({ required Pointer repoPointer, required Pointer commitPointer, @@ -22,11 +19,7 @@ bool descendantOf({ ancestorPointer, ); - if (result < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return result == 1 || false; - } + return result == 1 || false; } /// Count the number of unique commits between two commit objects. diff --git a/lib/src/commit.dart b/lib/src/commit.dart index 445adb2..6d8c487 100644 --- a/lib/src/commit.dart +++ b/lib/src/commit.dart @@ -2,6 +2,7 @@ import 'dart:ffi'; import 'package:libgit2dart/libgit2dart.dart'; import 'package:libgit2dart/src/bindings/commit.dart' as bindings; +import 'package:libgit2dart/src/bindings/graph.dart' as graph_bindings; import 'package:libgit2dart/src/bindings/libgit2_bindings.dart'; class Commit { @@ -309,6 +310,18 @@ class Commit { return Commit(bindings.nthGenAncestor(commitPointer: _commitPointer, n: n)); } + /// Checks if commit is the descendant of another [ancestor] commit. + /// + /// Note that a commit is not considered a descendant of itself, in contrast + /// to `git merge-base --is-ancestor`. + bool descendantOf(Commit ancestor) { + return graph_bindings.descendantOf( + repoPointer: bindings.owner(_commitPointer), + commitPointer: bindings.id(_commitPointer), + ancestorPointer: bindings.id(ancestor.pointer), + ); + } + /// Creates an in-memory copy of a commit. /// /// **IMPORTANT**: Should be freed to release allocated memory. diff --git a/lib/src/repository.dart b/lib/src/repository.dart index d82799e..0a47078 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -617,21 +617,6 @@ class Repository { ); } - /// Checks if a provided [commit] is the descendant of another [ancestor] - /// commit. - /// - /// Note that a commit is not considered a descendant of itself, in contrast - /// to `git merge-base --is-ancestor`. - /// - /// Throws a [LibGit2Error] if error occured. - bool descendantOf({required Oid commit, required Oid ancestor}) { - return graph_bindings.descendantOf( - repoPointer: _repoPointer, - commitPointer: commit.pointer, - ancestorPointer: ancestor.pointer, - ); - } - /// Returns list with the `ahead` and `behind` number of unique commits /// respectively. /// diff --git a/test/repository_test.dart b/test/repository_test.dart index 30a9bf5..abb0485 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -250,32 +250,15 @@ void main() { }); test('checks if commit is a descendant of another commit', () { - final commit1 = repo['821ed6e8']; - final commit2 = repo['78b8bf12']; + final commit1 = Commit.lookup(repo: repo, oid: repo['821ed6e8']); + final commit2 = Commit.lookup(repo: repo, oid: repo['78b8bf12']); - expect( - repo.descendantOf(commit: commit1, ancestor: commit2), - true, - ); - expect( - repo.descendantOf(commit: commit1, ancestor: commit1), - false, - ); - expect( - repo.descendantOf(commit: commit2, ancestor: commit1), - false, - ); - }); + expect(commit1.descendantOf(commit2), true); + expect(commit1.descendantOf(commit1), false); + expect(commit2.descendantOf(commit1), false); - test('throws when trying to check if commit is descendant and error occurs', - () { - final commit1 = repo['821ed6e8']; - final commit2 = repo['78b8bf12']; - final nullRepo = Repository(nullptr); - expect( - () => nullRepo.descendantOf(commit: commit1, ancestor: commit2), - throwsA(isA()), - ); + commit1.free(); + commit2.free(); }); test('returns number of ahead behind commits', () {