1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-06-14 22:15:13 +02:00

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."
This commit is contained in:
Andrew Gallant
2016-09-20 20:24:03 -04:00
parent 7698b60256
commit 7402db7b43
3 changed files with 53 additions and 7 deletions

View File

@ -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) happen.](https://github.com/BurntSushi/ripgrep/issues/1)
To recursively search the current directory, while respecting all `.gitignore` To recursively search the current directory, while respecting all `.gitignore`
files: files, ignore hidden files and directories and skip binary files:
``` ```
$ rg foobar $ 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 insufficient. In all cases, `.rgignore` patterns take precedence over
`.gitignore`. `.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 (Tip: If your ignore files aren't being adhered to like you expect, run your

View File

@ -74,6 +74,12 @@ Common options:
to list all available types. to list all available types.
-T, --type-not TYPE ... Do not search files matching TYPE. Multiple -T, --type-not TYPE ... Do not search files matching TYPE. Multiple
not-type flags may be provided. 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. -v, --invert-match Invert matching.
-w, --word-regexp Only show matches surrounded by word boundaries. -w, --word-regexp Only show matches surrounded by word boundaries.
This is equivalent to putting \\b before and This is equivalent to putting \\b before and
@ -199,6 +205,7 @@ pub struct RawArgs {
flag_type_list: bool, flag_type_list: bool,
flag_type_add: Vec<String>, flag_type_add: Vec<String>,
flag_type_clear: Vec<String>, flag_type_clear: Vec<String>,
flag_unrestricted: u32,
flag_with_filename: bool, flag_with_filename: bool,
flag_word_regexp: bool, flag_word_regexp: bool,
} }
@ -312,6 +319,9 @@ impl RawArgs {
.line_terminator(eol) .line_terminator(eol)
.build() .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 { let mut args = Args {
pattern: pattern, pattern: pattern,
paths: paths, paths: paths,
@ -327,18 +337,18 @@ impl RawArgs {
glob_overrides: glob_overrides, glob_overrides: glob_overrides,
grep: grep, grep: grep,
heading: !self.flag_no_heading && self.flag_heading, heading: !self.flag_no_heading && self.flag_heading,
hidden: self.flag_hidden, hidden: hidden,
ignore_case: self.flag_ignore_case, ignore_case: self.flag_ignore_case,
invert_match: self.flag_invert_match, invert_match: self.flag_invert_match,
line_number: !self.flag_no_line_number && self.flag_line_number, line_number: !self.flag_no_line_number && self.flag_line_number,
mmap: mmap, mmap: mmap,
no_ignore: self.flag_no_ignore, no_ignore: no_ignore,
no_ignore_parent: no_ignore_parent:
// --no-ignore implies --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, quiet: self.flag_quiet,
replace: self.flag_replace.clone().map(|s| s.into_bytes()), replace: self.flag_replace.clone().map(|s| s.into_bytes()),
text: self.flag_text, text: text,
threads: threads, threads: threads,
type_defs: btypes.definitions(), type_defs: btypes.definitions(),
type_list: self.flag_type_list, type_list: self.flag_type_list,

View File

@ -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] #[test]
fn binary_nosearch() { fn binary_nosearch() {
let wd = WorkDir::new("binary_nosearch"); let wd = WorkDir::new("binary_nosearch");