Skip to content

Commit

Permalink
Merge pull request #255 from epage/ignore
Browse files Browse the repository at this point in the history
feat(ignore): Typos=specific ignores
  • Loading branch information
epage committed May 21, 2021
2 parents 0cf1675 + 327a84a commit 5eb4665
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/reference.md
Expand Up @@ -14,6 +14,8 @@ Configuration is read from the following (in precedence order)

| Field | Argument | Format | Description |
|------------------------|-------------------|--------|-------------|
| files.binary | --binary | bool | Check binary files as text |
| files.extend-exclude | --exclude | list of strings | Typos-specific ignore globs (gitignore syntax) |
| 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
5 changes: 5 additions & 0 deletions src/bin/typos-cli/args.rs
Expand Up @@ -200,6 +200,10 @@ impl ConfigArgs {
#[derive(Debug, StructOpt)]
#[structopt(rename_all = "kebab-case")]
pub(crate) struct WalkArgs {
#[structopt(long)]
/// Ignore all files & directories matching the pattern.
exclude: Vec<String>,

#[structopt(long, overrides_with("no-hidden"))]
/// Search hidden files and directories.
hidden: bool,
Expand Down Expand Up @@ -240,6 +244,7 @@ pub(crate) struct WalkArgs {
impl WalkArgs {
pub fn to_config(&self) -> config::Walk {
config::Walk {
extend_exclude: self.exclude.clone(),
ignore_hidden: self.ignore_hidden(),
ignore_files: self.ignore_files(),
ignore_dot: self.ignore_dot(),
Expand Down
10 changes: 10 additions & 0 deletions src/bin/typos-cli/main.rs
Expand Up @@ -196,6 +196,16 @@ fn run_checks(
.git_ignore(walk_policy.ignore_vcs())
.git_exclude(walk_policy.ignore_vcs())
.parents(walk_policy.ignore_parent());
if !walk_policy.extend_exclude.is_empty() {
let mut overrides = ignore::overrides::OverrideBuilder::new(".");
for pattern in walk_policy.extend_exclude.iter() {
overrides
.add(&format!("!{}", pattern))
.with_code(proc_exit::Code::CONFIG_ERR)?;
}
let overrides = overrides.build().with_code(proc_exit::Code::CONFIG_ERR)?;
walk.overrides(overrides);
}

// HACK: Diff doesn't handle mixing content
let output_reporter = if args.diff {
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Expand Up @@ -61,6 +61,7 @@ impl Config {
#[serde(deny_unknown_fields, default)]
#[serde(rename_all = "kebab-case")]
pub struct Walk {
pub extend_exclude: Vec<String>,
/// Skip hidden files and directories.
pub ignore_hidden: Option<bool>,
/// Respect ignore files.
Expand All @@ -79,6 +80,7 @@ impl Walk {
pub fn from_defaults() -> Self {
let empty = Self::default();
Self {
extend_exclude: empty.extend_exclude.clone(),
ignore_hidden: Some(empty.ignore_hidden()),
ignore_files: Some(true),
ignore_dot: Some(empty.ignore_dot()),
Expand All @@ -89,6 +91,8 @@ impl Walk {
}

pub fn update(&mut self, source: &Walk) {
self.extend_exclude
.extend(source.extend_exclude.iter().cloned());
if let Some(source) = source.ignore_hidden {
self.ignore_hidden = Some(source);
}
Expand All @@ -114,6 +118,10 @@ impl Walk {
}
}

pub fn extend_exclude(&self) -> &[String] {
&self.extend_exclude
}

pub fn ignore_hidden(&self) -> bool {
self.ignore_hidden.unwrap_or(true)
}
Expand Down

0 comments on commit 5eb4665

Please sign in to comment.