diff --git a/SwiftGit2.xcodeproj/project.pbxproj b/SwiftGit2.xcodeproj/project.pbxproj index 0660870..5690abf 100644 --- a/SwiftGit2.xcodeproj/project.pbxproj +++ b/SwiftGit2.xcodeproj/project.pbxproj @@ -729,7 +729,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint is not installed\"\nfi"; + shellScript = "if which swiftlint >/dev/null; then\n swiftlint --lenient\nelse\n echo \"warning: SwiftLint is not installed\"\nfi"; }; /* End PBXShellScriptBuildPhase section */ diff --git a/SwiftGit2/References.swift b/SwiftGit2/References.swift index 293d629..67389c4 100644 --- a/SwiftGit2/References.swift +++ b/SwiftGit2/References.swift @@ -64,7 +64,6 @@ extension Reference: Hashable { /// A git branch. public struct Branch: ReferenceType { - /// The full name of the reference (e.g., `refs/heads/master`). public let longName: String @@ -103,9 +102,9 @@ public struct Branch: ReferenceType { return nil } name = String(validatingUTF8: namePointer!)! - + longName = String(validatingUTF8: git_reference_name(pointer))! - + var oid: OID if git_reference_type(pointer).rawValue == GIT_REF_SYMBOLIC.rawValue { var resolved: OpaquePointer? = nil @@ -120,27 +119,6 @@ public struct Branch: ReferenceType { } commit = PointerTo(oid) } - - public func allCommits(in repo: Repository) -> [Commit] { - var commits: [Commit] = [] - var walker: OpaquePointer? = nil - var unsafeCommit: OpaquePointer? = nil - var oid = self.oid.oid - git_revwalk_new(&walker, repo.pointer) - git_revwalk_sorting(walker, GIT_SORT_TOPOLOGICAL.rawValue) - git_revwalk_push(walker, &oid) - while git_revwalk_next(&oid, walker) == GIT_OK.rawValue { - guard - git_commit_lookup(&unsafeCommit, repo.pointer, &oid) == GIT_OK.rawValue, - let commit = unsafeCommit else { - return commits - } - commits += [Commit(commit)] - git_commit_free(unsafeCommit) - } - git_revwalk_free(walker) - return commits - } } extension Branch: Hashable { diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index ea08a08..d8eec7a 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -509,4 +509,35 @@ final public class Repository { progress: CheckoutProgressBlock? = nil) -> Result<(), NSError> { return setHEAD(reference).flatMap { self.checkout(strategy: strategy, progress: progress) } } + + + /// Load all commits in the specified branch in topological & time order + /// + /// :param: branch The branch to get all commits from + /// :returns: Returns a result with array of branches or the error that occurred + public func allCommits(in branch: Branch) -> Result<[Commit], NSError> { + var commits: [Commit] = [] + var walker: OpaquePointer? = nil + var unsafeCommit: OpaquePointer? = nil + var oid = branch.oid.oid + git_revwalk_new(&walker, self.pointer) + defer { + git_revwalk_free(walker) + } + git_revwalk_sorting(walker, GIT_SORT_TOPOLOGICAL.rawValue) + git_revwalk_sorting(walker, GIT_SORT_TIME.rawValue) + git_revwalk_push(walker, &oid) + while git_revwalk_next(&oid, walker) == GIT_OK.rawValue { + let result = git_commit_lookup(&unsafeCommit, self.pointer, &oid) + guard result == GIT_OK.rawValue else { + return Result.failure(NSError(gitError: result, pointOfFailure: "git_commit_lookup")) + } + guard let commit = unsafeCommit else { + continue + } + commits += [Commit(commit)] + git_commit_free(unsafeCommit) + } + return Result.success(commits) + } } diff --git a/SwiftGit2Tests/ReferencesSpec.swift b/SwiftGit2Tests/ReferencesSpec.swift index 32355e6..058340f 100644 --- a/SwiftGit2Tests/ReferencesSpec.swift +++ b/SwiftGit2Tests/ReferencesSpec.swift @@ -116,17 +116,6 @@ class BranchSpec: QuickSpec { expect(branch1.hashValue).to(equal(branch2.hashValue)) } } - - describe("Branch.allCommits(in:)") { - it("should return all (9) commits") { - let repo = Fixtures.simpleRepository - var count = 0 - for branch in repo.localBranches().value! { - count += branch.allCommits(in: repo).count - } - expect(count).to(equal(9)) - } - } } } diff --git a/SwiftGit2Tests/RepositorySpec.swift b/SwiftGit2Tests/RepositorySpec.swift index c7f554c..4992045 100644 --- a/SwiftGit2Tests/RepositorySpec.swift +++ b/SwiftGit2Tests/RepositorySpec.swift @@ -600,6 +600,17 @@ class RepositorySpec: QuickSpec { expect(repo.HEAD().value?.longName).to(equal("HEAD")) } } + + fdescribe("Repository.allCommits(in:)") { + it("should return all (9) commits") { + let repo = Fixtures.simpleRepository + let branches = repo.localBranches().value! + let commits = branches.map { repo.allCommits(in: $0).value!.map { $0 } } + let count = commits.reduce(0) { $0 + $1.count } + let expected = 9 + expect(count).to(equal(expected)) + } + } } func temporaryURL(forPurpose purpose: String) -> URL {