Skip to content

Commit 5000452

Browse files
0spotter0o2sh
andauthored
add cli option --force-url-http for URL display (#1314)
* add cli option --force-url-http for URL display * * 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 * rust fmt --------- Co-authored-by: o2sh <[email protected]>
1 parent 8c1ad74 commit 5000452

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

Diff for: src/cli.rs

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ pub struct InfoCliOptions {
8484
/// Show the email address of each author
8585
#[arg(long, short = 'E')]
8686
pub email: bool,
87+
/// Display repository URL as HTTP
88+
#[arg(long)]
89+
pub http_url: bool,
8790
/// Count hidden files and directories
8891
#[arg(long)]
8992
pub include_hidden: bool,
@@ -243,6 +246,7 @@ impl Default for InfoCliOptions {
243246
no_bots: Option::default(),
244247
no_merges: Default::default(),
245248
email: Default::default(),
249+
http_url: Default::default(),
246250
include_hidden: Default::default(),
247251
r#type: vec![LanguageType::Programming, LanguageType::Markup],
248252
disabled_fields: Vec::default(),

Diff for: src/info/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
143143
.ok()
144144
.context("BUG: panic in language statistics thread")??;
145145
let manifest = get_manifest(&repo_path)?;
146-
let repo_url = get_repo_url(&repo);
146+
let repo_url = get_repo_url(&repo, cli_options.info.http_url);
147147

148148
let git_metrics = traverse_commit_graph(
149149
&repo,

Diff for: src/info/url.rs

+68-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl UrlInfo {
1616
}
1717
}
1818

19-
pub fn get_repo_url(repo: &Repository) -> String {
19+
pub fn get_repo_url(repo: &Repository, http_url: bool) -> String {
2020
let config = repo.config_snapshot();
2121
let remotes = match config.plumbing().sections_by_name("remote") {
2222
Some(sections) => sections,
@@ -36,17 +36,32 @@ pub fn get_repo_url(repo: &Repository) -> String {
3636
}
3737

3838
match remote_url {
39-
Some(url) => remove_token_from_url(&url),
39+
Some(url) => format_url(&url, http_url),
4040
None => String::default(),
4141
}
4242
}
4343

44+
fn format_url(url: &str, http_url: bool) -> String {
45+
let removed_token = remove_token_from_url(&url);
46+
if !http_url || removed_token.starts_with("http") {
47+
removed_token
48+
} else {
49+
create_http_url_from_ssh(url)
50+
}
51+
}
52+
4453
fn remove_token_from_url(url: &str) -> String {
4554
let pattern = Regex::new(r"(https?://)([^@]+@)").unwrap();
4655
let replaced_url = pattern.replace(url, "$1").to_string();
4756
replaced_url
4857
}
4958

59+
fn create_http_url_from_ssh(url: &str) -> String {
60+
let pattern = Regex::new(r"([^@]+)@([^:]+):(.*)").unwrap();
61+
let replaced_url = pattern.replace(url, "https://${2}/${3}").to_string();
62+
replaced_url
63+
}
64+
5065
#[typetag::serialize]
5166
impl InfoField for UrlInfo {
5267
fn value(&self) -> String {
@@ -74,6 +89,46 @@ mod test {
7489
);
7590
}
7691

92+
#[test]
93+
fn test_format_url_http() {
94+
let remote_url_github =
95+
"https://[email protected]/0spotter0/onefetch.git";
96+
let res_url_github = format_url(remote_url_github, true);
97+
assert_eq!("https://github.com/0spotter0/onefetch.git", res_url_github);
98+
99+
let remote_url_gitlab =
100+
"https://john:[email protected]/0spotter0/onefetch.git";
101+
let res_url_gitlab = format_url(remote_url_gitlab, true);
102+
assert_eq!("https://gitlab.com/0spotter0/onefetch.git", res_url_gitlab);
103+
}
104+
105+
#[test]
106+
fn test_format_url_ssh() {
107+
let remote_url_github = "[email protected]:0spotter0/onefetch.git";
108+
let res_url_github_force_http_true = format_url(remote_url_github, true);
109+
let res_url_github_force_http_false = format_url(remote_url_github, false);
110+
assert_eq!(
111+
"https://github.com/0spotter0/onefetch.git",
112+
res_url_github_force_http_true
113+
);
114+
assert_eq!(
115+
"[email protected]:0spotter0/onefetch.git",
116+
res_url_github_force_http_false
117+
);
118+
119+
let remote_url_gitlab = "[email protected]:0spotter0/onefetch.git";
120+
let res_url_gitlab_force_http_true = format_url(remote_url_gitlab, true);
121+
let res_url_gitlab_force_http_false = format_url(remote_url_gitlab, false);
122+
assert_eq!(
123+
"https://gitlab.com/0spotter0/onefetch.git",
124+
res_url_gitlab_force_http_true
125+
);
126+
assert_eq!(
127+
"[email protected]:0spotter0/onefetch.git",
128+
res_url_gitlab_force_http_false
129+
);
130+
}
131+
77132
#[test]
78133
fn test_token_removal_github() {
79134
let remote_url =
@@ -88,4 +143,15 @@ mod test {
88143
let res_url = remove_token_from_url(remote_url);
89144
assert_eq!("https://gitlab.com/jim4067/myproject.git", res_url);
90145
}
146+
147+
#[test]
148+
fn test_create_http_url_from_ssh() {
149+
let remote_url_github = "[email protected]:0spotter0/onefetch.git";
150+
let res_url_github = create_http_url_from_ssh(remote_url_github);
151+
assert_eq!("https://github.com/0spotter0/onefetch.git", res_url_github);
152+
153+
let remote_url_gitlab = "[email protected]:0spotter0/onefetch.git";
154+
let res_url_gitlab = create_http_url_from_ssh(remote_url_gitlab);
155+
assert_eq!("https://gitlab.com/0spotter0/onefetch.git", res_url_gitlab);
156+
}
91157
}

0 commit comments

Comments
 (0)