Fix metadata::transform picking wrong package when multiple versions are in metadata
Diff
src/main.rs | 24 +++++++++++++++---------
src/metadata.rs | 10 ++++++----
2 files changed, 16 insertions(+), 18 deletions(-)
@@ -12,8 +12,8 @@
pub mod providers;
pub mod util;
use crate::cache::{Cache, ConcreteCache, Yoked};
use crate::{
cache::{Cache, ConcreteCache, Yoked},
config::Args,
metadata::{CargoConfig, CargoIndexCrateMetadata},
providers::{gitlab::Gitlab, PackageProvider, Release, User, UserProvider},
@@ -30,8 +30,8 @@
low_level::{HashOutput, PackFileEntry},
PktLine,
};
use semver::Version;
use std::{
borrow::Cow,
fmt::Write,
net::{SocketAddr, SocketAddrV6},
pin::Pin,
@@ -243,15 +243,20 @@
crate_version: &str,
do_as: &Arc<User>,
) -> anyhow::Result<Yoked<CargoIndexCrateMetadata<'static>>> {
let crate_v: Option<Version> = crate_version.parse().ok();
let crate_v = crate_v.as_ref();
if let Some(cache) = cache
.get::<CargoIndexCrateMetadata<'static>>(checksum)
.await?
{
debug!("Using metadata from cache");
return Ok(cache);
if crate_v.is_none() || crate_v == Some(&cache.get().vers) {
debug!(crate_name, crate_version, "Using metadata from cache");
return Ok(cache);
}
}
debug!("Fetching metadata from GitLab");
debug!(crate_name, crate_version, "Fetching metadata from GitLab");
let metadata = gitlab
@@ -261,7 +266,7 @@
let cksum = checksum.to_string();
let metadata = metadata::transform(metadata, crate_name, cksum)
let metadata = metadata::transform(metadata, crate_name, crate_v, cksum)
.ok_or_else(|| anyhow!("the supplied metadata.json did contain the released crate"))?;
@@ -642,11 +647,4 @@
}
res
}
#[derive(Hash, Debug, PartialEq, Eq)]
struct MetadataCacheKey<'a> {
checksum: Cow<'a, str>,
crate_name: Cow<'a, str>,
crate_version: Cow<'a, str>,
}
@@ -15,12 +15,12 @@
pub fn transform(
metadata: cargo_metadata::Metadata,
crate_name: &str,
crate_version: Option<&Version>,
cksum: String,
) -> Option<CargoIndexCrateMetadata<'static>> {
let package: Package = metadata
.packages
.into_iter()
.find(|v| v.name == crate_name)?;
let package: Package = metadata.packages.into_iter().find(|v| {
v.name == crate_name && (crate_version.is_none() || Some(&v.version) == crate_version)
})?;
Some(CargoIndexCrateMetadata {
name: Cow::Owned(package.name),
@@ -71,7 +71,7 @@
pub struct CargoIndexCrateMetadata<'a> {
#[serde(borrow)]
name: Cow<'a, str>,
vers: Version,
pub vers: Version,
#[serde(borrow)]
deps: Vec<CargoIndexCrateMetadataDependency<'a>>,
cksum: String,