Skip to content

Commit c92682c

Browse files
committed
Adopt sha256Checksum property in ManifestLoader
1 parent 85fb8da commit c92682c

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
557557
key: ManifestCacheKey,
558558
cache: PersistentCacheProtocol
559559
) throws -> ManifestParseResult {
560-
let keyHash = try key.computeHash()
560+
let keyHash = ByteString(encodingAsUTF8: key.sha256Checksum)
561561
let cacheHit = try keyHash.withData {
562562
try cache.get(key: $0)
563563
}.flatMap {
@@ -588,6 +588,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
588588
let toolsVersion: ToolsVersion
589589
let env: [String: String]
590590
let swiftpmVersion: String
591+
let sha256Checksum: String
591592

592593
init (packageIdentity: PackageIdentity,
593594
manifestPath: AbsolutePath,
@@ -596,37 +597,38 @@ public final class ManifestLoader: ManifestLoaderProtocol {
596597
swiftpmVersion: String,
597598
fileSystem: FileSystem
598599
) throws {
600+
let manifestContents = try fileSystem.readFileContents(manifestPath).contents
601+
let sha256Checksum = try Self.computeSHA256Checksum(packageIdentity: packageIdentity, manifestContents: manifestContents, toolsVersion: toolsVersion, env: env, swiftpmVersion: swiftpmVersion)
602+
599603
self.packageIdentity = packageIdentity
600604
self.manifestPath = manifestPath
601-
self.manifestContents = try fileSystem.readFileContents(manifestPath).contents
605+
self.manifestContents = manifestContents
602606
self.toolsVersion = toolsVersion
603607
self.env = env
604608
self.swiftpmVersion = swiftpmVersion
609+
self.sha256Checksum = sha256Checksum
605610
}
606611

607-
// TODO: is there a way to avoid the dual hashing?
608612
func hash(into hasher: inout Hasher) {
609-
hasher.combine(self.packageIdentity)
610-
hasher.combine(self.manifestContents)
611-
hasher.combine(self.toolsVersion.description)
612-
for key in self.env.keys.sorted(by: >) {
613-
hasher.combine(key)
614-
hasher.combine(env[key]!)
615-
}
616-
hasher.combine(self.swiftpmVersion)
613+
hasher.combine(self.sha256Checksum)
617614
}
618615

619-
// TODO: is there a way to avoid the dual hashing?
620-
func computeHash() throws -> ByteString {
616+
private static func computeSHA256Checksum(
617+
packageIdentity: PackageIdentity,
618+
manifestContents: [UInt8],
619+
toolsVersion: ToolsVersion,
620+
env: [String: String],
621+
swiftpmVersion: String
622+
) throws -> String {
621623
let stream = BufferedOutputByteStream()
622-
stream <<< self.packageIdentity
623-
stream <<< self.manifestContents
624-
stream <<< self.toolsVersion.description
625-
for key in self.env.keys.sorted(by: >) {
624+
stream <<< packageIdentity
625+
stream <<< manifestContents
626+
stream <<< toolsVersion.description
627+
for key in env.keys.sorted(by: >) {
626628
stream <<< key <<< env[key]!
627629
}
628-
stream <<< self.swiftpmVersion
629-
return SHA256().hash(stream.bytes)
630+
stream <<< swiftpmVersion
631+
return stream.bytes.sha256Checksum
630632
}
631633
}
632634

0 commit comments

Comments
 (0)