Skip to content

Commit e24222e

Browse files
committed
only wright contents hash
1 parent 898e508 commit e24222e

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/cargo/sources/registry/remote.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +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 checks the `index_version` in two parts the cache file is considered valid if `index_version`
167-
// ends with the hash of the HEAD commit OR if it starts with the hash of the file's contents.
168-
// Cargo writes the cached files with `index_version` = `git_file_hash` + ":" + `git_commit_hash`.
166+
// However if an old cargo has written such a file we still know how to read it, as long as we check for that hash value.
167+
//
168+
// Cargo now uses a hash of the file's contents as provided by git.
169169
fn load(
170170
&mut self,
171171
_root: &Path,
@@ -177,10 +177,9 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
177177
}
178178
// Check if the cache is valid.
179179
let git_commit_hash = self.current_version();
180-
if let (Some(i), Some(c)) = (index_version, git_commit_hash) {
181-
if i.ends_with(c.as_str()) {
182-
return Poll::Ready(Ok(LoadResponse::CacheValid));
183-
}
180+
if index_version.is_some() && index_version == git_commit_hash.as_deref() {
181+
// This file was written by an old version of cargo, but it is still up-to-date.
182+
return Poll::Ready(Ok(LoadResponse::CacheValid));
184183
}
185184
// Note that the index calls this method and the filesystem is locked
186185
// in the index, so we don't need to worry about an `update_index`
@@ -189,18 +188,16 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
189188
registry: &RemoteRegistry<'_>,
190189
path: &Path,
191190
index_version: Option<&str>,
192-
git_commit_hash: Option<&str>,
193191
) -> CargoResult<LoadResponse> {
194192
let repo = registry.repo()?;
195193
let tree = registry.tree()?;
196194
let entry = tree.get_path(path);
197195
let entry = entry?;
198-
let git_file_hash = entry.id().to_string();
196+
let git_file_hash = Some(entry.id().to_string());
199197

200-
if let Some(i) = index_version {
201-
if i.starts_with(git_file_hash.as_str()) {
202-
return Ok(LoadResponse::CacheValid);
203-
}
198+
// Check if the cache is valid.
199+
if index_version.is_some() && index_version == git_file_hash.as_deref() {
200+
return Ok(LoadResponse::CacheValid);
204201
}
205202

206203
let object = entry.to_object(repo)?;
@@ -211,11 +208,11 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
211208

212209
Ok(LoadResponse::Data {
213210
raw_data: blob.content().to_vec(),
214-
index_version: git_commit_hash.map(|c| git_file_hash + c),
211+
index_version: git_file_hash,
215212
})
216213
}
217214

218-
match load_helper(&self, path, index_version, git_commit_hash.as_deref()) {
215+
match load_helper(&self, path, index_version) {
219216
Ok(result) => Poll::Ready(Ok(result)),
220217
Err(_) if !self.updated => {
221218
// If git returns an error and we haven't updated the repo, return

0 commit comments

Comments
 (0)