@@ -107,7 +107,7 @@ impl fmt::Display for GitUrl {
107
107
format ! ( ":{}" , & self . path)
108
108
}
109
109
}
110
- _ => ( & self . path ) . to_string ( ) ,
110
+ _ => self . path . to_string ( ) ,
111
111
} ;
112
112
113
113
let git_url_str = format ! ( "{}{}{}{}{}" , scheme_prefix, auth_info, host, port, path) ;
@@ -156,11 +156,7 @@ impl GitUrl {
156
156
/// Returns a `Result<GitUrl>` after normalizing and parsing `url` for metadata
157
157
pub fn parse ( url : & str ) -> Result < GitUrl , GitUrlParseError > {
158
158
// Normalize the url so we can use Url crate to process ssh urls
159
- let normalized = if let Ok ( url) = normalize_url ( url) {
160
- url
161
- } else {
162
- return Err ( GitUrlParseError :: UrlNormalizeFailed ) ;
163
- } ;
159
+ let normalized = normalize_url ( url) ?;
164
160
165
161
// Some pre-processing for paths
166
162
let scheme = if let Ok ( scheme) = Scheme :: from_str ( normalized. scheme ( ) ) {
@@ -170,6 +166,9 @@ impl GitUrl {
170
166
normalized. scheme ( ) . to_string ( ) ,
171
167
) ) ;
172
168
} ;
169
+ if normalized. path ( ) . is_empty ( ) {
170
+ return Err ( GitUrlParseError :: EmptyPath ) ;
171
+ }
173
172
174
173
// Normalized ssh urls can always have their first '/' removed
175
174
let urlpath = match & scheme {
@@ -211,7 +210,7 @@ impl GitUrl {
211
210
let mut fullname: Vec < & str > = Vec :: new ( ) ;
212
211
213
212
// TODO: Add support for parsing out orgs from these urls
214
- let hosts_w_organization_in_path = vec ! [ "dev.azure.com" , "ssh.dev.azure.com" ] ;
213
+ let hosts_w_organization_in_path = [ "dev.azure.com" , "ssh.dev.azure.com" ] ;
215
214
//vec!["dev.azure.com", "ssh.dev.azure.com", "visualstudio.com"];
216
215
217
216
let host_str = if let Some ( host) = normalized. host_str ( ) {
@@ -362,7 +361,7 @@ fn normalize_file_path(filepath: &str) -> Result<Url, GitUrlParseError> {
362
361
if let Ok ( file_url) = normalize_url ( & format ! ( "file://{}" , filepath) ) {
363
362
Ok ( file_url)
364
363
} else {
365
- return Err ( GitUrlParseError :: FileUrlNormalizeFailedSchemeAdded ) ;
364
+ Err ( GitUrlParseError :: FileUrlNormalizeFailedSchemeAdded )
366
365
}
367
366
}
368
367
}
@@ -463,11 +462,7 @@ fn is_ssh_url(url: &str) -> bool {
463
462
464
463
// Make sure we provided a username, and not just `@`
465
464
let parts: Vec < & str > = url. split ( '@' ) . collect ( ) ;
466
- if parts. len ( ) != 2 && !parts[ 0 ] . is_empty ( ) {
467
- return false ;
468
- } else {
469
- return true ;
470
- }
465
+ return parts. len ( ) == 2 || parts[ 0 ] . is_empty ( ) ;
471
466
}
472
467
473
468
// it's an ssh url if we have a domain:path pattern
@@ -481,21 +476,14 @@ fn is_ssh_url(url: &str) -> bool {
481
476
// This should also handle if a port is specified
482
477
// no port example: ssh://user@domain:path/to/repo.git
483
478
// port example: ssh://user@domain:port/path/to/repo.git
484
- if parts. len ( ) != 2 && !parts[ 0 ] . is_empty ( ) && !parts[ 1 ] . is_empty ( ) {
485
- return false ;
486
- } else {
487
- return true ;
488
- }
479
+ parts. len ( ) == 2 && parts[ 0 ] . is_empty ( ) && parts[ 1 ] . is_empty ( )
489
480
}
490
481
491
482
#[ derive( Error , Debug , PartialEq , Eq ) ]
492
483
pub enum GitUrlParseError {
493
- #[ error( "Error from Url crate" ) ]
484
+ #[ error( "Error from Url crate: {0} " ) ]
494
485
UrlParseError ( #[ from] url:: ParseError ) ,
495
486
496
- #[ error( "Url normalization into url::Url failed" ) ]
497
- UrlNormalizeFailed ,
498
-
499
487
#[ error( "No url scheme was found, then failed to normalize as ssh url." ) ]
500
488
SshUrlNormalizeFailedNoScheme ,
501
489
@@ -526,6 +514,8 @@ pub enum GitUrlParseError {
526
514
UnsupportedUrlHostFormat ,
527
515
#[ error( "Git Url not in expected format for SSH" ) ]
528
516
UnsupportedSshUrlFormat ,
517
+ #[ error( "Normalized URL has no path" ) ]
518
+ EmptyPath ,
529
519
530
520
#[ error( "Found null bytes within input url before parsing" ) ]
531
521
FoundNullBytes ,
0 commit comments