diff --git a/lib/src/bindings/config.dart b/lib/src/bindings/config.dart index 613a414..8497cdf 100644 --- a/lib/src/bindings/config.dart +++ b/lib/src/bindings/config.dart @@ -280,3 +280,23 @@ List getMultivar( return entries; } + +/// Set the value of a multivar config variable in the config file with the +/// highest level (usually the local one). +/// +/// The regular expression is applied case-sensitively on the value. +void setMultivar( + Pointer cfg, + String name, + String regexp, + String value, +) { + final nameC = name.toNativeUtf8().cast(); + final regexpC = regexp.toNativeUtf8().cast(); + final valueC = value.toNativeUtf8().cast(); + libgit2.git_config_set_multivar(cfg, nameC, regexpC, valueC); + + calloc.free(nameC); + calloc.free(regexpC); + calloc.free(valueC); +} diff --git a/lib/src/config.dart b/lib/src/config.dart index e104d0d..7bff5d4 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -113,7 +113,7 @@ class Config { /// Map of key/value entries from config file. Map variables = {}; - /// Sets value of config key + /// Sets the [value] of config [key] void setVariable(String key, dynamic value) { try { if (value.runtimeType == bool) { @@ -150,6 +150,15 @@ class Config { return config.getMultivar(configPointer.value, key, regexp); } + /// Sets the [value] of a multivar [key] in the config file with the + /// highest level (usually the local one). + /// + /// The [regexp] is applied case-sensitively on the value. + /// Empty [regexp] sets [value] for all values of a multivar [key] + void setMultivar(String key, String regexp, String value) { + config.setMultivar(configPointer.value, key, regexp, value); + } + /// Releases memory allocated for config object. void close() { calloc.free(configPointer); diff --git a/test/config_test.dart b/test/config_test.dart index 0969076..0e0f159 100644 --- a/test/config_test.dart +++ b/test/config_test.dart @@ -10,12 +10,12 @@ void main() { const configFileName = 'test_config'; const contents = ''' [core] - repositoryformatversion = 0 - bare = false - gitproxy = proxy-command for kernel.org - gitproxy = default-proxy +\trepositoryformatversion = 0 +\tbare = false +\tgitproxy = proxy-command for kernel.org +\tgitproxy = default-proxy [remote "origin"] - url = someurl +\turl = someurl '''; late Config config; @@ -90,6 +90,22 @@ void main() { ['proxy-command for kernel.org'], ); }); + + test('sets value of multivar', () { + config.setMultivar('core.gitproxy', 'default', 'updated'); + final multivarValues = config.getMultivar('core.gitproxy'); + expect(multivarValues, isNot(contains('default-proxy'))); + expect(multivarValues, contains('updated')); + }); + + test('sets value for all multivar values when regexp is empty', () { + config.setMultivar('core.gitproxy', '', 'updated'); + final multivarValues = config.getMultivar('core.gitproxy'); + expect(multivarValues, isNot(contains('default-proxy'))); + expect(multivarValues, isNot(contains('proxy-command for kernel.org'))); + expect(multivarValues, contains('updated')); + expect(multivarValues.length, equals(2)); + }); }); ; }