Skip to content

Commit 898e508

Browse files
committed
Cache based on contents hash
1 parent 04a4081 commit 898e508

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/cargo/sources/registry/remote.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,9 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
163163
// Older versions of Cargo used the single value of the hash of the HEAD commit as a `index_version`.
164164
// This is technically correct but a little too conservative. If a new commit is fetched all cached
165165
// files need to be regenerated even if a particular file was not changed.
166-
// Cargo now reads the `index_version` in two parts the cache file is considered valid if `index_version`
166+
// Cargo now checks the `index_version` in two parts the cache file is considered valid if `index_version`
167167
// ends with the hash of the HEAD commit OR if it starts with the hash of the file's contents.
168-
// In the future cargo can write cached files with `index_version` = `git_file_hash + ":" + `git_commit_hash`,
169-
// but for now it still uses `git_commit_hash` to be compatible with older Cargoes.
168+
// Cargo writes the cached files with `index_version` = `git_file_hash` + ":" + `git_commit_hash`.
170169
fn load(
171170
&mut self,
172171
_root: &Path,
@@ -178,7 +177,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
178177
}
179178
// Check if the cache is valid.
180179
let git_commit_hash = self.current_version();
181-
if let (Some(c), Some(i)) = (git_commit_hash, index_version) {
180+
if let (Some(i), Some(c)) = (index_version, git_commit_hash) {
182181
if i.ends_with(c.as_str()) {
183182
return Poll::Ready(Ok(LoadResponse::CacheValid));
184183
}
@@ -212,9 +211,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
212211

213212
Ok(LoadResponse::Data {
214213
raw_data: blob.content().to_vec(),
215-
index_version: git_commit_hash.map(String::from),
216-
// TODO: When the reading code has been stable for long enough (Say 8/2022)
217-
// change to `git_file_hash + ":" + git_commit_hash`
214+
index_version: git_commit_hash.map(|c| git_file_hash + c),
218215
})
219216
}
220217

0 commit comments

Comments
 (0)