gr_bin/git/
url.rs

1use eyre::{eyre, ContextCompat, Result};
2use tracing::{debug, instrument};
3
4#[instrument(skip_all)]
5pub fn parse_url(url: &str) -> Result<(String, String)> {
6    debug!("Parsing URL {url}.");
7    if let Some((first, rest)) = url.split_once(':') {
8        // Split host and path based on protocol
9        let (host, path) = match first {
10            "http" | "https" | "ssh" | "git" => rest[2..]
11                .split_once('/')
12                .wrap_err("URL should contain a path."),
13            "ftp" | "ftps" => Err(eyre!("FTP protocol is not supported.")),
14            _ => Ok((first, rest)),
15        }?;
16
17        // Remove user from host if it exists
18        let host = host.split_once('@').map_or(host, |(_, h)| h);
19
20        // Remove ".git" from end if it is there
21        let path = path.split_once(".git").map_or(path, |(p, _)| p);
22
23        debug!("Parsed remote URL to host {host} and path {path}.");
24
25        Ok((host.to_string(), path.to_string()))
26    } else {
27        Err(eyre!("Local directories are not supported."))
28    }
29}