Skip to content

Commit

Permalink
Merge pull request #1100 from epage/invert
Browse files Browse the repository at this point in the history
fix(cli): Allow negative expressions in extend-exclude
  • Loading branch information
epage authored Sep 16, 2024
2 parents 945d407 + ad3538f commit c4a6592
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
24 changes: 16 additions & 8 deletions crates/typos-cli/src/bin/typos-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,27 +241,35 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
walk.sort_by_file_name(|a, b| a.cmp(b));
}
if !walk_policy.extend_exclude.is_empty() {
let mut overrides = ignore::overrides::OverrideBuilder::new(".");
let mut ignores = ignore::gitignore::GitignoreBuilder::new(".");
for pattern in walk_policy.extend_exclude.iter() {
overrides
.add(&format!("!{pattern}"))
ignores
.add_line(None, pattern)
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
}
let overrides = overrides
.build()
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
let ignores = ignores.build().with_code(proc_exit::sysexits::CONFIG_ERR)?;
if args.force_exclude {
let mut ancestors = path.ancestors().collect::<Vec<_>>();
ancestors.reverse();
for path in ancestors {
match overrides.matched(path, path.is_dir()) {
match ignores.matched(path, path.is_dir()) {
ignore::Match::None => {}
ignore::Match::Ignore(_) => continue 'path,
ignore::Match::Whitelist(_) => break,
}
}
}
walk.overrides(overrides);
walk.filter_entry(move |entry| {
let path = entry.path();
let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
let matched = ignores.matched(path, is_dir);
log::debug!("match({path:?}, {is_dir}) == {matched:?}");
match matched {
ignore::Match::None => true,
ignore::Match::Ignore(_) => false,
ignore::Match::Whitelist(_) => true,
}
});
}

// HACK: Diff doesn't handle mixing content
Expand Down
12 changes: 12 additions & 0 deletions crates/typos-cli/tests/cmd/extend-exclude-inverted.in/_typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[files]
extend-exclude = [
"*",
"!checked/",
"!checked/**",
]
ignore-hidden = true
ignore-files = true
ignore-dot = true
ignore-vcs = true
ignore-global = true
ignore-parent = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hte

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hte

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hte

12 changes: 12 additions & 0 deletions crates/typos-cli/tests/cmd/extend-exclude-inverted.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bin.name = "typos"
stdin = ""
stdout = """
error: `hte` should be `the`
--> ./checked/file.txt:1:1
|
1 | hte
| ^^^
|
"""
stderr = ""
status.code = 2

0 comments on commit c4a6592

Please sign in to comment.