Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cli option --force-url-http for URL display #1314

Merged
merged 3 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Always display the repo url as http
#[arg(long)]
pub force_url_http: bool,
/// Display repository URL as HTTP
#[arg(long)]
pub http_url: bool,

/// Count hidden files and directories
#[arg(long)]
pub include_hidden: bool,
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
.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,
Expand Down
45 changes: 43 additions & 2 deletions src/info/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn create_http_url(url: &str) -> String {
fn create_http_url_from_ssh(url: &str) -> String {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a unit test specifically for this method

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 {
Expand Down Expand Up @@ -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 =
Expand Down
Loading