feat(config): add ability to delete variable

This commit is contained in:
Aleksey Kulikov 2021-06-16 17:12:32 +03:00
parent 2cdcccefc9
commit 4988b295a5
3 changed files with 40 additions and 3 deletions

View file

@ -233,11 +233,22 @@ Map<String, dynamic> getVariables(Pointer<git_config> cfg) {
return entries;
}
/// Delete a config variable from the config file with the highest level
/// (usually the local one).
///
/// Throws a [LibGit2Error] if error occured.
void deleteVariable(Pointer<git_config> cfg, String name) {
final nameC = name.toNativeUtf8().cast<Int8>();
final error = libgit2.git_config_delete_entry(cfg, nameC);
calloc.free(nameC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Iterate over the values of multivar
// TODO
/// Multivars variables get/set
// TODO
/// Deletes a config variable
// TODO

View file

@ -129,6 +129,19 @@ class Config {
}
}
/// Deletes variable from the config file with the highest level
/// (usually the local one).
///
/// Throws a [LibGit2Error] if error occured.
void deleteVariable(String key) {
try {
config.deleteVariable(configPointer.value, key);
variables = config.getVariables(configPointer.value);
} catch (e) {
rethrow;
}
}
/// Releases memory allocated for config object.
void close() {
calloc.free(configPointer);

View file

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:test/test.dart';
import 'package:libgit2dart/src/util.dart';
import 'package:libgit2dart/src/config.dart';
import 'package:libgit2dart/src/error.dart';
void main() {
final tmpDir = Directory.systemTemp.path;
@ -51,6 +52,18 @@ void main() {
config.setVariable('remote.origin.url', 'updated');
expect(config.variables['remote.origin.url'], equals('updated'));
});
test('deletes variable', () {
config.deleteVariable('core.bare');
expect(config.variables['core.bare'], isNull);
});
test('throws on deleting non existing variable', () {
expect(
() => config.deleteVariable('not.there'),
throwsA(isA<LibGit2Error>()),
);
});
});
;
}