Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore: add inverted_matching option for OverrideBuilder #2945

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading