mirror of
https://github.com/astral-sh/uv.git
synced 2025-08-04 10:58:28 +00:00
Refactor distribution types to adhere to a clear hierarchy (#369)
## Summary This PR refactors our `RemoteDistribution` type such that it now follows a clear hierarchy that matches the actual variants, and encodes the differences between source and built distributions: ```rust pub enum Distribution { Built(BuiltDistribution), Source(SourceDistribution), } pub enum BuiltDistribution { Registry(RegistryBuiltDistribution), DirectUrl(DirectUrlBuiltDistribution), } pub enum SourceDistribution { Registry(RegistrySourceDistribution), DirectUrl(DirectUrlSourceDistribution), Git(GitSourceDistribution), } /// A built distribution (wheel) that exists in a registry, like `PyPI`. pub struct RegistryBuiltDistribution { pub name: PackageName, pub version: Version, pub file: File, } /// A built distribution (wheel) that exists at an arbitrary URL. pub struct DirectUrlBuiltDistribution { pub name: PackageName, pub url: Url, } /// A source distribution that exists in a registry, like `PyPI`. pub struct RegistrySourceDistribution { pub name: PackageName, pub version: Version, pub file: File, } /// A source distribution that exists at an arbitrary URL. pub struct DirectUrlSourceDistribution { pub name: PackageName, pub url: Url, } /// A source distribution that exists in a Git repository. pub struct GitSourceDistribution { pub name: PackageName, pub url: Url, } ``` Most of the PR just stems downstream from this change. There are no behavioral changes, so I'm largely relying on lint, tests, and the compiler for correctness.
This commit is contained in:
parent
33c0901a28
commit
a148f9d0be
36 changed files with 1729 additions and 1098 deletions
|
@ -7,18 +7,18 @@ mod git;
|
|||
mod source;
|
||||
mod util;
|
||||
|
||||
/// A reference to a Git repository.
|
||||
/// A URL reference to a Git repository.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Git {
|
||||
pub struct GitUrl {
|
||||
/// The URL of the Git repository, with any query parameters and fragments removed.
|
||||
url: Url,
|
||||
repository: Url,
|
||||
/// The reference to the commit to use, which could be a branch, tag or revision.
|
||||
reference: GitReference,
|
||||
/// The precise commit to use, if known.
|
||||
precise: Option<git2::Oid>,
|
||||
}
|
||||
|
||||
impl Git {
|
||||
impl GitUrl {
|
||||
#[must_use]
|
||||
pub(crate) fn with_precise(mut self, precise: git2::Oid) -> Self {
|
||||
self.precise = Some(precise);
|
||||
|
@ -26,8 +26,8 @@ impl Git {
|
|||
}
|
||||
|
||||
/// Return the [`Url`] of the Git repository.
|
||||
pub fn url(&self) -> &Url {
|
||||
&self.url
|
||||
pub fn repository(&self) -> &Url {
|
||||
&self.repository
|
||||
}
|
||||
|
||||
/// Return the reference to the commit to use, which could be a branch, tag or revision.
|
||||
|
@ -49,10 +49,10 @@ impl Git {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Url> for Git {
|
||||
impl TryFrom<Url> for GitUrl {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
/// Initialize a [`Git`] source from a URL.
|
||||
/// Initialize a [`GitUrl`] source from a URL.
|
||||
fn try_from(mut url: Url) -> Result<Self, Self::Error> {
|
||||
// Remove any query parameters and fragments.
|
||||
url.set_fragment(None);
|
||||
|
@ -72,16 +72,16 @@ impl TryFrom<Url> for Git {
|
|||
};
|
||||
|
||||
Ok(Self {
|
||||
url,
|
||||
repository: url,
|
||||
reference,
|
||||
precise,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Git> for Url {
|
||||
fn from(git: Git) -> Self {
|
||||
let mut url = git.url;
|
||||
impl From<GitUrl> for Url {
|
||||
fn from(git: GitUrl) -> Self {
|
||||
let mut url = git.repository;
|
||||
|
||||
// If we have a precise commit, add `@` and the commit hash to the URL.
|
||||
if let Some(precise) = git.precise {
|
||||
|
@ -105,9 +105,9 @@ impl From<Git> for Url {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Git {
|
||||
impl std::fmt::Display for GitUrl {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.url)
|
||||
write!(f, "{}", self.repository)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,12 +11,12 @@ use url::Url;
|
|||
use puffin_cache::{digest, RepositoryUrl};
|
||||
|
||||
use crate::git::GitRemote;
|
||||
use crate::{FetchStrategy, Git};
|
||||
use crate::{FetchStrategy, GitUrl};
|
||||
|
||||
/// A remote Git source that can be checked out locally.
|
||||
pub struct GitSource {
|
||||
/// The Git reference from the manifest file.
|
||||
git: Git,
|
||||
git: GitUrl,
|
||||
/// The HTTP client to use for fetching.
|
||||
client: Client,
|
||||
/// The fetch strategy to use when cloning.
|
||||
|
@ -29,7 +29,7 @@ pub struct GitSource {
|
|||
|
||||
impl GitSource {
|
||||
/// Initialize a new Git source.
|
||||
pub fn new(git: Git, cache: impl Into<PathBuf>) -> Self {
|
||||
pub fn new(git: GitUrl, cache: impl Into<PathBuf>) -> Self {
|
||||
Self {
|
||||
git,
|
||||
client: Client::new(),
|
||||
|
@ -51,10 +51,10 @@ impl GitSource {
|
|||
/// Fetch the underlying Git repository at the given revision.
|
||||
pub fn fetch(self) -> Result<Fetch> {
|
||||
// The path to the repo, within the Git database.
|
||||
let ident = digest(&RepositoryUrl::new(&self.git.url));
|
||||
let ident = digest(&RepositoryUrl::new(&self.git.repository));
|
||||
let db_path = self.cache.join("db").join(&ident);
|
||||
|
||||
let remote = GitRemote::new(&self.git.url);
|
||||
let remote = GitRemote::new(&self.git.repository);
|
||||
let (db, actual_rev, task) = match (self.git.precise, remote.db_at(&db_path).ok()) {
|
||||
// If we have a locked revision, and we have a preexisting database
|
||||
// which has that revision, then no update needs to happen.
|
||||
|
@ -65,7 +65,7 @@ impl GitSource {
|
|||
// situation that we have a locked revision but the database
|
||||
// doesn't have it.
|
||||
(locked_rev, db) => {
|
||||
debug!("Updating git source `{:?}`", self.git.url);
|
||||
debug!("Updating git source `{:?}`", self.git.repository);
|
||||
|
||||
// Report the checkout operation to the reporter.
|
||||
let task = self.reporter.as_ref().map(|reporter| {
|
||||
|
@ -114,13 +114,13 @@ impl GitSource {
|
|||
}
|
||||
|
||||
pub struct Fetch {
|
||||
/// The [`Git`] reference that was fetched.
|
||||
git: Git,
|
||||
/// The [`GitUrl`] reference that was fetched.
|
||||
git: GitUrl,
|
||||
/// The path to the checked out repository.
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl From<Fetch> for Git {
|
||||
impl From<Fetch> for GitUrl {
|
||||
fn from(fetch: Fetch) -> Self {
|
||||
fetch.git
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue