From a7040e5040d238502f98c9a60df1f8770f38747c Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Tue, 1 Jun 2021 20:42:28 +0300 Subject: [PATCH] feat: prototype repository api --- lib/src/repository.dart | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/src/repository.dart diff --git a/lib/src/repository.dart b/lib/src/repository.dart new file mode 100644 index 0000000..09b34c1 --- /dev/null +++ b/lib/src/repository.dart @@ -0,0 +1,40 @@ +import 'dart:ffi'; +import 'package:ffi/ffi.dart'; +import 'bindings/libgit2_bindings.dart'; +import 'bindings/repository.dart' as repository; +import 'util.dart'; + +/// A Repository is the primary interface into a git repository +class Repository { + Repository(); + + /// Initializes a new instance of the [Repository] class. + /// For a standard repository, [dir] should either point to the `.git` folder + /// or to the working directory. For a bare repository, [dir] should directly + /// point to the repository folder. + Repository.open(String dir) { + libgit2.git_libgit2_init(); + + _repoPointer = repository.open(dir); + _head = repository.revParseSingle(_repoPointer.value, 'HEAD^{commit}'); + headCommit = libgit2 + .git_commit_message(_head.value.cast()) + .cast() + .toDartString(); + path = repository.path(_repoPointer.value); + namespace = repository.getNamespace(_repoPointer.value); + isBare = repository.isBare(_repoPointer.value); + + // free up neccessary pointers + calloc.free(_repoPointer); + calloc.free(_head); + libgit2.git_libgit2_shutdown(); + } + + late Pointer> _repoPointer; + late Pointer> _head; + late String headCommit; + late String path; + late String namespace; + late bool isBare; +}