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

fix(cli): Allow negative expressions in extend-exclude #1100

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
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
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
Loading