Skip to content

Commit 28249bd

Browse files
committed
feat: add Worktree::pathspec() to easily get worktree-scoped pathspec searches.
1 parent 59bb3c4 commit 28249bd

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

gix/src/config/tree/sections/gitoxide.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ mod subsections {
337337
pub const ICASE: keys::Boolean = keys::Boolean::new_boolean("icase", &Gitoxide::PATHSPEC)
338338
.with_environment_override("GIT_ICASE_PATHSPECS")
339339
.with_note("Compare string in a case-insensitive manner");
340+
/// The `gitoxide.pathspec.inheritIgnoreCase` key, defaulting to `true` if unspecified.
341+
/// If set, pathspecs will automatically be match case-insensitively if the underlying filesystem is configured that way.
342+
pub const INHERIT_IGNORE_CASE: keys::Boolean =
343+
keys::Boolean::new_boolean("inheritIgnoreCase", &Gitoxide::PATHSPEC)
344+
.with_note("Inherit `core.ignoreCase` for defaults in pathspecs");
345+
/// The default value for `gitoxide.pathspec.inheritIgnoreCase`.
346+
pub const INHERIT_IGNORE_CASE_DEFAULT: bool = true;
340347
}
341348

342349
impl Section for Pathspec {
@@ -345,7 +352,13 @@ mod subsections {
345352
}
346353

347354
fn keys(&self) -> &[&dyn Key] {
348-
&[&Self::GLOB, &Self::NOGLOB, &Self::LITERAL, &Self::ICASE]
355+
&[
356+
&Self::GLOB,
357+
&Self::NOGLOB,
358+
&Self::LITERAL,
359+
&Self::ICASE,
360+
&Self::INHERIT_IGNORE_CASE,
361+
]
349362
}
350363

351364
fn parent(&self) -> Option<&dyn Section> {

gix/src/worktree/mod.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ pub mod excludes {
141141

142142
///
143143
pub mod attributes {
144-
/// The error returned by [`Worktree::attributes()`][crate::Worktree::attributes()].
144+
use crate::Worktree;
145+
146+
/// The error returned by [`Worktree::attributes()`].
145147
#[derive(Debug, thiserror::Error)]
146148
#[allow(missing_docs)]
147149
pub enum Error {
@@ -151,7 +153,7 @@ pub mod attributes {
151153
CreateCache(#[from] crate::repository::attributes::Error),
152154
}
153155

154-
impl<'repo> crate::Worktree<'repo> {
156+
impl<'repo> Worktree<'repo> {
155157
/// Configure a file-system cache checking if files below the repository are excluded or for querying their attributes.
156158
///
157159
/// This takes into consideration all the usual repository configuration, namely:
@@ -180,3 +182,58 @@ pub mod attributes {
180182
}
181183
}
182184
}
185+
186+
///
187+
pub mod pathspec {
188+
use crate::bstr::BStr;
189+
use crate::config::cache::util::ApplyLeniencyDefaultValue;
190+
use crate::config::tree::gitoxide;
191+
use crate::Worktree;
192+
193+
/// The error returned by [`Worktree::pathspec()`].
194+
#[derive(Debug, thiserror::Error)]
195+
#[allow(missing_docs)]
196+
pub enum Error {
197+
#[error(transparent)]
198+
Init(#[from] crate::pathspec::init::Error),
199+
#[error(transparent)]
200+
OpenIndex(#[from] crate::worktree::open_index::Error),
201+
}
202+
203+
impl<'repo> Worktree<'repo> {
204+
/// Configure pathspecs `patterns` to be matched against, with pathspec attributes read from the worktree and then from the index
205+
/// if needed.
206+
///
207+
/// ### Deviation
208+
///
209+
/// Pathspec attributes match case-insensitively by default if the underlying filesystem is configured that way.
210+
pub fn pathspec(
211+
&self,
212+
patterns: impl IntoIterator<Item = impl AsRef<BStr>>,
213+
) -> Result<crate::Pathspec<'repo>, Error> {
214+
let index = self.index()?;
215+
let inherit_ignore_case = self
216+
.parent
217+
.config
218+
.resolved
219+
.boolean_by_key("gitoxide.pathspec.inheritIgnoreCase")
220+
.map(|res| {
221+
gitoxide::Pathspec::INHERIT_IGNORE_CASE
222+
.enrich_error(res)
223+
.with_lenient_default_value(
224+
self.parent.config.lenient_config,
225+
gitoxide::Pathspec::INHERIT_IGNORE_CASE_DEFAULT,
226+
)
227+
})
228+
.transpose()
229+
.map_err(|err| Error::Init(crate::pathspec::init::Error::Defaults(err.into())))?
230+
.unwrap_or(gitoxide::Pathspec::INHERIT_IGNORE_CASE_DEFAULT);
231+
Ok(self.parent.pathspec(
232+
patterns,
233+
inherit_ignore_case,
234+
&index,
235+
gix_worktree::cache::state::attributes::Source::WorktreeThenIdMapping,
236+
)?)
237+
}
238+
}
239+
}

0 commit comments

Comments
 (0)