99//!
1010//! This module impl that cache in all the gory details
1111
12- use std:: cmp:: Ordering ;
13- use std:: collections:: { BTreeSet , HashMap , HashSet } ;
14- use std:: rc:: Rc ;
15-
16- use log:: debug;
17-
1812use crate :: core:: resolver:: context:: Context ;
1913use crate :: core:: resolver:: errors:: describe_path;
14+ use crate :: core:: resolver:: types:: { ConflictReason , DepInfo , FeaturesSet } ;
15+ use crate :: core:: resolver:: { ActivateResult , ResolveOpts } ;
2016use crate :: core:: { Dependency , FeatureValue , PackageId , PackageIdSpec , Registry , Summary } ;
17+ use crate :: core:: { GitReference , SourceId } ;
2118use crate :: util:: errors:: { CargoResult , CargoResultExt } ;
2219use crate :: util:: interning:: InternedString ;
23-
24- use crate :: core:: resolver:: types:: { ConflictReason , DepInfo , FeaturesSet } ;
25- use crate :: core:: resolver:: { ActivateResult , ResolveOpts } ;
20+ use crate :: util:: Config ;
21+ use log:: debug;
22+ use std:: cmp:: Ordering ;
23+ use std:: collections:: { BTreeSet , HashMap , HashSet } ;
24+ use std:: rc:: Rc ;
2625
2726pub struct RegistryQueryer < ' a > {
2827 pub registry : & ' a mut ( dyn Registry + ' a ) ,
@@ -41,6 +40,10 @@ pub struct RegistryQueryer<'a> {
4140 > ,
4241 /// all the cases we ended up using a supplied replacement
4342 used_replacements : HashMap < PackageId , Summary > ,
43+ /// Where to print warnings, if configured.
44+ config : Option < & ' a Config > ,
45+ /// Sources that we've already wared about possibly colliding in the future.
46+ warned_git_collisions : HashSet < SourceId > ,
4447}
4548
4649impl < ' a > RegistryQueryer < ' a > {
@@ -49,6 +52,7 @@ impl<'a> RegistryQueryer<'a> {
4952 replacements : & ' a [ ( PackageIdSpec , Dependency ) ] ,
5053 try_to_use : & ' a HashSet < PackageId > ,
5154 minimal_versions : bool ,
55+ config : Option < & ' a Config > ,
5256 ) -> Self {
5357 RegistryQueryer {
5458 registry,
@@ -58,6 +62,8 @@ impl<'a> RegistryQueryer<'a> {
5862 registry_cache : HashMap :: new ( ) ,
5963 summary_cache : HashMap :: new ( ) ,
6064 used_replacements : HashMap :: new ( ) ,
65+ config,
66+ warned_git_collisions : HashSet :: new ( ) ,
6167 }
6268 }
6369
@@ -69,13 +75,52 @@ impl<'a> RegistryQueryer<'a> {
6975 self . used_replacements . get ( & p)
7076 }
7177
78+ /// Issues a future-compatible warning targeted at removing reliance on
79+ /// unifying behavior between these two dependency directives:
80+ ///
81+ /// ```toml
82+ /// [dependencies]
83+ /// a = { git = 'https://example.org/foo' }
84+ /// a = { git = 'https://example.org/foo', branch = 'master }
85+ /// ```
86+ ///
87+ /// Historical versions of Cargo considered these equivalent but going
88+ /// forward we'd like to fix this. For more details see the comments in
89+ /// src/cargo/sources/git/utils.rs
90+ fn warn_colliding_git_sources ( & mut self , id : SourceId ) -> CargoResult < ( ) > {
91+ let config = match self . config {
92+ Some ( config) => config,
93+ None => return Ok ( ( ) ) ,
94+ } ;
95+ let prev = match self . warned_git_collisions . replace ( id) {
96+ Some ( prev) => prev,
97+ None => return Ok ( ( ) ) ,
98+ } ;
99+ match ( id. git_reference ( ) , prev. git_reference ( ) ) {
100+ ( Some ( GitReference :: DefaultBranch ) , Some ( GitReference :: Branch ( b) ) )
101+ | ( Some ( GitReference :: Branch ( b) ) , Some ( GitReference :: DefaultBranch ) )
102+ if b == "master" => { }
103+ _ => return Ok ( ( ) ) ,
104+ }
105+
106+ config. shell ( ) . warn ( & format ! (
107+ "two git dependencies found for `{}` \
108+ where one uses `branch = \" master\" ` and the other doesn't; \
109+ this will break in a future version of Cargo, so please \
110+ ensure the dependency forms are consistent",
111+ id. url( ) ,
112+ ) ) ?;
113+ Ok ( ( ) )
114+ }
115+
72116 /// Queries the `registry` to return a list of candidates for `dep`.
73117 ///
74118 /// This method is the location where overrides are taken into account. If
75119 /// any candidates are returned which match an override then the override is
76120 /// applied by performing a second query for what the override should
77121 /// return.
78122 pub fn query ( & mut self , dep : & Dependency ) -> CargoResult < Rc < Vec < Summary > > > {
123+ self . warn_colliding_git_sources ( dep. source_id ( ) ) ?;
79124 if let Some ( out) = self . registry_cache . get ( dep) . cloned ( ) {
80125 return Ok ( out) ;
81126 }
0 commit comments