feat(index): add ability to pass options to addAll(...) API method (#68)

This commit is contained in:
Aleksey Kulikov 2022-06-17 14:52:46 +03:00 committed by GitHub
parent d113af44b5
commit aa073c531e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 4 deletions

View file

@ -292,6 +292,7 @@ void addFromBuffer({
void addAll({
required Pointer<git_index> indexPointer,
required List<String> pathspec,
required int flags,
}) {
final pathspecC = calloc<git_strarray>();
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,
);

View file

@ -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;
}

View file

@ -215,9 +215,18 @@ class Index with IterableMixin<IndexEntry> {
/// 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<String> pathspec) {
bindings.addAll(indexPointer: _indexPointer, pathspec: pathspec);
void addAll(
List<String> pathspec, {
Set<GitIndexAddOption> 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.

View file

@ -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);
});
}

View file

@ -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);