refactor(commit)!: return Tree object instead of Oid for tree getter

This commit is contained in:
Aleksey Kulikov 2021-09-10 12:16:26 +03:00
parent 458c0bdc71
commit 050c0eb57a
5 changed files with 20 additions and 15 deletions

View file

@ -1,6 +1,7 @@
import 'dart:ffi'; import 'dart:ffi';
import 'bindings/libgit2_bindings.dart'; import 'bindings/libgit2_bindings.dart';
import 'bindings/commit.dart' as bindings; import 'bindings/commit.dart' as bindings;
import 'bindings/tree.dart' as tree_bindings;
import 'repository.dart'; import 'repository.dart';
import 'oid.dart'; import 'oid.dart';
import 'signature.dart'; import 'signature.dart';
@ -98,8 +99,12 @@ class Commit {
return parents; return parents;
} }
/// Get the id of the tree pointed to by a commit. /// Get the tree pointed to by a commit.
Oid get tree => Oid(bindings.tree(_commitPointer)); 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. /// Releases memory allocated for commit object.
void free() => bindings.free(_commitPointer); void free() => bindings.free(_commitPointer);

View file

@ -44,7 +44,7 @@ void main() {
test('successfully checkouts tree', () { test('successfully checkouts tree', () {
final masterHead = final masterHead =
repo['821ed6e80627b8769d170a293862f9fc60825226'] as Commit; repo['821ed6e80627b8769d170a293862f9fc60825226'] as Commit;
final masterTree = repo[masterHead.tree.sha] as Tree; final masterTree = masterHead.tree;
expect( expect(
masterTree.entries.any((e) => e.name == 'another_feature_file'), masterTree.entries.any((e) => e.name == 'another_feature_file'),
false, false,
@ -53,7 +53,7 @@ void main() {
repo.checkout(refName: 'refs/heads/feature'); repo.checkout(refName: 'refs/heads/feature');
final featureHead = final featureHead =
repo['5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'] as Commit; repo['5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'] as Commit;
final featureTree = repo[featureHead.tree.sha] as Tree; final featureTree = featureHead.tree;
final repoHead = repo.head; final repoHead = repo.head;
expect(repoHead.target.sha, featureHead.id.sha); expect(repoHead.target.sha, featureHead.id.sha);
expect(repo.status, isEmpty); expect(repo.status, isEmpty);

View file

@ -74,7 +74,7 @@ void main() {
expect(commit.author, author); expect(commit.author, author);
expect(commit.committer, commiter); expect(commit.committer, commiter);
expect(commit.time, 124); expect(commit.time, 124);
expect(commit.tree.sha, tree); expect(commit.tree.id.sha, tree);
expect(commit.parents.length, 1); expect(commit.parents.length, 1);
expect(commit.parents[0].sha, mergeCommit); expect(commit.parents[0].sha, mergeCommit);
@ -99,7 +99,7 @@ void main() {
expect(commit.author, author); expect(commit.author, author);
expect(commit.committer, commiter); expect(commit.committer, commiter);
expect(commit.time, 124); expect(commit.time, 124);
expect(commit.tree.sha, tree); expect(commit.tree.id.sha, tree);
expect(commit.parents.length, 0); expect(commit.parents.length, 0);
commit.free(); commit.free();
@ -123,7 +123,7 @@ void main() {
expect(commit.author, author); expect(commit.author, author);
expect(commit.committer, commiter); expect(commit.committer, commiter);
expect(commit.time, 124); expect(commit.time, 124);
expect(commit.tree.sha, tree); expect(commit.tree.id.sha, tree);
expect(commit.parents.length, 2); expect(commit.parents.length, 2);
expect(commit.parents[0].sha, mergeCommit); expect(commit.parents[0].sha, mergeCommit);
expect(commit.parents[1].sha, 'fc38877b2552ab554752d9a77e1f48f738cca79b'); expect(commit.parents[1].sha, 'fc38877b2552ab554752d9a77e1f48f738cca79b');
@ -149,7 +149,7 @@ void main() {
expect(commit.author, author); expect(commit.author, author);
expect(commit.committer, commiter); expect(commit.committer, commiter);
expect(commit.time, 124); expect(commit.time, 124);
expect(commit.tree.sha, tree); expect(commit.tree.id.sha, tree);
expect(commit.parents.length, 1); expect(commit.parents.length, 1);
expect(commit.parents[0].sha, mergeCommit); expect(commit.parents[0].sha, mergeCommit);

View file

@ -183,9 +183,9 @@ void main() {
final baseCommit = final baseCommit =
repo[repo.mergeBase(ourCommit.id.sha, theirCommit.id.sha).sha] repo[repo.mergeBase(ourCommit.id.sha, theirCommit.id.sha).sha]
as Commit; as Commit;
final theirTree = repo[theirCommit.tree.sha] as Tree; final theirTree = theirCommit.tree;
final ourTree = repo[ourCommit.tree.sha] as Tree; final ourTree = ourCommit.tree;
final ancestorTree = repo[baseCommit.tree.sha] as Tree; final ancestorTree = baseCommit.tree;
final mergeIndex = repo.mergeTrees( final mergeIndex = repo.mergeTrees(
ancestorTree: ancestorTree, ancestorTree: ancestorTree,
@ -221,9 +221,9 @@ void main() {
final baseCommit = final baseCommit =
repo[repo.mergeBase(ourCommit.id.sha, theirCommit.id.sha).sha] repo[repo.mergeBase(ourCommit.id.sha, theirCommit.id.sha).sha]
as Commit; as Commit;
final theirTree = repo[theirCommit.tree.sha] as Tree; final theirTree = theirCommit.tree;
final ourTree = repo[ourCommit.tree.sha] as Tree; final ourTree = ourCommit.tree;
final ancestorTree = repo[baseCommit.tree.sha] as Tree; final ancestorTree = baseCommit.tree;
final mergeIndex = repo.mergeTrees( final mergeIndex = repo.mergeTrees(
ancestorTree: ancestorTree, ancestorTree: ancestorTree,

View file

@ -502,7 +502,7 @@ void main() {
test('successfully peels to object of provided type', () { test('successfully peels to object of provided type', () {
final ref = repo.references['refs/heads/master']; final ref = repo.references['refs/heads/master'];
final commit = repo[ref.target.sha] as Commit; 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 peeledCommit = ref.peel(GitObject.commit) as Commit;
final peeledTree = ref.peel(GitObject.tree) as Tree; final peeledTree = ref.peel(GitObject.tree) as Tree;