Add an initializer for Branch

This commit is contained in:
Matt Diephouse 2015-02-13 14:39:25 -05:00
parent 28f3706a1d
commit d84cf377d0
2 changed files with 33 additions and 3 deletions

View File

@ -54,9 +54,6 @@ public struct Branch: ReferenceType {
/// The full name of the reference (e.g., `refs/heads/master`).
public let longName: String
/// The name of the remote this branch belongs to, or nil if it's a local branch.
public let remoteName: String?
/// The short human-readable name of the branch (e.g., `master`).
public let name: String
@ -75,6 +72,23 @@ public struct Branch: ReferenceType {
///
/// This is the same as `commit.oid`, but is declared here to adhere to `ReferenceType`.
public var oid: OID { return commit.oid }
/// Create an instance with a libgit2 `git_reference` object.
///
/// Returns `nil` if the pointer isn't a branch.
public init?(_ pointer: COpaquePointer) {
let namePointer = UnsafeMutablePointer<UnsafePointer<Int8>>.alloc(1)
let success = git_branch_name(namePointer, pointer)
if success != GIT_OK.value {
namePointer.dealloc(1)
return nil
}
name = String.fromCString(namePointer.memory)!
namePointer.dealloc(1)
longName = String.fromCString(git_reference_name(pointer))!
commit = PointerTo<Commit>(OID(git_reference_target(pointer).memory))
}
}
/// A git tag reference, which can be either a lightweight tag or a Tag object.

View File

@ -61,3 +61,19 @@ class ReferenceSpec: QuickSpec {
}
}
}
class BranchSpec: QuickSpec {
override func spec() {
describe("init()") {
it("should initialize its properties") {
let repo = Fixtures.mantleRepository
let branch = from_git_reference(repo, "refs/heads/master") { Branch($0)! }
expect(branch.longName).to(equal("refs/heads/master"))
expect(branch.name).to(equal("master"))
expect(branch.shortName).to(equal(branch.name))
expect(branch.commit.oid).to(equal(OID(string: "f797bd4837b61d37847a4833024aab268599a681")!))
expect(branch.oid).to(equal(branch.commit.oid))
}
}
}
}