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

@ -1,6 +1,14 @@
import 'dart:io';
import 'package:libgit2dart/libgit2dart.dart';
void main() {
import 'helpers.dart';
void main() async {
// Preparing example repository.
final tmpDir = '${Directory.systemTemp.path}/example_repo/';
await prepareRepo(tmpDir);
// Open system + global config file.
final config = Config.open();
@ -15,7 +23,7 @@ void main() {
// Open config file at provided path.
// Exception is thrown if file not found.
try {
final repoConfig = Config.open(path: '.git/config');
final repoConfig = Config.open('$tmpDir/.git/config');
print('\nAll entries of repo config:');
final entries = repoConfig.getEntries();
@ -49,4 +57,7 @@ void main() {
} catch (e) {
print('\n$e');
}
// Removing example repository.
await disposeRepo(tmpDir);
}

View file

@ -10,21 +10,21 @@ import '../util.dart';
/// anything with it.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<Pointer<git_config>> newConfig() {
Pointer<git_config> newConfig() {
final out = calloc<Pointer<git_config>>();
final error = libgit2.git_config_new(out);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
return out;
}
/// Create a new config instance containing a single on-disk file
///
/// Throws a [LibGit2Error] if error occured.
Pointer<Pointer<git_config>> open(String path) {
Pointer<git_config> open(String path) {
final out = calloc<Pointer<git_config>>();
final pathC = path.toNativeUtf8().cast<Int8>();
final error = libgit2.git_config_open_ondisk(out, pathC);
@ -32,9 +32,9 @@ Pointer<Pointer<git_config>> open(String path) {
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
return out;
}
/// Open the global, XDG and system configuration files
@ -44,15 +44,15 @@ Pointer<Pointer<git_config>> open(String path) {
/// be used when accessing default config data outside a repository.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<Pointer<git_config>> openDefault() {
Pointer<git_config> openDefault() {
final out = calloc<Pointer<git_config>>();
final error = libgit2.git_config_open_default(out);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
return out;
}
/// Locate the path to the global configuration file
@ -67,19 +67,19 @@ Pointer<Pointer<git_config>> openDefault() {
/// This method will not guess the path to the xdg compatible config file
/// (`.config/git/config`).
///
/// Throws a [LibGit2Error] if error occured.
/// Throws an error if file has not been found.
String findGlobal() {
final out = calloc<git_buf>();
final out = calloc<git_buf>(2);
final error = libgit2.git_config_find_global(out);
final path = out.ref.ptr.cast<Utf8>().toDartString();
calloc.free(out);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
if (error != 0) {
throw Error();
} else {
return path;
}
}
/// Locate the path to the system configuration file
///
@ -94,10 +94,10 @@ String findSystem() {
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
} else {
return path;
}
}
/// Locate the path to the global xdg compatible configuration file
///
@ -113,9 +113,27 @@ String findXdg() {
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return path;
}
}
return path;
/// 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.
Pointer<git_config> snapshot(Pointer<git_config> config) {
final out = calloc<Pointer<git_config>>();
final error = libgit2.git_config_snapshot(out, config);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Get the value of a config variable.
@ -131,10 +149,10 @@ String getValue(Pointer<git_config> cfg, String variable) {
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
} else {
return value.ref.value.cast<Utf8>().toDartString();
}
}
/// Set the value of a boolean config variable in the config file with the
/// highest level (usually the local one).

View file

@ -192,6 +192,44 @@ Map<String, String> identity(Pointer<git_repository> repo) {
return identity;
}
/// Get the configuration file for this repository.
///
/// If a configuration file has not been set, the default config set for the repository
/// will be returned, including global and system configurations (if they are available).
///
/// The configuration file must be freed once it's no longer being used by the user.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> config(Pointer<git_repository> repo) {
final out = calloc<Pointer<git_config>>();
final error = libgit2.git_repository_config(out, repo);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Get a snapshot of the repository's configuration.
///
/// Convenience function to take a snapshot from the repository's configuration.
/// The contents of this snapshot will not change, even if the underlying config files are modified.
///
/// The configuration file must be freed once it's no longer being used by the user.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> configSnapshot(Pointer<git_repository> repo) {
final out = calloc<Pointer<git_config>>();
final error = libgit2.git_repository_config_snapshot(out, repo);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Get the Index file for this repository.
///
/// If a custom index has not been set, the default index for the repository

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;
}
_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;
}
_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;
_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);

View file

@ -1,4 +1,5 @@
import 'dart:ffi';
import 'config.dart';
import 'index.dart';
import 'odb.dart';
import 'oid.dart';
@ -188,6 +189,26 @@ class Repository {
libgit2.git_libgit2_shutdown();
}
/// Returns the configuration file for this repository.
///
/// If a configuration file has not been set, the default config set for the repository
/// will be returned, including global and system configurations (if they are available).
///
/// The configuration file must be freed once it's no longer being used by the user.
///
/// Throws a [LibGit2Error] if error occured.
Config get config => Config(bindings.config(_repoPointer));
/// Returns a snapshot of the repository's configuration.
///
/// Convenience function to take a snapshot from the repository's configuration.
/// The contents of this snapshot will not change, even if the underlying config files are modified.
///
/// The configuration file must be freed once it's no longer being used by the user.
///
/// Throws a [LibGit2Error] if error occured.
Config get configSnapshot => Config(bindings.configSnapshot(_repoPointer));
/// Returns [Reference] object pointing to repository head.
///
/// Must be freed once it's no longer being used.

View file

@ -22,7 +22,7 @@ void main() {
group('Config', () {
setUp(() {
File('$tmpDir/$configFileName').writeAsStringSync(contents);
config = Config.open(path: '$tmpDir/$configFileName');
config = Config.open('$tmpDir/$configFileName');
});
tearDown(() {

View file

@ -141,6 +141,14 @@ void main() {
await Directory(tmpDir).delete(recursive: true);
});
test('returns config for repository', () {
final config = repo.config;
expect(config.getValue('remote.origin.url'),
'git://github.com/SkinnyMind/libgit2dart.git');
config.free();
});
test('returns empty string when there is no namespace', () {
expect(repo.namespace, isEmpty);
});