mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
refactor: revert 'use ffi Arena for resource management'
This commit is contained in:
parent
6a097c1841
commit
a78c38d8e3
9 changed files with 588 additions and 620 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue