From 10b9864219f6a5f67218eb78ebca78416329435c Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Thu, 23 Sep 2021 11:07:16 +0300 Subject: [PATCH] feat(repository): add alias for commit creation --- lib/src/bindings/commit.dart | 12 +++++++----- lib/src/commit.dart | 10 ++++++++-- lib/src/repository.dart | 28 ++++++++++++++++++++++++++++ test/commit_test.dart | 3 +-- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/lib/src/bindings/commit.dart b/lib/src/bindings/commit.dart index c43344b..0e320fe 100644 --- a/lib/src/bindings/commit.dart +++ b/lib/src/bindings/commit.dart @@ -73,19 +73,21 @@ void annotatedFree(Pointer commit) { /// Throws a [LibGit2Error] if error occured. Pointer create( Pointer repo, - String? updateRef, + String updateRef, Pointer author, Pointer committer, - String? messageEncoding, + String messageEncoding, String message, Pointer tree, int parentCount, List parents, ) { final out = calloc(); - final updateRefC = updateRef?.toNativeUtf8().cast() ?? nullptr; - final messageEncodingC = - messageEncoding?.toNativeUtf8().cast() ?? nullptr; + final updateRefC = + updateRef.isEmpty ? nullptr : updateRef.toNativeUtf8().cast(); + final messageEncodingC = messageEncoding.isEmpty + ? nullptr + : messageEncoding.toNativeUtf8().cast(); final messageC = message.toNativeUtf8().cast(); Pointer> parentsC = calloc.call>(parentCount); diff --git a/lib/src/commit.dart b/lib/src/commit.dart index 47ad4ca..5a00997 100644 --- a/lib/src/commit.dart +++ b/lib/src/commit.dart @@ -33,6 +33,12 @@ class Commit { /// Creates new commit in the repository. /// + /// [updateRef] is name of the reference that will be updated to point to this commit. + /// If the reference is not direct, it will be resolved to a direct reference. Use "HEAD" + /// to update the HEAD of the current branch and make it point to this commit. If the + /// reference doesn't exist yet, it will be created. If it does exist, the first parent + /// must be the tip of this branch. + /// /// Throws a [LibGit2Error] if error occured. static Oid create({ required Repository repo, @@ -41,8 +47,8 @@ class Commit { required Signature commiter, required String treeSHA, required List parents, - String? updateRef, - String? messageEncoding, + String updateRef = '', + String messageEncoding = '', }) { final tree = Tree.lookup(repo, treeSHA); diff --git a/lib/src/repository.dart b/lib/src/repository.dart index 6b1a9aa..7b1953f 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -394,6 +394,34 @@ class Repository { /// Throws a [LibGit2Error] if error occured. Commit revParseSingle(String spec) => RevParse.single(this, spec); + /// Creates new commit in the repository. + /// + /// [updateRef] is name of the reference that will be updated to point to this commit. + /// If the reference is not direct, it will be resolved to a direct reference. Use "HEAD" + /// to update the HEAD of the current branch and make it point to this commit. If the + /// reference doesn't exist yet, it will be created. If it does exist, the first parent + /// must be the tip of this branch. + /// + /// Throws a [LibGit2Error] if error occured. + Oid createCommit({ + required String message, + required Signature author, + required Signature commiter, + required String treeSHA, + required List parents, + String updateRef = '', + String messageEncoding = '', + }) { + return Commit.create( + repo: this, + message: message, + author: author, + commiter: commiter, + treeSHA: treeSHA, + parents: parents, + ); + } + /// Finds a single object and intermediate reference (if there is one) by a [spec] revision string. /// /// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions diff --git a/test/commit_test.dart b/test/commit_test.dart index e5e95ce..57448ae 100644 --- a/test/commit_test.dart +++ b/test/commit_test.dart @@ -97,8 +97,7 @@ void main() { }); test('successfully creates commit without parents', () { - final oid = Commit.create( - repo: repo, + final oid = repo.createCommit( message: message, author: author, commiter: commiter,