feat(remote): add bindings and api

This commit is contained in:
Aleksey Kulikov 2021-09-22 18:11:20 +03:00
parent 3b883c49e3
commit e0e16aea30
9 changed files with 1218 additions and 1 deletions

View file

@ -0,0 +1,87 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import '../error.dart';
import 'libgit2_bindings.dart';
import '../util.dart';
/// Get the source specifier.
String source(Pointer<git_refspec> refspec) {
return libgit2.git_refspec_src(refspec).cast<Utf8>().toDartString();
}
/// Get the destination specifier.
String destination(Pointer<git_refspec> refspec) {
return libgit2.git_refspec_dst(refspec).cast<Utf8>().toDartString();
}
/// Get the force update setting.
bool force(Pointer<git_refspec> refspec) {
return libgit2.git_refspec_force(refspec) == 1 ? true : false;
}
/// Get the refspec's string.
String string(Pointer<git_refspec> refspec) {
return libgit2.git_refspec_string(refspec).cast<Utf8>().toDartString();
}
/// Get the refspec's direction.
int direction(Pointer<git_refspec> refspec) =>
libgit2.git_refspec_direction(refspec);
/// Check if a refspec's source descriptor matches a reference.
bool matchesSource(Pointer<git_refspec> refspec, String refname) {
final refnameC = refname.toNativeUtf8().cast<Int8>();
final result = libgit2.git_refspec_src_matches(refspec, refnameC);
calloc.free(refnameC);
return result == 1 ? true : false;
}
/// Check if a refspec's destination descriptor matches a reference.
bool matchesDestination(Pointer<git_refspec> refspec, String refname) {
final refnameC = refname.toNativeUtf8().cast<Int8>();
final result = libgit2.git_refspec_dst_matches(refspec, refnameC);
calloc.free(refnameC);
return result == 1 ? true : false;
}
/// Transform a reference to its target following the refspec's rules.
///
/// Throws a [LibGit2Error] if error occured.
String transform(Pointer<git_refspec> spec, String name) {
final out = calloc<git_buf>(sizeOf<git_buf>());
final nameC = name.toNativeUtf8().cast<Int8>();
final error = libgit2.git_refspec_transform(out, spec, nameC);
calloc.free(nameC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
final result = out.ref.ptr.cast<Utf8>().toDartString();
calloc.free(out);
return result;
}
}
/// Transform a target reference to its source reference following the refspec's rules.
///
/// Throws a [LibGit2Error] if error occured.
String rTransform(Pointer<git_refspec> spec, String name) {
final out = calloc<git_buf>(sizeOf<git_buf>());
final nameC = name.toNativeUtf8().cast<Int8>();
final error = libgit2.git_refspec_rtransform(out, spec, nameC);
calloc.free(nameC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
final result = out.ref.ptr.cast<Utf8>().toDartString();
calloc.free(out);
return result;
}
}