use crate::index::{CloneOptions, LAST_SEEN_REFNAME};
use crate::Index;
use git_repository as git;
use std::path::Path;
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Clone(#[from] git2::Error),
#[error(transparent)]
Open(#[from] git::open::Error),
}
impl Index {
pub fn from_path_or_cloned_with_options(
path: impl AsRef<Path>,
CloneOptions {
repository_url,
fetch_options,
}: CloneOptions<'_>,
) -> Result<Index, Error> {
let repo = git2::Repository::open(path.as_ref()).or_else(|err| {
if err.class() == git2::ErrorClass::Repository {
let mut builder = git2::build::RepoBuilder::new();
if let Some(fo) = fetch_options {
builder.fetch_options(fo);
}
builder.bare(true).clone(&repository_url, path.as_ref())
} else {
Err(err)
}
})?;
let mut repo = git::open(repo.path())?.apply_environment();
repo.object_cache_size_if_unset(4 * 1024 * 1024);
Ok(Index {
repo,
remote_name: "origin",
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, CloneOptions::default())
}
}