refactor: use ffi Arena for resource management

This commit is contained in:
Aleksey Kulikov 2021-08-25 18:33:17 +03:00
parent d0bb7aaa0f
commit 747996b40c
17 changed files with 629 additions and 582 deletions

View file

@ -1,5 +1,12 @@
export 'src/repository.dart';
export 'src/config.dart';
export 'src/commit.dart';
export 'src/index.dart';
export 'src/odb.dart';
export 'src/oid.dart';
export 'src/reference.dart';
export 'src/reflog.dart';
export 'src/tree.dart';
export 'src/signature.dart';
export 'src/error.dart';
export 'src/enums.dart';

View file

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

View file

@ -11,7 +11,8 @@ import '../util.dart';
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> newConfig() {
final out = calloc<Pointer<git_config>>();
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_config_new(out);
if (error < 0) {
@ -19,22 +20,24 @@ Pointer<git_config> newConfig() {
} 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) {
final out = calloc<Pointer<git_config>>();
final pathC = path.toNativeUtf8().cast<Int8>();
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);
calloc.free(pathC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
});
}
/// Open the global, XDG and system configuration files
@ -45,7 +48,8 @@ Pointer<git_config> open(String path) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> openDefault() {
final out = calloc<Pointer<git_config>>();
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_config_open_default(out);
if (error < 0) {
@ -53,6 +57,7 @@ Pointer<git_config> openDefault() {
} else {
return out.value;
}
});
}
/// Locate the path to the global configuration file
@ -69,16 +74,17 @@ Pointer<git_config> openDefault() {
///
/// Throws an error if file has not been found.
String findGlobal() {
final out = calloc<git_buf>(sizeOf<git_buf>());
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();
calloc.free(out);
if (error != 0) {
throw Error();
} else {
return path;
}
});
}
/// Locate the path to the system configuration file
@ -87,16 +93,17 @@ String findGlobal() {
///
/// Throws a [LibGit2Error] if error occured.
String findSystem() {
final out = calloc<git_buf>();
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();
calloc.free(out);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return path;
}
});
}
/// Locate the path to the global xdg compatible configuration file
@ -106,16 +113,17 @@ String findSystem() {
///
/// Throws a [LibGit2Error] if error occured.
String findXdg() {
final out = calloc<git_buf>();
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();
calloc.free(out);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return path;
}
});
}
/// Create a snapshot of the configuration.
@ -126,7 +134,8 @@ String findXdg() {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> snapshot(Pointer<git_config> config) {
final out = calloc<Pointer<git_config>>();
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_config_snapshot(out, config);
if (error < 0) {
@ -134,24 +143,24 @@ Pointer<git_config> snapshot(Pointer<git_config> config) {
} 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) {
final out = calloc<Pointer<git_config_entry>>();
final name = variable.toNativeUtf8().cast<Int8>();
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 value = out.value;
calloc.free(out);
calloc.free(name);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return 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
@ -159,14 +168,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) {
final name = variable.toNativeUtf8().cast<Int8>();
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);
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
@ -174,13 +184,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) {
final name = variable.toNativeUtf8().cast<Int8>();
using((Arena arena) {
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_set_int64(cfg, name, value);
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
@ -188,21 +199,22 @@ 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) {
final name = variable.toNativeUtf8().cast<Int8>();
final valueC = value.toNativeUtf8().cast<Int8>();
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);
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) {
final iterator = calloc<Pointer<git_config_iterator>>();
final entry = calloc<Pointer<git_config_entry>>();
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>{};
@ -212,10 +224,9 @@ Map<String, String> getEntries(Pointer<git_config> cfg) {
entries[entry.value.ref.name.cast<Utf8>().toDartString()] =
entry.value.ref.value.cast<Utf8>().toDartString();
}
calloc.free(entry);
calloc.free(iterator);
return entries;
});
}
/// Delete a config variable from the config file with the highest level
@ -223,13 +234,14 @@ Map<String, String> getEntries(Pointer<git_config> cfg) {
///
/// Throws a [LibGit2Error] if error occured.
void delete(Pointer<git_config> cfg, String variable) {
final name = variable.toNativeUtf8().cast<Int8>();
using((Arena arena) {
final name = variable.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_config_delete_entry(cfg, name);
calloc.free(name);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
});
}
/// Iterate over the values of a multivar
@ -241,10 +253,12 @@ List<String> multivarValues(
String variable,
String? regexp,
) {
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>>();
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>[];
@ -258,12 +272,8 @@ List<String> multivarValues(
}
}
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
@ -276,14 +286,12 @@ void setMultivar(
String regexp,
String value,
) {
final name = variable.toNativeUtf8().cast<Int8>();
final regexpC = regexp.toNativeUtf8().cast<Int8>();
final valueC = value.toNativeUtf8().cast<Int8>();
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);
calloc.free(name);
calloc.free(regexpC);
calloc.free(valueC);
});
}
/// Deletes one or several values from a multivar in the config file
@ -295,12 +303,11 @@ void deleteMultivar(
String variable,
String regexp,
) {
final name = variable.toNativeUtf8().cast<Int8>();
final regexpC = regexp.toNativeUtf8().cast<Int8>();
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);
calloc.free(name);
calloc.free(regexpC);
});
}
/// Free the configuration and its associated memory and files.

View file

@ -62,11 +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) {
final pathC = path.toNativeUtf8().cast<Int8>();
return using((Arena arena) {
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final result = libgit2.git_index_find(nullptr, index, pathC);
calloc.free(pathC);
return result == git_error_code.GIT_ENOTFOUND ? false : true;
});
}
/// Get the count of entries currently in the index.
@ -97,15 +98,16 @@ Pointer<git_index_entry> getByPath(
String path,
int stage,
) {
final pathC = path.toNativeUtf8().cast<Int8>();
return using((Arena arena) {
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final result = libgit2.git_index_get_bypath(index, pathC, stage);
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.
@ -150,13 +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) {
final pathC = path.toNativeUtf8().cast<Int8>();
using((Arena arena) {
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_index_add_bypath(index, pathC);
calloc.free(pathC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
});
}
/// Add or update index entries matching files in the working directory.
@ -169,10 +172,12 @@ void addByPath(Pointer<git_index> index, String path) {
///
/// Throws a [LibGit2Error] if error occured.
void addAll(Pointer<git_index> index, List<String> pathspec) {
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);
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);
for (var i = 0; i < pathspec.length; i++) {
strArray[i] = pathPointers[i];
@ -189,15 +194,10 @@ void addAll(Pointer<git_index> index, List<String> pathspec) {
nullptr,
);
calloc.free(pathspecC);
calloc.free(strArray);
for (var p in pathPointers) {
calloc.free(p);
}
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,23 +215,26 @@ void write(Pointer<git_index> index) {
///
/// Throws a [LibGit2Error] if error occured.
void remove(Pointer<git_index> index, String path, int stage) {
final pathC = path.toNativeUtf8().cast<Int8>();
using((Arena arena) {
final pathC = path.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_index_remove(index, pathC, stage);
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) {
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);
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);
for (var i = 0; i < pathspec.length; i++) {
strArray[i] = pathPointers[i];
@ -247,15 +250,10 @@ void removeAll(Pointer<git_index> index, List<String> pathspec) {
nullptr,
);
calloc.free(pathspecC);
calloc.free(strArray);
for (var p in pathPointers) {
calloc.free(p);
}
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
});
}
/// Get the repository this index relates to.

View file

@ -42,16 +42,17 @@ Pointer<git_oid> fromSHA(String hex) {
///
/// Throws a [LibGit2Error] if error occured.
String toSHA(Pointer<git_oid> id) {
final out = calloc.allocate<Int8>(40);
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);
malloc.free(out);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return result;
}
});
}
/// Compare two oid structures.

View file

@ -35,7 +35,8 @@ Pointer<git_oid> target(Pointer<git_reference> ref) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> resolve(Pointer<git_reference> ref) {
final out = calloc<Pointer<git_reference>>();
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final error = libgit2.git_reference_resolve(out, ref);
if (error < 0) {
@ -43,6 +44,7 @@ Pointer<git_reference> resolve(Pointer<git_reference> ref) {
} else {
return out.value;
}
});
}
/// Lookup a reference by name in a repository.
@ -53,16 +55,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) {
final out = calloc<Pointer<git_reference>>();
final nameC = name.toNativeUtf8().cast<Int8>();
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);
calloc.free(nameC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
});
}
/// Lookup a reference by DWIMing its short name.
@ -72,24 +75,27 @@ 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) {
final out = calloc<Pointer<git_reference>>();
final nameC = name.toNativeUtf8().cast<Int8>();
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);
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) {
var result = calloc<Int8>();
return using((Arena arena) {
var result = arena<Int8>();
result = libgit2.git_reference_name(ref);
return result.cast<Utf8>().toDartString();
});
}
/// Get the reference's short name.
@ -120,8 +126,9 @@ Pointer<git_reference> rename(
bool force,
String? logMessage,
) {
final out = calloc<Pointer<git_reference>>();
final newNameC = newName.toNativeUtf8().cast<Int8>();
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(
@ -131,14 +138,13 @@ Pointer<git_reference> rename(
forceC,
logMessageC,
);
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.
@ -148,7 +154,8 @@ Pointer<git_reference> rename(
///
/// Throws a [LibGit2Error] if error occured.
List<String> list(Pointer<git_repository> repo) {
var array = calloc<git_strarray>();
return using((Arena arena) {
var array = arena<git_strarray>();
final error = libgit2.git_reference_list(array, repo);
var result = <String>[];
@ -161,23 +168,24 @@ List<String> list(Pointer<git_repository> repo) {
}
}
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) {
final refname = name.toNativeUtf8().cast<Int8>();
return using((Arena arena) {
final refname = name.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_reference_has_log(repo, refname);
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.
@ -233,8 +241,9 @@ Pointer<git_reference> createDirect(
bool force,
String? logMessage,
) {
final out = calloc<Pointer<git_reference>>();
final nameC = name.toNativeUtf8().cast<Int8>();
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(
@ -245,14 +254,13 @@ Pointer<git_reference> createDirect(
forceC,
logMessageC,
);
calloc.free(nameC);
calloc.free(logMessageC);
if (error < 0) {
throw (LibGit2Error(libgit2.git_error_last()));
} else {
return out.value;
}
});
}
/// Create a new symbolic reference.
@ -284,8 +292,9 @@ Pointer<git_reference> createSymbolic(
bool force,
String? logMessage,
) {
final out = calloc<Pointer<git_reference>>();
final nameC = name.toNativeUtf8().cast<Int8>();
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;
@ -297,15 +306,13 @@ Pointer<git_reference> createSymbolic(
forceC,
logMessageC,
);
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.
@ -338,16 +345,18 @@ Pointer<git_reference> setTarget(
Pointer<git_oid> oid,
String? logMessage,
) {
final out = calloc<Pointer<git_reference>>();
final logMessageC = logMessage?.toNativeUtf8().cast<Int8>() ?? nullptr;
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);
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
@ -366,19 +375,20 @@ Pointer<git_reference> setTargetSymbolic(
String target,
String? logMessage,
) {
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);
calloc.free(targetC);
calloc.free(logMessageC);
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);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
});
}
/// Compare two references.
@ -397,10 +407,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) {
final refname = name.toNativeUtf8().cast<Int8>();
return using((Arena arena) {
final refname = name.toNativeUtf8(allocator: arena).cast<Int8>();
final result = libgit2.git_reference_is_valid_name(refname);
calloc.free(refname);
return result == 1 ? true : false;
});
}
/// Free the given reference.

View file

@ -13,16 +13,17 @@ import '../util.dart';
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_reflog> read(Pointer<git_repository> repo, String name) {
final out = calloc<Pointer<git_reflog>>();
final nameC = name.toNativeUtf8().cast<Int8>();
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);
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,16 +10,17 @@ import '../util.dart';
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> open(String path) {
final out = calloc<Pointer<git_repository>>();
final pathC = path.toNativeUtf8().cast<Int8>();
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);
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].
@ -28,16 +29,17 @@ Pointer<git_repository> open(String path) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> openBare(String barePath) {
final out = calloc<Pointer<git_repository>>();
final barePathC = barePath.toNativeUtf8().cast<Int8>();
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);
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]
@ -48,41 +50,40 @@ Pointer<git_repository> openBare(String barePath) {
///
/// Throws a [LibGit2Error] if error occured.
String discover(String startPath, String ceilingDirs) {
final out = calloc<git_buf>(sizeOf<git_buf>());
final startPathC = startPath.toNativeUtf8().cast<Int8>();
final ceilingDirsC = ceilingDirs.toNativeUtf8().cast<Int8>();
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);
var result = '';
if (error == git_error_code.GIT_ENOTFOUND) {
return result;
return '';
} else if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.ref.ptr.cast<Utf8>().toDartString();
}
result = out.ref.ptr.cast<Utf8>().toDartString();
calloc.free(out);
calloc.free(startPathC);
calloc.free(ceilingDirsC);
return result;
});
}
/// Creates a new Git repository in the given folder.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> init(String path, bool isBare) {
final out = calloc<Pointer<git_repository>>();
final pathC = path.toNativeUtf8().cast<Int8>();
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);
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
@ -124,13 +125,15 @@ String getNamespace(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
void setNamespace(Pointer<git_repository> repo, String? namespace) {
final nmspace = namespace?.toNativeUtf8().cast<Int8>() ?? nullptr;
using((Arena arena) {
final nmspace =
namespace?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final error = libgit2.git_repository_set_namespace(repo, nmspace);
calloc.free(nmspace);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
});
}
/// Check if a repository is bare or not.
@ -162,7 +165,8 @@ bool isEmpty(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_reference> head(Pointer<git_repository> repo) {
final out = calloc<Pointer<git_reference>>();
return using((Arena arena) {
final out = arena<Pointer<git_reference>>();
final error = libgit2.git_repository_head(out, repo);
if (error < 0) {
@ -170,6 +174,7 @@ Pointer<git_reference> head(Pointer<git_repository> repo) {
} else {
return out.value;
}
});
}
/// Check if a repository's HEAD is detached.
@ -208,19 +213,20 @@ 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) {
final nameC = name?.toNativeUtf8().cast<Int8>() ?? nullptr;
final emailC = email?.toNativeUtf8().cast<Int8>() ?? nullptr;
using((Arena arena) {
final nameC = name?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
final emailC =
email?.toNativeUtf8(allocator: arena).cast<Int8>() ?? nullptr;
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) {
final name = calloc<Pointer<Int8>>();
final email = calloc<Pointer<Int8>>();
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>{};
@ -231,10 +237,8 @@ Map<String, String> identity(Pointer<git_repository> repo) {
email.value.cast<Utf8>().toDartString();
}
calloc.free(name);
calloc.free(email);
return identity;
});
}
/// Get the configuration file for this repository.
@ -246,7 +250,8 @@ Map<String, String> identity(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> config(Pointer<git_repository> repo) {
final out = calloc<Pointer<git_config>>();
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_repository_config(out, repo);
if (error < 0) {
@ -254,6 +259,7 @@ Pointer<git_config> config(Pointer<git_repository> repo) {
} else {
return out.value;
}
});
}
/// Get a snapshot of the repository's configuration.
@ -265,7 +271,8 @@ Pointer<git_config> config(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_config> configSnapshot(Pointer<git_repository> repo) {
final out = calloc<Pointer<git_config>>();
return using((Arena arena) {
final out = arena<Pointer<git_config>>();
final error = libgit2.git_repository_config_snapshot(out, repo);
if (error < 0) {
@ -273,6 +280,7 @@ Pointer<git_config> configSnapshot(Pointer<git_repository> repo) {
} else {
return out.value;
}
});
}
/// Get the Index file for this repository.
@ -284,7 +292,8 @@ Pointer<git_config> configSnapshot(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_index> index(Pointer<git_repository> repo) {
final out = calloc<Pointer<git_index>>();
return using((Arena arena) {
final out = arena<Pointer<git_index>>();
final error = libgit2.git_repository_index(out, repo);
if (error < 0) {
@ -292,6 +301,7 @@ Pointer<git_index> index(Pointer<git_repository> repo) {
} else {
return out.value;
}
});
}
/// Determine if the repository was a shallow clone.
@ -318,16 +328,16 @@ bool isWorktree(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
String message(Pointer<git_repository> repo) {
final out = calloc<git_buf>();
return using((Arena arena) {
final out = arena<git_buf>(sizeOf<git_buf>());
final error = libgit2.git_repository_message(out, repo);
final result = out.ref.ptr.cast<Utf8>().toDartString();
calloc.free(out);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return result;
return out.ref.ptr.cast<Utf8>().toDartString();
}
});
}
/// Remove git's prepared message.
@ -344,7 +354,8 @@ void removeMessage(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_odb> odb(Pointer<git_repository> repo) {
final out = calloc<Pointer<git_odb>>();
return using((Arena arena) {
final out = arena<Pointer<git_odb>>();
final error = libgit2.git_repository_odb(out, repo);
if (error < 0) {
@ -352,6 +363,7 @@ Pointer<git_odb> odb(Pointer<git_repository> repo) {
} else {
return out.value;
}
});
}
/// Get the Reference Database Backend for this repository.
@ -364,7 +376,8 @@ Pointer<git_odb> odb(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_refdb> refdb(Pointer<git_repository> repo) {
final out = calloc<Pointer<git_refdb>>();
return using((Arena arena) {
final out = arena<Pointer<git_refdb>>();
final error = libgit2.git_repository_refdb(out, repo);
if (error < 0) {
@ -372,6 +385,7 @@ Pointer<git_refdb> refdb(Pointer<git_repository> repo) {
} else {
return out.value;
}
});
}
/// Make the repository HEAD point to the specified reference.
@ -387,13 +401,14 @@ Pointer<git_refdb> refdb(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
void setHead(Pointer<git_repository> repo, String ref) {
final refname = ref.toNativeUtf8().cast<Int8>();
using((Arena arena) {
final refname = ref.toNativeUtf8(allocator: arena).cast<Int8>();
final error = libgit2.git_repository_set_head(repo, refname);
calloc.free(refname);
if (error < 0 && error != -1) {
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
});
}
/// Make the repository HEAD directly point to the commit.
@ -408,7 +423,7 @@ void setHead(Pointer<git_repository> repo, String ref) {
void setHeadDetached(Pointer<git_repository> repo, Pointer<git_oid> commitish) {
final error = libgit2.git_repository_set_head_detached(repo, commitish);
if (error < 0 && (error != -1 || error != -3)) {
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
@ -427,7 +442,7 @@ void setHeadDetachedFromAnnotated(
final error =
libgit2.git_repository_set_head_detached_from_annotated(repo, commitish);
if (error < 0 && (error != -1 || error != -3)) {
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
@ -447,15 +462,16 @@ void setWorkdir(
String path,
bool updateGitlink,
) {
final workdir = path.toNativeUtf8().cast<Int8>();
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);
calloc.free(workdir);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
});
}
/// Determines the status of a git repository - ie, whether an operation
@ -495,7 +511,8 @@ String workdir(Pointer<git_repository> repo) {
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_repository> wrapODB(Pointer<git_odb> odb) {
final out = calloc<Pointer<git_repository>>();
return using((Arena arena) {
final out = arena<Pointer<git_repository>>();
final error = libgit2.git_repository_wrap_odb(out, odb);
if (error < 0) {
@ -503,6 +520,7 @@ Pointer<git_repository> wrapODB(Pointer<git_odb> odb) {
} else {
return out.value;
}
});
}
/// Find a single object, as specified by a [spec] string.
@ -510,24 +528,25 @@ Pointer<git_repository> wrapODB(Pointer<git_odb> odb) {
/// The returned object should be released when no longer needed.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<Pointer<git_object>> revParseSingle(
Pointer<git_object> revParseSingle(
Pointer<git_repository> repo,
String spec,
) {
final out = calloc<Pointer<git_object>>();
final specC = spec.toNativeUtf8().cast<Int8>();
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,
);
calloc.free(specC);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
return out;
});
}
/// Free a previously allocated repository.

View file

@ -16,36 +16,36 @@ Pointer<git_signature> create(
int time,
int offset,
) {
final out = calloc<Pointer<git_signature>>();
final nameC = name.toNativeUtf8().cast<Int8>();
final emailC = email.toNativeUtf8().cast<Int8>();
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);
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) {
final out = calloc<Pointer<git_signature>>();
final nameC = name.toNativeUtf8().cast<Int8>();
final emailC = email.toNativeUtf8().cast<Int8>();
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);
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,7 +11,8 @@ 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) {
final out = calloc<Pointer<git_tree>>();
return using((Arena arena) {
final out = arena<Pointer<git_tree>>();
final error = libgit2.git_tree_lookup(out, repo, id);
if (error < 0) {
@ -19,6 +20,7 @@ Pointer<git_tree> lookup(Pointer<git_repository> repo, Pointer<git_oid> id) {
} else {
return out.value;
}
});
}
/// Lookup a tree object from the repository, given a prefix of its identifier (short id).
@ -29,7 +31,8 @@ Pointer<git_tree> lookupPrefix(
Pointer<git_oid> id,
int len,
) {
final out = calloc<Pointer<git_tree>>();
return using((Arena arena) {
final out = arena<Pointer<git_tree>>();
final error = libgit2.git_tree_lookup_prefix(out, repo, id, len);
if (error < 0) {
@ -37,6 +40,7 @@ Pointer<git_tree> lookupPrefix(
} else {
return out.value;
}
});
}
/// Close an open tree.

View file

@ -2,7 +2,6 @@ import 'dart:io';
import 'package:test/test.dart';
import 'package:libgit2dart/libgit2dart.dart';
import 'package:libgit2dart/src/commit.dart';
import 'helpers/util.dart';
void main() {

View file

@ -1,8 +1,7 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:libgit2dart/src/config.dart';
import 'package:libgit2dart/src/error.dart';
import 'package:libgit2dart/libgit2dart.dart';
void main() {
final tmpDir = Directory.systemTemp.path;

View file

@ -1,9 +1,7 @@
import 'dart:io';
import 'package:libgit2dart/libgit2dart.dart';
import 'package:test/test.dart';
import 'package:libgit2dart/src/index.dart';
import 'package:libgit2dart/libgit2dart.dart';
import 'helpers/util.dart';
void main() {

View file

@ -1,10 +1,7 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:libgit2dart/src/repository.dart';
import 'package:libgit2dart/src/odb.dart';
import 'package:libgit2dart/src/oid.dart';
import 'package:libgit2dart/libgit2dart.dart';
import 'helpers/util.dart';
void main() {

View file

@ -1,5 +1,5 @@
import 'package:test/test.dart';
import 'package:libgit2dart/src/oid.dart';
import 'package:libgit2dart/libgit2dart.dart';
void main() {
const sha = '9d81c715ff606057fa448e558c7458467a86c8c7';
@ -20,8 +20,7 @@ void main() {
test('returns sha hex string', () {
final oid = Oid.fromSHA(sha);
final hex = oid.sha;
expect(hex, equals(sha));
expect(oid.sha, equals(sha));
});
group('compare', () {

View file

@ -1,9 +1,7 @@
import 'dart:io';
import 'package:test/test.dart';
import 'package:libgit2dart/src/repository.dart';
import 'package:libgit2dart/src/reflog.dart';
import 'package:libgit2dart/libgit2dart.dart';
import 'helpers/util.dart';
void main() {

View file

@ -214,6 +214,11 @@ void main() {
});
group('setHead', () {
late Reference head;
setUp(() => head = repo.head);
tearDown(() => head.free());
test('successfully sets head when target is reference', () {
expect(repo.head.name, 'refs/heads/master');
expect(repo.head.target.sha, lastCommit);