From a78c38d8e30eb1381bfe2fbfef06d50b60b6d301 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Fri, 27 Aug 2021 15:05:05 +0300 Subject: [PATCH] refactor: revert 'use ffi Arena for resource management' --- lib/src/bindings/commit.dart | 16 +- lib/src/bindings/config.dart | 296 +++++++++++++-------------- lib/src/bindings/index.dart | 148 +++++++------- lib/src/bindings/oid.dart | 20 +- lib/src/bindings/reference.dart | 286 +++++++++++++------------- lib/src/bindings/reflog.dart | 20 +- lib/src/bindings/repository.dart | 337 +++++++++++++++---------------- lib/src/bindings/signature.dart | 46 +++-- lib/src/bindings/tree.dart | 39 ++-- 9 files changed, 588 insertions(+), 620 deletions(-) diff --git a/lib/src/bindings/commit.dart b/lib/src/bindings/commit.dart index f63c78a..0df462a 100644 --- a/lib/src/bindings/commit.dart +++ b/lib/src/bindings/commit.dart @@ -10,16 +10,14 @@ import '../util.dart'; /// /// Throws a [LibGit2Error] if error occured. Pointer lookup(Pointer repo, Pointer id) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_commit_lookup(out, repo, id); + final out = calloc>(); + final error = libgit2.git_commit_lookup(out, repo, id); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Get the encoding for the message of a commit, as a string representing a standard encoding name. diff --git a/lib/src/bindings/config.dart b/lib/src/bindings/config.dart index 1369c83..6de77fa 100644 --- a/lib/src/bindings/config.dart +++ b/lib/src/bindings/config.dart @@ -11,33 +11,31 @@ import '../util.dart'; /// /// Throws a [LibGit2Error] if error occured. Pointer newConfig() { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_config_new(out); + final out = calloc>(); + final error = libgit2.git_config_new(out); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Create a new config instance containing a single on-disk file /// /// Throws a [LibGit2Error] if error occured. Pointer open(String path) { - return using((Arena arena) { - final out = arena>(); - final pathC = path.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_config_open_ondisk(out, pathC); + final out = calloc>(); + final pathC = path.toNativeUtf8().cast(); + final error = libgit2.git_config_open_ondisk(out, pathC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(pathC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Open the global, XDG and system configuration files @@ -48,16 +46,14 @@ Pointer open(String path) { /// /// Throws a [LibGit2Error] if error occured. Pointer openDefault() { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_config_open_default(out); + final out = calloc>(); + final error = libgit2.git_config_open_default(out); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Locate the path to the global configuration file @@ -74,17 +70,14 @@ Pointer openDefault() { /// /// Throws an error if file has not been found. String findGlobal() { - return using((Arena arena) { - final out = arena(sizeOf()); - final error = libgit2.git_config_find_global(out); - final path = out.ref.ptr.cast().toDartString(); + final out = calloc(sizeOf()); + final error = libgit2.git_config_find_global(out); - if (error != 0) { - throw Error(); - } else { - return path; - } - }); + if (error != 0) { + throw Error(); + } else { + return out.ref.ptr.cast().toDartString(); + } } /// Locate the path to the system configuration file @@ -93,17 +86,14 @@ String findGlobal() { /// /// Throws a [LibGit2Error] if error occured. String findSystem() { - return using((Arena arena) { - final out = arena(); - final error = libgit2.git_config_find_system(out); - final path = out.ref.ptr.cast().toDartString(); + final out = calloc(); + final error = libgit2.git_config_find_system(out); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return path; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.ref.ptr.cast().toDartString(); + } } /// Locate the path to the global xdg compatible configuration file @@ -113,17 +103,14 @@ String findSystem() { /// /// Throws a [LibGit2Error] if error occured. String findXdg() { - return using((Arena arena) { - final out = arena(); - final error = libgit2.git_config_find_xdg(out); - final path = out.ref.ptr.cast().toDartString(); + final out = calloc(); + final error = libgit2.git_config_find_xdg(out); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return path; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.ref.ptr.cast().toDartString(); + } } /// Create a snapshot of the configuration. @@ -134,33 +121,31 @@ String findXdg() { /// /// Throws a [LibGit2Error] if error occured. Pointer snapshot(Pointer config) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_config_snapshot(out, config); + final out = calloc>(); + final error = libgit2.git_config_snapshot(out, config); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Get the value of a config variable. /// /// Throws a [LibGit2Error] if error occured. String getValue(Pointer cfg, String variable) { - return using((Arena arena) { - final out = arena>(); - final name = variable.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_config_get_entry(out, cfg, name); + final out = calloc>(); + final name = variable.toNativeUtf8().cast(); + final error = libgit2.git_config_get_entry(out, cfg, name); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value.ref.value.cast().toDartString(); - } - }); + calloc.free(name); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value.ref.value.cast().toDartString(); + } } /// Set the value of a boolean config variable in the config file with the @@ -168,15 +153,15 @@ String getValue(Pointer cfg, String variable) { /// /// Throws a [LibGit2Error] if error occured. void setBool(Pointer cfg, String variable, bool value) { - using((Arena arena) { - final name = variable.toNativeUtf8(allocator: arena).cast(); - final valueC = value ? 1 : 0; - final error = libgit2.git_config_set_bool(cfg, name, valueC); + final name = variable.toNativeUtf8().cast(); + final valueC = value ? 1 : 0; + final error = libgit2.git_config_set_bool(cfg, name, valueC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(name); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Set the value of an integer config variable in the config file with the @@ -184,14 +169,14 @@ void setBool(Pointer cfg, String variable, bool value) { /// /// Throws a [LibGit2Error] if error occured. void setInt(Pointer cfg, String variable, int value) { - using((Arena arena) { - final name = variable.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_config_set_int64(cfg, name, value); + final name = variable.toNativeUtf8().cast(); + final error = libgit2.git_config_set_int64(cfg, name, value); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(name); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Set the value of a string config variable in the config file with the @@ -199,34 +184,36 @@ void setInt(Pointer cfg, String variable, int value) { /// /// Throws a [LibGit2Error] if error occured. void setString(Pointer cfg, String variable, String value) { - using((Arena arena) { - final name = variable.toNativeUtf8(allocator: arena).cast(); - final valueC = value.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_config_set_string(cfg, name, valueC); + final name = variable.toNativeUtf8().cast(); + final valueC = value.toNativeUtf8().cast(); + final error = libgit2.git_config_set_string(cfg, name, valueC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(name); + calloc.free(valueC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Iterate over all the config variables. Map getEntries(Pointer cfg) { - return using((Arena arena) { - final iterator = arena>(); - final entry = arena>(); - libgit2.git_config_iterator_new(iterator, cfg); - var error = 0; - final entries = {}; + final iterator = calloc>(); + final entry = calloc>(); + libgit2.git_config_iterator_new(iterator, cfg); + var error = 0; + final entries = {}; - while (error != -31) { - error = libgit2.git_config_next(entry, iterator.value); - entries[entry.value.ref.name.cast().toDartString()] = - entry.value.ref.value.cast().toDartString(); - } + while (error != -31) { + error = libgit2.git_config_next(entry, iterator.value); + entries[entry.value.ref.name.cast().toDartString()] = + entry.value.ref.value.cast().toDartString(); + } - return entries; - }); + calloc.free(iterator); + calloc.free(entry); + + return entries; } /// Delete a config variable from the config file with the highest level @@ -234,14 +221,14 @@ Map getEntries(Pointer cfg) { /// /// Throws a [LibGit2Error] if error occured. void delete(Pointer cfg, String variable) { - using((Arena arena) { - final name = variable.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_config_delete_entry(cfg, name); + final name = variable.toNativeUtf8().cast(); + final error = libgit2.git_config_delete_entry(cfg, name); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(name); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Iterate over the values of a multivar @@ -253,27 +240,29 @@ List multivarValues( String variable, String? regexp, ) { - return using((Arena arena) { - final name = variable.toNativeUtf8(allocator: arena).cast(); - final regexpC = - regexp?.toNativeUtf8(allocator: arena).cast() ?? nullptr; - final iterator = arena>(); - final entry = arena>(); - libgit2.git_config_multivar_iterator_new(iterator, cfg, name, regexpC); - var error = 0; - final entries = []; + final name = variable.toNativeUtf8().cast(); + final regexpC = regexp?.toNativeUtf8().cast() ?? nullptr; + final iterator = calloc>(); + final entry = calloc>(); + libgit2.git_config_multivar_iterator_new(iterator, cfg, name, regexpC); + var error = 0; + final entries = []; - while (error == 0) { - error = libgit2.git_config_next(entry, iterator.value); - if (error != -31) { - entries.add(entry.value.ref.value.cast().toDartString()); - } else { - break; - } + while (error == 0) { + error = libgit2.git_config_next(entry, iterator.value); + if (error != -31) { + entries.add(entry.value.ref.value.cast().toDartString()); + } else { + break; } + } - return entries; - }); + calloc.free(name); + calloc.free(regexpC); + calloc.free(iterator); + calloc.free(entry); + + return entries; } /// Set the value of a multivar config variable in the config file with the @@ -286,28 +275,27 @@ void setMultivar( String regexp, String value, ) { - using((Arena arena) { - final name = variable.toNativeUtf8(allocator: arena).cast(); - final regexpC = regexp.toNativeUtf8(allocator: arena).cast(); - final valueC = value.toNativeUtf8(allocator: arena).cast(); - libgit2.git_config_set_multivar(cfg, name, regexpC, valueC); - }); + final name = variable.toNativeUtf8().cast(); + final regexpC = regexp.toNativeUtf8().cast(); + final valueC = value.toNativeUtf8().cast(); + libgit2.git_config_set_multivar(cfg, name, regexpC, valueC); + + calloc.free(name); + calloc.free(regexpC); + calloc.free(valueC); } /// Deletes one or several values from a multivar in the config file /// with the highest level (usually the local one). /// /// The regexp is applied case-sensitively on the value. -void deleteMultivar( - Pointer cfg, - String variable, - String regexp, -) { - using((Arena arena) { - final name = variable.toNativeUtf8(allocator: arena).cast(); - final regexpC = regexp.toNativeUtf8(allocator: arena).cast(); - libgit2.git_config_delete_multivar(cfg, name, regexpC); - }); +void deleteMultivar(Pointer cfg, String variable, String regexp) { + final name = variable.toNativeUtf8().cast(); + final regexpC = regexp.toNativeUtf8().cast(); + libgit2.git_config_delete_multivar(cfg, name, regexpC); + + calloc.free(name); + calloc.free(regexpC); } /// Free the configuration and its associated memory and files. diff --git a/lib/src/bindings/index.dart b/lib/src/bindings/index.dart index 01cafa2..4741233 100644 --- a/lib/src/bindings/index.dart +++ b/lib/src/bindings/index.dart @@ -62,12 +62,12 @@ Pointer writeTree(Pointer index) { /// Find the first position of any entries which point to given path in the Git index. bool find(Pointer index, String path) { - return using((Arena arena) { - final pathC = path.toNativeUtf8(allocator: arena).cast(); - final result = libgit2.git_index_find(nullptr, index, pathC); + final pathC = path.toNativeUtf8().cast(); + final result = libgit2.git_index_find(nullptr, index, pathC); - return result == git_error_code.GIT_ENOTFOUND ? false : true; - }); + calloc.free(pathC); + + return result == git_error_code.GIT_ENOTFOUND ? false : true; } /// Get the count of entries currently in the index. @@ -98,16 +98,16 @@ Pointer getByPath( String path, int stage, ) { - return using((Arena arena) { - final pathC = path.toNativeUtf8(allocator: arena).cast(); - final result = libgit2.git_index_get_bypath(index, pathC, stage); + final pathC = path.toNativeUtf8().cast(); + final result = libgit2.git_index_get_bypath(index, pathC, stage); - if (result == nullptr) { - throw ArgumentError.value('$path was not found'); - } else { - return result; - } - }); + calloc.free(pathC); + + if (result == nullptr) { + throw ArgumentError.value('$path was not found'); + } else { + return result; + } } /// Clear the contents (all the entries) of an index object. @@ -152,14 +152,14 @@ void add(Pointer index, Pointer sourceEntry) { /// /// Throws a [LibGit2Error] if error occured. void addByPath(Pointer index, String path) { - using((Arena arena) { - final pathC = path.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_index_add_bypath(index, pathC); + final pathC = path.toNativeUtf8().cast(); + final error = libgit2.git_index_add_bypath(index, pathC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(pathC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Add or update index entries matching files in the working directory. @@ -172,32 +172,35 @@ void addByPath(Pointer index, String path) { /// /// Throws a [LibGit2Error] if error occured. void addAll(Pointer index, List pathspec) { - using((Arena arena) { - var pathspecC = arena(); - final List> pathPointers = pathspec - .map((e) => e.toNativeUtf8(allocator: arena).cast()) - .toList(); - final Pointer> strArray = arena(pathspec.length); + var pathspecC = calloc(); + final List> pathPointers = + pathspec.map((e) => e.toNativeUtf8().cast()).toList(); + final Pointer> strArray = calloc(pathspec.length); - for (var i = 0; i < pathspec.length; i++) { - strArray[i] = pathPointers[i]; - } + for (var i = 0; i < pathspec.length; i++) { + strArray[i] = pathPointers[i]; + } - pathspecC.ref.strings = strArray; - pathspecC.ref.count = pathspec.length; + pathspecC.ref.strings = strArray; + pathspecC.ref.count = pathspec.length; - final error = libgit2.git_index_add_all( - index, - pathspecC, - 0, - nullptr, - nullptr, - ); + final error = libgit2.git_index_add_all( + index, + pathspecC, + 0, + nullptr, + nullptr, + ); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(pathspecC); + for (var p in pathPointers) { + calloc.free(p); + } + calloc.free(strArray); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Write an existing index object from memory back to disk using an atomic file lock. @@ -215,45 +218,48 @@ void write(Pointer index) { /// /// Throws a [LibGit2Error] if error occured. void remove(Pointer index, String path, int stage) { - using((Arena arena) { - final pathC = path.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_index_remove(index, pathC, stage); + final pathC = path.toNativeUtf8().cast(); + final error = libgit2.git_index_remove(index, pathC, stage); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(pathC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Remove all matching index entries. /// /// Throws a [LibGit2Error] if error occured. void removeAll(Pointer index, List pathspec) { - using((Arena arena) { - final pathspecC = arena(); - final List> pathPointers = pathspec - .map((e) => e.toNativeUtf8(allocator: arena).cast()) - .toList(); - final Pointer> strArray = arena(pathspec.length); + final pathspecC = calloc(); + final List> pathPointers = + pathspec.map((e) => e.toNativeUtf8().cast()).toList(); + final Pointer> strArray = calloc(pathspec.length); - for (var i = 0; i < pathspec.length; i++) { - strArray[i] = pathPointers[i]; - } + for (var i = 0; i < pathspec.length; i++) { + strArray[i] = pathPointers[i]; + } - pathspecC.ref.strings = strArray; - pathspecC.ref.count = pathspec.length; + pathspecC.ref.strings = strArray; + pathspecC.ref.count = pathspec.length; - final error = libgit2.git_index_remove_all( - index, - pathspecC, - nullptr, - nullptr, - ); + final error = libgit2.git_index_remove_all( + index, + pathspecC, + nullptr, + nullptr, + ); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(pathspecC); + for (var p in pathPointers) { + calloc.free(p); + } + calloc.free(strArray); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Get the repository this index relates to. diff --git a/lib/src/bindings/oid.dart b/lib/src/bindings/oid.dart index 7ee200b..08fbc27 100644 --- a/lib/src/bindings/oid.dart +++ b/lib/src/bindings/oid.dart @@ -42,17 +42,17 @@ Pointer fromSHA(String hex) { /// /// Throws a [LibGit2Error] if error occured. String toSHA(Pointer id) { - return using((Arena arena) { - final out = arena(40); - final error = libgit2.git_oid_fmt(out, id); - final result = out.cast().toDartString(length: 40); + final out = calloc(40); + final error = libgit2.git_oid_fmt(out, id); + final result = out.cast().toDartString(length: 40); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return result; - } - }); + calloc.free(out); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return result; + } } /// Compare two oid structures. diff --git a/lib/src/bindings/reference.dart b/lib/src/bindings/reference.dart index 2c5faa8..028e9a8 100644 --- a/lib/src/bindings/reference.dart +++ b/lib/src/bindings/reference.dart @@ -35,16 +35,14 @@ Pointer target(Pointer ref) { /// /// Throws a [LibGit2Error] if error occured. Pointer resolve(Pointer ref) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_reference_resolve(out, ref); + final out = calloc>(); + final error = libgit2.git_reference_resolve(out, ref); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Lookup a reference by name in a repository. @@ -55,17 +53,17 @@ Pointer resolve(Pointer ref) { /// /// Throws a [LibGit2Error] if error occured. Pointer lookup(Pointer repo, String name) { - return using((Arena arena) { - final out = arena>(); - final nameC = name.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_reference_lookup(out, repo, nameC); + final out = calloc>(); + final nameC = name.toNativeUtf8().cast(); + final error = libgit2.git_reference_lookup(out, repo, nameC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(nameC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Lookup a reference by DWIMing its short name. @@ -75,27 +73,25 @@ Pointer lookup(Pointer repo, String name) { /// /// Throws a [LibGit2Error] if error occured. Pointer lookupDWIM(Pointer repo, String name) { - return using((Arena arena) { - final out = arena>(); - final nameC = name.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_reference_dwim(out, repo, nameC); + final out = calloc>(); + final nameC = name.toNativeUtf8().cast(); + final error = libgit2.git_reference_dwim(out, repo, nameC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(nameC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Get the full name of a reference. String name(Pointer ref) { - return using((Arena arena) { - var result = arena(); - result = libgit2.git_reference_name(ref); + var result = calloc(); + result = libgit2.git_reference_name(ref); - return result.cast().toDartString(); - }); + return result.cast().toDartString(); } /// Get the reference's short name. @@ -126,66 +122,64 @@ Pointer rename( bool force, String? logMessage, ) { - return using((Arena arena) { - final out = arena>(); - final newNameC = newName.toNativeUtf8(allocator: arena).cast(); - final forceC = force == true ? 1 : 0; - final logMessageC = logMessage?.toNativeUtf8().cast() ?? nullptr; - final error = libgit2.git_reference_rename( - out, - ref, - newNameC, - forceC, - logMessageC, - ); + final out = calloc>(); + final newNameC = newName.toNativeUtf8().cast(); + final forceC = force == true ? 1 : 0; + final logMessageC = logMessage?.toNativeUtf8().cast() ?? nullptr; + final error = libgit2.git_reference_rename( + out, + ref, + newNameC, + forceC, + logMessageC, + ); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(newNameC); + calloc.free(logMessageC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Fill a list with all the references that can be found in a repository. /// -/// The string array will be filled with the names of all references; -/// these values are owned by the user and should be free'd manually when no longer needed. -/// /// Throws a [LibGit2Error] if error occured. List list(Pointer repo) { - return using((Arena arena) { - var array = arena(); - final error = libgit2.git_reference_list(array, repo); - var result = []; + var array = calloc(); + final error = libgit2.git_reference_list(array, repo); + var result = []; - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - for (var i = 0; i < array.ref.count; i++) { - result.add( - array.ref.strings.elementAt(i).value.cast().toDartString()); - } + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + for (var i = 0; i < array.ref.count; i++) { + result.add( + array.ref.strings.elementAt(i).value.cast().toDartString()); } + } - return result; - }); + calloc.free(array); + + return result; } /// Check if a reflog exists for the specified reference. /// /// Throws a [LibGit2Error] if error occured. bool hasLog(Pointer repo, String name) { - return using((Arena arena) { - final refname = name.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_reference_has_log(repo, refname); + final refname = name.toNativeUtf8().cast(); + final error = libgit2.git_reference_has_log(repo, refname); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return error == 1 ? true : false; - } - }); + calloc.free(refname); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return error == 1 ? true : false; + } } /// Check if a reference is a local branch. @@ -241,26 +235,27 @@ Pointer createDirect( bool force, String? logMessage, ) { - return using((Arena arena) { - final out = arena>(); - final nameC = name.toNativeUtf8(allocator: arena).cast(); - final forceC = force == true ? 1 : 0; - final logMessageC = logMessage?.toNativeUtf8().cast() ?? nullptr; - final error = libgit2.git_reference_create( - out, - repo, - nameC, - oid, - forceC, - logMessageC, - ); + final out = calloc>(); + final nameC = name.toNativeUtf8().cast(); + final forceC = force == true ? 1 : 0; + final logMessageC = logMessage?.toNativeUtf8().cast() ?? nullptr; + final error = libgit2.git_reference_create( + out, + repo, + nameC, + oid, + forceC, + logMessageC, + ); - if (error < 0) { - throw (LibGit2Error(libgit2.git_error_last())); - } else { - return out.value; - } - }); + calloc.free(nameC); + calloc.free(logMessageC); + + if (error < 0) { + throw (LibGit2Error(libgit2.git_error_last())); + } else { + return out.value; + } } /// Create a new symbolic reference. @@ -292,27 +287,29 @@ Pointer createSymbolic( bool force, String? logMessage, ) { - return using((Arena arena) { - final out = arena>(); - final nameC = name.toNativeUtf8(allocator: arena).cast(); - final targetC = target.toNativeUtf8().cast(); - final forceC = force == true ? 1 : 0; - final logMessageC = logMessage?.toNativeUtf8().cast() ?? nullptr; - final error = libgit2.git_reference_symbolic_create( - out, - repo, - nameC, - targetC, - forceC, - logMessageC, - ); + final out = calloc>(); + final nameC = name.toNativeUtf8().cast(); + final targetC = target.toNativeUtf8().cast(); + final forceC = force == true ? 1 : 0; + final logMessageC = logMessage?.toNativeUtf8().cast() ?? nullptr; + final error = libgit2.git_reference_symbolic_create( + out, + repo, + nameC, + targetC, + forceC, + logMessageC, + ); - if (error < 0) { - throw (LibGit2Error(libgit2.git_error_last())); - } else { - return out.value; - } - }); + calloc.free(nameC); + calloc.free(targetC); + calloc.free(logMessageC); + + if (error < 0) { + throw (LibGit2Error(libgit2.git_error_last())); + } else { + return out.value; + } } /// Delete an existing reference. @@ -345,18 +342,17 @@ Pointer setTarget( Pointer oid, String? logMessage, ) { - return using((Arena arena) { - final out = arena>(); - final logMessageC = - logMessage?.toNativeUtf8(allocator: arena).cast() ?? nullptr; - final error = libgit2.git_reference_set_target(out, ref, oid, logMessageC); + final out = calloc>(); + final logMessageC = logMessage?.toNativeUtf8().cast() ?? nullptr; + final error = libgit2.git_reference_set_target(out, ref, oid, logMessageC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(logMessageC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Create a new reference with the same name as the given reference but a different @@ -375,20 +371,20 @@ Pointer setTargetSymbolic( String target, String? logMessage, ) { - return using((Arena arena) { - final out = arena>(); - final targetC = target.toNativeUtf8(allocator: arena).cast(); - final logMessageC = - logMessage?.toNativeUtf8(allocator: arena).cast() ?? nullptr; - final error = libgit2.git_reference_symbolic_set_target( - out, ref, targetC, logMessageC); + final out = calloc>(); + final targetC = target.toNativeUtf8().cast(); + final logMessageC = logMessage?.toNativeUtf8().cast() ?? nullptr; + final error = + libgit2.git_reference_symbolic_set_target(out, ref, targetC, logMessageC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(targetC); + calloc.free(logMessageC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Compare two references. @@ -407,12 +403,12 @@ bool compare(Pointer ref1, Pointer ref2) { /// the characters '~', '^', ':', '\', '?', '[', and '*', and the sequences ".." /// and "@{" which have special meaning to revparse. bool isValidName(String name) { - return using((Arena arena) { - final refname = name.toNativeUtf8(allocator: arena).cast(); - final result = libgit2.git_reference_is_valid_name(refname); + final refname = name.toNativeUtf8().cast(); + final result = libgit2.git_reference_is_valid_name(refname); - return result == 1 ? true : false; - }); + calloc.free(refname); + + return result == 1 ? true : false; } /// Free the given reference. diff --git a/lib/src/bindings/reflog.dart b/lib/src/bindings/reflog.dart index 0a1894c..64e8c02 100644 --- a/lib/src/bindings/reflog.dart +++ b/lib/src/bindings/reflog.dart @@ -13,17 +13,17 @@ import '../util.dart'; /// /// Throws a [LibGit2Error] if error occured. Pointer read(Pointer repo, String name) { - return using((Arena arena) { - final out = arena>(); - final nameC = name.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_reflog_read(out, repo, nameC); + final out = calloc>(); + final nameC = name.toNativeUtf8().cast(); + final error = libgit2.git_reflog_read(out, repo, nameC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(nameC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Get the number of log entries in a reflog. diff --git a/lib/src/bindings/repository.dart b/lib/src/bindings/repository.dart index 68c0942..fc8469e 100644 --- a/lib/src/bindings/repository.dart +++ b/lib/src/bindings/repository.dart @@ -10,17 +10,17 @@ import '../util.dart'; /// /// Throws a [LibGit2Error] if error occured. Pointer open(String path) { - return using((Arena arena) { - final out = arena>(); - final pathC = path.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_repository_open(out, pathC); + final out = calloc>(); + final pathC = path.toNativeUtf8().cast(); + final error = libgit2.git_repository_open(out, pathC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(pathC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Attempt to open an already-existing bare repository at [bare_path]. @@ -29,17 +29,17 @@ Pointer open(String path) { /// /// Throws a [LibGit2Error] if error occured. Pointer openBare(String barePath) { - return using((Arena arena) { - final out = arena>(); - final barePathC = barePath.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_repository_open_bare(out, barePathC); + final out = calloc>(); + final barePathC = barePath.toNativeUtf8().cast(); + final error = libgit2.git_repository_open_bare(out, barePathC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(barePathC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Look for a git repository and return its path. The lookup start from [startPath] @@ -50,40 +50,40 @@ Pointer openBare(String barePath) { /// /// Throws a [LibGit2Error] if error occured. String discover(String startPath, String ceilingDirs) { - return using((Arena arena) { - final out = arena(sizeOf()); - final startPathC = startPath.toNativeUtf8(allocator: arena).cast(); - final ceilingDirsC = - ceilingDirs.toNativeUtf8(allocator: arena).cast(); - final error = - libgit2.git_repository_discover(out, startPathC, 0, ceilingDirsC); + final out = calloc(sizeOf()); + final startPathC = startPath.toNativeUtf8().cast(); + final ceilingDirsC = ceilingDirs.toNativeUtf8().cast(); + final error = + libgit2.git_repository_discover(out, startPathC, 0, ceilingDirsC); - if (error == git_error_code.GIT_ENOTFOUND) { - return ''; - } else if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.ref.ptr.cast().toDartString(); - } - }); + calloc.free(startPathC); + calloc.free(ceilingDirsC); + + if (error == git_error_code.GIT_ENOTFOUND) { + return ''; + } else if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.ref.ptr.cast().toDartString(); + } } /// Creates a new Git repository in the given folder. /// /// Throws a [LibGit2Error] if error occured. Pointer init(String path, bool isBare) { - return using((Arena arena) { - final out = arena>(); - final pathC = path.toNativeUtf8(allocator: arena).cast(); - final isBareC = isBare ? 1 : 0; - final error = libgit2.git_repository_init(out, pathC, isBareC); + 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()); - } else { - return out.value; - } - }); + calloc.free(pathC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Returns the path to the `.git` folder for normal repositories or the @@ -125,15 +125,14 @@ String getNamespace(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. void setNamespace(Pointer repo, String? namespace) { - using((Arena arena) { - final nmspace = - namespace?.toNativeUtf8(allocator: arena).cast() ?? nullptr; - final error = libgit2.git_repository_set_namespace(repo, nmspace); + final nmspace = namespace?.toNativeUtf8().cast() ?? nullptr; + final error = libgit2.git_repository_set_namespace(repo, nmspace); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(nmspace); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Check if a repository is bare or not. @@ -165,16 +164,14 @@ bool isEmpty(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. Pointer head(Pointer repo) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_repository_head(out, repo); + final out = calloc>(); + final error = libgit2.git_repository_head(out, repo); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Check if a repository's HEAD is detached. @@ -213,32 +210,33 @@ bool isBranchUnborn(Pointer repo) { /// 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. void setIdentity(Pointer repo, String? name, String? email) { - using((Arena arena) { - final nameC = name?.toNativeUtf8(allocator: arena).cast() ?? nullptr; - final emailC = - email?.toNativeUtf8(allocator: arena).cast() ?? nullptr; + final nameC = name?.toNativeUtf8().cast() ?? nullptr; + final emailC = email?.toNativeUtf8().cast() ?? nullptr; - libgit2.git_repository_set_ident(repo, nameC, emailC); - }); + libgit2.git_repository_set_ident(repo, nameC, emailC); + + calloc.free(nameC); + calloc.free(emailC); } /// Retrieve the configured identity to use for reflogs. Map identity(Pointer repo) { - return using((Arena arena) { - final name = arena>(); - final email = arena>(); - libgit2.git_repository_ident(name, email, repo); - var identity = {}; - - if (name.value == nullptr && email.value == nullptr) { - return identity; - } else { - identity[name.value.cast().toDartString()] = - email.value.cast().toDartString(); - } + final name = calloc>(); + final email = calloc>(); + libgit2.git_repository_ident(name, email, repo); + var identity = {}; + if (name.value == nullptr && email.value == nullptr) { return identity; - }); + } else { + identity[name.value.cast().toDartString()] = + email.value.cast().toDartString(); + } + + calloc.free(name); + calloc.free(email); + + return identity; } /// Get the configuration file for this repository. @@ -250,16 +248,14 @@ Map identity(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. Pointer config(Pointer repo) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_repository_config(out, repo); + final out = calloc>(); + final error = libgit2.git_repository_config(out, repo); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Get a snapshot of the repository's configuration. @@ -271,16 +267,14 @@ Pointer config(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. Pointer configSnapshot(Pointer repo) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_repository_config_snapshot(out, repo); + final out = calloc>(); + final error = libgit2.git_repository_config_snapshot(out, repo); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Get the Index file for this repository. @@ -292,16 +286,14 @@ Pointer configSnapshot(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. Pointer index(Pointer repo) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_repository_index(out, repo); + final out = calloc>(); + final error = libgit2.git_repository_index(out, repo); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Determine if the repository was a shallow clone. @@ -328,16 +320,14 @@ bool isWorktree(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. String message(Pointer repo) { - return using((Arena arena) { - final out = arena(sizeOf()); - final error = libgit2.git_repository_message(out, repo); + final out = calloc(sizeOf()); + final error = libgit2.git_repository_message(out, repo); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.ref.ptr.cast().toDartString(); - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.ref.ptr.cast().toDartString(); + } } /// Remove git's prepared message. @@ -354,16 +344,14 @@ void removeMessage(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. Pointer odb(Pointer repo) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_repository_odb(out, repo); + final out = calloc>(); + final error = libgit2.git_repository_odb(out, repo); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Get the Reference Database Backend for this repository. @@ -376,16 +364,14 @@ Pointer odb(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. Pointer refdb(Pointer repo) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_repository_refdb(out, repo); + final out = calloc>(); + final error = libgit2.git_repository_refdb(out, repo); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Make the repository HEAD point to the specified reference. @@ -401,14 +387,14 @@ Pointer refdb(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. void setHead(Pointer repo, String ref) { - using((Arena arena) { - final refname = ref.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_repository_set_head(repo, refname); + final refname = ref.toNativeUtf8().cast(); + final error = libgit2.git_repository_set_head(repo, refname); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(refname); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Make the repository HEAD directly point to the commit. @@ -462,16 +448,19 @@ void setWorkdir( String path, bool updateGitlink, ) { - using((Arena arena) { - final workdir = path.toNativeUtf8(allocator: arena).cast(); - final updateGitlinkC = updateGitlink ? 1 : 0; - final error = - libgit2.git_repository_set_workdir(repo, workdir, updateGitlinkC); + final workdir = path.toNativeUtf8().cast(); + final updateGitlinkC = updateGitlink ? 1 : 0; + final error = libgit2.git_repository_set_workdir( + repo, + workdir, + updateGitlinkC, + ); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - }); + calloc.free(workdir); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Determines the status of a git repository - ie, whether an operation @@ -511,16 +500,14 @@ String workdir(Pointer repo) { /// /// Throws a [LibGit2Error] if error occured. Pointer wrapODB(Pointer odb) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_repository_wrap_odb(out, odb); + final out = calloc>(); + final error = libgit2.git_repository_wrap_odb(out, odb); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Find a single object, as specified by a [spec] string. @@ -532,21 +519,21 @@ Pointer revParseSingle( Pointer repo, String spec, ) { - return using((Arena arena) { - final out = arena>(); - final specC = spec.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_revparse_single( - out, - repo, - specC, - ); + final out = calloc>(); + final specC = spec.toNativeUtf8().cast(); + final error = libgit2.git_revparse_single( + out, + repo, + specC, + ); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(specC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Free a previously allocated repository. diff --git a/lib/src/bindings/signature.dart b/lib/src/bindings/signature.dart index df2fd84..abc301c 100644 --- a/lib/src/bindings/signature.dart +++ b/lib/src/bindings/signature.dart @@ -16,36 +16,38 @@ Pointer create( int time, int offset, ) { - return using((Arena arena) { - final out = arena>(); - final nameC = name.toNativeUtf8(allocator: arena).cast(); - final emailC = email.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_signature_new(out, nameC, emailC, time, offset); + final out = calloc>(); + final nameC = name.toNativeUtf8().cast(); + final emailC = email.toNativeUtf8().cast(); + final error = libgit2.git_signature_new(out, nameC, emailC, time, offset); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(nameC); + calloc.free(emailC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Create a new action signature with a timestamp of 'now'. /// /// Throws a [LibGit2Error] if error occured. Pointer now(String name, String email) { - return using((Arena arena) { - final out = arena>(); - final nameC = name.toNativeUtf8(allocator: arena).cast(); - final emailC = email.toNativeUtf8(allocator: arena).cast(); - final error = libgit2.git_signature_now(out, nameC, emailC); + final out = calloc>(); + final nameC = name.toNativeUtf8().cast(); + final emailC = email.toNativeUtf8().cast(); + final error = libgit2.git_signature_now(out, nameC, emailC); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + calloc.free(nameC); + calloc.free(emailC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Free an existing signature. diff --git a/lib/src/bindings/tree.dart b/lib/src/bindings/tree.dart index fbeea80..5eb8672 100644 --- a/lib/src/bindings/tree.dart +++ b/lib/src/bindings/tree.dart @@ -11,16 +11,14 @@ Pointer id(Pointer tree) => libgit2.git_tree_id(tree); /// /// Throws a [LibGit2Error] if error occured. Pointer lookup(Pointer repo, Pointer id) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_tree_lookup(out, repo, id); + final out = calloc>(); + final error = libgit2.git_tree_lookup(out, repo, id); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } /// Lookup a tree object from the repository, given a prefix of its identifier (short id). @@ -31,22 +29,15 @@ Pointer lookupPrefix( Pointer id, int len, ) { - return using((Arena arena) { - final out = arena>(); - final error = libgit2.git_tree_lookup_prefix(out, repo, id, len); + final out = calloc>(); + final error = libgit2.git_tree_lookup_prefix(out, repo, id, len); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return out.value; - } - }); + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } else { + return out.value; + } } -/// Close an open tree. -/// -/// You can no longer use the git_tree pointer after this call. -/// -/// IMPORTANT: You MUST call this method when you stop using a tree to release memory. -/// Failure to do so will cause a memory leak. +/// Close an open tree to release memory. void free(Pointer tree) => libgit2.git_tree_free(tree);