mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 20:29:08 -04:00
136 lines
3.6 KiB
Dart
136 lines
3.6 KiB
Dart
import 'dart:ffi';
|
|
import 'package:ffi/ffi.dart';
|
|
import '../error.dart';
|
|
import 'libgit2_bindings.dart';
|
|
import '../util.dart';
|
|
|
|
/// Updates files in the index and the working tree to match the content of the commit
|
|
/// pointed at by HEAD.
|
|
///
|
|
/// Note that this is not the correct mechanism used to switch branches; do not change
|
|
/// your HEAD and then call this method, that would leave you with checkout conflicts
|
|
/// since your working directory would then appear to be dirty. Instead, checkout the
|
|
/// target of the branch and then update HEAD using `setHead` to point to the branch you checked out.
|
|
///
|
|
/// Throws a [LibGit2Error] if error occured.
|
|
void head({
|
|
required Pointer<git_repository> repoPointer,
|
|
required int strategy,
|
|
String? directory,
|
|
List<String>? paths,
|
|
}) {
|
|
final initOpts =
|
|
initOptions(strategy: strategy, directory: directory, paths: paths);
|
|
final optsC = initOpts[0];
|
|
final pathPointers = initOpts[1];
|
|
final strArray = initOpts[2];
|
|
|
|
final error = libgit2.git_checkout_head(repoPointer, optsC);
|
|
|
|
for (var p in pathPointers) {
|
|
calloc.free(p);
|
|
}
|
|
|
|
calloc.free(strArray);
|
|
calloc.free(optsC);
|
|
|
|
if (error < 0) {
|
|
throw LibGit2Error(libgit2.git_error_last());
|
|
}
|
|
}
|
|
|
|
/// Updates files in the working tree to match the content of the index.
|
|
///
|
|
/// Throws a [LibGit2Error] if error occured.
|
|
void index({
|
|
required Pointer<git_repository> repoPointer,
|
|
required int strategy,
|
|
String? directory,
|
|
List<String>? paths,
|
|
}) {
|
|
final initOpts =
|
|
initOptions(strategy: strategy, directory: directory, paths: paths);
|
|
final optsC = initOpts[0];
|
|
final pathPointers = initOpts[1];
|
|
final strArray = initOpts[2];
|
|
|
|
final error = libgit2.git_checkout_index(repoPointer, nullptr, optsC);
|
|
|
|
for (var p in pathPointers) {
|
|
calloc.free(p);
|
|
}
|
|
|
|
calloc.free(strArray);
|
|
calloc.free(optsC);
|
|
|
|
if (error < 0) {
|
|
throw LibGit2Error(libgit2.git_error_last());
|
|
}
|
|
}
|
|
|
|
/// Updates files in the index and working tree to match the content of the tree
|
|
/// pointed at by the treeish.
|
|
///
|
|
/// Throws a [LibGit2Error] if error occured.
|
|
void tree({
|
|
required Pointer<git_repository> repoPointer,
|
|
required Pointer<git_object> treeishPointer,
|
|
required int strategy,
|
|
String? directory,
|
|
List<String>? paths,
|
|
}) {
|
|
final initOpts = initOptions(
|
|
strategy: strategy,
|
|
directory: directory,
|
|
paths: paths,
|
|
);
|
|
final optsC = initOpts[0];
|
|
final pathPointers = initOpts[1];
|
|
final strArray = initOpts[2];
|
|
|
|
final error = libgit2.git_checkout_tree(repoPointer, treeishPointer, optsC);
|
|
|
|
for (var p in pathPointers) {
|
|
calloc.free(p);
|
|
}
|
|
|
|
calloc.free(strArray);
|
|
calloc.free(optsC);
|
|
|
|
if (error < 0) {
|
|
throw LibGit2Error(libgit2.git_error_last());
|
|
}
|
|
}
|
|
|
|
List<dynamic> initOptions({
|
|
required int strategy,
|
|
String? directory,
|
|
List<String>? paths,
|
|
}) {
|
|
final optsC = calloc<git_checkout_options>(sizeOf<git_checkout_options>());
|
|
libgit2.git_checkout_options_init(optsC, GIT_CHECKOUT_OPTIONS_VERSION);
|
|
|
|
optsC.ref.checkout_strategy = strategy;
|
|
|
|
if (directory != null) {
|
|
optsC.ref.target_directory = directory.toNativeUtf8().cast<Int8>();
|
|
}
|
|
|
|
List<Pointer<Int8>> pathPointers = [];
|
|
Pointer<Pointer<Int8>> strArray = nullptr;
|
|
if (paths != null) {
|
|
pathPointers = paths.map((e) => e.toNativeUtf8().cast<Int8>()).toList();
|
|
strArray = calloc(paths.length);
|
|
for (var i = 0; i < paths.length; i++) {
|
|
strArray[i] = pathPointers[i];
|
|
}
|
|
optsC.ref.paths.strings = strArray;
|
|
optsC.ref.paths.count = paths.length;
|
|
}
|
|
|
|
var result = <dynamic>[];
|
|
result.add(optsC);
|
|
result.add(pathPointers);
|
|
result.add(strArray);
|
|
return result;
|
|
}
|