feat(tag): add more bindings and api methods

This commit is contained in:
Aleksey Kulikov 2021-10-06 15:36:17 +03:00
parent 0ed5e7c797
commit c88b75b0fd
3 changed files with 72 additions and 0 deletions

View file

@ -4,6 +4,26 @@ import '../error.dart';
import 'libgit2_bindings.dart'; import 'libgit2_bindings.dart';
import '../util.dart'; import '../util.dart';
/// Fill a list with all the tags in the repository.
///
/// Throws a [LibGit2Error] if error occured.
List<String> list(Pointer<git_repository> repo) {
final out = calloc<git_strarray>();
final error = libgit2.git_tag_list(out, repo);
var result = <String>[];
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
for (var i = 0; i < out.ref.count; i++) {
result.add(out.ref.strings[i].cast<Utf8>().toDartString());
}
calloc.free(out);
return result;
}
}
/// Lookup a tag object from the repository. /// Lookup a tag object from the repository.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
@ -99,5 +119,29 @@ Pointer<git_oid> create({
} }
} }
/// Delete an existing tag reference.
///
/// The tag name will be checked for validity.
///
/// Throws a [LibGit2Error] if error occured.
void delete({
required Pointer<git_repository> repoPointer,
required String tagName,
}) {
final tagNameC = tagName.toNativeUtf8().cast<Int8>();
final error = libgit2.git_tag_delete(repoPointer, tagNameC);
calloc.free(tagNameC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Get the repository that contains the tag.
Pointer<git_repository> owner(Pointer<git_tag> tag) {
return libgit2.git_tag_owner(tag);
}
/// Close an open tag to release memory. /// Close an open tag to release memory.
void free(Pointer<git_tag> tag) => libgit2.git_tag_free(tag); void free(Pointer<git_tag> tag) => libgit2.git_tag_free(tag);

View file

@ -74,6 +74,13 @@ class Tag {
return Oid(result); return Oid(result);
} }
/// Returns a list with all the tags in the repository.
///
/// Throws a [LibGit2Error] if error occured.
static List<String> list(Repository repo) {
return bindings.list(repo.pointer);
}
/// Get the tagged object (commit, tree, blob, tag) 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.
@ -122,6 +129,16 @@ class Tag {
} }
} }
/// Deletes an existing tag reference.
///
/// The tag name will be checked for validity.
///
/// Throws a [LibGit2Error] if error occured.
void delete() {
final owner = bindings.owner(_tagPointer);
bindings.delete(repoPointer: owner, tagName: name);
}
/// Releases memory allocated for tag object. /// Releases memory allocated for tag object.
void free() => bindings.free(_tagPointer); void free() => bindings.free(_tagPointer);
} }

View file

@ -80,5 +80,16 @@ void main() {
newTagTarget.free(); newTagTarget.free();
signature.free(); signature.free();
}); });
test('returns list of tags in repository', () {
expect(Tag.list(repo), ['v0.1', 'v0.2']);
});
test('successfully deletes tag', () {
expect(Tag.list(repo), ['v0.1', 'v0.2']);
tag.delete();
expect(Tag.list(repo), ['v0.1']);
});
}); });
} }