@@ -16,7 +16,7 @@ impl UrlInfo {
16
16
}
17
17
}
18
18
19
- pub fn get_repo_url ( repo : & Repository ) -> String {
19
+ pub fn get_repo_url ( repo : & Repository , http_url : bool ) -> String {
20
20
let config = repo. config_snapshot ( ) ;
21
21
let remotes = match config. plumbing ( ) . sections_by_name ( "remote" ) {
22
22
Some ( sections) => sections,
@@ -36,17 +36,32 @@ pub fn get_repo_url(repo: &Repository) -> String {
36
36
}
37
37
38
38
match remote_url {
39
- Some ( url) => remove_token_from_url ( & url) ,
39
+ Some ( url) => format_url ( & url, http_url ) ,
40
40
None => String :: default ( ) ,
41
41
}
42
42
}
43
43
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
+
44
53
fn remove_token_from_url ( url : & str ) -> String {
45
54
let pattern = Regex :: new ( r"(https?://)([^@]+@)" ) . unwrap ( ) ;
46
55
let replaced_url = pattern. replace ( url, "$1" ) . to_string ( ) ;
47
56
replaced_url
48
57
}
49
58
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
+
50
65
#[ typetag:: serialize]
51
66
impl InfoField for UrlInfo {
52
67
fn value ( & self ) -> String {
@@ -74,6 +89,46 @@ mod test {
74
89
) ;
75
90
}
76
91
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
+
77
132
#[ test]
78
133
fn test_token_removal_github ( ) {
79
134
let remote_url =
@@ -88,4 +143,15 @@ mod test {
88
143
let res_url = remove_token_from_url ( remote_url) ;
89
144
assert_eq ! ( "https://gitlab.com/jim4067/myproject.git" , res_url) ;
90
145
}
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
+ }
91
157
}
0 commit comments