#![allow(clippy::result_large_err)]
use std::{borrow::Cow, path::PathBuf, time::Duration};
use git_lock::acquire::Fail;
use crate::{
bstr::BStr,
config,
config::{
cache::util::{ApplyLeniency, ApplyLeniencyDefault},
checkout_options,
tree::{Checkout, Core, Key},
Cache,
},
remote,
repository::identity,
};
impl Cache {
pub(crate) fn diff_algorithm(&self) -> Result<git_diff::blob::Algorithm, config::diff::algorithm::Error> {
use crate::config::diff::algorithm::Error;
self.diff_algorithm
.get_or_try_init(|| {
let name = self
.resolved
.string("diff", None, "algorithm")
.unwrap_or_else(|| Cow::Borrowed("myers".into()));
config::tree::Diff::ALGORITHM
.try_into_algorithm(name)
.or_else(|err| match err {
Error::Unimplemented { .. } if self.lenient_config => Ok(git_diff::blob::Algorithm::Histogram),
err => Err(err),
})
.with_lenient_default(self.lenient_config)
})
.copied()
}
#[cfg(any(feature = "async-network-client", feature = "blocking-network-client"))]
pub(crate) fn user_agent_tuple(&self) -> (&'static str, Option<Cow<'static, str>>) {
use config::tree::Gitoxide;
let agent = self
.user_agent
.get_or_init(|| {
self.resolved
.string_by_key(Gitoxide::USER_AGENT.logical_name().as_str())
.map(|s| s.to_string())
.unwrap_or_else(|| crate::env::agent().into())
})
.to_owned();
("agent", Some(git_protocol::agent(agent).into()))
}
pub(crate) fn personas(&self) -> &identity::Personas {
self.personas
.get_or_init(|| identity::Personas::from_config_and_env(&self.resolved))
}
pub(crate) fn url_rewrite(&self) -> &remote::url::Rewrite {
self.url_rewrite
.get_or_init(|| remote::url::Rewrite::from_config(&self.resolved, self.filter_config_section))
}
#[cfg(any(feature = "blocking-network-client", feature = "async-network-client"))]
pub(crate) fn url_scheme(&self) -> Result<&remote::url::SchemePermission, config::protocol::allow::Error> {
self.url_scheme
.get_or_try_init(|| remote::url::SchemePermission::from_config(&self.resolved, self.filter_config_section))
}
pub(crate) fn diff_renames(
&self,
) -> Result<Option<crate::object::tree::diff::Renames>, crate::object::tree::diff::renames::Error> {
self.diff_renames
.get_or_try_init(|| {
crate::object::tree::diff::Renames::try_from_config(&self.resolved, self.lenient_config)
})
.copied()
}
pub(crate) fn lock_timeout(
&self,
) -> Result<(git_lock::acquire::Fail, git_lock::acquire::Fail), config::lock_timeout::Error> {
let mut out: [git_lock::acquire::Fail; 2] = Default::default();
for (idx, (key, default_ms)) in [(&Core::FILES_REF_LOCK_TIMEOUT, 100), (&Core::PACKED_REFS_TIMEOUT, 1000)]
.into_iter()
.enumerate()
{
out[idx] = self
.resolved
.integer_filter("core", None, key.name, &mut self.filter_config_section.clone())
.map(|res| key.try_into_lock_timeout(res))
.transpose()
.with_leniency(self.lenient_config)?
.unwrap_or_else(|| Fail::AfterDurationWithBackoff(Duration::from_millis(default_ms)));
}
Ok((out[0], out[1]))
}
pub(crate) fn excludes_file(&self) -> Option<Result<PathBuf, git_config::path::interpolate::Error>> {
self.trusted_file_path("core", None, Core::EXCLUDES_FILE.name)?
.map(|p| p.into_owned())
.into()
}
pub(crate) fn trusted_file_path(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
key: impl AsRef<str>,
) -> Option<Result<Cow<'_, std::path::Path>, git_config::path::interpolate::Error>> {
let path = self.resolved.path_filter(
section_name,
subsection_name,
key,
&mut self.filter_config_section.clone(),
)?;
let install_dir = crate::path::install_dir().ok();
let home = self.home_dir();
let ctx = crate::config::cache::interpolate_context(install_dir.as_deref(), home.as_deref());
Some(path.interpolate(ctx))
}
pub(crate) fn apply_leniency<T, E>(&self, res: Option<Result<T, E>>) -> Result<Option<T>, E> {
res.transpose().with_leniency(self.lenient_config)
}
pub(crate) fn checkout_options(
&self,
git_dir: &std::path::Path,
) -> Result<git_worktree::index::checkout::Options, checkout_options::Error> {
fn boolean(
me: &Cache,
full_key: &str,
key: &'static config::tree::keys::Boolean,
default: bool,
) -> Result<bool, checkout_options::Error> {
debug_assert_eq!(
full_key,
key.logical_name(),
"BUG: key name and hardcoded name must match"
);
Ok(me
.apply_leniency(me.resolved.boolean_by_key(full_key).map(|v| key.enrich_error(v)))?
.unwrap_or(default))
}
fn assemble_attribute_globals(
me: &Cache,
_git_dir: &std::path::Path,
) -> Result<git_attributes::MatchGroup, checkout_options::Error> {
let _attributes_file = match me
.trusted_file_path("core", None, Core::ATTRIBUTES_FILE.name)
.transpose()?
{
Some(attributes) => Some(attributes.into_owned()),
None => me.xdg_config_path("attributes").ok().flatten(),
};
Ok(Default::default())
}
let thread_limit = self.apply_leniency(
self.resolved
.integer_filter_by_key("checkout.workers", &mut self.filter_config_section.clone())
.map(|value| Checkout::WORKERS.try_from_workers(value)),
)?;
Ok(git_worktree::index::checkout::Options {
fs: git_worktree::fs::Capabilities {
precompose_unicode: boolean(self, "core.precomposeUnicode", &Core::PRECOMPOSE_UNICODE, false)?,
ignore_case: boolean(self, "core.ignoreCase", &Core::IGNORE_CASE, false)?,
executable_bit: boolean(self, "core.fileMode", &Core::FILE_MODE, true)?,
symlink: boolean(self, "core.symlinks", &Core::SYMLINKS, true)?,
},
thread_limit,
destination_is_initially_empty: false,
overwrite_existing: false,
keep_going: false,
trust_ctime: boolean(self, "core.trustCTime", &Core::TRUST_C_TIME, true)?,
check_stat: self
.apply_leniency(
self.resolved
.string("core", None, "checkStat")
.map(|v| Core::CHECK_STAT.try_into_checkstat(v)),
)?
.unwrap_or(true),
attribute_globals: assemble_attribute_globals(self, git_dir)?,
})
}
pub(crate) fn xdg_config_path(
&self,
resource_file_name: &str,
) -> Result<Option<PathBuf>, git_sec::permission::Error<PathBuf>> {
std::env::var_os("XDG_CONFIG_HOME")
.map(|path| (PathBuf::from(path), &self.xdg_config_home_env))
.or_else(|| {
std::env::var_os("HOME").map(|path| {
(
{
let mut p = PathBuf::from(path);
p.push(".config");
p
},
&self.home_env,
)
})
})
.and_then(|(base, permission)| {
let resource = base.join("git").join(resource_file_name);
permission.check(resource).transpose()
})
.transpose()
}
pub(crate) fn home_dir(&self) -> Option<PathBuf> {
std::env::var_os("HOME")
.map(PathBuf::from)
.and_then(|path| self.home_env.check_opt(path))
}
}