mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 20:29:08 -04:00
feat: add more functions to api
This commit is contained in:
parent
b094d42030
commit
aa5815ff27
2 changed files with 157 additions and 17 deletions
|
@ -174,3 +174,72 @@ 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).
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
void setBool(Pointer<git_config> cfg, String name, bool value) {
|
||||
final nameC = name.toNativeUtf8().cast<Int8>();
|
||||
final valueC = value ? 1 : 0;
|
||||
final error = libgit2.git_config_set_bool(cfg, nameC, valueC);
|
||||
calloc.free(nameC);
|
||||
|
||||
if (error < 0) {
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the value of an integer config variable in the config file with the
|
||||
/// highest level (usually the local one).
|
||||
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);
|
||||
calloc.free(nameC);
|
||||
|
||||
if (error < 0) {
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the value of a string config variable in the config file with the
|
||||
/// highest level (usually the local one).
|
||||
void setString(Pointer<git_config> cfg, String name, String value) {
|
||||
final nameC = name.toNativeUtf8().cast<Int8>();
|
||||
final valueC = value.toNativeUtf8().cast<Int8>();
|
||||
final error = libgit2.git_config_set_string(cfg, nameC, valueC);
|
||||
calloc.free(nameC);
|
||||
calloc.free(valueC);
|
||||
|
||||
if (error < 0) {
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterate over all the config variables.
|
||||
Map<String, dynamic> getEntries(Pointer<git_config> cfg) {
|
||||
final iterator = calloc<Pointer<git_config_iterator>>();
|
||||
final entry = calloc<Pointer<git_config_entry>>();
|
||||
libgit2.git_config_iterator_new(iterator, cfg);
|
||||
var error = 0;
|
||||
final entries = <String, dynamic>{};
|
||||
|
||||
while (error != -31) {
|
||||
error = libgit2.git_config_next(entry, iterator.value);
|
||||
entries[entry.value.ref.name.cast<Utf8>().toDartString()] =
|
||||
entry.value.ref.value.cast<Utf8>().toDartString();
|
||||
}
|
||||
calloc.free(entry);
|
||||
calloc.free(iterator);
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
/// Iterate over the values of multivar
|
||||
// TODO
|
||||
|
||||
/// Multivars variables get/set
|
||||
// TODO
|
||||
|
||||
/// Deletes a config variable
|
||||
// TODO
|
|
@ -19,9 +19,79 @@ class Config {
|
|||
libgit2.git_libgit2_init();
|
||||
|
||||
if (path == null) {
|
||||
configPointer = config.openDefault();
|
||||
try {
|
||||
configPointer = config.openDefault();
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
} else {
|
||||
configPointer = config.open(path!);
|
||||
try {
|
||||
configPointer = config.open(path!);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
entries = config.getEntries(configPointer.value);
|
||||
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
|
||||
/// Initializes a new instance of [Config] class.
|
||||
///
|
||||
/// Opens the system configuration file.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Config.system() {
|
||||
libgit2.git_libgit2_init();
|
||||
|
||||
try {
|
||||
final systemPath = config.findSystem();
|
||||
configPointer = config.open(systemPath);
|
||||
entries = config.getEntries(configPointer.value);
|
||||
} catch (e) {
|
||||
configPointer = nullptr;
|
||||
rethrow;
|
||||
}
|
||||
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
|
||||
/// Initializes a new instance of [Config] class.
|
||||
///
|
||||
/// Opens the global configuration file.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Config.global() {
|
||||
libgit2.git_libgit2_init();
|
||||
|
||||
try {
|
||||
final globalPath = config.findGlobal();
|
||||
configPointer = config.open(globalPath);
|
||||
entries = config.getEntries(configPointer.value);
|
||||
} catch (e) {
|
||||
configPointer = nullptr;
|
||||
rethrow;
|
||||
}
|
||||
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
|
||||
/// Initializes a new instance of [Config] class.
|
||||
///
|
||||
/// Opens the global XDG configuration file.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Config.xdg() {
|
||||
libgit2.git_libgit2_init();
|
||||
|
||||
try {
|
||||
final xdgPath = config.findXdg();
|
||||
configPointer = config.open(xdgPath);
|
||||
entries = config.getEntries(configPointer.value);
|
||||
} catch (e) {
|
||||
configPointer = nullptr;
|
||||
rethrow;
|
||||
}
|
||||
|
||||
libgit2.git_libgit2_shutdown();
|
||||
|
@ -33,24 +103,25 @@ class Config {
|
|||
/// Pointer to memory address for allocated config object.
|
||||
late Pointer<Pointer<git_config>> configPointer;
|
||||
|
||||
/// Get boolean value of `key` [variable]
|
||||
bool getBool(String variable) {
|
||||
return config.getBool(configPointer.value, variable);
|
||||
}
|
||||
/// Map of key/value entries from config file.
|
||||
Map<String, dynamic> entries = {};
|
||||
|
||||
///Get integer value of `key` [variable]
|
||||
int getInt(String variable) {
|
||||
return config.getInt(configPointer.value, variable);
|
||||
/// Sets value of config key
|
||||
void setEntry(String key, dynamic value) {
|
||||
try {
|
||||
if (value.runtimeType == bool) {
|
||||
config.setBool(configPointer.value, key, value);
|
||||
} else if (value.runtimeType == int) {
|
||||
config.setInt(configPointer.value, key, value);
|
||||
} else {
|
||||
config.setString(configPointer.value, key, value);
|
||||
}
|
||||
entries = config.getEntries(configPointer.value);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
///Get string value of `key` [variable]
|
||||
String getString(String variable) {
|
||||
return config.getString(configPointer.value, variable);
|
||||
}
|
||||
|
||||
/// Set value of config key
|
||||
// TODO
|
||||
|
||||
/// Releases memory allocated for config object.
|
||||
void close() {
|
||||
calloc.free(configPointer);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue