mirror of
https://github.com/gosticks/SwiftGit2.git
synced 2025-10-16 11:55:34 +00:00
CommitIterator now not hold branch as reference.
Make private var instead to prevent unexpected state change from external influence. Uses enum instead of tuple of stop and error to indicate state of Git command result.
This commit is contained in:
parent
915b07f4a9
commit
4dd24cad97
@ -8,15 +8,29 @@ import libgit2
|
|||||||
|
|
||||||
public class CommitIterator: IteratorProtocol {
|
public class CommitIterator: IteratorProtocol {
|
||||||
public typealias Element = Result<Commit, NSError>
|
public typealias Element = Result<Commit, NSError>
|
||||||
var repo: Repository
|
let repo: Repository
|
||||||
var branch: Branch
|
private var oid: git_oid
|
||||||
var revisionWalker: OpaquePointer? = nil
|
private var revisionWalker: OpaquePointer? = nil
|
||||||
var oid: git_oid
|
|
||||||
private var unsafeCommit: OpaquePointer? = nil
|
|
||||||
|
|
||||||
public init(repo: Repository, branch: Branch) {
|
private enum Next {
|
||||||
|
case over
|
||||||
|
case ok
|
||||||
|
case error(NSError)
|
||||||
|
|
||||||
|
init(_ result: Int32, name: String) {
|
||||||
|
switch result {
|
||||||
|
case GIT_ITEROVER.rawValue:
|
||||||
|
self = .over
|
||||||
|
case GIT_OK.rawValue:
|
||||||
|
self = .ok
|
||||||
|
default:
|
||||||
|
self = .error(NSError(gitError: result, pointOfFailure: name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
init(repo: Repository, branch: Branch) {
|
||||||
self.repo = repo
|
self.repo = repo
|
||||||
self.branch = branch
|
|
||||||
self.oid = branch.oid.oid
|
self.oid = branch.oid.oid
|
||||||
setupRevisionWalker()
|
setupRevisionWalker()
|
||||||
}
|
}
|
||||||
@ -32,36 +46,29 @@ public class CommitIterator: IteratorProtocol {
|
|||||||
git_revwalk_push(revisionWalker, &oid)
|
git_revwalk_push(revisionWalker, &oid)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func result(withName name: String, from result: Int32) -> (stop: Bool, error: NSError?) {
|
private func next(withName name: String, from result: Int32) -> Next {
|
||||||
guard result == GIT_OK.rawValue else {
|
if result == GIT_OK.rawValue || result == GIT_ITEROVER.rawValue {
|
||||||
if result == GIT_ITEROVER.rawValue {
|
return Next(result, name: name)
|
||||||
return (stop: true, error: nil)
|
|
||||||
} else {
|
} else {
|
||||||
return (stop: false, error: NSError(gitError: result, pointOfFailure: name))
|
return Next.error(NSError(gitError: result, pointOfFailure: name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (stop: false, error: nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
public func next() -> Element? {
|
public func next() -> Element? {
|
||||||
let revwalkGitResult = git_revwalk_next(&self.oid, self.revisionWalker)
|
var unsafeCommit: OpaquePointer? = nil
|
||||||
let revwalkResult = result(withName: "git_revwalk_next", from: revwalkGitResult)
|
let revwalkGitResult = git_revwalk_next(&oid, revisionWalker)
|
||||||
if revwalkResult.stop {
|
let nextResult = next(withName: "git_revwalk_next", from: revwalkGitResult)
|
||||||
return nil
|
if case let .error(error) = nextResult {
|
||||||
} else if let error = revwalkResult.error {
|
|
||||||
return Result.failure(error)
|
return Result.failure(error)
|
||||||
}
|
} else if case .over = nextResult {
|
||||||
let lookupGitResult = git_commit_lookup(&self.unsafeCommit, self.repo.pointer, &self.oid)
|
|
||||||
let lookupResult = result(withName: "git_commit_lookup", from: lookupGitResult)
|
|
||||||
if lookupResult.stop {
|
|
||||||
return nil
|
|
||||||
} else if let error = lookupResult.error {
|
|
||||||
return Result.failure(error)
|
|
||||||
}
|
|
||||||
guard let commit = unsafeCommit else {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
guard git_commit_lookup(&unsafeCommit, repo.pointer, &oid) == GIT_OK.rawValue,
|
||||||
|
let unwrapCommit = unsafeCommit else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
let result: Element = Result.success(Commit(unwrapCommit))
|
||||||
git_commit_free(unsafeCommit)
|
git_commit_free(unsafeCommit)
|
||||||
return Result.success(Commit(commit))
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -601,18 +601,30 @@ class RepositorySpec: QuickSpec {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fdescribe("Repository.allCommits(in:)") {
|
describe("Repository.allCommits(in:)") {
|
||||||
it("should return all (9) commits") {
|
it("should return all (9) commits") {
|
||||||
let repo = Fixtures.simpleRepository
|
let repo = Fixtures.simpleRepository
|
||||||
let branches = repo.localBranches().value!
|
let branches = repo.localBranches().value!
|
||||||
var count = 0
|
let expectedCount = 9
|
||||||
let expected = 9
|
let expectedMessages: [String] = [
|
||||||
|
"List branches in README\n",
|
||||||
|
"Create a README\n",
|
||||||
|
"Merge branch 'alphabetize'\n",
|
||||||
|
"Alphabetize branches\n",
|
||||||
|
"List new branches\n",
|
||||||
|
"List branches in README\n",
|
||||||
|
"Create a README\n",
|
||||||
|
"List branches in README\n",
|
||||||
|
"Create a README\n"
|
||||||
|
]
|
||||||
|
var commitMessages: [String] = []
|
||||||
for branch in branches {
|
for branch in branches {
|
||||||
while repo.commits(in: branch).next() != nil {
|
while let commit = repo.commits(in: branch).next() {
|
||||||
count += 1
|
commitMessages.append(commit.value!.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect(count).to(equal(expected))
|
expect(commitMessages.count).to(equal(expectedCount))
|
||||||
|
expect(commitMessages).to(equal(expectedMessages))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user