From 6a08a7b803385a14aef5c93f8ab3dab9a2f52c91 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Thu, 17 Jun 2021 17:01:10 +0300 Subject: [PATCH] feat(config): add ability to get value(s) of multivar variable --- lib/src/bindings/config.dart | 36 ++++++++++++++++++++++++++++++++---- lib/src/config.dart | 8 ++++++++ test/config_test.dart | 30 ++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/lib/src/bindings/config.dart b/lib/src/bindings/config.dart index 8c9135c..613a414 100644 --- a/lib/src/bindings/config.dart +++ b/lib/src/bindings/config.dart @@ -247,8 +247,36 @@ void deleteVariable(Pointer 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 getMultivar( + Pointer cfg, + String name, + String? regexp, +) { + final nameC = name.toNativeUtf8().cast(); + final regexpC = regexp?.toNativeUtf8().cast() ?? nullptr; + final iterator = calloc>(); + final entry = calloc>(); + libgit2.git_config_multivar_iterator_new(iterator, cfg, nameC, regexpC); + var error = 0; + final entries = []; -/// Multivars variables get/set -// TODO \ No newline at end of file + while (error == 0) { + error = libgit2.git_config_next(entry, iterator.value); + if (error != -31) { + entries.add(entry.value.ref.value.cast().toDartString()); + } else { + break; + } + } + + calloc.free(nameC); + calloc.free(regexpC); + calloc.free(iterator); + calloc.free(entry); + + return entries; +} diff --git a/lib/src/config.dart b/lib/src/config.dart index 30f43c7..e104d0d 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -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 getMultivar(String key, {String? regexp}) { + return config.getMultivar(configPointer.value, key, regexp); + } + /// Releases memory allocated for config object. void close() { calloc.free(configPointer); diff --git a/test/config_test.dart b/test/config_test.dart index 156eda7..0969076 100644 --- a/test/config_test.dart +++ b/test/config_test.dart @@ -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()), ); }); + + 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'], + ); + }); }); ; }