Skip to content

Commit

Permalink
Fix extract_host to handle SSH URLs that do not start with git@
Browse files Browse the repository at this point in the history
  • Loading branch information
gifnksm authored and davidB committed Sep 3, 2023
1 parent 63bad27 commit f0fb8f7
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,23 @@ fn extract_host(url: &str) -> Result<Option<String>, git2::Error> {
let url_re2 = regex::Regex::new(
r"^(https?|ssh)://([[:alnum:]:\._-]+@)?(?P<host>[[:alnum:]\._-]+)(:\d+)?/(?P<path>[[:alnum:]\._\-/]+)$",
).map_err(|source| git2::Error::from_str(&format!("failed to parse url '{}': {:#?}", url, source)))?;
let git_re =
regex::Regex::new(r"^git@(?P<host>[[:alnum:]\._-]+):(?P<path>[[:alnum:]\._\-/]+).git$")
.map_err(|source| {
git2::Error::from_str(&format!("failed to parse url '{}': {:#?}", url, source))
})?;
let git_re2 =
regex::Regex::new(r"^git@(?P<host>[[:alnum:]\._-]+):(?P<path>[[:alnum:]\._\-/]+)$")
.map_err(|source| {
git2::Error::from_str(&format!("failed to parse url '{}': {:#?}", url, source))
})?;
Ok(git_re
let git_re = regex::Regex::new(
r"^([[:alnum:]:\._-]+@)?(?P<host>[[:alnum:]\._-]+):(?P<path>[[:alnum:]\._\-/]+).git$",
)
.map_err(|source| {
git2::Error::from_str(&format!("failed to parse url '{}': {:#?}", url, source))
})?;
let git_re2 = regex::Regex::new(
r"^([[:alnum:]:\._-]+@)?(?P<host>[[:alnum:]\._-]+):(?P<path>[[:alnum:]\._\-/]+)$",
)
.map_err(|source| {
git2::Error::from_str(&format!("failed to parse url '{}': {:#?}", url, source))
})?;
Ok(url_re
.captures(url)
.or_else(|| git_re2.captures(url))
.or_else(|| url_re.captures(url))
.or_else(|| url_re2.captures(url))
.or_else(|| git_re.captures(url))
.or_else(|| git_re2.captures(url))
.map(|caps| caps["host"].to_string()))
}

Expand All @@ -283,6 +285,18 @@ mod tests {
extract_host("https://github.com/davidB/git2_credentials.git"),
Ok(Some("github.com".to_string()))
);
assert_eq!(
extract_host("ssh://aur@aur.archlinux.org/souko.git"),
Ok(Some("aur.archlinux.org".to_string()))
);
assert_eq!(
extract_host("aur@aur.archlinux.org:souko.git"),
Ok(Some("aur.archlinux.org".to_string()))
);
assert_eq!(
extract_host("aur.archlinux.org:souko.git"),
Ok(Some("aur.archlinux.org".to_string()))
);
Ok(())
}
}

0 comments on commit f0fb8f7

Please sign in to comment.