fix(tag): tag can point to any type of git object

This commit is contained in:
Aleksey Kulikov 2021-09-04 13:52:29 +03:00
parent fb69de66d2
commit 56713da648
4 changed files with 25 additions and 5 deletions

View file

@ -34,6 +34,9 @@ Pointer<git_object> target(Pointer<git_tag> tag) {
} }
} }
/// Get the type of a tag's tagged object.
int targetType(Pointer<git_tag> tag) => libgit2.git_tag_target_type(tag);
/// Get the id of a tag. /// Get the id of a tag.
Pointer<git_oid> id(Pointer<git_tag> tag) => libgit2.git_tag_id(tag); Pointer<git_oid> id(Pointer<git_tag> tag) => libgit2.git_tag_id(tag);

View file

@ -1,5 +1,7 @@
import 'dart:ffi'; import 'dart:ffi';
import 'package:libgit2dart/libgit2dart.dart';
import 'bindings/libgit2_bindings.dart'; import 'bindings/libgit2_bindings.dart';
import 'bindings/tag.dart' as bindings; import 'bindings/tag.dart' as bindings;
import 'bindings/object.dart' as object_bindings; import 'bindings/object.dart' as object_bindings;
@ -73,12 +75,27 @@ class Tag {
return Oid(result); return Oid(result);
} }
/// Get the tagged object of a tag. /// Get the tagged object (commit, tree, blob, tag) of a tag.
/// ///
/// This method performs a repository lookup for the given object and returns it. /// This method performs a repository lookup for the given object and returns it.
/// ///
/// Returned object should be explicitly downcasted to one of four of git object types.
///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Commit get target => Commit(bindings.target(_tagPointer).cast()); Object get target {
final type = bindings.targetType(_tagPointer);
final object = bindings.target(_tagPointer);
if (type == GitObject.commit.value) {
return Commit(object.cast());
} else if (type == GitObject.tree.value) {
return Tree(object.cast());
} else if (type == GitObject.blob.value) {
return Blob(object.cast());
} else {
return Tag(object.cast());
}
}
/// Get the id of a tag. /// Get the id of a tag.
Oid get id => Oid(bindings.id(_tagPointer)); Oid get id => Oid(bindings.id(_tagPointer));

View file

@ -325,7 +325,7 @@ void main() {
final newTag = repo[oid.sha] as Tag; final newTag = repo[oid.sha] as Tag;
final tagger = newTag.tagger; final tagger = newTag.tagger;
final newTagTarget = newTag.target; final newTagTarget = newTag.target as Commit;
expect(newTag.id.sha, '131a5eb6b7a880b5096c550ee7351aeae7b95a42'); expect(newTag.id.sha, '131a5eb6b7a880b5096c550ee7351aeae7b95a42');
expect(newTag.name, tagName); expect(newTag.name, tagName);

View file

@ -40,7 +40,7 @@ void main() {
time: 1630599723, time: 1630599723,
offset: 180, offset: 180,
); );
final target = tag.target; final target = tag.target as Commit;
final tagger = tag.tagger; final tagger = tag.tagger;
expect(tag.id.sha, tagSHA); expect(tag.id.sha, tagSHA);
@ -74,7 +74,7 @@ void main() {
final newTag = Tag.lookup(repo, oid.sha); final newTag = Tag.lookup(repo, oid.sha);
final tagger = newTag.tagger; final tagger = newTag.tagger;
final newTagTarget = newTag.target; final newTagTarget = newTag.target as Commit;
expect(newTag.id.sha, '131a5eb6b7a880b5096c550ee7351aeae7b95a42'); expect(newTag.id.sha, '131a5eb6b7a880b5096c550ee7351aeae7b95a42');
expect(newTag.name, tagName); expect(newTag.name, tagName);