mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 20:29:08 -04:00
106 lines
2.5 KiB
Dart
106 lines
2.5 KiB
Dart
import 'dart:ffi';
|
|
import 'package:ffi/ffi.dart';
|
|
import '../error.dart';
|
|
import 'libgit2_bindings.dart';
|
|
import '../util.dart';
|
|
|
|
/// Parse N characters of a hex formatted object id into a git_oid.
|
|
///
|
|
/// If N is odd, the last byte's high nibble will be read in and the low nibble set to zero.
|
|
///
|
|
/// Throws a [LibGit2Error] if error occured.
|
|
Pointer<git_oid> fromStrN(String hex) {
|
|
final out = calloc<git_oid>();
|
|
final str = hex.toNativeUtf8().cast<Int8>();
|
|
final error = libgit2.git_oid_fromstrn(out, str, hex.length);
|
|
|
|
calloc.free(str);
|
|
|
|
if (error < 0) {
|
|
calloc.free(out);
|
|
throw LibGit2Error(libgit2.git_error_last());
|
|
} else {
|
|
return out;
|
|
}
|
|
}
|
|
|
|
/// Parse a hex formatted object id into a git_oid.
|
|
///
|
|
/// Throws a [LibGit2Error] if error occured.
|
|
Pointer<git_oid> fromSHA(String hex) {
|
|
final out = calloc<git_oid>();
|
|
final str = hex.toNativeUtf8().cast<Int8>();
|
|
final error = libgit2.git_oid_fromstr(out, str);
|
|
|
|
calloc.free(str);
|
|
|
|
if (error < 0) {
|
|
calloc.free(out);
|
|
throw LibGit2Error(libgit2.git_error_last());
|
|
} else {
|
|
return out;
|
|
}
|
|
}
|
|
|
|
/// Copy an already raw oid into a git_oid structure.
|
|
///
|
|
/// Throws a [LibGit2Error] if error occured.
|
|
Pointer<git_oid> fromRaw(Array<Uint8> raw) {
|
|
final out = calloc<git_oid>();
|
|
var rawC = calloc<Uint8>(20);
|
|
for (var i = 0; i < 20; i++) {
|
|
rawC[i] = raw[i];
|
|
}
|
|
final error = libgit2.git_oid_fromraw(out, rawC);
|
|
|
|
calloc.free(rawC);
|
|
|
|
if (error < 0) {
|
|
calloc.free(out);
|
|
throw LibGit2Error(libgit2.git_error_last());
|
|
} else {
|
|
return out;
|
|
}
|
|
}
|
|
|
|
/// Format a git_oid into a hex string.
|
|
///
|
|
/// Throws a [LibGit2Error] if error occured.
|
|
String toSHA(Pointer<git_oid> id) {
|
|
final out = calloc<Int8>(40);
|
|
final error = libgit2.git_oid_fmt(out, id);
|
|
|
|
if (error < 0) {
|
|
calloc.free(out);
|
|
throw LibGit2Error(libgit2.git_error_last());
|
|
} else {
|
|
final result = out.cast<Utf8>().toDartString(length: 40);
|
|
calloc.free(out);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// Compare two oid structures.
|
|
///
|
|
/// Returns <0 if a < b, 0 if a == b, >0 if a > b.
|
|
int compare({
|
|
required Pointer<git_oid> aPointer,
|
|
required Pointer<git_oid> bPointer,
|
|
}) {
|
|
return libgit2.git_oid_cmp(aPointer, bPointer);
|
|
}
|
|
|
|
/// Copy an oid from one structure to another.
|
|
///
|
|
/// Throws a [LibGit2Error] if error occured.
|
|
Pointer<git_oid> copy(Pointer<git_oid> src) {
|
|
final out = calloc<git_oid>();
|
|
final error = libgit2.git_oid_cpy(out, src);
|
|
|
|
if (error < 0) {
|
|
calloc.free(out);
|
|
throw LibGit2Error(libgit2.git_error_last());
|
|
} else {
|
|
return out;
|
|
}
|
|
}
|