From 050c0eb57ad664ca17b451b1f4e3b715a774bb4d Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Fri, 10 Sep 2021 12:16:26 +0300 Subject: [PATCH] refactor(commit)!: return Tree object instead of Oid for tree getter --- lib/src/commit.dart | 9 +++++++-- test/checkout_test.dart | 4 ++-- test/commit_test.dart | 8 ++++---- test/merge_test.dart | 12 ++++++------ test/reference_test.dart | 2 +- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/src/commit.dart b/lib/src/commit.dart index f870803..47ad4ca 100644 --- a/lib/src/commit.dart +++ b/lib/src/commit.dart @@ -1,6 +1,7 @@ import 'dart:ffi'; import 'bindings/libgit2_bindings.dart'; import 'bindings/commit.dart' as bindings; +import 'bindings/tree.dart' as tree_bindings; import 'repository.dart'; import 'oid.dart'; import 'signature.dart'; @@ -98,8 +99,12 @@ class Commit { return parents; } - /// Get the id of the tree pointed to by a commit. - Oid get tree => Oid(bindings.tree(_commitPointer)); + /// Get the tree pointed to by a commit. + Tree get tree { + final repo = bindings.owner(_commitPointer); + final oid = bindings.tree(_commitPointer); + return Tree(tree_bindings.lookup(repo, oid)); + } /// Releases memory allocated for commit object. void free() => bindings.free(_commitPointer); diff --git a/test/checkout_test.dart b/test/checkout_test.dart index 37a0553..02b6d3f 100644 --- a/test/checkout_test.dart +++ b/test/checkout_test.dart @@ -44,7 +44,7 @@ void main() { test('successfully checkouts tree', () { final masterHead = repo['821ed6e80627b8769d170a293862f9fc60825226'] as Commit; - final masterTree = repo[masterHead.tree.sha] as Tree; + final masterTree = masterHead.tree; expect( masterTree.entries.any((e) => e.name == 'another_feature_file'), false, @@ -53,7 +53,7 @@ void main() { repo.checkout(refName: 'refs/heads/feature'); final featureHead = repo['5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'] as Commit; - final featureTree = repo[featureHead.tree.sha] as Tree; + final featureTree = featureHead.tree; final repoHead = repo.head; expect(repoHead.target.sha, featureHead.id.sha); expect(repo.status, isEmpty); diff --git a/test/commit_test.dart b/test/commit_test.dart index a29bdc1..b91e769 100644 --- a/test/commit_test.dart +++ b/test/commit_test.dart @@ -74,7 +74,7 @@ void main() { expect(commit.author, author); expect(commit.committer, commiter); expect(commit.time, 124); - expect(commit.tree.sha, tree); + expect(commit.tree.id.sha, tree); expect(commit.parents.length, 1); expect(commit.parents[0].sha, mergeCommit); @@ -99,7 +99,7 @@ void main() { expect(commit.author, author); expect(commit.committer, commiter); expect(commit.time, 124); - expect(commit.tree.sha, tree); + expect(commit.tree.id.sha, tree); expect(commit.parents.length, 0); commit.free(); @@ -123,7 +123,7 @@ void main() { expect(commit.author, author); expect(commit.committer, commiter); expect(commit.time, 124); - expect(commit.tree.sha, tree); + expect(commit.tree.id.sha, tree); expect(commit.parents.length, 2); expect(commit.parents[0].sha, mergeCommit); expect(commit.parents[1].sha, 'fc38877b2552ab554752d9a77e1f48f738cca79b'); @@ -149,7 +149,7 @@ void main() { expect(commit.author, author); expect(commit.committer, commiter); expect(commit.time, 124); - expect(commit.tree.sha, tree); + expect(commit.tree.id.sha, tree); expect(commit.parents.length, 1); expect(commit.parents[0].sha, mergeCommit); diff --git a/test/merge_test.dart b/test/merge_test.dart index 4518164..611f9a5 100644 --- a/test/merge_test.dart +++ b/test/merge_test.dart @@ -183,9 +183,9 @@ void main() { final baseCommit = repo[repo.mergeBase(ourCommit.id.sha, theirCommit.id.sha).sha] as Commit; - final theirTree = repo[theirCommit.tree.sha] as Tree; - final ourTree = repo[ourCommit.tree.sha] as Tree; - final ancestorTree = repo[baseCommit.tree.sha] as Tree; + final theirTree = theirCommit.tree; + final ourTree = ourCommit.tree; + final ancestorTree = baseCommit.tree; final mergeIndex = repo.mergeTrees( ancestorTree: ancestorTree, @@ -221,9 +221,9 @@ void main() { final baseCommit = repo[repo.mergeBase(ourCommit.id.sha, theirCommit.id.sha).sha] as Commit; - final theirTree = repo[theirCommit.tree.sha] as Tree; - final ourTree = repo[ourCommit.tree.sha] as Tree; - final ancestorTree = repo[baseCommit.tree.sha] as Tree; + final theirTree = theirCommit.tree; + final ourTree = ourCommit.tree; + final ancestorTree = baseCommit.tree; final mergeIndex = repo.mergeTrees( ancestorTree: ancestorTree, diff --git a/test/reference_test.dart b/test/reference_test.dart index eb8f488..a03b7a0 100644 --- a/test/reference_test.dart +++ b/test/reference_test.dart @@ -502,7 +502,7 @@ void main() { test('successfully peels to object of provided type', () { final ref = repo.references['refs/heads/master']; final commit = repo[ref.target.sha] as Commit; - final tree = repo[commit.tree.sha] as Tree; + final tree = commit.tree; final peeledCommit = ref.peel(GitObject.commit) as Commit; final peeledTree = ref.peel(GitObject.tree) as Tree;