From d0be6179b952eabc1c30b682fc557e13a95f18f5 Mon Sep 17 00:00:00 2001 From: Matt Diephouse Date: Mon, 8 Dec 2014 23:28:34 -0500 Subject: [PATCH] Add a Blob struct --- SwiftGit2/Objects.swift | 28 +++++++++++++++++ SwiftGit2/Repository.swift | 22 +++++++++++++ SwiftGit2Tests/ObjectsSpec.swift | 49 +++++++++++++++++++++++++++++ SwiftGit2Tests/RepositorySpec.swift | 29 +++++++++++++++++ 4 files changed, 128 insertions(+) diff --git a/SwiftGit2/Objects.swift b/SwiftGit2/Objects.swift index 2c4461e..010ae04 100644 --- a/SwiftGit2/Objects.swift +++ b/SwiftGit2/Objects.swift @@ -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 +} diff --git a/SwiftGit2/Repository.swift b/SwiftGit2/Repository.swift index c5dd492..717a849 100644 --- a/SwiftGit2/Repository.swift +++ b/SwiftGit2/Repository.swift @@ -57,6 +57,28 @@ final public class Repository { public let directoryURL: NSURL? // 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 { + let pointer = UnsafeMutablePointer.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. /// diff --git a/SwiftGit2Tests/ObjectsSpec.swift b/SwiftGit2Tests/ObjectsSpec.swift index 2e95fa2..55c0fa5 100644 --- a/SwiftGit2Tests/ObjectsSpec.swift +++ b/SwiftGit2Tests/ObjectsSpec.swift @@ -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)) + } + } + } +} diff --git a/SwiftGit2Tests/RepositorySpec.swift b/SwiftGit2Tests/RepositorySpec.swift index 58ba467..136f578 100644 --- a/SwiftGit2Tests/RepositorySpec.swift +++ b/SwiftGit2Tests/RepositorySpec.swift @@ -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