Something went wrong. Try again.
Monorepo for Tangled
Something went wrong. Try again.
1.8 kB · 65 lines
Rust
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566use std::path::PathBuf;
use itertools::Itertools as _;use jacquard_common::types::did::Did;
use crate::scratch::TempRepository;
#[derive(Debug, Clone)]pub struct Layout { scan_path: PathBuf,}
#[derive(Debug, thiserror::Error)]pub enum Error { #[error("repo not found")] RepoNotFound, #[error("open_scratch requires at least one repo")] NotEnoughRepos, #[error("scratch repo: {0}")] Io(#[from] std::io::Error), #[error("init scratch repo: {0}")] Init(#[from] gix::init::Error), #[error("open scratch repo: {0}")] Open(#[from] gix::open::Error),}
impl Layout { pub fn new(scan_path: PathBuf) -> Self { Self { scan_path } }
pub fn repo_path(&self, repo: &Did) -> PathBuf { // TODO: normalize DID like knot_git::Layout does. self.scan_path.join(repo.as_str()) }
pub fn open(&self, repo: &Did) -> Result<gix::Repository, gix::open::Error> { gix::open(self.repo_path(repo)) }
pub fn open_scratch(&self, dids: &[&Did]) -> Result<TempRepository, Error> { if dids.is_empty() { return Err(Error::NotEnoughRepos); } let alternates = dids .iter() .copied() .unique() .map(|did| { std::fs::canonicalize(self.repo_path(did).join("objects")) .map(|dir| format!("{}\n", dir.display())) .map_err(|_| Error::RepoNotFound) }) .collect::<Result<String, _>>()?;
let scratch = tempfile::tempdir()?; gix::init_bare(scratch.path())?;
let info_dir = scratch.path().join("objects").join("info"); std::fs::create_dir_all(&info_dir)?; std::fs::write(info_dir.join("alternates"), alternates)?;
Ok(TempRepository::open(scratch)?) }}