From 9acf3a8a9ecd44bcdb314287451ea549d9e65a21 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Fri, 9 Jul 2021 15:38:15 +0300 Subject: [PATCH] feat(repository): add ability to set and get namespace --- lib/src/bindings/repository.dart | 36 ++++++++++++++++---------------- lib/src/repository.dart | 20 +++++++++++++++++- test/repository_test.dart | 8 +++++++ 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/lib/src/bindings/repository.dart b/lib/src/bindings/repository.dart index c831328..90ba990 100644 --- a/lib/src/bindings/repository.dart +++ b/lib/src/bindings/repository.dart @@ -70,6 +70,24 @@ String getNamespace(Pointer 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 repo, String? namespace) { + final nmspace = namespace?.toNativeUtf8().cast() ?? 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 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 repo, String namespace) { - final nmspace = namespace.toNativeUtf8().cast(); - 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 diff --git a/lib/src/repository.dart b/lib/src/repository.dart index e910fbb..621553c 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -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); } diff --git a/test/repository_test.dart b/test/repository_test.dart index 814fa95..78f1f74 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -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(), ''); });