feat(repository): add ability to set and get namespace

This commit is contained in:
Aleksey Kulikov 2021-07-09 15:38:15 +03:00
parent b8b69100f1
commit 9acf3a8a9e
3 changed files with 45 additions and 19 deletions

View file

@ -70,6 +70,24 @@ String getNamespace(Pointer<git_repository> repo) {
}
}
/// Sets the active namespace for this repository.
///
/// This namespace affects all reference operations for the repo. See `man gitnamespaces`
///
/// The [namespace] should not include the refs folder, e.g. to namespace all references
/// under refs/namespaces/foo/, use foo as the namespace.
///
/// Throws a [LibGit2Error] if error occured.
void setNamespace(Pointer<git_repository> repo, String? namespace) {
final nmspace = namespace?.toNativeUtf8().cast<Int8>() ?? nullptr;
final error = libgit2.git_repository_set_namespace(repo, nmspace);
calloc.free(nmspace);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Check if a repository is bare or not.
bool isBare(Pointer<git_repository> repo) {
final result = libgit2.git_repository_is_bare(repo);
@ -331,24 +349,6 @@ void setHeadDetachedFromAnnotated(
}
}
/// Sets the active namespace for this Git Repository
///
/// This namespace affects all reference operations for the repo. See `man gitnamespaces`
///
/// The [namespace] should not include the refs folder, e.g. to namespace all references
/// under refs/namespaces/foo/, use foo as the namespace.
///
/// Throws a [LibGit2Error] if error occured.
void setNamespace(Pointer<git_repository> repo, String namespace) {
final nmspace = namespace.toNativeUtf8().cast<Int8>();
final error = libgit2.git_repository_set_namespace(repo, nmspace);
calloc.free(nmspace);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Set the path to the working directory for this repository
///
/// The working directory doesn't need to be the same one that contains the

View file

@ -44,6 +44,24 @@ class Repository {
/// empty string is returned.
String getNamespace() => bindings.getNamespace(_repoPointer);
/// Sets the active namespace for this repository.
///
/// This namespace affects all reference operations for the repo. See `man gitnamespaces`
///
/// The [namespace] should not include the refs folder, e.g. to namespace all references
/// under refs/namespaces/foo/, use foo as the namespace.
///
/// Pass null to unset.
///
/// Throws a [LibGit2Error] if error occured.
void setNamespace(String? namespace) {
try {
bindings.setNamespace(_repoPointer, namespace);
} catch (e) {
rethrow;
}
}
/// Checks whether this repository is a bare repository or not.
bool isBare() => bindings.isBare(_repoPointer);
@ -85,7 +103,7 @@ class Repository {
/// Sets the identity to be used for writing reflogs.
///
/// If both are set, this name and email will be used to write to the reflog.
/// Pass NULL to unset. When unset, the identity will be taken from the repository's configuration.
/// Pass null to unset. When unset, the identity will be taken from the repository's configuration.
void setIdentity({required String? name, required String? email}) {
bindings.setIdentity(_repoPointer, name, email);
}

View file

@ -83,6 +83,14 @@ void main() {
});
test('returns empty string when there is no namespace', () {
expect(repo.getNamespace(), isEmpty);
});
test('successfully sets and unsets the namespace', () {
expect(repo.getNamespace(), '');
repo.setNamespace('some');
expect(repo.getNamespace(), 'some');
repo.setNamespace(null);
expect(repo.getNamespace(), '');
});