feat(repository): add alias for commit creation

This commit is contained in:
Aleksey Kulikov 2021-09-23 11:07:16 +03:00
parent 7187d890d6
commit 10b9864219
4 changed files with 44 additions and 9 deletions

View file

@ -73,19 +73,21 @@ void annotatedFree(Pointer<git_annotated_commit> commit) {
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_oid> create( Pointer<git_oid> create(
Pointer<git_repository> repo, Pointer<git_repository> repo,
String? updateRef, String updateRef,
Pointer<git_signature> author, Pointer<git_signature> author,
Pointer<git_signature> committer, Pointer<git_signature> committer,
String? messageEncoding, String messageEncoding,
String message, String message,
Pointer<git_tree> tree, Pointer<git_tree> tree,
int parentCount, int parentCount,
List<String> parents, List<String> parents,
) { ) {
final out = calloc<git_oid>(); final out = calloc<git_oid>();
final updateRefC = updateRef?.toNativeUtf8().cast<Int8>() ?? nullptr; final updateRefC =
final messageEncodingC = updateRef.isEmpty ? nullptr : updateRef.toNativeUtf8().cast<Int8>();
messageEncoding?.toNativeUtf8().cast<Int8>() ?? nullptr; final messageEncodingC = messageEncoding.isEmpty
? nullptr
: messageEncoding.toNativeUtf8().cast<Int8>();
final messageC = message.toNativeUtf8().cast<Int8>(); final messageC = message.toNativeUtf8().cast<Int8>();
Pointer<Pointer<git_commit>> parentsC = Pointer<Pointer<git_commit>> parentsC =
calloc.call<Pointer<git_commit>>(parentCount); calloc.call<Pointer<git_commit>>(parentCount);

View file

@ -33,6 +33,12 @@ class Commit {
/// Creates new commit in the repository. /// 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. /// Throws a [LibGit2Error] if error occured.
static Oid create({ static Oid create({
required Repository repo, required Repository repo,
@ -41,8 +47,8 @@ class Commit {
required Signature commiter, required Signature commiter,
required String treeSHA, required String treeSHA,
required List<String> parents, required List<String> parents,
String? updateRef, String updateRef = '',
String? messageEncoding, String messageEncoding = '',
}) { }) {
final tree = Tree.lookup(repo, treeSHA); final tree = Tree.lookup(repo, treeSHA);

View file

@ -394,6 +394,34 @@ class Repository {
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Commit revParseSingle(String spec) => RevParse.single(this, spec); 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<String> 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. /// 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 /// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions

View file

@ -97,8 +97,7 @@ void main() {
}); });
test('successfully creates commit without parents', () { test('successfully creates commit without parents', () {
final oid = Commit.create( final oid = repo.createCommit(
repo: repo,
message: message, message: message,
author: author, author: author,
commiter: commiter, commiter: commiter,