From 3b13646b1b157750a567a686011a18e5233294bd Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Wed, 1 Sep 2021 10:42:24 +0300 Subject: [PATCH] feat(repository): add api method for revparse_single --- lib/src/bindings/object.dart | 6 ++++++ lib/src/bindings/repository.dart | 2 ++ lib/src/enums.dart | 2 ++ lib/src/repository.dart | 20 +++++++++++++++----- test/repository_test.dart | 18 ++++++++++++++++++ 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 lib/src/bindings/object.dart diff --git a/lib/src/bindings/object.dart b/lib/src/bindings/object.dart new file mode 100644 index 0000000..f29c00c --- /dev/null +++ b/lib/src/bindings/object.dart @@ -0,0 +1,6 @@ +import 'dart:ffi'; +import 'libgit2_bindings.dart'; +import '../util.dart'; + +/// Get the object type of an object. +int type(Pointer obj) => libgit2.git_object_type(obj); diff --git a/lib/src/bindings/repository.dart b/lib/src/bindings/repository.dart index fc8469e..704d92a 100644 --- a/lib/src/bindings/repository.dart +++ b/lib/src/bindings/repository.dart @@ -511,6 +511,8 @@ Pointer wrapODB(Pointer odb) { } /// Find a single object, as specified by a [spec] string. +/// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions +/// for information on the syntax accepted. /// /// The returned object should be released when no longer needed. /// diff --git a/lib/src/enums.dart b/lib/src/enums.dart index 185662e..6781efe 100644 --- a/lib/src/enums.dart +++ b/lib/src/enums.dart @@ -17,3 +17,5 @@ enum GitFilemode { undreadable, tree, blob, blobExecutable, link, commit } /// [reverse] Iterate through the repository contents in reverse /// order; this sorting mode can be combined with any of the above. enum GitSort { none, topological, time, reverse } + +enum GitObject { commit, tree, blob, tag } diff --git a/lib/src/repository.dart b/lib/src/repository.dart index e0abd5e..b108284 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -1,15 +1,14 @@ import 'dart:ffi'; -import 'package:libgit2dart/src/enums.dart'; -import 'package:libgit2dart/src/revwalk.dart'; - +import 'bindings/libgit2_bindings.dart'; +import 'bindings/repository.dart' as bindings; import 'commit.dart'; import 'config.dart'; import 'index.dart'; import 'odb.dart'; import 'oid.dart'; import 'reference.dart'; -import 'bindings/libgit2_bindings.dart'; -import 'bindings/repository.dart' as bindings; +import 'revwalk.dart'; +import 'enums.dart'; import 'util.dart'; class Repository { @@ -324,6 +323,17 @@ class Repository { return Commit.lookup(this, oid); } + /// Find a single object, as specified by a [spec] string. + /// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions + /// for information on the syntax accepted. + /// + /// The returned object should be released when no longer needed. + /// + /// Throws a [LibGit2Error] if error occured. + Commit revParseSingle(String spec) { + return Commit(bindings.revParseSingle(_repoPointer, spec).cast()); + } + /// Returns the list of commits starting from provided [oid]. /// /// If [sorting] isn't provided default will be used (reverse chronological order, like in git). diff --git a/test/repository_test.dart b/test/repository_test.dart index c4ba14a..7c31768 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -199,6 +199,24 @@ void main() { } }); + test('returns commit with different spec strings', () { + const headSHA = '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8'; + const parentSHA = 'c68ff54aabf660fcdd9a2838d401583fe31249e3'; + + final headCommit = repo.revParseSingle('HEAD'); + expect(headCommit.id.sha, headSHA); + + final parentCommit = repo.revParseSingle('HEAD^'); + expect(parentCommit.id.sha, parentSHA); + + final initCommit = repo.revParseSingle('@{-1}'); + expect(initCommit.id.sha, '5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'); + + headCommit.free(); + parentCommit.free(); + initCommit.free(); + }); + group('.discover()', () { test('discovers repository', () async { final subDir = '${tmpDir}subdir1/subdir2/';