#![allow(missing_docs)]
use std::path::{Path, PathBuf};
use bstr::{BStr, ByteSlice};
use git_hash::oid;
use super::Cache;
use crate::{fs, fs::PathOidMapping};
#[derive(Clone)]
pub enum State {
CreateDirectoryAndAttributesStack {
unlink_on_collision: bool,
#[cfg(debug_assertions)]
test_mkdir_calls: usize,
attributes: state::Attributes,
},
AttributesAndIgnoreStack {
attributes: state::Attributes,
ignore: state::Ignore,
},
IgnoreStack(state::Ignore),
}
#[cfg(debug_assertions)]
impl<'paths> Cache<'paths> {
pub fn set_case(&mut self, case: git_glob::pattern::Case) {
self.case = case;
}
pub fn num_mkdir_calls(&self) -> usize {
match self.state {
State::CreateDirectoryAndAttributesStack { test_mkdir_calls, .. } => test_mkdir_calls,
_ => 0,
}
}
pub fn reset_mkdir_calls(&mut self) {
if let State::CreateDirectoryAndAttributesStack { test_mkdir_calls, .. } = &mut self.state {
*test_mkdir_calls = 0;
}
}
pub fn unlink_on_collision(&mut self, value: bool) {
if let State::CreateDirectoryAndAttributesStack {
unlink_on_collision, ..
} = &mut self.state
{
*unlink_on_collision = value;
}
}
}
#[must_use]
pub struct Platform<'a, 'paths> {
parent: &'a Cache<'paths>,
is_dir: Option<bool>,
}
impl<'paths> Cache<'paths> {
pub fn new(
worktree_root: impl Into<PathBuf>,
state: State,
case: git_glob::pattern::Case,
buf: Vec<u8>,
attribute_files_in_index: Vec<PathOidMapping<'paths>>,
) -> Self {
let root = worktree_root.into();
Cache {
stack: fs::Stack::new(root),
state,
case,
buf,
attribute_files_in_index,
}
}
pub fn at_path<Find, E>(
&mut self,
relative: impl AsRef<Path>,
is_dir: Option<bool>,
find: Find,
) -> std::io::Result<Platform<'_, 'paths>>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<git_object::BlobRef<'a>, E>,
E: std::error::Error + Send + Sync + 'static,
{
let mut delegate = platform::StackDelegate {
state: &mut self.state,
buf: &mut self.buf,
is_dir: is_dir.unwrap_or(false),
attribute_files_in_index: &self.attribute_files_in_index,
find,
};
self.stack.make_relative_path_current(relative, &mut delegate)?;
Ok(Platform { parent: self, is_dir })
}
pub fn at_entry<'r, Find, E>(
&mut self,
relative: impl Into<&'r BStr>,
is_dir: Option<bool>,
find: Find,
) -> std::io::Result<Platform<'_, 'paths>>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<git_object::BlobRef<'a>, E>,
E: std::error::Error + Send + Sync + 'static,
{
let relative = relative.into();
let relative_path = git_path::from_bstr(relative);
self.at_path(
relative_path,
is_dir.or_else(|| relative.ends_with_str("/").then(|| true)),
find,
)
}
pub fn base(&self) -> &Path {
self.stack.root()
}
}
mod platform;
pub mod state;