diff options
author | Andrew Gallant <jamslam@gmail.com> | 2019-04-08 19:28:38 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2019-04-14 19:29:27 -0400 |
commit | a7d26c8f144a4957b75f71087a66692d0b25759a (patch) | |
tree | 4888ac5ea66643ac919d4e12c60cc51992bef11a /src/app.rs | |
parent | bd222ae93fa0cabe7d51ba8db40ece99579bdaed (diff) |
binary: rejigger ripgrep's handling of binary files
This commit attempts to surface binary filtering in a slightly more
user friendly way. Namely, before, ripgrep would silently stop
searching a file if it detected a NUL byte, even if it had previously
printed a match. This can lead to the user quite reasonably assuming
that there are no more matches, since a partial search is fairly
unintuitive. (ripgrep has this behavior by default because it really
wants to NOT search binary files at all, just like it doesn't search
gitignored or hidden files.)
With this commit, if a match has already been printed and ripgrep detects
a NUL byte, then it will print a warning message indicating that the search
stopped prematurely.
Moreover, this commit adds a new flag, --binary, which causes ripgrep to
stop filtering binary files, but in a way that still avoids dumping
binary data into terminals. That is, the --binary flag makes ripgrep
behave more like grep's default behavior.
For files explicitly specified in a search, e.g., `rg foo some-file`,
then no binary filtering is applied (just like no gitignore and no
hidden file filtering is applied). Instead, ripgrep behaves as if you
gave the --binary flag for all explicitly given files.
This was a fairly invasive change, and potentially increases the UX
complexity of ripgrep around binary files. (Before, there were two
binary modes, where as now there are three.) However, ripgrep is now a
bit louder with warning messages when binary file detection might
otherwise be hiding potential matches, so hopefully this is a net
improvement.
Finally, the `-uuu` convenience now maps to `--no-ignore --hidden
--binary`, since this is closer to the actualy intent of the
`--unrestricted` flag, i.e., to reduce ripgrep's smart filtering. As a
consequence, `rg -uuu foo` should now search roughly the same number of
bytes as `grep -r foo`, and `rg -uuua foo` should search roughly the
same number of bytes as `grep -ra foo`. (The "roughly" weasel word is
used because grep's and ripgrep's binary file detection might differ
somewhat---perhaps based on buffer sizes---which can impact exactly what
is and isn't searched.)
See the numerous tests in tests/binary.rs for intended behavior.
Fixes #306, Fixes #855
Diffstat (limited to 'src/app.rs')
-rw-r--r-- | src/app.rs | 73 |
1 files changed, 64 insertions, 9 deletions
@@ -27,6 +27,9 @@ configuration file. The file can specify one shell argument per line. Lines starting with '#' are ignored. For more details, see the man page or the README. +Tip: to disable all smart filtering and make ripgrep behave a bit more like +classical grep, use 'rg -uuu'. + Project home page: https://github.com/BurntSushi/ripgrep Use -h for short descriptions and --help for more details."; @@ -545,6 +548,7 @@ pub fn all_args_and_flags() -> Vec<RGArg> { // "positive" flag. flag_after_context(&mut args); flag_before_context(&mut args); + flag_binary(&mut args); flag_block_buffered(&mut args); flag_byte_offset(&mut args); flag_case_sensitive(&mut args); @@ -691,6 +695,55 @@ This overrides the --context flag. args.push(arg); } +fn flag_binary(args: &mut Vec<RGArg>) { + const SHORT: &str = "Search binary files."; + const LONG: &str = long!("\ +Enabling this flag will cause ripgrep to search binary files. By default, +ripgrep attempts to automatically skip binary files in order to improve the +relevance of results and make the search faster. + +Binary files are heuristically detected based on whether they contain a NUL +byte or not. By default (without this flag set), once a NUL byte is seen, +ripgrep will stop searching the file. Usually, NUL bytes occur in the beginning +of most binary files. If a NUL byte occurs after a match, then ripgrep will +still stop searching the rest of the file, but a warning will be printed. + +In contrast, when this flag is provided, ripgrep will continue searching a file +even if a NUL byte is found. In particular, if a NUL byte is found then ripgrep +will continue searching until either a match is found or the end of the file is +reached, whichever comes sooner. If a match is found, then ripgrep will stop +and print a warning saying that the search stopped prematurely. + +If you want ripgrep to search a file without any special NUL byte handling at +all (and potentially print binary data to stdout), then you should use the +'-a/--text' flag. + +The '--binary' flag is a flag for controlling ripgrep's automatic filtering +mechanism. As such, it does not need to be used when searching a file +explicitly or when searching stdin. That is, it is only applicable when +recursively searching a directory. + +Note that when the '-u/--unrestricted' flag is provided for a third time, then +this flag is automatically enabled. + +This flag can be disabled with '--no-binary'. It overrides the '-a/--text' +flag. +"); + let arg = RGArg::switch("binary") + .help(SHORT).long_help(LONG) + .overrides("no-binary") + .overrides("text") + .overrides("no-text"); + args.push(arg); + + let arg = RGArg::switch("no-binary") + .hidden() + .overrides("binary") + .overrides("text") + .overrides("no-text"); + args.push(arg); +} + fn flag_block_buffered(args: &mut Vec<RGArg>) { const SHORT: &str = "Force block buffering."; const LONG: &str = long!("\ @@ -1874,7 +1927,7 @@ fn flag_pre(args: &mut Vec<RGArg>) { For each input FILE, search the standard output of COMMAND FILE rather than the contents of FILE. This option expects the COMMAND program to either be an absolute path or to be available in your PATH. Either an empty string COMMAND -or the `--no-pre` flag will disable this behavior. +or the '--no-pre' flag will disable this behavior. WARNING: When this flag is set, ripgrep will unconditionally spawn a process for every file that is searched. Therefore, this can incur an @@ -2208,20 +2261,23 @@ escape codes to be printed that alter the behavior of your terminal. When binary file detection is enabled it is imperfect. In general, it uses a simple heuristic. If a NUL byte is seen during search, then the file is considered binary and search stops (unless this flag is present). +Alternatively, if the '--binary' flag is used, then ripgrep will only quit +when it sees a NUL byte after it sees a match (or searches the entire file). -Note that when the `-u/--unrestricted` flag is provided for a third time, then -this flag is automatically enabled. - -This flag can be disabled with --no-text. +This flag can be disabled with '--no-text'. It overrides the '--binary' flag. "); let arg = RGArg::switch("text").short("a") .help(SHORT).long_help(LONG) - .overrides("no-text"); + .overrides("no-text") + .overrides("binary") + .overrides("no-binary"); args.push(arg); let arg = RGArg::switch("no-text") .hidden() - .overrides("text"); + .overrides("text") + .overrides("binary") + .overrides("no-binary"); args.push(arg); } @@ -2350,8 +2406,7 @@ Reduce the level of \"smart\" searching. A single -u won't respect .gitignore (etc.) files. Two -u flags will additionally search hidden files and directories. Three -u flags will additionally search binary files. --uu is roughly equivalent to grep -r and -uuu is roughly equivalent to grep -a --r. +'rg -uuu' is roughly equivalent to 'grep -r'. "); let arg = RGArg::switch("unrestricted").short("u") .help(SHORT).long_help(LONG) |