use gitpoulpi_config::Integration;
use crate::spec::RemoteRef;
pub(super) fn parse_remote(remote: &str) -> Option<RemoteRef> {
let remote = remote.trim();
if remote.is_empty() {
return None;
}
let (scheme, host, port, path) = split(remote)?;
let host = host.to_ascii_lowercase();
let segments: Vec<String> = path
.split('/')
.filter(|s| !s.is_empty())
.map(decode)
.collect();
let services = |org: &str, rest: &[String]| -> Option<RemoteRef> {
let (project, repo) = project_and_repo(rest)?;
(!org.is_empty()).then(|| RemoteRef {
web_base: format!("https://dev.azure.com/{org}"),
project: format!("{project}/{repo}"),
})
};
if host == "dev.azure.com" {
let (org, rest) = segments.split_first()?;
return services(org, rest);
}
if host == "ssh.dev.azure.com" || host == "vs-ssh.visualstudio.com" {
let rest = segments.strip_prefix(&["v3".to_string()][..])?;
let (org, rest) = rest.split_first()?;
let (project, repo) = match rest {
[project, repo] => (project, repo),
_ => return None,
};
return services(org, &[project.clone(), "_git".into(), repo.clone()]);
}
if let Some(org) = host.strip_suffix(".visualstudio.com") {
if org.contains('.') {
return None;
}
let rest = match segments.first() {
Some(first) if first.eq_ignore_ascii_case("DefaultCollection") => &segments[1..],
_ => &segments[..],
};
return services(org, rest);
}
let git = segments.iter().position(|s| s == "_git")?;
let repo = segments.get(git + 1)?.clone();
let prefix = &segments[..git];
let (collection, project) = match prefix {
[] => return None,
[only] => (vec![only.clone()], repo.clone()),
[first, second] if first.eq_ignore_ascii_case("tfs") => {
(vec![first.clone(), second.clone()], repo.clone())
}
_ => (
prefix[..prefix.len() - 1].to_vec(),
prefix[prefix.len() - 1].clone(),
),
};
let web_scheme = if scheme == "http" { "http" } else { "https" };
let authority = match (scheme.as_str(), port) {
("http" | "https", Some(port)) => format!("{host}:{port}"),
_ => host.clone(),
};
Some(RemoteRef {
web_base: format!("{web_scheme}://{authority}/{}", collection.join("/")),
project: format!("{project}/{repo}"),
})
}
fn project_and_repo(rest: &[String]) -> Option<(String, String)> {
match rest {
[project, git, repo] if git == "_git" => Some((project.clone(), repo.clone())),
[git, repo] if git == "_git" => Some((repo.clone(), repo.clone())),
_ => None,
}
}
fn split(remote: &str) -> Option<(String, String, Option<String>, String)> {
if let Some((scheme, rest)) = remote.split_once("://") {
let (authority, path) = rest.split_once('/').unwrap_or((rest, ""));
let authority = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
let (host, port) = match authority.rsplit_once(':') {
Some((h, p)) if !p.is_empty() && p.chars().all(|c| c.is_ascii_digit()) => {
(h, Some(p.to_string()))
}
_ => (authority, None),
};
return (!host.is_empty()).then(|| {
(
scheme.to_ascii_lowercase(),
host.to_string(),
port,
path.to_string(),
)
});
}
let after_user = remote.rsplit_once('@').map_or(remote, |(_, r)| r);
let (host, path) = after_user.split_once(':')?;
(!host.is_empty()).then(|| ("ssh".into(), host.to_string(), None, path.to_string()))
}
fn decode(segment: &str) -> String {
fn hex(b: u8) -> Option<u8> {
(b as char).to_digit(16).map(|d| d as u8)
}
let bytes = segment.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' && i + 2 < bytes.len() {
if let (Some(hi), Some(lo)) = (hex(bytes[i + 1]), hex(bytes[i + 2])) {
out.push(hi * 16 + lo);
i += 3;
continue;
}
}
out.push(bytes[i]);
i += 1;
}
String::from_utf8_lossy(&out).into_owned()
}
fn root_key(url: &str) -> String {
let rest = url.split_once("://").map_or(url, |(_, r)| r);
let rest = rest.trim_end_matches('/');
let (authority, path) = rest.split_once('/').unwrap_or((rest, ""));
let authority = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
let host = match authority.rsplit_once(':') {
Some((h, p)) if p.chars().all(|c| c.is_ascii_digit()) => h,
_ => authority,
};
format!(
"{}/{}",
host.to_ascii_lowercase(),
path.to_ascii_lowercase()
)
.trim_end_matches('/')
.to_string()
}
pub(super) fn owns_remote(integration: &Integration, remote: &RemoteRef) -> bool {
if integration.url.trim().is_empty() {
return false;
}
let root = gitpoulpi_config::normalize_azure_url(&integration.url);
root_key(&root) == root_key(&remote.web_base)
}