feat(config): add ability to set value for variable

This commit is contained in:
Aleksey Kulikov 2021-06-16 16:48:28 +03:00
parent 570e5bad52
commit 2cdcccefc9
4 changed files with 40 additions and 63 deletions

View file

@ -1,53 +1,42 @@
import 'package:libgit2dart/libgit2dart.dart';
void main() {
final repoConfig = Config(path: '.git/config');
// Open system + global config file.
final config = Config.open();
print('All entries of repo config:');
for (final entry in repoConfig.entries.entries) {
print('All entries of system/global config:');
// config.variables hold key/value from config file
for (final entry in config.variables.entries) {
print('${entry.key}: ${entry.value}');
}
// .close should be called on object to free memory when done.
config.close();
repoConfig.close();
// Open config file at provided path.
// Exception is thrown if file not found.
try {
final systemConfig = Config.system();
final repoConfig = Config.open(path: '.git/config');
print(
'\nUser Name from system config: ${systemConfig.entries['user.name']}');
print('All entries of repo config:');
for (final entry in repoConfig.variables.entries) {
print('${entry.key}: ${entry.value}');
}
systemConfig.close();
repoConfig.close();
} catch (e) {
print('\n$e');
print(e);
}
// Open global config file if there's one.
// Exception is thrown if file not found.
try {
final globalConfig = Config.global();
print(
'\nUser Name from global config: ${globalConfig.entries['user.name']}');
final userName = globalConfig.variables['user.name'];
print('\nUser Name from global config: $userName');
globalConfig.close();
} catch (e) {
print('\n$e');
}
try {
final xdgConfig = Config.xdg();
print('\nAll entries of repo config:');
print('\nUser Name from xdg config: ${xdgConfig.entries['user.name']}');
xdgConfig.close();
} catch (e) {
print('\n$e');
}
final config = Config();
print('\nAll entries of system/global config:');
for (final entry in config.entries.entries) {
print('${entry.key}: ${entry.value}');
}
config.close();
}

View file

@ -169,12 +169,6 @@ int getInt(Pointer<git_config> cfg, String variable) {
return result;
}
/// Get the value of a config variable and parse it as a string.
String getString(Pointer<git_config> cfg, String variable) {
final value = getConfigValue(cfg, variable);
return value.cast<Utf8>().toDartString();
}
/// Set the value of a boolean config variable in the config file with the
/// highest level (usually the local one).
///
@ -192,6 +186,8 @@ void setBool(Pointer<git_config> cfg, String name, bool value) {
/// Set the value of an integer config variable in the config file with the
/// highest level (usually the local one).
///
/// Throws a [LibGit2Error] if error occured.
void setInt(Pointer<git_config> cfg, String name, int value) {
final nameC = name.toNativeUtf8().cast<Int8>();
final error = libgit2.git_config_set_int64(cfg, nameC, value);
@ -204,6 +200,8 @@ void setInt(Pointer<git_config> cfg, String name, int value) {
/// Set the value of a string config variable in the config file with the
/// highest level (usually the local one).
///
/// Throws a [LibGit2Error] if error occured.
void setString(Pointer<git_config> cfg, String name, String value) {
final nameC = name.toNativeUtf8().cast<Int8>();
final valueC = value.toNativeUtf8().cast<Int8>();

View file

@ -114,7 +114,7 @@ class Config {
Map<String, dynamic> variables = {};
/// Sets value of config key
void setEntry(String key, dynamic value) {
void setVariable(String key, dynamic value) {
try {
if (value.runtimeType == bool) {
config.setBool(configPointer.value, key, value);

View file

@ -5,62 +5,52 @@ import 'package:libgit2dart/src/util.dart';
import 'package:libgit2dart/src/config.dart';
void main() {
final tmpDir = Directory.systemTemp.path;
const configFileName = 'test_config';
late Config config;
group('Config', () {
setUpAll(() {
libgit2.git_libgit2_init();
});
setUp(() {
File('${Directory.current.path}/test/$configFileName').writeAsStringSync(
File('$tmpDir/$configFileName').writeAsStringSync(
'[core]\n\trepositoryformatversion = 0\n\tbare = false\n[remote "origin"]\n\turl = someurl');
config = Config.open(path: '$tmpDir/$configFileName');
});
tearDown(() {
File('${Directory.current.path}/test/$configFileName').deleteSync();
config.close();
File('$tmpDir/$configFileName').deleteSync();
});
tearDownAll(() {
libgit2.git_libgit2_shutdown();
});
test('opens file successfully', () {
final config = Config();
expect(config, isA<Config>());
config.close();
});
test('opens file successfully with provided path', () {
final config = Config(path: 'test/$configFileName');
expect(config, isA<Config>());
config.close();
});
test('gets entries of file', () {
final config = Config(path: 'test/$configFileName');
expect(config.entries['core.repositoryformatversion'], equals('0'));
config.close();
expect(config.variables['core.repositoryformatversion'], equals('0'));
});
test('sets boolean value for provided key', () {
final config = Config(path: 'test/$configFileName');
config.setEntry('core.bare', true);
expect(config.entries['core.bare'], equals('true'));
config.close();
config.setVariable('core.bare', true);
expect(config.variables['core.bare'], equals('true'));
});
test('sets integer value for provided key', () {
final config = Config(path: 'test/$configFileName');
config.setEntry('core.repositoryformatversion', 1);
expect(config.entries['core.repositoryformatversion'], equals('1'));
config.close();
config.setVariable('core.repositoryformatversion', 1);
expect(config.variables['core.repositoryformatversion'], equals('1'));
});
test('sets string value for provided key', () {
final config = Config(path: 'test/$configFileName');
config.setEntry('remote.origin.url', 'updated');
expect(config.entries['remote.origin.url'], equals('updated'));
config.close();
config.setVariable('remote.origin.url', 'updated');
expect(config.variables['remote.origin.url'], equals('updated'));
});
});
;
}