diff options
author | Dan Davison <dandavison7@gmail.com> | 2021-04-24 09:59:11 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-24 09:59:11 -0400 |
commit | 9f2f44d8777352a2abaec9b6f643cc56593bbe38 (patch) | |
tree | 00d38509866b9e47aa674909ca483b80c72a321c | |
parent | 86479d420f014fa63c717d8854fca33d5d6b6361 (diff) | |
parent | b1234ba4e8934cd133e6777b909c69608626bce7 (diff) |
Merge pull request #572 from dandavison/fix-github-url-parsing
Fix GitHub url parsing
-rw-r--r-- | src/git_config/git_config_entry.rs | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/git_config/git_config_entry.rs b/src/git_config/git_config_entry.rs index 4b0d80a3..2dcb8783 100644 --- a/src/git_config/git_config_entry.rs +++ b/src/git_config/git_config_entry.rs @@ -14,13 +14,26 @@ pub enum GitConfigEntry { Path(PathBuf), } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub enum GitRemoteRepo { GitHubRepo(String), } lazy_static! { - static ref GITHUB_REMOTE_URL: Regex = Regex::new(r"github\.com[:/]([^/]+)/(.+)").unwrap(); + static ref GITHUB_REMOTE_URL: Regex = Regex::new( + r"(?x) + ^ + (?:https://|git@) # Support both HTTPS and SSH URLs + github\.com + [:/] # This separator differs between SSH and HTTPS URLs + ([^/]+) # Capture the user/org name + / + (.+?) # Capture the repo name (lazy to avoid consuming '.git' if present) + (?:\.git)? # Non-capturing group to consume '.git' if present + $ + " + ) + .unwrap(); } impl FromStr for GitRemoteRepo { @@ -37,3 +50,26 @@ impl FromStr for GitRemoteRepo { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_github_urls() { + let urls = &[ + "https://github.com/dandavison/delta.git", + "https://github.com/dandavison/delta", + "git@github.com:dandavison/delta.git", + "git@github.com:dandavison/delta", + ]; + for url in urls { + let parsed = GitRemoteRepo::from_str(url); + assert!(parsed.is_ok()); + assert_eq!( + parsed.unwrap(), + GitRemoteRepo::GitHubRepo("dandavison/delta".to_string()) + ); + } + } +} |