Make Branch Hashable

This commit is contained in:
Matt Diephouse 2015-02-13 14:44:02 -05:00
parent d84cf377d0
commit f46d869de7
2 changed files with 36 additions and 0 deletions

View File

@ -91,6 +91,17 @@ public struct Branch: ReferenceType {
}
}
extension Branch: Hashable {
public var hashValue: Int {
return longName.hashValue ^ oid.hashValue
}
}
public func == (lhs: Branch, rhs: Branch) -> Bool {
return lhs.longName == rhs.longName
&& lhs.oid == rhs.oid
}
/// A git tag reference, which can be either a lightweight tag or a Tag object.
public enum TagReference: ReferenceType {
/// A lightweight tag, which is just a name and an OID.

View File

@ -75,5 +75,30 @@ class BranchSpec: QuickSpec {
expect(branch.oid).to(equal(branch.commit.oid))
}
}
describe("==") {
it("should be true with equal branches") {
let repo = Fixtures.simpleRepository
let branch1 = from_git_reference(repo, "refs/heads/master") { Branch($0)! }
let branch2 = from_git_reference(repo, "refs/heads/master") { Branch($0)! }
expect(branch1).to(equal(branch2))
}
it("should be false with unequal branches") {
let repo = Fixtures.simpleRepository
let branch1 = from_git_reference(repo, "refs/heads/master") { Branch($0)! }
let branch2 = from_git_reference(repo, "refs/heads/another-branch") { Branch($0)! }
expect(branch1).notTo(equal(branch2))
}
}
describe("hashValue") {
it("should be equal with equal references") {
let repo = Fixtures.simpleRepository
let branch1 = from_git_reference(repo, "refs/heads/master") { Branch($0)! }
let branch2 = from_git_reference(repo, "refs/heads/master") { Branch($0)! }
expect(branch1.hashValue).to(equal(branch2.hashValue))
}
}
}
}