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'; /// Get the source specifier. String source(Pointer refspec) { return libgit2.git_refspec_src(refspec).cast().toDartString(); } /// Get the destination specifier. String destination(Pointer refspec) { return libgit2.git_refspec_dst(refspec).cast().toDartString(); } /// Get the force update setting. bool force(Pointer refspec) { return libgit2.git_refspec_force(refspec) == 1 || false; } /// Get the refspec's string. String string(Pointer refspec) { return libgit2.git_refspec_string(refspec).cast().toDartString(); } /// Get the refspec's direction. int direction(Pointer refspec) => libgit2.git_refspec_direction(refspec); /// Check if a refspec's source descriptor matches a reference. bool matchesSource({ required Pointer refspecPointer, required String refname, }) { final refnameC = refname.toNativeUtf8().cast(); final result = libgit2.git_refspec_src_matches(refspecPointer, refnameC); calloc.free(refnameC); return result == 1 || false; } /// Check if a refspec's destination descriptor matches a reference. bool matchesDestination({ required Pointer refspecPointer, required String refname, }) { final refnameC = refname.toNativeUtf8().cast(); final result = libgit2.git_refspec_dst_matches(refspecPointer, refnameC); calloc.free(refnameC); return result == 1 || false; } /// Transform a reference to its target following the refspec's rules. /// /// Throws a [LibGit2Error] if error occured. String transform({ required Pointer refspecPointer, required String name, }) { final out = calloc(sizeOf()); final nameC = name.toNativeUtf8().cast(); final error = libgit2.git_refspec_transform(out, refspecPointer, nameC); calloc.free(nameC); if (error < 0) { calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.ref.ptr.cast().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({ required Pointer refspecPointer, required String name, }) { final out = calloc(sizeOf()); final nameC = name.toNativeUtf8().cast(); final error = libgit2.git_refspec_rtransform(out, refspecPointer, nameC); calloc.free(nameC); if (error < 0) { calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.ref.ptr.cast().toDartString(); calloc.free(out); return result; } }