feat(reference): add ability to create direct reference

This commit is contained in:
Aleksey Kulikov 2021-08-05 19:48:58 +03:00
parent 6643527f2d
commit 9190ed2e0f
16 changed files with 669 additions and 60 deletions

View file

@ -2,7 +2,6 @@ import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'bindings/libgit2_bindings.dart';
import 'bindings/reference.dart' as bindings;
import 'repository.dart';
import 'oid.dart';
import 'util.dart';
@ -15,22 +14,45 @@ class Reference {
libgit2.git_libgit2_init();
}
/// Initializes a new instance of the [Reference] class by creating a new direct reference.
///
/// The direct reference will be created in the repository and written to the disk.
/// The generated [Reference] object must be freed by the user.
///
/// Valid reference names must follow one of two patterns:
///
/// Top-level names must contain only capital letters and underscores, and must begin and end
/// with a letter. (e.g. "HEAD", "ORIG_HEAD").
/// Names prefixed with "refs/" can be almost anything. You must avoid the characters
/// '~', '^', ':', '\', '?', '[', and '*', and the sequences ".." and "@{" which have
/// special meaning to revparse.
///
/// Throws a [LibGit2Error] if a reference already exists with the given name
/// unless force is true, in which case it will be overwritten.
///
/// The message for the reflog will be ignored if the reference does not belong in the
/// standard set (HEAD, branches and remote-tracking branches) and it does not have a reflog.
Reference.createDirect({
required Pointer<git_repository> repo,
required String name,
required Pointer<git_oid> oid,
required bool force,
required String logMessage,
}) {
_refPointer = bindings.createDirect(repo, name, oid, force, logMessage);
}
/// Initializes a new instance of the [Reference] class by
/// lookingup a reference by [name] in a [repository].
/// lookingup a reference by [name] in a repository.
///
/// Should be freed with `free()` to release allocated memory.
///
/// The name will be checked for validity.
///
/// Throws a [LibGit2Error] if error occured.
Reference.lookup(Repository repository, String name) {
Reference.lookup(Pointer<git_repository> repo, String name) {
libgit2.git_libgit2_init();
try {
_refPointer = bindings.lookup(repository.pointer, name);
} catch (e) {
rethrow;
}
_refPointer = bindings.lookup(repo, name);
}
/// Pointer to memory address for allocated reference object.
@ -60,18 +82,18 @@ class Reference {
: ReferenceType.symbolic;
}
/// Returns the SHA hex of the OID pointed to by a reference.
String get target {
late final Pointer<git_oid>? oidPointer;
final sha = '';
/// Returns the OID pointed to by a reference.
///
/// Throws an exception if error occured.
Oid get target {
late final Pointer<git_oid> oidPointer;
if (type == ReferenceType.direct) {
oidPointer = bindings.target(_refPointer);
} else {
oidPointer = bindings.target(bindings.resolve(_refPointer));
}
return oidPointer == nullptr ? sha : Oid(oidPointer!).sha;
return Oid(oidPointer);
}
/// Returns the full name of a reference.
@ -81,22 +103,14 @@ class Reference {
///
/// Throws a [LibGit2Error] if error occured.
static List<String> list(Pointer<git_repository> repo) {
try {
return bindings.list(repo);
} catch (e) {
rethrow;
}
return bindings.list(repo);
}
/// Checks if a reflog exists for the specified reference [name].
///
/// Throws a [LibGit2Error] if error occured.
static bool hasLog(Repository repo, String name) {
try {
return bindings.hasLog(repo.pointer, name);
} catch (e) {
rethrow;
}
static bool hasLog(Pointer<git_repository> repo, String name) {
return bindings.hasLog(repo, name);
}
/// Checks if a reference is a local branch.
@ -111,6 +125,17 @@ class Reference {
/// Check if a reference is a tag.
bool get isTag => bindings.isTag(_refPointer);
/// Returns the repository where a reference resides.
Pointer<git_repository> get owner => bindings.owner(_refPointer);
/// Delete an existing reference.
///
/// This method works for both direct and symbolic references.
/// The reference will be immediately removed on disk but the memory will not be freed.
///
/// Throws a [LibGit2Error] if the reference has changed from the time it was looked up.
void delete() => bindings.delete(_refPointer);
/// Releases memory allocated for reference object.
void free() {
calloc.free(_refPointer);