feat(worktree): add base bindings and api

This commit is contained in:
Aleksey Kulikov 2021-09-06 20:11:41 +03:00
parent 11dbb8195d
commit a00078ba76
5 changed files with 224 additions and 2 deletions

54
lib/src/worktree.dart Normal file
View file

@ -0,0 +1,54 @@
import 'dart:ffi';
import 'bindings/libgit2_bindings.dart';
import 'bindings/worktree.dart' as bindings;
import 'repository.dart';
class Worktree {
/// Initializes a new instance of [Worktree] class by creating new worktree
/// with provided [Repository] object worktree [name] and [path].
///
/// Should be freed with `free()` to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
Worktree.create({
required Repository repo,
required String name,
required String path,
}) {
_worktreePointer = bindings.create(repo.pointer, name, path);
}
/// Initializes a new instance of [Worktree] class by looking up existing worktree
/// with provided [Repository] object and worktree [name].
///
/// Should be freed with `free()` to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
Worktree.lookup(Repository repo, String name) {
_worktreePointer = bindings.lookup(repo.pointer, name);
}
/// Pointer to memory address for allocated branch object.
late final Pointer<git_worktree> _worktreePointer;
/// Returns list of names of linked working trees.
///
/// Throws a [LibGit2Error] if error occured.
static List<String> list(Repository repo) => bindings.list(repo.pointer);
/// Returns the name of the worktree.
String get name => bindings.name(_worktreePointer);
/// Returns the filesystem path for the worktree.
String get path => bindings.path(_worktreePointer);
/// Prunes working tree.
///
/// Prune the working tree, that is remove the git data structures on disk.
///
/// Throws a [LibGit2Error] if error occured.
void prune() => bindings.prune(_worktreePointer);
/// Releases memory allocated for worktree object.
void free() => bindings.free(_worktreePointer);
}