@@ -45,7 +45,7 @@ public final class Repository {
4545 public static func clone( from remoteURL: URL ,
4646 to localURL: URL ,
4747 configuration: Clone . Configuration = . default) throws -> Repository {
48- var pointer : OpaquePointer ? = nil
48+ var pointer : OpaquePointer ?
4949
5050 var options = configuration. rawValue
5151 let remoteURLString = remoteURL. isFileURL ? remoteURL. path : remoteURL. absoluteString
@@ -58,6 +58,15 @@ public final class Repository {
5858
5959 // MARK: -
6060
61+ public subscript< T: Reference > ( _ shorthand: String ) -> T ? {
62+ var pointer : OpaquePointer ?
63+ guard case . success = result ( of: { git_reference_dwim ( & pointer, self . pointer, shorthand) } ) ,
64+ pointer != nil
65+ else { return nil }
66+
67+ return T ( pointer!)
68+ }
69+
6170 /**
6271 The repository's working directory.
6372
@@ -83,7 +92,8 @@ public final class Repository {
8392 public lazy var index : Index ? = {
8493 var pointer : OpaquePointer ?
8594 guard case . success = result ( of: { git_repository_index ( & pointer, self . pointer) } ) ,
86- pointer != nil else { return nil }
95+ pointer != nil
96+ else { return nil }
8797 let index = Index ( pointer!)
8898 index. managed = true
8999
@@ -124,14 +134,26 @@ public final class Repository {
124134 - Throws: An error if no object exists for the
125135 - Returns: The corresponding object.
126136 */
127- public func lookup< T: Object > ( _ id: Object . ID ) throws -> T ? {
128- var result : OpaquePointer ? = nil
137+ func lookup< T: Object > ( type : T . Type , with id: Object . ID ) throws -> T ? {
138+ var pointer : OpaquePointer ?
129139 var oid = id. rawValue
130- try attempt { git_object_lookup ( & result , self . pointer, & oid, T . type) }
140+ try attempt { git_object_lookup ( & pointer , self . pointer, & oid, T . type) }
131141// defer { git_object_free(pointer) }
132- guard let pointer = result else { return nil }
133142
134- return T ( pointer)
143+ switch type {
144+ case is Blob . Type ,
145+ is Commit . Type ,
146+ is Tree . Type :
147+ return T ( pointer!)
148+ default :
149+ return Object . type ( of: pointer) ? . init ( pointer!) as? T
150+ }
151+ }
152+
153+ func lookup< T: Reference > ( type: T . Type , named name: String ) throws -> T ? {
154+ var pointer : OpaquePointer ?
155+ try attempt { git_reference_lookup ( & pointer, self . pointer, name) }
156+ return T ( pointer!)
135157 }
136158
137159 /**
@@ -170,16 +192,16 @@ public final class Repository {
170192
171193 @discardableResult
172194 public func createCommit( message: String , author: Signature ? = nil , committer: Signature ? = nil ) throws -> Commit {
173- let tree = try lookup ( try Object . ID { oid in
195+ let tree = try lookup ( type : Tree . self , with : try Object . ID { oid in
174196 try attempt { git_index_write_tree ( oid, index? . pointer) }
175- } ) as Tree ?
197+ } )
176198
177199 var committer = ( try committer ?? author ?? Signature . default ( for: self ) ) . rawValue
178200 var author = ( try author ?? Signature . default ( for: self ) ) . rawValue
179201
180202 var parents = [ head? . commit] . compactMap { $0? . pointer } as [ OpaquePointer ? ]
181203
182- return try lookup ( try Object . ID { oid in
204+ return try lookup ( type : Commit . self , with : try Object . ID { oid in
183205 try attempt { git_commit_create ( oid, pointer, " HEAD " , & author, & committer, " UTF-8 " , message, tree? . pointer, parents. count, & parents) }
184206 } ) !
185207 }
0 commit comments