From cc78e7945f4d3d06749b2c7d2f2651db5eba6c32 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Fri, 14 Jan 2022 15:08:45 +0300 Subject: [PATCH] feat(index): add binding and API method for git_index_new (#33) --- lib/src/bindings/index.dart | 12 ++++++++++++ lib/src/index.dart | 8 ++++++++ test/index_test.dart | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/lib/src/bindings/index.dart b/lib/src/bindings/index.dart index e1221c8..e7d7033 100644 --- a/lib/src/bindings/index.dart +++ b/lib/src/bindings/index.dart @@ -5,6 +5,18 @@ import 'package:libgit2dart/src/bindings/libgit2_bindings.dart'; import 'package:libgit2dart/src/error.dart'; import 'package:libgit2dart/src/util.dart'; +/// Create an in-memory index object. +/// +/// This index object cannot be read/written to the filesystem, but may be +/// used to perform in-memory index operations. +/// +/// The index must be freed once it's no longer in use. +Pointer newInMemory() { + final out = calloc>(); + libgit2.git_index_new(out); + return out.value; +} + /// Read index capabilities flags. int capabilities(Pointer index) => libgit2.git_index_caps(index); diff --git a/lib/src/index.dart b/lib/src/index.dart index 3aaeeab..f493e07 100644 --- a/lib/src/index.dart +++ b/lib/src/index.dart @@ -14,6 +14,14 @@ class Index with IterableMixin { /// **IMPORTANT**: Should be freed to release allocated memory. const Index(this._indexPointer); + /// Creates an in-memory index object. + /// + /// This index object cannot be read/written to the filesystem, but may be + /// used to perform in-memory index operations. + /// + /// **IMPORTANT**: Should be freed to release allocated memory. + Index.newInMemory() : _indexPointer = bindings.newInMemory(); + final Pointer _indexPointer; /// Pointer to memory address for allocated index object. diff --git a/test/index_test.dart b/test/index_test.dart index ede77cc..1b47d27 100644 --- a/test/index_test.dart +++ b/test/index_test.dart @@ -29,6 +29,10 @@ void main() { const fileSha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'; const featureFileSha = '9c78c21d6680a7ffebc76f7ac68cacc11d8f48bc'; + test('creates new in memory index object', () { + expect(Index.newInMemory(), isA()); + }); + test('returns full path to the index file on disk', () { expect(index.path, p.join(repo.path, 'index')); });