feat(commit): add base bindings and api

This commit is contained in:
Aleksey Kulikov 2021-08-24 19:08:12 +03:00
parent 696d55bb3a
commit dc5f510aa5
12 changed files with 485 additions and 17 deletions

71
lib/src/signature.dart Normal file
View file

@ -0,0 +1,71 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'bindings/libgit2_bindings.dart';
import 'bindings/signature.dart' as bindings;
import 'util.dart';
class Signature {
/// Initializes a new instance of [Signature] class from provided pointer to
/// signature object in memory.
///
/// Should be freed with `free()` to release allocated memory.
Signature(this._signaturePointer) {
libgit2.git_libgit2_init();
}
/// Initializes a new instance of [Signature] class from provided [name], [email],
/// and optional [time] in seconds from epoch and [offset] in minutes.
///
/// If [time] isn't provided [Signature] will be created with a timestamp of 'now'.
///
/// Should be freed with `free()` to release allocated memory.
Signature.create({
required String name,
required String email,
int? time,
int offset = 0,
}) {
libgit2.git_libgit2_init();
if (time == null) {
_signaturePointer = bindings.now(name, email);
} else {
_signaturePointer = bindings.create(name, email, time, offset);
}
}
/// Pointer to memory address for allocated signature object.
late final Pointer<git_signature> _signaturePointer;
/// Returns full name of the author.
String get name => _signaturePointer.ref.name.cast<Utf8>().toDartString();
/// Returns email of the author.
String get email => _signaturePointer.ref.email.cast<Utf8>().toDartString();
/// Returns time in seconds from epoch.
int get time => _signaturePointer.ref.when.time;
/// Returns timezone offset in minutes.
int get offset => _signaturePointer.ref.when.offset;
@override
bool operator ==(other) {
return (other is Signature) &&
(name == other.name) &&
(email == other.email) &&
(time == other.time) &&
(offset == other.offset) &&
(_signaturePointer.ref.when.sign ==
other._signaturePointer.ref.when.sign);
}
@override
int get hashCode => _signaturePointer.address.hashCode;
/// Releases memory allocated for signature object.
void free() {
bindings.free(_signaturePointer);
libgit2.git_libgit2_shutdown();
}
}