@@ -7,7 +7,7 @@ use std::time::Duration;
77use std:: { cmp, env} ;
88
99use anyhow:: { bail, format_err} ;
10- use crates_io:: { NewCrate , NewCrateDependency , Registry } ;
10+ use crates_io:: { self , NewCrate , NewCrateDependency , Registry } ;
1111use curl:: easy:: { Easy , InfoType , SslOpt , SslVersion } ;
1212use log:: { log, Level } ;
1313use percent_encoding:: { percent_encode, NON_ALPHANUMERIC } ;
@@ -378,27 +378,8 @@ fn registry(
378378 token : token_config,
379379 index : index_config,
380380 } = registry_configuration ( config, registry. clone ( ) ) ?;
381- let token = match ( & index, & token, & token_config) {
382- // No token.
383- ( None , None , None ) => {
384- if validate_token {
385- bail ! ( "no upload token found, please run `cargo login` or pass `--token`" ) ;
386- }
387- None
388- }
389- // Token on command-line.
390- ( _, Some ( _) , _) => token,
391- // Token in config, no --index, loading from config is OK for crates.io.
392- ( None , None , Some ( _) ) => token_config,
393- // --index, no --token
394- ( Some ( _) , None , _) => {
395- if validate_token {
396- bail ! ( "command-line argument --index requires --token to be specified" )
397- }
398- None
399- }
400- } ;
401- let sid = get_source_id ( config, index_config. or ( index) , registry) ?;
381+ let opt_index = index_config. as_ref ( ) . or ( index. as_ref ( ) ) ;
382+ let sid = get_source_id ( config, opt_index, registry. as_ref ( ) ) ?;
402383 if !sid. is_remote_registry ( ) {
403384 bail ! (
404385 "{} does not support API commands.\n \
@@ -426,6 +407,50 @@ fn registry(
426407 cfg. and_then ( |cfg| cfg. api )
427408 . ok_or_else ( || format_err ! ( "{} does not support API commands" , sid) ) ?
428409 } ;
410+ let token = match ( & index, & token, & token_config) {
411+ // No token.
412+ ( None , None , None ) => {
413+ if validate_token {
414+ bail ! ( "no upload token found, please run `cargo login` or pass `--token`" ) ;
415+ }
416+ None
417+ }
418+ // Token on command-line.
419+ ( _, Some ( _) , _) => token,
420+ // Token in config, no --index, loading from config is OK for crates.io.
421+ ( None , None , Some ( _) ) => {
422+ // Check `is_default_registry` so that the crates.io index can
423+ // change config.json's "api" value, and this won't affect most
424+ // people. It will affect those using source replacement, but
425+ // hopefully that's a relatively small set of users.
426+ if registry. is_none ( )
427+ && !sid. is_default_registry ( )
428+ && !crates_io:: is_url_crates_io ( & api_host)
429+ {
430+ if validate_token {
431+ config. shell ( ) . warn (
432+ "using `registry.token` config value with source \
433+ replacement is deprecated\n \
434+ This may become a hard error in the future; \
435+ see <https://github.com/rust-lang/cargo/issues/xxx>.\n \
436+ Use the --token command-line flag to remove this warning.",
437+ ) ?;
438+ token_config
439+ } else {
440+ None
441+ }
442+ } else {
443+ token_config
444+ }
445+ }
446+ // --index, no --token
447+ ( Some ( _) , None , _) => {
448+ if validate_token {
449+ bail ! ( "command-line argument --index requires --token to be specified" )
450+ }
451+ None
452+ }
453+ } ;
429454 let handle = http_handle ( config) ?;
430455 Ok ( ( Registry :: new_handle ( api_host, token, handle) , sid) )
431456}
@@ -782,8 +807,8 @@ pub fn yank(
782807/// If both are None, returns the source for crates.io.
783808fn get_source_id (
784809 config : & Config ,
785- index : Option < String > ,
786- reg : Option < String > ,
810+ index : Option < & String > ,
811+ reg : Option < & String > ,
787812) -> CargoResult < SourceId > {
788813 match ( reg, index) {
789814 ( Some ( r) , _) => SourceId :: alt_registry ( config, & r) ,
0 commit comments