From bfe2d118b25bef4bb82666bec349a3aa4ca67157 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Sat, 4 Dec 2021 16:02:15 +0300 Subject: [PATCH] feat(reset): add binding and api method for git_reset_default --- lib/src/bindings/reset.dart | 42 +++++++++++++++++++++++++++++++++++++ lib/src/repository.dart | 20 ++++++++++++++++++ test/reset_test.dart | 27 ++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/lib/src/bindings/reset.dart b/lib/src/bindings/reset.dart index d21a758..3f57a24 100644 --- a/lib/src/bindings/reset.dart +++ b/lib/src/bindings/reset.dart @@ -1,6 +1,8 @@ import 'dart:ffi'; +import 'package:ffi/ffi.dart'; import 'package:libgit2dart/src/bindings/libgit2_bindings.dart'; +import 'package:libgit2dart/src/error.dart'; import 'package:libgit2dart/src/util.dart'; /// Sets the current head to the specified commit oid and optionally resets the @@ -22,3 +24,43 @@ void reset({ }) { libgit2.git_reset(repoPointer, targetPointer, resetType, checkoutOptsPointer); } + +/// 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. +/// +/// Throws a [LibGit2Error] if error occured. +void resetDefault({ + required Pointer repoPointer, + required Pointer targetPointer, + required List pathspec, +}) { + final pathspecC = calloc(); + final pathPointers = + pathspec.map((e) => e.toNativeUtf8().cast()).toList(); + final strArray = calloc>(pathspec.length); + + for (var i = 0; i < pathspec.length; i++) { + strArray[i] = pathPointers[i]; + } + + pathspecC.ref.strings = strArray; + pathspecC.ref.count = pathspec.length; + + final error = libgit2.git_reset_default( + repoPointer, + targetPointer, + pathspecC, + ); + + calloc.free(pathspecC); + for (final p in pathPointers) { + calloc.free(p); + } + calloc.free(strArray); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } +} diff --git a/lib/src/repository.dart b/lib/src/repository.dart index 93558eb..3ccd1d2 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -1227,6 +1227,26 @@ class Repository { object_bindings.free(object); } + /// Updates some entries in the index from the [oid] commit tree. + /// + /// The scope of the updated entries is determined by the paths being passed + /// in the [pathspec]. + /// + /// 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, + ); + + reset_bindings.resetDefault( + repoPointer: _repoPointer, + targetPointer: object, + pathspec: pathspec, + ); + } + /// Returns a [Diff] with changes between the trees, tree and index, tree and /// workdir or index and workdir. /// diff --git a/test/reset_test.dart b/test/reset_test.dart index 5231fcc..4d5b3b3 100644 --- a/test/reset_test.dart +++ b/test/reset_test.dart @@ -61,5 +61,32 @@ void main() { index.free(); }); + + group('resetDefault', () { + test('successfully updates entry in the index', () { + file.writeAsStringSync('new edit'); + + final index = repo.index; + index.add('feature_file'); + expect(repo.status['feature_file'], {GitStatus.indexModified}); + + final head = repo.head; + repo.resetDefault(oid: head.target, pathspec: ['feature_file']); + expect(repo.status['feature_file'], {GitStatus.wtModified}); + + head.free(); + index.free(); + }); + + test('throws when pathspec list is empty', () { + final head = repo.head; + expect( + () => repo.resetDefault(oid: head.target, pathspec: []), + throwsA(isA()), + ); + + head.free(); + }); + }); }); }