in Sources/XCRemoteCache/Commands/Prepare/Prepare.swift [75:121]
func prepare() throws -> PrepareResult {
do {
guard fileAccessor.fileExists(atPath: PhaseCacheModeController.xcodeSelectLink.path) else {
throw PrepareError.missingXcodeSelectDirectory
}
let commonSha: String
do {
commonSha = try gitClient.getCommonPrimarySha()
} catch let GitClientError.noCommonShaWithPrimaryRepo(remoteName, error) {
guard context.gracefullyHandleMissingCommonSha else {
throw GitClientError.noCommonShaWithPrimaryRepo(remoteName: remoteName, error: error)
}
infoLog("Cannot find a common sha with the primary branch: \(error). Gracefully disabling remote cache")
try disable()
return .failed
}
if context.offline {
// Optimistically take first common sha
return try enableCommit(sha: commonSha, age: 0)
}
// Remove old artifacts from local cache
cacheInvalidator.invalidateArtifacts()
// calling `git` is expensive, so optimistically tring the common sha first
if try isArtifactAvailable(for: commonSha) {
return try enableCommit(sha: commonSha, age: 0)
}
// Find a list of all potential commits that may have artifacts that can be used
let allCommonCommits = try gitClient.getPreviousCommits(starting: commonSha, maximum: context.maximumSha)
// First commit was checked already
for (index, sha) in allCommonCommits.dropFirst().enumerated() {
// Check if the marker file for a `sha` commit is available on the remote cache server
if try isArtifactAvailable(for: sha) {
// adding 1 because current HEAD was already checked
return try enableCommit(sha: sha, age: index + 1)
}
}
infoLog("No artifacts available")
try disable()
} catch {
try disable()
throw error
}
return .failed
}