diff --git a/lib/src/bindings/reference.dart b/lib/src/bindings/reference.dart index 42434e1..41e81fa 100644 --- a/lib/src/bindings/reference.dart +++ b/lib/src/bindings/reference.dart @@ -381,6 +381,12 @@ Pointer setTargetSymbolic( } } +/// Compare two references. +bool compare(Pointer ref1, Pointer ref2) { + final result = libgit2.git_reference_cmp(ref1, ref2); + return result == 0 ? true : false; +} + /// Ensure the reference name is well-formed. /// /// Valid reference names must follow one of two patterns: diff --git a/lib/src/reference.dart b/lib/src/reference.dart index 55124b1..2cbeffb 100644 --- a/lib/src/reference.dart +++ b/lib/src/reference.dart @@ -253,6 +253,15 @@ class Reference { /// Throws a [LibGit2Error] if the reference has changed from the time it was looked up. 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. void free() { bindings.free(_refPointer); diff --git a/test/reference_test.dart b/test/reference_test.dart index ff01705..76ed204 100644 --- a/test/reference_test.dart +++ b/test/reference_test.dart @@ -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()', () { test('returns true for valid names', () { expect(Reference.isValidName('HEAD'), true);