Skip to content

Commit

Permalink
Allow use of sparse registries (#373)
Browse files Browse the repository at this point in the history
Registries that use the [sparse registry protocol] show up in
`Cargo.lock` not as `registry+URL`, but as `sparse+URL`. This patch
makes it so that guppy (and hakari in particular in my case) doesn't
error out on lockfiles with dependencies from such registries.

For sparse registries, hakari will look for entries in
`.config/hakari.toml` with `sparse+` in the listed URL:

```toml
[registries."reg"]
index = "sparse+https://whatever"
```

This matches what goes in `.cargo/config.toml`.

[sparse registry protocol]:
https://doc.rust-lang.org/cargo/reference/registry-index.html#sparse-protocol

---------

Co-authored-by: Rain <[email protected]>
  • Loading branch information
jonhoo and sunshowers authored Jan 5, 2025
1 parent fe3e11b commit 09c9e5a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
30 changes: 30 additions & 0 deletions guppy/src/graph/graph_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,27 @@ pub enum ExternalSource<'g> {
/// ```
Registry(&'g str),

/// This is a registry source that uses the [sparse registry protocol][sparse], e.g. `"sparse+https://index.crates.io"`.
///
/// The associated data is the part of the string after the initial `"sparse+"`.
///
/// # Examples
///
/// ```
/// use guppy::graph::ExternalSource;
///
/// let source = "sparse+https://index.crates.io";
/// let parsed = ExternalSource::new(source).expect("this source is understood by guppy");
///
/// assert_eq!(
/// parsed,
/// ExternalSource::Sparse("https://index.crates.io"),
/// );
/// ```
///
/// [sparse]: https://doc.rust-lang.org/cargo/reference/registry-index.html#sparse-protocol
Sparse(&'g str),

/// This is a Git source.
///
/// An example of a Git source string is `"git+https://github.com/rust-lang/cargo.git?branch=main#0227f048fcb7c798026ede6cc20c92befc84c3a4"`.
Expand Down Expand Up @@ -1470,6 +1491,11 @@ impl<'g> ExternalSource<'g> {
/// Used for matching with the `Registry` variant.
pub const REGISTRY_PLUS: &'static str = "registry+";

/// The string `"sparse+"`.
///
/// Also used for matching with the `Sparse` variant.
pub const SPARSE_PLUS: &'static str = "sparse+";

/// The string `"git+"`.
///
/// Used for matching with the `Git` variant.
Expand Down Expand Up @@ -1504,6 +1530,9 @@ impl<'g> ExternalSource<'g> {
if let Some(registry) = source.strip_prefix(Self::REGISTRY_PLUS) {
// A registry source.
Some(ExternalSource::Registry(registry))
} else if let Some(sparse) = source.strip_prefix(Self::SPARSE_PLUS) {
// A sparse registry source.
Some(ExternalSource::Sparse(sparse))
} else if let Some(rest) = source.strip_prefix(Self::GIT_PLUS) {
// A Git source.
// Look for a trailing #, which indicates the resolved revision.
Expand Down Expand Up @@ -1555,6 +1584,7 @@ impl fmt::Display for ExternalSource<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ExternalSource::Registry(url) => write!(f, "{}{}", Self::REGISTRY_PLUS, url),
ExternalSource::Sparse(url) => write!(f, "{}{}", Self::SPARSE_PLUS, url),
ExternalSource::Git {
repository,
req,
Expand Down
26 changes: 26 additions & 0 deletions tools/hakari/src/toml_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,32 @@ pub(crate) fn write_toml(
);
itable.insert("registry", registry_name.into());
}
Some(ExternalSource::Sparse(registry_url)) => {
let registry_name = builder
.registries
.get_by_right(&format!(
"{}{}",
ExternalSource::SPARSE_PLUS,
registry_url
))
.ok_or_else(|| TomlOutError::UnrecognizedRegistry {
package_id: dep.id().clone(),
registry_url: registry_url.to_owned(),
})?;
itable.insert(
"version",
format!(
"{}",
VersionDisplay::new(
dep.version(),
options.exact_versions,
dep_format < DepFormatVersion::V3
)
)
.into(),
);
itable.insert("registry", registry_name.into());
}
_ => {
return Err(TomlOutError::UnrecognizedExternal {
package_id: dep.id().clone(),
Expand Down

0 comments on commit 09c9e5a

Please sign in to comment.