From 35afc87b583175704a2b20446ce144a46817fa1f Mon Sep 17 00:00:00 2001 From: Viktor Borisov Date: Sat, 25 Feb 2023 02:43:34 +0700 Subject: [PATCH] add name_to_id binding and headCommit to repo - implemented git_reference_name_to_id binding - created repository.headCommit getter --- lib/src/bindings/reference.dart | 26 ++++++++++++++++++++++++++ lib/src/reference.dart | 18 ++++++++++++++++++ lib/src/repository.dart | 5 +++++ test/reference_test.dart | 5 +++++ test/repository_test.dart | 4 ++++ 5 files changed, 58 insertions(+) diff --git a/lib/src/bindings/reference.dart b/lib/src/bindings/reference.dart index 81e190c..3313a45 100644 --- a/lib/src/bindings/reference.dart +++ b/lib/src/bindings/reference.dart @@ -449,5 +449,31 @@ Pointer duplicate(Pointer source) { return result; } +/// Lookup a reference by name and resolve immediately to OID. +/// +/// This function provides a quick way to resolve a reference name straight +/// through to the object id that it refers to. This avoids having to +/// allocate or free any `git_reference` objects for simple situations. +/// +/// The name will be checked for validity. +/// See [createSymbolic] for rules about valid names. +/// +/// Throws a [LibGit2Error] if error occured. +Pointer nameToId({ + required Pointer repoPointer, + required String refName, +}) { + final result = calloc(); + final nameC = refName.toChar(); + + final error = libgit2.git_reference_name_to_id(result, repoPointer, nameC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return result; + } +} + /// 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 c5d4471..738fb23 100644 --- a/lib/src/reference.dart +++ b/lib/src/reference.dart @@ -191,6 +191,24 @@ class Reference extends Equatable { bindings.ensureLog(repoPointer: repo.pointer, refName: refName); } + /// Lookup a reference by name and resolve immediately to OID. + /// + /// This function provides a quick way to resolve a reference name straight + /// through to the object id that it refers to. This avoids having to + /// allocate or free any `git_reference` objects for simple situations. + /// + /// The name will be checked for validity. + /// See [createSymbolic] for rules about valid names. + /// + /// Throws a [LibGit2Error] if error occured. + static Oid nameToId({ + required Repository repo, + required String refName, + }) { + final res = bindings.nameToId(repoPointer: repo.pointer, refName: refName); + return Oid(res); + } + /// Creates a copy of an existing reference. Reference duplicate() => Reference(bindings.duplicate(_refPointer)); diff --git a/lib/src/repository.dart b/lib/src/repository.dart index 03c7190..6af1d91 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -339,6 +339,11 @@ class Repository extends Equatable { ); } + /// Retrieve the commit that HEAD is currently pointing to + Commit get headCommit { + return Commit.lookup(repo: this, oid: head.target); + } + /// Removes all the metadata associated with an ongoing command like /// merge, revert, cherry-pick, etc. For example: MERGE_HEAD, MERGE_MSG, etc. /// diff --git a/test/reference_test.dart b/test/reference_test.dart index 7b74b87..76d32c8 100644 --- a/test/reference_test.dart +++ b/test/reference_test.dart @@ -530,6 +530,11 @@ void main() { ); }); + test('get oid by name', () { + final oid = Reference.nameToId(repo: repo, refName: 'HEAD'); + expect(oid, repo.head.target); + }); + test('manually releases allocated memory', () { final ref = Reference.lookup(repo: repo, name: 'refs/heads/master'); expect(() => ref.free(), returnsNormally); diff --git a/test/repository_test.dart b/test/repository_test.dart index 7d155c3..043f370 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -246,6 +246,10 @@ void main() { expect(() => repo.free(), returnsNormally); }); + test('get head commit', () { + expect(repo.headCommit.oid.sha, lastCommit); + }); + test('returns string representation of Repository object', () { expect(repo.toString(), contains('Repository{')); });