mirror of
https://github.com/gosticks/SwiftGit2.git
synced 2025-10-16 11:55:34 +00:00
Add a Blob struct
This commit is contained in:
parent
9b3cad3b73
commit
d0be6179b9
@ -223,3 +223,31 @@ extension Tree: Hashable {
|
||||
public func == (lhs: Tree, rhs: Tree) -> Bool {
|
||||
return lhs.oid == rhs.oid
|
||||
}
|
||||
|
||||
/// A git blob.
|
||||
public struct Blob: Object {
|
||||
/// The OID of the blob.
|
||||
public let oid: OID
|
||||
|
||||
/// The contents of the blob.
|
||||
public let data: NSData
|
||||
|
||||
/// Create an instance with a libgit2 `git_blob`.
|
||||
public init(pointer: COpaquePointer) {
|
||||
oid = OID(oid: git_object_id(pointer).memory)
|
||||
|
||||
// Swift doesn't get the types right without `Int(Int64(...))` :(
|
||||
let length = Int(Int64(git_blob_rawsize(pointer).value))
|
||||
data = NSData(bytes: git_blob_rawcontent(pointer), length: length)
|
||||
}
|
||||
}
|
||||
|
||||
extension Blob: Hashable {
|
||||
public var hashValue: Int {
|
||||
return oid.hashValue
|
||||
}
|
||||
}
|
||||
|
||||
public func == (lhs: Blob, rhs: Blob) -> Bool {
|
||||
return lhs.oid == rhs.oid
|
||||
}
|
||||
|
||||
@ -58,6 +58,28 @@ final public class Repository {
|
||||
|
||||
// MARK: - Object Lookups
|
||||
|
||||
/// Loads the blob with the given OID.
|
||||
///
|
||||
/// 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> {
|
||||
let pointer = UnsafeMutablePointer<COpaquePointer>.alloc(1)
|
||||
let repository = self.pointer
|
||||
var oid = oid.oid
|
||||
let result = git_object_lookup(pointer, repository, &oid, GIT_OBJ_BLOB)
|
||||
|
||||
if result < GIT_OK.value {
|
||||
pointer.dealloc(1)
|
||||
return failure()
|
||||
}
|
||||
|
||||
let commit = Blob(pointer: pointer.memory)
|
||||
git_object_free(pointer.memory)
|
||||
pointer.dealloc(1)
|
||||
return success(commit)
|
||||
}
|
||||
|
||||
/// Loads the commit with the given OID.
|
||||
///
|
||||
/// oid - The OID of the commit to look up.
|
||||
|
||||
@ -279,3 +279,52 @@ class TreeSpec: QuickSpec {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BlobSpec: QuickSpec {
|
||||
override func spec() {
|
||||
describe("init(pointer:)") {
|
||||
it("should initialize its properties") {
|
||||
let repo = Fixtures.simpleRepository
|
||||
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
|
||||
|
||||
let blob = from_git_object(repo, oid) { Blob(pointer: $0) }
|
||||
let contents = "# Simple Repository\nA simple repository used for testing SwiftGit2.\n\n## Branches\n\n- master\n\n"
|
||||
let data = (contents as NSString).dataUsingEncoding(NSUTF8StringEncoding)!
|
||||
expect(blob.oid).to(equal(oid))
|
||||
expect(blob.data).to(equal(data))
|
||||
}
|
||||
}
|
||||
|
||||
describe("==") {
|
||||
it("should be true with equal objects") {
|
||||
let repo = Fixtures.simpleRepository
|
||||
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
|
||||
|
||||
let blob1 = from_git_object(repo, oid) { Blob(pointer: $0) }
|
||||
let blob2 = blob1
|
||||
expect(blob1).to(equal(blob2))
|
||||
}
|
||||
|
||||
it("should be false with unequal objects") {
|
||||
let repo = Fixtures.simpleRepository
|
||||
let oid1 = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
|
||||
let oid2 = OID(string: "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391")!
|
||||
|
||||
let blob1 = from_git_object(repo, oid1) { Blob(pointer: $0) }
|
||||
let blob2 = from_git_object(repo, oid2) { Blob(pointer: $0) }
|
||||
expect(blob1).notTo(equal(blob2))
|
||||
}
|
||||
}
|
||||
|
||||
describe("hashValue") {
|
||||
it("should be equal with equal objects") {
|
||||
let repo = Fixtures.simpleRepository
|
||||
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
|
||||
|
||||
let blob1 = from_git_object(repo, oid) { Blob(pointer: $0) }
|
||||
let blob2 = blob1
|
||||
expect(blob1.hashValue).to(equal(blob2.hashValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,6 +26,35 @@ class RepositorySpec: QuickSpec {
|
||||
}
|
||||
}
|
||||
|
||||
describe("-blobWithOID()") {
|
||||
it("should return the commit if it exists") {
|
||||
let repo = Fixtures.simpleRepository
|
||||
let oid = OID(string: "41078396f5187daed5f673e4a13b185bbad71fba")!
|
||||
|
||||
let result = repo.blobWithOID(oid)
|
||||
let blob = result.value()
|
||||
expect(blob).notTo(beNil())
|
||||
expect(blob?.oid).to(equal(oid))
|
||||
}
|
||||
|
||||
it("should error if the blob doesn't exist") {
|
||||
let repo = Fixtures.simpleRepository
|
||||
let oid = OID(string: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")!
|
||||
|
||||
let result = repo.blobWithOID(oid)
|
||||
expect(result.error()).notTo(beNil())
|
||||
}
|
||||
|
||||
it("should error if the oid doesn't point to a blob") {
|
||||
let repo = Fixtures.simpleRepository
|
||||
// This is a tree in the repository
|
||||
let oid = OID(string: "f93e3a1a1525fb5b91020da86e44810c87a2d7bc")!
|
||||
|
||||
let result = repo.blobWithOID(oid)
|
||||
expect(result.error()).notTo(beNil())
|
||||
}
|
||||
}
|
||||
|
||||
describe("-commitWithOID()") {
|
||||
it("should return the commit if it exists") {
|
||||
let repo = Fixtures.simpleRepository
|
||||
|
||||
Loading…
Reference in New Issue
Block a user