Implement localBranches()

This commit is contained in:
Matt Diephouse 2015-02-14 12:04:08 -05:00
parent d38b1a9b7a
commit 4b0823918d
2 changed files with 42 additions and 1 deletions

View File

@ -10,6 +10,10 @@ import Foundation
import LlamaKit
extension git_strarray {
func filter(f: (String) -> Bool) -> [String] {
return map { $0 }.filter(f)
}
func map<T>(f: (String) -> T) -> [T] {
return Swift.map(0..<self.count) {
let string = String.fromCString(self.strings[Int($0)])!
@ -268,8 +272,33 @@ final public class Repository {
return failure()
}
/// Load and return a list of all local branches.
public func localBranches() -> Result<[Branch]> {
return failure()
let pointer = UnsafeMutablePointer<git_strarray>.alloc(1)
let repository = self.pointer
let result = git_reference_list(pointer, repository)
if result != GIT_OK.value {
pointer.dealloc(1)
return failure()
}
let strarray = pointer.memory
let branches = strarray
.filter {
$0.hasPrefix("refs/heads/")
}
.map {
self.referenceWithName($0).map { $0 as Branch }
}
git_strarray_free(pointer)
pointer.dealloc(1)
let error = branches.reduce(nil) { $0 == nil ? $0 : $1.error() }
if let error = error {
return failure(error)
}
return success(branches.map { $0.value()! })
}
public func remoteBranches() -> Result<[Branch]> {

View File

@ -324,6 +324,18 @@ class RepositorySpec: QuickSpec {
}
}
describe("-localBranches()") {
it("should return all the local branches") {
let repo = Fixtures.simpleRepository
let expected = [
repo.localBranchWithName("another-branch").value()!,
repo.localBranchWithName("master").value()!,
repo.localBranchWithName("yet-another-branch").value()!,
]
expect(repo.localBranches().value()).to(equal(expected))
}
}
describe("-localBranchWithName()") {
it("should return the branch if it exists") {
let result = Fixtures.simpleRepository.localBranchWithName("master")