Skip to content

Commit 8cfcacf

Browse files
committed
WIP: Use repository cache without update if offline
This is a very crude way of doing it, but it works. I think we need to figure out three things to make this prod worthy: - determine that we failed to update because of missing network connectivity - check that the cache is "valid" -- ideally this would involve checking whether it can fulfill the dependency requirements, but I don't think that is really possible. We should validate that there's an actual clone present, though - how to communicate the fact that we chose an offline version of the cache, maybe simply a warning diagnostic?
1 parent e5266bf commit 8cfcacf

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

Sources/SourceControl/RepositoryManager.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,21 @@ public class RepositoryManager: Cancellable {
325325
}
326326
}
327327
} catch {
328-
cacheUsed = false
329-
// Fetch without populating the cache in the case of an error.
330-
observabilityScope.emit(warning: "skipping cache due to an error: \(error)")
331-
// it is possible that we already created the directory from failed attempts, so clear leftover data if present.
332-
try? self.fileSystem.removeFileTree(repositoryPath)
333-
try self.provider.fetch(repository: handle.repository, to: repositoryPath, progressHandler: updateFetchProgress(progress:))
328+
// TODO: Better detection of whether the error was due to be offline
329+
if "\(error)".contains("Could not resolve host") {
330+
// TODO: check validity of the cache
331+
cacheUsed = true
332+
// Copy the repository from the cache into the repository path.
333+
try self.fileSystem.createDirectory(repositoryPath.parentDirectory, recursive: true)
334+
try self.provider.copy(from: cachedRepositoryPath, to: repositoryPath)
335+
} else {
336+
cacheUsed = false
337+
// Fetch without populating the cache in the case of an error.
338+
observabilityScope.emit(warning: "skipping cache due to an error: \(error)")
339+
// it is possible that we already created the directory from failed attempts, so clear leftover data if present.
340+
try? self.fileSystem.removeFileTree(repositoryPath)
341+
try self.provider.fetch(repository: handle.repository, to: repositoryPath, progressHandler: updateFetchProgress(progress:))
342+
}
334343
}
335344
} else {
336345
// it is possible that we already created the directory from failed attempts, so clear leftover data if present.

0 commit comments

Comments
 (0)