@@ -27,7 +27,7 @@ impl<'cfg> GitSource<'cfg> {
27
27
assert ! ( source_id. is_git( ) , "id is not git, id={}" , source_id) ;
28
28
29
29
let remote = GitRemote :: new ( source_id. url ( ) ) ;
30
- let ident = ident ( source_id. url ( ) ) ? ;
30
+ let ident = ident ( & source_id) ;
31
31
32
32
let reference = match source_id. precise ( ) {
33
33
Some ( s) => GitReference :: Rev ( s. to_string ( ) ) ,
@@ -59,58 +59,17 @@ impl<'cfg> GitSource<'cfg> {
59
59
}
60
60
}
61
61
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 ( )
65
66
. path_segments ( )
66
- . and_then ( |mut s| s. next_back ( ) )
67
+ . and_then ( |s| s. rev ( ) . next ( ) )
67
68
. unwrap_or ( "" ) ;
68
69
69
70
let ident = if ident == "" { "_empty" } else { ident } ;
70
71
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( ) ) )
114
73
}
115
74
116
75
impl < ' cfg > Debug for GitSource < ' cfg > {
@@ -241,56 +200,54 @@ impl<'cfg> Source for GitSource<'cfg> {
241
200
#[ cfg( test) ]
242
201
mod test {
243
202
use super :: ident;
203
+ use crate :: core:: { GitReference , SourceId } ;
244
204
use crate :: util:: IntoUrl ;
245
- use url:: Url ;
246
205
247
206
#[ test]
248
207
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" ) ) ;
250
209
assert ! ( ident. starts_with( "cargo-" ) ) ;
251
210
}
252
211
253
212
#[ test]
254
213
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" ) ) ;
256
215
assert ! ( ident. starts_with( "_empty-" ) ) ;
257
216
}
258
217
259
218
#[ test]
260
219
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" ) ) ;
263
222
assert_eq ! ( ident1, ident2) ;
264
223
}
265
224
266
225
#[ test]
267
226
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" ) ) ;
270
229
assert_eq ! ( ident1, ident2) ;
271
230
}
272
231
273
232
#[ test]
274
233
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" ) ) ;
277
236
assert_eq ! ( ident1, ident2) ;
278
237
}
279
238
280
239
#[ test]
281
240
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" ) ) ;
284
243
assert_eq ! ( ident1, ident2) ;
285
244
}
286
245
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 ( )
295
252
}
296
253
}
0 commit comments