History

refactor (#450)

unify access to trusted file paths, allowing it to be queried through the snapshot and as easily through the config cache directly.

Sebastian Thiel committed 4 years ago

Commit 28de9df1 parent f22bdc02 files · +27 −15Browse files

Unified · Split

2 files
  1. git-repository/src/config/cache/access.rs +23 −4
  2. git-repository/src/config/snapshot/access.rs +4 −11
git-repository/src/config/cache/access.rs+23−4
@@ -1,3 +1,4 @@
1+use std::borrow::Cow;
12 use std::{convert::TryInto, path::PathBuf, time::Duration};
23
34 use git_lock::acquire::Fail;
@@ -61,12 +62,30 @@ impl Cache { · 57 unmodified lines
6162
6263 /// The path to the user-level excludes file to ignore certain files in the worktree.
6364 pub(crate) fn excludes_file(&self) -> Option<Result<PathBuf, git_config::path::interpolate::Error>> {
64− let home = self.home_dir();
65+ self.trusted_file_path("core", None, "excludesFile")?
66+ .map(|p| p.into_owned())
67+ .into()
68+ }
69+
70+ /// A helper to obtain a file from trusted configuration at `section_name`, `subsection_name`, and `key`, which is interpolated
71+ /// if present.
72+ pub(crate) fn trusted_file_path(
73+ &self,
74+ section_name: impl AsRef<str>,
75+ subsection_name: Option<&str>,
76+ key: impl AsRef<str>,
77+ ) -> Option<Result<Cow<'_, std::path::Path>, git_config::path::interpolate::Error>> {
78+ let path = self.resolved.path_filter(
79+ section_name,
80+ subsection_name,
81+ key,
82+ &mut self.filter_config_section.clone(),
83+ )?;
84+
6585 let install_dir = crate::path::install_dir().ok();
86+ let home = self.home_dir();
6687 let ctx = crate::config::cache::interpolate_context(install_dir.as_deref(), home.as_deref());
67− self.resolved
68− .path_filter("core", None, "excludesFile", &mut self.filter_config_section.clone())
69− .map(|p| p.interpolate(ctx).map(|p| p.into_owned()))
88+ Some(path.interpolate(ctx))
7089 }
7190
7291 pub(crate) fn apply_leniency<T, E>(&self, res: Option<Result<T, E>>) -> Result<Option<T>, E> {
git-repository/src/config/snapshot/access.rs+4−11
@@ -4,7 +4,7 @@ use git_features::threading::OwnShared; · 3 unmodified lines
44
55 use crate::{
66 bstr::BStr,
7− config::{cache::interpolate_context, CommitAutoRollback, Snapshot, SnapshotMut},
7+ config::{CommitAutoRollback, Snapshot, SnapshotMut},
88 };
99
1010 /// Access configuration values, frozen in time, using a `key` which is a `.` separated string of up to
@@ -70,16 +70,9 @@ impl<'repo> Snapshot<'repo> { · 59 unmodified lines
7070 key: &str,
7171 ) -> Option<Result<Cow<'_, std::path::Path>, git_config::path::interpolate::Error>> {
7272 let key = git_config::parse::key(key)?;
73− let path = self.repo.config.resolved.path_filter(
74− key.section_name,
75− key.subsection_name,
76− key.value_name,
77− &mut self.repo.filter_config_section(),
78− )?;
79−
80− let install_dir = self.repo.install_dir().ok();
81− let home = self.repo.config.home_dir();
82− Some(path.interpolate(interpolate_context(install_dir.as_deref(), home.as_deref())))
73+ self.repo
74+ .config
75+ .trusted_file_path(key.section_name, key.subsection_name, key.value_name)
8376 }
8477 }
8578