Skip to content

Commit d77aef5

Browse files
committed
Add configuration option for controlling crates.io protocol
`registries.crates-io.protocol` can be set to either `sparse` or `git`. The default is `git` unless `-Z sparse-registry` is passed.
1 parent b8f30cb commit d77aef5

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/cargo/core/source/source_id.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::core::PackageId;
22
use crate::sources::registry::CRATES_IO_HTTP_INDEX;
33
use crate::sources::{DirectorySource, CRATES_IO_DOMAIN, CRATES_IO_INDEX, CRATES_IO_REGISTRY};
44
use crate::sources::{GitSource, PathSource, RegistrySource};
5-
use crate::util::{CanonicalUrl, CargoResult, Config, IntoUrl};
5+
use crate::util::{config, CanonicalUrl, CargoResult, Config, IntoUrl};
66
use log::trace;
77
use serde::de;
88
use serde::ser;
@@ -215,7 +215,7 @@ impl SourceId {
215215
/// Returns the `SourceId` corresponding to the main repository, using the
216216
/// sparse HTTP index if allowed.
217217
pub fn crates_io_maybe_sparse_http(config: &Config) -> CargoResult<SourceId> {
218-
if config.cli_unstable().sparse_registry {
218+
if Self::crates_io_is_sparse(config)? {
219219
config.check_registry_index_not_set()?;
220220
let url = CRATES_IO_HTTP_INDEX.into_url().unwrap();
221221
SourceId::new(SourceKind::Registry, url, Some(CRATES_IO_REGISTRY))
@@ -224,6 +224,21 @@ impl SourceId {
224224
}
225225
}
226226

227+
/// Returns whether to access crates.io over the sparse protocol.
228+
pub fn crates_io_is_sparse(config: &Config) -> CargoResult<bool> {
229+
let proto: Option<config::Value<String>> = config.get("registries.crates-io.protocol")?;
230+
let is_sparse = match proto.as_ref().map(|v| v.val.as_str()) {
231+
Some("sparse") => true,
232+
Some("git") => false,
233+
Some(unknown) => anyhow::bail!(
234+
"unsupported registry protocol `{unknown}` (defined in {})",
235+
proto.as_ref().unwrap().definition
236+
),
237+
None => config.cli_unstable().sparse_registry,
238+
};
239+
Ok(is_sparse)
240+
}
241+
227242
/// Gets the `SourceId` associated with given name of the remote registry.
228243
pub fn alt_registry(config: &Config, key: &str) -> CargoResult<SourceId> {
229244
if key == CRATES_IO_REGISTRY {

src/cargo/sources/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<'cfg> SourceConfigMap<'cfg> {
9191
replace_with: None,
9292
},
9393
)?;
94-
if config.cli_unstable().sparse_registry {
94+
if SourceId::crates_io_is_sparse(config)? {
9595
base.add(
9696
CRATES_IO_REGISTRY,
9797
SourceConfig {

tests/testsuite/registry.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,6 +2703,24 @@ fn http_requires_z_flag() {
27032703
.run();
27042704
}
27052705

2706+
#[cargo_test]
2707+
fn protocol_sparse_requires_z_flag() {
2708+
cargo_process("install bar")
2709+
.with_status(101)
2710+
.env("CARGO_REGISTRIES_CRATES_IO_PROTOCOL", "sparse")
2711+
.with_stderr("[ERROR] usage of sparse registries requires `-Z sparse-registry`")
2712+
.run()
2713+
}
2714+
2715+
#[cargo_test]
2716+
fn protocol() {
2717+
cargo_process("install bar")
2718+
.with_status(101)
2719+
.env("CARGO_REGISTRIES_CRATES_IO_PROTOCOL", "invalid")
2720+
.with_stderr("[ERROR] unsupported registry protocol `invalid` (defined in environment variable `CARGO_REGISTRIES_CRATES_IO_PROTOCOL`)")
2721+
.run()
2722+
}
2723+
27062724
#[cargo_test]
27072725
fn http_requires_trailing_slash() {
27082726
cargo_process("-Z sparse-registry install bar --index sparse+https://index.crates.io")

0 commit comments

Comments
 (0)