From 5f829dd1ca25ba133e97f1e213c025189ba01d87 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Sat, 18 Jun 2022 11:54:46 +0300 Subject: [PATCH] feat(worktree): add ability to pass options to `prune(...)` API method (#71) --- lib/src/bindings/worktree.dart | 12 ++++++++++-- lib/src/git_types.dart | 15 +++++++++++++++ lib/src/worktree.dart | 11 ++++++++--- test/git_types_test.dart | 10 ++++++++++ test/worktree_test.dart | 16 ++++++++++++++++ 5 files changed, 59 insertions(+), 5 deletions(-) diff --git a/lib/src/bindings/worktree.dart b/lib/src/bindings/worktree.dart index 663c1f2..61923da 100644 --- a/lib/src/bindings/worktree.dart +++ b/lib/src/bindings/worktree.dart @@ -96,8 +96,16 @@ bool isPrunable(Pointer wt) { /// Prune working tree. /// /// Prune the working tree, that is remove the git data structures on disk. -void prune(Pointer wt) { - libgit2.git_worktree_prune(wt, nullptr); +void prune({required Pointer worktreePointer, int? flags}) { + final opts = calloc(); + 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. diff --git a/lib/src/git_types.dart b/lib/src/git_types.dart index 65be8ed..3659324 100644 --- a/lib/src/git_types.dart +++ b/lib/src/git_types.dart @@ -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; +} diff --git a/lib/src/worktree.dart b/lib/src/worktree.dart index 32939c6..d78e7cc 100644 --- a/lib/src/worktree.dart +++ b/lib/src/worktree.dart @@ -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? flags]) { + bindings.prune( + worktreePointer: _worktreePointer, + flags: flags?.fold(0, (acc, e) => acc! | e.value), + ); + } /// Whether worktree is valid. /// diff --git a/test/git_types_test.dart b/test/git_types_test.dart index cdbacaf..68ef591 100644 --- a/test/git_types_test.dart +++ b/test/git_types_test.dart @@ -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); + }); } diff --git a/test/worktree_test.dart b/test/worktree_test.dart index 0da572a..550cf33 100644 --- a/test/worktree_test.dart +++ b/test/worktree_test.dart @@ -151,6 +151,22 @@ void main() { expect(repo.worktrees, []); }); + test('prunes worktree with provided flags', () { + expect(repo.worktrees, []); + + 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, []); + }); + test('throws when trying get list of worktrees and error occurs', () { expect( () => Worktree.list(Repository(nullptr)),