@@ -77,57 +77,34 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
7777 // Double check. It is safe theoretically, unless logic has updated.
7878 assert_eq ! ( pkgs. len( ) , 1 ) ;
7979
80- let ( pkg, cli_features) = pkgs. pop ( ) . unwrap ( ) ;
81-
82- let mut publish_registry = match opts. reg_or_index . as_ref ( ) {
83- Some ( RegistryOrIndex :: Registry ( registry) ) => Some ( registry. clone ( ) ) ,
84- _ => None ,
85- } ;
86- if let Some ( ref allowed_registries) = * pkg. publish ( ) {
87- if publish_registry. is_none ( ) && allowed_registries. len ( ) == 1 {
88- // If there is only one allowed registry, push to that one directly,
89- // even though there is no registry specified in the command.
90- let default_registry = & allowed_registries[ 0 ] ;
91- if default_registry != CRATES_IO_REGISTRY {
92- // Don't change the registry for crates.io and don't warn the user.
93- // crates.io will be defaulted even without this.
94- opts. gctx . shell ( ) . note ( & format ! (
95- "found `{}` as only allowed registry. Publishing to it automatically." ,
96- default_registry
97- ) ) ?;
98- publish_registry = Some ( default_registry. clone ( ) ) ;
80+ let just_pkgs: Vec < _ > = pkgs. iter ( ) . map ( |p| p. 0 ) . collect ( ) ;
81+ let reg_or_index = match opts. reg_or_index . clone ( ) {
82+ Some ( r) => {
83+ validate_registry ( & just_pkgs, Some ( & r) ) ?;
84+ Some ( r)
85+ }
86+ None => {
87+ let reg = super :: infer_registry ( & just_pkgs) ?;
88+ validate_registry ( & just_pkgs, reg. as_ref ( ) ) ?;
89+ if let Some ( RegistryOrIndex :: Registry ( ref registry) ) = & reg {
90+ if registry != CRATES_IO_REGISTRY {
91+ // Don't warn for crates.io.
92+ opts. gctx . shell ( ) . note ( & format ! (
93+ "found `{}` as only allowed registry. Publishing to it automatically." ,
94+ registry
95+ ) ) ?;
96+ }
9997 }
98+ reg
10099 }
100+ } ;
101101
102- let reg_name = publish_registry
103- . clone ( )
104- . unwrap_or_else ( || CRATES_IO_REGISTRY . to_string ( ) ) ;
105- if allowed_registries. is_empty ( ) {
106- bail ! (
107- "`{}` cannot be published.\n \
108- `package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.",
109- pkg. name( ) ,
110- ) ;
111- } else if !allowed_registries. contains ( & reg_name) {
112- bail ! (
113- "`{}` cannot be published.\n \
114- The registry `{}` is not listed in the `package.publish` value in Cargo.toml.",
115- pkg. name( ) ,
116- reg_name
117- ) ;
118- }
119- }
120102 // This is only used to confirm that we can create a token before we build the package.
121103 // This causes the credential provider to be called an extra time, but keeps the same order of errors.
104+ let ( pkg, cli_features) = pkgs. pop ( ) . unwrap ( ) ;
122105 let ver = pkg. version ( ) . to_string ( ) ;
123106 let operation = Operation :: Read ;
124107
125- let reg_or_index = match opts. reg_or_index . clone ( ) {
126- Some ( RegistryOrIndex :: Registry ( _) ) | None => {
127- publish_registry. map ( RegistryOrIndex :: Registry )
128- }
129- val => val,
130- } ;
131108 let source_ids = super :: get_source_id ( opts. gctx , reg_or_index. as_ref ( ) ) ?;
132109 let mut registry = super :: registry (
133110 opts. gctx ,
@@ -498,3 +475,37 @@ fn transmit(
498475
499476 Ok ( ( ) )
500477}
478+
479+ fn validate_registry ( pkgs : & [ & Package ] , reg_or_index : Option < & RegistryOrIndex > ) -> CargoResult < ( ) > {
480+ for pkg in pkgs {
481+ if pkg. publish ( ) == & Some ( Vec :: new ( ) ) {
482+ bail ! (
483+ "`{}` cannot be published.\n \
484+ `package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.",
485+ pkg. name( ) ,
486+ ) ;
487+ }
488+ }
489+
490+ let reg_name = match reg_or_index {
491+ Some ( RegistryOrIndex :: Registry ( r) ) => Some ( r. as_str ( ) ) ,
492+ None => Some ( CRATES_IO_REGISTRY ) ,
493+ Some ( RegistryOrIndex :: Index ( _) ) => None ,
494+ } ;
495+ if let Some ( reg_name) = reg_name {
496+ for pkg in pkgs {
497+ if let Some ( allowed) = pkg. publish ( ) . as_ref ( ) {
498+ if !allowed. iter ( ) . any ( |a| a == reg_name) {
499+ bail ! (
500+ "`{}` cannot be published.\n \
501+ The registry `{}` is not listed in the `package.publish` value in Cargo.toml.",
502+ pkg. name( ) ,
503+ reg_name
504+ ) ;
505+ }
506+ }
507+ }
508+ }
509+
510+ Ok ( ( ) )
511+ }
0 commit comments