diff --git a/lib/src/bindings/reference.dart b/lib/src/bindings/reference.dart index 89150ef..93b00e9 100644 --- a/lib/src/bindings/reference.dart +++ b/lib/src/bindings/reference.dart @@ -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 repoPointer, + required String refName, +}) { + final refNameC = refName.toNativeUtf8().cast(); + 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 ref) { return libgit2.git_reference_is_branch(ref) == 1 || false; @@ -410,5 +429,12 @@ Pointer peel({ } } +/// Create a copy of an existing reference. +Pointer duplicate(Pointer source) { + final out = calloc>(); + libgit2.git_reference_dup(out, source); + return out.value; +} + /// Free the given reference. void free(Pointer ref) => libgit2.git_reference_free(ref); diff --git a/lib/src/reference.dart b/lib/src/reference.dart index 4324474..adad335 100644 --- a/lib/src/reference.dart +++ b/lib/src/reference.dart @@ -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. diff --git a/test/reference_test.dart b/test/reference_test.dart index 9f1d90a..9661b8c 100644 --- a/test/reference_test.dart +++ b/test/reference_test.dart @@ -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');