Skip to content

Commit

Permalink
feat: add Worktree::pathspec() to easily get worktree-scoped pathsp…
Browse files Browse the repository at this point in the history
…ec searches.
  • Loading branch information
Byron committed Aug 21, 2023
1 parent 59bb3c4 commit 28249bd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
15 changes: 14 additions & 1 deletion gix/src/config/tree/sections/gitoxide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ mod subsections {
pub const ICASE: keys::Boolean = keys::Boolean::new_boolean("icase", &Gitoxide::PATHSPEC)
.with_environment_override("GIT_ICASE_PATHSPECS")
.with_note("Compare string in a case-insensitive manner");
/// The `gitoxide.pathspec.inheritIgnoreCase` key, defaulting to `true` if unspecified.
/// If set, pathspecs will automatically be match case-insensitively if the underlying filesystem is configured that way.
pub const INHERIT_IGNORE_CASE: keys::Boolean =
keys::Boolean::new_boolean("inheritIgnoreCase", &Gitoxide::PATHSPEC)
.with_note("Inherit `core.ignoreCase` for defaults in pathspecs");
/// The default value for `gitoxide.pathspec.inheritIgnoreCase`.
pub const INHERIT_IGNORE_CASE_DEFAULT: bool = true;
}

impl Section for Pathspec {
Expand All @@ -345,7 +352,13 @@ mod subsections {
}

fn keys(&self) -> &[&dyn Key] {
&[&Self::GLOB, &Self::NOGLOB, &Self::LITERAL, &Self::ICASE]
&[
&Self::GLOB,
&Self::NOGLOB,
&Self::LITERAL,
&Self::ICASE,
&Self::INHERIT_IGNORE_CASE,
]
}

fn parent(&self) -> Option<&dyn Section> {
Expand Down
61 changes: 59 additions & 2 deletions gix/src/worktree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ pub mod excludes {

///
pub mod attributes {
/// The error returned by [`Worktree::attributes()`][crate::Worktree::attributes()].
use crate::Worktree;

/// The error returned by [`Worktree::attributes()`].
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
Expand All @@ -151,7 +153,7 @@ pub mod attributes {
CreateCache(#[from] crate::repository::attributes::Error),
}

impl<'repo> crate::Worktree<'repo> {
impl<'repo> Worktree<'repo> {
/// Configure a file-system cache checking if files below the repository are excluded or for querying their attributes.
///
/// This takes into consideration all the usual repository configuration, namely:
Expand Down Expand Up @@ -180,3 +182,58 @@ pub mod attributes {
}
}
}

///
pub mod pathspec {
use crate::bstr::BStr;
use crate::config::cache::util::ApplyLeniencyDefaultValue;
use crate::config::tree::gitoxide;
use crate::Worktree;

/// The error returned by [`Worktree::pathspec()`].
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Init(#[from] crate::pathspec::init::Error),
#[error(transparent)]
OpenIndex(#[from] crate::worktree::open_index::Error),
}

impl<'repo> Worktree<'repo> {
/// Configure pathspecs `patterns` to be matched against, with pathspec attributes read from the worktree and then from the index
/// if needed.
///
/// ### Deviation
///
/// Pathspec attributes match case-insensitively by default if the underlying filesystem is configured that way.
pub fn pathspec(
&self,
patterns: impl IntoIterator<Item = impl AsRef<BStr>>,
) -> Result<crate::Pathspec<'repo>, Error> {
let index = self.index()?;
let inherit_ignore_case = self
.parent
.config
.resolved
.boolean_by_key("gitoxide.pathspec.inheritIgnoreCase")
.map(|res| {
gitoxide::Pathspec::INHERIT_IGNORE_CASE
.enrich_error(res)
.with_lenient_default_value(
self.parent.config.lenient_config,
gitoxide::Pathspec::INHERIT_IGNORE_CASE_DEFAULT,
)
})
.transpose()
.map_err(|err| Error::Init(crate::pathspec::init::Error::Defaults(err.into())))?
.unwrap_or(gitoxide::Pathspec::INHERIT_IGNORE_CASE_DEFAULT);
Ok(self.parent.pathspec(
patterns,
inherit_ignore_case,
&index,
gix_worktree::cache::state::attributes::Source::WorktreeThenIdMapping,
)?)
}
}
}

0 comments on commit 28249bd

Please sign in to comment.