From a49414d1848c242bbeeace681d340b30bad2aae7 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Fri, 05 Jul 2024 14:34:25 +0100 Subject: [PATCH] Fix metadata::transform picking wrong package when multiple versions are in metadata --- src/main.rs | 24 +++++++++++++++--------- src/metadata.rs | 10 ++++++---- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2897836..81aa26b 100644 --- a/src/main.rs +++ a/src/main.rs @@ -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, ) -> anyhow::Result>> { + let crate_v: Option = crate_version.parse().ok(); + let crate_v = crate_v.as_ref(); + if let Some(cache) = cache .get::>(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"); // fetch metadata from the provider let metadata = gitlab @@ -261,7 +266,7 @@ // transform the `cargo metadata` output to the cargo index // format 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"))?; // cache the transformed value so the next user to pull it @@ -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>, } diff --git a/src/metadata.rs b/src/metadata.rs index 4d4d140..8a2b7b1 100644 --- a/src/metadata.rs +++ a/src/metadata.rs @@ -15,12 +15,12 @@ pub fn transform( metadata: cargo_metadata::Metadata, crate_name: &str, + crate_version: Option<&Version>, cksum: String, ) -> Option> { - 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>, cksum: String, -- rgit 0.1.3