From 62741cc682448f50aaaf5be198d6eeafda37c3d8 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 24 Apr 2021 07:04:53 -0400 Subject: Add failing tests for remote repo string parsing --- src/git_config/git_config_entry.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/git_config/git_config_entry.rs b/src/git_config/git_config_entry.rs index 4b0d80a3..bee64149 100644 --- a/src/git_config/git_config_entry.rs +++ b/src/git_config/git_config_entry.rs @@ -14,7 +14,7 @@ pub enum GitConfigEntry { Path(PathBuf), } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub enum GitRemoteRepo { GitHubRepo(String), } @@ -37,3 +37,28 @@ impl FromStr for GitRemoteRepo { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_github_url_with_dot_git_suffix() { + let parsed = GitRemoteRepo::from_str("git@github.com:dandavison/delta.git"); + assert!(parsed.is_ok()); + assert_eq!( + parsed.unwrap(), + GitRemoteRepo::GitHubRepo("dandavison/delta".to_string()) + ); + } + + #[test] + fn test_parse_github_url_without_dot_git_suffix() { + let parsed = GitRemoteRepo::from_str("git@github.com:dandavison/delta"); + assert!(parsed.is_ok()); + assert_eq!( + parsed.unwrap(), + GitRemoteRepo::GitHubRepo("dandavison/delta".to_string()) + ); + } +} -- cgit v1.2.3 From 9a218b27c761daecbfdaddcefaf0393fee8959ad Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 24 Apr 2021 07:38:46 -0400 Subject: Fix Github repo URL regex Ref #563 --- src/git_config/git_config_entry.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/git_config/git_config_entry.rs b/src/git_config/git_config_entry.rs index bee64149..bf324ef6 100644 --- a/src/git_config/git_config_entry.rs +++ b/src/git_config/git_config_entry.rs @@ -20,7 +20,8 @@ pub enum GitRemoteRepo { } lazy_static! { - static ref GITHUB_REMOTE_URL: Regex = Regex::new(r"github\.com[:/]([^/]+)/(.+)").unwrap(); + static ref GITHUB_REMOTE_URL: Regex = + Regex::new(r"github\.com[:/]([^/]+)/(.+?)(?:\.git)?$").unwrap(); } impl FromStr for GitRemoteRepo { -- cgit v1.2.3 From 9b13f7bd77f74cbe5c54378eac065659b39849da Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 24 Apr 2021 07:55:02 -0400 Subject: Test HTTPS and SSH Github URLs --- src/git_config/git_config_entry.rs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/git_config/git_config_entry.rs b/src/git_config/git_config_entry.rs index bf324ef6..f8c63c69 100644 --- a/src/git_config/git_config_entry.rs +++ b/src/git_config/git_config_entry.rs @@ -44,22 +44,20 @@ mod tests { use super::*; #[test] - fn test_parse_github_url_with_dot_git_suffix() { - let parsed = GitRemoteRepo::from_str("git@github.com:dandavison/delta.git"); - assert!(parsed.is_ok()); - assert_eq!( - parsed.unwrap(), - GitRemoteRepo::GitHubRepo("dandavison/delta".to_string()) - ); - } - - #[test] - fn test_parse_github_url_without_dot_git_suffix() { - let parsed = GitRemoteRepo::from_str("git@github.com:dandavison/delta"); - assert!(parsed.is_ok()); - assert_eq!( - parsed.unwrap(), - GitRemoteRepo::GitHubRepo("dandavison/delta".to_string()) - ); + 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()) + ); + } } } -- cgit v1.2.3 From b1234ba4e8934cd133e6777b909c69608626bce7 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 24 Apr 2021 07:55:19 -0400 Subject: Implement tested feature: support HTTPs and SSH URLs --- src/git_config/git_config_entry.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/git_config/git_config_entry.rs b/src/git_config/git_config_entry.rs index f8c63c69..2dcb8783 100644 --- a/src/git_config/git_config_entry.rs +++ b/src/git_config/git_config_entry.rs @@ -20,8 +20,20 @@ pub enum GitRemoteRepo { } lazy_static! { - static ref GITHUB_REMOTE_URL: Regex = - Regex::new(r"github\.com[:/]([^/]+)/(.+?)(?:\.git)?$").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 { -- cgit v1.2.3