Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(lockfile): Make diffing/printing more reusable #13564

Merged
merged 5 commits into from
Mar 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/cargo/ops/cargo_generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,7 @@ fn print_lockfile_update(
&& candidate.minor == current.minor
&& candidate.patch == current.patch))
}
let possibilities = if let Some(query) = [diff.added.iter(), diff.unchanged.iter()]
.into_iter()
.flatten()
.next()
.filter(|s| s.source_id().is_registry())
{
let query =
crate::core::dependency::Dependency::parse(query.name(), None, query.source_id())?;
let possibilities = if let Some(query) = diff.alternatives_query() {
loop {
match registry.query_vec(&query, QueryKind::Exact) {
std::task::Poll::Ready(res) => {
Expand Down Expand Up @@ -320,6 +313,7 @@ fn fill_with_deps<'a>(
}
}

/// All resolved versions of a package name within a [`SourceId`]
#[derive(Default, Clone, Debug)]
pub struct PackageDiff {
removed: Vec<PackageId>,
Expand Down Expand Up @@ -405,4 +399,25 @@ impl PackageDiff {

changes.into_iter().map(|(_, v)| v).collect()
}

/// For querying [`PackageRegistry`] for alternative versions to report to the user
pub fn alternatives_query(&self) -> Option<crate::core::dependency::Dependency> {
let package_id = [
self.added.iter(),
self.unchanged.iter(),
self.removed.iter(),
weihanglo marked this conversation as resolved.
Show resolved Hide resolved
]
.into_iter()
.flatten()
.next()
// Limit to registry as that is the only source with meaningful alternative versions
.filter(|s| s.source_id().is_registry())?;
let query = crate::core::dependency::Dependency::parse(
package_id.name(),
None,
package_id.source_id(),
)
.expect("already a valid dependency");
Some(query)
}
}