feat(repository): add ability to discover repository from sub directories

This commit is contained in:
Aleksey Kulikov 2021-08-21 14:20:57 +03:00
parent da8494d3e2
commit 543ebff223
4 changed files with 53 additions and 1 deletions

View file

@ -69,7 +69,7 @@ Pointer<git_config> openDefault() {
///
/// Throws an error if file has not been found.
String findGlobal() {
final out = calloc<git_buf>(2);
final out = calloc<git_buf>(sizeOf<git_buf>());
final error = libgit2.git_config_find_global(out);
final path = out.ref.ptr.cast<Utf8>().toDartString();
calloc.free(out);

View file

@ -40,6 +40,35 @@ Pointer<git_repository> openBare(String barePath) {
return out.value;
}
/// Look for a git repository and return its path. The lookup start from [startPath]
/// and walk across parent directories if nothing has been found. The lookup ends when
/// the first repository is found, or when reaching a directory referenced in [ceilingDirs].
///
/// The method will automatically detect if the repository is bare (if there is a repository).
///
/// Throws a [LibGit2Error] if error occured.
String discover(String startPath, String ceilingDirs) {
final out = calloc<git_buf>(sizeOf<git_buf>());
final startPathC = startPath.toNativeUtf8().cast<Int8>();
final ceilingDirsC = ceilingDirs.toNativeUtf8().cast<Int8>();
final error =
libgit2.git_repository_discover(out, startPathC, 0, ceilingDirsC);
var result = '';
if (error == git_error_code.GIT_ENOTFOUND) {
return result;
} else if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
result = out.ref.ptr.cast<Utf8>().toDartString();
calloc.free(out);
calloc.free(startPathC);
calloc.free(ceilingDirsC);
return result;
}
/// Returns the path to the `.git` folder for normal repositories or the
/// repository itself for bare repositories.
String path(Pointer<git_repository> repo) {

View file

@ -29,6 +29,17 @@ class Repository {
/// Pointer to memory address for allocated repository object.
Pointer<git_repository> get pointer => _repoPointer;
/// Look for a git repository and return its path. The lookup start from [startPath]
/// and walk across parent directories if nothing has been found. The lookup ends when
/// the first repository is found, or when reaching a directory referenced in [ceilingDirs].
///
/// The method will automatically detect if the repository is bare (if there is a repository).
///
/// Throws a [LibGit2Error] if error occured.
static String discover(String startPath, [String ceilingDirs = '']) {
return bindings.discover(startPath, ceilingDirs);
}
/// Returns path to the `.git` folder for normal repositories
/// or path to the repository itself for bare repositories.
String get path => bindings.path(_repoPointer);