@@ -21,7 +21,7 @@ use url::Url;
21
21
22
22
use crate :: core:: { Package , PackageId , SourceId } ;
23
23
use crate :: sources:: source:: Source ;
24
- use crate :: sources:: { RegistrySource , SourceConfigMap , CRATES_IO_REGISTRY } ;
24
+ use crate :: sources:: { RegistrySource , SourceConfigMap } ;
25
25
use crate :: util:: auth;
26
26
use crate :: util:: cache_lock:: CacheLockMode ;
27
27
use crate :: util:: context:: { GlobalContext , PathAndArgs } ;
@@ -191,7 +191,7 @@ fn registry(
191
191
///
192
192
/// The return value is a pair of `SourceId`s: The first may be a built-in replacement of
193
193
/// crates.io (such as index.crates.io), while the second is always the original source.
194
- fn get_source_id (
194
+ pub ( crate ) fn get_source_id (
195
195
gctx : & GlobalContext ,
196
196
reg_or_index : Option < & RegistryOrIndex > ,
197
197
) -> CargoResult < RegistrySourceIds > {
@@ -324,87 +324,40 @@ pub(crate) struct RegistrySourceIds {
324
324
}
325
325
326
326
/// If this set of packages has an unambiguous publish registry, find it.
327
- pub ( crate ) fn infer_registry (
328
- gctx : & GlobalContext ,
329
- pkgs : & [ & Package ] ,
330
- reg_or_index : Option < RegistryOrIndex > ,
331
- ) -> CargoResult < SourceId > {
332
- let reg_or_index = match reg_or_index {
333
- Some ( r) => r,
334
- None => {
335
- if pkgs[ 1 ..] . iter ( ) . all ( |p| p. publish ( ) == pkgs[ 0 ] . publish ( ) ) {
336
- // If all packages have the same publish settings, we take that as the default.
337
- match pkgs[ 0 ] . publish ( ) . as_deref ( ) {
338
- Some ( [ unique_pkg_reg] ) => RegistryOrIndex :: Registry ( unique_pkg_reg. to_owned ( ) ) ,
339
- None | Some ( [ ] ) => RegistryOrIndex :: Registry ( CRATES_IO_REGISTRY . to_owned ( ) ) ,
340
- Some ( [ reg, ..] ) if pkgs. len ( ) == 1 => {
341
- // For backwards compatibility, avoid erroring if there's only one package.
342
- // The registry doesn't affect packaging in this case.
343
- RegistryOrIndex :: Registry ( reg. to_owned ( ) )
344
- }
345
- Some ( regs) => {
346
- let mut regs: Vec < _ > = regs. iter ( ) . map ( |s| format ! ( "\" {}\" " , s) ) . collect ( ) ;
347
- regs. sort ( ) ;
348
- regs. dedup ( ) ;
349
- // unwrap: the match block ensures that there's more than one reg.
350
- let ( last_reg, regs) = regs. split_last ( ) . unwrap ( ) ;
351
- bail ! (
352
- "--registry is required to disambiguate between {} or {} registries" ,
353
- regs. join( ", " ) ,
354
- last_reg
355
- )
356
- }
357
- }
358
- } else {
359
- let common_regs = pkgs
360
- . iter ( )
361
- // `None` means "all registries", so drop them instead of including them
362
- // in the intersection.
363
- . filter_map ( |p| p. publish ( ) . as_deref ( ) )
364
- . map ( |p| p. iter ( ) . collect :: < HashSet < _ > > ( ) )
365
- . reduce ( |xs, ys| xs. intersection ( & ys) . cloned ( ) . collect ( ) )
366
- . unwrap_or_default ( ) ;
367
- if common_regs. is_empty ( ) {
368
- bail ! ( "conflicts between `package.publish` fields in the selected packages" ) ;
369
- } else {
370
- bail ! (
371
- "--registry is required because not all `package.publish` settings agree" ,
372
- ) ;
373
- }
327
+ pub ( crate ) fn infer_registry ( pkgs : & [ & Package ] ) -> CargoResult < Option < RegistryOrIndex > > {
328
+ if pkgs[ 1 ..] . iter ( ) . all ( |p| p. publish ( ) == pkgs[ 0 ] . publish ( ) ) {
329
+ // If all packages have the same publish settings, we take that as the default.
330
+ match pkgs[ 0 ] . publish ( ) . as_deref ( ) {
331
+ Some ( [ unique_pkg_reg] ) => {
332
+ Ok ( Some ( RegistryOrIndex :: Registry ( unique_pkg_reg. to_owned ( ) ) ) )
374
333
}
375
- }
376
- } ;
377
-
378
- // Validate the registry against the packages' allow-lists. For backwards compatibility, we
379
- // skip this if only a single package is being published (because in that case the registry
380
- // doesn't affect the packaging step).
381
- if pkgs. len ( ) > 1 {
382
- if let RegistryOrIndex :: Registry ( reg_name) = & reg_or_index {
383
- for pkg in pkgs {
384
- if let Some ( allowed) = pkg. publish ( ) . as_ref ( ) {
385
- if !allowed. iter ( ) . any ( |a| a == reg_name) {
386
- bail ! (
387
- "`{}` cannot be packaged.\n \
388
- The registry `{}` is not listed in the `package.publish` value in Cargo.toml.",
389
- pkg. name( ) ,
390
- reg_name
391
- ) ;
392
- }
393
- }
334
+ None | Some ( [ ] ) => Ok ( None ) ,
335
+ Some ( regs) => {
336
+ let mut regs: Vec < _ > = regs. iter ( ) . map ( |s| format ! ( "\" {}\" " , s) ) . collect ( ) ;
337
+ regs. sort ( ) ;
338
+ regs. dedup ( ) ;
339
+ // unwrap: the match block ensures that there's more than one reg.
340
+ let ( last_reg, regs) = regs. split_last ( ) . unwrap ( ) ;
341
+ bail ! (
342
+ "--registry is required to disambiguate between {} or {} registries" ,
343
+ regs. join( ", " ) ,
344
+ last_reg
345
+ )
394
346
}
395
347
}
348
+ } else {
349
+ let common_regs = pkgs
350
+ . iter ( )
351
+ // `None` means "all registries", so drop them instead of including them
352
+ // in the intersection.
353
+ . filter_map ( |p| p. publish ( ) . as_deref ( ) )
354
+ . map ( |p| p. iter ( ) . collect :: < HashSet < _ > > ( ) )
355
+ . reduce ( |xs, ys| xs. intersection ( & ys) . cloned ( ) . collect ( ) )
356
+ . unwrap_or_default ( ) ;
357
+ if common_regs. is_empty ( ) {
358
+ bail ! ( "conflicts between `package.publish` fields in the selected packages" ) ;
359
+ } else {
360
+ bail ! ( "--registry is required because not all `package.publish` settings agree" , ) ;
361
+ }
396
362
}
397
-
398
- let sid = match reg_or_index {
399
- RegistryOrIndex :: Index ( url) => SourceId :: for_registry ( & url) ?,
400
- RegistryOrIndex :: Registry ( reg) if reg == CRATES_IO_REGISTRY => SourceId :: crates_io ( gctx) ?,
401
- RegistryOrIndex :: Registry ( reg) => SourceId :: alt_registry ( gctx, & reg) ?,
402
- } ;
403
-
404
- // Load source replacements that are built-in to Cargo.
405
- let sid = SourceConfigMap :: empty ( gctx) ?
406
- . load ( sid, & HashSet :: new ( ) ) ?
407
- . replaced_source_id ( ) ;
408
-
409
- Ok ( sid)
410
363
}
0 commit comments