diff --git a/src/common/git.rs b/src/common/git.rs index 2823fe6..3ed2dde 100644 --- a/src/common/git.rs +++ b/src/common/git.rs @@ -16,7 +16,23 @@ pub fn meta(uri: &str) -> (String, String, String) { let actual_uri = if uri.contains("://") || uri.contains('@') { uri.to_string() } else { - format!("https://github.com/{uri}") + if let Some((domain, route)) = uri.split_once('/') { + if domain.contains(".") { + format!("https://{domain}/{route}") + } else { + // Users can pass name starting wirh a slash + let first_char = uri.chars().next(); + + if first_char == Some('/') { + format!("https://github.com{uri}") + } + else { + format!("https://github.com/{uri}") + } + } + } else { + panic!("Invalid link") + } }; let uri_to_split = actual_uri.replace(':', "/"); @@ -54,4 +70,36 @@ mod tests { assert_eq!(user, "user".to_string()); assert_eq!(repo, "repo".to_string()); } + + #[test] + fn test_meta_github_repo_name() { + let (actual_uri, user, repo) = meta("user/repo"); + assert_eq!(actual_uri, "https://github.com/user/repo".to_string()); + assert_eq!(user, "user".to_string()); + assert_eq!(repo, "repo".to_string()); + } + + #[test] + fn test_meta_github_repo_name_with_slash() { + let (actual_uri, user, repo) = meta("/user/repo"); + assert_eq!(actual_uri, "https://github.com/user/repo".to_string()); + assert_eq!(user, "user".to_string()); + assert_eq!(repo, "repo".to_string()); + } + + #[test] + fn test_meta_github_clean_link() { + let (actual_uri, user, repo) = meta("github.com/user/repo"); + assert_eq!(actual_uri, "https://github.com/user/repo".to_string()); + assert_eq!(user, "user".to_string()); + assert_eq!(repo, "repo".to_string()); + } + + #[test] + fn test_meta_random_git_provider_http() { + let (actual_uri, user, repo) = meta("https://sr.ht/user/repo"); + assert_eq!(actual_uri, "https://sr.ht/user/repo".to_string()); + assert_eq!(user, "user".to_string()); + assert_eq!(repo, "repo".to_string()); + } }