feat(reference): add ability to compare references

This commit is contained in:
Aleksey Kulikov 2021-08-09 12:08:58 +03:00
parent 1f0201d259
commit 7b6e0c36f6
3 changed files with 30 additions and 0 deletions

View file

@ -381,6 +381,12 @@ Pointer<git_reference> setTargetSymbolic(
} }
} }
/// Compare two references.
bool compare(Pointer<git_reference> ref1, Pointer<git_reference> ref2) {
final result = libgit2.git_reference_cmp(ref1, ref2);
return result == 0 ? true : false;
}
/// Ensure the reference name is well-formed. /// Ensure the reference name is well-formed.
/// ///
/// Valid reference names must follow one of two patterns: /// Valid reference names must follow one of two patterns:

View file

@ -253,6 +253,15 @@ class Reference {
/// Throws a [LibGit2Error] if the reference has changed from the time it was looked up. /// Throws a [LibGit2Error] if the reference has changed from the time it was looked up.
void delete() => bindings.delete(_refPointer); void delete() => bindings.delete(_refPointer);
@override
bool operator ==(other) {
return (other is Reference) &&
bindings.compare(_refPointer, other._refPointer);
}
@override
int get hashCode => _refPointer.address.hashCode;
/// Releases memory allocated for reference object. /// Releases memory allocated for reference object.
void free() { void free() {
bindings.free(_refPointer); bindings.free(_refPointer);

View file

@ -523,6 +523,21 @@ void main() {
}); });
}); });
test('checks equality', () {
final ref1 = Reference.get(repo, 'refs/heads/master');
final ref2 = Reference.get(repo, 'refs/heads/master');
final ref3 = Reference.get(repo, 'refs/heads/feature');
expect(ref1 == ref2, true);
expect(ref1 != ref2, false);
expect(ref1 == ref3, false);
expect(ref1 != ref3, true);
ref1.free();
ref2.free();
ref3.free();
});
group('isValidName()', () { group('isValidName()', () {
test('returns true for valid names', () { test('returns true for valid names', () {
expect(Reference.isValidName('HEAD'), true); expect(Reference.isValidName('HEAD'), true);