From 0e8148fbbdfdf4412219b962474ebd9672c6bc24 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 16 Sep 2024 11:40:31 -0400 Subject: [PATCH 1/4] test(cli): Show inverted extend-exclude behavior --- .../tests/cmd/extend-exclude-inverted.in/_typos.toml | 12 ++++++++++++ .../cmd/extend-exclude-inverted.in/checked/file.txt | 2 ++ .../tests/cmd/extend-exclude-inverted.in/file.txt | 2 ++ .../extend-exclude-inverted.in/unchecked/file.txt | 2 ++ .../typos-cli/tests/cmd/extend-exclude-inverted.toml | 4 ++++ 5 files changed, 22 insertions(+) create mode 100644 crates/typos-cli/tests/cmd/extend-exclude-inverted.in/_typos.toml create mode 100644 crates/typos-cli/tests/cmd/extend-exclude-inverted.in/checked/file.txt create mode 100644 crates/typos-cli/tests/cmd/extend-exclude-inverted.in/file.txt create mode 100644 crates/typos-cli/tests/cmd/extend-exclude-inverted.in/unchecked/file.txt create mode 100644 crates/typos-cli/tests/cmd/extend-exclude-inverted.toml diff --git a/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/_typos.toml b/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/_typos.toml new file mode 100644 index 0000000..6e5c8fd --- /dev/null +++ b/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/_typos.toml @@ -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 diff --git a/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/checked/file.txt b/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/checked/file.txt new file mode 100644 index 0000000..e7cbd35 --- /dev/null +++ b/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/checked/file.txt @@ -0,0 +1,2 @@ +hte + diff --git a/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/file.txt b/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/file.txt new file mode 100644 index 0000000..e7cbd35 --- /dev/null +++ b/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/file.txt @@ -0,0 +1,2 @@ +hte + diff --git a/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/unchecked/file.txt b/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/unchecked/file.txt new file mode 100644 index 0000000..e7cbd35 --- /dev/null +++ b/crates/typos-cli/tests/cmd/extend-exclude-inverted.in/unchecked/file.txt @@ -0,0 +1,2 @@ +hte + diff --git a/crates/typos-cli/tests/cmd/extend-exclude-inverted.toml b/crates/typos-cli/tests/cmd/extend-exclude-inverted.toml new file mode 100644 index 0000000..324f41d --- /dev/null +++ b/crates/typos-cli/tests/cmd/extend-exclude-inverted.toml @@ -0,0 +1,4 @@ +bin.name = "typos" +stdin = "" +stdout = "" +stderr = "" From 777cf42dc2d133b4dd91f2ec52cdd7347f9c3d74 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 16 Sep 2024 13:46:30 -0500 Subject: [PATCH 2/4] refactor(cli): Be explicit in overrides --- crates/typos-cli/src/bin/typos-cli/main.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/typos-cli/src/bin/typos-cli/main.rs b/crates/typos-cli/src/bin/typos-cli/main.rs index ca0638a..f0ba487 100644 --- a/crates/typos-cli/src/bin/typos-cli/main.rs +++ b/crates/typos-cli/src/bin/typos-cli/main.rs @@ -261,7 +261,17 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult { } } } - 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 = overrides.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 From d9b55f966cbffa8e201ffa525cadfc608938b3d0 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 16 Sep 2024 13:06:55 -0400 Subject: [PATCH 3/4] fix(cli): Allow negative expressions in extend-exclude Fixes #1099 --- crates/typos-cli/src/bin/typos-cli/main.rs | 6 +++--- .../typos-cli/tests/cmd/extend-exclude-inverted.toml | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/typos-cli/src/bin/typos-cli/main.rs b/crates/typos-cli/src/bin/typos-cli/main.rs index f0ba487..81e4d35 100644 --- a/crates/typos-cli/src/bin/typos-cli/main.rs +++ b/crates/typos-cli/src/bin/typos-cli/main.rs @@ -244,7 +244,7 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult { let mut overrides = ignore::overrides::OverrideBuilder::new("."); for pattern in walk_policy.extend_exclude.iter() { overrides - .add(&format!("!{pattern}")) + .add(pattern) .with_code(proc_exit::sysexits::CONFIG_ERR)?; } let overrides = overrides @@ -268,8 +268,8 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult { log::debug!("match({path:?}, {is_dir}) == {matched:?}"); match matched { ignore::Match::None => true, - ignore::Match::Ignore(_) => false, - ignore::Match::Whitelist(_) => true, + ignore::Match::Ignore(_) => true, + ignore::Match::Whitelist(_) => false, } }); } diff --git a/crates/typos-cli/tests/cmd/extend-exclude-inverted.toml b/crates/typos-cli/tests/cmd/extend-exclude-inverted.toml index 324f41d..b8e837b 100644 --- a/crates/typos-cli/tests/cmd/extend-exclude-inverted.toml +++ b/crates/typos-cli/tests/cmd/extend-exclude-inverted.toml @@ -1,4 +1,12 @@ bin.name = "typos" stdin = "" -stdout = "" +stdout = """ +error: `hte` should be `the` + --> ./checked/file.txt:1:1 + | +1 | hte + | ^^^ + | +""" stderr = "" +status.code = 2 From ad3538f7199c880c450a1ea0b5b465c187cece5c Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 16 Sep 2024 13:37:31 -0500 Subject: [PATCH 4/4] refactor(cli): Switch from Overrides to Gitignore Avoid any double negatives that can cause confusion --- crates/typos-cli/src/bin/typos-cli/main.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/typos-cli/src/bin/typos-cli/main.rs b/crates/typos-cli/src/bin/typos-cli/main.rs index 81e4d35..9dd8773 100644 --- a/crates/typos-cli/src/bin/typos-cli/main.rs +++ b/crates/typos-cli/src/bin/typos-cli/main.rs @@ -241,20 +241,18 @@ 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(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::>(); 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, @@ -264,12 +262,12 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult { 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 = overrides.matched(path, is_dir); + let matched = ignores.matched(path, is_dir); log::debug!("match({path:?}, {is_dir}) == {matched:?}"); match matched { ignore::Match::None => true, - ignore::Match::Ignore(_) => true, - ignore::Match::Whitelist(_) => false, + ignore::Match::Ignore(_) => false, + ignore::Match::Whitelist(_) => true, } }); }