fi lePath: String) throws -> String { let data = try Data(contentsOf: URL( fi leURLWithPath: fi lePath)) let header = "blob \(data.count)\0".data(using: .utf8)! let store = header + data // SHA-1 let sha1 = Insecure.SHA1.hash(data: store) let hash = sha1.map { String(format: "%02x", $0) }.joined() // zlibѹॖ let compressed = try (store as NSData).compressed(using: .zlib) // .git/objects/xx/xxxx... let dir = ".git/objects/" + String(hash.pre fi x(2)) let fi le = String(hash.dropFirst(2)) try FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true) try compressed.write(to: URL( fi leURLWithPath: "\(dir)/\( fi le)")) return hash }
String) throws -> String { var body = "tree \(treeHash)\n" if let p = parent { body += "parent \(p)\n" } let date = Int(Date().timeIntervalSince1970) body += "author \(author) \(date) +0000\n" body += "committer \(author) \(date) +0000\n\n" body += message + "\n" let data = body.data(using: .utf8)! let header = "commit \(data.count)\0".data(using: .utf8)! let store = header + data let sha1 = Insecure.SHA1.hash(data: store) let hash = sha1.map { String(format: "%02x", $0) }.joined() let compressed = try (store as NSData).compressed(using: .zlib) let dir = ".git/objects/" + String(hash.pre fi x(2)) let fi le = String(hash.dropFirst(2)) try FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true) try compressed.write(to: URL( fi leURLWithPath: "\(dir)/\( fi le)")) return hash }
let dir = ".git/objects/" + String(hash.pre fi x(2)) let fi le = String(hash.dropFirst(2)) try FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true) let src = "\(remotePath)/.git/objects/\(String(hash.pre fi x(2)))/\( fi le)" let dst = "\(dir)/\( fi le)" if !FileManager.default. fi leExists(atPath: dst) { try FileManager.default.copyItem(atPath: src, toPath: dst) } }
String { let dir = ".git/objects/" + String(hash.pre fi x(2)) let fi le = String(hash.dropFirst(2)) let compressed = try Data(contentsOf: URL( fi leURLWithPath: "\(dir)/\( fi le)")) // zlibల։ let decompressed = try (compressed as NSData).decompressed(using: .zlib) let str = String(data: decompressed as Data, encoding: .utf8)! // header ("blob 123\0" ͳͲ) Λ֎ͯ͠ฦ͢ if let range = str.range(of: "\0") { return String(str[range.upperBound...]) } return str }