feat(reflog): add more bindings and API methods (#29)

This commit is contained in:
Aleksey Kulikov 2021-12-23 10:58:44 +03:00 committed by GitHub
parent ff2dd8b408
commit fda5173e7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 305 additions and 12 deletions

View file

@ -2,6 +2,7 @@ import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
import 'package:libgit2dart/src/error.dart';
import 'package:libgit2dart/src/util.dart';
/// Read the reflog for the given reference.
@ -23,6 +24,90 @@ Pointer<git_reflog> read({
return out.value;
}
/// Write an existing in-memory reflog object back to disk using an atomic file
/// lock.
void write(Pointer<git_reflog> reflog) {
final error = libgit2.git_reflog_write(reflog);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Delete the reflog for the given reference.
void delete({
required Pointer<git_repository> repoPointer,
required String name,
}) {
final nameC = name.toNativeUtf8().cast<Int8>();
libgit2.git_reflog_delete(repoPointer, nameC);
calloc.free(nameC);
}
/// Rename a reflog.
///
/// The reflog to be renamed is expected to already exist.
///
/// The new name will be checked for validity.
///
/// Throws a [LibGit2Error] if error occured.
void rename({
required Pointer<git_repository> repoPointer,
required String oldName,
required String newName,
}) {
final oldNameC = oldName.toNativeUtf8().cast<Int8>();
final newNameC = newName.toNativeUtf8().cast<Int8>();
final error = libgit2.git_reflog_rename(repoPointer, oldNameC, newNameC);
calloc.free(oldNameC);
calloc.free(newNameC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Add a new entry to the in-memory reflog.
///
/// Throws a [LibGit2Error] if error occured.
void add({
required Pointer<git_reflog> reflogPointer,
required Pointer<git_oid> oidPointer,
required Pointer<git_signature> committerPointer,
required String message,
}) {
final messageC =
message.isEmpty ? nullptr : message.toNativeUtf8().cast<Int8>();
final error = libgit2.git_reflog_append(
reflogPointer,
oidPointer,
committerPointer,
messageC,
);
calloc.free(messageC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Remove an entry from the reflog by its index.
///
/// Throws a [LibGit2Error] if error occured.
void remove({
required Pointer<git_reflog> reflogPointer,
required int index,
}) {
final error = libgit2.git_reflog_drop(reflogPointer, index, 1);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Get the number of log entries in a reflog.
int entryCount(Pointer<git_reflog> reflog) =>
libgit2.git_reflog_entrycount(reflog);
@ -40,7 +125,8 @@ Pointer<git_reflog_entry> getByIndex({
/// Get the log message.
String entryMessage(Pointer<git_reflog_entry> entry) {
return libgit2.git_reflog_entry_message(entry).cast<Utf8>().toDartString();
final result = libgit2.git_reflog_entry_message(entry);
return result == nullptr ? '' : result.cast<Utf8>().toDartString();
}
/// Get the committer of this entry.
@ -48,5 +134,15 @@ Pointer<git_signature> entryCommiter(Pointer<git_reflog_entry> entry) {
return libgit2.git_reflog_entry_committer(entry);
}
/// Get the new oid.
Pointer<git_oid> entryOidNew(Pointer<git_reflog_entry> entry) {
return libgit2.git_reflog_entry_id_new(entry);
}
/// Get the old oid.
Pointer<git_oid> entryOidOld(Pointer<git_reflog_entry> entry) {
return libgit2.git_reflog_entry_id_old(entry);
}
/// Free the reflog.
void free(Pointer<git_reflog> reflog) => libgit2.git_reflog_free(reflog);