unify access to trusted file paths, allowing it to be queried through the snapshot and as easily through the config cache directly.
Unified · Split
| @@ -1,3 +1,4 @@ | |||
| 1 | use std::borrow::Cow; | ||
| 1 | use std::{convert::TryInto, path::PathBuf, time::Duration}; | 2 | use std::{convert::TryInto, path::PathBuf, time::Duration}; |
| 2 | 3 | ||
| 3 | use git_lock::acquire::Fail; | 4 | use git_lock::acquire::Fail; |
| @@ -61,12 +62,30 @@ impl Cache { · 57 unmodified lines | |||
| 61 | 62 | ||
| 62 | /// The path to the user-level excludes file to ignore certain files in the worktree. | 63 | /// The path to the user-level excludes file to ignore certain files in the worktree. |
| 63 | pub(crate) fn excludes_file(&self) -> Option<Result<PathBuf, git_config::path::interpolate::Error>> { | 64 | 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 | |||
| 65 | let install_dir = crate::path::install_dir().ok(); | 85 | let install_dir = crate::path::install_dir().ok(); |
| 86 | let home = self.home_dir(); | ||
| 66 | let ctx = crate::config::cache::interpolate_context(install_dir.as_deref(), home.as_deref()); | 87 | let ctx = crate::config::cache::interpolate_context(install_dir.as_deref(), home.as_deref()); |
| 67 | self.resolved | 88 | Some(path.interpolate(ctx)) |
| 68 | .path_filter("core", None, "excludesFile", &mut self.filter_config_section.clone()) | ||
| 69 | .map(|p| p.interpolate(ctx).map(|p| p.into_owned())) | ||
| 70 | } | 89 | } |
| 71 | 90 | ||
| 72 | pub(crate) fn apply_leniency<T, E>(&self, res: Option<Result<T, E>>) -> Result<Option<T>, E> { | 91 | pub(crate) fn apply_leniency<T, E>(&self, res: Option<Result<T, E>>) -> Result<Option<T>, E> { |
| @@ -4,7 +4,7 @@ use git_features::threading::OwnShared; · 3 unmodified lines | |||
| 4 | 4 | ||
| 5 | use crate::{ | 5 | use crate::{ |
| 6 | bstr::BStr, | 6 | bstr::BStr, |
| 7 | config::{cache::interpolate_context, CommitAutoRollback, Snapshot, SnapshotMut}, | 7 | config::{CommitAutoRollback, Snapshot, SnapshotMut}, |
| 8 | }; | 8 | }; |
| 9 | 9 | ||
| 10 | /// Access configuration values, frozen in time, using a `key` which is a `.` separated string of up to | 10 | /// 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 | |||
| 70 | key: &str, | 70 | key: &str, |
| 71 | ) -> Option<Result<Cow<'_, std::path::Path>, git_config::path::interpolate::Error>> { | 71 | ) -> Option<Result<Cow<'_, std::path::Path>, git_config::path::interpolate::Error>> { |
| 72 | let key = git_config::parse::key(key)?; | 72 | let key = git_config::parse::key(key)?; |
| 73 | let path = self.repo.config.resolved.path_filter( | 73 | self.repo |
| 74 | key.section_name, | 74 | .config |
| 75 | key.subsection_name, | 75 | .trusted_file_path(key.section_name, key.subsection_name, key.value_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()))) | ||
| 83 | } | 76 | } |
| 84 | } | 77 | } |
| 85 | 78 | ||