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

This commit is contained in:
Aleksey Kulikov 2021-06-17 17:59:20 +03:00
parent 6a08a7b803
commit 7bea406ab9
3 changed files with 51 additions and 6 deletions

View file

@ -280,3 +280,23 @@ List<String> 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<git_config> cfg,
String name,
String regexp,
String value,
) {
final nameC = name.toNativeUtf8().cast<Int8>();
final regexpC = regexp.toNativeUtf8().cast<Int8>();
final valueC = value.toNativeUtf8().cast<Int8>();
libgit2.git_config_set_multivar(cfg, nameC, regexpC, valueC);
calloc.free(nameC);
calloc.free(regexpC);
calloc.free(valueC);
}

View file

@ -113,7 +113,7 @@ class Config {
/// Map of key/value entries from config file.
Map<String, dynamic> 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);

View file

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