From 5904801e20632f5e90b2f7ae50a3554b0541cb60 Mon Sep 17 00:00:00 2001 From: sammypotter <50476978+0spotter0@users.noreply.github.com> Date: Tue, 23 Apr 2024 00:51:20 +0200 Subject: [PATCH 1/3] add cli option --force-url-http for URL display --- src/cli.rs | 4 ++++ src/info/mod.rs | 2 +- src/info/url.rs | 45 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 67b9dca09..19ac1e0cd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -84,6 +84,9 @@ pub struct InfoCliOptions { /// Show the email address of each author #[arg(long, short = 'E')] pub email: bool, + /// Always display the repo url as http + #[arg(long)] + pub force_url_http: bool, /// Count hidden files and directories #[arg(long)] pub include_hidden: bool, @@ -243,6 +246,7 @@ impl Default for InfoCliOptions { no_bots: Option::default(), no_merges: Default::default(), email: Default::default(), + force_url_http: Default::default(), include_hidden: Default::default(), r#type: vec![LanguageType::Programming, LanguageType::Markup], disabled_fields: Vec::default(), diff --git a/src/info/mod.rs b/src/info/mod.rs index 49e5f9160..e96c30baa 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -143,7 +143,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result { .ok() .context("BUG: panic in language statistics thread")??; let manifest = get_manifest(&repo_path)?; - let repo_url = get_repo_url(&repo); + let repo_url = get_repo_url(&repo, cli_options.info.force_url_http); let git_metrics = traverse_commit_graph( &repo, diff --git a/src/info/url.rs b/src/info/url.rs index d43237acb..95c1a1825 100644 --- a/src/info/url.rs +++ b/src/info/url.rs @@ -16,7 +16,7 @@ impl UrlInfo { } } -pub fn get_repo_url(repo: &Repository) -> String { +pub fn get_repo_url(repo: &Repository, force_url_http: bool) -> String { let config = repo.config_snapshot(); let remotes = match config.plumbing().sections_by_name("remote") { Some(sections) => sections, @@ -36,17 +36,32 @@ pub fn get_repo_url(repo: &Repository) -> String { } match remote_url { - Some(url) => remove_token_from_url(&url), + Some(url) => format_url(&url, force_url_http), None => String::default(), } } +fn format_url(url: &str, force_url_http: bool) -> String { + let removed_token = remove_token_from_url(&url); + if !force_url_http || removed_token.starts_with("http") { + removed_token + } else { + create_http_url(url) + } +} + fn remove_token_from_url(url: &str) -> String { let pattern = Regex::new(r"(https?://)([^@]+@)").unwrap(); let replaced_url = pattern.replace(url, "$1").to_string(); replaced_url } +fn create_http_url(url: &str) -> String { + let pattern = Regex::new(r"([^@]+)@([^:]+):(.*)").unwrap(); + let replaced_url = pattern.replace(url, "https://${2}/${3}").to_string(); + replaced_url +} + #[typetag::serialize] impl InfoField for UrlInfo { fn value(&self) -> String { @@ -74,6 +89,32 @@ mod test { ); } + #[test] + fn test_format_url_http() { + let remote_url_github = "https://1234567890abcdefghijklmnopqrstuvwxyz@github.com/0spotter0/onefetch.git"; + let res_url_github = format_url(remote_url_github, true); + assert_eq!("https://github.com/0spotter0/onefetch.git", res_url_github); + + let remote_url_gitlab = "https://john:abc123personaltoken@gitlab.com/0spotter0/onefetch.git"; + let res_url_gitlab = format_url(remote_url_gitlab, true); + assert_eq!("https://gitlab.com/0spotter0/onefetch.git", res_url_gitlab); + } + + #[test] + fn test_format_url_ssh() { + let remote_url_github = "git@github.com:0spotter0/onefetch.git"; + let res_url_github_force_http_true = format_url(remote_url_github, true); + let res_url_github_force_http_false = format_url(remote_url_github, false); + assert_eq!("https://github.com/0spotter0/onefetch.git", res_url_github_force_http_true); + assert_eq!("git@github.com:0spotter0/onefetch.git", res_url_github_force_http_false); + + let remote_url_gitlab = "git@gitlab.com:0spotter0/onefetch.git"; + let res_url_gitlab_force_http_true = format_url(remote_url_gitlab, true); + let res_url_gitlab_force_http_false = format_url(remote_url_gitlab, false); + assert_eq!("https://gitlab.com/0spotter0/onefetch.git", res_url_gitlab_force_http_true); + assert_eq!("git@gitlab.com:0spotter0/onefetch.git", res_url_gitlab_force_http_false); + } + #[test] fn test_token_removal_github() { let remote_url = From 6e95aa6b4bf2d4e40c2477560da5dea89a1b933e Mon Sep 17 00:00:00 2001 From: sammypotter <50476978+0spotter0@users.noreply.github.com> Date: Sat, 27 Apr 2024 23:26:41 +0100 Subject: [PATCH 2/3] * rename force_url_http to http_url * rename create_http_url to create_http_url_from_ssh * add test for create_http_url_from_ssh --- src/cli.rs | 6 +++--- src/info/mod.rs | 2 +- src/info/url.rs | 23 +++++++++++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 19ac1e0cd..4893b9df0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -84,9 +84,9 @@ pub struct InfoCliOptions { /// Show the email address of each author #[arg(long, short = 'E')] pub email: bool, - /// Always display the repo url as http + /// Display repository URL as HTTP #[arg(long)] - pub force_url_http: bool, + pub http_url: bool, /// Count hidden files and directories #[arg(long)] pub include_hidden: bool, @@ -246,7 +246,7 @@ impl Default for InfoCliOptions { no_bots: Option::default(), no_merges: Default::default(), email: Default::default(), - force_url_http: Default::default(), + http_url: Default::default(), include_hidden: Default::default(), r#type: vec![LanguageType::Programming, LanguageType::Markup], disabled_fields: Vec::default(), diff --git a/src/info/mod.rs b/src/info/mod.rs index e96c30baa..11536ec21 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -143,7 +143,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result { .ok() .context("BUG: panic in language statistics thread")??; let manifest = get_manifest(&repo_path)?; - let repo_url = get_repo_url(&repo, cli_options.info.force_url_http); + let repo_url = get_repo_url(&repo, cli_options.info.http_url); let git_metrics = traverse_commit_graph( &repo, diff --git a/src/info/url.rs b/src/info/url.rs index 95c1a1825..2e47ada9e 100644 --- a/src/info/url.rs +++ b/src/info/url.rs @@ -16,7 +16,7 @@ impl UrlInfo { } } -pub fn get_repo_url(repo: &Repository, force_url_http: bool) -> String { +pub fn get_repo_url(repo: &Repository, http_url: bool) -> String { let config = repo.config_snapshot(); let remotes = match config.plumbing().sections_by_name("remote") { Some(sections) => sections, @@ -36,17 +36,17 @@ pub fn get_repo_url(repo: &Repository, force_url_http: bool) -> String { } match remote_url { - Some(url) => format_url(&url, force_url_http), + Some(url) => format_url(&url, http_url), None => String::default(), } } -fn format_url(url: &str, force_url_http: bool) -> String { +fn format_url(url: &str, http_url: bool) -> String { let removed_token = remove_token_from_url(&url); - if !force_url_http || removed_token.starts_with("http") { + if !http_url || removed_token.starts_with("http") { removed_token } else { - create_http_url(url) + create_http_url_from_ssh(url) } } @@ -56,7 +56,7 @@ fn remove_token_from_url(url: &str) -> String { replaced_url } -fn create_http_url(url: &str) -> String { +fn create_http_url_from_ssh(url: &str) -> String { let pattern = Regex::new(r"([^@]+)@([^:]+):(.*)").unwrap(); let replaced_url = pattern.replace(url, "https://${2}/${3}").to_string(); replaced_url @@ -129,4 +129,15 @@ mod test { let res_url = remove_token_from_url(remote_url); assert_eq!("https://gitlab.com/jim4067/myproject.git", res_url); } + + #[test] + fn test_create_http_url_from_ssh() { + let remote_url_github = "git@github.com:0spotter0/onefetch.git"; + let res_url_github = create_http_url_from_ssh(remote_url_github); + assert_eq!("https://github.com/0spotter0/onefetch.git", res_url_github); + + let remote_url_gitlab = "git@gitlab.com:0spotter0/onefetch.git"; + let res_url_gitlab = create_http_url_from_ssh(remote_url_gitlab); + assert_eq!("https://gitlab.com/0spotter0/onefetch.git", res_url_gitlab); + } } From 3cc73abc4eafa7936f1fc7a62109e7af077e091f Mon Sep 17 00:00:00 2001 From: o2sh Date: Sun, 28 Apr 2024 11:43:44 +0200 Subject: [PATCH 3/3] rust fmt --- src/info/url.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/info/url.rs b/src/info/url.rs index 2e47ada9e..c68c924b8 100644 --- a/src/info/url.rs +++ b/src/info/url.rs @@ -91,11 +91,13 @@ mod test { #[test] fn test_format_url_http() { - let remote_url_github = "https://1234567890abcdefghijklmnopqrstuvwxyz@github.com/0spotter0/onefetch.git"; + let remote_url_github = + "https://1234567890abcdefghijklmnopqrstuvwxyz@github.com/0spotter0/onefetch.git"; let res_url_github = format_url(remote_url_github, true); assert_eq!("https://github.com/0spotter0/onefetch.git", res_url_github); - let remote_url_gitlab = "https://john:abc123personaltoken@gitlab.com/0spotter0/onefetch.git"; + let remote_url_gitlab = + "https://john:abc123personaltoken@gitlab.com/0spotter0/onefetch.git"; let res_url_gitlab = format_url(remote_url_gitlab, true); assert_eq!("https://gitlab.com/0spotter0/onefetch.git", res_url_gitlab); } @@ -105,14 +107,26 @@ mod test { let remote_url_github = "git@github.com:0spotter0/onefetch.git"; let res_url_github_force_http_true = format_url(remote_url_github, true); let res_url_github_force_http_false = format_url(remote_url_github, false); - assert_eq!("https://github.com/0spotter0/onefetch.git", res_url_github_force_http_true); - assert_eq!("git@github.com:0spotter0/onefetch.git", res_url_github_force_http_false); + assert_eq!( + "https://github.com/0spotter0/onefetch.git", + res_url_github_force_http_true + ); + assert_eq!( + "git@github.com:0spotter0/onefetch.git", + res_url_github_force_http_false + ); let remote_url_gitlab = "git@gitlab.com:0spotter0/onefetch.git"; let res_url_gitlab_force_http_true = format_url(remote_url_gitlab, true); let res_url_gitlab_force_http_false = format_url(remote_url_gitlab, false); - assert_eq!("https://gitlab.com/0spotter0/onefetch.git", res_url_gitlab_force_http_true); - assert_eq!("git@gitlab.com:0spotter0/onefetch.git", res_url_gitlab_force_http_false); + assert_eq!( + "https://gitlab.com/0spotter0/onefetch.git", + res_url_gitlab_force_http_true + ); + assert_eq!( + "git@gitlab.com:0spotter0/onefetch.git", + res_url_gitlab_force_http_false + ); } #[test]