From aa073c531e2672fbf393d6690bc1ae594c574e0f Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Fri, 17 Jun 2022 14:52:46 +0300 Subject: [PATCH] feat(index): add ability to pass options to `addAll(...)` API method (#68) --- lib/src/bindings/index.dart | 3 ++- lib/src/git_types.dart | 19 +++++++++++++++++++ lib/src/index.dart | 13 +++++++++++-- test/git_types_test.dart | 11 +++++++++++ test/index_test.dart | 5 ++++- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/src/bindings/index.dart b/lib/src/bindings/index.dart index c7e1d06..5ae8db9 100644 --- a/lib/src/bindings/index.dart +++ b/lib/src/bindings/index.dart @@ -292,6 +292,7 @@ void addFromBuffer({ void addAll({ required Pointer indexPointer, required List pathspec, + required int flags, }) { final pathspecC = calloc(); final pathPointers = pathspec.map((e) => e.toChar()).toList(); @@ -307,7 +308,7 @@ void addAll({ final error = libgit2.git_index_add_all( indexPointer, pathspecC, - 0, + flags, nullptr, nullptr, ); diff --git a/lib/src/git_types.dart b/lib/src/git_types.dart index 564e960..65be8ed 100644 --- a/lib/src/git_types.dart +++ b/lib/src/git_types.dart @@ -1173,3 +1173,22 @@ enum GitBlobFilter { const GitBlobFilter(this.value); final int value; } + +/// Flags for APIs that add files matching pathspec. +enum GitIndexAddOption { + defaults(0), + + /// Skip the checking of ignore rules. + force(1), + + /// Disable glob expansion and force exact matching of files in working + /// directory. + disablePathspecMatch(2), + + /// Check that each entry in the pathspec is an exact match to a filename on + /// disk is either not ignored or already in the index. + checkPathspec(4); + + const GitIndexAddOption(this.value); + final int value; +} diff --git a/lib/src/index.dart b/lib/src/index.dart index 63edaad..96ff8f8 100644 --- a/lib/src/index.dart +++ b/lib/src/index.dart @@ -215,9 +215,18 @@ class Index with IterableMixin { /// that matches will be added to the index (either updating an existing /// entry or adding a new entry). /// + /// [flags] is optional combination of [GitIndexAddOption] flags. + /// /// Throws a [LibGit2Error] if error occured. - void addAll(List pathspec) { - bindings.addAll(indexPointer: _indexPointer, pathspec: pathspec); + void addAll( + List pathspec, { + Set flags = const {GitIndexAddOption.defaults}, + }) { + bindings.addAll( + indexPointer: _indexPointer, + pathspec: pathspec, + flags: flags.fold(0, (acc, e) => acc | e.value), + ); } /// Updates all index entries to match the working directory. diff --git a/test/git_types_test.dart b/test/git_types_test.dart index c0d81e9..cdbacaf 100644 --- a/test/git_types_test.dart +++ b/test/git_types_test.dart @@ -551,4 +551,15 @@ void main() { expect(actual, expected); }); }); + + test('GitIndexAddOption returns correct values', () { + const expected = { + GitIndexAddOption.defaults: 0, + GitIndexAddOption.force: 1, + GitIndexAddOption.disablePathspecMatch: 2, + GitIndexAddOption.checkPathspec: 4, + }; + final actual = {for (final e in GitIndexAddOption.values) e: e.value}; + expect(actual, expected); + }); } diff --git a/test/index_test.dart b/test/index_test.dart index 9a496a1..a681ad4 100644 --- a/test/index_test.dart +++ b/test/index_test.dart @@ -175,7 +175,10 @@ void main() { group('addAll()', () { test('adds with provided pathspec', () { index.clear(); - index.addAll(['file', 'feature_file']); + index.addAll( + ['file', 'feature_file'], + flags: {GitIndexAddOption.checkPathspec, GitIndexAddOption.force}, + ); expect(index.length, 2); expect(index['file'].oid.sha, fileSha);