feat(config): add ability to delete multivar

This commit is contained in:
Aleksey Kulikov 2021-06-18 17:58:44 +03:00
parent dadd235b66
commit 36f874c4a6
3 changed files with 63 additions and 5 deletions

View file

@ -251,7 +251,7 @@ List<String> getMultivarValue(
/// Set the value of a multivar config variable in the config file with the /// Set the value of a multivar config variable in the config file with the
/// highest level (usually the local one). /// highest level (usually the local one).
/// ///
/// The regular expression is applied case-sensitively on the value. /// The regexp is applied case-sensitively on the value.
void setMultivarValue( void setMultivarValue(
Pointer<git_config> cfg, Pointer<git_config> cfg,
String variable, String variable,
@ -267,3 +267,20 @@ void setMultivarValue(
calloc.free(regexpC); calloc.free(regexpC);
calloc.free(valueC); calloc.free(valueC);
} }
/// Deletes one or several values from a multivar in the config file
/// with the highest level (usually the local one).
///
/// The regexp is applied case-sensitively on the value.
void deleteMultivar(
Pointer<git_config> cfg,
String variable,
String regexp,
) {
final name = variable.toNativeUtf8().cast<Int8>();
final regexpC = regexp.toNativeUtf8().cast<Int8>();
libgit2.git_config_delete_multivar(cfg, name, regexpC);
calloc.free(name);
calloc.free(regexpC);
}

View file

@ -105,12 +105,12 @@ class Config {
/// Pointer to memory address for allocated config object. /// Pointer to memory address for allocated config object.
late Pointer<Pointer<git_config>> configPointer; late Pointer<Pointer<git_config>> configPointer;
/// Returns map of all the config variables and their values /// Returns map of all the config variables and their values.
Map<String, String> getEntries() { Map<String, String> getEntries() {
return config.getEntries(configPointer.value); return config.getEntries(configPointer.value);
} }
/// Returns the value of config [variable] /// Returns the value of config [variable].
String getValue(String variable) { String getValue(String variable) {
try { try {
return config.getValue(configPointer.value, variable); return config.getValue(configPointer.value, variable);
@ -119,7 +119,7 @@ class Config {
} }
} }
/// Sets the [value] of config [variable] /// Sets the [value] of config [variable].
void setValue(String variable, dynamic value) { void setValue(String variable, dynamic value) {
try { try {
if (value.runtimeType == bool) { if (value.runtimeType == bool) {
@ -158,11 +158,20 @@ class Config {
/// highest level (usually the local one). /// highest level (usually the local one).
/// ///
/// The [regexp] is applied case-sensitively on the value. /// The [regexp] is applied case-sensitively on the value.
/// Empty [regexp] sets [value] for all values of a multivar [variable] /// Empty [regexp] sets [value] for all values of a multivar [variable].
void setMultivarValue(String variable, String regexp, String value) { void setMultivarValue(String variable, String regexp, String value) {
config.setMultivarValue(configPointer.value, variable, regexp, value); config.setMultivarValue(configPointer.value, variable, regexp, value);
} }
/// Deletes one or several values from a multivar [variable] in the config file
/// with the highest level (usually the local one).
///
/// The [regexp] is applied case-sensitively on the value.
/// Empty [regexp] deletes all values of a multivar [variable].
void deleteMultivar(String variable, String regexp) {
config.deleteMultivar(configPointer.value, variable, regexp);
}
/// Releases memory allocated for config object. /// Releases memory allocated for config object.
void close() { void close() {
calloc.free(configPointer); calloc.free(configPointer);

View file

@ -80,6 +80,7 @@ void main() {
group('deleteEntry()', () { group('deleteEntry()', () {
test('successfully deletes entry', () { test('successfully deletes entry', () {
expect(config.getValue('core.bare'), equals('false'));
config.deleteEntry('core.bare'); config.deleteEntry('core.bare');
final entries = config.getEntries(); final entries = config.getEntries();
expect(entries['core.bare'], isNull); expect(entries['core.bare'], isNull);
@ -133,6 +134,37 @@ void main() {
expect(multivarValues.length, equals(2)); expect(multivarValues.length, equals(2));
}); });
}); });
group('deleteMultivar()', () {
test('successfully deletes value of a multivar', () {
expect(
config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'),
['proxy-command for kernel.org'],
);
config.deleteMultivar('core.gitproxy', 'for kernel.org\$');
expect(
config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'),
[],
);
});
test('successfully deletes all values of a multivar when regexp is empty',
() {
expect(
config.getMultivarValue('core.gitproxy'),
[
'proxy-command for kernel.org',
'default-proxy',
],
);
config.deleteMultivar('core.gitproxy', '');
expect(config.getMultivarValue('core.gitproxy'), []);
});
});
}); });
; ;
} }