Implement --ignore-file
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent 8c1b037 · Browse files · 5 files·+58 added−3 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+58 added−3 removed
- tests:+22 added−3 removed
- other:+36 added−0 removed
Provenance
- Commit
3748303- Tree
ef57406- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
8c1b037
5 files changed+58 added−3 removed
modified:src/app.rs+11 added
121 unmodified lines122 .number_of_values(1)123 .multiple(true),124 )125 .arg(126+ arg("ignore-file")127+ .long("ignore-file")128+ .takes_value(true)129+ .value_name("path")130+ .number_of_values(1)131+ .multiple(true),132+ )133+ .arg(134 arg("color")135 .long("color")136 .short("c")137 .takes_value(true)89 unmodified lines227 doc!(h, "exclude"228 , "Exclude entries that match the given glob pattern"229 , "Exclude files/directories that match the given glob pattern. This overrides any \230 other ignore logic. Multiple exclude patterns can be specified.");231+ doc!(h, "ignore-file"232+ , "Add a custom ignore-file in .gitignore format"233+ , "Add a custom ignore-file in .gitignore format. These files have a low precedence.");234 doc!(h, "color"235 , "When to use colors: never, *auto*, always"236 , "Declare when to use color for the pattern match output:\n \237 'auto': show colors if the output goes to an interactive console (default)\n \22 unmodified linesmodified:src/internal.rs+4 added
8 unmodified lines9 use std::ffi::OsString;10 use std::process;11 use std::time;12 use std::io::Write;13+use std::path::PathBuf;14 15 use exec::CommandTemplate;16 use lscolors::LsColors;17 use regex_syntax::Parser;74 unmodified lines92 pub command: Option<CommandTemplate>,93 94 /// A list of glob patterns that should be excluded from the search.95 pub exclude_patterns: Vec<String>,96+97+ /// A list of custom ignore files.98+ pub ignore_files: Vec<PathBuf>,99 }100 101 /// Print error message to stderr and exit with status `1`.102 pub fn error(message: &str) -> ! {155 unmodified linesmodified:src/main.rs+4 added
189 unmodified lines190 exclude_patterns: matches191 .values_of("exclude")192 .map(|v| v.map(|p| String::from("!") + p).collect())193 .unwrap_or_else(|| vec![]),194+ ignore_files: matches195+ .values_of("ignore-file")196+ .map(|vs| vs.map(PathBuf::from).collect())197+ .unwrap_or_else(|| vec![]),198 };199 200 match RegexBuilder::new(&pattern_regex)201 .case_insensitive(!config.case_sensitive)13 unmodified linesmodified:src/walk.rs+17 added
12 unmodified lines13 use internal::{error, FdOptions, EXITCODE_SIGINT, MAX_BUFFER_LENGTH};14 use output;15 16 use std::process;17+use std::error::Error;18 use std::path::PathBuf;19 use std::sync::{Arc, Mutex};20 use std::sync::atomic::{AtomicBool, Ordering};21 use std::sync::mpsc::channel;54 unmodified lines76 if config.read_fdignore {77 walker.add_custom_ignore_filename(".fdignore");78 }79 80+ for ignore_file in &config.ignore_files {81+ let result = walker.add_ignore(ignore_file);82+ if let Some(err) = result {83+ match err {84+ ignore::Error::Partial(_) => (),85+ _ => {86+ error(&format!(87+ "Error while parsing custom ignore file '{}': {}.",88+ ignore_file.to_string_lossy(),89+ err.description()90+ ));91+ }92+ }93+ }94+ }95+96 for path_entry in path_iter {97 walker.add(path_entry.as_path());98 }99 182 unmodified linesmodified:tests/tests.rs+22 added-3 removed
295 unmodified lines296 one/two/three/directory_foo",297 );298 }299 300-/// Custom ignore files300+/// .gitignore and .fdignore301 #[test]302-fn test_custom_ignore() {302+fn test_gitignore_and_fdignore() {303 let files = &[304 "ignored-by-nothing",305 "ignored-by-fdignore",306 "ignored-by-gitignore",27 unmodified lines334 ignored-by-both",335 );336 }337 338-/// Precedence of custom ignore files338+/// Precedence of .fdignore files339 #[test]340 fn test_custom_ignore_precedence() {341 let dirs = &["inner"];342 let files = &["inner/foo"];34 unmodified lines377 one/two/three/directory_foo",378 );379 }380 381+/// Custom ignore files (--ignore-file)382+#[test]383+fn test_custom_ignore_files() {384+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);385+386+ // Ignore 'C.Foo2' and everything in 'three'.387+ fs::File::create(te.test_root().join("custom.ignore"))388+ .unwrap()389+ .write_all(b"C.Foo2\nthree")390+ .unwrap();391+392+ te.assert_output(393+ &["--ignore-file", "custom.ignore", "foo"],394+ "a.foo395+ one/b.foo396+ one/two/c.foo",397+ );398+}399+400 /// Ignored files with ripgrep aliases (-u / -uu)401 #[test]402 fn test_no_ignore_aliases() {403 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);600 unmodified lines