diff options
author | Andrew Gallant <jamslam@gmail.com> | 2018-09-04 22:45:24 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2018-09-04 23:18:55 -0400 |
commit | 241bc8f8fcfdc725afa65ee539c37960b10550b1 (patch) | |
tree | d5ae44155e4d3c91f6a70c5a23f2d2d29d99d668 /src/search.rs | |
parent | b6e30124e07fb3b3530bae2c0cf19e0893aa9831 (diff) |
ripgrep: add --pre-glob flag
The --pre-glob flag is like the --glob flag, except it applies to filtering
files through the preprocessor instead of for search. This makes it
possible to apply the preprocessor to only a small subset of files, which
can greatly reduce the process overhead of using a preprocessor when
searching large directories.
Diffstat (limited to 'src/search.rs')
-rw-r--r-- | src/search.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/search.rs b/src/search.rs index 457f8f7a..9baf513f 100644 --- a/src/search.rs +++ b/src/search.rs @@ -11,6 +11,7 @@ use grep::pcre2::{RegexMatcher as PCRE2RegexMatcher}; use grep::printer::{JSON, Standard, Summary, Stats}; use grep::regex::{RegexMatcher as RustRegexMatcher}; use grep::searcher::Searcher; +use ignore::overrides::Override; use serde_json as json; use termcolor::WriteColor; @@ -23,6 +24,7 @@ use subject::Subject; struct Config { json_stats: bool, preprocessor: Option<PathBuf>, + preprocessor_globs: Override, search_zip: bool, } @@ -31,6 +33,7 @@ impl Default for Config { Config { json_stats: false, preprocessor: None, + preprocessor_globs: Override::empty(), search_zip: false, } } @@ -108,6 +111,17 @@ impl SearchWorkerBuilder { self } + /// Set the globs for determining which files should be run through the + /// preprocessor. By default, with no globs and a preprocessor specified, + /// every file is run through the preprocessor. + pub fn preprocessor_globs( + &mut self, + globs: Override, + ) -> &mut SearchWorkerBuilder { + self.config.preprocessor_globs = globs; + self + } + /// Enable the decompression and searching of common compressed files. /// /// When enabled, if a particular file path is recognized as a compressed @@ -298,7 +312,7 @@ impl<W: WriteColor> SearchWorker<W> { let stdin = io::stdin(); // A `return` here appeases the borrow checker. NLL will fix this. return self.search_reader(path, stdin.lock()); - } else if self.config.preprocessor.is_some() { + } else if self.should_preprocess(path) { self.search_preprocessor(path) } else if self.should_decompress(path) { self.search_decompress(path) @@ -316,6 +330,20 @@ impl<W: WriteColor> SearchWorker<W> { self.decomp_builder.get_matcher().has_command(path) } + /// Returns true if and only if the given file path should be run through + /// the preprocessor. + fn should_preprocess(&self, path: &Path) -> bool { + if !self.config.preprocessor.is_some() { + return false; + } + if self.config.preprocessor_globs.is_empty() { + return true; + } + !self.config.preprocessor_globs.matched(path, false).is_ignore() + } + + /// Search the given file path by first asking the preprocessor for the + /// data to search instead of opening the path directly. fn search_preprocessor( &mut self, path: &Path, @@ -333,6 +361,9 @@ impl<W: WriteColor> SearchWorker<W> { }) } + /// Attempt to decompress the data at the given file path and search the + /// result. If the given file path isn't recognized as a compressed file, + /// then search it without doing any decompression. fn search_decompress( &mut self, path: &Path, |