From 7402db7b43191c84b47c532b24c831e64b4c212c Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Tue, 20 Sep 2016 20:24:03 -0400 Subject: Add "unrestricted" flag. I don't like having multiple flags do the same thing, but -u, -uu and -uuu are much easier to remember, particularly with -uuu meaning "search everything." --- README-NEW.md | 9 ++++++--- src/args.rs | 18 ++++++++++++++---- tests/tests.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/README-NEW.md b/README-NEW.md index 146df5bf..9941b311 100644 --- a/README-NEW.md +++ b/README-NEW.md @@ -120,7 +120,7 @@ simply not work on UTF-16 encoded files or other more exotic encodings. happen.](https://github.com/BurntSushi/ripgrep/issues/1) To recursively search the current directory, while respecting all `.gitignore` -files: +files, ignore hidden files and directories and skip binary files: ``` $ rg foobar @@ -131,10 +131,13 @@ directories. `.rgignore` files can be used when `.gitignore` files are insufficient. In all cases, `.rgignore` patterns take precedence over `.gitignore`. -To ignore all ignore files, use `--no-ignore`: +To ignore all ignore files, use `-u`. To additionally search hidden files +and directories, use `-uu`. To additionally search binary files, use `-uuu`. +(In other words, "search everything, dammit!") In particular, `rg -uuu` is +equivalent to `grep -r`. ``` -$ rg --no-ignore foobar +$ rg -uuu foobar # equivalent to `grep -r` ``` (Tip: If your ignore files aren't being adhered to like you expect, run your diff --git a/src/args.rs b/src/args.rs index 3481333b..63080703 100644 --- a/src/args.rs +++ b/src/args.rs @@ -74,6 +74,12 @@ Common options: to list all available types. -T, --type-not TYPE ... Do not search files matching TYPE. Multiple not-type flags may be provided. + -u, --unrestricted ... Reduce the level of 'smart' searching. A + single -u doesn't respect .gitignore (etc.) + files. Two -u flags will search hidden files + and directories. Three -u flags will search + binary files. -uu is equivalent to grep -r, + and -uuu is equivalent to grep -a -r. -v, --invert-match Invert matching. -w, --word-regexp Only show matches surrounded by word boundaries. This is equivalent to putting \\b before and @@ -199,6 +205,7 @@ pub struct RawArgs { flag_type_list: bool, flag_type_add: Vec, flag_type_clear: Vec, + flag_unrestricted: u32, flag_with_filename: bool, flag_word_regexp: bool, } @@ -312,6 +319,9 @@ impl RawArgs { .line_terminator(eol) .build() ); + let no_ignore = self.flag_no_ignore || self.flag_unrestricted >= 1; + let hidden = self.flag_hidden || self.flag_unrestricted >= 2; + let text = self.flag_text || self.flag_unrestricted >= 3; let mut args = Args { pattern: pattern, paths: paths, @@ -327,18 +337,18 @@ impl RawArgs { glob_overrides: glob_overrides, grep: grep, heading: !self.flag_no_heading && self.flag_heading, - hidden: self.flag_hidden, + hidden: hidden, ignore_case: self.flag_ignore_case, invert_match: self.flag_invert_match, line_number: !self.flag_no_line_number && self.flag_line_number, mmap: mmap, - no_ignore: self.flag_no_ignore, + no_ignore: no_ignore, no_ignore_parent: // --no-ignore implies --no-ignore-parent - self.flag_no_ignore_parent || self.flag_no_ignore, + self.flag_no_ignore_parent || no_ignore, quiet: self.flag_quiet, replace: self.flag_replace.clone().map(|s| s.into_bytes()), - text: self.flag_text, + text: text, threads: threads, type_defs: btypes.definitions(), type_list: self.flag_type_list, diff --git a/tests/tests.rs b/tests/tests.rs index daff20da..d08dc6ae 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -525,6 +525,39 @@ baz/sherlock:be, to a very large extent, the result of luck. Sherlock Holmes } }); +sherlock!(unrestricted1, "Sherlock", ".", |wd: WorkDir, mut cmd: Command| { + wd.create(".gitignore", "sherlock\n"); + cmd.arg("-u"); + + let lines: String = wd.stdout(&mut cmd); + let expected = "\ +sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock +sherlock:be, to a very large extent, the result of luck. Sherlock Holmes +"; + assert_eq!(lines, expected); +}); + +sherlock!(unrestricted2, "Sherlock", ".", |wd: WorkDir, mut cmd: Command| { + wd.remove("sherlock"); + wd.create(".sherlock", hay::SHERLOCK); + cmd.arg("-uu"); + + let lines: String = wd.stdout(&mut cmd); + let expected = "\ +.sherlock:For the Doctor Watsons of this world, as opposed to the Sherlock +.sherlock:be, to a very large extent, the result of luck. Sherlock Holmes +"; + assert_eq!(lines, expected); +}); + +sherlock!(unrestricted3, "foo", ".", |wd: WorkDir, mut cmd: Command| { + wd.create("file", "foo\x00bar\nfoo\x00baz\n"); + cmd.arg("-uuu"); + + let lines: String = wd.stdout(&mut cmd); + assert_eq!(lines, "file:foo\nfile:foo\n"); +}); + #[test] fn binary_nosearch() { let wd = WorkDir::new("binary_nosearch"); -- cgit v1.2.3