Slide 18
Slide 18 text
git commit
func gitCommit(treeHash: String, parent: String?, message: String, author: 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
}