diff --git a/SwiftGit2/OID.swift b/SwiftGit2/OID.swift index 6352ca4..bfa69a5 100644 --- a/SwiftGit2/OID.swift +++ b/SwiftGit2/OID.swift @@ -35,6 +35,11 @@ public struct OID { pointer.dealloc(1) } + /// Create an instance from a libgit2 `git_oid`. + public init(oid: git_oid) { + self.oid = oid + } + // MARK: - Properties public let oid: git_oid @@ -50,3 +55,15 @@ extension OID: Printable { return String(bytesNoCopy: string, length: length, encoding: NSASCIIStringEncoding, freeWhenDone: true)! } } + +extension OID: Hashable { + public var hashValue: Int { + return Int(self.oid.id.0) ^ Int(self.oid.id.1) ^ Int(self.oid.id.2) + } +} + +public func == (lhs: OID, rhs: OID) -> Bool { + var left = lhs.oid + var right = rhs.oid + return git_oid_cmp(&left, &right) == 0 +} diff --git a/SwiftGit2Tests/OIDSpec.swift b/SwiftGit2Tests/OIDSpec.swift index fe8c3ae..058243c 100644 --- a/SwiftGit2Tests/OIDSpec.swift +++ b/SwiftGit2Tests/OIDSpec.swift @@ -38,5 +38,29 @@ class OIDSpec: QuickSpec { expect(oid.description).to(equal(SHA)) } } + + describe("==") { + it("should be equal when identical") { + let SHA = "1234567890123456789012345678901234567890" + let oid1 = OID(string: SHA)! + let oid2 = OID(string: SHA)! + expect(oid1).to(equal(oid2)) + } + + it("should be not equal when different") { + let oid1 = OID(string: "1234567890123456789012345678901234567890")! + let oid2 = OID(string: "0000000000000000000000000000000000000000")! + expect(oid1).notTo(equal(oid2)) + } + } + + describe("hashValue") { + it("should be equal when OIDs are equal") { + let SHA = "1234567890123456789012345678901234567890" + let oid1 = OID(string: SHA)! + let oid2 = OID(string: SHA)! + expect(oid1.hashValue).to(equal(oid2.hashValue)) + } + } } }