feat(repository): add ability to get repository config

This commit is contained in:
Aleksey Kulikov 2021-08-12 20:00:25 +03:00
parent 0194d7c361
commit 594aca6474
7 changed files with 145 additions and 54 deletions

View file

@ -7,9 +7,13 @@ import 'util.dart';
/// [Config] provides management of global configuration options
/// (system, global, XDG, excluding repository config)
class Config {
Config();
/// Initializes a new instance of [Config] class from provided
/// pointer to config object in memory.
Config(this._configPointer) {
libgit2.git_libgit2_init();
}
/// Initializes a new instance of [Config] class.
/// Initializes a new instance of [Config] class from provided [path].
///
/// If [path] isn't provided, opens global, XDG and system config files.
///
@ -17,14 +21,14 @@ class Config {
/// Git config file following the default Git config syntax (see man git-config).
///
/// [Config] object should be closed with [free] function to release allocated memory.
Config.open({this.path}) {
Config.open([String? path]) {
libgit2.git_libgit2_init();
if (path == null) {
_configPointer = bindings.openDefault().value;
_configPointer = bindings.openDefault();
} else {
if (File(path!).existsSync()) {
_configPointer = bindings.open(path!).value;
if (File(path).existsSync()) {
_configPointer = bindings.open(path);
} else {
throw Exception('File not found');
}
@ -39,30 +43,20 @@ class Config {
Config.system() {
libgit2.git_libgit2_init();
try {
final systemPath = bindings.findSystem();
_configPointer = bindings.open(systemPath).value;
} catch (e) {
_configPointer = nullptr;
rethrow;
}
final systemPath = bindings.findSystem();
_configPointer = bindings.open(systemPath);
}
/// Initializes a new instance of [Config] class.
///
/// Opens the global configuration file.
///
/// Throws a [LibGit2Error] if error occured.
/// Throws an error if file has not been found.
Config.global() {
libgit2.git_libgit2_init();
try {
final globalPath = bindings.findGlobal();
_configPointer = bindings.open(globalPath).value;
} catch (e) {
_configPointer = nullptr;
rethrow;
}
final globalPath = bindings.findGlobal();
_configPointer = bindings.open(globalPath);
}
/// Initializes a new instance of [Config] class.
@ -73,21 +67,22 @@ class Config {
Config.xdg() {
libgit2.git_libgit2_init();
try {
final xdgPath = bindings.findXdg();
_configPointer = bindings.open(xdgPath).value;
} catch (e) {
_configPointer = nullptr;
rethrow;
}
final xdgPath = bindings.findXdg();
_configPointer = bindings.open(xdgPath);
}
/// Path to on-disk config file provided by user.
String? path;
/// Pointer to memory address for allocated config object.
late final Pointer<git_config> _configPointer;
/// Create a snapshot of the configuration.
///
/// Create a snapshot of the current state of a configuration, which allows you to look
/// into a consistent view of the configuration for looking up complex values
/// (e.g. a remote, submodule).
///
/// Throws a [LibGit2Error] if error occured.
Config get snapshot => Config(bindings.snapshot(_configPointer));
/// Returns map of all the config variables and their values.
Map<String, String> getEntries() {
return bindings.getEntries(_configPointer);