@@ -2,7 +2,7 @@ use crate::core::PackageId;
22use crate :: sources:: registry:: CRATES_IO_HTTP_INDEX ;
33use crate :: sources:: { DirectorySource , CRATES_IO_DOMAIN , CRATES_IO_INDEX , CRATES_IO_REGISTRY } ;
44use crate :: sources:: { GitSource , PathSource , RegistrySource } ;
5- use crate :: util:: { CanonicalUrl , CargoResult , Config , IntoUrl } ;
5+ use crate :: util:: { config , CanonicalUrl , CargoResult , Config , IntoUrl } ;
66use log:: trace;
77use serde:: de;
88use 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 {
0 commit comments