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,16 +10,14 @@ import '../util.dart';
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_commit> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) {
return using((Arena arena) {
final out = arena<Pointer<git_commit>>();
final error = libgit2.git_commit_lookup(out, repo, id);
final out = calloc<Pointer<git_commit>>();
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.

View file

@ -11,33 +11,31 @@ import '../util.dart';
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> newConfig() {
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_config_new(out);
final out = calloc<Pointer<git_config>>();
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<git_config> open(String path) {
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_open_ondisk(out, pathC);
final out = calloc<Pointer<git_config>>();
final pathC = path.toNativeUtf8().cast<Int8>();
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<git_config> open(String path) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> openDefault() {
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_config_open_default(out);
final out = calloc<Pointer<git_config>>();
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<git_config> openDefault() {
///
/// Throws an error if file has not been found.
String findGlobal() {
return using((Arena arena) {
final out = arena<git_buf>(sizeOf<git_buf>());
final error = libgit2.git_config_find_global(out);
final path = out.ref.ptr.cast<Utf8>().toDartString();
final out = calloc<git_buf>(sizeOf<git_buf>());
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<Utf8>().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<git_buf>();
final error = libgit2.git_config_find_system(out);
final path = out.ref.ptr.cast<Utf8>().toDartString();
final out = calloc<git_buf>();
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<Utf8>().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<git_buf>();
final error = libgit2.git_config_find_xdg(out);
final path = out.ref.ptr.cast<Utf8>().toDartString();
final out = calloc<git_buf>();
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<Utf8>().toDartString();
}
}
/// Create a snapshot of the configuration.
@ -134,33 +121,31 @@ String findXdg() {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> snapshot(Pointer<git_config> config) {
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_config_snapshot(out, config);
final out = calloc<Pointer<git_config>>();
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<git_config> cfg, String variable) {
return using((Arena arena) {
final out = arena<Pointer<git_config_entry>>();
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_get_entry(out, cfg, name);
final out = calloc<Pointer<git_config_entry>>();
final name = variable.toNativeUtf8().cast<Int8>();
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<Utf8>().toDartString();
}
});
calloc.free(name);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value.ref.value.cast<Utf8>().toDartString();
}
}
/// 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.
void setBool(Pointer<git_config> cfg, String variable, bool value) {
using((Arena arena) {
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final valueC = value ? 1 : 0;
final error = libgit2.git_config_set_bool(cfg, name, valueC);
final name = variable.toNativeUtf8().cast<Int8>();
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<git_config> cfg, String variable, bool value) {
///
/// Throws a [LibGit2Error] if error occured.
void setInt(Pointer<git_config> cfg, String variable, int value) {
using((Arena arena) {
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_set_int64(cfg, name, value);
final name = variable.toNativeUtf8().cast<Int8>();
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<git_config> cfg, String variable, int value) {
///
/// Throws a [LibGit2Error] if error occured.
void setString(Pointer<git_config> cfg, String variable, String value) {
using((Arena arena) {
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final valueC = value.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_set_string(cfg, name, valueC);
final name = variable.toNativeUtf8().cast<Int8>();
final valueC = value.toNativeUtf8().cast<Int8>();
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<String, String> getEntries(Pointer<git_config> cfg) {
return using((Arena arena) {
final iterator = arena<Pointer<git_config_iterator>>();
final entry = arena<Pointer<git_config_entry>>();
libgit2.git_config_iterator_new(iterator, cfg);
var error = 0;
final entries = <String, String>{};
final iterator = calloc<Pointer<git_config_iterator>>();
final entry = calloc<Pointer<git_config_entry>>();
libgit2.git_config_iterator_new(iterator, cfg);
var error = 0;
final entries = <String, String>{};
while (error != -31) {
error = libgit2.git_config_next(entry, iterator.value);
entries[entry.value.ref.name.cast<Utf8>().toDartString()] =
entry.value.ref.value.cast<Utf8>().toDartString();
}
while (error != -31) {
error = libgit2.git_config_next(entry, iterator.value);
entries[entry.value.ref.name.cast<Utf8>().toDartString()] =
entry.value.ref.value.cast<Utf8>().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<String, String> getEntries(Pointer<git_config> cfg) {
///
/// Throws a [LibGit2Error] if error occured.
void delete(Pointer<git_config> cfg, String variable) {
using((Arena arena) {
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_delete_entry(cfg, name);
final name = variable.toNativeUtf8().cast<Int8>();
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<String> multivarValues(
String variable,
String? regexp,
) {
return using((Arena arena) {
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final regexpC =
regexp?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final iterator = arena<Pointer<git_config_iterator>>();
final entry = arena<Pointer<git_config_entry>>();
libgit2.git_config_multivar_iterator_new(iterator, cfg, name, regexpC);
var error = 0;
final entries = <String>[];
final name = variable.toNativeUtf8().cast<Int8>();
final regexpC = regexp?.toNativeUtf8().cast<Int8>() ?? nullptr;
final iterator = calloc<Pointer<git_config_iterator>>();
final entry = calloc<Pointer<git_config_entry>>();
libgit2.git_config_multivar_iterator_new(iterator, cfg, name, regexpC);
var error = 0;
final entries = <String>[];
while (error == 0) {
error = libgit2.git_config_next(entry, iterator.value);
if (error != -31) {
entries.add(entry.value.ref.value.cast<Utf8>().toDartString());
} else {
break;
}
while (error == 0) {
error = libgit2.git_config_next(entry, iterator.value);
if (error != -31) {
entries.add(entry.value.ref.value.cast<Utf8>().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<Int8>();
final regexpC = regexp.toNativeUtf8(allocator: arena).cast<Int8>();
final valueC = value.toNativeUtf8(allocator: arena).cast<Int8>();
libgit2.git_config_set_multivar(cfg, name, regexpC, valueC);
});
final name = variable.toNativeUtf8().cast<Int8>();
final regexpC = regexp.toNativeUtf8().cast<Int8>();
final valueC = value.toNativeUtf8().cast<Int8>();
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<git_config> cfg,
String variable,
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);
});
void deleteMultivar(Pointer<git_config> cfg, String variable, String regexp) {
final name = variable.toNativeUtf8().cast<Int8>();
final regexpC = regexp.toNativeUtf8().cast<Int8>();
libgit2.git_config_delete_multivar(cfg, name, regexpC);
calloc.free(name);
calloc.free(regexpC);
}
/// 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.
bool find(Pointer<git_index> index, String path) {
return using((Arena arena) {
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final result = libgit2.git_index_find(nullptr, index, pathC);
final pathC = path.toNativeUtf8().cast<Int8>();
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<git_index_entry> getByPath(
String path,
int stage,
) {
return using((Arena arena) {
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final result = libgit2.git_index_get_bypath(index, pathC, stage);
final pathC = path.toNativeUtf8().cast<Int8>();
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<git_index> index, Pointer<git_index_entry> sourceEntry) {
///
/// Throws a [LibGit2Error] if error occured.
void addByPath(Pointer<git_index> index, String path) {
using((Arena arena) {
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_index_add_bypath(index, pathC);
final pathC = path.toNativeUtf8().cast<Int8>();
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<git_index> index, String path) {
///
/// Throws a [LibGit2Error] if error occured.
void addAll(Pointer<git_index> index, List<String> pathspec) {
using((Arena arena) {
var pathspecC = arena<git_strarray>();
final List<Pointer<Int8>> pathPointers = pathspec
.map((e) => e.toNativeUtf8(allocator: arena).cast<Int8>())
.toList();
final Pointer<Pointer<Int8>> strArray = arena(pathspec.length);
var pathspecC = calloc<git_strarray>();
final List<Pointer<Int8>> pathPointers =
pathspec.map((e) => e.toNativeUtf8().cast<Int8>()).toList();
final Pointer<Pointer<Int8>> 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<git_index> index) {
///
/// Throws a [LibGit2Error] if error occured.
void remove(Pointer<git_index> index, String path, int stage) {
using((Arena arena) {
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_index_remove(index, pathC, stage);
final pathC = path.toNativeUtf8().cast<Int8>();
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<git_index> index, List<String> pathspec) {
using((Arena arena) {
final pathspecC = arena<git_strarray>();
final List<Pointer<Int8>> pathPointers = pathspec
.map((e) => e.toNativeUtf8(allocator: arena).cast<Int8>())
.toList();
final Pointer<Pointer<Int8>> strArray = arena(pathspec.length);
final pathspecC = calloc<git_strarray>();
final List<Pointer<Int8>> pathPointers =
pathspec.map((e) => e.toNativeUtf8().cast<Int8>()).toList();
final Pointer<Pointer<Int8>> 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.

View file

@ -42,17 +42,17 @@ Pointer<git_oid> fromSHA(String hex) {
///
/// Throws a [LibGit2Error] if error occured.
String toSHA(Pointer<git_oid> id) {
return using((Arena arena) {
final out = arena<Int8>(40);
final error = libgit2.git_oid_fmt(out, id);
final result = out.cast<Utf8>().toDartString(length: 40);
final out = calloc<Int8>(40);
final error = libgit2.git_oid_fmt(out, id);
final result = out.cast<Utf8>().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.

View file

@ -35,16 +35,14 @@ Pointer<git_oid> target(Pointer<git_reference> ref) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> resolve(Pointer<git_reference> ref) {
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final error = libgit2.git_reference_resolve(out, ref);
final out = calloc<Pointer<git_reference>>();
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<git_reference> resolve(Pointer<git_reference> ref) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> lookup(Pointer<git_repository> repo, String name) {
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_reference_lookup(out, repo, nameC);
final out = calloc<Pointer<git_reference>>();
final nameC = name.toNativeUtf8().cast<Int8>();
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<git_reference> lookup(Pointer<git_repository> repo, String name) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> lookupDWIM(Pointer<git_repository> repo, String name) {
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_reference_dwim(out, repo, nameC);
final out = calloc<Pointer<git_reference>>();
final nameC = name.toNativeUtf8().cast<Int8>();
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<git_reference> ref) {
return using((Arena arena) {
var result = arena<Int8>();
result = libgit2.git_reference_name(ref);
var result = calloc<Int8>();
result = libgit2.git_reference_name(ref);
return result.cast<Utf8>().toDartString();
});
return result.cast<Utf8>().toDartString();
}
/// Get the reference's short name.
@ -126,66 +122,64 @@ Pointer<git_reference> rename(
bool force,
String? logMessage,
) {
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final newNameC = newName.toNativeUtf8(allocator: arena).cast<Int8>();
final forceC = force == true ? 1 : 0;
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr;
final error = libgit2.git_reference_rename(
out,
ref,
newNameC,
forceC,
logMessageC,
);
final out = calloc<Pointer<git_reference>>();
final newNameC = newName.toNativeUtf8().cast<Int8>();
final forceC = force == true ? 1 : 0;
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? 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<String> list(Pointer<git_repository> repo) {
return using((Arena arena) {
var array = arena<git_strarray>();
final error = libgit2.git_reference_list(array, repo);
var result = <String>[];
var array = calloc<git_strarray>();
final error = libgit2.git_reference_list(array, repo);
var result = <String>[];
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<Utf8>().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<Utf8>().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<git_repository> repo, String name) {
return using((Arena arena) {
final refname = name.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_reference_has_log(repo, refname);
final refname = name.toNativeUtf8().cast<Int8>();
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<git_reference> createDirect(
bool force,
String? logMessage,
) {
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final forceC = force == true ? 1 : 0;
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr;
final error = libgit2.git_reference_create(
out,
repo,
nameC,
oid,
forceC,
logMessageC,
);
final out = calloc<Pointer<git_reference>>();
final nameC = name.toNativeUtf8().cast<Int8>();
final forceC = force == true ? 1 : 0;
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? 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<git_reference> createSymbolic(
bool force,
String? logMessage,
) {
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final targetC = target.toNativeUtf8().cast<Int8>();
final forceC = force == true ? 1 : 0;
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr;
final error = libgit2.git_reference_symbolic_create(
out,
repo,
nameC,
targetC,
forceC,
logMessageC,
);
final out = calloc<Pointer<git_reference>>();
final nameC = name.toNativeUtf8().cast<Int8>();
final targetC = target.toNativeUtf8().cast<Int8>();
final forceC = force == true ? 1 : 0;
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? 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<git_reference> setTarget(
Pointer<git_oid> oid,
String? logMessage,
) {
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final logMessageC =
logMessage?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final error = libgit2.git_reference_set_target(out, ref, oid, logMessageC);
final out = calloc<Pointer<git_reference>>();
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? 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<git_reference> setTargetSymbolic(
String target,
String? logMessage,
) {
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final targetC = target.toNativeUtf8(allocator: arena).cast<Int8>();
final logMessageC =
logMessage?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final error = libgit2.git_reference_symbolic_set_target(
out, ref, targetC, logMessageC);
final out = calloc<Pointer<git_reference>>();
final targetC = target.toNativeUtf8().cast<Int8>();
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? 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<git_reference> ref1, Pointer<git_reference> 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<Int8>();
final result = libgit2.git_reference_is_valid_name(refname);
final refname = name.toNativeUtf8().cast<Int8>();
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.

View file

@ -13,17 +13,17 @@ import '../util.dart';
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_reflog> read(Pointer<git_repository> repo, String name) {
return using((Arena arena) {
final out = arena<Pointer<git_reflog>>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_reflog_read(out, repo, nameC);
final out = calloc<Pointer<git_reflog>>();
final nameC = name.toNativeUtf8().cast<Int8>();
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.

View file

@ -10,17 +10,17 @@ import '../util.dart';
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> open(String path) {
return using((Arena arena) {
final out = arena<Pointer<git_repository>>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_repository_open(out, pathC);
final out = calloc<Pointer<git_repository>>();
final pathC = path.toNativeUtf8().cast<Int8>();
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<git_repository> open(String path) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> openBare(String barePath) {
return using((Arena arena) {
final out = arena<Pointer<git_repository>>();
final barePathC = barePath.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_repository_open_bare(out, barePathC);
final out = calloc<Pointer<git_repository>>();
final barePathC = barePath.toNativeUtf8().cast<Int8>();
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<git_repository> openBare(String barePath) {
///
/// Throws a [LibGit2Error] if error occured.
String discover(String startPath, String ceilingDirs) {
return using((Arena arena) {
final out = arena<git_buf>(sizeOf<git_buf>());
final startPathC = startPath.toNativeUtf8(allocator: arena).cast<Int8>();
final ceilingDirsC =
ceilingDirs.toNativeUtf8(allocator: arena).cast<Int8>();
final error =
libgit2.git_repository_discover(out, startPathC, 0, ceilingDirsC);
final out = calloc<git_buf>(sizeOf<git_buf>());
final startPathC = startPath.toNativeUtf8().cast<Int8>();
final ceilingDirsC = ceilingDirs.toNativeUtf8().cast<Int8>();
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<Utf8>().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<Utf8>().toDartString();
}
}
/// Creates a new Git repository in the given folder.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> init(String path, bool isBare) {
return using((Arena arena) {
final out = arena<Pointer<git_repository>>();
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final isBareC = isBare ? 1 : 0;
final error = libgit2.git_repository_init(out, pathC, isBareC);
final out = calloc<Pointer<git_repository>>();
final pathC = path.toNativeUtf8().cast<Int8>();
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<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
void setNamespace(Pointer<git_repository> repo, String? namespace) {
using((Arena arena) {
final nmspace =
namespace?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final error = libgit2.git_repository_set_namespace(repo, nmspace);
final nmspace = namespace?.toNativeUtf8().cast<Int8>() ?? 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<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> head(Pointer<git_repository> repo) {
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final error = libgit2.git_repository_head(out, repo);
final out = calloc<Pointer<git_reference>>();
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<git_repository> 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<git_repository> repo, String? name, String? email) {
using((Arena arena) {
final nameC = name?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final emailC =
email?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final nameC = name?.toNativeUtf8().cast<Int8>() ?? nullptr;
final emailC = email?.toNativeUtf8().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.
Map<String, String> identity(Pointer<git_repository> repo) {
return using((Arena arena) {
final name = arena<Pointer<Int8>>();
final email = arena<Pointer<Int8>>();
libgit2.git_repository_ident(name, email, repo);
var identity = <String, String>{};
if (name.value == nullptr && email.value == nullptr) {
return identity;
} else {
identity[name.value.cast<Utf8>().toDartString()] =
email.value.cast<Utf8>().toDartString();
}
final name = calloc<Pointer<Int8>>();
final email = calloc<Pointer<Int8>>();
libgit2.git_repository_ident(name, email, repo);
var identity = <String, String>{};
if (name.value == nullptr && email.value == nullptr) {
return identity;
});
} else {
identity[name.value.cast<Utf8>().toDartString()] =
email.value.cast<Utf8>().toDartString();
}
calloc.free(name);
calloc.free(email);
return identity;
}
/// Get the configuration file for this repository.
@ -250,16 +248,14 @@ Map<String, String> identity(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> config(Pointer<git_repository> repo) {
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_repository_config(out, repo);
final out = calloc<Pointer<git_config>>();
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<git_config> config(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> configSnapshot(Pointer<git_repository> repo) {
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_repository_config_snapshot(out, repo);
final out = calloc<Pointer<git_config>>();
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<git_config> configSnapshot(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_index> index(Pointer<git_repository> repo) {
return using((Arena arena) {
final out = arena<Pointer<git_index>>();
final error = libgit2.git_repository_index(out, repo);
final out = calloc<Pointer<git_index>>();
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<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
String message(Pointer<git_repository> repo) {
return using((Arena arena) {
final out = arena<git_buf>(sizeOf<git_buf>());
final error = libgit2.git_repository_message(out, repo);
final out = calloc<git_buf>(sizeOf<git_buf>());
final error = libgit2.git_repository_message(out, repo);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.ref.ptr.cast<Utf8>().toDartString();
}
});
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.ref.ptr.cast<Utf8>().toDartString();
}
}
/// Remove git's prepared message.
@ -354,16 +344,14 @@ void removeMessage(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_odb> odb(Pointer<git_repository> repo) {
return using((Arena arena) {
final out = arena<Pointer<git_odb>>();
final error = libgit2.git_repository_odb(out, repo);
final out = calloc<Pointer<git_odb>>();
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<git_odb> odb(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_refdb> refdb(Pointer<git_repository> repo) {
return using((Arena arena) {
final out = arena<Pointer<git_refdb>>();
final error = libgit2.git_repository_refdb(out, repo);
final out = calloc<Pointer<git_refdb>>();
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<git_refdb> refdb(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
void setHead(Pointer<git_repository> repo, String ref) {
using((Arena arena) {
final refname = ref.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_repository_set_head(repo, refname);
final refname = ref.toNativeUtf8().cast<Int8>();
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<Int8>();
final updateGitlinkC = updateGitlink ? 1 : 0;
final error =
libgit2.git_repository_set_workdir(repo, workdir, updateGitlinkC);
final workdir = path.toNativeUtf8().cast<Int8>();
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<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> wrapODB(Pointer<git_odb> odb) {
return using((Arena arena) {
final out = arena<Pointer<git_repository>>();
final error = libgit2.git_repository_wrap_odb(out, odb);
final out = calloc<Pointer<git_repository>>();
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<git_object> revParseSingle(
Pointer<git_repository> repo,
String spec,
) {
return using((Arena arena) {
final out = arena<Pointer<git_object>>();
final specC = spec.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_revparse_single(
out,
repo,
specC,
);
final out = calloc<Pointer<git_object>>();
final specC = spec.toNativeUtf8().cast<Int8>();
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.

View file

@ -16,36 +16,38 @@ Pointer<git_signature> create(
int time,
int offset,
) {
return using((Arena arena) {
final out = arena<Pointer<git_signature>>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final emailC = email.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_signature_new(out, nameC, emailC, time, offset);
final out = calloc<Pointer<git_signature>>();
final nameC = name.toNativeUtf8().cast<Int8>();
final emailC = email.toNativeUtf8().cast<Int8>();
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<git_signature> now(String name, String email) {
return using((Arena arena) {
final out = arena<Pointer<git_signature>>();
final nameC = name.toNativeUtf8(allocator: arena).cast<Int8>();
final emailC = email.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_signature_now(out, nameC, emailC);
final out = calloc<Pointer<git_signature>>();
final nameC = name.toNativeUtf8().cast<Int8>();
final emailC = email.toNativeUtf8().cast<Int8>();
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.

View file

@ -11,16 +11,14 @@ Pointer<git_oid> id(Pointer<git_tree> tree) => libgit2.git_tree_id(tree);
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_tree> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) {
return using((Arena arena) {
final out = arena<Pointer<git_tree>>();
final error = libgit2.git_tree_lookup(out, repo, id);
final out = calloc<Pointer<git_tree>>();
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<git_tree> lookupPrefix(
Pointer<git_oid> id,
int len,
) {
return using((Arena arena) {
final out = arena<Pointer<git_tree>>();
final error = libgit2.git_tree_lookup_prefix(out, repo, id, len);
final out = calloc<Pointer<git_tree>>();
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<git_tree> tree) => libgit2.git_tree_free(tree);