1- use anyhow:: Result ;
2- use gix:: Repository ;
3- use regex:: regex;
41use serde:: Serialize ;
52
63#[ derive( Serialize ) ]
@@ -15,100 +12,3 @@ impl Url {
1512 }
1613 }
1714}
18-
19- pub fn get_repo_url ( repo : & Repository , hide_token : bool , http_url : bool ) -> Result < String > {
20- let remote = match repo. find_default_remote ( gix:: remote:: Direction :: Fetch ) {
21- Some ( remote) => remote?,
22- None => return Ok ( String :: new ( ) ) ,
23- } ;
24-
25- Ok ( remote
26- . url ( gix:: remote:: Direction :: Fetch )
27- . map ( |url| format_url ( & url. to_string ( ) , hide_token, http_url) )
28- . unwrap_or_default ( ) )
29- }
30-
31- fn format_url ( url : & str , hide_token : bool , http_url : bool ) -> String {
32- let formatted_url = if hide_token {
33- remove_token_from_url ( url)
34- } else {
35- String :: from ( url)
36- } ;
37-
38- if http_url && !formatted_url. starts_with ( "http" ) {
39- create_http_url_from_ssh ( & formatted_url)
40- } else {
41- formatted_url
42- }
43- }
44-
45- fn remove_token_from_url ( url : & str ) -> String {
46- regex ! ( r"(https?://)([^@]+@)" )
47- . replace ( url, "$1" )
48- . to_string ( )
49- }
50-
51- fn create_http_url_from_ssh ( url : & str ) -> String {
52- regex ! ( r"([^@]+)@([^:]+):(.*)" )
53- . replace ( url, "https://${2}/${3}" )
54- . to_string ( )
55- }
56-
57- #[ cfg( test) ]
58- mod test {
59- use super :: * ;
60- use rstest:: rstest;
61-
62- #[ rstest]
63- #[ case(
64- "https://username:token@github.com/user/repo" ,
65- true ,
66- false ,
67- "https://github.com/user/repo"
68- ) ]
69- #[ case(
70- "https://user:token@gitlab.com/user/repo" ,
71- true ,
72- false ,
73- "https://gitlab.com/user/repo"
74- ) ]
75- #[ case(
76- "git@github.com:user/repo.git" ,
77- false ,
78- true ,
79- "https://github.com/user/repo.git"
80- ) ]
81- #[ case(
82- "git@gitlab.com:user/repo" ,
83- false ,
84- true ,
85- "https://gitlab.com/user/repo"
86- ) ]
87- #[ case(
88- "https://github.com/user/repo" ,
89- true ,
90- true ,
91- "https://github.com/user/repo"
92- ) ]
93- #[ case(
94- "https://username:token@github.com/user/repo" ,
95- false ,
96- false ,
97- "https://username:token@github.com/user/repo"
98- ) ]
99- fn test_format_url (
100- #[ case] url : & str ,
101- #[ case] hide_token : bool ,
102- #[ case] http_url : bool ,
103- #[ case] expected : & str ,
104- ) {
105- assert_eq ! ( format_url( url, hide_token, http_url) , expected) ;
106- }
107-
108- #[ rstest]
109- #[ case( "git@github.com:user/repo.git" , "https://github.com/user/repo.git" ) ]
110- #[ case( "git@gitlab.com:user/repo" , "https://gitlab.com/user/repo" ) ]
111- fn test_create_http_url_from_ssh ( #[ case] url : & str , #[ case] expected : & str ) {
112- assert_eq ! ( create_http_url_from_ssh( url) , expected) ;
113- }
114- }
0 commit comments