feat(reference): add more bindings and API methods (#28)

This commit is contained in:
Aleksey Kulikov 2021-12-22 19:53:38 +03:00 committed by GitHub
parent fb4694cf06
commit ff2dd8b408
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 0 deletions

View file

@ -157,6 +157,25 @@ bool hasLog({
return result == 1 || false;
}
/// Ensure there is a reflog for a particular reference.
///
/// Make sure that successive updates to the reference will append to its log.
///
/// Throws a [LibGit2Error] if error occured.
void ensureLog({
required Pointer<git_repository> repoPointer,
required String refName,
}) {
final refNameC = refName.toNativeUtf8().cast<Int8>();
final error = libgit2.git_reference_ensure_log(repoPointer, refNameC);
calloc.free(refNameC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Check if a reference is a local branch.
bool isBranch(Pointer<git_reference> ref) {
return libgit2.git_reference_is_branch(ref) == 1 || false;
@ -410,5 +429,12 @@ Pointer<git_object> peel({
}
}
/// Create a copy of an existing reference.
Pointer<git_reference> duplicate(Pointer<git_reference> source) {
final out = calloc<Pointer<git_reference>>();
libgit2.git_reference_dup(out, source);
return out.value;
}
/// Free the given reference.
void free(Pointer<git_reference> ref) => libgit2.git_reference_free(ref);

View file

@ -139,6 +139,11 @@ class Reference {
refdb_bindings.free(refdb);
}
/// Creates a copy of an existing reference.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
Reference duplicate() => Reference(bindings.duplicate(_refPointer));
/// Type of the reference.
ReferenceType get type {
return bindings.referenceType(_refPointer) == 1
@ -242,6 +247,17 @@ class Reference {
);
}
/// Ensures there is a reflog for a particular reference.
///
/// Makes sure that successive updates to the reference will append to its
/// log.
///
/// Throws a [LibGit2Error] if error occured.
void ensureLog() {
final owner = bindings.owner(_refPointer);
bindings.ensureLog(repoPointer: owner, refName: name);
}
/// [RefLog] object.
///
/// **IMPORTANT**: Should be freed to release allocated memory.

View file

@ -128,6 +128,19 @@ void main() {
ref.free();
});
test('duplicates existing reference', () {
expect(repo.references.length, 6);
final ref = repo.lookupReference('refs/heads/master');
final duplicate = ref.duplicate();
expect(repo.references.length, 6);
expect(duplicate, equals(ref));
duplicate.free();
ref.free();
});
group('create direct', () {
test('successfully creates with Oid as target', () {
final ref = repo.lookupReference('refs/heads/master');