Show error if pattern matches leading dot but --hidden is not given, closes #615
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 17bd256 · Browse files · 3 files·+75 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+75 added−1 removed
- tests:+15 added−0 removed
- other:+60 added−1 removed
Provenance
- Commit
cadaef3- Tree
1091fb0- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
17bd256
3 files changed+75 added−1 removed
modified:src/main.rs+12 added-1 removed
28 unmodified lines29 #[cfg(unix)]30 use crate::filter::OwnerFilter;31 use crate::filter::{SizeFilter, TimeFilter};32 use crate::options::Options;33-use crate::regex_helper::pattern_has_uppercase_char;33+use crate::regex_helper::{pattern_has_uppercase_char, pattern_matches_strings_with_leading_dot};34 35 // We use jemalloc for performance reasons, see https://github.com/sharkdp/fd/pull/48136 // FIXME: re-enable jemalloc on macOS, see comment in Cargo.toml file for more infos37 #[cfg(all(392 unmodified lines430 }431 }),432 };433 434+ if cfg!(unix)435+ && config.ignore_hidden436+ && pattern_matches_strings_with_leading_dot(&pattern_regex)437+ {438+ return Err(anyhow!(439+ "The pattern seems to only match files with a leading dot, but hidden files are \440+ filtered by default. Consider adding -H/--hidden to search hidden files as well \441+ or adjust your search pattern."442+ ));443+ }444+445 let re = RegexBuilder::new(&pattern_regex)446 .case_insensitive(!config.case_sensitive)447 .dot_matches_new_line(true)448 .build()24 unmodified linesmodified:src/regex_helper.rs+48 added
32 unmodified lines33 _ => false,34 }35 }36 37+/// Determine if a regex pattern only matches strings starting with a literal dot (hidden files)38+pub fn pattern_matches_strings_with_leading_dot(pattern: &str) -> bool {39+ let mut parser = ParserBuilder::new().allow_invalid_utf8(true).build();40+41+ parser42+ .parse(pattern)43+ .map(|hir| hir_matches_strings_with_leading_dot(&hir))44+ .unwrap_or(false)45+}46+47+/// See above.48+fn hir_matches_strings_with_leading_dot(hir: &Hir) -> bool {49+ use regex_syntax::hir::*;50+51+ // Note: this only really detects the simplest case where a regex starts with52+ // "^\\.", i.e. a start text anchor and a literal dot character. There are a lot53+ // of other patterns that ONLY match hidden files, e.g. ^(\\.foo|\\.bar) which are54+ // not (yet) detected by this algorithm.55+ match *hir.kind() {56+ HirKind::Concat(ref hirs) => {57+ let mut hirs = hirs.iter();58+ if let Some(hir) = hirs.next() {59+ if *hir.kind() != HirKind::Anchor(Anchor::StartText) {60+ return false;61+ }62+ } else {63+ return false;64+ }65+66+ if let Some(hir) = hirs.next() {67+ *hir.kind() == HirKind::Literal(Literal::Unicode('.'))68+ } else {69+ false70+ }71+ }72+ _ => false,73+ }74+}75+76 #[test]77 fn pattern_has_uppercase_char_simple() {78 assert!(pattern_has_uppercase_char("A"));79 assert!(pattern_has_uppercase_char("foo.EXE"));8 unmodified lines88 89 assert!(!pattern_has_uppercase_char(r"\Acargo"));90 assert!(!pattern_has_uppercase_char(r"carg\x6F"));91 }92+93+#[test]94+fn matches_strings_with_leading_dot_simple() {95+ assert!(pattern_matches_strings_with_leading_dot("^\\.gitignore"));96+97+ assert!(!pattern_matches_strings_with_leading_dot("^.gitignore"));98+ assert!(!pattern_matches_strings_with_leading_dot("\\.gitignore"));99+ assert!(!pattern_matches_strings_with_leading_dot("^gitignore"));100+}modified:tests/tests.rs+15 added
1697 unmodified lines1698 te.assert_failure(&["--max-buffer-time=a"]);1699 1700 te.assert_failure(&["--max-results=a"]);1701 }1702+1703+/// Print error if search pattern starts with a dot and --hidden is not set1704+/// (Unix only, hidden files on Windows work differently)1705+#[test]1706+#[cfg(unix)]1707+fn test_error_if_hidden_not_set_and_pattern_starts_with_dot() {1708+ let te = TestEnv::new(&[], &[".gitignore", ".whatever", "non-hidden"]);1709+1710+ te.assert_failure(&["^\\.gitignore"]);1711+ te.assert_failure(&["--glob", ".gitignore"]);1712+1713+ te.assert_output(&["--hidden", "^\\.gitignore"], ".gitignore");1714+ te.assert_output(&["--hidden", "--glob", ".gitignore"], ".gitignore");1715+ te.assert_output(&[".gitignore"], "");1716+}