Add a Signature struct

This commit is contained in:
Matt Diephouse 2014-12-06 00:01:09 -05:00
parent cfe199bc94
commit b49cbacfd7
2 changed files with 62 additions and 6 deletions

View File

@ -6,12 +6,36 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
//
import Foundation
/// A git object.
public protocol Object {
/// The OID of the object.
var oid: OID { get }
}
public struct Signature {
/// The name of the person.
public let name: String
/// The email of the person.
public let email: String
/// The time when the action happened.
public let time: NSDate
/// The time zone that `time` should be interpreted relative to.
public let timeZone: NSTimeZone
/// Create an instance with a libgit2 `git_signature`.
public init(signature: git_signature) {
name = String.fromCString(signature.name)!
email = String.fromCString(signature.email)!
time = NSDate(timeIntervalSince1970: NSTimeInterval(signature.when.time))
timeZone = NSTimeZone(forSecondsFromGMT: NSInteger(60 * signature.when.offset))
}
}
/// A git commit.
public struct Commit: Object {
public let oid: OID

View File

@ -11,16 +11,48 @@ import SwiftGit2
import Nimble
import Quick
class CommitSpec: QuickSpec {
func from_git_object<T>(repository: Repository, oid: OID, f: COpaquePointer -> T) -> T{
let repository = repository.pointer
var oid = oid.oid
let pointer = UnsafeMutablePointer<COpaquePointer>.alloc(1)
git_object_lookup(pointer, repository, &oid, GIT_OBJ_ANY)
let result = f(pointer.memory)
git_object_free(pointer.memory)
pointer.dealloc(1)
return result
}
class SignatureSpec: QuickSpec {
override func spec() {
describe("init(pointer:)") {
it("should set its properties") {
describe("init(signature:)") {
it("should initialize its properties") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let commit = repo.commitWithOID(oid).value()
expect(commit?.oid).to(equal(oid))
expect(commit?.message).to(equal("Create a README\n"))
let raw_signature = from_git_object(repo, oid) { git_commit_author($0).memory }
let signature = Signature(signature: raw_signature)
expect(signature.name).to(equal("Matt Diephouse"))
expect(signature.email).to(equal("matt@diephouse.com"))
expect(signature.time).to(equal(NSDate(timeIntervalSince1970: 1416186947)))
expect(signature.timeZone.abbreviation).to(equal("GMT-5"))
}
}
}
}
class CommitSpec: QuickSpec {
override func spec() {
describe("init(pointer:)") {
it("should initialize its properties") {
let repo = Fixtures.simpleRepository
let oid = OID(string: "dc220a3f0c22920dab86d4a8d3a3cb7e69d6205a")!
let commit = from_git_object(repo, oid) { Commit(pointer: $0) }
expect(commit.oid).to(equal(oid))
expect(commit.message).to(equal("Create a README\n"))
}
}
}