use crate::index::{CloneOptions, LAST_SEEN_REFNAME};
use crate::Index;
use std::borrow::Cow;
use std::path::Path;
use std::sync::atomic::AtomicBool;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
PrepareClone(#[from] Box<gix::clone::Error>),
#[error(transparent)]
Fetch(#[from] Box<gix::clone::fetch::Error>),
#[error(transparent)]
Open(#[from] Box<gix::open::Error>),
}
impl_from_boxed!(gix::clone::Error => Error::PrepareClone);
impl_from_boxed!(gix::clone::fetch::Error => Error::Fetch);
impl_from_boxed!(gix::open::Error => Error::Open);
impl Index {
pub fn from_path_or_cloned_with_options<P>(
path: impl AsRef<Path>,
progress: P,
should_interrupt: &AtomicBool,
CloneOptions { url }: CloneOptions,
) -> Result<Index, Error>
where
P: gix::NestedProgress,
P::SubProgress: 'static,
{
let path = path.as_ref();
let mut repo = match gix::open(path) {
Ok(repo) => repo,
Err(gix::open::Error::NotARepository { .. }) => {
let (repo, _out) =
gix::prepare_clone_bare(url, path)?.fetch_only(progress, should_interrupt)?;
repo
}
Err(err) => return Err(err.into()),
};
repo.object_cache_size_if_unset(4 * 1024 * 1024);
let remote_name = repo.remote_names().into_iter().next().map(Cow::into_owned);
Ok(Index {
repo,
remote_name,
branch_name: "master",
seen_ref_name: LAST_SEEN_REFNAME,
})
}
pub fn from_path_or_cloned(path: impl AsRef<Path>) -> Result<Index, Error> {
Index::from_path_or_cloned_with_options(
path,
gix::progress::Discard,
&AtomicBool::default(),
CloneOptions::default(),
)
}
}