@@ -27,7 +27,7 @@ impl<'cfg> GitSource<'cfg> {
2727 assert ! ( source_id. is_git( ) , "id is not git, id={}" , source_id) ;
2828
2929 let remote = GitRemote :: new ( source_id. url ( ) ) ;
30- let ident = ident ( source_id. url ( ) ) ? ;
30+ let ident = ident ( & source_id) ;
3131
3232 let reference = match source_id. precise ( ) {
3333 Some ( s) => GitReference :: Rev ( s. to_string ( ) ) ,
@@ -59,58 +59,17 @@ impl<'cfg> GitSource<'cfg> {
5959 }
6060}
6161
62- fn ident ( url : & Url ) -> CargoResult < String > {
63- let url = canonicalize_url ( url) ?;
64- let ident = url
62+ fn ident ( id : & SourceId ) -> String {
63+ let ident = id
64+ . canonical_url ( )
65+ . raw_canonicalized_url ( )
6566 . path_segments ( )
66- . and_then ( |mut s| s. next_back ( ) )
67+ . and_then ( |s| s. rev ( ) . next ( ) )
6768 . unwrap_or ( "" ) ;
6869
6970 let ident = if ident == "" { "_empty" } else { ident } ;
7071
71- Ok ( format ! ( "{}-{}" , ident, short_hash( & url) ) )
72- }
73-
74- // Some hacks and heuristics for making equivalent URLs hash the same.
75- pub fn canonicalize_url ( url : & Url ) -> CargoResult < Url > {
76- let mut url = url. clone ( ) ;
77-
78- // cannot-be-a-base-urls (e.g., `github.com:rust-lang-nursery/rustfmt.git`)
79- // are not supported.
80- if url. cannot_be_a_base ( ) {
81- failure:: bail!(
82- "invalid url `{}`: cannot-be-a-base-URLs are not supported" ,
83- url
84- )
85- }
86-
87- // Strip a trailing slash.
88- if url. path ( ) . ends_with ( '/' ) {
89- url. path_segments_mut ( ) . unwrap ( ) . pop_if_empty ( ) ;
90- }
91-
92- // HACK: for GitHub URLs specifically, just lower-case
93- // everything. GitHub treats both the same, but they hash
94- // differently, and we're gonna be hashing them. This wants a more
95- // general solution, and also we're almost certainly not using the
96- // same case conversion rules that GitHub does. (See issue #84.)
97- if url. host_str ( ) == Some ( "github.com" ) {
98- url. set_scheme ( "https" ) . unwrap ( ) ;
99- let path = url. path ( ) . to_lowercase ( ) ;
100- url. set_path ( & path) ;
101- }
102-
103- // Repos can generally be accessed with or without `.git` extension.
104- let needs_chopping = url. path ( ) . ends_with ( ".git" ) ;
105- if needs_chopping {
106- let last = {
107- let last = url. path_segments ( ) . unwrap ( ) . next_back ( ) . unwrap ( ) ;
108- last[ ..last. len ( ) - 4 ] . to_owned ( )
109- } ;
110- url. path_segments_mut ( ) . unwrap ( ) . pop ( ) . push ( & last) ;
111- }
112-
113- Ok ( url)
72+ format ! ( "{}-{}" , ident, short_hash( id. canonical_url( ) ) )
11473}
11574
11675impl < ' cfg > Debug for GitSource < ' cfg > {
@@ -241,56 +200,54 @@ impl<'cfg> Source for GitSource<'cfg> {
241200#[ cfg( test) ]
242201mod test {
243202 use super :: ident;
203+ use crate :: core:: { GitReference , SourceId } ;
244204 use crate :: util:: IntoUrl ;
245- use url:: Url ;
246205
247206 #[ test]
248207 pub fn test_url_to_path_ident_with_path ( ) {
249- let ident = ident ( & url ( "https://github.com/carlhuda/cargo" ) ) . unwrap ( ) ;
208+ let ident = ident ( & src ( "https://github.com/carlhuda/cargo" ) ) ;
250209 assert ! ( ident. starts_with( "cargo-" ) ) ;
251210 }
252211
253212 #[ test]
254213 pub fn test_url_to_path_ident_without_path ( ) {
255- let ident = ident ( & url ( "https://github.com" ) ) . unwrap ( ) ;
214+ let ident = ident ( & src ( "https://github.com" ) ) ;
256215 assert ! ( ident. starts_with( "_empty-" ) ) ;
257216 }
258217
259218 #[ test]
260219 fn test_canonicalize_idents_by_stripping_trailing_url_slash ( ) {
261- let ident1 = ident ( & url ( "https://github.com/PistonDevelopers/piston/" ) ) . unwrap ( ) ;
262- let ident2 = ident ( & url ( "https://github.com/PistonDevelopers/piston" ) ) . unwrap ( ) ;
220+ let ident1 = ident ( & src ( "https://github.com/PistonDevelopers/piston/" ) ) ;
221+ let ident2 = ident ( & src ( "https://github.com/PistonDevelopers/piston" ) ) ;
263222 assert_eq ! ( ident1, ident2) ;
264223 }
265224
266225 #[ test]
267226 fn test_canonicalize_idents_by_lowercasing_github_urls ( ) {
268- let ident1 = ident ( & url ( "https://github.com/PistonDevelopers/piston" ) ) . unwrap ( ) ;
269- let ident2 = ident ( & url ( "https://github.com/pistondevelopers/piston" ) ) . unwrap ( ) ;
227+ let ident1 = ident ( & src ( "https://github.com/PistonDevelopers/piston" ) ) ;
228+ let ident2 = ident ( & src ( "https://github.com/pistondevelopers/piston" ) ) ;
270229 assert_eq ! ( ident1, ident2) ;
271230 }
272231
273232 #[ test]
274233 fn test_canonicalize_idents_by_stripping_dot_git ( ) {
275- let ident1 = ident ( & url ( "https://github.com/PistonDevelopers/piston" ) ) . unwrap ( ) ;
276- let ident2 = ident ( & url ( "https://github.com/PistonDevelopers/piston.git" ) ) . unwrap ( ) ;
234+ let ident1 = ident ( & src ( "https://github.com/PistonDevelopers/piston" ) ) ;
235+ let ident2 = ident ( & src ( "https://github.com/PistonDevelopers/piston.git" ) ) ;
277236 assert_eq ! ( ident1, ident2) ;
278237 }
279238
280239 #[ test]
281240 fn test_canonicalize_idents_different_protocols ( ) {
282- let ident1 = ident ( & url ( "https://github.com/PistonDevelopers/piston" ) ) . unwrap ( ) ;
283- let ident2 = ident ( & url ( "git://github.com/PistonDevelopers/piston" ) ) . unwrap ( ) ;
241+ let ident1 = ident ( & src ( "https://github.com/PistonDevelopers/piston" ) ) ;
242+ let ident2 = ident ( & src ( "git://github.com/PistonDevelopers/piston" ) ) ;
284243 assert_eq ! ( ident1, ident2) ;
285244 }
286245
287- #[ test]
288- fn test_canonicalize_cannot_be_a_base_urls ( ) {
289- assert ! ( ident( & url( "github.com:PistonDevelopers/piston" ) ) . is_err( ) ) ;
290- assert ! ( ident( & url( "google.com:PistonDevelopers/piston" ) ) . is_err( ) ) ;
291- }
292-
293- fn url ( s : & str ) -> Url {
294- s. into_url ( ) . unwrap ( )
246+ fn src ( s : & str ) -> SourceId {
247+ SourceId :: for_git (
248+ & s. into_url ( ) . unwrap ( ) ,
249+ GitReference :: Branch ( "master" . to_string ( ) ) ,
250+ )
251+ . unwrap ( )
295252 }
296253}
0 commit comments