This commit is contained in:
Steven Merino 2025-07-05 15:10:51 +02:00 committed by GitHub
commit 4e7b1d279d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,7 +4,10 @@ This provides functionality similar to `--include` or `--exclude` in command
line tools.
*/
use std::path::Path;
use std::{
path::Path,
sync::atomic::{AtomicBool, Ordering},
};
use crate::{
gitignore::{self, Gitignore, GitignoreBuilder},
@ -101,14 +104,24 @@ impl Override {
if self.is_empty() {
return Match::None;
}
let mat = self.0.matched(path, is_dir).invert();
if mat.is_none() && self.num_whitelists() > 0 && !is_dir {
let mat = if INVERTED_MATCHING.load(Ordering::Relaxed) {
self.0.matched(path, is_dir).invert()
} else {
self.0.matched(path, is_dir)
};
if mat.is_none()
&& (self.num_whitelists() > 0
&& INVERTED_MATCHING.load(Ordering::Relaxed))
&& !is_dir
{
return Match::Ignore(Glob::unmatched());
}
mat.map(move |giglob| Glob(GlobInner::Matched(giglob)))
}
}
static INVERTED_MATCHING: AtomicBool = AtomicBool::new(true);
/// Builds a matcher for a set of glob overrides.
#[derive(Clone, Debug)]
pub struct OverrideBuilder {
@ -141,6 +154,17 @@ impl OverrideBuilder {
Ok(self)
}
/// Enables inverting the gitignore file matching.
///
/// If enabled it acts as an --include
/// If disabled it acts as an --exclude
///
/// This is enabled by default.
pub fn inverted_matching(&mut self, yes: bool) -> &mut OverrideBuilder {
INVERTED_MATCHING.store(yes, Ordering::Relaxed);
self
}
/// Toggle whether the globs should be matched case insensitively or not.
///
/// When this option is changed, only globs added after the change will be affected.