Merge pull request #72 from mattrubin/swift-api-guidelines

Swift API design guidelines
This commit is contained in:
Matt Diephouse 2016-12-23 17:38:18 -05:00 committed by GitHub
commit 2cc5737d77
10 changed files with 271 additions and 264 deletions

View File

@ -5,7 +5,6 @@ disabled_rules:
- function_body_length
- line_length
- type_body_length
- variable_name
opt_in_rules:
- closure_spacing

View File

@ -38,7 +38,7 @@ public struct CheckoutStrategy: OptionSet {
return value
}
public var git_checkout_strategy: git_checkout_strategy_t {
public var gitCheckoutStrategy: git_checkout_strategy_t {
return git_checkout_strategy_t(UInt32(self.value))
}

View File

@ -3,27 +3,29 @@ import libgit2
public let libGit2ErrorDomain = "org.libgit2.libgit2"
/// Returns an NSError with an error domain and message for libgit2 errors.
///
/// :param: errorCode An error code returned by a libgit2 function.
/// :param: libGit2PointOfFailure The name of the libgit2 function that produced the
/// error code.
/// :returns: An NSError with a libgit2 error domain, code, and message.
internal func libGit2Error(_ errorCode: Int32, libGit2PointOfFailure: String? = nil) -> NSError {
let code = Int(errorCode)
var userInfo: [String: String] = [:]
internal extension NSError {
/// Returns an NSError with an error domain and message for libgit2 errors.
///
/// :param: errorCode An error code returned by a libgit2 function.
/// :param: libGit2PointOfFailure The name of the libgit2 function that produced the
/// error code.
/// :returns: An NSError with a libgit2 error domain, code, and message.
internal convenience init(gitError errorCode: Int32, pointOfFailure: String? = nil) {
let code = Int(errorCode)
var userInfo: [String: String] = [:]
if let message = errorMessage(errorCode) {
userInfo[NSLocalizedDescriptionKey] = message
} else {
userInfo[NSLocalizedDescriptionKey] = "Unknown libgit2 error."
if let message = errorMessage(errorCode) {
userInfo[NSLocalizedDescriptionKey] = message
} else {
userInfo[NSLocalizedDescriptionKey] = "Unknown libgit2 error."
}
if let pointOfFailure = pointOfFailure {
userInfo[NSLocalizedFailureReasonErrorKey] = "\(pointOfFailure) failed."
}
self.init(domain: libGit2ErrorDomain, code: code, userInfo: userInfo)
}
if let pointOfFailure = libGit2PointOfFailure {
userInfo[NSLocalizedFailureReasonErrorKey] = "\(pointOfFailure) failed."
}
return NSError(domain: libGit2ErrorDomain, code: code, userInfo: userInfo)
}
/// Returns the libgit2 error message for the given error code.

View File

@ -13,14 +13,14 @@ func == (lhs: git_otype, rhs: git_otype) -> Bool {
}
extension git_strarray {
func filter(f: (String) -> Bool) -> [String] {
return map { $0 }.filter(f)
func filter(_ isIncluded: (String) -> Bool) -> [String] {
return map { $0 }.filter(isIncluded)
}
func map<T>(f: (String) -> T) -> [T] {
func map<T>(_ transform: (String) -> T) -> [T] {
return (0..<self.count).map {
let string = String(validatingUTF8: self.strings[$0]!)!
return f(string)
return transform(string)
}
}
}

View File

@ -14,17 +14,17 @@ public typealias CheckoutProgressBlock = (String?, Int, Int) -> Void
/// Helper function used as the libgit2 progress callback in git_checkout_options.
/// This is a function with a type signature of git_checkout_progress_cb.
private func checkoutProgressCallback(path: UnsafePointer<Int8>?, completed_steps: Int, total_steps: Int, payload: UnsafeMutableRawPointer?) -> Void {
private func checkoutProgressCallback(path: UnsafePointer<Int8>?, completedSteps: Int, totalSteps: Int, payload: UnsafeMutableRawPointer?) -> Void {
if let payload = payload {
let buffer = payload.assumingMemoryBound(to: CheckoutProgressBlock.self)
let block: CheckoutProgressBlock
if completed_steps < total_steps {
if completedSteps < totalSteps {
block = buffer.pointee
} else {
block = buffer.move()
buffer.deallocate(capacity: 1)
}
block(path.flatMap(String.init(validatingUTF8:)), completed_steps, total_steps)
block(path.flatMap(String.init(validatingUTF8:)), completedSteps, totalSteps)
}
}
@ -40,7 +40,7 @@ private func checkoutOptions(strategy: CheckoutStrategy, progress: CheckoutProgr
var options = pointer.move()
pointer.deallocate(capacity: 1)
options.checkout_strategy = strategy.git_checkout_strategy.rawValue
options.checkout_strategy = strategy.gitCheckoutStrategy.rawValue
if progress != nil {
options.progress_cb = checkoutProgressCallback
@ -103,14 +103,14 @@ final public class Repository {
/// URL - The URL of the repository.
///
/// Returns a `Result` with a `Repository` or an error.
class public func atURL(_ url: URL) -> Result<Repository, NSError> {
class public func at(_ url: URL) -> Result<Repository, NSError> {
var pointer: OpaquePointer? = nil
let result = url.withUnsafeFileSystemRepresentation {
git_repository_open(&pointer, $0)
}
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_repository_open"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_repository_open"))
}
let repository = Repository(pointer!)
@ -128,7 +128,7 @@ final public class Repository {
/// checkoutProgress - A block that's called with the progress of the checkout.
///
/// Returns a `Result` with a `Repository` or an error.
class public func cloneFromURL(_ remoteURL: URL, toURL: URL, localClone: Bool = false, bare: Bool = false,
class public func clone(from remoteURL: URL, to localURL: URL, localClone: Bool = false, bare: Bool = false,
credentials: Credentials = .Default(), checkoutStrategy: CheckoutStrategy = .Safe, checkoutProgress: CheckoutProgressBlock? = nil) -> Result<Repository, NSError> {
var options = cloneOptions(
bare: bare, localClone: localClone,
@ -137,12 +137,12 @@ final public class Repository {
var pointer: OpaquePointer? = nil
let remoteURLString = (remoteURL as NSURL).isFileReferenceURL() ? remoteURL.path : remoteURL.absoluteString
let result = toURL.withUnsafeFileSystemRepresentation {
git_clone(&pointer, remoteURLString, $0, &options)
let result = localURL.withUnsafeFileSystemRepresentation { localPath in
git_clone(&pointer, remoteURLString, localPath, &options)
}
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_clone"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_clone"))
}
let repository = Repository(pointer!)
@ -185,13 +185,13 @@ final public class Repository {
///
/// Returns the result of calling `transform` or an error if the object
/// cannot be loaded.
func withLibgit2Object<T>(_ oid: OID, type: git_otype, transform: (OpaquePointer) -> Result<T, NSError>) -> Result<T, NSError> {
private func withGitObject<T>(_ oid: OID, type: git_otype, transform: (OpaquePointer) -> Result<T, NSError>) -> Result<T, NSError> {
var pointer: OpaquePointer? = nil
var oid = oid.oid
let result = git_object_lookup(&pointer, self.pointer, &oid, type)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_object_lookup"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_object_lookup"))
}
let value = transform(pointer!)
@ -199,8 +199,8 @@ final public class Repository {
return value
}
func withLibgit2Object<T>(_ oid: OID, type: git_otype, transform: (OpaquePointer) -> T) -> Result<T, NSError> {
return withLibgit2Object(oid, type: type) { Result.success(transform($0)) }
private func withGitObject<T>(_ oid: OID, type: git_otype, transform: (OpaquePointer) -> T) -> Result<T, NSError> {
return withGitObject(oid, type: type) { Result.success(transform($0)) }
}
/// Loads the object with the given OID.
@ -208,8 +208,8 @@ final public class Repository {
/// oid - The OID of the blob to look up.
///
/// Returns a `Blob`, `Commit`, `Tag`, or `Tree` if one exists, or an error.
public func objectWithOID(_ oid: OID) -> Result<ObjectType, NSError> {
return withLibgit2Object(oid, type: GIT_OBJ_ANY) { object in
public func object(_ oid: OID) -> Result<ObjectType, NSError> {
return withGitObject(oid, type: GIT_OBJ_ANY) { object in
let type = git_object_type(object)
if type == Blob.type {
return Result.success(Blob(object))
@ -237,8 +237,8 @@ final public class Repository {
/// oid - The OID of the blob to look up.
///
/// Returns the blob if it exists, or an error.
public func blobWithOID(_ oid: OID) -> Result<Blob, NSError> {
return self.withLibgit2Object(oid, type: GIT_OBJ_BLOB) { Blob($0) }
public func blob(_ oid: OID) -> Result<Blob, NSError> {
return withGitObject(oid, type: GIT_OBJ_BLOB) { Blob($0) }
}
/// Loads the commit with the given OID.
@ -246,8 +246,8 @@ final public class Repository {
/// oid - The OID of the commit to look up.
///
/// Returns the commit if it exists, or an error.
public func commitWithOID(_ oid: OID) -> Result<Commit, NSError> {
return self.withLibgit2Object(oid, type: GIT_OBJ_COMMIT) { Commit($0) }
public func commit(_ oid: OID) -> Result<Commit, NSError> {
return withGitObject(oid, type: GIT_OBJ_COMMIT) { Commit($0) }
}
/// Loads the tag with the given OID.
@ -255,8 +255,8 @@ final public class Repository {
/// oid - The OID of the tag to look up.
///
/// Returns the tag if it exists, or an error.
public func tagWithOID(_ oid: OID) -> Result<Tag, NSError> {
return self.withLibgit2Object(oid, type: GIT_OBJ_TAG) { Tag($0) }
public func tag(_ oid: OID) -> Result<Tag, NSError> {
return withGitObject(oid, type: GIT_OBJ_TAG) { Tag($0) }
}
/// Loads the tree with the given OID.
@ -264,8 +264,8 @@ final public class Repository {
/// oid - The OID of the tree to look up.
///
/// Returns the tree if it exists, or an error.
public func treeWithOID(_ oid: OID) -> Result<Tree, NSError> {
return self.withLibgit2Object(oid, type: GIT_OBJ_TREE) { Tree($0) }
public func tree(_ oid: OID) -> Result<Tree, NSError> {
return withGitObject(oid, type: GIT_OBJ_TREE) { Tree($0) }
}
/// Loads the referenced object from the pointer.
@ -273,8 +273,8 @@ final public class Repository {
/// pointer - A pointer to an object.
///
/// Returns the object if it exists, or an error.
public func objectFromPointer<T>(_ pointer: PointerTo<T>) -> Result<T, NSError> {
return self.withLibgit2Object(pointer.oid, type: pointer.type) { T($0) }
public func object<T>(from pointer: PointerTo<T>) -> Result<T, NSError> {
return withGitObject(pointer.oid, type: pointer.type) { T($0) }
}
/// Loads the referenced object from the pointer.
@ -282,16 +282,16 @@ final public class Repository {
/// pointer - A pointer to an object.
///
/// Returns the object if it exists, or an error.
public func objectFromPointer(_ pointer: Pointer) -> Result<ObjectType, NSError> {
public func object(from pointer: Pointer) -> Result<ObjectType, NSError> {
switch pointer {
case let .Blob(oid):
return blobWithOID(oid).map { $0 as ObjectType }
return blob(oid).map { $0 as ObjectType }
case let .Commit(oid):
return commitWithOID(oid).map { $0 as ObjectType }
return commit(oid).map { $0 as ObjectType }
case let .Tag(oid):
return tagWithOID(oid).map { $0 as ObjectType }
return tag(oid).map { $0 as ObjectType }
case let .Tree(oid):
return treeWithOID(oid).map { $0 as ObjectType }
return tree(oid).map { $0 as ObjectType }
}
}
@ -306,12 +306,12 @@ final public class Repository {
if result != GIT_OK.rawValue {
pointer.deallocate(capacity: 1)
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_remote_list"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_remote_list"))
}
let strarray = pointer.pointee
let remotes: [Result<Remote, NSError>] = strarray.map {
return self.remoteWithName($0)
return self.remote(named: $0)
}
git_strarray_free(pointer)
pointer.deallocate(capacity: 1)
@ -328,12 +328,12 @@ final public class Repository {
/// name - The name of the remote.
///
/// Returns the remote if it exists, or an error.
public func remoteWithName(_ name: String) -> Result<Remote, NSError> {
public func remote(named name: String) -> Result<Remote, NSError> {
var pointer: OpaquePointer? = nil
let result = git_remote_lookup(&pointer, self.pointer, name)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_remote_lookup"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_remote_lookup"))
}
let value = Remote(pointer!)
@ -344,13 +344,13 @@ final public class Repository {
// MARK: - Reference Lookups
/// Load all the references with the given prefix (e.g. "refs/heads/")
public func referencesWithPrefix(_ prefix: String) -> Result<[ReferenceType], NSError> {
public func references(withPrefix prefix: String) -> Result<[ReferenceType], NSError> {
let pointer = UnsafeMutablePointer<git_strarray>.allocate(capacity: 1)
let result = git_reference_list(pointer, self.pointer)
if result != GIT_OK.rawValue {
pointer.deallocate(capacity: 1)
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_reference_list"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_reference_list"))
}
let strarray = pointer.pointee
@ -359,7 +359,7 @@ final public class Repository {
$0.hasPrefix(prefix)
}
.map {
self.referenceWithName($0)
self.reference(named: $0)
}
git_strarray_free(pointer)
pointer.deallocate(capacity: 1)
@ -376,12 +376,12 @@ final public class Repository {
/// If the reference is a branch, a `Branch` will be returned. If the
/// reference is a tag, a `TagReference` will be returned. Otherwise, a
/// `Reference` will be returned.
public func referenceWithName(_ name: String) -> Result<ReferenceType, NSError> {
public func reference(named name: String) -> Result<ReferenceType, NSError> {
var pointer: OpaquePointer? = nil
let result = git_reference_lookup(&pointer, self.pointer, name)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_reference_lookup"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_reference_lookup"))
}
let value = referenceWithLibGit2Reference(pointer!)
@ -391,7 +391,7 @@ final public class Repository {
/// Load and return a list of all local branches.
public func localBranches() -> Result<[Branch], NSError> {
return referencesWithPrefix("refs/heads/")
return references(withPrefix: "refs/heads/")
.map { (refs: [ReferenceType]) in
return refs.map { $0 as! Branch }
}
@ -399,33 +399,33 @@ final public class Repository {
/// Load and return a list of all remote branches.
public func remoteBranches() -> Result<[Branch], NSError> {
return referencesWithPrefix("refs/remotes/")
return references(withPrefix: "refs/remotes/")
.map { (refs: [ReferenceType]) in
return refs.map { $0 as! Branch }
}
}
/// Load the local branch with the given name (e.g., "master").
public func localBranchWithName(_ name: String) -> Result<Branch, NSError> {
return referenceWithName("refs/heads/" + name).map { $0 as! Branch }
public func localBranch(named name: String) -> Result<Branch, NSError> {
return reference(named: "refs/heads/" + name).map { $0 as! Branch }
}
/// Load the remote branch with the given name (e.g., "origin/master").
public func remoteBranchWithName(_ name: String) -> Result<Branch, NSError> {
return referenceWithName("refs/remotes/" + name).map { $0 as! Branch }
public func remoteBranch(named name: String) -> Result<Branch, NSError> {
return reference(named: "refs/remotes/" + name).map { $0 as! Branch }
}
/// Load and return a list of all the `TagReference`s.
public func allTags() -> Result<[TagReference], NSError> {
return referencesWithPrefix("refs/tags/")
return references(withPrefix: "refs/tags/")
.map { (refs: [ReferenceType]) in
return refs.map { $0 as! TagReference }
}
}
/// Load the tag with the given name (e.g., "tag-2").
public func tagWithName(_ name: String) -> Result<TagReference, NSError> {
return referenceWithName("refs/tags/" + name).map { $0 as! TagReference }
public func tag(named name: String) -> Result<TagReference, NSError> {
return reference(named: "refs/tags/" + name).map { $0 as! TagReference }
}
// MARK: - Working Directory
@ -437,7 +437,7 @@ final public class Repository {
var pointer: OpaquePointer? = nil
let result = git_repository_head(&pointer, self.pointer)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_repository_head"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_repository_head"))
}
let value = referenceWithLibGit2Reference(pointer!)
git_reference_free(pointer)
@ -452,7 +452,7 @@ final public class Repository {
var oid = oid.oid
let result = git_repository_set_head_detached(self.pointer, &oid)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_repository_set_head"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_repository_set_head"))
}
return Result.success()
}
@ -464,7 +464,7 @@ final public class Repository {
public func setHEAD(_ reference: ReferenceType) -> Result<(), NSError> {
let result = git_repository_set_head(self.pointer, reference.longName)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_repository_set_head"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_repository_set_head"))
}
return Result.success()
}
@ -479,7 +479,7 @@ final public class Repository {
let result = git_checkout_head(self.pointer, &options)
if result != GIT_OK.rawValue {
return Result.failure(libGit2Error(result, libGit2PointOfFailure: "git_checkout_head"))
return Result.failure(NSError(gitError: result, pointOfFailure: "git_checkout_head"))
}
return Result.success()

View File

@ -54,22 +54,22 @@ final class Fixtures {
// MARK: - Helpers
func repositoryWithName(_ name: String) -> Repository {
func repository(named name: String) -> Repository {
let url = directoryURL.appendingPathComponent(name, isDirectory: true)
return Repository.atURL(url).value!
return Repository.at(url).value!
}
// MARK: - The Fixtures
class var detachedHeadRepository: Repository {
return Fixtures.sharedInstance.repositoryWithName("detached-head")
return Fixtures.sharedInstance.repository(named: "detached-head")
}
class var simpleRepository: Repository {
return Fixtures.sharedInstance.repositoryWithName("simple-repository")
return Fixtures.sharedInstance.repository(named: "simple-repository")
}
class var mantleRepository: Repository {
return Fixtures.sharedInstance.repositoryWithName("Mantle")
return Fixtures.sharedInstance.repository(named: "Mantle")
}
}

View File

@ -12,16 +12,18 @@ import Nimble
import Quick
import libgit2
func from_git_object<T>(_ repository: Repository, oid: OID, f: (OpaquePointer) -> T) -> T {
let repository = repository.pointer
var oid = oid.oid
var pointer: OpaquePointer? = nil
git_object_lookup(&pointer, repository, &oid, GIT_OBJ_ANY)
let result = f(pointer!)
git_object_free(pointer)
return result
private extension Repository {
func withGitObject<T>(_ oid: OID, transform: (OpaquePointer) -> T) -> T {
let repository = self.pointer
var oid = oid.oid
var pointer: OpaquePointer? = nil
git_object_lookup(&pointer, repository, &oid, GIT_OBJ_ANY)
let result = transform(pointer!)
git_object_free(pointer)
return result
}
}
class SignatureSpec: QuickSpec {
@ -31,7 +33,7 @@ class SignatureSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let raw_signature = from_git_object(repo, oid: oid) { git_commit_author($0).pointee }
let raw_signature = repo.withGitObject(oid) { git_commit_author($0).pointee }
let signature = Signature(raw_signature)
expect(signature.name).to(equal("Matt Diephouse"))
@ -46,7 +48,7 @@ class SignatureSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let author1 = from_git_object(repo, oid: oid) { commit in
let author1 = repo.withGitObject(oid) { commit in
Signature(git_commit_author(commit).pointee)
}
let author2 = author1
@ -59,10 +61,10 @@ class SignatureSpec: QuickSpec {
let oid1 = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let oid2 = OID(string: "24e1e40ee77525d9e279f079f9906ad6d98c8940")!
let author1 = from_git_object(repo, oid: oid1) { commit in
let author1 = repo.withGitObject(oid1) { commit in
Signature(git_commit_author(commit).pointee)
}
let author2 = from_git_object(repo, oid: oid2) { commit in
let author2 = repo.withGitObject(oid2) { commit in
Signature(git_commit_author(commit).pointee)
}
@ -75,7 +77,7 @@ class SignatureSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let author1 = from_git_object(repo, oid: oid) { commit in
let author1 = repo.withGitObject(oid) { commit in
Signature(git_commit_author(commit).pointee)
}
let author2 = author1
@ -93,11 +95,11 @@ class CommitSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "24e1e40ee77525d9e279f079f9906ad6d98c8940")!
let commit = from_git_object(repo, oid: oid) { Commit($0) }
let author = from_git_object(repo, oid: oid) { commit in
let commit = repo.withGitObject(oid) { Commit($0) }
let author = repo.withGitObject(oid) { commit in
Signature(git_commit_author(commit).pointee)
}
let committer = from_git_object(repo, oid: oid) { commit in
let committer = repo.withGitObject(oid) { commit in
Signature(git_commit_committer(commit).pointee)
}
let tree = PointerTo<Tree>(OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!)
@ -116,7 +118,7 @@ class CommitSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let commit = from_git_object(repo, oid: oid) { Commit($0) }
let commit = repo.withGitObject(oid) { Commit($0) }
expect(commit.parents).to(equal([]))
}
@ -124,7 +126,7 @@ class CommitSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "c4ed03a6b7d7ce837d31d83757febbe84dd465fd")!
let commit = from_git_object(repo, oid: oid) { Commit($0) }
let commit = repo.withGitObject(oid) { Commit($0) }
let parents: [PointerTo<Commit>] = [
PointerTo(OID(string: "315b3f344221db91ddc54b269f3c9af422da0f2e")!),
PointerTo(OID(string: "57f6197561d1f99b03c160f4026a07f06b43cf20")!),
@ -138,7 +140,7 @@ class CommitSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let commit1 = from_git_object(repo, oid: oid) { Commit($0) }
let commit1 = repo.withGitObject(oid) { Commit($0) }
let commit2 = commit1
expect(commit1).to(equal(commit2))
}
@ -148,8 +150,8 @@ class CommitSpec: QuickSpec {
let oid1 = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let oid2 = OID(string: "c4ed03a6b7d7ce837d31d83757febbe84dd465fd")!
let commit1 = from_git_object(repo, oid: oid1) { Commit($0) }
let commit2 = from_git_object(repo, oid: oid2) { Commit($0) }
let commit1 = repo.withGitObject(oid1) { Commit($0) }
let commit2 = repo.withGitObject(oid2) { Commit($0) }
expect(commit1).notTo(equal(commit2))
}
}
@ -159,7 +161,7 @@ class CommitSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let commit1 = from_git_object(repo, oid: oid) { Commit($0) }
let commit1 = repo.withGitObject(oid) { Commit($0) }
let commit2 = commit1
expect(commit1.hashValue).to(equal(commit2.hashValue))
}
@ -187,7 +189,7 @@ class TreeEntrySpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!
let entry = from_git_object(repo, oid: oid) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
let entry = repo.withGitObject(oid) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
expect(entry.attributes).to(equal(Int32(GIT_FILEMODE_BLOB.rawValue)))
expect(entry.object).to(equal(Pointer.Blob(OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!)))
expect(entry.name).to(equal("README.md"))
@ -199,7 +201,7 @@ class TreeEntrySpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!
let entry1 = from_git_object(repo, oid: oid) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
let entry1 = repo.withGitObject(oid) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
let entry2 = entry1
expect(entry1).to(equal(entry2))
}
@ -209,8 +211,8 @@ class TreeEntrySpec: QuickSpec {
let oid1 = OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!
let oid2 = OID(string: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc")!
let entry1 = from_git_object(repo, oid: oid1) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
let entry2 = from_git_object(repo, oid: oid2) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
let entry1 = repo.withGitObject(oid1) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
let entry2 = repo.withGitObject(oid2) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
expect(entry1).notTo(equal(entry2))
}
}
@ -220,7 +222,7 @@ class TreeEntrySpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!
let entry1 = from_git_object(repo, oid: oid) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
let entry1 = repo.withGitObject(oid) { Tree.Entry(git_tree_entry_byindex($0, 0)) }
let entry2 = entry1
expect(entry1.hashValue).to(equal(entry2.hashValue))
}
@ -235,7 +237,7 @@ class TreeSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!
let tree = from_git_object(repo, oid: oid) { Tree($0) }
let tree = repo.withGitObject(oid) { Tree($0) }
let entries = [
"README.md": Tree.Entry(attributes: Int32(GIT_FILEMODE_BLOB.rawValue), object: .Blob(OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!), name: "README.md"),
]
@ -248,7 +250,7 @@ class TreeSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!
let tree1 = from_git_object(repo, oid: oid) { Tree($0) }
let tree1 = repo.withGitObject(oid) { Tree($0) }
let tree2 = tree1
expect(tree1).to(equal(tree2))
}
@ -258,8 +260,8 @@ class TreeSpec: QuickSpec {
let oid1 = OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!
let oid2 = OID(string: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc")!
let tree1 = from_git_object(repo, oid: oid1) { Tree($0) }
let tree2 = from_git_object(repo, oid: oid2) { Tree($0) }
let tree1 = repo.withGitObject(oid1) { Tree($0) }
let tree2 = repo.withGitObject(oid2) { Tree($0) }
expect(tree1).notTo(equal(tree2))
}
}
@ -269,7 +271,7 @@ class TreeSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "219e9f39c2fb59ed1dfb3e78ed75055a57528f31")!
let tree1 = from_git_object(repo, oid: oid) { Tree($0) }
let tree1 = repo.withGitObject(oid) { Tree($0) }
let tree2 = tree1
expect(tree1.hashValue).to(equal(tree2.hashValue))
}
@ -284,7 +286,7 @@ class BlobSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
let blob = from_git_object(repo, oid: oid) { Blob($0) }
let blob = repo.withGitObject(oid) { Blob($0) }
let contents = "# Simple Repository\nA simple repository used for testing SwiftGit2.\n\n## Branches\n\n- master\n\n"
let data = contents.data(using: String.Encoding.utf8)!
expect(blob.oid).to(equal(oid))
@ -297,7 +299,7 @@ class BlobSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
let blob1 = from_git_object(repo, oid: oid) { Blob($0) }
let blob1 = repo.withGitObject(oid) { Blob($0) }
let blob2 = blob1
expect(blob1).to(equal(blob2))
}
@ -307,8 +309,8 @@ class BlobSpec: QuickSpec {
let oid1 = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
let oid2 = OID(string: "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391")!
let blob1 = from_git_object(repo, oid: oid1) { Blob($0) }
let blob2 = from_git_object(repo, oid: oid2) { Blob($0) }
let blob1 = repo.withGitObject(oid1) { Blob($0) }
let blob2 = repo.withGitObject(oid2) { Blob($0) }
expect(blob1).notTo(equal(blob2))
}
}
@ -318,7 +320,7 @@ class BlobSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
let blob1 = from_git_object(repo, oid: oid) { Blob($0) }
let blob1 = repo.withGitObject(oid) { Blob($0) }
let blob2 = blob1
expect(blob1.hashValue).to(equal(blob2.hashValue))
}
@ -333,8 +335,8 @@ class TagSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "57943b8ee00348180ceeedc960451562750f6d33")!
let tag = from_git_object(repo, oid: oid) { Tag($0) }
let tagger = from_git_object(repo, oid: oid) { Signature(git_tag_tagger($0).pointee) }
let tag = repo.withGitObject(oid) { Tag($0) }
let tagger = repo.withGitObject(oid) { Signature(git_tag_tagger($0).pointee) }
expect(tag.oid).to(equal(oid))
expect(tag.target).to(equal(Pointer.Commit(OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!)))
@ -349,7 +351,7 @@ class TagSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "57943b8ee00348180ceeedc960451562750f6d33")!
let tag1 = from_git_object(repo, oid: oid) { Tag($0) }
let tag1 = repo.withGitObject(oid) { Tag($0) }
let tag2 = tag1
expect(tag1).to(equal(tag2))
}
@ -359,8 +361,8 @@ class TagSpec: QuickSpec {
let oid1 = OID(string: "57943b8ee00348180ceeedc960451562750f6d33")!
let oid2 = OID(string: "13bda91157f255ab224ff88d0a11a82041c9d0c1")!
let tag1 = from_git_object(repo, oid: oid1) { Tag($0) }
let tag2 = from_git_object(repo, oid: oid2) { Tag($0) }
let tag1 = repo.withGitObject(oid1) { Tag($0) }
let tag2 = repo.withGitObject(oid2) { Tag($0) }
expect(tag1).notTo(equal(tag2))
}
}
@ -370,7 +372,7 @@ class TagSpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "57943b8ee00348180ceeedc960451562750f6d33")!
let tag1 = from_git_object(repo, oid: oid) { Tag($0) }
let tag1 = repo.withGitObject(oid) { Tag($0) }
let tag2 = tag1
expect(tag1.hashValue).to(equal(tag2.hashValue))
}

View File

@ -12,15 +12,17 @@ import Nimble
import Quick
import libgit2
func from_git_reference<T>(_ repository: Repository, name: String, f: (OpaquePointer) -> T) -> T {
let repository = repository.pointer
var pointer: OpaquePointer? = nil
git_reference_lookup(&pointer, repository, name)
let result = f(pointer!)
git_object_free(pointer)
return result
private extension Repository {
func withGitReference<T>(named name: String, transform: (OpaquePointer) -> T) -> T {
let repository = self.pointer
var pointer: OpaquePointer? = nil
git_reference_lookup(&pointer, repository, name)
let result = transform(pointer!)
git_reference_free(pointer)
return result
}
}
class ReferenceSpec: QuickSpec {
@ -28,7 +30,7 @@ class ReferenceSpec: QuickSpec {
describe("Reference(pointer)") {
it("should initialize its properties") {
let repo = Fixtures.simpleRepository
let ref = from_git_reference(repo, name: "refs/heads/master") { Reference($0) }
let ref = repo.withGitReference(named: "refs/heads/master") { Reference($0) }
expect(ref.longName).to(equal("refs/heads/master"))
expect(ref.shortName).to(equal("master"))
expect(ref.oid).to(equal(OID(string: "c4ed03a6b7d7ce837d31d83757febbe84dd465fd")!))
@ -38,15 +40,15 @@ class ReferenceSpec: QuickSpec {
describe("==(Reference, Reference)") {
it("should be true with equal references") {
let repo = Fixtures.simpleRepository
let ref1 = from_git_reference(repo, name: "refs/heads/master") { Reference($0) }
let ref2 = from_git_reference(repo, name: "refs/heads/master") { Reference($0) }
let ref1 = repo.withGitReference(named: "refs/heads/master") { Reference($0) }
let ref2 = repo.withGitReference(named: "refs/heads/master") { Reference($0) }
expect(ref1).to(equal(ref2))
}
it("should be false with unequal references") {
let repo = Fixtures.simpleRepository
let ref1 = from_git_reference(repo, name: "refs/heads/master") { Reference($0) }
let ref2 = from_git_reference(repo, name: "refs/heads/another-branch") { Reference($0) }
let ref1 = repo.withGitReference(named: "refs/heads/master") { Reference($0) }
let ref2 = repo.withGitReference(named: "refs/heads/another-branch") { Reference($0) }
expect(ref1).notTo(equal(ref2))
}
}
@ -54,8 +56,8 @@ class ReferenceSpec: QuickSpec {
describe("Reference.hashValue") {
it("should be equal with equal references") {
let repo = Fixtures.simpleRepository
let ref1 = from_git_reference(repo, name: "refs/heads/master") { Reference($0) }
let ref2 = from_git_reference(repo, name: "refs/heads/master") { Reference($0) }
let ref1 = repo.withGitReference(named: "refs/heads/master") { Reference($0) }
let ref2 = repo.withGitReference(named: "refs/heads/master") { Reference($0) }
expect(ref1.hashValue).to(equal(ref2.hashValue))
}
}
@ -67,7 +69,7 @@ class BranchSpec: QuickSpec {
describe("Branch(pointer)") {
it("should initialize its properties") {
let repo = Fixtures.mantleRepository
let branch = from_git_reference(repo, name: "refs/heads/master") { Branch($0)! }
let branch = repo.withGitReference(named: "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))
@ -79,7 +81,7 @@ class BranchSpec: QuickSpec {
it("should work with symoblic refs") {
let repo = Fixtures.mantleRepository
let branch = from_git_reference(repo, name: "refs/remotes/origin/HEAD") { Branch($0)! }
let branch = repo.withGitReference(named: "refs/remotes/origin/HEAD") { Branch($0)! }
expect(branch.longName).to(equal("refs/remotes/origin/HEAD"))
expect(branch.name).to(equal("origin/HEAD"))
expect(branch.shortName).to(equal(branch.name))
@ -93,15 +95,15 @@ class BranchSpec: QuickSpec {
describe("==(Branch, Branch)") {
it("should be true with equal branches") {
let repo = Fixtures.simpleRepository
let branch1 = from_git_reference(repo, name: "refs/heads/master") { Branch($0)! }
let branch2 = from_git_reference(repo, name: "refs/heads/master") { Branch($0)! }
let branch1 = repo.withGitReference(named: "refs/heads/master") { Branch($0)! }
let branch2 = repo.withGitReference(named: "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, name: "refs/heads/master") { Branch($0)! }
let branch2 = from_git_reference(repo, name: "refs/heads/another-branch") { Branch($0)! }
let branch1 = repo.withGitReference(named: "refs/heads/master") { Branch($0)! }
let branch2 = repo.withGitReference(named: "refs/heads/another-branch") { Branch($0)! }
expect(branch1).notTo(equal(branch2))
}
}
@ -109,8 +111,8 @@ class BranchSpec: QuickSpec {
describe("Branch.hashValue") {
it("should be equal with equal references") {
let repo = Fixtures.simpleRepository
let branch1 = from_git_reference(repo, name: "refs/heads/master") { Branch($0)! }
let branch2 = from_git_reference(repo, name: "refs/heads/master") { Branch($0)! }
let branch1 = repo.withGitReference(named: "refs/heads/master") { Branch($0)! }
let branch2 = repo.withGitReference(named: "refs/heads/master") { Branch($0)! }
expect(branch1.hashValue).to(equal(branch2.hashValue))
}
}
@ -122,7 +124,7 @@ class TagReferenceSpec: QuickSpec {
describe("TagReference(pointer)") {
it("should work with an annotated tag") {
let repo = Fixtures.simpleRepository
let tag = from_git_reference(repo, name: "refs/tags/tag-2") { TagReference($0)! }
let tag = repo.withGitReference(named: "refs/tags/tag-2") { TagReference($0)! }
expect(tag.longName).to(equal("refs/tags/tag-2"))
expect(tag.name).to(equal("tag-2"))
expect(tag.shortName).to(equal(tag.name))
@ -131,7 +133,7 @@ class TagReferenceSpec: QuickSpec {
it("should work with a lightweight tag") {
let repo = Fixtures.mantleRepository
let tag = from_git_reference(repo, name: "refs/tags/1.5.4") { TagReference($0)! }
let tag = repo.withGitReference(named: "refs/tags/1.5.4") { TagReference($0)! }
expect(tag.longName).to(equal("refs/tags/1.5.4"))
expect(tag.name).to(equal("1.5.4"))
expect(tag.shortName).to(equal(tag.name))
@ -140,7 +142,7 @@ class TagReferenceSpec: QuickSpec {
it("should return nil if not a tag") {
let repo = Fixtures.simpleRepository
let tag = from_git_reference(repo, name: "refs/heads/master") { TagReference($0) }
let tag = repo.withGitReference(named: "refs/heads/master") { TagReference($0) }
expect(tag).to(beNil())
}
}
@ -148,15 +150,15 @@ class TagReferenceSpec: QuickSpec {
describe("==(TagReference, TagReference)") {
it("should be true with equal tag references") {
let repo = Fixtures.simpleRepository
let tag1 = from_git_reference(repo, name: "refs/tags/tag-2") { TagReference($0)! }
let tag2 = from_git_reference(repo, name: "refs/tags/tag-2") { TagReference($0)! }
let tag1 = repo.withGitReference(named: "refs/tags/tag-2") { TagReference($0)! }
let tag2 = repo.withGitReference(named: "refs/tags/tag-2") { TagReference($0)! }
expect(tag1).to(equal(tag2))
}
it("should be false with unequal tag references") {
let repo = Fixtures.simpleRepository
let tag1 = from_git_reference(repo, name: "refs/tags/tag-1") { TagReference($0)! }
let tag2 = from_git_reference(repo, name: "refs/tags/tag-2") { TagReference($0)! }
let tag1 = repo.withGitReference(named: "refs/tags/tag-1") { TagReference($0)! }
let tag2 = repo.withGitReference(named: "refs/tags/tag-2") { TagReference($0)! }
expect(tag1).notTo(equal(tag2))
}
}
@ -164,8 +166,8 @@ class TagReferenceSpec: QuickSpec {
describe("TagReference.hashValue") {
it("should be equal with equal references") {
let repo = Fixtures.simpleRepository
let tag1 = from_git_reference(repo, name: "refs/tags/tag-2") { TagReference($0)! }
let tag2 = from_git_reference(repo, name: "refs/tags/tag-2") { TagReference($0)! }
let tag1 = repo.withGitReference(named: "refs/tags/tag-2") { TagReference($0)! }
let tag2 = repo.withGitReference(named: "refs/tags/tag-2") { TagReference($0)! }
expect(tag1.hashValue).to(equal(tag2.hashValue))
}
}

View File

@ -12,15 +12,17 @@ import Nimble
import Quick
import libgit2
func with_git_remote<T>(_ repository: Repository, name: String, f: (OpaquePointer) -> T) -> T {
let repository = repository.pointer
var pointer: OpaquePointer? = nil
git_remote_lookup(&pointer, repository, name)
let result = f(pointer!)
git_object_free(pointer)
return result
private extension Repository {
func withGitRemote<T>(named name: String, transform: (OpaquePointer) -> T) -> T {
let repository = self.pointer
var pointer: OpaquePointer? = nil
git_remote_lookup(&pointer, repository, name)
let result = transform(pointer!)
git_remote_free(pointer)
return result
}
}
class RemoteSpec: QuickSpec {
@ -28,7 +30,7 @@ class RemoteSpec: QuickSpec {
describe("Remote(pointer)") {
it("should initialize its properties") {
let repo = Fixtures.mantleRepository
let remote = with_git_remote(repo, name: "upstream") { Remote($0) }
let remote = repo.withGitRemote(named: "upstream") { Remote($0) }
expect(remote.name).to(equal("upstream"))
expect(remote.URL).to(equal("git@github.com:Mantle/Mantle.git"))
@ -38,15 +40,15 @@ class RemoteSpec: QuickSpec {
describe("==(Remote, Remote)") {
it("should be true with equal objects") {
let repo = Fixtures.mantleRepository
let remote1 = with_git_remote(repo, name: "upstream") { Remote($0) }
let remote1 = repo.withGitRemote(named: "upstream") { Remote($0) }
let remote2 = remote1
expect(remote1).to(equal(remote2))
}
it("should be false with unequal objcets") {
let repo = Fixtures.mantleRepository
let origin = with_git_remote(repo, name: "origin") { Remote($0) }
let upstream = with_git_remote(repo, name: "upstream") { Remote($0) }
let origin = repo.withGitRemote(named: "origin") { Remote($0) }
let upstream = repo.withGitRemote(named: "upstream") { Remote($0) }
expect(origin).notTo(equal(upstream))
}
}
@ -54,7 +56,7 @@ class RemoteSpec: QuickSpec {
describe("Remote.hashValue") {
it("should be equal with equal objcets") {
let repo = Fixtures.mantleRepository
let remote1 = with_git_remote(repo, name: "upstream") { Remote($0) }
let remote1 = repo.withGitRemote(named: "upstream") { Remote($0) }
let remote2 = remote1
expect(remote1.hashValue).to(equal(remote2.hashValue))
}

View File

@ -14,7 +14,7 @@ import Guanaco
class RepositorySpec: QuickSpec {
override func spec() {
describe("Repository.Type.atURL()") {
describe("Repository.Type.at(_:)") {
it("should work if the repo exists") {
let repo = Fixtures.simpleRepository
expect(repo.directoryURL).notTo(beNil())
@ -22,7 +22,7 @@ class RepositorySpec: QuickSpec {
it("should fail if the repo doesn't exist") {
let url = URL(fileURLWithPath: "blah")
let result = Repository.atURL(url)
let result = Repository.at(url)
expect(result).to(haveFailed(beAnError(
domain: equal(libGit2ErrorDomain),
localizedDescription: match("Failed to resolve path")
@ -30,11 +30,11 @@ class RepositorySpec: QuickSpec {
}
}
describe("Repository.Type.clone()") {
describe("Repository.Type.clone(from:to:)") {
it("should handle local clones") {
let remoteRepo = Fixtures.simpleRepository
let localURL = self.temporaryURLForPurpose("local-clone")
let result = Repository.cloneFromURL(remoteRepo.directoryURL!, toURL: localURL, localClone: true)
let localURL = self.temporaryURL(forPurpose: "local-clone")
let result = Repository.clone(from: remoteRepo.directoryURL!, to: localURL, localClone: true)
expect(result).to(haveSucceeded())
@ -45,8 +45,8 @@ class RepositorySpec: QuickSpec {
it("should handle bare clones") {
let remoteRepo = Fixtures.simpleRepository
let localURL = self.temporaryURLForPurpose("bare-clone")
let result = Repository.cloneFromURL(remoteRepo.directoryURL!, toURL: localURL, localClone: true, bare: true)
let localURL = self.temporaryURL(forPurpose: "bare-clone")
let result = Repository.clone(from: remoteRepo.directoryURL!, to: localURL, localClone: true, bare: true)
expect(result).to(haveSucceeded())
@ -57,13 +57,13 @@ class RepositorySpec: QuickSpec {
it("should have set a valid remote url") {
let remoteRepo = Fixtures.simpleRepository
let localURL = self.temporaryURLForPurpose("valid-remote-clone")
let cloneResult = Repository.cloneFromURL(remoteRepo.directoryURL!, toURL: localURL, localClone: true)
let localURL = self.temporaryURL(forPurpose: "valid-remote-clone")
let cloneResult = Repository.clone(from: remoteRepo.directoryURL!, to: localURL, localClone: true)
expect(cloneResult).to(haveSucceeded())
if case .success(let clonedRepo) = cloneResult {
let remoteResult = clonedRepo.remoteWithName("origin")
let remoteResult = clonedRepo.remote(named: "origin")
expect(remoteResult).to(haveSucceeded())
if case .success(let remote) = remoteResult {
@ -74,13 +74,13 @@ class RepositorySpec: QuickSpec {
it("should be able to clone a remote repository") {
let remoteRepoURL = URL(string: "https://github.com/libgit2/libgit2.github.com.git")
let localURL = self.temporaryURLForPurpose("public-remote-clone")
let cloneResult = Repository.cloneFromURL(remoteRepoURL!, toURL: localURL)
let localURL = self.temporaryURL(forPurpose: "public-remote-clone")
let cloneResult = Repository.clone(from: remoteRepoURL!, to: localURL)
expect(cloneResult).to(haveSucceeded())
if case .success(let clonedRepo) = cloneResult {
let remoteResult = clonedRepo.remoteWithName("origin")
let remoteResult = clonedRepo.remote(named: "origin")
expect(remoteResult).to(haveSucceeded())
if case .success(let remote) = remoteResult {
@ -96,15 +96,15 @@ class RepositorySpec: QuickSpec {
it("should be able to clone a remote repository requiring credentials") {
let remoteRepoURL = URL(string: privateRepo)
let localURL = self.temporaryURLForPurpose("private-remote-clone")
let localURL = self.temporaryURL(forPurpose: "private-remote-clone")
let cloneResult = Repository.cloneFromURL(remoteRepoURL!, toURL: localURL,
let cloneResult = Repository.clone(from: remoteRepoURL!, to: localURL,
credentials: .SSHMemory(username: gitUsername, publicKey: publicKey, privateKey: privateKey, passphrase: passphrase))
expect(cloneResult).to(haveSucceeded())
if case .success(let clonedRepo) = cloneResult {
let remoteResult = clonedRepo.remoteWithName("origin")
let remoteResult = clonedRepo.remote(named: "origin")
expect(remoteResult).to(haveSucceeded())
if case .success(let remote) = remoteResult {
@ -115,12 +115,12 @@ class RepositorySpec: QuickSpec {
}
}
describe("Repository.blobWithOID()") {
describe("Repository.blob(_:)") {
it("should return the commit if it exists") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
let result = repo.blobWithOID(oid)
let result = repo.blob(oid)
expect(result.map { $0.oid }).to(haveSucceeded(equal(oid)))
}
@ -128,7 +128,7 @@ class RepositorySpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")!
let result = repo.blobWithOID(oid)
let result = repo.blob(oid)
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
@ -137,17 +137,17 @@ class RepositorySpec: QuickSpec {
// This is a tree in the repository
let oid = OID(string: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc")!
let result = repo.blobWithOID(oid)
let result = repo.blob(oid)
expect(result).to(haveFailed())
}
}
describe("Repository.commitWithOID()") {
describe("Repository.commit(_:)") {
it("should return the commit if it exists") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let result = repo.commitWithOID(oid)
let result = repo.commit(oid)
expect(result.map { $0.oid }).to(haveSucceeded(equal(oid)))
}
@ -155,7 +155,7 @@ class RepositorySpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")!
let result = repo.commitWithOID(oid)
let result = repo.commit(oid)
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
@ -164,17 +164,17 @@ class RepositorySpec: QuickSpec {
// This is a tree in the repository
let oid = OID(string: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc")!
let result = repo.commitWithOID(oid)
let result = repo.commit(oid)
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
}
describe("Repository.tagWithOID()") {
describe("Repository.tag(_:)") {
it("should return the tag if it exists") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "57943b8ee00348180ceeedc960451562750f6d33")!
let result = repo.tagWithOID(oid)
let result = repo.tag(oid)
expect(result.map { $0.oid }).to(haveSucceeded(equal(oid)))
}
@ -182,7 +182,7 @@ class RepositorySpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")!
let result = repo.tagWithOID(oid)
let result = repo.tag(oid)
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
@ -191,17 +191,17 @@ class RepositorySpec: QuickSpec {
// This is a commit in the repository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let result = repo.tagWithOID(oid)
let result = repo.tag(oid)
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
}
describe("Repository.treeWithOID()") {
describe("Repository.tree(_:)") {
it("should return the tree if it exists") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc")!
let result = repo.treeWithOID(oid)
let result = repo.tree(oid)
expect(result.map { $0.oid }).to(haveSucceeded(equal(oid)))
}
@ -209,7 +209,7 @@ class RepositorySpec: QuickSpec {
let repo = Fixtures.simpleRepository
let oid = OID(string: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")!
let result = repo.treeWithOID(oid)
let result = repo.tree(oid)
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
@ -218,60 +218,60 @@ class RepositorySpec: QuickSpec {
// This is a commit in the repository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let result = repo.treeWithOID(oid)
let result = repo.tree(oid)
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
}
describe("Repository.objectWithOID()") {
describe("Repository.object(_:)") {
it("should work with a blob") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
let blob = repo.blobWithOID(oid).value
let result = repo.objectWithOID(oid)
let blob = repo.blob(oid).value
let result = repo.object(oid)
expect(result.map { $0 as! Blob }).to(haveSucceeded(equal(blob)))
}
it("should work with a commit") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let commit = repo.commitWithOID(oid).value
let result = repo.objectWithOID(oid)
let commit = repo.commit(oid).value
let result = repo.object(oid)
expect(result.map { $0 as! Commit }).to(haveSucceeded(equal(commit)))
}
it("should work with a tag") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "57943b8ee00348180ceeedc960451562750f6d33")!
let tag = repo.tagWithOID(oid).value
let result = repo.objectWithOID(oid)
let tag = repo.tag(oid).value
let result = repo.object(oid)
expect(result.map { $0 as! Tag }).to(haveSucceeded(equal(tag)))
}
it("should work with a tree") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc")!
let tree = repo.treeWithOID(oid).value
let result = repo.objectWithOID(oid)
let tree = repo.tree(oid).value
let result = repo.object(oid)
expect(result.map { $0 as! Tree }).to(haveSucceeded(equal(tree)))
}
it("should error if there's no object with that oid") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")!
let result = repo.objectWithOID(oid)
let result = repo.object(oid)
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
}
describe("Repsoitory.objectFromPointer(PointerTo)") {
describe("Repository.object(from: PointerTo)") {
it("should work with commits") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let pointer = PointerTo<Commit>(oid)
let commit = repo.commitWithOID(oid).value!
expect(repo.objectFromPointer(pointer)).to(haveSucceeded(equal(commit)))
let commit = repo.commit(oid).value!
expect(repo.object(from: pointer)).to(haveSucceeded(equal(commit)))
}
it("should work with trees") {
@ -279,8 +279,8 @@ class RepositorySpec: QuickSpec {
let oid = OID(string: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc")!
let pointer = PointerTo<Tree>(oid)
let tree = repo.treeWithOID(oid).value!
expect(repo.objectFromPointer(pointer)).to(haveSucceeded(equal(tree)))
let tree = repo.tree(oid).value!
expect(repo.object(from: pointer)).to(haveSucceeded(equal(tree)))
}
it("should work with blobs") {
@ -288,8 +288,8 @@ class RepositorySpec: QuickSpec {
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
let pointer = PointerTo<Blob>(oid)
let blob = repo.blobWithOID(oid).value!
expect(repo.objectFromPointer(pointer)).to(haveSucceeded(equal(blob)))
let blob = repo.blob(oid).value!
expect(repo.object(from: pointer)).to(haveSucceeded(equal(blob)))
}
it("should work with tags") {
@ -297,19 +297,19 @@ class RepositorySpec: QuickSpec {
let oid = OID(string: "57943b8ee00348180ceeedc960451562750f6d33")!
let pointer = PointerTo<Tag>(oid)
let tag = repo.tagWithOID(oid).value!
expect(repo.objectFromPointer(pointer)).to(haveSucceeded(equal(tag)))
let tag = repo.tag(oid).value!
expect(repo.object(from: pointer)).to(haveSucceeded(equal(tag)))
}
}
describe("Repository.objectFromPointer(Pointer)") {
describe("Repository.object(from: Pointer)") {
it("should work with commits") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let pointer = Pointer.Commit(oid)
let commit = repo.commitWithOID(oid).value!
let result = repo.objectFromPointer(pointer).map { $0 as! Commit }
let commit = repo.commit(oid).value!
let result = repo.object(from: pointer).map { $0 as! Commit }
expect(result).to(haveSucceeded(equal(commit)))
}
@ -318,8 +318,8 @@ class RepositorySpec: QuickSpec {
let oid = OID(string: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc")!
let pointer = Pointer.Tree(oid)
let tree = repo.treeWithOID(oid).value!
let result = repo.objectFromPointer(pointer).map { $0 as! Tree }
let tree = repo.tree(oid).value!
let result = repo.object(from: pointer).map { $0 as! Tree }
expect(result).to(haveSucceeded(equal(tree)))
}
@ -328,8 +328,8 @@ class RepositorySpec: QuickSpec {
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
let pointer = Pointer.Blob(oid)
let blob = repo.blobWithOID(oid).value!
let result = repo.objectFromPointer(pointer).map { $0 as! Blob }
let blob = repo.blob(oid).value!
let result = repo.object(from: pointer).map { $0 as! Blob }
expect(result).to(haveSucceeded(equal(blob)))
}
@ -338,8 +338,8 @@ class RepositorySpec: QuickSpec {
let oid = OID(string: "57943b8ee00348180ceeedc960451562750f6d33")!
let pointer = Pointer.Tag(oid)
let tag = repo.tagWithOID(oid).value!
let result = repo.objectFromPointer(pointer).map { $0 as! Tag }
let tag = repo.tag(oid).value!
let result = repo.object(from: pointer).map { $0 as! Tag }
expect(result).to(haveSucceeded(equal(tag)))
}
}
@ -360,50 +360,50 @@ class RepositorySpec: QuickSpec {
}
}
describe("Repository.remoteWithName()") {
describe("Repository.remote(named:)") {
it("should return the remote if it exists") {
let repo = Fixtures.mantleRepository
let result = repo.remoteWithName("upstream")
let result = repo.remote(named: "upstream")
expect(result.map { $0.name }).to(haveSucceeded(equal("upstream")))
}
it("should error if the remote doesn't exist") {
let repo = Fixtures.simpleRepository
let result = repo.remoteWithName("nonexistent")
let result = repo.remote(named: "nonexistent")
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
}
describe("Repository.referenceWithName()") {
describe("Repository.reference(named:)") {
it("should return a local branch if it exists") {
let name = "refs/heads/master"
let result = Fixtures.simpleRepository.referenceWithName(name)
let result = Fixtures.simpleRepository.reference(named: name)
expect(result.map { $0.longName }).to(haveSucceeded(equal(name)))
expect(result.value as? Branch).notTo(beNil())
}
it("should return a remote branch if it exists") {
let name = "refs/remotes/upstream/master"
let result = Fixtures.mantleRepository.referenceWithName(name)
let result = Fixtures.mantleRepository.reference(named: name)
expect(result.map { $0.longName }).to(haveSucceeded(equal(name)))
expect(result.value as? Branch).notTo(beNil())
}
it("should return a tag if it exists") {
let name = "refs/tags/tag-2"
let result = Fixtures.simpleRepository.referenceWithName(name)
let result = Fixtures.simpleRepository.reference(named: name)
expect(result.value?.longName).to(equal(name))
expect(result.value as? TagReference).notTo(beNil())
}
it("should return the reference if it exists") {
let name = "refs/other-ref"
let result = Fixtures.simpleRepository.referenceWithName(name)
let result = Fixtures.simpleRepository.reference(named: name)
expect(result.value?.longName).to(equal(name))
}
it("should error if the reference doesn't exist") {
let result = Fixtures.simpleRepository.referenceWithName("refs/heads/nonexistent")
let result = Fixtures.simpleRepository.reference(named: "refs/heads/nonexistent")
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
}
@ -412,9 +412,9 @@ class RepositorySpec: QuickSpec {
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!,
repo.localBranch(named: "another-branch").value!,
repo.localBranch(named: "master").value!,
repo.localBranch(named: "yet-another-branch").value!,
]
expect(repo.localBranches().value).to(equal(expected))
}
@ -442,7 +442,7 @@ class RepositorySpec: QuickSpec {
"upstream/reversible-transformer",
"upstream/subclassing-notes",
]
let expected = expectedNames.map { repo.remoteBranchWithName($0).value! }
let expected = expectedNames.map { repo.remoteBranch(named: $0).value! }
let actual = repo.remoteBranches().value!.sorted {
return $0.longName.characters.lexicographicallyPrecedes($1.longName.characters)
}
@ -451,26 +451,26 @@ class RepositorySpec: QuickSpec {
}
}
describe("Repository.localBranchWithName()") {
describe("Repository.localBranch(named:)") {
it("should return the branch if it exists") {
let result = Fixtures.simpleRepository.localBranchWithName("master")
let result = Fixtures.simpleRepository.localBranch(named: "master")
expect(result.value?.longName).to(equal("refs/heads/master"))
}
it("should error if the branch doesn't exists") {
let result = Fixtures.simpleRepository.localBranchWithName("nonexistent")
let result = Fixtures.simpleRepository.localBranch(named: "nonexistent")
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
}
describe("Repository.remoteBranchWithName()") {
describe("Repository.remoteBranch(named:)") {
it("should return the branch if it exists") {
let result = Fixtures.mantleRepository.remoteBranchWithName("upstream/master")
let result = Fixtures.mantleRepository.remoteBranch(named: "upstream/master")
expect(result.value?.longName).to(equal("refs/remotes/upstream/master"))
}
it("should error if the branch doesn't exists") {
let result = Fixtures.simpleRepository.remoteBranchWithName("origin/nonexistent")
let result = Fixtures.simpleRepository.remoteBranch(named: "origin/nonexistent")
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
}
@ -479,21 +479,21 @@ class RepositorySpec: QuickSpec {
it("should return all the tags") {
let repo = Fixtures.simpleRepository
let expected = [
repo.tagWithName("tag-1").value!,
repo.tagWithName("tag-2").value!,
repo.tag(named: "tag-1").value!,
repo.tag(named: "tag-2").value!,
]
expect(repo.allTags().value).to(equal(expected))
}
}
describe("Repository.tagWithName()") {
describe("Repository.tag(named:)") {
it("should return the tag if it exists") {
let result = Fixtures.simpleRepository.tagWithName("tag-2")
let result = Fixtures.simpleRepository.tag(named: "tag-2")
expect(result.value?.longName).to(equal("refs/tags/tag-2"))
}
it("should error if the branch doesn't exists") {
let result = Fixtures.simpleRepository.tagWithName("nonexistent")
let result = Fixtures.simpleRepository.tag(named: "nonexistent")
expect(result).to(haveFailed(beAnError(domain: equal(libGit2ErrorDomain))))
}
}
@ -526,7 +526,7 @@ class RepositorySpec: QuickSpec {
expect(HEAD?.longName).to(equal("HEAD"))
expect(HEAD?.oid).to(equal(oid))
expect(repo.setHEAD(repo.localBranchWithName("master").value!)).to(haveSucceeded())
expect(repo.setHEAD(repo.localBranch(named: "master").value!)).to(haveSucceeded())
expect(repo.HEAD().value?.shortName).to(equal("master"))
}
}
@ -537,7 +537,7 @@ class RepositorySpec: QuickSpec {
let oid = repo.HEAD().value!.oid
expect(repo.HEAD().value?.longName).to(equal("HEAD"))
let branch = repo.localBranchWithName("another-branch").value!
let branch = repo.localBranch(named: "another-branch").value!
expect(repo.setHEAD(branch)).to(haveSucceeded())
expect(repo.HEAD().value?.shortName).to(equal(branch.name))
@ -561,7 +561,7 @@ class RepositorySpec: QuickSpec {
expect(HEAD?.longName).to(equal("HEAD"))
expect(HEAD?.oid).to(equal(oid))
expect(repo.checkout(repo.localBranchWithName("master").value!, strategy: CheckoutStrategy.None)).to(haveSucceeded())
expect(repo.checkout(repo.localBranch(named: "master").value!, strategy: CheckoutStrategy.None)).to(haveSucceeded())
expect(repo.HEAD().value?.shortName).to(equal("master"))
}
@ -586,7 +586,7 @@ class RepositorySpec: QuickSpec {
let oid = repo.HEAD().value!.oid
expect(repo.HEAD().value?.longName).to(equal("HEAD"))
let branch = repo.localBranchWithName("another-branch").value!
let branch = repo.localBranch(named: "another-branch").value!
expect(repo.checkout(branch, strategy: CheckoutStrategy.None)).to(haveSucceeded())
expect(repo.HEAD().value?.shortName).to(equal(branch.name))
@ -596,7 +596,7 @@ class RepositorySpec: QuickSpec {
}
}
func temporaryURLForPurpose(_ purpose: String) -> URL {
func temporaryURL(forPurpose purpose: String) -> URL {
let globallyUniqueString = ProcessInfo.processInfo.globallyUniqueString
let path = "\(NSTemporaryDirectory())\(globallyUniqueString)_\(purpose)"
return URL(fileURLWithPath: path)