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

26
lib/src/bindings/odb.dart Normal file
View file

@ -0,0 +1,26 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import '../error.dart';
import 'libgit2_bindings.dart';
import '../util.dart';
/// Determine if an object can be found in the object database by an abbreviated object ID.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_oid> existsPrefix(
Pointer<git_odb> db,
Pointer<git_oid> shortOid,
int len,
) {
final out = calloc<git_oid>();
final error = libgit2.git_odb_exists_prefix(out, db, shortOid, len);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out;
}
}
/// Close an open object database.
void free(Pointer<git_odb> db) => libgit2.git_odb_free(db);

View file

@ -4,6 +4,24 @@ import '../error.dart';
import 'libgit2_bindings.dart';
import '../util.dart';
/// Parse N characters of a hex formatted object id into a git_oid.
///
/// If N is odd, the last byte's high nibble will be read in and the low nibble set to zero.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_oid> fromStrN(String hex) {
final out = calloc<git_oid>();
final str = hex.toNativeUtf8().cast<Int8>();
final error = libgit2.git_oid_fromstrn(out, str, hex.length);
calloc.free(str);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out;
}
}
/// Parse a hex formatted object id into a git_oid.
///
/// Throws a [LibGit2Error] if error occured.

View file

@ -11,8 +11,17 @@ int referenceType(Pointer<git_reference> ref) =>
/// Get the OID pointed to by a direct reference.
///
/// Only available if the reference is direct (i.e. an object id reference, not a symbolic one).
Pointer<git_oid>? target(Pointer<git_reference> ref) =>
libgit2.git_reference_target(ref);
///
/// Throws an exception if error occured.
Pointer<git_oid> target(Pointer<git_reference> ref) {
final result = libgit2.git_reference_target(ref);
if (result == nullptr) {
throw Exception('OID for reference isn\'t available');
} else {
return result;
}
}
/// Resolve a symbolic reference to a direct reference.
///
@ -127,6 +136,70 @@ bool isTag(Pointer<git_reference> ref) {
return result == 1 ? true : false;
}
/// Create a new direct reference.
///
/// A direct reference (also called an object id reference) refers directly to a
/// specific object id (a.k.a. OID or SHA) in the repository. The id permanently refers to
/// the object (although the reference itself can be moved). For example, in libgit2
/// the direct ref "refs/tags/v0.17.0" refers to OID 5b9fac39d8a76b9139667c26a63e6b3f204b3977.
///
/// 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.
/// This function will throw 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.
Pointer<git_reference> createDirect(
Pointer<git_repository> repo,
String name,
Pointer<git_oid> oid,
bool force,
String logMessage,
) {
final out = calloc<Pointer<git_reference>>();
final nameC = name.toNativeUtf8().cast<Int8>();
final forceC = force == true ? 1 : 0;
final logMessageC = logMessage.toNativeUtf8().cast<Int8>();
final error =
libgit2.git_reference_create(out, repo, nameC, oid, forceC, logMessageC);
calloc.free(nameC);
calloc.free(logMessageC);
if (error < 0) {
throw (LibGit2Error(libgit2.git_error_last()));
} else {
return out.value;
}
}
/// 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(Pointer<git_reference> ref) {
final error = libgit2.git_reference_delete(ref);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
}
/// Get the repository where a reference resides.
Pointer<git_repository> owner(Pointer<git_reference> ref) {
return libgit2.git_reference_owner(ref);
}
/// Ensure the reference name is well-formed.
///
/// Valid reference names must follow one of two patterns:

View file

@ -0,0 +1,56 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import '../error.dart';
import 'libgit2_bindings.dart';
import '../util.dart';
/// Read the reflog for the given reference.
///
/// If there is no reflog file for the given reference yet, an empty reflog
/// object will be returned.
///
/// The reflog must be freed manually.
///
/// 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>();
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.
int entryCount(Pointer<git_reflog> reflog) =>
libgit2.git_reflog_entrycount(reflog);
/// Lookup an entry by its index.
///
/// Requesting the reflog entry with an index of 0 (zero) will return
/// the most recently created entry.
Pointer<git_reflog_entry> entryByIndex(Pointer<git_reflog> reflog, int idx) {
return libgit2.git_reflog_entry_byindex(reflog, idx);
}
/// Get the log message.
String entryMessage(Pointer<git_reflog_entry> entry) {
final result = libgit2.git_reflog_entry_message(entry);
return result.cast<Utf8>().toDartString();
}
/// Get the committer of this entry.
Map<String, String> entryCommiter(Pointer<git_reflog_entry> entry) {
final result = libgit2.git_reflog_entry_committer(entry);
var committer = <String, String>{};
committer['name'] = result.ref.name.cast<Utf8>().toDartString();
committer['email'] = result.ref.email.cast<Utf8>().toDartString();
return committer;
}
/// Free the reflog.
void free(Pointer<git_reflog> reflog) => libgit2.git_reflog_free(reflog);