mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 20:29:08 -04:00
feat(config): add ability to get values of all variables
This commit is contained in:
parent
f6b8cc7005
commit
2d1c026e73
4 changed files with 22 additions and 19 deletions
|
@ -5,8 +5,8 @@ void main() {
|
||||||
final config = Config.open();
|
final config = Config.open();
|
||||||
|
|
||||||
print('All entries of system/global config:');
|
print('All entries of system/global config:');
|
||||||
// config.variables hold key/value from config file
|
final entries = config.getEntries();
|
||||||
for (final entry in config.variables.entries) {
|
for (final entry in entries.entries) {
|
||||||
print('${entry.key}: ${entry.value}');
|
print('${entry.key}: ${entry.value}');
|
||||||
}
|
}
|
||||||
// .close should be called on object to free memory when done.
|
// .close should be called on object to free memory when done.
|
||||||
|
@ -18,7 +18,8 @@ void main() {
|
||||||
final repoConfig = Config.open(path: '.git/config');
|
final repoConfig = Config.open(path: '.git/config');
|
||||||
|
|
||||||
print('All entries of repo config:');
|
print('All entries of repo config:');
|
||||||
for (final entry in repoConfig.variables.entries) {
|
final entries = repoConfig.getEntries();
|
||||||
|
for (final entry in entries.entries) {
|
||||||
print('${entry.key}: ${entry.value}');
|
print('${entry.key}: ${entry.value}');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +33,8 @@ void main() {
|
||||||
try {
|
try {
|
||||||
final globalConfig = Config.global();
|
final globalConfig = Config.global();
|
||||||
|
|
||||||
final userName = globalConfig.variables['user.name'];
|
// get value of config variable
|
||||||
|
final userName = globalConfig.getValue('user.name');
|
||||||
print('\nUser Name from global config: $userName');
|
print('\nUser Name from global config: $userName');
|
||||||
|
|
||||||
globalConfig.close();
|
globalConfig.close();
|
||||||
|
|
|
@ -182,12 +182,12 @@ void setString(Pointer<git_config> cfg, String name, String value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate over all the config variables.
|
/// Iterate over all the config variables.
|
||||||
Map<String, dynamic> getVariables(Pointer<git_config> cfg) {
|
Map<String, String> getEntries(Pointer<git_config> cfg) {
|
||||||
final iterator = calloc<Pointer<git_config_iterator>>();
|
final iterator = calloc<Pointer<git_config_iterator>>();
|
||||||
final entry = calloc<Pointer<git_config_entry>>();
|
final entry = calloc<Pointer<git_config_entry>>();
|
||||||
libgit2.git_config_iterator_new(iterator, cfg);
|
libgit2.git_config_iterator_new(iterator, cfg);
|
||||||
var error = 0;
|
var error = 0;
|
||||||
final entries = <String, dynamic>{};
|
final entries = <String, String>{};
|
||||||
|
|
||||||
while (error != -31) {
|
while (error != -31) {
|
||||||
error = libgit2.git_config_next(entry, iterator.value);
|
error = libgit2.git_config_next(entry, iterator.value);
|
||||||
|
|
|
@ -39,8 +39,6 @@ class Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
variables = config.getVariables(configPointer.value);
|
|
||||||
|
|
||||||
libgit2.git_libgit2_shutdown();
|
libgit2.git_libgit2_shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +53,6 @@ class Config {
|
||||||
try {
|
try {
|
||||||
final systemPath = config.findSystem();
|
final systemPath = config.findSystem();
|
||||||
configPointer = config.open(systemPath);
|
configPointer = config.open(systemPath);
|
||||||
variables = config.getVariables(configPointer.value);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
configPointer = nullptr;
|
configPointer = nullptr;
|
||||||
rethrow;
|
rethrow;
|
||||||
|
@ -75,7 +72,6 @@ class Config {
|
||||||
try {
|
try {
|
||||||
final globalPath = config.findGlobal();
|
final globalPath = config.findGlobal();
|
||||||
configPointer = config.open(globalPath);
|
configPointer = config.open(globalPath);
|
||||||
variables = config.getVariables(configPointer.value);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
configPointer = nullptr;
|
configPointer = nullptr;
|
||||||
rethrow;
|
rethrow;
|
||||||
|
@ -95,7 +91,6 @@ class Config {
|
||||||
try {
|
try {
|
||||||
final xdgPath = config.findXdg();
|
final xdgPath = config.findXdg();
|
||||||
configPointer = config.open(xdgPath);
|
configPointer = config.open(xdgPath);
|
||||||
variables = config.getVariables(configPointer.value);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
configPointer = nullptr;
|
configPointer = nullptr;
|
||||||
rethrow;
|
rethrow;
|
||||||
|
@ -110,8 +105,10 @@ 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;
|
||||||
|
|
||||||
/// Map of key/value entries from config file.
|
/// Returns map of all the config variables and their values
|
||||||
Map<String, dynamic> variables = {};
|
Map<String, String> getEntries() {
|
||||||
|
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) {
|
||||||
|
@ -132,7 +129,6 @@ class Config {
|
||||||
} else {
|
} else {
|
||||||
config.setString(configPointer.value, variable, value);
|
config.setString(configPointer.value, variable, value);
|
||||||
}
|
}
|
||||||
variables = config.getVariables(configPointer.value);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
|
@ -145,7 +141,6 @@ class Config {
|
||||||
void deleteVariable(String key) {
|
void deleteVariable(String key) {
|
||||||
try {
|
try {
|
||||||
config.deleteVariable(configPointer.value, key);
|
config.deleteVariable(configPointer.value, key);
|
||||||
variables = config.getVariables(configPointer.value);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,11 @@ void main() {
|
||||||
expect(config, isA<Config>());
|
expect(config, isA<Config>());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('getEntries() returns map with variables and values', () {
|
||||||
|
final entries = config.getEntries();
|
||||||
|
expect(entries['remote.origin.url'], equals('someurl'));
|
||||||
|
});
|
||||||
|
|
||||||
group('getValue()', () {
|
group('getValue()', () {
|
||||||
test('returns value of variable', () {
|
test('returns value of variable', () {
|
||||||
expect(config.getValue('core.bare'), equals('false'));
|
expect(config.getValue('core.bare'), equals('false'));
|
||||||
|
@ -59,23 +64,24 @@ void main() {
|
||||||
group('setValue()', () {
|
group('setValue()', () {
|
||||||
test('sets boolean value for provided variable', () {
|
test('sets boolean value for provided variable', () {
|
||||||
config.setValue('core.bare', true);
|
config.setValue('core.bare', true);
|
||||||
expect(config.variables['core.bare'], equals('true'));
|
expect(config.getValue('core.bare'), equals('true'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('sets integer value for provided variable', () {
|
test('sets integer value for provided variable', () {
|
||||||
config.setValue('core.repositoryformatversion', 1);
|
config.setValue('core.repositoryformatversion', 1);
|
||||||
expect(config.variables['core.repositoryformatversion'], equals('1'));
|
expect(config.getValue('core.repositoryformatversion'), equals('1'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('sets string value for provided variable', () {
|
test('sets string value for provided variable', () {
|
||||||
config.setValue('remote.origin.url', 'updated');
|
config.setValue('remote.origin.url', 'updated');
|
||||||
expect(config.variables['remote.origin.url'], equals('updated'));
|
expect(config.getValue('remote.origin.url'), equals('updated'));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('deletes variable', () {
|
test('deletes variable', () {
|
||||||
config.deleteVariable('core.bare');
|
config.deleteVariable('core.bare');
|
||||||
expect(config.variables['core.bare'], isNull);
|
final entries = config.getEntries();
|
||||||
|
expect(entries['core.bare'], isNull);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('throws on deleting non existing variable', () {
|
test('throws on deleting non existing variable', () {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue