use crate::diff::DiffEntry;
use crate::error::Result;
use crate::index::Index;
use crate::pathspec::Pathspec;
use crate::porcelain::status::{
collect_untracked_and_ignored_inner, status as status_impl,
status_diff_entry_matches_pathspec_typed, status_options_from_config, IgnoredMode, StatusModel,
StatusOptions, UntrackedCollectMeta, UntrackedScan,
};
use crate::progress::{CancelToken, ProgressSink};
use crate::repo::Repository;
use crate::worktree_rules::WorktreeRules;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum UntrackedCachePolicy {
#[default]
Refresh,
Skip,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct UntrackedOptions {
pub ignored_mode: IgnoredMode,
pub show_all: bool,
pub pathspec: Pathspec,
pub cache: UntrackedCachePolicy,
}
impl Default for UntrackedOptions {
fn default() -> Self {
Self {
ignored_mode: IgnoredMode::No,
show_all: false,
pathspec: Pathspec::entire_tree(crate::pathspec::PathspecGlobalFlags::default()),
cache: UntrackedCachePolicy::default(),
}
}
}
impl UntrackedOptions {
#[must_use]
pub fn with_ignored_mode(mut self, ignored_mode: IgnoredMode) -> Self {
self.ignored_mode = ignored_mode;
self
}
#[must_use]
pub fn with_show_all(mut self, show_all: bool) -> Self {
self.show_all = show_all;
self
}
#[must_use]
pub fn with_pathspec(mut self, pathspec: Pathspec) -> Self {
self.pathspec = pathspec;
self
}
#[must_use]
pub fn with_cache(mut self, cache: UntrackedCachePolicy) -> Self {
self.cache = cache;
self
}
}
#[derive(Debug, Copy, Clone)]
pub struct Status<'repo> {
repo: &'repo Repository,
}
impl<'repo> Status<'repo> {
#[inline]
#[must_use]
pub fn new(repo: &'repo Repository) -> Self {
Self { repo }
}
#[inline]
#[must_use]
pub fn repository(&self) -> &'repo Repository {
self.repo
}
pub fn run(
&self,
options: &StatusOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> Result<StatusModel> {
status_impl(self.repo, options, progress, cancel)
}
pub fn untracked(
&self,
index: &mut Index,
options: &UntrackedOptions,
progress: &mut dyn ProgressSink,
cancel: &CancelToken,
) -> Result<(Vec<String>, Vec<String>)> {
let _ = progress;
let (untracked, ignored, _) = self.untracked_with_meta(index, options, None, cancel)?;
Ok((untracked, ignored))
}
pub fn untracked_with_meta(
&self,
index: &mut Index,
options: &UntrackedOptions,
rules: Option<&WorktreeRules>,
cancel: &CancelToken,
) -> Result<(Vec<String>, Vec<String>, UntrackedCollectMeta)> {
let work_tree = self
.repo
.work_tree()
.ok_or(crate::repo::RepositoryError::RequiresWorkTree)?;
let owned_rules;
let rules = match rules {
Some(rules) => rules,
None => {
owned_rules = WorktreeRules::from_repository(self.repo, index)?;
&owned_rules
}
};
let specs = if options.pathspec.is_entire_tree() {
&[] as &[String]
} else {
options.pathspec.specs()
};
let use_cache = matches!(options.cache, UntrackedCachePolicy::Refresh);
collect_untracked_and_ignored_inner(
self.repo,
index,
work_tree,
specs,
Some(&options.pathspec),
UntrackedScan {
ignored_mode: options.ignored_mode,
show_all: options.show_all,
sort_paths: true,
use_untracked_cache: use_cache,
cache_miss: Default::default(),
clean_unit_dirs: None,
listing_cache: None,
},
rules,
Some(cancel.clone()),
)
}
pub fn expand_untracked_for_staging(
&self,
index: &mut Index,
untracked: Vec<String>,
pathspecs: &[String],
) -> Result<Vec<String>> {
crate::porcelain::status::expand_untracked_for_staging(
self.repo,
index,
self.repo
.work_tree()
.ok_or(crate::repo::RepositoryError::RequiresWorkTree)?,
untracked,
pathspecs,
)
}
pub fn diff_entry_matches_pathspec(
&self,
entry: &DiffEntry,
pathspec: &Pathspec,
) -> Result<bool> {
status_diff_entry_matches_pathspec_typed(self.repo, entry, pathspec)
}
}
impl StatusOptions {
pub fn from_config(config: &crate::config::Config<'_>) -> Result<Self> {
Ok(status_options_from_config(config.snapshot()?.as_ref())?)
}
}