Skip to content

Commit

Permalink
Fix Repo address should be parsed even without .git at the end (#11)
Browse files Browse the repository at this point in the history
* Make git suffix optional, add dashes matching in domain name, add tests

* Bump version for the fix
  • Loading branch information
oren0e authored Feb 19, 2023
1 parent 39d2e04 commit 5dd38a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gitopen"
version = "1.3.1"
version = "1.3.2"
authors = ["Oren Epshtain"]
edition = "2018"
license-file = "LICENSE"
Expand Down
33 changes: 29 additions & 4 deletions src/match_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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"))?;
Expand All @@ -38,9 +45,10 @@ pub fn parse_url_from_git(s: &str) -> AnyhowResult<String> {
.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)
}

Expand Down Expand Up @@ -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 = "[email protected]: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 = "[email protected]: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"
);
}
}

0 comments on commit 5dd38a2

Please sign in to comment.