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
Provenance
- Commit
5ad69fb- Tree
eff0824- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
19afb15
5 files changed+92 added−11 removed
modified:src/app.rs+24 added-11 removed
80 unmodified lines81 .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 lines118 .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 lines166 , "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 line178 '{}': 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 linesmodified:src/internal.rs+3 added
75 unmodified lines76 pub extension: Option<String>,77 78 /// If a value is supplied, each item found will be used to generate and execute commands.79 pub command: Option<TokenizedCommand>,80+81+ /// A list of glob patterns that should be excluded from the search.82+ pub exclude_patterns: Vec<String>,83 }84 85 /// Print error message to stderr and exit with status `1`.86 pub fn error(message: &str) -> ! {28 unmodified linesmodified:src/main.rs+4 added
139 unmodified lines140 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 };149 150 match RegexBuilder::new(pattern)151 .case_insensitive(!config.case_sensitive)6 unmodified linesmodified:src/walk.rs+14 added
16 unmodified lines17 use std::thread;18 use std::time;19 20 use ignore::{self, WalkBuilder};21+use ignore::overrides::OverrideBuilder;22 use regex::Regex;23 24 /// The receiver thread can either be buffering results or directly streaming to the console.25 enum ReceiverMode {22 unmodified lines48 pub fn scan(root: &Path, pattern: Arc<Regex>, config: Arc<FdOptions>) {49 let (tx, rx) = channel();50 let threads = config.threads;51 52+ let mut override_builder = OverrideBuilder::new(root);53+54+ 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+ });63+64 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 linesmodified:tests/tests.rs+47 added
536 unmodified lines537 abs_path = &abs_path538 ),539 );540 }541+542+/// Exclude patterns (--exclude)543+#[test]544+fn test_excludes() {545+ let te = TestEnv::new();546+547+ te.assert_output(548+ &["--exclude", "*.foo"],549+ "one550+ one/two551+ one/two/C.Foo2552+ one/two/three553+ one/two/three/directory_foo554+ symlink",555+ );556+557+ te.assert_output(558+ &["--exclude", "*.foo", "--exclude", "*.Foo2"],559+ "one560+ one/two561+ one/two/three562+ one/two/three/directory_foo563+ symlink",564+ );565+566+ te.assert_output(567+ &["--exclude", "*.foo", "--exclude", "*.Foo2", "foo"],568+ "one/two/three/directory_foo",569+ );570+571+ te.assert_output(572+ &["--exclude", "one/two", "foo"],573+ "a.foo574+ one/b.foo",575+ );576+577+ 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+}