Make --upgrade imply --refresh (#5943)

## Summary

I think this seems reasonable... Otherwise, we might not go back to PyPI
to revalidate the list of available versions despite the user passing
`--upgrade`.
This commit is contained in:
Charlie Marsh 2024-08-08 20:11:31 -04:00 committed by GitHub
parent 3701b60f61
commit fd1d508108
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 134 additions and 101 deletions

View file

@ -3,7 +3,7 @@ use pep508_rs::PackageName;
use pypi_types::Requirement;
use rustc_hash::FxHashMap;
use uv_cache::Refresh;
use uv_cache::{Refresh, Timestamp};
/// Whether to reinstall packages.
#[derive(Debug, Default, Clone)]
@ -44,30 +44,15 @@ impl Reinstall {
pub fn is_all(&self) -> bool {
matches!(self, Self::All)
}
}
/// Create a [`Refresh`] policy by integrating the [`Reinstall`] policy.
pub fn to_refresh(self, refresh: Refresh) -> Refresh {
match (self, refresh) {
// If the policy is `None`, return the existing refresh policy.
(Self::None, Refresh::None(timestamp)) => Refresh::None(timestamp),
(Self::None, Refresh::All(timestamp)) => Refresh::All(timestamp),
(Self::None, Refresh::Packages(packages, timestamp)) => {
Refresh::Packages(packages, timestamp)
}
// If the policy is `All`, refresh all packages.
(Self::All, Refresh::None(timestamp)) => Refresh::All(timestamp),
(Self::All, Refresh::All(timestamp)) => Refresh::All(timestamp),
(Self::All, Refresh::Packages(_packages, timestamp)) => Refresh::All(timestamp),
// If the policy is `Packages`, take the "max" of the two policies.
(Self::Packages(packages), Refresh::None(timestamp)) => {
Refresh::Packages(packages, timestamp)
}
(Self::Packages(_packages), Refresh::All(timestamp)) => Refresh::All(timestamp),
(Self::Packages(packages1), Refresh::Packages(packages2, timestamp)) => {
Refresh::Packages(packages1.into_iter().chain(packages2).collect(), timestamp)
}
/// Create a [`Refresh`] policy by integrating the [`Reinstall`] policy.
impl From<Reinstall> for Refresh {
fn from(value: Reinstall) -> Self {
match value {
Reinstall::None => Self::None(Timestamp::now()),
Reinstall::All => Self::All(Timestamp::now()),
Reinstall::Packages(packages) => Self::Packages(packages, Timestamp::now()),
}
}
}
@ -144,3 +129,16 @@ impl Upgrade {
}
}
}
/// Create a [`Refresh`] policy by integrating the [`Upgrade`] policy.
impl From<Upgrade> for Refresh {
fn from(value: Upgrade) -> Self {
match value {
Upgrade::None => Self::None(Timestamp::now()),
Upgrade::All => Self::All(Timestamp::now()),
Upgrade::Packages(packages) => {
Self::Packages(packages.into_keys().collect::<Vec<_>>(), Timestamp::now())
}
}
}
}