From 5dd38a2adb922932ea17b109dbd84cb8b9ffb332 Mon Sep 17 00:00:00 2001 From: oren0e <33298364+oren0e@users.noreply.github.com> Date: Sun, 19 Feb 2023 21:17:59 +0200 Subject: [PATCH] Fix Repo address should be parsed even without `.git` at the end (#11) * Make git suffix optional, add dashes matching in domain name, add tests * Bump version for the fix --- Cargo.toml | 2 +- src/match_logic.rs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 727bf5f..2bd6a6c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gitopen" -version = "1.3.1" +version = "1.3.2" authors = ["Oren Epshtain"] edition = "2018" license-file = "LICENSE" diff --git a/src/match_logic.rs b/src/match_logic.rs index 516af0a..0f58e99 100644 --- a/src/match_logic.rs +++ b/src/match_logic.rs @@ -25,8 +25,15 @@ fn is_https(s: &str) -> bool { false } +fn remove_git_suffix(s: &str) -> &str { + match s.strip_suffix(".git") { + Some(value) => value, + None => s, + } +} + pub fn parse_url_from_git(s: &str) -> AnyhowResult { - let re = Regex::new(r"((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@:/\-~]+)(\.git)(/)?")?; + let re = Regex::new(r"((git|ssh|http(s)?)|(git@[\w\.-]+))(:(//)?)([\w\.@:/\-~]+)(\.git)?(/)?")?; let url_parts = re .captures(s) .ok_or_else(|| anyhow!("Git repository not found"))?; @@ -38,9 +45,10 @@ pub fn parse_url_from_git(s: &str) -> AnyhowResult { .captures(&url_parts[1]) .ok_or_else(|| anyhow!("Regex error capturing ssh domain"))?; - let result: String = - "https://".to_string() + &match_domain[1].to_string() + r"/" + &url_parts[7].to_string(); - + let result: String = "https://".to_string() + + &match_domain[1].to_string() + + r"/" + + remove_git_suffix(&url_parts[7]); Ok(result) } @@ -143,4 +151,21 @@ mod tests { let sad_case = "my-proj/src/var/main.rs90"; let _sad_result = parse_path_and_line_arg(sad_case, ':').unwrap(); } + + #[test] + fn test_no_git_suffix() { + let git_repo = "git@git.company.com:project/repo_name"; + let result_url = parse_url_from_git(git_repo).unwrap(); + assert_eq!(result_url, "https://git.company.com/project/repo_name"); + } + + #[test] + fn test_dash_in_org_name() { + let git_repo = "git@git.food-supplier.com:project/repo_name"; + let result_url = parse_url_from_git(git_repo).unwrap(); + assert_eq!( + result_url, + "https://git.food-supplier.com/project/repo_name" + ); + } }