use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::OnceLock;
use eyre::{Result, WrapErr, eyre};
use sha1::Digest as _;
use crate::error::{InstallError, VcsFailure};
#[derive(Debug, Clone, Copy)]
pub struct SourceSpec<'a> {
pub package_name: &'a str,
pub urls: &'a [String],
pub reference: &'a str,
pub dest: &'a Path,
}
pub trait SourceFetcher: Sync {
fn install(&self, spec: &SourceSpec<'_>) -> Result<()>;
}
#[derive(Debug)]
pub struct GitSourceFetcher {
mirror_root: PathBuf,
git_checked: OnceLock<Result<(), String>>,
}
impl GitSourceFetcher {
pub fn new(mirror_root: impl Into<PathBuf>) -> Self {
Self {
mirror_root: mirror_root.into(),
git_checked: OnceLock::new(),
}
}
fn ensure_git(&self) -> Result<()> {
let checked =
self.git_checked
.get_or_init(|| match Command::new("git").arg("--version").output() {
Ok(out) if out.status.success() => Ok(()),
_ => Err(String::new()),
});
checked.clone().map_err(|_| git_missing("invocation"))
}
fn install_from(&self, url: &str, reference: &str, dest: &Path) -> Result<()> {
let mirror = self.ensure_mirror(url, reference)?;
let _ = std::fs::remove_dir_all(dest);
if let Some(parent) = dest.parent() {
std::fs::create_dir_all(parent)
.wrap_err_with(|| format!("creating {}", parent.display()))?;
}
run_git(
"clone",
url,
None,
&[
"clone".as_ref(),
"--quiet".as_ref(),
mirror.as_os_str(),
dest.as_os_str(),
],
)?;
run_git(
"checkout",
url,
Some(dest),
&[
"checkout".as_ref(),
"--quiet".as_ref(),
"--detach".as_ref(),
reference.as_ref(),
],
)?;
let _ = run_git(
"set-url",
url,
Some(dest),
&[
"remote".as_ref(),
"set-url".as_ref(),
"origin".as_ref(),
url.as_ref(),
],
);
Ok(())
}
fn ensure_mirror(&self, url: &str, reference: &str) -> Result<PathBuf> {
std::fs::create_dir_all(&self.mirror_root)
.wrap_err_with(|| format!("creating {}", self.mirror_root.display()))?;
let mirror = self.mirror_root.join(mirror_dir_name(url));
if mirror.join("HEAD").is_file() {
if !reference.is_empty() && !commit_present(&mirror, reference) {
run_git(
"fetch",
url,
Some(&mirror),
&["remote".as_ref(), "update".as_ref(), "--prune".as_ref()],
)?;
}
} else {
let _ = std::fs::remove_dir_all(&mirror);
run_git(
"clone",
url,
None,
&[
"clone".as_ref(),
"--mirror".as_ref(),
"--quiet".as_ref(),
url.as_ref(),
mirror.as_os_str(),
],
)?;
}
Ok(mirror)
}
}
impl SourceFetcher for GitSourceFetcher {
fn install(&self, spec: &SourceSpec<'_>) -> Result<()> {
self.ensure_git()?;
let mut last_err = None;
for url in spec.urls {
match self.install_from(url, spec.reference, spec.dest) {
Ok(()) => return Ok(()),
Err(e) => last_err = Some(e),
}
}
Err(last_err.unwrap_or_else(|| eyre!("no clone URL")))
}
}
pub(crate) fn materialize_sources(
fetcher: &dyn SourceFetcher,
specs: &[SourceSpec<'_>],
) -> Result<usize> {
use rayon::prelude::*;
specs.par_iter().try_for_each(|spec| {
if spec.urls.is_empty() {
return Err(eyre!(
"package `{}` has a git source but no clone URL",
spec.package_name
));
}
fetcher
.install(spec)
.wrap_err_with(|| format!("installing `{}` from git source", spec.package_name))
})?;
Ok(specs.len())
}
fn commit_present(mirror: &Path, reference: &str) -> bool {
run_git(
"cat-file",
"",
Some(mirror),
&[
"cat-file".as_ref(),
"-e".as_ref(),
format!("{reference}^{{commit}}").as_ref(),
],
)
.is_ok()
}
fn mirror_dir_name(url: &str) -> String {
let sanitized: String = url
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-') {
c
} else {
'-'
}
})
.collect();
let skip = sanitized.chars().count().saturating_sub(60);
let tail: String = sanitized.chars().skip(skip).collect();
let digest = sha1::Sha1::digest(url.as_bytes());
let mut short = String::with_capacity(16);
for b in &digest[..8] {
use std::fmt::Write as _;
let _ = write!(short, "{b:02x}");
}
format!("{tail}-{short}")
}
fn run_git(
operation: &str,
url: &str,
cwd: Option<&Path>,
args: &[&std::ffi::OsStr],
) -> Result<()> {
let mut cmd = Command::new("git");
if let Some(dir) = cwd {
cmd.arg("-C").arg(dir);
}
cmd.args(args);
let out = cmd.output().map_err(|_| git_missing(operation))?;
if out.status.success() {
Ok(())
} else {
Err(classify_git(
operation,
url,
&String::from_utf8_lossy(&out.stderr),
))
}
}
fn git_missing(operation: &str) -> eyre::Report {
InstallError::Vcs {
failure: VcsFailure::GitMissing,
operation: operation.to_owned(),
url: String::new(),
detail: "`git` could not be run".to_owned(),
}
.into()
}
fn classify_git(operation: &str, url: &str, stderr: &str) -> eyre::Report {
let s = stderr.to_ascii_lowercase();
let failure = if [
"authentication failed",
"could not read username",
"could not read password",
"permission denied",
"access denied",
"403",
"terminal prompts disabled",
]
.iter()
.any(|m| s.contains(m))
{
VcsFailure::Auth
} else if [
"couldn't find remote ref",
"did not match any",
"reference is not a tree",
"unable to read tree",
"not a valid object name",
"bad object",
"pathspec",
]
.iter()
.any(|m| s.contains(m))
{
VcsFailure::MissingRef
} else if [
"repository not found",
"could not read from remote repository",
"not found",
]
.iter()
.any(|m| s.contains(m))
{
VcsFailure::Unreachable
} else {
VcsFailure::Other
};
InstallError::Vcs {
failure,
operation: operation.to_owned(),
url: url.to_owned(),
detail: stderr.trim().to_owned(),
}
.into()
}
#[cfg(test)]
pub(crate) mod tests;