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)
}
pub fn stage(
&self,
options: &crate::porcelain::add::StageOptions,
progress: &mut dyn crate::progress::ProgressSink,
cancel: &crate::progress::CancelToken,
) -> crate::error::Result<crate::porcelain::add::StageOutcome> {
crate::porcelain::add::stage(self.repo, options, progress, cancel)
}
pub fn stage_for_commit(
&self,
progress: &mut dyn crate::progress::ProgressSink,
cancel: &crate::progress::CancelToken,
) -> crate::error::Result<crate::porcelain::add::StageOutcome> {
crate::porcelain::add::stage_for_commit(self.repo, progress, cancel)
}
pub fn stage_on(
&self,
index: &mut Index,
options: &crate::porcelain::add::StageOptions,
progress: &mut dyn crate::progress::ProgressSink,
cancel: &crate::progress::CancelToken,
) -> crate::error::Result<crate::porcelain::add::StageOutcome> {
crate::porcelain::add::stage_on_index(self.repo, index, options, progress, cancel)
}
pub fn stage_worktree_paths(
&self,
pathspecs: &[String],
) -> crate::error::Result<crate::porcelain::staging::StageWorktreeChangesOutcome> {
crate::porcelain::staging::stage_worktree_changes(self.repo, pathspecs)
}
pub fn stage_tracked_modifications(
&self,
work_tree: &Path,
) -> crate::error::Result<crate::porcelain::stage_tracked::StageTrackedSummary> {
let thresholds =
self.staging_thresholds_from_options(&crate::porcelain::add::StageOptions::default());
crate::porcelain::stage_tracked::stage_tracked_modifications_with_thresholds(
self.repo, work_tree, thresholds,
)
}
pub fn stage_tracked_modifications_on(
&self,
work_tree: &Path,
index_path: &Path,
index: &mut Index,
options: &crate::porcelain::add::StageOptions,
) -> crate::error::Result<crate::porcelain::stage_tracked::StageTrackedSummary> {
let thresholds = self.staging_thresholds_from_options(options);
crate::porcelain::stage_tracked::stage_tracked_modifications_in_index_with_thresholds(
self.repo, work_tree, index_path, index, thresholds,
)
}
fn staging_thresholds_from_options(
&self,
options: &crate::porcelain::add::StageOptions,
) -> crate::porcelain::bulk_index::IndexStagingThresholds {
#[cfg(any(test, feature = "test-support"))]
{
options.staging_thresholds.unwrap_or_default()
}
#[cfg(not(any(test, feature = "test-support")))]
{
options.staging_thresholds.unwrap_or_default()
}
}
pub fn stage_hunks(
&self,
side: crate::diff::DiffSide,
selection: &crate::porcelain::hunk_select::HunkSelection,
pathspecs: &[String],
diff_options: crate::diff::DiffOptions,
dry_run: bool,
cancel: &crate::progress::CancelToken,
) -> crate::error::Result<crate::porcelain::hunk_select::StageHunksOutcome> {
crate::porcelain::hunk_select::stage_hunks(
self.repo,
side,
selection,
pathspecs,
diff_options,
dry_run,
cancel,
)
}
pub fn restore_hunks(
&self,
selection: &crate::porcelain::hunk_select::HunkSelection,
pathspecs: &[String],
restore_staged: bool,
restore_worktree: bool,
dry_run: bool,
) -> crate::error::Result<crate::porcelain::hunk_select::RestoreHunksOutcome> {
crate::porcelain::hunk_select::restore_hunks(
self.repo,
selection,
pathspecs,
restore_staged,
restore_worktree,
dry_run,
)
}
pub fn hunk_diff_options(&self) -> crate::error::Result<crate::diff::DiffOptions> {
crate::porcelain::hunk_select::diff_options_for_hunk_select(self.repo)
}
pub fn unstaged_file_diffs(
&self,
pathspecs: &[String],
) -> crate::error::Result<
Vec<(
crate::diff::FileDiff,
Vec<crate::diff::structured::StructuredHunk>,
)>,
> {
crate::porcelain::hunk_select::unstaged_file_diffs(self.repo, pathspecs)
}
pub fn list_unstaged_hunk_ids(
&self,
pathspecs: &[String],
diff_options: crate::diff::DiffOptions,
) -> crate::error::Result<std::collections::BTreeMap<String, Vec<crate::diff::HunkId>>> {
crate::porcelain::hunk_select::list_unstaged_hunk_ids(self.repo, pathspecs, diff_options)
}
pub fn resolve_hunk_selection(
&self,
side: crate::diff::DiffSide,
selection: &crate::porcelain::hunk_select::HunkSelection,
pathspecs: &[String],
diff_options: crate::diff::DiffOptions,
) -> crate::error::Result<Vec<crate::apply::FilePatch>> {
crate::porcelain::hunk_select::resolve_selection(
self.repo,
side,
selection,
pathspecs,
diff_options,
)
}
pub fn pathspec_matches_known(&self, resolved: &str) -> crate::error::Result<bool> {
crate::porcelain::pathspec_match::pathspec_matches_known_in_repository(self.repo, resolved)
}
pub fn ensure_pathspec_matches_known(
&self,
display_spec: &str,
resolved: &str,
) -> crate::error::Result<()> {
crate::porcelain::pathspec_match::ensure_pathspec_matches_known_in_repository(
self.repo,
display_spec,
resolved,
)
}
}