use std::path::Path;
use crate::error::Result;
use crate::index::{Index, IndexParseContext};
use crate::repo::Repository;
use crate::split_index::WriteSplitIndexRequest;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum SparseIndexLoad {
#[default]
Expand,
Preserve,
}
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct IndexLoadOptions<'a> {
pub path: Option<&'a Path>,
pub sparse: SparseIndexLoad,
pub parse_context: Option<IndexParseContext>,
}
impl<'a> IndexLoadOptions<'a> {
#[must_use]
pub fn with_path(mut self, path: &'a Path) -> Self {
self.path = Some(path);
self
}
#[must_use]
pub fn with_sparse(mut self, sparse: SparseIndexLoad) -> Self {
self.sparse = sparse;
self
}
#[must_use]
pub fn with_parse_context(mut self, parse_context: IndexParseContext) -> Self {
self.parse_context = Some(parse_context);
self
}
}
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct IndexWriteOptions<'a> {
pub path: Option<&'a Path>,
pub split: WriteSplitIndexRequest,
}
impl<'a> IndexWriteOptions<'a> {
#[must_use]
pub fn with_path(mut self, path: &'a Path) -> Self {
self.path = Some(path);
self
}
#[must_use]
pub fn with_split(mut self, split: WriteSplitIndexRequest) -> Self {
self.split = split;
self
}
}
#[derive(Debug, Copy, Clone)]
pub struct IndexFile<'repo> {
repo: &'repo Repository,
}
impl<'repo> IndexFile<'repo> {
#[inline]
#[must_use]
pub fn new(repo: &'repo Repository) -> Self {
Self { repo }
}
#[inline]
#[must_use]
pub fn repository(&self) -> &'repo Repository {
self.repo
}
#[inline]
#[must_use]
pub fn path(&self) -> std::path::PathBuf {
self.repo.index_path()
}
#[inline]
pub fn path_for_env(&self) -> Result<std::path::PathBuf> {
self.repo.index_path_for_env()
}
pub fn load(&self, options: &IndexLoadOptions<'_>) -> crate::error::Result<Index> {
let path = match options.path {
Some(path) => path.to_path_buf(),
None => self.repo.index_path_for_env()?,
};
let parse_ctx = options
.parse_context
.unwrap_or_else(|| IndexParseContext::from_policy(self.repo.thread_policy()));
match options.sparse {
SparseIndexLoad::Expand => self.repo.load_index_at_with_context(&path, parse_ctx),
SparseIndexLoad::Preserve => {
self.repo.load_index_at_preserving_sparse(&path, parse_ctx)
}
}
}
pub fn write(
&self,
index: &mut Index,
options: &IndexWriteOptions<'_>,
) -> crate::error::Result<()> {
let path = match options.path {
Some(path) => path.to_path_buf(),
None => self.repo.index_path_for_env()?,
};
self.repo.write_index_at_split(&path, index, options.split)
}
pub fn apply_sparse_checkout(&self, index: &mut Index) -> Result<Vec<String>> {
let config = self.repo.config().snapshot()?;
Ok(crate::sparse_checkout::apply_sparse_checkout_with_config(
self.repo.git_dir(),
self.repo.work_tree(),
index,
config.as_ref(),
))
}
#[inline]
pub fn begin_update(&self) -> crate::error::Result<crate::index::IndexLock> {
self.repo.begin_index_update()
}
#[inline]
pub fn commit_update(
&self,
lock: &mut crate::index::IndexLock,
index: &mut Index,
updated_workdir: bool,
) -> crate::error::Result<()> {
self.repo.commit_index_update(lock, index, updated_workdir)
}
#[inline]
pub fn try_write(&self, index: &mut Index) -> crate::error::Result<bool> {
self.repo.try_write_index(index)
}
}