use super::{CleanupRequest, Git, owned};
pub(super) fn repo_mismatch<T: Git>(git: &T, req: &CleanupRequest) -> Option<String> {
let stated = req
.repo
.as_deref()
.map(str::trim)
.filter(|r| !r.is_empty())?;
let url = match git.run(
&req.repo_root,
&owned(&["config", "--get", "remote.origin.url"]),
) {
Ok(out) if out.success && !out.stdout.trim().is_empty() => out.stdout.trim().to_string(),
Ok(out) => {
return Some(format!(
"the registry names `{stated}`, and the `origin` remote at {} could not be read \
to confirm it: {} — refusing rather than running destructive git in a \
repository this cleanup may not be for (#7275)",
req.repo_root.display(),
out.stderr.trim()
));
}
Err(e) => {
return Some(format!(
"the registry names `{stated}`, and the `origin` remote at {} could not be read \
to confirm it: {e:#} (#7275)",
req.repo_root.display()
));
}
};
let actual = match crate::session_manager::worktree_repo_slug::parse_repo_slug(
&url,
&crate::session_manager::ssh_host_alias::SshHostAliases::for_current_user(),
) {
Ok(slug) => slug,
Err(_) => {
return Some(format!(
"the registry names `{stated}`, and the `origin` URL at {} ({}) names no \
repository this can compare it against — refusing (#7275)",
req.repo_root.display(),
crate::core::remote_url_redact::redact_stored_url(&url) ));
}
};
let agrees = actual.eq_ignore_ascii_case(stated)
|| actual
.to_ascii_lowercase()
.ends_with(&format!("/{}", stated.to_ascii_lowercase()));
if agrees {
return None;
}
Some(format!(
"the registry names `{stated}` but the checkout at {} is `{actual}` — refusing: a merged \
pull request in one repository must never authorise branch deletion or worktree removal \
in another (#7275)",
req.repo_root.display()
))
}