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 repoPointer, required int strategy, String? directory, List? 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 repoPointer, required int strategy, String? directory, List? 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 repoPointer, required Pointer treeishPointer, required int strategy, String? directory, List? 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 initOptions({ required int strategy, String? directory, List? paths, }) { final optsC = calloc(sizeOf()); 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(); } List> pathPointers = []; Pointer> strArray = nullptr; if (paths != null) { pathPointers = paths.map((e) => e.toNativeUtf8().cast()).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 = []; result.add(optsC); result.add(pathPointers); result.add(strArray); return result; }