refactor: revert 'use ffi Arena for resource management'

This commit is contained in:
Aleksey Kulikov 2021-08-27 15:05:05 +03:00
parent 6a097c1841
commit a78c38d8e3
9 changed files with 588 additions and 620 deletions

View file

@ -10,8 +10,7 @@ import '../util.dart';
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_commit> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) { Pointer<git_commit> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) {
return using((Arena arena) { final out = calloc<Pointer<git_commit>>();
final out = arena<Pointer<git_commit>>();
final error = libgit2.git_commit_lookup(out, repo, id); final error = libgit2.git_commit_lookup(out, repo, id);
if (error < 0) { if (error < 0) {
@ -19,7 +18,6 @@ Pointer<git_commit> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Get the encoding for the message of a commit, as a string representing a standard encoding name. /// Get the encoding for the message of a commit, as a string representing a standard encoding name.

View file

@ -11,8 +11,7 @@ import '../util.dart';
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_config> newConfig() { Pointer<git_config> newConfig() {
return using((Arena arena) { final out = calloc<Pointer<git_config>>();
final out = arena<Pointer<git_config>>();
final error = libgit2.git_config_new(out); final error = libgit2.git_config_new(out);
if (error < 0) { if (error < 0) {
@ -20,24 +19,23 @@ Pointer<git_config> newConfig() {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Create a new config instance containing a single on-disk file /// Create a new config instance containing a single on-disk file
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_config> open(String path) { Pointer<git_config> open(String path) {
return using((Arena arena) { final out = calloc<Pointer<git_config>>();
final out = arena<Pointer<git_config>>(); final pathC = path.toNativeUtf8().cast<Int8>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_open_ondisk(out, pathC); final error = libgit2.git_config_open_ondisk(out, pathC);
calloc.free(pathC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Open the global, XDG and system configuration files /// Open the global, XDG and system configuration files
@ -48,8 +46,7 @@ Pointer<git_config> open(String path) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_config> openDefault() { Pointer<git_config> openDefault() {
return using((Arena arena) { final out = calloc<Pointer<git_config>>();
final out = arena<Pointer<git_config>>();
final error = libgit2.git_config_open_default(out); final error = libgit2.git_config_open_default(out);
if (error < 0) { if (error < 0) {
@ -57,7 +54,6 @@ Pointer<git_config> openDefault() {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Locate the path to the global configuration file /// Locate the path to the global configuration file
@ -74,17 +70,14 @@ Pointer<git_config> openDefault() {
/// ///
/// Throws an error if file has not been found. /// Throws an error if file has not been found.
String findGlobal() { String findGlobal() {
return using((Arena arena) { final out = calloc<git_buf>(sizeOf<git_buf>());
final out = arena<git_buf>(sizeOf<git_buf>());
final error = libgit2.git_config_find_global(out); final error = libgit2.git_config_find_global(out);
final path = out.ref.ptr.cast<Utf8>().toDartString();
if (error != 0) { if (error != 0) {
throw Error(); throw Error();
} else { } else {
return path; return out.ref.ptr.cast<Utf8>().toDartString();
} }
});
} }
/// Locate the path to the system configuration file /// Locate the path to the system configuration file
@ -93,17 +86,14 @@ String findGlobal() {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
String findSystem() { String findSystem() {
return using((Arena arena) { final out = calloc<git_buf>();
final out = arena<git_buf>();
final error = libgit2.git_config_find_system(out); final error = libgit2.git_config_find_system(out);
final path = out.ref.ptr.cast<Utf8>().toDartString();
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return path; return out.ref.ptr.cast<Utf8>().toDartString();
} }
});
} }
/// Locate the path to the global xdg compatible configuration file /// Locate the path to the global xdg compatible configuration file
@ -113,17 +103,14 @@ String findSystem() {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
String findXdg() { String findXdg() {
return using((Arena arena) { final out = calloc<git_buf>();
final out = arena<git_buf>();
final error = libgit2.git_config_find_xdg(out); final error = libgit2.git_config_find_xdg(out);
final path = out.ref.ptr.cast<Utf8>().toDartString();
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return path; return out.ref.ptr.cast<Utf8>().toDartString();
} }
});
} }
/// Create a snapshot of the configuration. /// Create a snapshot of the configuration.
@ -134,8 +121,7 @@ String findXdg() {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_config> snapshot(Pointer<git_config> config) { Pointer<git_config> snapshot(Pointer<git_config> config) {
return using((Arena arena) { final out = calloc<Pointer<git_config>>();
final out = arena<Pointer<git_config>>();
final error = libgit2.git_config_snapshot(out, config); final error = libgit2.git_config_snapshot(out, config);
if (error < 0) { if (error < 0) {
@ -143,24 +129,23 @@ Pointer<git_config> snapshot(Pointer<git_config> config) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Get the value of a config variable. /// Get the value of a config variable.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
String getValue(Pointer<git_config> cfg, String variable) { String getValue(Pointer<git_config> cfg, String variable) {
return using((Arena arena) { final out = calloc<Pointer<git_config_entry>>();
final out = arena<Pointer<git_config_entry>>(); final name = variable.toNativeUtf8().cast<Int8>();
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_get_entry(out, cfg, name); final error = libgit2.git_config_get_entry(out, cfg, name);
calloc.free(name);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value.ref.value.cast<Utf8>().toDartString(); return out.value.ref.value.cast<Utf8>().toDartString();
} }
});
} }
/// Set the value of a boolean config variable in the config file with the /// Set the value of a boolean config variable in the config file with the
@ -168,15 +153,15 @@ String getValue(Pointer<git_config> cfg, String variable) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void setBool(Pointer<git_config> cfg, String variable, bool value) { void setBool(Pointer<git_config> cfg, String variable, bool value) {
using((Arena arena) { final name = variable.toNativeUtf8().cast<Int8>();
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final valueC = value ? 1 : 0; final valueC = value ? 1 : 0;
final error = libgit2.git_config_set_bool(cfg, name, valueC); final error = libgit2.git_config_set_bool(cfg, name, valueC);
calloc.free(name);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Set the value of an integer config variable in the config file with the /// Set the value of an integer config variable in the config file with the
@ -184,14 +169,14 @@ void setBool(Pointer<git_config> cfg, String variable, bool value) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void setInt(Pointer<git_config> cfg, String variable, int value) { void setInt(Pointer<git_config> cfg, String variable, int value) {
using((Arena arena) { final name = variable.toNativeUtf8().cast<Int8>();
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_set_int64(cfg, name, value); final error = libgit2.git_config_set_int64(cfg, name, value);
calloc.free(name);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Set the value of a string config variable in the config file with the /// Set the value of a string config variable in the config file with the
@ -199,22 +184,22 @@ void setInt(Pointer<git_config> cfg, String variable, int value) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void setString(Pointer<git_config> cfg, String variable, String value) { void setString(Pointer<git_config> cfg, String variable, String value) {
using((Arena arena) { final name = variable.toNativeUtf8().cast<Int8>();
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>(); final valueC = value.toNativeUtf8().cast<Int8>();
final valueC = value.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_set_string(cfg, name, valueC); final error = libgit2.git_config_set_string(cfg, name, valueC);
calloc.free(name);
calloc.free(valueC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Iterate over all the config variables. /// Iterate over all the config variables.
Map<String, String> getEntries(Pointer<git_config> cfg) { Map<String, String> getEntries(Pointer<git_config> cfg) {
return using((Arena arena) { final iterator = calloc<Pointer<git_config_iterator>>();
final iterator = arena<Pointer<git_config_iterator>>(); final entry = calloc<Pointer<git_config_entry>>();
final entry = arena<Pointer<git_config_entry>>();
libgit2.git_config_iterator_new(iterator, cfg); libgit2.git_config_iterator_new(iterator, cfg);
var error = 0; var error = 0;
final entries = <String, String>{}; final entries = <String, String>{};
@ -225,8 +210,10 @@ Map<String, String> getEntries(Pointer<git_config> cfg) {
entry.value.ref.value.cast<Utf8>().toDartString(); entry.value.ref.value.cast<Utf8>().toDartString();
} }
calloc.free(iterator);
calloc.free(entry);
return entries; return entries;
});
} }
/// Delete a config variable from the config file with the highest level /// Delete a config variable from the config file with the highest level
@ -234,14 +221,14 @@ Map<String, String> getEntries(Pointer<git_config> cfg) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void delete(Pointer<git_config> cfg, String variable) { void delete(Pointer<git_config> cfg, String variable) {
using((Arena arena) { final name = variable.toNativeUtf8().cast<Int8>();
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_delete_entry(cfg, name); final error = libgit2.git_config_delete_entry(cfg, name);
calloc.free(name);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Iterate over the values of a multivar /// Iterate over the values of a multivar
@ -253,12 +240,10 @@ List<String> multivarValues(
String variable, String variable,
String? regexp, String? regexp,
) { ) {
return using((Arena arena) { final name = variable.toNativeUtf8().cast<Int8>();
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>(); final regexpC = regexp?.toNativeUtf8().cast<Int8>() ?? nullptr;
final regexpC = final iterator = calloc<Pointer<git_config_iterator>>();
regexp?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr; final entry = calloc<Pointer<git_config_entry>>();
final iterator = arena<Pointer<git_config_iterator>>();
final entry = arena<Pointer<git_config_entry>>();
libgit2.git_config_multivar_iterator_new(iterator, cfg, name, regexpC); libgit2.git_config_multivar_iterator_new(iterator, cfg, name, regexpC);
var error = 0; var error = 0;
final entries = <String>[]; final entries = <String>[];
@ -272,8 +257,12 @@ List<String> multivarValues(
} }
} }
calloc.free(name);
calloc.free(regexpC);
calloc.free(iterator);
calloc.free(entry);
return entries; return entries;
});
} }
/// Set the value of a multivar config variable in the config file with the /// Set the value of a multivar config variable in the config file with the
@ -286,28 +275,27 @@ void setMultivar(
String regexp, String regexp,
String value, String value,
) { ) {
using((Arena arena) { final name = variable.toNativeUtf8().cast<Int8>();
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>(); final regexpC = regexp.toNativeUtf8().cast<Int8>();
final regexpC = regexp.toNativeUtf8(allocator: arena).cast<Int8>(); final valueC = value.toNativeUtf8().cast<Int8>();
final valueC = value.toNativeUtf8(allocator: arena).cast<Int8>();
libgit2.git_config_set_multivar(cfg, name, regexpC, valueC); 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 /// Deletes one or several values from a multivar in the config file
/// with the highest level (usually the local one). /// with the highest level (usually the local one).
/// ///
/// The regexp is applied case-sensitively on the value. /// The regexp is applied case-sensitively on the value.
void deleteMultivar( void deleteMultivar(Pointer<git_config> cfg, String variable, String regexp) {
Pointer<git_config> cfg, final name = variable.toNativeUtf8().cast<Int8>();
String variable, final regexpC = regexp.toNativeUtf8().cast<Int8>();
String regexp,
) {
using((Arena arena) {
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final regexpC = regexp.toNativeUtf8(allocator: arena).cast<Int8>();
libgit2.git_config_delete_multivar(cfg, name, regexpC); libgit2.git_config_delete_multivar(cfg, name, regexpC);
});
calloc.free(name);
calloc.free(regexpC);
} }
/// Free the configuration and its associated memory and files. /// Free the configuration and its associated memory and files.

View file

@ -62,12 +62,12 @@ Pointer<git_oid> writeTree(Pointer<git_index> index) {
/// Find the first position of any entries which point to given path in the Git index. /// Find the first position of any entries which point to given path in the Git index.
bool find(Pointer<git_index> index, String path) { bool find(Pointer<git_index> index, String path) {
return using((Arena arena) { final pathC = path.toNativeUtf8().cast<Int8>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final result = libgit2.git_index_find(nullptr, index, pathC); final result = libgit2.git_index_find(nullptr, index, pathC);
calloc.free(pathC);
return result == git_error_code.GIT_ENOTFOUND ? false : true; return result == git_error_code.GIT_ENOTFOUND ? false : true;
});
} }
/// Get the count of entries currently in the index. /// Get the count of entries currently in the index.
@ -98,16 +98,16 @@ Pointer<git_index_entry> getByPath(
String path, String path,
int stage, int stage,
) { ) {
return using((Arena arena) { final pathC = path.toNativeUtf8().cast<Int8>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final result = libgit2.git_index_get_bypath(index, pathC, stage); final result = libgit2.git_index_get_bypath(index, pathC, stage);
calloc.free(pathC);
if (result == nullptr) { if (result == nullptr) {
throw ArgumentError.value('$path was not found'); throw ArgumentError.value('$path was not found');
} else { } else {
return result; return result;
} }
});
} }
/// Clear the contents (all the entries) of an index object. /// Clear the contents (all the entries) of an index object.
@ -152,14 +152,14 @@ void add(Pointer<git_index> index, Pointer<git_index_entry> sourceEntry) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void addByPath(Pointer<git_index> index, String path) { void addByPath(Pointer<git_index> index, String path) {
using((Arena arena) { final pathC = path.toNativeUtf8().cast<Int8>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_index_add_bypath(index, pathC); final error = libgit2.git_index_add_bypath(index, pathC);
calloc.free(pathC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Add or update index entries matching files in the working directory. /// Add or update index entries matching files in the working directory.
@ -172,12 +172,10 @@ void addByPath(Pointer<git_index> index, String path) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void addAll(Pointer<git_index> index, List<String> pathspec) { void addAll(Pointer<git_index> index, List<String> pathspec) {
using((Arena arena) { var pathspecC = calloc<git_strarray>();
var pathspecC = arena<git_strarray>(); final List<Pointer<Int8>> pathPointers =
final List<Pointer<Int8>> pathPointers = pathspec pathspec.map((e) => e.toNativeUtf8().cast<Int8>()).toList();
.map((e) => e.toNativeUtf8(allocator: arena).cast<Int8>()) final Pointer<Pointer<Int8>> strArray = calloc(pathspec.length);
.toList();
final Pointer<Pointer<Int8>> strArray = arena(pathspec.length);
for (var i = 0; i < pathspec.length; i++) { for (var i = 0; i < pathspec.length; i++) {
strArray[i] = pathPointers[i]; strArray[i] = pathPointers[i];
@ -194,10 +192,15 @@ void addAll(Pointer<git_index> index, List<String> pathspec) {
nullptr, nullptr,
); );
calloc.free(pathspecC);
for (var p in pathPointers) {
calloc.free(p);
}
calloc.free(strArray);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Write an existing index object from memory back to disk using an atomic file lock. /// Write an existing index object from memory back to disk using an atomic file lock.
@ -215,26 +218,24 @@ void write(Pointer<git_index> index) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void remove(Pointer<git_index> index, String path, int stage) { void remove(Pointer<git_index> index, String path, int stage) {
using((Arena arena) { final pathC = path.toNativeUtf8().cast<Int8>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_index_remove(index, pathC, stage); final error = libgit2.git_index_remove(index, pathC, stage);
calloc.free(pathC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Remove all matching index entries. /// Remove all matching index entries.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void removeAll(Pointer<git_index> index, List<String> pathspec) { void removeAll(Pointer<git_index> index, List<String> pathspec) {
using((Arena arena) { final pathspecC = calloc<git_strarray>();
final pathspecC = arena<git_strarray>(); final List<Pointer<Int8>> pathPointers =
final List<Pointer<Int8>> pathPointers = pathspec pathspec.map((e) => e.toNativeUtf8().cast<Int8>()).toList();
.map((e) => e.toNativeUtf8(allocator: arena).cast<Int8>()) final Pointer<Pointer<Int8>> strArray = calloc(pathspec.length);
.toList();
final Pointer<Pointer<Int8>> strArray = arena(pathspec.length);
for (var i = 0; i < pathspec.length; i++) { for (var i = 0; i < pathspec.length; i++) {
strArray[i] = pathPointers[i]; strArray[i] = pathPointers[i];
@ -250,10 +251,15 @@ void removeAll(Pointer<git_index> index, List<String> pathspec) {
nullptr, nullptr,
); );
calloc.free(pathspecC);
for (var p in pathPointers) {
calloc.free(p);
}
calloc.free(strArray);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Get the repository this index relates to. /// Get the repository this index relates to.

View file

@ -42,17 +42,17 @@ Pointer<git_oid> fromSHA(String hex) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
String toSHA(Pointer<git_oid> id) { String toSHA(Pointer<git_oid> id) {
return using((Arena arena) { final out = calloc<Int8>(40);
final out = arena<Int8>(40);
final error = libgit2.git_oid_fmt(out, id); final error = libgit2.git_oid_fmt(out, id);
final result = out.cast<Utf8>().toDartString(length: 40); final result = out.cast<Utf8>().toDartString(length: 40);
calloc.free(out);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return result; return result;
} }
});
} }
/// Compare two oid structures. /// Compare two oid structures.

View file

@ -35,8 +35,7 @@ Pointer<git_oid> target(Pointer<git_reference> ref) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> resolve(Pointer<git_reference> ref) { Pointer<git_reference> resolve(Pointer<git_reference> ref) {
return using((Arena arena) { final out = calloc<Pointer<git_reference>>();
final out = arena<Pointer<git_reference>>();
final error = libgit2.git_reference_resolve(out, ref); final error = libgit2.git_reference_resolve(out, ref);
if (error < 0) { if (error < 0) {
@ -44,7 +43,6 @@ Pointer<git_reference> resolve(Pointer<git_reference> ref) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Lookup a reference by name in a repository. /// Lookup a reference by name in a repository.
@ -55,17 +53,17 @@ Pointer<git_reference> resolve(Pointer<git_reference> ref) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> lookup(Pointer<git_repository> repo, String name) { Pointer<git_reference> lookup(Pointer<git_repository> repo, String name) {
return using((Arena arena) { final out = calloc<Pointer<git_reference>>();
final out = arena<Pointer<git_reference>>(); final nameC = name.toNativeUtf8().cast<Int8>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_reference_lookup(out, repo, nameC); final error = libgit2.git_reference_lookup(out, repo, nameC);
calloc.free(nameC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Lookup a reference by DWIMing its short name. /// Lookup a reference by DWIMing its short name.
@ -75,27 +73,25 @@ Pointer<git_reference> lookup(Pointer<git_repository> repo, String name) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> lookupDWIM(Pointer<git_repository> repo, String name) { Pointer<git_reference> lookupDWIM(Pointer<git_repository> repo, String name) {
return using((Arena arena) { final out = calloc<Pointer<git_reference>>();
final out = arena<Pointer<git_reference>>(); final nameC = name.toNativeUtf8().cast<Int8>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_reference_dwim(out, repo, nameC); final error = libgit2.git_reference_dwim(out, repo, nameC);
calloc.free(nameC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Get the full name of a reference. /// Get the full name of a reference.
String name(Pointer<git_reference> ref) { String name(Pointer<git_reference> ref) {
return using((Arena arena) { var result = calloc<Int8>();
var result = arena<Int8>();
result = libgit2.git_reference_name(ref); result = libgit2.git_reference_name(ref);
return result.cast<Utf8>().toDartString(); return result.cast<Utf8>().toDartString();
});
} }
/// Get the reference's short name. /// Get the reference's short name.
@ -126,9 +122,8 @@ Pointer<git_reference> rename(
bool force, bool force,
String? logMessage, String? logMessage,
) { ) {
return using((Arena arena) { final out = calloc<Pointer<git_reference>>();
final out = arena<Pointer<git_reference>>(); final newNameC = newName.toNativeUtf8().cast<Int8>();
final newNameC = newName.toNativeUtf8(allocator: arena).cast<Int8>();
final forceC = force == true ? 1 : 0; final forceC = force == true ? 1 : 0;
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr; final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr;
final error = libgit2.git_reference_rename( final error = libgit2.git_reference_rename(
@ -139,23 +134,21 @@ Pointer<git_reference> rename(
logMessageC, logMessageC,
); );
calloc.free(newNameC);
calloc.free(logMessageC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Fill a list with all the references that can be found in a repository. /// 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. /// Throws a [LibGit2Error] if error occured.
List<String> list(Pointer<git_repository> repo) { List<String> list(Pointer<git_repository> repo) {
return using((Arena arena) { var array = calloc<git_strarray>();
var array = arena<git_strarray>();
final error = libgit2.git_reference_list(array, repo); final error = libgit2.git_reference_list(array, repo);
var result = <String>[]; var result = <String>[];
@ -168,24 +161,25 @@ List<String> list(Pointer<git_repository> repo) {
} }
} }
calloc.free(array);
return result; return result;
});
} }
/// Check if a reflog exists for the specified reference. /// Check if a reflog exists for the specified reference.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
bool hasLog(Pointer<git_repository> repo, String name) { bool hasLog(Pointer<git_repository> repo, String name) {
return using((Arena arena) { final refname = name.toNativeUtf8().cast<Int8>();
final refname = name.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_reference_has_log(repo, refname); final error = libgit2.git_reference_has_log(repo, refname);
calloc.free(refname);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return error == 1 ? true : false; return error == 1 ? true : false;
} }
});
} }
/// Check if a reference is a local branch. /// Check if a reference is a local branch.
@ -241,9 +235,8 @@ Pointer<git_reference> createDirect(
bool force, bool force,
String? logMessage, String? logMessage,
) { ) {
return using((Arena arena) { final out = calloc<Pointer<git_reference>>();
final out = arena<Pointer<git_reference>>(); final nameC = name.toNativeUtf8().cast<Int8>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final forceC = force == true ? 1 : 0; final forceC = force == true ? 1 : 0;
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr; final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr;
final error = libgit2.git_reference_create( final error = libgit2.git_reference_create(
@ -255,12 +248,14 @@ Pointer<git_reference> createDirect(
logMessageC, logMessageC,
); );
calloc.free(nameC);
calloc.free(logMessageC);
if (error < 0) { if (error < 0) {
throw (LibGit2Error(libgit2.git_error_last())); throw (LibGit2Error(libgit2.git_error_last()));
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Create a new symbolic reference. /// Create a new symbolic reference.
@ -292,9 +287,8 @@ Pointer<git_reference> createSymbolic(
bool force, bool force,
String? logMessage, String? logMessage,
) { ) {
return using((Arena arena) { final out = calloc<Pointer<git_reference>>();
final out = arena<Pointer<git_reference>>(); final nameC = name.toNativeUtf8().cast<Int8>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final targetC = target.toNativeUtf8().cast<Int8>(); final targetC = target.toNativeUtf8().cast<Int8>();
final forceC = force == true ? 1 : 0; final forceC = force == true ? 1 : 0;
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr; final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr;
@ -307,12 +301,15 @@ Pointer<git_reference> createSymbolic(
logMessageC, logMessageC,
); );
calloc.free(nameC);
calloc.free(targetC);
calloc.free(logMessageC);
if (error < 0) { if (error < 0) {
throw (LibGit2Error(libgit2.git_error_last())); throw (LibGit2Error(libgit2.git_error_last()));
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Delete an existing reference. /// Delete an existing reference.
@ -345,18 +342,17 @@ Pointer<git_reference> setTarget(
Pointer<git_oid> oid, Pointer<git_oid> oid,
String? logMessage, String? logMessage,
) { ) {
return using((Arena arena) { final out = calloc<Pointer<git_reference>>();
final out = arena<Pointer<git_reference>>(); final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr;
final logMessageC =
logMessage?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final error = libgit2.git_reference_set_target(out, ref, oid, logMessageC); final error = libgit2.git_reference_set_target(out, ref, oid, logMessageC);
calloc.free(logMessageC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Create a new reference with the same name as the given reference but a different /// Create a new reference with the same name as the given reference but a different
@ -375,20 +371,20 @@ Pointer<git_reference> setTargetSymbolic(
String target, String target,
String? logMessage, String? logMessage,
) { ) {
return using((Arena arena) { final out = calloc<Pointer<git_reference>>();
final out = arena<Pointer<git_reference>>(); final targetC = target.toNativeUtf8().cast<Int8>();
final targetC = target.toNativeUtf8(allocator: arena).cast<Int8>(); final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr;
final logMessageC = final error =
logMessage?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr; libgit2.git_reference_symbolic_set_target(out, ref, targetC, logMessageC);
final error = libgit2.git_reference_symbolic_set_target(
out, ref, targetC, logMessageC); calloc.free(targetC);
calloc.free(logMessageC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Compare two references. /// Compare two references.
@ -407,12 +403,12 @@ bool compare(Pointer<git_reference> ref1, Pointer<git_reference> ref2) {
/// the characters '~', '^', ':', '\', '?', '[', and '*', and the sequences ".." /// the characters '~', '^', ':', '\', '?', '[', and '*', and the sequences ".."
/// and "@{" which have special meaning to revparse. /// and "@{" which have special meaning to revparse.
bool isValidName(String name) { bool isValidName(String name) {
return using((Arena arena) { final refname = name.toNativeUtf8().cast<Int8>();
final refname = name.toNativeUtf8(allocator: arena).cast<Int8>();
final result = libgit2.git_reference_is_valid_name(refname); final result = libgit2.git_reference_is_valid_name(refname);
calloc.free(refname);
return result == 1 ? true : false; return result == 1 ? true : false;
});
} }
/// Free the given reference. /// Free the given reference.

View file

@ -13,17 +13,17 @@ import '../util.dart';
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_reflog> read(Pointer<git_repository> repo, String name) { Pointer<git_reflog> read(Pointer<git_repository> repo, String name) {
return using((Arena arena) { final out = calloc<Pointer<git_reflog>>();
final out = arena<Pointer<git_reflog>>(); final nameC = name.toNativeUtf8().cast<Int8>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_reflog_read(out, repo, nameC); final error = libgit2.git_reflog_read(out, repo, nameC);
calloc.free(nameC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Get the number of log entries in a reflog. /// Get the number of log entries in a reflog.

View file

@ -10,17 +10,17 @@ import '../util.dart';
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> open(String path) { Pointer<git_repository> open(String path) {
return using((Arena arena) { final out = calloc<Pointer<git_repository>>();
final out = arena<Pointer<git_repository>>(); final pathC = path.toNativeUtf8().cast<Int8>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_repository_open(out, pathC); final error = libgit2.git_repository_open(out, pathC);
calloc.free(pathC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Attempt to open an already-existing bare repository at [bare_path]. /// Attempt to open an already-existing bare repository at [bare_path].
@ -29,17 +29,17 @@ Pointer<git_repository> open(String path) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> openBare(String barePath) { Pointer<git_repository> openBare(String barePath) {
return using((Arena arena) { final out = calloc<Pointer<git_repository>>();
final out = arena<Pointer<git_repository>>(); final barePathC = barePath.toNativeUtf8().cast<Int8>();
final barePathC = barePath.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_repository_open_bare(out, barePathC); final error = libgit2.git_repository_open_bare(out, barePathC);
calloc.free(barePathC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Look for a git repository and return its path. The lookup start from [startPath] /// Look for a git repository and return its path. The lookup start from [startPath]
@ -50,14 +50,15 @@ Pointer<git_repository> openBare(String barePath) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
String discover(String startPath, String ceilingDirs) { String discover(String startPath, String ceilingDirs) {
return using((Arena arena) { final out = calloc<git_buf>(sizeOf<git_buf>());
final out = arena<git_buf>(sizeOf<git_buf>()); final startPathC = startPath.toNativeUtf8().cast<Int8>();
final startPathC = startPath.toNativeUtf8(allocator: arena).cast<Int8>(); final ceilingDirsC = ceilingDirs.toNativeUtf8().cast<Int8>();
final ceilingDirsC =
ceilingDirs.toNativeUtf8(allocator: arena).cast<Int8>();
final error = final error =
libgit2.git_repository_discover(out, startPathC, 0, ceilingDirsC); libgit2.git_repository_discover(out, startPathC, 0, ceilingDirsC);
calloc.free(startPathC);
calloc.free(ceilingDirsC);
if (error == git_error_code.GIT_ENOTFOUND) { if (error == git_error_code.GIT_ENOTFOUND) {
return ''; return '';
} else if (error < 0) { } else if (error < 0) {
@ -65,25 +66,24 @@ String discover(String startPath, String ceilingDirs) {
} else { } else {
return out.ref.ptr.cast<Utf8>().toDartString(); return out.ref.ptr.cast<Utf8>().toDartString();
} }
});
} }
/// Creates a new Git repository in the given folder. /// Creates a new Git repository in the given folder.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> init(String path, bool isBare) { Pointer<git_repository> init(String path, bool isBare) {
return using((Arena arena) { final out = calloc<Pointer<git_repository>>();
final out = arena<Pointer<git_repository>>(); final pathC = path.toNativeUtf8().cast<Int8>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final isBareC = isBare ? 1 : 0; final isBareC = isBare ? 1 : 0;
final error = libgit2.git_repository_init(out, pathC, isBareC); final error = libgit2.git_repository_init(out, pathC, isBareC);
calloc.free(pathC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Returns the path to the `.git` folder for normal repositories or the /// Returns the path to the `.git` folder for normal repositories or the
@ -125,15 +125,14 @@ String getNamespace(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void setNamespace(Pointer<git_repository> repo, String? namespace) { void setNamespace(Pointer<git_repository> repo, String? namespace) {
using((Arena arena) { final nmspace = namespace?.toNativeUtf8().cast<Int8>() ?? nullptr;
final nmspace =
namespace?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final error = libgit2.git_repository_set_namespace(repo, nmspace); final error = libgit2.git_repository_set_namespace(repo, nmspace);
calloc.free(nmspace);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Check if a repository is bare or not. /// Check if a repository is bare or not.
@ -165,8 +164,7 @@ bool isEmpty(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> head(Pointer<git_repository> repo) { Pointer<git_reference> head(Pointer<git_repository> repo) {
return using((Arena arena) { final out = calloc<Pointer<git_reference>>();
final out = arena<Pointer<git_reference>>();
final error = libgit2.git_repository_head(out, repo); final error = libgit2.git_repository_head(out, repo);
if (error < 0) { if (error < 0) {
@ -174,7 +172,6 @@ Pointer<git_reference> head(Pointer<git_repository> repo) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Check if a repository's HEAD is detached. /// Check if a repository's HEAD is detached.
@ -213,20 +210,19 @@ bool isBranchUnborn(Pointer<git_repository> repo) {
/// If both are set, this name and email will be used to write to the reflog. /// 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(Pointer<git_repository> repo, String? name, String? email) { void setIdentity(Pointer<git_repository> repo, String? name, String? email) {
using((Arena arena) { final nameC = name?.toNativeUtf8().cast<Int8>() ?? nullptr;
final nameC = name?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr; final emailC = email?.toNativeUtf8().cast<Int8>() ?? nullptr;
final emailC =
email?.toNativeUtf8(allocator: arena).cast<Int8>() ?? 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. /// Retrieve the configured identity to use for reflogs.
Map<String, String> identity(Pointer<git_repository> repo) { Map<String, String> identity(Pointer<git_repository> repo) {
return using((Arena arena) { final name = calloc<Pointer<Int8>>();
final name = arena<Pointer<Int8>>(); final email = calloc<Pointer<Int8>>();
final email = arena<Pointer<Int8>>();
libgit2.git_repository_ident(name, email, repo); libgit2.git_repository_ident(name, email, repo);
var identity = <String, String>{}; var identity = <String, String>{};
@ -237,8 +233,10 @@ Map<String, String> identity(Pointer<git_repository> repo) {
email.value.cast<Utf8>().toDartString(); email.value.cast<Utf8>().toDartString();
} }
calloc.free(name);
calloc.free(email);
return identity; return identity;
});
} }
/// Get the configuration file for this repository. /// Get the configuration file for this repository.
@ -250,8 +248,7 @@ Map<String, String> identity(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_config> config(Pointer<git_repository> repo) { Pointer<git_config> config(Pointer<git_repository> repo) {
return using((Arena arena) { final out = calloc<Pointer<git_config>>();
final out = arena<Pointer<git_config>>();
final error = libgit2.git_repository_config(out, repo); final error = libgit2.git_repository_config(out, repo);
if (error < 0) { if (error < 0) {
@ -259,7 +256,6 @@ Pointer<git_config> config(Pointer<git_repository> repo) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Get a snapshot of the repository's configuration. /// Get a snapshot of the repository's configuration.
@ -271,8 +267,7 @@ Pointer<git_config> config(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_config> configSnapshot(Pointer<git_repository> repo) { Pointer<git_config> configSnapshot(Pointer<git_repository> repo) {
return using((Arena arena) { final out = calloc<Pointer<git_config>>();
final out = arena<Pointer<git_config>>();
final error = libgit2.git_repository_config_snapshot(out, repo); final error = libgit2.git_repository_config_snapshot(out, repo);
if (error < 0) { if (error < 0) {
@ -280,7 +275,6 @@ Pointer<git_config> configSnapshot(Pointer<git_repository> repo) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Get the Index file for this repository. /// Get the Index file for this repository.
@ -292,8 +286,7 @@ Pointer<git_config> configSnapshot(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_index> index(Pointer<git_repository> repo) { Pointer<git_index> index(Pointer<git_repository> repo) {
return using((Arena arena) { final out = calloc<Pointer<git_index>>();
final out = arena<Pointer<git_index>>();
final error = libgit2.git_repository_index(out, repo); final error = libgit2.git_repository_index(out, repo);
if (error < 0) { if (error < 0) {
@ -301,7 +294,6 @@ Pointer<git_index> index(Pointer<git_repository> repo) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Determine if the repository was a shallow clone. /// Determine if the repository was a shallow clone.
@ -328,8 +320,7 @@ bool isWorktree(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
String message(Pointer<git_repository> repo) { String message(Pointer<git_repository> repo) {
return using((Arena arena) { final out = calloc<git_buf>(sizeOf<git_buf>());
final out = arena<git_buf>(sizeOf<git_buf>());
final error = libgit2.git_repository_message(out, repo); final error = libgit2.git_repository_message(out, repo);
if (error < 0) { if (error < 0) {
@ -337,7 +328,6 @@ String message(Pointer<git_repository> repo) {
} else { } else {
return out.ref.ptr.cast<Utf8>().toDartString(); return out.ref.ptr.cast<Utf8>().toDartString();
} }
});
} }
/// Remove git's prepared message. /// Remove git's prepared message.
@ -354,8 +344,7 @@ void removeMessage(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_odb> odb(Pointer<git_repository> repo) { Pointer<git_odb> odb(Pointer<git_repository> repo) {
return using((Arena arena) { final out = calloc<Pointer<git_odb>>();
final out = arena<Pointer<git_odb>>();
final error = libgit2.git_repository_odb(out, repo); final error = libgit2.git_repository_odb(out, repo);
if (error < 0) { if (error < 0) {
@ -363,7 +352,6 @@ Pointer<git_odb> odb(Pointer<git_repository> repo) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Get the Reference Database Backend for this repository. /// Get the Reference Database Backend for this repository.
@ -376,8 +364,7 @@ Pointer<git_odb> odb(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_refdb> refdb(Pointer<git_repository> repo) { Pointer<git_refdb> refdb(Pointer<git_repository> repo) {
return using((Arena arena) { final out = calloc<Pointer<git_refdb>>();
final out = arena<Pointer<git_refdb>>();
final error = libgit2.git_repository_refdb(out, repo); final error = libgit2.git_repository_refdb(out, repo);
if (error < 0) { if (error < 0) {
@ -385,7 +372,6 @@ Pointer<git_refdb> refdb(Pointer<git_repository> repo) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Make the repository HEAD point to the specified reference. /// Make the repository HEAD point to the specified reference.
@ -401,14 +387,14 @@ Pointer<git_refdb> refdb(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void setHead(Pointer<git_repository> repo, String ref) { void setHead(Pointer<git_repository> repo, String ref) {
using((Arena arena) { final refname = ref.toNativeUtf8().cast<Int8>();
final refname = ref.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_repository_set_head(repo, refname); final error = libgit2.git_repository_set_head(repo, refname);
calloc.free(refname);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Make the repository HEAD directly point to the commit. /// Make the repository HEAD directly point to the commit.
@ -462,16 +448,19 @@ void setWorkdir(
String path, String path,
bool updateGitlink, bool updateGitlink,
) { ) {
using((Arena arena) { final workdir = path.toNativeUtf8().cast<Int8>();
final workdir = path.toNativeUtf8(allocator: arena).cast<Int8>();
final updateGitlinkC = updateGitlink ? 1 : 0; final updateGitlinkC = updateGitlink ? 1 : 0;
final error = final error = libgit2.git_repository_set_workdir(
libgit2.git_repository_set_workdir(repo, workdir, updateGitlinkC); repo,
workdir,
updateGitlinkC,
);
calloc.free(workdir);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
});
} }
/// Determines the status of a git repository - ie, whether an operation /// Determines the status of a git repository - ie, whether an operation
@ -511,8 +500,7 @@ String workdir(Pointer<git_repository> repo) {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> wrapODB(Pointer<git_odb> odb) { Pointer<git_repository> wrapODB(Pointer<git_odb> odb) {
return using((Arena arena) { final out = calloc<Pointer<git_repository>>();
final out = arena<Pointer<git_repository>>();
final error = libgit2.git_repository_wrap_odb(out, odb); final error = libgit2.git_repository_wrap_odb(out, odb);
if (error < 0) { if (error < 0) {
@ -520,7 +508,6 @@ Pointer<git_repository> wrapODB(Pointer<git_odb> odb) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Find a single object, as specified by a [spec] string. /// Find a single object, as specified by a [spec] string.
@ -532,21 +519,21 @@ Pointer<git_object> revParseSingle(
Pointer<git_repository> repo, Pointer<git_repository> repo,
String spec, String spec,
) { ) {
return using((Arena arena) { final out = calloc<Pointer<git_object>>();
final out = arena<Pointer<git_object>>(); final specC = spec.toNativeUtf8().cast<Int8>();
final specC = spec.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_revparse_single( final error = libgit2.git_revparse_single(
out, out,
repo, repo,
specC, specC,
); );
calloc.free(specC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Free a previously allocated repository. /// Free a previously allocated repository.

View file

@ -16,36 +16,38 @@ Pointer<git_signature> create(
int time, int time,
int offset, int offset,
) { ) {
return using((Arena arena) { final out = calloc<Pointer<git_signature>>();
final out = arena<Pointer<git_signature>>(); final nameC = name.toNativeUtf8().cast<Int8>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>(); final emailC = email.toNativeUtf8().cast<Int8>();
final emailC = email.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_signature_new(out, nameC, emailC, time, offset); final error = libgit2.git_signature_new(out, nameC, emailC, time, offset);
calloc.free(nameC);
calloc.free(emailC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Create a new action signature with a timestamp of 'now'. /// Create a new action signature with a timestamp of 'now'.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_signature> now(String name, String email) { Pointer<git_signature> now(String name, String email) {
return using((Arena arena) { final out = calloc<Pointer<git_signature>>();
final out = arena<Pointer<git_signature>>(); final nameC = name.toNativeUtf8().cast<Int8>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>(); final emailC = email.toNativeUtf8().cast<Int8>();
final emailC = email.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_signature_now(out, nameC, emailC); final error = libgit2.git_signature_now(out, nameC, emailC);
calloc.free(nameC);
calloc.free(emailC);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Free an existing signature. /// Free an existing signature.

View file

@ -11,8 +11,7 @@ Pointer<git_oid> id(Pointer<git_tree> tree) => libgit2.git_tree_id(tree);
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_tree> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) { Pointer<git_tree> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) {
return using((Arena arena) { final out = calloc<Pointer<git_tree>>();
final out = arena<Pointer<git_tree>>();
final error = libgit2.git_tree_lookup(out, repo, id); final error = libgit2.git_tree_lookup(out, repo, id);
if (error < 0) { if (error < 0) {
@ -20,7 +19,6 @@ Pointer<git_tree> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) {
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Lookup a tree object from the repository, given a prefix of its identifier (short id). /// Lookup a tree object from the repository, given a prefix of its identifier (short id).
@ -31,8 +29,7 @@ Pointer<git_tree> lookupPrefix(
Pointer<git_oid> id, Pointer<git_oid> id,
int len, int len,
) { ) {
return using((Arena arena) { final out = calloc<Pointer<git_tree>>();
final out = arena<Pointer<git_tree>>();
final error = libgit2.git_tree_lookup_prefix(out, repo, id, len); final error = libgit2.git_tree_lookup_prefix(out, repo, id, len);
if (error < 0) { if (error < 0) {
@ -40,13 +37,7 @@ Pointer<git_tree> lookupPrefix(
} else { } else {
return out.value; return out.value;
} }
});
} }
/// Close an open tree. /// Close an open tree to release memory.
///
/// 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.
void free(Pointer<git_tree> tree) => libgit2.git_tree_free(tree); void free(Pointer<git_tree> tree) => libgit2.git_tree_free(tree);