@@ -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+
4453fn 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]
5166impl 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://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) ;
103+ }
104+
105+ #[ 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 ) ;
122+ assert_eq ! (
123+ "https://gitlab.com/0spotter0/onefetch.git" ,
124+ res_url_gitlab_force_http_true
125+ ) ;
126+ assert_eq ! (
127+ "git@gitlab.com: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 = "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) ;
156+ }
91157}
0 commit comments