1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
use crate::prelude::*; #[derive(PartialEq, Debug, Default)] pub struct GitUrlComponents { pub domain: String, pub username: String, pub repo: String, pub suffix: String, } pub fn parse_git_url_https(http_url: &str) -> Option<GitUrlComponents> { let mut split: Vec<_> = http_url.split('/').collect(); while let Some(&"") = split.last() { split.pop(); } if split.len() != 5 { return None; } if split[0] != "https:" && split[0] != "http:" { return None; } let domain = split[2]; let username = split[3]; let repo = split[4]; let suffix = if repo.ends_with(".git") { "" } else { ".git" }; Some(GitUrlComponents { domain: domain.to_string(), username: username.to_string(), repo: repo.to_string(), suffix: suffix.to_string(), }) } pub fn fetch_and_checkout_git_repo(repo: &git2::Repository) -> Result<()> { repo.find_remote("origin")?.fetch(&["master"], None, None)?; repo.set_head("FETCH_HEAD")?; let mut opts = git2::build::CheckoutBuilder::new(); opts.force(); repo.checkout_head(Some(&mut opts))?; Ok(()) } #[test] fn parse_git_url_https_test() { assert_eq!( parse_git_url_https("https://github.com/dpc/trust"), Some(GitUrlComponents { domain: "github.com".to_string(), username: "dpc".to_string(), repo: "trust".to_string(), suffix: ".git".to_string() }) ); assert_eq!( parse_git_url_https("https://gitlab.com/hackeraudit/web.git"), Some(GitUrlComponents { domain: "gitlab.com".to_string(), username: "hackeraudit".to_string(), repo: "web.git".to_string(), suffix: "".to_string() }) ); assert_eq!( parse_git_url_https("https://gitlab.com/hackeraudit/web.git/"), Some(GitUrlComponents { domain: "gitlab.com".to_string(), username: "hackeraudit".to_string(), repo: "web.git".to_string(), suffix: "".to_string() }) ); assert_eq!( parse_git_url_https("https://gitlab.com/hackeraudit/web.git/////////"), Some(GitUrlComponents { domain: "gitlab.com".to_string(), username: "hackeraudit".to_string(), repo: "web.git".to_string(), suffix: "".to_string() }) ); } pub fn https_to_git_url(http_url: &str) -> Option<String> { parse_git_url_https(http_url).map(|components| { format!( "git@{}:{}/{}{}", components.domain, components.username, components.repo, components.suffix ) }) } #[test] fn https_to_git_url_test() { assert_eq!( https_to_git_url("https://github.com/dpc/trust"), Some("git@github.com:dpc/trust.git".into()) ); assert_eq!( https_to_git_url("https://gitlab.com/hackeraudit/web.git"), Some("git@gitlab.com:hackeraudit/web.git".into()) ); assert_eq!( https_to_git_url("https://gitlab.com/hackeraudit/web.git/"), Some("git@gitlab.com:hackeraudit/web.git".into()) ); assert_eq!( https_to_git_url("https://gitlab.com/hackeraudit/web.git/////////"), Some("git@gitlab.com:hackeraudit/web.git".into()) ); }