From 696d55bb3a5f0bba521737cd6a96939473c72d2a Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Sat, 21 Aug 2021 15:36:42 +0300 Subject: [PATCH] feat(repository): add ability to initialize repository --- lib/src/bindings/repository.dart | 16 ++++++++++++++++ lib/src/repository.dart | 14 +++++++++++++- test/repository_test.dart | 30 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/src/bindings/repository.dart b/lib/src/bindings/repository.dart index 2e4b9a9..ca44f9a 100644 --- a/lib/src/bindings/repository.dart +++ b/lib/src/bindings/repository.dart @@ -69,6 +69,22 @@ String discover(String startPath, String ceilingDirs) { return result; } +/// Creates a new Git repository in the given folder. +/// +/// Throws a [LibGit2Error] if error occured. +Pointer init(String path, bool isBare) { + final out = calloc>(); + final pathC = path.toNativeUtf8().cast(); + final isBareC = isBare ? 1 : 0; + final error = libgit2.git_repository_init(out, pathC, isBareC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } + + return out.value; +} + /// Returns the path to the `.git` folder for normal repositories or the /// repository itself for bare repositories. String path(Pointer repo) { diff --git a/lib/src/repository.dart b/lib/src/repository.dart index f30bda1..c24bdc8 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -10,12 +10,24 @@ import 'util.dart'; /// A Repository is the primary interface into a git repository class Repository { + /// Initializes a new instance of the [Repository] class by creating a new + /// Git repository in the given folder. + /// + /// Should be freed with `free()` to release allocated memory. + /// + /// Throws a [LibGit2Error] if error occured. + Repository.init(String path, {bool isBare = false}) { + libgit2.git_libgit2_init(); + + _repoPointer = bindings.init(path, isBare); + } + /// Initializes a new instance of the [Repository] class. /// For a standard repository, [path] should either point to the `.git` folder /// or to the working directory. For a bare repository, [path] should directly /// point to the repository folder. /// - /// [Repository] object should be close with [free] function to release allocated memory. + /// Should be freed with `free()` to release allocated memory. /// /// Throws a [LibGit2Error] if error occured. Repository.open(String path) { diff --git a/test/repository_test.dart b/test/repository_test.dart index 1b08ea6..cc2f8be 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -14,6 +14,36 @@ void main() { ); }); + group('.init()', () { + final initDir = '${Directory.systemTemp.path}/init_repo/'; + + setUp(() async { + if (await Directory(initDir).exists()) { + await Directory(initDir).delete(recursive: true); + } else { + await Directory(initDir).create(); + } + }); + + tearDown(() async { + repo.free(); + await Directory(initDir).delete(recursive: true); + }); + + test('successfully creates new bare repo at provided path', () { + repo = Repository.init(initDir, isBare: true); + expect(repo.path, initDir); + expect(repo.isBare, true); + }); + + test('successfully creates new standard repo at provided path', () { + repo = Repository.init(initDir); + expect(repo.path, '$initDir.git/'); + expect(repo.isBare, false); + expect(repo.isEmpty, true); + }); + }); + group('empty', () { group('bare', () { setUp(() {