Skip to content

Commit

Permalink
Add "--force-exclude" option
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Sep 30, 2023
1 parent 5edf45b commit a430620
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
description: Source code spell checker, binary install
language: python
entry: typos
args: [--write-changes]
args: [--write-changes, --force-exclude]
types: [text]
stages: [commit, merge-commit, push, manual]

Expand All @@ -12,7 +12,7 @@
description: Source code spell checker, Docker image
language: docker
entry: typos
args: [--write-changes]
args: [--write-changes, --force-exclude]
types: [text]
stages: [commit, merge-commit, push, manual]

Expand All @@ -21,6 +21,6 @@
description: Source code spell checker, source install
language: rust
entry: typos
args: [--write-changes]
args: [--write-changes, --force-exclude]
types: [text]
stages: [commit, merge-commit, push, manual]
11 changes: 11 additions & 0 deletions crates/typos-cli/src/bin/typos-cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ pub(crate) struct WalkArgs {
#[arg(long, value_name = "GLOB")]
exclude: Vec<String>,

/// Respect excluded files even for paths passed explicitly.
#[arg(long, overrides_with("force_exclude"), hide = true)]
no_force_exclude: bool,
#[arg(long, overrides_with("no_force_exclude"))]
force_exclude: bool,

/// Search hidden files and directories.
#[arg(long, overrides_with("no_hidden"))]
hidden: bool,
Expand Down Expand Up @@ -236,6 +242,7 @@ impl WalkArgs {
pub fn to_config(&self) -> config::Walk {
config::Walk {
extend_exclude: self.exclude.clone(),
force_exclude: self.force_exclude(),
ignore_hidden: self.ignore_hidden(),
ignore_files: self.ignore_files(),
ignore_dot: self.ignore_dot(),
Expand All @@ -245,6 +252,10 @@ impl WalkArgs {
}
}

fn force_exclude(&self) -> Option<bool> {
resolve_bool_arg(self.force_exclude, self.no_force_exclude)
}

fn ignore_hidden(&self) -> Option<bool> {
resolve_bool_arg(self.no_hidden, self.hidden)
}
Expand Down
6 changes: 6 additions & 0 deletions crates/typos-cli/src/bin/typos-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
let overrides = overrides
.build()
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
if walk_policy.force_exclude() && path.is_file() {
match overrides.matched(path, false) {
ignore::Match::Ignore(_) => continue,
_ => (),
}

Check failure

Code scanning / clippy

you seem to be trying to use match for destructuring a single pattern. Consider using if let Error

you seem to be trying to use match for destructuring a single pattern. Consider using if let
}
walk.overrides(overrides);
}

Expand Down
10 changes: 10 additions & 0 deletions crates/typos-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ impl Config {
#[serde(rename_all = "kebab-case")]
pub struct Walk {
pub extend_exclude: Vec<String>,
// Exclude files even if passed explicitly.
pub force_exclude: Option<bool>,
/// Skip hidden files and directories.
pub ignore_hidden: Option<bool>,
/// Respect ignore files.
Expand All @@ -117,6 +119,7 @@ impl Walk {
let empty = Self::default();
Self {
extend_exclude: empty.extend_exclude.clone(),
force_exclude: Some(false),
ignore_hidden: Some(empty.ignore_hidden()),
ignore_files: Some(true),
ignore_dot: Some(empty.ignore_dot()),
Expand All @@ -129,6 +132,9 @@ impl Walk {
pub fn update(&mut self, source: &Walk) {
self.extend_exclude
.extend(source.extend_exclude.iter().cloned());
if let Some(source) = source.force_exclude {
self.force_exclude = Some(source);
}
if let Some(source) = source.ignore_hidden {
self.ignore_hidden = Some(source);
}
Expand Down Expand Up @@ -158,6 +164,10 @@ impl Walk {
&self.extend_exclude
}

pub fn force_exclude(&self) -> bool {
self.force_exclude.unwrap_or(false)
}

pub fn ignore_hidden(&self) -> bool {
self.ignore_hidden.unwrap_or(true)
}
Expand Down
5 changes: 5 additions & 0 deletions crates/typos-cli/tests/cmd/force-exclude.in/_typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[files]
extend-exclude = ["file.ignore"]

[default.extend-identifiers]
hello = "goodbye"
1 change: 1 addition & 0 deletions crates/typos-cli/tests/cmd/force-exclude.in/file.ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
6 changes: 6 additions & 0 deletions crates/typos-cli/tests/cmd/force-exclude.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin.name = "typos"
args = "file.ignore --force-exclude"
stdin = ""
stdout = ""
stderr = ""
status.code = 0
1 change: 1 addition & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Configuration is read from the following (in precedence order)
|------------------------|-------------------|--------|-------------|
| files.binary | --binary | bool | Check binary files as text |
| files.extend-exclude | --exclude | list of strings | Typos-specific ignore globs (gitignore syntax) |
| file.force-excude | --force-exclude | bool | Respect excluded files even for paths passed explicitly. |
| files.ignore-hidden | --hidden | bool | Skip hidden files and directories. |
| files.ignore-files | --ignore | bool | Respect ignore files. |
| files.ignore-dot | --ignore-dot | bool | Respect .ignore files. |
Expand Down

0 comments on commit a430620

Please sign in to comment.