Skip to content
Myr

Add support for exclude-patterns

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add `--exclude`/`-E` option. * Support for multiple exclude patterns Example: ``` bash > fd --exclude 'tests/**/*.rs' mod src/exec/mod.rs src/fshelper/mod.rs src/lscolors/mod.rs ``` Closes #89

authored by sharkdp · committed by David Peter · parent 19afb15 · Browse files · 5 files+92 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+92 added−11 removed

  • tests:+47 added−0 removed
  • other:+45 added−11 removed

5 files+92 added−11 removed

modified:src/app.rs+24 added-11 removed
80 unmodified lines
81 .takes_value(true)82 .value_name("ext"),83 )84 .arg(85 arg("exec")86 .long("exec")87 .short("x")88 .takes_value(true)89 .value_name("cmd"),90 )91 .arg(92 arg("exclude")93 .long("exclude")94 .short("E")95 .takes_value(true)96 .value_name("pattern")97 .number_of_values(1)98 .multiple(true),99 )100 .arg(101 arg("color")102 .long("color")103 .short("c")104 .takes_value(true)
13 unmodified lines
118 .long("max-buffer-time")119 .takes_value(true)120 .hidden(true),121 )106 .arg(107 arg("exec")108 .long("exec")109 .short("x")110 .takes_value(true)111 .value_name("cmd"),112 )122 .arg(arg("pattern"))123 .arg(arg("path"))124}125
40 unmodified lines
166 , "Filter the search by type:\n \167 'f' or 'file': regular files\n \168 'd' or 'directory': directories\n \169 'l' or 'symlink': symbolic links");170 doc!(h, "extension"171 , "Filter by file extension"172 , "(Additionally) filter search results by their file extension.");173 doc!(h, "exec"174 , "Execute the given command for each search result"175 , "Execute the given command for each search result.\n\176 The following are valid tokens that can be used within the expression for generating \
1 unmodified line
178 '{}': places the input in the location of this token\n \179 '{.}': removes the extension from the input\n \180 '{/}': places the basename of the input\n \181 '{//}': places the parent of the input\n \170 '{/.}': places the basename of the input, without the extension\n");171 doc!(h, "extension"172 , "Filter by file extension"173 , "(Additionally) filter search results by their file extension.");182 '{/.}': places the basename of the input, without the extension");183 doc!(h, "exclude"184 , "Exclude entries that match the given glob pattern."185 , "Exclude files/directories that match the given glob pattern. This overrides any \186 other ignore logic. Multiple exclude patterns can be specified.");187 doc!(h, "color"188 , "When to use colors: never, *auto*, always"189 , "Declare when to use color for the pattern match output:\n \190 'auto': show colors if the output goes to an interactive console (default)\n \
22 unmodified lines
modified:src/internal.rs+3 added
75 unmodified lines
76 pub extension: Option<String>,7778 /// If a value is supplied, each item found will be used to generate and execute commands.79 pub command: Option<TokenizedCommand>,8081 /// A list of glob patterns that should be excluded from the search.82 pub exclude_patterns: Vec<String>,83}8485/// Print error message to stderr and exit with status `1`.86pub fn error(message: &str) -> ! {
28 unmodified lines
modified:src/main.rs+4 added
139 unmodified lines
140 extension: matches.value_of("extension").map(|e| {141 e.trim_left_matches('.').to_lowercase()142 }),143 command,144 exclude_patterns: matches145 .values_of("exclude")146 .map(|v| v.map(|p| String::from("!") + p).collect())147 .unwrap_or(vec![]),148 };149150 match RegexBuilder::new(pattern)151 .case_insensitive(!config.case_sensitive)
6 unmodified lines
modified:src/walk.rs+14 added
16 unmodified lines
17use std::thread;18use std::time;1920use ignore::{self, WalkBuilder};21use ignore::overrides::OverrideBuilder;22use regex::Regex;2324/// The receiver thread can either be buffering results or directly streaming to the console.25enum ReceiverMode {
22 unmodified lines
48pub fn scan(root: &Path, pattern: Arc<Regex>, config: Arc<FdOptions>) {49 let (tx, rx) = channel();50 let threads = config.threads;5152 let mut override_builder = OverrideBuilder::new(root);5354 for pattern in config.exclude_patterns.iter() {55 let res = override_builder.add(pattern);56 if res.is_err() {57 error(&format!("Error: malformed exclude pattern '{}'", pattern));58 }59 }60 let overrides = override_builder.build().unwrap_or_else(|_| {61 error("Mismatch in exclude patterns");62 });6364 let walker = WalkBuilder::new(root)65 .hidden(config.ignore_hidden)66 .ignore(config.read_ignore)67 .git_ignore(config.read_ignore)68 .parents(config.read_ignore)69 .git_global(config.read_ignore)70 .git_exclude(config.read_ignore)71 .overrides(overrides)72 .follow_links(config.follow_links)73 .max_depth(config.max_depth)74 .threads(threads)75 .build_parallel();
155 unmodified lines
modified:tests/tests.rs+47 added
536 unmodified lines
537 abs_path = &abs_path538 ),539 );540}541542/// Exclude patterns (--exclude)543#[test]544fn test_excludes() {545 let te = TestEnv::new();546547 te.assert_output(548 &["--exclude", "*.foo"],549 "one550 one/two551 one/two/C.Foo2552 one/two/three553 one/two/three/directory_foo554 symlink",555 );556557 te.assert_output(558 &["--exclude", "*.foo", "--exclude", "*.Foo2"],559 "one560 one/two561 one/two/three562 one/two/three/directory_foo563 symlink",564 );565566 te.assert_output(567 &["--exclude", "*.foo", "--exclude", "*.Foo2", "foo"],568 "one/two/three/directory_foo",569 );570571 te.assert_output(572 &["--exclude", "one/two", "foo"],573 "a.foo574 one/b.foo",575 );576577 te.assert_output(578 &["--exclude", "one/**/*.foo"],579 "a.foo580 one581 one/two582 one/two/C.Foo2583 one/two/three584 one/two/three/directory_foo585 symlink",586 );587}