mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
feat(config): add ability to set value for variable
This commit is contained in:
parent
570e5bad52
commit
2cdcccefc9
4 changed files with 40 additions and 63 deletions
|
@ -1,53 +1,42 @@
|
||||||
import 'package:libgit2dart/libgit2dart.dart';
|
import 'package:libgit2dart/libgit2dart.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
final repoConfig = Config(path: '.git/config');
|
// Open system + global config file.
|
||||||
|
final config = Config.open();
|
||||||
|
|
||||||
print('All entries of repo config:');
|
print('All entries of system/global config:');
|
||||||
for (final entry in repoConfig.entries.entries) {
|
// config.variables hold key/value from config file
|
||||||
|
for (final entry in config.variables.entries) {
|
||||||
print('${entry.key}: ${entry.value}');
|
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 {
|
try {
|
||||||
final systemConfig = Config.system();
|
final repoConfig = Config.open(path: '.git/config');
|
||||||
|
|
||||||
print(
|
print('All entries of repo config:');
|
||||||
'\nUser Name from system config: ${systemConfig.entries['user.name']}');
|
for (final entry in repoConfig.variables.entries) {
|
||||||
|
print('${entry.key}: ${entry.value}');
|
||||||
|
}
|
||||||
|
|
||||||
systemConfig.close();
|
repoConfig.close();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('\n$e');
|
print(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Open global config file if there's one.
|
||||||
|
// Exception is thrown if file not found.
|
||||||
try {
|
try {
|
||||||
final globalConfig = Config.global();
|
final globalConfig = Config.global();
|
||||||
|
|
||||||
print(
|
final userName = globalConfig.variables['user.name'];
|
||||||
'\nUser Name from global config: ${globalConfig.entries['user.name']}');
|
print('\nUser Name from global config: $userName');
|
||||||
|
|
||||||
globalConfig.close();
|
globalConfig.close();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('\n$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();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -169,12 +169,6 @@ int getInt(Pointer<git_config> cfg, String variable) {
|
||||||
return result;
|
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
|
/// Set the value of a boolean config variable in the config file with the
|
||||||
/// highest level (usually the local one).
|
/// 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
|
/// Set the value of an integer config variable in the config file with the
|
||||||
/// highest level (usually the local one).
|
/// highest level (usually the local one).
|
||||||
|
///
|
||||||
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
void setInt(Pointer<git_config> cfg, String name, int value) {
|
void setInt(Pointer<git_config> cfg, String name, int value) {
|
||||||
final nameC = name.toNativeUtf8().cast<Int8>();
|
final nameC = name.toNativeUtf8().cast<Int8>();
|
||||||
final error = libgit2.git_config_set_int64(cfg, nameC, value);
|
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
|
/// Set the value of a string config variable in the config file with the
|
||||||
/// highest level (usually the local one).
|
/// highest level (usually the local one).
|
||||||
|
///
|
||||||
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
void setString(Pointer<git_config> cfg, String name, String value) {
|
void setString(Pointer<git_config> cfg, String name, String value) {
|
||||||
final nameC = name.toNativeUtf8().cast<Int8>();
|
final nameC = name.toNativeUtf8().cast<Int8>();
|
||||||
final valueC = value.toNativeUtf8().cast<Int8>();
|
final valueC = value.toNativeUtf8().cast<Int8>();
|
||||||
|
|
|
@ -114,7 +114,7 @@ class Config {
|
||||||
Map<String, dynamic> variables = {};
|
Map<String, dynamic> variables = {};
|
||||||
|
|
||||||
/// Sets value of config key
|
/// Sets value of config key
|
||||||
void setEntry(String key, dynamic value) {
|
void setVariable(String key, dynamic value) {
|
||||||
try {
|
try {
|
||||||
if (value.runtimeType == bool) {
|
if (value.runtimeType == bool) {
|
||||||
config.setBool(configPointer.value, key, value);
|
config.setBool(configPointer.value, key, value);
|
||||||
|
|
|
@ -5,62 +5,52 @@ import 'package:libgit2dart/src/util.dart';
|
||||||
import 'package:libgit2dart/src/config.dart';
|
import 'package:libgit2dart/src/config.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
final tmpDir = Directory.systemTemp.path;
|
||||||
const configFileName = 'test_config';
|
const configFileName = 'test_config';
|
||||||
|
late Config config;
|
||||||
|
|
||||||
group('Config', () {
|
group('Config', () {
|
||||||
setUpAll(() {
|
setUpAll(() {
|
||||||
libgit2.git_libgit2_init();
|
libgit2.git_libgit2_init();
|
||||||
});
|
});
|
||||||
|
|
||||||
setUp(() {
|
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');
|
'[core]\n\trepositoryformatversion = 0\n\tbare = false\n[remote "origin"]\n\turl = someurl');
|
||||||
|
config = Config.open(path: '$tmpDir/$configFileName');
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDown(() {
|
tearDown(() {
|
||||||
File('${Directory.current.path}/test/$configFileName').deleteSync();
|
config.close();
|
||||||
|
File('$tmpDir/$configFileName').deleteSync();
|
||||||
});
|
});
|
||||||
|
|
||||||
tearDownAll(() {
|
tearDownAll(() {
|
||||||
libgit2.git_libgit2_shutdown();
|
libgit2.git_libgit2_shutdown();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('opens file successfully', () {
|
|
||||||
final config = Config();
|
|
||||||
expect(config, isA<Config>());
|
|
||||||
config.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('opens file successfully with provided path', () {
|
test('opens file successfully with provided path', () {
|
||||||
final config = Config(path: 'test/$configFileName');
|
|
||||||
expect(config, isA<Config>());
|
expect(config, isA<Config>());
|
||||||
config.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('gets entries of file', () {
|
test('gets entries of file', () {
|
||||||
final config = Config(path: 'test/$configFileName');
|
expect(config.variables['core.repositoryformatversion'], equals('0'));
|
||||||
expect(config.entries['core.repositoryformatversion'], equals('0'));
|
|
||||||
config.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('sets boolean value for provided key', () {
|
test('sets boolean value for provided key', () {
|
||||||
final config = Config(path: 'test/$configFileName');
|
config.setVariable('core.bare', true);
|
||||||
config.setEntry('core.bare', true);
|
expect(config.variables['core.bare'], equals('true'));
|
||||||
expect(config.entries['core.bare'], equals('true'));
|
|
||||||
config.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('sets integer value for provided key', () {
|
test('sets integer value for provided key', () {
|
||||||
final config = Config(path: 'test/$configFileName');
|
config.setVariable('core.repositoryformatversion', 1);
|
||||||
config.setEntry('core.repositoryformatversion', 1);
|
expect(config.variables['core.repositoryformatversion'], equals('1'));
|
||||||
expect(config.entries['core.repositoryformatversion'], equals('1'));
|
|
||||||
config.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('sets string value for provided key', () {
|
test('sets string value for provided key', () {
|
||||||
final config = Config(path: 'test/$configFileName');
|
config.setVariable('remote.origin.url', 'updated');
|
||||||
config.setEntry('remote.origin.url', 'updated');
|
expect(config.variables['remote.origin.url'], equals('updated'));
|
||||||
expect(config.entries['remote.origin.url'], equals('updated'));
|
|
||||||
config.close();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue