Skip to content

Commit a986a18

Browse files
committed
Adopt sha256Checksum property in ManifestLoader
1 parent f47aad0 commit a986a18

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
664664
let toolsVersion: ToolsVersion
665665
let env: [String: String]
666666
let swiftpmVersion: String
667+
let sha256Checksum: String
667668

668669
init (packageIdentity: PackageIdentity,
669670
manifestPath: AbsolutePath,
@@ -672,37 +673,38 @@ public final class ManifestLoader: ManifestLoaderProtocol {
672673
swiftpmVersion: String,
673674
fileSystem: FileSystem
674675
) throws {
676+
let manifestContents = try fileSystem.readFileContents(manifestPath).contents
677+
let sha256Checksum = try Self.computeSHA256Checksum(packageIdentity: packageIdentity, manifestContents: manifestContents, toolsVersion: toolsVersion, env: env, swiftpmVersion: swiftpmVersion)
678+
675679
self.packageIdentity = packageIdentity
676680
self.manifestPath = manifestPath
677-
self.manifestContents = try fileSystem.readFileContents(manifestPath).contents
681+
self.manifestContents = manifestContents
678682
self.toolsVersion = toolsVersion
679683
self.env = env
680684
self.swiftpmVersion = swiftpmVersion
685+
self.sha256Checksum = sha256Checksum
681686
}
682687

683-
// TODO: is there a way to avoid the dual hashing?
684688
func hash(into hasher: inout Hasher) {
685-
hasher.combine(self.packageIdentity)
686-
hasher.combine(self.manifestContents)
687-
hasher.combine(self.toolsVersion.description)
688-
for key in self.env.keys.sorted(by: >) {
689-
hasher.combine(key)
690-
hasher.combine(env[key]!) // forced unwrap safe
691-
}
692-
hasher.combine(self.swiftpmVersion)
689+
hasher.combine(self.sha256Checksum)
693690
}
694691

695-
// TODO: is there a way to avoid the dual hashing?
696-
func computeHash() throws -> ByteString {
692+
private static func computeSHA256Checksum(
693+
packageIdentity: PackageIdentity,
694+
manifestContents: [UInt8],
695+
toolsVersion: ToolsVersion,
696+
env: [String: String],
697+
swiftpmVersion: String
698+
) throws -> String {
697699
let stream = BufferedOutputByteStream()
698-
stream <<< self.packageIdentity
699-
stream <<< self.manifestContents
700-
stream <<< self.toolsVersion.description
701-
for key in self.env.keys.sorted(by: >) {
702-
stream <<< key <<< env[key]! // forced unwrap safe
700+
stream <<< packageIdentity
701+
stream <<< manifestContents
702+
stream <<< toolsVersion.description
703+
for key in env.keys.sorted(by: >) {
704+
stream <<< key <<< env[key]! // forced unwrap safe
703705
}
704-
stream <<< self.swiftpmVersion
705-
return SHA256().hash(stream.bytes)
706+
stream <<< swiftpmVersion
707+
return stream.bytes.sha256Checksum
706708
}
707709
}
708710

@@ -849,7 +851,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
849851
if compilerResult.exitStatus != .terminated(code: 0) {
850852
return
851853
}
852-
854+
853855
// Pass an open file descriptor of a file to which the JSON representation of the manifest will be written.
854856
let jsonOutputFile = tmpDir.appending(component: "\(packageIdentity)-output.json")
855857
guard let jsonOutputFileDesc = fopen(jsonOutputFile.pathString, "w") else {
@@ -1135,12 +1137,11 @@ private final class SQLiteManifestCache: Closable {
11351137
}
11361138

11371139
func put(key: ManifestLoader.ManifestCacheKey, manifest: ManifestLoader.ManifestParseResult) throws {
1138-
let query = "INSERT OR IGNORE INTO MANIFEST_CACHE VALUES (?, ?);"
1140+
let query = "INSERT OR IGNORE INTO MANIFEST_CACHE VALUES (?, ?);"
11391141
try self.executeStatement(query) { statement -> Void in
1140-
let keyHash = try key.computeHash()
11411142
let data = try self.jsonEncoder.encode(manifest)
11421143
let bindings: [SQLite.SQLiteValue] = [
1143-
.string(keyHash.hexadecimalRepresentation),
1144+
.string(key.sha256Checksum),
11441145
.blob(data),
11451146
]
11461147
try statement.bind(bindings)
@@ -1151,8 +1152,7 @@ private final class SQLiteManifestCache: Closable {
11511152
func get(key: ManifestLoader.ManifestCacheKey) throws -> ManifestLoader.ManifestParseResult? {
11521153
let query = "SELECT value FROM MANIFEST_CACHE WHERE key == ? LIMIT 1;"
11531154
return try self.executeStatement(query) { statement -> ManifestLoader.ManifestParseResult? in
1154-
let keyHash = try key.computeHash()
1155-
try statement.bind([.string(keyHash.hexadecimalRepresentation)])
1155+
try statement.bind([.string(key.sha256Checksum)])
11561156
let data = try statement.step()?.blob(at: 0)
11571157
return try data.flatMap {
11581158
try self.jsonDecoder.decode(ManifestLoader.ManifestParseResult.self, from: $0)

0 commit comments

Comments
 (0)