feat(commit): add more bindings and API methods (#22)

This commit is contained in:
Aleksey Kulikov 2021-12-20 20:42:16 +03:00 committed by GitHub
parent e6bfdc5a85
commit 5740831bb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 438 additions and 28 deletions

View file

@ -115,6 +115,65 @@ Pointer<git_oid> create({
}
}
/// Create a commit and write it into a buffer.
///
/// Create a commit as with [create] but instead of writing it to the objectdb,
/// write the contents of the object into a buffer.
///
/// Throws a [LibGit2Error] if error occured.
String createBuffer({
required Pointer<git_repository> repoPointer,
required String updateRef,
required Pointer<git_signature> authorPointer,
required Pointer<git_signature> committerPointer,
String? messageEncoding,
required String message,
required Pointer<git_tree> treePointer,
required int parentCount,
required List<Pointer<git_commit>> parents,
}) {
final out = calloc<git_buf>();
final updateRefC = updateRef.toNativeUtf8().cast<Int8>();
final messageEncodingC =
messageEncoding?.toNativeUtf8().cast<Int8>() ?? nullptr;
final messageC = message.toNativeUtf8().cast<Int8>();
final parentsC = calloc<Pointer<git_commit>>(parentCount);
if (parents.isNotEmpty) {
for (var i = 0; i < parentCount; i++) {
parentsC[i] = parents[i];
}
} else {
parentsC[0] = nullptr;
}
final error = libgit2.git_commit_create_buffer(
out,
repoPointer,
authorPointer,
committerPointer,
messageEncodingC,
messageC,
treePointer,
parentCount,
parentsC,
);
calloc.free(updateRefC);
calloc.free(messageEncodingC);
calloc.free(messageC);
calloc.free(parentsC);
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
final result = out.ref.ptr.cast<Utf8>().toDartString(length: out.ref.size);
calloc.free(out);
return result;
}
}
/// Amend an existing commit by replacing only non-null values.
///
/// This creates a new commit that is exactly the same as the old commit,
@ -167,6 +226,14 @@ Pointer<git_oid> amend({
}
}
/// Create an in-memory copy of a commit. The copy must be explicitly free'd or
/// it will leak.
Pointer<git_commit> duplicate(Pointer<git_commit> source) {
final out = calloc<Pointer<git_commit>>();
libgit2.git_commit_dup(out, source);
return out.value;
}
/// Get the encoding for the message of a commit, as a string representing a
/// standard encoding name.
///
@ -185,6 +252,55 @@ String message(Pointer<git_commit> commit) {
return libgit2.git_commit_message(commit).cast<Utf8>().toDartString();
}
/// Get the short "summary" of the git commit message.
///
/// The returned message is the summary of the commit, comprising the first
/// paragraph of the message with whitespace trimmed and squashed.
///
/// Throws a [LibGit2Error] if error occured.
String summary(Pointer<git_commit> commit) {
final result = libgit2.git_commit_summary(commit);
if (result == nullptr) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return result.cast<Utf8>().toDartString();
}
}
/// Get the long "body" of the git commit message.
///
/// The returned message is the body of the commit, comprising everything but
/// the first paragraph of the message. Leading and trailing whitespaces are
/// trimmed.
String body(Pointer<git_commit> commit) {
final result = libgit2.git_commit_body(commit);
return result == nullptr ? '' : result.cast<Utf8>().toDartString();
}
/// Get an arbitrary header field.
///
/// Throws a [LibGit2Error] if error occured.
String headerField({
required Pointer<git_commit> commitPointer,
required String field,
}) {
final out = calloc<git_buf>();
final fieldC = field.toNativeUtf8().cast<Int8>();
final error = libgit2.git_commit_header_field(out, commitPointer, fieldC);
calloc.free(fieldC);
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
final result = out.ref.ptr.cast<Utf8>().toDartString(length: out.ref.size);
calloc.free(out);
return result;
}
}
/// Get the id of a commit.
Pointer<git_oid> id(Pointer<git_commit> commit) =>
libgit2.git_commit_id(commit);
@ -194,8 +310,6 @@ int parentCount(Pointer<git_commit> commit) =>
libgit2.git_commit_parentcount(commit);
/// Get the oid of a specified parent for a commit.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_oid> parentId({
required Pointer<git_commit> commitPointer,
required int position,
@ -203,9 +317,55 @@ Pointer<git_oid> parentId({
return libgit2.git_commit_parent_id(commitPointer, position);
}
/// Get the specified parent of the commit (0-based).
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_commit> parent({
required Pointer<git_commit> commitPointer,
required int position,
}) {
final out = calloc<Pointer<git_commit>>();
final error = libgit2.git_commit_parent(out, commitPointer, position);
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Get the commit object that is the th generation ancestor of the named
/// commit object, following only the first parents. The returned commit has to
/// be freed by the caller.
///
/// Passing 0 as the generation number returns another instance of the base
/// commit itself.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_commit> nthGenAncestor({
required Pointer<git_commit> commitPointer,
required int n,
}) {
final out = calloc<Pointer<git_commit>>();
final error = libgit2.git_commit_nth_gen_ancestor(out, commitPointer, n);
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Get the commit time (i.e. committer time) of a commit.
int time(Pointer<git_commit> commit) => libgit2.git_commit_time(commit);
/// Get the commit timezone offset in minutes (i.e. committer's preferred
/// timezone) of a commit.
int timeOffset(Pointer<git_commit> commit) =>
libgit2.git_commit_time_offset(commit);
/// Get the committer of a commit.
Pointer<git_signature> committer(Pointer<git_commit> commit) {
return libgit2.git_commit_committer(commit);
@ -217,10 +377,25 @@ Pointer<git_signature> author(Pointer<git_commit> commit) {
}
/// Get the id of the tree pointed to by a commit.
Pointer<git_oid> tree(Pointer<git_commit> commit) {
Pointer<git_oid> treeOid(Pointer<git_commit> commit) {
return libgit2.git_commit_tree_id(commit);
}
/// Get the tree pointed to by a commit.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_tree> tree(Pointer<git_commit> commit) {
final out = calloc<Pointer<git_tree>>();
final error = libgit2.git_commit_tree(out, commit);
if (error < 0) {
calloc.free(out);
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Reverts the given commit against the given "our" commit, producing an index
/// that reflects the result of the revert.
///