feat(reference): add ability to peel reference until object of specified type is found

This commit is contained in:
Aleksey Kulikov 2021-09-04 14:50:34 +03:00
parent 56713da648
commit f63808b4f8
4 changed files with 91 additions and 0 deletions

View file

@ -395,6 +395,25 @@ bool compare(Pointer<git_reference> ref1, Pointer<git_reference> ref2) {
return result == 0 ? true : false;
}
/// Recursively peel reference until object of the specified type is found.
///
/// The retrieved peeled object is owned by the repository and should be closed to release memory.
///
/// If you pass GIT_OBJECT_ANY as the target type, then the object will be peeled until a
/// non-tag object is met.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_object> peel(Pointer<git_reference> ref, int type) {
final out = calloc<Pointer<git_object>>();
final error = libgit2.git_reference_peel(out, ref, type);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out.value;
}
}
/// Ensure the reference name is well-formed.
///
/// Valid reference names must follow one of two patterns:

View file

@ -1,10 +1,17 @@
import 'dart:ffi';
import 'package:libgit2dart/libgit2dart.dart';
import 'bindings/libgit2_bindings.dart';
import 'bindings/reference.dart' as bindings;
import 'bindings/object.dart' as object_bindings;
import 'blob.dart';
import 'commit.dart';
import 'oid.dart';
import 'reflog.dart';
import 'git_types.dart';
import 'repository.dart';
import 'tag.dart';
import 'tree.dart';
import 'util.dart';
class References {
@ -169,6 +176,35 @@ class Reference {
}
}
/// Recursively peel reference until object of the specified [type] is found.
///
/// The retrieved peeled object is owned by the repository and should be closed to release memory.
///
/// If no [type] is provided, then the object will be peeled until a non-tag object is met.
///
/// Returned object should be explicitly downcasted to one of four of git object types.
///
/// ```dart
/// final commit = ref.peel(GitObject.commit) as Commit;
/// final tree = ref.peel(GitObject.tree) as Tree;
/// ```
///
/// Throws a [LibGit2Error] if error occured.
Object peel([GitObject type = GitObject.any]) {
final object = bindings.peel(_refPointer, type.value);
final objectType = object_bindings.type(object);
if (objectType == GitObject.commit.value) {
return Commit(object.cast());
} else if (objectType == GitObject.tree.value) {
return Tree(object.cast());
} else if (objectType == GitObject.blob.value) {
return Blob(object.cast());
} else {
return Tag(object.cast());
}
}
/// Returns the full name of a reference.
String get name => bindings.name(_refPointer);

View file

@ -81,6 +81,13 @@ class Tag {
///
/// Returned object should be explicitly downcasted to one of four of git object types.
///
/// ```dart
/// final commit = tag.target as Commit;
/// final tree = tag.target as Tree;
/// final blob = tag.target as Blob;
/// final tag = tag.target as Tag;
/// ```
///
/// Throws a [LibGit2Error] if error occured.
Object get target {
final type = bindings.targetType(_tagPointer);