feat(worktree): add ability to pass options to prune(...) API method (#71)

This commit is contained in:
Aleksey Kulikov 2022-06-18 11:54:46 +03:00 committed by GitHub
parent 2daadaa9a4
commit 5f829dd1ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 5 deletions

View file

@ -96,8 +96,16 @@ bool isPrunable(Pointer<git_worktree> wt) {
/// Prune working tree.
///
/// Prune the working tree, that is remove the git data structures on disk.
void prune(Pointer<git_worktree> wt) {
libgit2.git_worktree_prune(wt, nullptr);
void prune({required Pointer<git_worktree> worktreePointer, int? flags}) {
final opts = calloc<git_worktree_prune_options>();
libgit2.git_worktree_prune_options_init(
opts,
GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
);
if (flags != null) opts.ref.flags = flags;
libgit2.git_worktree_prune(worktreePointer, opts);
}
/// List names of linked working trees.

View file

@ -1192,3 +1192,18 @@ enum GitIndexAddOption {
const GitIndexAddOption(this.value);
final int value;
}
/// Flags to alter working tree pruning behavior.
enum GitWorktree {
/// Prune working tree even if working tree is valid.
pruneValid(1),
/// Prune working tree even if it is locked.
pruneLocked(2),
/// Prune checked out working tree.
pruneWorkingTree(4);
const GitWorktree(this.value);
final int value;
}

View file

@ -77,10 +77,15 @@ class Worktree extends Equatable {
/// Throws a [LibGit2Error] if error occured.
bool get isPrunable => bindings.isPrunable(_worktreePointer);
/// Prunes working tree.
/// Prunes working tree, that is removes the git data structures on disk.
///
/// Prune the working tree, that is remove the git data structures on disk.
void prune() => bindings.prune(_worktreePointer);
/// [flags] is optional combination of [GitWorktree] flags.
void prune([Set<GitWorktree>? flags]) {
bindings.prune(
worktreePointer: _worktreePointer,
flags: flags?.fold(0, (acc, e) => acc! | e.value),
);
}
/// Whether worktree is valid.
///

View file

@ -562,4 +562,14 @@ void main() {
final actual = {for (final e in GitIndexAddOption.values) e: e.value};
expect(actual, expected);
});
test('GitWorktree returns correct values', () {
const expected = {
GitWorktree.pruneValid: 1,
GitWorktree.pruneLocked: 2,
GitWorktree.pruneWorkingTree: 4,
};
final actual = {for (final e in GitWorktree.values) e: e.value};
expect(actual, expected);
});
}

View file

@ -151,6 +151,22 @@ void main() {
expect(repo.worktrees, <String>[]);
});
test('prunes worktree with provided flags', () {
expect(repo.worktrees, <String>[]);
final worktree = Worktree.create(
repo: repo,
name: worktreeName,
path: worktreeDir.path,
);
expect(repo.worktrees, [worktreeName]);
expect(worktree.isPrunable, false);
expect(worktree.isValid, true);
worktree.prune({GitWorktree.pruneValid});
expect(repo.worktrees, <String>[]);
});
test('throws when trying get list of worktrees and error occurs', () {
expect(
() => Worktree.list(Repository(nullptr)),