From 8eeb0c0b60da59828a48d995e969bdba5816ea31 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sat, 24 Sep 2016 21:31:24 -0400 Subject: Add --no-ignore-vcs flag. This flag will respect .ignore but not .gitignore. Closes #68. --- doc/rg.1 | 6 ++++++ doc/rg.1.md | 4 ++++ src/args.rs | 10 ++++++++++ src/ignore.rs | 32 ++++++++++++++++++++++++++------ tests/tests.rs | 12 ++++++++++++ 5 files changed, 58 insertions(+), 6 deletions(-) diff --git a/doc/rg.1 b/doc/rg.1 index df38ba7b..87c563b1 100644 --- a/doc/rg.1 +++ b/doc/rg.1 @@ -227,6 +227,12 @@ Don\[aq]t respect ignore files in parent directories. .RS .RE .TP +.B \-\-no\-ignore\-vcs +Don\[aq]t respect version control ignore files (e.g., .gitignore). +Note that .ignore files will continue to be respected. +.RS +.RE +.TP .B \-p, \-\-pretty Alias for \-\-color=always \-\-heading \-n. .RS diff --git a/doc/rg.1.md b/doc/rg.1.md index 5e02150f..f87c25c0 100644 --- a/doc/rg.1.md +++ b/doc/rg.1.md @@ -147,6 +147,10 @@ the raw speed of grep. --no-ignore-parent : Don't respect ignore files in parent directories. +--no-ignore-vcs +: Don't respect version control ignore files (e.g., .gitignore). + Note that .ignore files will continue to be respected. + -p, --pretty : Alias for --color=always --heading -n. diff --git a/src/args.rs b/src/args.rs index 760c63e7..ddd5e768 100644 --- a/src/args.rs +++ b/src/args.rs @@ -147,6 +147,10 @@ Less common options: --no-ignore-parent Don't respect ignore files in parent directories. + --no-ignore-vcs + Don't respect version control ignore files (e.g., .gitignore). + Note that .ignore files will continue to be respected. + -p, --pretty Alias for --color=always --heading -n. @@ -205,6 +209,7 @@ pub struct RawArgs { flag_no_heading: bool, flag_no_ignore: bool, flag_no_ignore_parent: bool, + flag_no_ignore_vcs: bool, flag_no_line_number: bool, flag_no_mmap: bool, flag_no_filename: bool, @@ -250,6 +255,7 @@ pub struct Args { mmap: bool, no_ignore: bool, no_ignore_parent: bool, + no_ignore_vcs: bool, quiet: bool, replace: Option>, text: bool, @@ -374,6 +380,9 @@ impl RawArgs { no_ignore_parent: // --no-ignore implies --no-ignore-parent self.flag_no_ignore_parent || no_ignore, + no_ignore_vcs: + // --no-ignore implies --no-ignore-vcs + self.flag_no_ignore_vcs || no_ignore, quiet: self.flag_quiet, replace: self.flag_replace.clone().map(|s| s.into_bytes()), text: text, @@ -639,6 +648,7 @@ impl Args { let mut ig = Ignore::new(); ig.ignore_hidden(!self.hidden); ig.no_ignore(self.no_ignore); + ig.no_ignore_vcs(self.no_ignore_vcs); ig.add_types(self.types.clone()); if !self.no_ignore_parent { try!(ig.push_parents(path)); diff --git a/src/ignore.rs b/src/ignore.rs index 7dae9fa9..9a33dde6 100644 --- a/src/ignore.rs +++ b/src/ignore.rs @@ -89,9 +89,11 @@ pub struct Ignore { types: Types, /// Whether to ignore hidden files or not. ignore_hidden: bool, - /// When true, don't look at .gitignore or .agignore files for ignore + /// When true, don't look at .gitignore or .ignore files for ignore /// rules. no_ignore: bool, + /// When true, don't look at .gitignore files for ignore rules. + no_ignore_vcs: bool, } impl Ignore { @@ -104,6 +106,7 @@ impl Ignore { types: Types::empty(), ignore_hidden: true, no_ignore: false, + no_ignore_vcs: true, } } @@ -119,6 +122,12 @@ impl Ignore { self } + /// When set, VCS ignore files are ignored. + pub fn no_ignore_vcs(&mut self, yes: bool) -> &mut Ignore { + self.no_ignore_vcs = yes; + self + } + /// Add a set of globs that overrides all other match logic. pub fn add_override(&mut self, gi: Gitignore) -> &mut Ignore { self.overrides = Overrides::new(Some(gi)); @@ -143,6 +152,9 @@ impl Ignore { let mut path = &*path; let mut saw_git = path.join(".git").is_dir(); let mut ignore_names = IGNORE_NAMES.to_vec(); + if self.no_ignore_vcs { + ignore_names.retain(|&name| name != ".gitignore"); + } let mut ignore_dir_results = vec![]; while let Some(parent) = path.parent() { if self.no_ignore { @@ -173,9 +185,12 @@ impl Ignore { pub fn push>(&mut self, path: P) -> Result<(), Error> { if self.no_ignore { self.stack.push(IgnoreDir::empty(path)); - return Ok(()); + Ok(()) + } else if self.no_ignore_vcs { + self.push_ignore_dir(IgnoreDir::without_vcs(path)) + } else { + self.push_ignore_dir(IgnoreDir::new(path)) } - self.push_ignore_dir(IgnoreDir::new(path)) } /// Pushes the result of building a directory matcher on to the stack. @@ -305,13 +320,18 @@ pub struct IgnoreDir { impl IgnoreDir { /// Create a new matcher for the given directory. - /// - /// If no ignore glob patterns could be found in the directory then `None` - /// is returned. pub fn new>(path: P) -> Result { IgnoreDir::with_ignore_names(path, IGNORE_NAMES.iter()) } + /// Create a new matcher for the given directory. + /// + /// Don't respect VCS ignore files. + pub fn without_vcs>(path: P) -> Result { + let names = IGNORE_NAMES.iter().filter(|name| **name != ".gitignore"); + IgnoreDir::with_ignore_names(path, names) + } + /// Create a new IgnoreDir that never matches anything with the given path. pub fn empty>(path: P) -> IgnoreDir { IgnoreDir { diff --git a/tests/tests.rs b/tests/tests.rs index 55cb2684..e94ec35f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -687,6 +687,18 @@ be, to a very large extent, the result of luck. Sherlock Holmes assert_eq!(lines, expected); }); +// See: https://github.com/BurntSushi/ripgrep/issues/68 +clean!(feature_68, "test", ".", |wd: WorkDir, mut cmd: Command| { + wd.create(".gitignore", "foo"); + wd.create(".ignore", "bar"); + wd.create("foo", "test"); + wd.create("bar", "test"); + cmd.arg("--no-ignore-vcs"); + + let lines: String = wd.stdout(&mut cmd); + assert_eq!(lines, "foo:test\n"); +}); + #[test] fn binary_nosearch() { let wd = WorkDir::new("binary_nosearch"); -- cgit v1.2.3