mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
test(reference): add tests for symbolic refs
This commit is contained in:
parent
2f896e6180
commit
6643527f2d
3 changed files with 40 additions and 8 deletions
|
@ -14,6 +14,28 @@ int referenceType(Pointer<git_reference> ref) =>
|
||||||
Pointer<git_oid>? target(Pointer<git_reference> ref) =>
|
Pointer<git_oid>? target(Pointer<git_reference> ref) =>
|
||||||
libgit2.git_reference_target(ref);
|
libgit2.git_reference_target(ref);
|
||||||
|
|
||||||
|
/// Resolve a symbolic reference to a direct reference.
|
||||||
|
///
|
||||||
|
/// This method iteratively peels a symbolic reference until it resolves
|
||||||
|
/// to a direct reference to an OID.
|
||||||
|
///
|
||||||
|
/// The peeled reference must be freed manually once it's no longer needed.
|
||||||
|
///
|
||||||
|
/// If a direct reference is passed as an argument, a copy of that reference is returned.
|
||||||
|
/// This copy must be manually freed too.
|
||||||
|
///
|
||||||
|
/// Throws a [LibGit2Error] if error occured.
|
||||||
|
Pointer<git_reference> resolve(Pointer<git_reference> ref) {
|
||||||
|
final out = calloc<Pointer<git_reference>>();
|
||||||
|
final error = libgit2.git_reference_resolve(out, ref);
|
||||||
|
|
||||||
|
if (error < 0) {
|
||||||
|
throw LibGit2Error(libgit2.git_error_last());
|
||||||
|
} else {
|
||||||
|
return out.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Lookup a reference by name in a repository.
|
/// Lookup a reference by name in a repository.
|
||||||
///
|
///
|
||||||
/// The returned reference must be freed by the user.
|
/// The returned reference must be freed by the user.
|
||||||
|
|
|
@ -60,18 +60,18 @@ class Reference {
|
||||||
: ReferenceType.symbolic;
|
: ReferenceType.symbolic;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the SHA hex of the OID pointed to by a direct reference.
|
/// Returns the SHA hex of the OID pointed to by a reference.
|
||||||
///
|
|
||||||
/// Only available if the reference is direct (i.e. an object id reference, not a symbolic one).
|
|
||||||
String get target {
|
String get target {
|
||||||
final oidPointer = bindings.target(_refPointer);
|
late final Pointer<git_oid>? oidPointer;
|
||||||
final sha = '';
|
final sha = '';
|
||||||
if (oidPointer == nullptr) {
|
|
||||||
return sha;
|
if (type == ReferenceType.direct) {
|
||||||
|
oidPointer = bindings.target(_refPointer);
|
||||||
} else {
|
} else {
|
||||||
final oid = Oid(oidPointer!);
|
oidPointer = bindings.target(bindings.resolve(_refPointer));
|
||||||
return oid.sha;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return oidPointer == nullptr ? sha : Oid(oidPointer!).sha;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the full name of a reference.
|
/// Returns the full name of a reference.
|
||||||
|
|
|
@ -33,6 +33,10 @@ void main() {
|
||||||
test('returns correct type of reference', () {
|
test('returns correct type of reference', () {
|
||||||
expect(repo.head.type, ReferenceType.direct);
|
expect(repo.head.type, ReferenceType.direct);
|
||||||
repo.head.free();
|
repo.head.free();
|
||||||
|
|
||||||
|
final ref = Reference.lookup(repo, 'HEAD');
|
||||||
|
expect(ref.type, ReferenceType.symbolic);
|
||||||
|
ref.free();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('returns SHA hex of direct reference', () {
|
test('returns SHA hex of direct reference', () {
|
||||||
|
@ -40,6 +44,12 @@ void main() {
|
||||||
repo.head.free();
|
repo.head.free();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('returns SHA hex of symbolic reference', () {
|
||||||
|
final ref = Reference.lookup(repo, 'HEAD');
|
||||||
|
expect(ref.target, lastCommit);
|
||||||
|
ref.free();
|
||||||
|
});
|
||||||
|
|
||||||
test('returns the full name of a reference', () {
|
test('returns the full name of a reference', () {
|
||||||
expect(repo.head.name, 'refs/heads/master');
|
expect(repo.head.name, 'refs/heads/master');
|
||||||
repo.head.free();
|
repo.head.free();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue