feat(config): add ability to get value(s) of multivar variable

This commit is contained in:
Aleksey Kulikov 2021-06-17 17:01:10 +03:00
parent 4988b295a5
commit 6a08a7b803
3 changed files with 68 additions and 6 deletions

View file

@ -247,8 +247,36 @@ void deleteVariable(Pointer<git_config> cfg, String name) {
}
}
/// Iterate over the values of multivar
// TODO
/// Iterate over the values of a multivar
///
/// If regexp is present, then the iterator will only iterate over all
/// values which match the pattern.
List<String> getMultivar(
Pointer<git_config> cfg,
String name,
String? regexp,
) {
final nameC = name.toNativeUtf8().cast<Int8>();
final regexpC = regexp?.toNativeUtf8().cast<Int8>() ?? nullptr;
final iterator = calloc<Pointer<git_config_iterator>>();
final entry = calloc<Pointer<git_config_entry>>();
libgit2.git_config_multivar_iterator_new(iterator, cfg, nameC, regexpC);
var error = 0;
final entries = <String>[];
/// Multivars variables get/set
// TODO
while (error == 0) {
error = libgit2.git_config_next(entry, iterator.value);
if (error != -31) {
entries.add(entry.value.ref.value.cast<Utf8>().toDartString());
} else {
break;
}
}
calloc.free(nameC);
calloc.free(regexpC);
calloc.free(iterator);
calloc.free(entry);
return entries;
}

View file

@ -142,6 +142,14 @@ class Config {
}
}
/// Returns list of values for multivar [key]
///
/// If [regexp] is present, then the iterator will only iterate over all
/// values which match the pattern.
List<String> getMultivar(String key, {String? regexp}) {
return config.getMultivar(configPointer.value, key, regexp);
}
/// Releases memory allocated for config object.
void close() {
calloc.free(configPointer);

View file

@ -8,6 +8,16 @@ import 'package:libgit2dart/src/error.dart';
void main() {
final tmpDir = Directory.systemTemp.path;
const configFileName = 'test_config';
const contents = '''
[core]
repositoryformatversion = 0
bare = false
gitproxy = proxy-command for kernel.org
gitproxy = default-proxy
[remote "origin"]
url = someurl
''';
late Config config;
group('Config', () {
@ -16,8 +26,7 @@ void main() {
});
setUp(() {
File('$tmpDir/$configFileName').writeAsStringSync(
'[core]\n\trepositoryformatversion = 0\n\tbare = false\n[remote "origin"]\n\turl = someurl');
File('$tmpDir/$configFileName').writeAsStringSync(contents);
config = Config.open(path: '$tmpDir/$configFileName');
});
@ -64,6 +73,23 @@ void main() {
throwsA(isA<LibGit2Error>()),
);
});
test('returns values of multivar', () {
expect(
config.getMultivar('core.gitproxy'),
[
'proxy-command for kernel.org',
'default-proxy',
],
);
});
test('returns values of multivar with regexp', () {
expect(
config.getMultivar('core.gitproxy', regexp: 'for kernel.org\$'),
['proxy-command for kernel.org'],
);
});
});
;
}