Skip to content

Commit 8ef64d4

Browse files
authored
Add CLI flag to hide token from repository URL (#1319)
1 parent 3e40b97 commit 8ef64d4

File tree

3 files changed

+74
-65
lines changed

3 files changed

+74
-65
lines changed

src/cli.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub struct InfoCliOptions {
8787
/// Display repository URL as HTTP
8888
#[arg(long)]
8989
pub http_url: bool,
90+
/// Hide token in repository URL
91+
#[arg(long)]
92+
pub hide_token: bool,
9093
/// Count hidden files and directories
9194
#[arg(long)]
9295
pub include_hidden: bool,
@@ -247,6 +250,7 @@ impl Default for InfoCliOptions {
247250
no_merges: Default::default(),
248251
email: Default::default(),
249252
http_url: Default::default(),
253+
hide_token: Default::default(),
250254
include_hidden: Default::default(),
251255
r#type: vec![LanguageType::Programming, LanguageType::Markup],
252256
disabled_fields: Vec::default(),

src/info/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ 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, cli_options.info.http_url);
146+
let repo_url = get_repo_url(
147+
&repo,
148+
cli_options.info.hide_token,
149+
cli_options.info.http_url,
150+
);
147151

148152
let git_metrics = traverse_commit_graph(
149153
&repo,

src/info/url.rs

Lines changed: 65 additions & 64 deletions
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, http_url: bool) -> String {
19+
pub fn get_repo_url(repo: &Repository, hide_token: bool, 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,22 @@ pub fn get_repo_url(repo: &Repository, http_url: bool) -> String {
3636
}
3737

3838
match remote_url {
39-
Some(url) => format_url(&url, http_url),
39+
Some(url) => format_url(&url, hide_token, 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
44+
fn format_url(url: &str, hide_token: bool, http_url: bool) -> String {
45+
let formatted_url = if hide_token {
46+
remove_token_from_url(url)
4847
} else {
49-
create_http_url_from_ssh(url)
48+
String::from(url)
49+
};
50+
51+
if http_url && !formatted_url.starts_with("http") {
52+
create_http_url_from_ssh(&formatted_url)
53+
} else {
54+
formatted_url
5055
}
5156
}
5257

@@ -76,6 +81,7 @@ impl InfoField for UrlInfo {
7681
#[cfg(test)]
7782
mod test {
7883
use super::*;
84+
use rstest::rstest;
7985

8086
#[test]
8187
fn test_display_url_info() {
@@ -89,69 +95,64 @@ mod test {
8995
);
9096
}
9197

92-
#[test]
93-
fn test_format_url_http() {
94-
let remote_url_github =
95-
"https://1234567890abcdefghijklmnopqrstuvwxyz@github.com/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:abc123personaltoken@gitlab.com/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);
98+
#[rstest]
99+
#[case(
100+
"https://username:token@github.com/user/repo",
101+
true,
102+
false,
103+
"https://github.com/user/repo"
104+
)]
105+
#[case(
106+
"https://user:token@gitlab.com/user/repo",
107+
true,
108+
false,
109+
"https://gitlab.com/user/repo"
110+
)]
111+
#[case(
112+
"git@github.com:user/repo.git",
113+
false,
114+
true,
115+
"https://github.com/user/repo.git"
116+
)]
117+
#[case(
118+
"git@gitlab.com:user/repo",
119+
false,
120+
true,
121+
"https://gitlab.com/user/repo"
122+
)]
123+
#[case(
124+
"https://github.com/user/repo",
125+
true,
126+
true,
127+
"https://github.com/user/repo"
128+
)]
129+
#[case(
130+
"https://username:token@github.com/user/repo",
131+
false,
132+
false,
133+
"https://username:token@github.com/user/repo"
134+
)]
135+
fn test_format_url(
136+
#[case] url: &str,
137+
#[case] hide_token: bool,
138+
#[case] http_url: bool,
139+
#[case] expected: &str,
140+
) {
141+
assert_eq!(format_url(url, hide_token, http_url), expected);
103142
}
104143

105144
#[test]
106-
fn test_format_url_ssh() {
107-
let remote_url_github = "git@github.com: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-
"git@github.com:0spotter0/onefetch.git",
116-
res_url_github_force_http_false
117-
);
118-
119-
let remote_url_gitlab = "git@gitlab.com: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);
145+
fn test_remove_token_from_url() {
122146
assert_eq!(
123-
"https://gitlab.com/0spotter0/onefetch.git",
124-
res_url_gitlab_force_http_true
147+
remove_token_from_url("https://username:token@github.com/user/repo"),
148+
"https://github.com/user/repo"
125149
);
126-
assert_eq!(
127-
"git@gitlab.com:0spotter0/onefetch.git",
128-
res_url_gitlab_force_http_false
129-
);
130-
}
131-
132-
#[test]
133-
fn test_token_removal_github() {
134-
let remote_url =
135-
"https://1234567890abcdefghijklmnopqrstuvwxyz@github.com/jim4067/onefetch.git";
136-
let res_url = remove_token_from_url(remote_url);
137-
assert_eq!("https://github.com/jim4067/onefetch.git", res_url);
138150
}
139151

140-
#[test]
141-
fn test_token_removal_gitlab() {
142-
let remote_url = "https://john:abc123personaltoken@gitlab.com/jim4067/myproject.git";
143-
let res_url = remove_token_from_url(remote_url);
144-
assert_eq!("https://gitlab.com/jim4067/myproject.git", res_url);
145-
}
146-
147-
#[test]
148-
fn test_create_http_url_from_ssh() {
149-
let remote_url_github = "git@github.com: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 = "git@gitlab.com: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);
152+
#[rstest]
153+
#[case("git@github.com:user/repo.git", "https://github.com/user/repo.git")]
154+
#[case("git@gitlab.com:user/repo", "https://gitlab.com/user/repo")]
155+
fn test_create_http_url_from_ssh(#[case] url: &str, #[case] expected: &str) {
156+
assert_eq!(create_http_url_from_ssh(url), expected);
156157
}
157158
}

0 commit comments

Comments
 (0)