Skip to content

Commit

Permalink
ignore: add inverted_matching option for OverrideBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
yuuhikaze committed Dec 9, 2024
1 parent 79cbe89 commit 36ac416
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions crates/ignore/src/overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 36ac416

Please sign in to comment.