Skip to content
Myr

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

3 files+75 added−1 removed

modified:src/main.rs+12 added-1 removed
28 unmodified lines
29#[cfg(unix)]30use crate::filter::OwnerFilter;31use crate::filter::{SizeFilter, TimeFilter};32use crate::options::Options;33use crate::regex_helper::pattern_has_uppercase_char;33use crate::regex_helper::{pattern_has_uppercase_char, pattern_matches_strings_with_leading_dot};3435// 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 lines
430 }431 }),432 };433434 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 }444445 let re = RegexBuilder::new(&pattern_regex)446 .case_insensitive(!config.case_sensitive)447 .dot_matches_new_line(true)448 .build()
24 unmodified lines
modified:src/regex_helper.rs+48 added
32 unmodified lines
33 _ => false,34 }35}3637/// Determine if a regex pattern only matches strings starting with a literal dot (hidden files)38pub fn pattern_matches_strings_with_leading_dot(pattern: &str) -> bool {39 let mut parser = ParserBuilder::new().allow_invalid_utf8(true).build();4041 parser42 .parse(pattern)43 .map(|hir| hir_matches_strings_with_leading_dot(&hir))44 .unwrap_or(false)45}4647/// See above.48fn hir_matches_strings_with_leading_dot(hir: &Hir) -> bool {49 use regex_syntax::hir::*;5051 // 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 }6566 if let Some(hir) = hirs.next() {67 *hir.kind() == HirKind::Literal(Literal::Unicode('.'))68 } else {69 false70 }71 }72 _ => false,73 }74}7576#[test]77fn pattern_has_uppercase_char_simple() {78 assert!(pattern_has_uppercase_char("A"));79 assert!(pattern_has_uppercase_char("foo.EXE"));
8 unmodified lines
8889 assert!(!pattern_has_uppercase_char(r"\Acargo"));90 assert!(!pattern_has_uppercase_char(r"carg\x6F"));91}9293#[test]94fn matches_strings_with_leading_dot_simple() {95 assert!(pattern_matches_strings_with_leading_dot("^\\.gitignore"));9697 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 lines
1698 te.assert_failure(&["--max-buffer-time=a"]);16991700 te.assert_failure(&["--max-results=a"]);1701}17021703/// 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)]1707fn test_error_if_hidden_not_set_and_pattern_starts_with_dot() {1708 let te = TestEnv::new(&[], &[".gitignore", ".whatever", "non-hidden"]);17091710 te.assert_failure(&["^\\.gitignore"]);1711 te.assert_failure(&["--glob", ".gitignore"]);17121713 te.assert_output(&["--hidden", "^\\.gitignore"], ".gitignore");1714 te.assert_output(&["--hidden", "--glob", ".gitignore"], ".gitignore");1715 te.assert_output(&[".gitignore"], "");1716}