From 664e0688b2ecd24472cdc2a5a95a2c7277c11453 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Wed, 18 May 2022 15:40:40 +0300 Subject: [PATCH] feat(reset): add ability to remove entries in index --- lib/src/bindings/reset.dart | 9 ++++++--- lib/src/repository.dart | 18 ++++++++++++------ test/reset_test.dart | 11 +++++++++++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lib/src/bindings/reset.dart b/lib/src/bindings/reset.dart index 3f57a24..dbe59f3 100644 --- a/lib/src/bindings/reset.dart +++ b/lib/src/bindings/reset.dart @@ -28,12 +28,15 @@ void reset({ /// Updates some entries in the index from the target commit tree. /// /// The scope of the updated entries is determined by the paths being passed in -/// the pathspec parameters. +/// the [pathspec] parameters. +/// +/// Passing a null [targetPointer] will result in removing entries in the index +/// matching the provided [pathspec]s. /// /// Throws a [LibGit2Error] if error occured. void resetDefault({ required Pointer repoPointer, - required Pointer targetPointer, + required Pointer? targetPointer, required List pathspec, }) { final pathspecC = calloc(); @@ -50,7 +53,7 @@ void resetDefault({ final error = libgit2.git_reset_default( repoPointer, - targetPointer, + targetPointer ?? nullptr, pathspecC, ); diff --git a/lib/src/repository.dart b/lib/src/repository.dart index 52e21c7..2cc9f04 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -578,13 +578,19 @@ class Repository extends Equatable { /// The scope of the updated entries is determined by the paths being passed /// in the [pathspec]. /// + /// Passing a null [oid] will result in removing entries in the index + /// matching the provided [pathspec]s. + /// /// Throws a [LibGit2Error] if error occured. - void resetDefault({required Oid oid, required List pathspec}) { - final object = object_bindings.lookup( - repoPointer: _repoPointer, - oidPointer: oid.pointer, - type: GitObject.commit.value, - ); + void resetDefault({required Oid? oid, required List pathspec}) { + Pointer? object; + if (oid != null) { + object = object_bindings.lookup( + repoPointer: _repoPointer, + oidPointer: oid.pointer, + type: GitObject.commit.value, + ); + } reset_bindings.resetDefault( repoPointer: _repoPointer, diff --git a/test/reset_test.dart b/test/reset_test.dart index 2e477cb..96366d6 100644 --- a/test/reset_test.dart +++ b/test/reset_test.dart @@ -61,6 +61,17 @@ void main() { expect(repo.status['feature_file'], {GitStatus.wtModified}); }); + test('removes entry in the index when null oid is provided', () { + const fileName = 'new_file.txt'; + File(p.join(tmpDir.path, fileName)).createSync(); + + repo.index.add(fileName); + expect(repo.status[fileName], {GitStatus.indexNew}); + + repo.resetDefault(oid: null, pathspec: [fileName]); + expect(repo.status[fileName], {GitStatus.wtNew}); + }); + test('throws when pathspec list is empty', () { expect( () => repo.resetDefault(oid: repo.head.target, pathspec: []),