2016-11-10 00:19:40 +02:00
|
|
|
use std::cmp;
|
2016-09-05 06:52:23 +02:00
|
|
|
use std::env;
|
2019-01-26 21:36:34 +02:00
|
|
|
use std::ffi::{OsStr, OsString};
|
2018-08-30 02:53:52 +02:00
|
|
|
use std::fs;
|
2023-09-20 20:42:03 +02:00
|
|
|
use std::io::{self, IsTerminal, Write};
|
2016-09-05 06:52:23 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-01-26 21:36:34 +02:00
|
|
|
use std::process;
|
2023-07-08 00:56:50 +02:00
|
|
|
use std::str::FromStr;
|
2016-12-24 19:53:09 +02:00
|
|
|
use std::sync::Arc;
|
2018-08-26 04:53:27 +02:00
|
|
|
use std::time::SystemTime;
|
2016-09-05 06:52:23 +02:00
|
|
|
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
use clap;
|
2018-08-30 02:53:52 +02:00
|
|
|
use grep::cli;
|
2018-08-03 23:26:22 +02:00
|
|
|
use grep::matcher::LineTerminator;
|
|
|
|
#[cfg(feature = "pcre2")]
|
|
|
|
use grep::pcre2::{
|
|
|
|
RegexMatcher as PCRE2RegexMatcher,
|
|
|
|
RegexMatcherBuilder as PCRE2RegexMatcherBuilder,
|
|
|
|
};
|
|
|
|
use grep::printer::{
|
2023-09-21 23:28:58 +02:00
|
|
|
default_color_specs, ColorSpecs, HyperlinkPattern, JSONBuilder,
|
|
|
|
PathPrinter, PathPrinterBuilder, Standard, StandardBuilder, Stats,
|
|
|
|
Summary, SummaryBuilder, SummaryKind, JSON,
|
2018-08-03 23:26:22 +02:00
|
|
|
};
|
|
|
|
use grep::regex::{
|
|
|
|
RegexMatcher as RustRegexMatcher,
|
|
|
|
RegexMatcherBuilder as RustRegexMatcherBuilder,
|
|
|
|
};
|
|
|
|
use grep::searcher::{
|
|
|
|
BinaryDetection, Encoding, MmapChoice, Searcher, SearcherBuilder,
|
|
|
|
};
|
|
|
|
use ignore::overrides::{Override, OverrideBuilder};
|
|
|
|
use ignore::types::{FileTypeDef, Types, TypesBuilder};
|
|
|
|
use ignore::{Walk, WalkBuilder, WalkParallel};
|
2016-09-05 06:52:23 +02:00
|
|
|
use log;
|
2020-02-18 01:08:47 +02:00
|
|
|
use termcolor::{BufferWriter, ColorChoice, WriteColor};
|
2016-09-05 06:52:23 +02:00
|
|
|
|
2019-01-19 17:15:56 +02:00
|
|
|
use crate::app;
|
|
|
|
use crate::config;
|
|
|
|
use crate::logger::Logger;
|
2020-02-18 01:08:47 +02:00
|
|
|
use crate::messages::{set_ignore_messages, set_messages};
|
2019-01-19 17:15:56 +02:00
|
|
|
use crate::search::{
|
|
|
|
PatternMatcher, Printer, SearchWorker, SearchWorkerBuilder,
|
|
|
|
};
|
2022-11-28 06:36:15 +02:00
|
|
|
use crate::subject::{Subject, SubjectBuilder};
|
2019-01-19 17:15:56 +02:00
|
|
|
use crate::Result;
|
2016-09-05 06:52:23 +02:00
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// The command that ripgrep should execute based on the command line
|
|
|
|
/// configuration.
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
pub enum Command {
|
|
|
|
/// Search using exactly one thread.
|
|
|
|
Search,
|
|
|
|
/// Search using possibly many threads.
|
|
|
|
SearchParallel,
|
|
|
|
/// The command line parameters suggest that a search should occur, but
|
|
|
|
/// ripgrep knows that a match can never be found (e.g., no given patterns
|
|
|
|
/// or --max-count=0).
|
|
|
|
SearchNever,
|
|
|
|
/// Show the files that would be searched, but don't actually search them,
|
|
|
|
/// and use exactly one thread.
|
|
|
|
Files,
|
|
|
|
/// Show the files that would be searched, but don't actually search them,
|
|
|
|
/// and perform directory traversal using possibly many threads.
|
|
|
|
FilesParallel,
|
|
|
|
/// List all file type definitions configured, including the default file
|
|
|
|
/// types and any additional file types added to the command line.
|
|
|
|
Types,
|
2019-04-14 22:46:02 +02:00
|
|
|
/// Print the version of PCRE2 in use.
|
|
|
|
PCRE2Version,
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
/// Returns true if and only if this command requires executing a search.
|
|
|
|
fn is_search(&self) -> bool {
|
|
|
|
use self::Command::*;
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
Search | SearchParallel => true,
|
2020-02-18 01:08:47 +02:00
|
|
|
SearchNever | Files | FilesParallel | Types | PCRE2Version => {
|
|
|
|
false
|
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The primary configuration object used throughout ripgrep. It provides a
|
|
|
|
/// high-level convenient interface to the provided command line arguments.
|
|
|
|
///
|
|
|
|
/// An `Args` object is cheap to clone and can be used from multiple threads
|
|
|
|
/// simultaneously.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Args(Arc<ArgsImp>);
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct ArgsImp {
|
|
|
|
/// Mid-to-low level routines for extracting CLI arguments.
|
|
|
|
matches: ArgMatches,
|
2023-01-05 15:15:09 +02:00
|
|
|
/// The command we want to execute.
|
|
|
|
command: Command,
|
|
|
|
/// The number of threads to use. This is based in part on available
|
|
|
|
/// threads, in part on the number of threads requested and in part on the
|
|
|
|
/// command we're running.
|
|
|
|
threads: usize,
|
2018-08-03 23:26:22 +02:00
|
|
|
/// A matcher built from the patterns.
|
|
|
|
///
|
|
|
|
/// It's important that this is only built once, since building this goes
|
|
|
|
/// through regex compilation and various types of analyses. That is, if
|
2022-06-24 15:58:20 +02:00
|
|
|
/// you need many of these (one per thread, for example), it is better to
|
2018-08-03 23:26:22 +02:00
|
|
|
/// build it once and then clone it.
|
|
|
|
matcher: PatternMatcher,
|
|
|
|
/// The paths provided at the command line. This is guaranteed to be
|
|
|
|
/// non-empty. (If no paths are provided, then a default path is created.)
|
2016-09-05 06:52:23 +02:00
|
|
|
paths: Vec<PathBuf>,
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns true if and only if `paths` had to be populated with a single
|
|
|
|
/// default path.
|
|
|
|
using_default_path: bool,
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Args {
|
|
|
|
/// Parse the command line arguments for this process.
|
|
|
|
///
|
|
|
|
/// If a CLI usage error occurred, then exit the process and print a usage
|
|
|
|
/// or error message. Similarly, if the user requested the version of
|
2016-09-08 22:15:44 +02:00
|
|
|
/// ripgrep, then print the version and exit.
|
2016-09-05 06:52:23 +02:00
|
|
|
///
|
|
|
|
/// Also, initialize a global logger.
|
|
|
|
pub fn parse() -> Result<Args> {
|
2018-02-04 03:33:52 +02:00
|
|
|
// We parse the args given on CLI. This does not include args from
|
|
|
|
// the config. We use the CLI args as an initial configuration while
|
|
|
|
// trying to parse config files. If a config file exists and has
|
|
|
|
// arguments, then we re-parse argv, otherwise we just use the matches
|
|
|
|
// we have here.
|
2019-01-26 21:36:34 +02:00
|
|
|
let early_matches = ArgMatches::new(clap_matches(env::args_os())?);
|
2018-08-03 23:26:22 +02:00
|
|
|
set_messages(!early_matches.is_present("no-messages"));
|
|
|
|
set_ignore_messages(!early_matches.is_present("no-ignore-messages"));
|
2016-09-05 06:52:23 +02:00
|
|
|
|
2018-02-04 04:31:28 +02:00
|
|
|
if let Err(err) = Logger::init() {
|
2018-08-03 23:26:22 +02:00
|
|
|
return Err(format!("failed to initialize logger: {}", err).into());
|
2018-02-04 04:31:28 +02:00
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
if early_matches.is_present("trace") {
|
|
|
|
log::set_max_level(log::LevelFilter::Trace);
|
|
|
|
} else if early_matches.is_present("debug") {
|
2018-02-04 03:33:52 +02:00
|
|
|
log::set_max_level(log::LevelFilter::Debug);
|
|
|
|
} else {
|
|
|
|
log::set_max_level(log::LevelFilter::Warn);
|
|
|
|
}
|
|
|
|
|
2019-01-26 21:36:34 +02:00
|
|
|
let matches = early_matches.reconfigure()?;
|
2018-02-04 03:33:52 +02:00
|
|
|
// The logging level may have changed if we brought in additional
|
|
|
|
// arguments from a configuration file, so recheck it and set the log
|
|
|
|
// level as appropriate.
|
2018-08-03 23:26:22 +02:00
|
|
|
if matches.is_present("trace") {
|
|
|
|
log::set_max_level(log::LevelFilter::Trace);
|
|
|
|
} else if matches.is_present("debug") {
|
2018-02-04 04:31:28 +02:00
|
|
|
log::set_max_level(log::LevelFilter::Debug);
|
2016-09-05 06:52:23 +02:00
|
|
|
} else {
|
2018-02-04 04:31:28 +02:00
|
|
|
log::set_max_level(log::LevelFilter::Warn);
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
set_messages(!matches.is_present("no-messages"));
|
|
|
|
set_ignore_messages(!matches.is_present("no-ignore-messages"));
|
2018-02-04 03:33:52 +02:00
|
|
|
matches.to_args()
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Return direct access to command line arguments.
|
|
|
|
fn matches(&self) -> &ArgMatches {
|
|
|
|
&self.0.matches
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the matcher builder from the patterns.
|
|
|
|
fn matcher(&self) -> &PatternMatcher {
|
|
|
|
&self.0.matcher
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the paths found in the command line arguments. This is
|
|
|
|
/// guaranteed to be non-empty. In the case where no explicit arguments are
|
|
|
|
/// provided, a single default path is provided automatically.
|
|
|
|
fn paths(&self) -> &[PathBuf] {
|
|
|
|
&self.0.paths
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if and only if `paths` had to be populated with a default
|
|
|
|
/// path, which occurs only when no paths were given as command line
|
|
|
|
/// arguments.
|
cli: print warning if nothing was searched
This was once part of ripgrep, but at some point, was unintentionally
removed. The value of this warning is that since ripgrep tries to be
"smart" by default, it can be surprising if it doesn't search certain
things. This warning covers the case when ripgrep searches *nothing*,
which happens somewhat more frequently than you might expect. e.g., If
you're searching within an ignore directory.
Note that for now, we only print this message when the user has not
supplied any explicit paths. It's not clear that we want to print this
otherwise, and in particular, it seems that the message shows up too
eagerly. e.g., 'rg foo does-not-exist' will both print an error about
'does-not-exist' not existing, *and* the message about no files being
searched, which seems annoying in this case. We can always refine this
logic later.
Fixes #1404, Closes #1762
2020-12-15 09:59:55 +02:00
|
|
|
pub fn using_default_path(&self) -> bool {
|
2018-08-03 23:26:22 +02:00
|
|
|
self.0.using_default_path
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the printer that should be used for formatting the output of
|
|
|
|
/// search results.
|
|
|
|
///
|
|
|
|
/// The returned printer will write results to the given writer.
|
|
|
|
fn printer<W: WriteColor>(&self, wtr: W) -> Result<Printer<W>> {
|
|
|
|
match self.matches().output_kind() {
|
|
|
|
OutputKind::Standard => {
|
2023-01-05 15:15:09 +02:00
|
|
|
let separator_search = self.command() == Command::Search;
|
2018-08-03 23:26:22 +02:00
|
|
|
self.matches()
|
|
|
|
.printer_standard(self.paths(), wtr, separator_search)
|
|
|
|
.map(Printer::Standard)
|
|
|
|
}
|
2020-02-18 01:08:47 +02:00
|
|
|
OutputKind::Summary => self
|
|
|
|
.matches()
|
|
|
|
.printer_summary(self.paths(), wtr)
|
|
|
|
.map(Printer::Summary),
|
2018-08-03 23:26:22 +02:00
|
|
|
OutputKind::JSON => {
|
2020-02-18 01:08:47 +02:00
|
|
|
self.matches().printer_json(wtr).map(Printer::JSON)
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// High level public routines for building data structures used by ripgrep
|
|
|
|
/// from command line arguments.
|
|
|
|
impl Args {
|
|
|
|
/// Create a new buffer writer for multi-threaded printing with color
|
|
|
|
/// support.
|
|
|
|
pub fn buffer_writer(&self) -> Result<BufferWriter> {
|
|
|
|
let mut wtr = BufferWriter::stdout(self.matches().color_choice());
|
|
|
|
wtr.separator(self.matches().file_separator()?);
|
|
|
|
Ok(wtr)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the high-level command that ripgrep should run.
|
2023-01-05 15:15:09 +02:00
|
|
|
pub fn command(&self) -> Command {
|
|
|
|
self.0.command
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builder a path printer that can be used for printing just file paths,
|
|
|
|
/// with optional color support.
|
|
|
|
///
|
|
|
|
/// The printer will print paths to the given writer.
|
|
|
|
pub fn path_printer<W: WriteColor>(
|
|
|
|
&self,
|
|
|
|
wtr: W,
|
|
|
|
) -> Result<PathPrinter<W>> {
|
|
|
|
let mut builder = PathPrinterBuilder::new();
|
|
|
|
builder
|
|
|
|
.color_specs(self.matches().color_specs()?)
|
2023-07-08 00:56:50 +02:00
|
|
|
.hyperlink_pattern(self.matches().hyperlink_pattern()?)
|
2018-08-03 23:26:22 +02:00
|
|
|
.separator(self.matches().path_separator()?)
|
|
|
|
.terminator(self.matches().path_terminator().unwrap_or(b'\n'));
|
|
|
|
Ok(builder.build(wtr))
|
|
|
|
}
|
|
|
|
|
2019-01-26 22:42:55 +02:00
|
|
|
/// Returns true if and only if ripgrep should be "quiet."
|
|
|
|
pub fn quiet(&self) -> bool {
|
|
|
|
self.matches().is_present("quiet")
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns true if and only if the search should quit after finding the
|
|
|
|
/// first match.
|
|
|
|
pub fn quit_after_match(&self) -> Result<bool> {
|
|
|
|
Ok(self.matches().is_present("quiet") && self.stats()?.is_none())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a worker for executing searches.
|
|
|
|
///
|
|
|
|
/// Search results are written to the given writer.
|
|
|
|
pub fn search_worker<W: WriteColor>(
|
|
|
|
&self,
|
|
|
|
wtr: W,
|
|
|
|
) -> Result<SearchWorker<W>> {
|
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
2019-04-09 01:28:38 +02:00
|
|
|
let matches = self.matches();
|
2018-08-03 23:26:22 +02:00
|
|
|
let matcher = self.matcher().clone();
|
|
|
|
let printer = self.printer(wtr)?;
|
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
2019-04-09 01:28:38 +02:00
|
|
|
let searcher = matches.searcher(self.paths())?;
|
2018-08-03 23:26:22 +02:00
|
|
|
let mut builder = SearchWorkerBuilder::new();
|
|
|
|
builder
|
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
2019-04-09 01:28:38 +02:00
|
|
|
.json_stats(matches.is_present("json"))
|
cli: fix arbitrary execution of program bug
This fixes a bug only present on Windows that would permit someone to
execute an arbitrary program if they crafted an appropriate directory
tree. Namely, if someone put an executable named 'xz.exe' in the root of
a directory tree and one ran 'rg -z foo' from the root of that tree,
then the 'xz.exe' executable in that tree would execute if there are any
'xz' files anywhere in the tree.
The root cause of this problem is that 'CreateProcess' on Windows will
implicitly look in the current working directory for an executable when
it is given a relative path to a program. Rust's standard library allows
this behavior to occur, so we work around it here. We work around it by
explicitly resolving programs like 'xz' via 'PATH'. That way, we only
ever pass an absolute path to 'CreateProcess', which avoids the implicit
behavior of checking the current working directory.
This fix doesn't apply to non-Windows systems as it is believed to only
impact Windows. In theory, the bug could apply on Unix if '.' is in
one's PATH, but at that point, you reap what you sow.
While the extent to which this is a security problem isn't clear, I
think users generally expect to be able to download or clone
repositories from the Internet and run ripgrep on them without fear of
anything too awful happening. Being able to execute an arbitrary program
probably violates that expectation. Therefore, CVE-2021-3013[1] was
created for this issue.
We apply the same logic to the --pre command, since the --pre command is
likely in a user's config file and it would be surprising for something
that the user is searching to modify which preprocessor command is used.
The --pre and -z/--search-zip flags are the only two ways that ripgrep
will invoke external programs, so this should cover any possible
exploitable cases of this bug.
[1] - https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-3013
2021-01-11 20:44:07 +02:00
|
|
|
.preprocessor(matches.preprocessor())?
|
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
2019-04-09 01:28:38 +02:00
|
|
|
.preprocessor_globs(matches.preprocessor_globs()?)
|
|
|
|
.search_zip(matches.is_present("search-zip"))
|
|
|
|
.binary_detection_implicit(matches.binary_detection_implicit())
|
|
|
|
.binary_detection_explicit(matches.binary_detection_explicit());
|
2018-08-03 23:26:22 +02:00
|
|
|
Ok(builder.build(matcher, searcher, printer))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a zero value for tracking statistics if and only if it has been
|
|
|
|
/// requested.
|
|
|
|
///
|
|
|
|
/// When this returns a `Stats` value, then it is guaranteed that the
|
|
|
|
/// search worker will be configured to track statistics as well.
|
|
|
|
pub fn stats(&self) -> Result<Option<Stats>> {
|
2023-01-05 15:15:09 +02:00
|
|
|
Ok(if self.command().is_search() && self.matches().stats() {
|
2018-08-03 23:26:22 +02:00
|
|
|
Some(Stats::new())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a builder for constructing subjects. A subject represents a
|
|
|
|
/// single unit of something to search. Typically, this corresponds to a
|
|
|
|
/// file or a stream such as stdin.
|
|
|
|
pub fn subject_builder(&self) -> SubjectBuilder {
|
|
|
|
let mut builder = SubjectBuilder::new();
|
2018-08-28 03:17:58 +02:00
|
|
|
builder.strip_dot_prefix(self.using_default_path());
|
2018-08-03 23:26:22 +02:00
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute the given function with a writer to stdout that enables color
|
|
|
|
/// support based on the command line configuration.
|
2018-08-30 02:53:52 +02:00
|
|
|
pub fn stdout(&self) -> cli::StandardStream {
|
2018-09-05 03:39:05 +02:00
|
|
|
let color = self.matches().color_choice();
|
|
|
|
if self.matches().is_present("line-buffered") {
|
|
|
|
cli::stdout_buffered_line(color)
|
|
|
|
} else if self.matches().is_present("block-buffered") {
|
|
|
|
cli::stdout_buffered_block(color)
|
|
|
|
} else {
|
|
|
|
cli::stdout(color)
|
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the type definitions compiled into ripgrep.
|
|
|
|
///
|
|
|
|
/// If there was a problem reading and parsing the type definitions, then
|
|
|
|
/// this returns an error.
|
|
|
|
pub fn type_defs(&self) -> Result<Vec<FileTypeDef>> {
|
|
|
|
Ok(self.matches().types()?.definitions().to_vec())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a walker that never uses additional threads.
|
|
|
|
pub fn walker(&self) -> Result<Walk> {
|
2023-01-05 15:15:09 +02:00
|
|
|
Ok(self
|
|
|
|
.matches()
|
|
|
|
.walker_builder(self.paths(), self.0.threads)?
|
|
|
|
.build())
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
|
2022-11-28 06:36:15 +02:00
|
|
|
/// Returns true if and only if `stat`-related sorting is required
|
|
|
|
pub fn needs_stat_sort(&self) -> bool {
|
|
|
|
return self.matches().sort_by().map_or(
|
|
|
|
false,
|
|
|
|
|sort_by| match sort_by.kind {
|
|
|
|
SortByKind::LastModified
|
|
|
|
| SortByKind::Created
|
|
|
|
| SortByKind::LastAccessed => sort_by.check().is_ok(),
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sort subjects if a sorter is specified, but only if the sort requires
|
|
|
|
/// stat calls. Non-stat related sorts are handled during file traversal
|
|
|
|
///
|
|
|
|
/// This function assumes that it is known that a stat-related sort is
|
|
|
|
/// required, and does not check for it again.
|
|
|
|
///
|
|
|
|
/// It is important that that precondition is fulfilled, since this function
|
|
|
|
/// consumes the subjects iterator, and is therefore a blocking function.
|
|
|
|
pub fn sort_by_stat<I>(&self, subjects: I) -> Vec<Subject>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = Subject>,
|
|
|
|
{
|
|
|
|
let sorter = match self.matches().sort_by() {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => return subjects.collect(),
|
|
|
|
};
|
|
|
|
use SortByKind::*;
|
|
|
|
let mut keyed = match sorter.kind {
|
|
|
|
LastModified => load_timestamps(subjects, |m| m.modified()),
|
|
|
|
LastAccessed => load_timestamps(subjects, |m| m.accessed()),
|
|
|
|
Created => load_timestamps(subjects, |m| m.created()),
|
|
|
|
_ => return subjects.collect(),
|
|
|
|
};
|
|
|
|
keyed.sort_by(|a, b| sort_by_option(&a.0, &b.0, sorter.reverse));
|
|
|
|
keyed.into_iter().map(|v| v.1).collect()
|
|
|
|
}
|
|
|
|
|
2020-05-25 05:12:53 +02:00
|
|
|
/// Return a parallel walker that may use additional threads.
|
2018-08-03 23:26:22 +02:00
|
|
|
pub fn walker_parallel(&self) -> Result<WalkParallel> {
|
2023-01-05 15:15:09 +02:00
|
|
|
Ok(self
|
|
|
|
.matches()
|
|
|
|
.walker_builder(self.paths(), self.0.threads)?
|
|
|
|
.build_parallel())
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `ArgMatches` wraps `clap::ArgMatches` and provides semantic meaning to
|
|
|
|
/// the parsed arguments.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct ArgMatches(clap::ArgMatches<'static>);
|
|
|
|
|
|
|
|
/// The output format. Generally, this corresponds to the printer that ripgrep
|
|
|
|
/// uses to show search results.
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
enum OutputKind {
|
|
|
|
/// Classic grep-like or ack-like format.
|
|
|
|
Standard,
|
|
|
|
/// Show matching files and possibly the number of matches in each file.
|
|
|
|
Summary,
|
|
|
|
/// Emit match information in the JSON Lines format.
|
|
|
|
JSON,
|
|
|
|
}
|
|
|
|
|
2018-08-26 04:53:27 +02:00
|
|
|
/// The sort criteria, if present.
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
struct SortBy {
|
|
|
|
/// Whether to reverse the sort criteria (i.e., descending order).
|
|
|
|
reverse: bool,
|
|
|
|
/// The actual sorting criteria.
|
|
|
|
kind: SortByKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
enum SortByKind {
|
|
|
|
/// No sorting at all.
|
|
|
|
None,
|
|
|
|
/// Sort by path.
|
|
|
|
Path,
|
|
|
|
/// Sort by last modified time.
|
|
|
|
LastModified,
|
|
|
|
/// Sort by last accessed time.
|
|
|
|
LastAccessed,
|
|
|
|
/// Sort by creation time.
|
|
|
|
Created,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SortBy {
|
|
|
|
fn asc(kind: SortByKind) -> SortBy {
|
2020-03-15 15:04:39 +02:00
|
|
|
SortBy { reverse: false, kind }
|
2018-08-26 04:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn desc(kind: SortByKind) -> SortBy {
|
2020-03-15 15:04:39 +02:00
|
|
|
SortBy { reverse: true, kind }
|
2018-08-26 04:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn none() -> SortBy {
|
|
|
|
SortBy::asc(SortByKind::None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to check that the sorting criteria selected is actually supported.
|
|
|
|
/// If it isn't, then an error is returned.
|
|
|
|
fn check(&self) -> Result<()> {
|
|
|
|
match self.kind {
|
|
|
|
SortByKind::None | SortByKind::Path => {}
|
|
|
|
SortByKind::LastModified => {
|
|
|
|
env::current_exe()?.metadata()?.modified()?;
|
|
|
|
}
|
|
|
|
SortByKind::LastAccessed => {
|
|
|
|
env::current_exe()?.metadata()?.accessed()?;
|
|
|
|
}
|
|
|
|
SortByKind::Created => {
|
|
|
|
env::current_exe()?.metadata()?.created()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-11-28 06:36:15 +02:00
|
|
|
/// Load sorters only if they are applicable at the walk stage.
|
|
|
|
///
|
|
|
|
/// In particular, sorts that involve `stat` calls are not loaded because
|
|
|
|
/// the walk inherently assumes that parent directories are aware of all its
|
|
|
|
/// decendent properties, but `stat` does not work that way.
|
|
|
|
fn configure_builder_sort(self, builder: &mut WalkBuilder) {
|
|
|
|
use SortByKind::*;
|
2018-08-26 04:53:27 +02:00
|
|
|
match self.kind {
|
2022-11-28 06:36:15 +02:00
|
|
|
Path if self.reverse => {
|
|
|
|
builder.sort_by_file_name(|a, b| a.cmp(b).reverse());
|
2018-08-26 04:53:27 +02:00
|
|
|
}
|
2022-11-28 06:36:15 +02:00
|
|
|
Path => {
|
|
|
|
builder.sort_by_file_name(|a, b| a.cmp(b));
|
2018-08-26 04:53:27 +02:00
|
|
|
}
|
2022-11-28 06:36:15 +02:00
|
|
|
// these use `stat` calls and will be sorted in Args::sort_by_stat()
|
|
|
|
LastModified | LastAccessed | Created | None => {}
|
|
|
|
};
|
2018-08-26 04:53:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SortByKind {
|
|
|
|
fn new(kind: &str) -> SortByKind {
|
|
|
|
match kind {
|
|
|
|
"none" => SortByKind::None,
|
|
|
|
"path" => SortByKind::Path,
|
|
|
|
"modified" => SortByKind::LastModified,
|
|
|
|
"accessed" => SortByKind::LastAccessed,
|
|
|
|
"created" => SortByKind::Created,
|
|
|
|
_ => SortByKind::None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:18:45 +02:00
|
|
|
/// Encoding mode the searcher will use.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
enum EncodingMode {
|
|
|
|
/// Use an explicit encoding forcefully, but let BOM sniffing override it.
|
|
|
|
Some(Encoding),
|
|
|
|
/// Use only BOM sniffing to auto-detect an encoding.
|
|
|
|
Auto,
|
|
|
|
/// Use no explicit encoding and disable all BOM sniffing. This will
|
|
|
|
/// always result in searching the raw bytes, regardless of their
|
|
|
|
/// true encoding.
|
|
|
|
Disabled,
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
impl ArgMatches {
|
|
|
|
/// Create an ArgMatches from clap's parse result.
|
|
|
|
fn new(clap_matches: clap::ArgMatches<'static>) -> ArgMatches {
|
|
|
|
ArgMatches(clap_matches)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run clap and return the matches using a config file if present. If clap
|
|
|
|
/// determines a problem with the user provided arguments (or if --help or
|
|
|
|
/// --version are given), then an error/usage/version will be printed and
|
|
|
|
/// the process will exit.
|
2018-02-04 03:33:52 +02:00
|
|
|
///
|
|
|
|
/// If there are no additional arguments from the environment (e.g., a
|
|
|
|
/// config file), then the given matches are returned as is.
|
2019-01-26 21:36:34 +02:00
|
|
|
fn reconfigure(self) -> Result<ArgMatches> {
|
2018-02-04 03:33:52 +02:00
|
|
|
// If the end user says no config, then respect it.
|
2018-08-03 23:26:22 +02:00
|
|
|
if self.is_present("no-config") {
|
2019-01-19 17:15:56 +02:00
|
|
|
log::debug!(
|
|
|
|
"not reading config files because --no-config is present"
|
|
|
|
);
|
2019-01-26 21:36:34 +02:00
|
|
|
return Ok(self);
|
2018-02-04 03:33:52 +02:00
|
|
|
}
|
|
|
|
// If the user wants ripgrep to use a config file, then parse args
|
|
|
|
// from that first.
|
2018-08-03 23:26:22 +02:00
|
|
|
let mut args = config::args();
|
2018-02-04 03:33:52 +02:00
|
|
|
if args.is_empty() {
|
2019-01-26 21:36:34 +02:00
|
|
|
return Ok(self);
|
2018-02-04 03:33:52 +02:00
|
|
|
}
|
|
|
|
let mut cliargs = env::args_os();
|
|
|
|
if let Some(bin) = cliargs.next() {
|
|
|
|
args.insert(0, bin);
|
|
|
|
}
|
|
|
|
args.extend(cliargs);
|
2019-01-19 17:15:56 +02:00
|
|
|
log::debug!("final argv: {:?}", args);
|
2019-01-26 21:36:34 +02:00
|
|
|
Ok(ArgMatches(clap_matches(args)?))
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert the result of parsing CLI arguments into ripgrep's higher level
|
|
|
|
/// configuration structure.
|
|
|
|
fn to_args(self) -> Result<Args> {
|
|
|
|
// We compute these once since they could be large.
|
|
|
|
let patterns = self.patterns()?;
|
|
|
|
let matcher = self.matcher(&patterns)?;
|
|
|
|
let mut paths = self.paths();
|
2020-02-18 01:08:47 +02:00
|
|
|
let using_default_path = if paths.is_empty() {
|
|
|
|
paths.push(self.path_default());
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2023-01-05 15:15:09 +02:00
|
|
|
// Now figure out the number of threads we'll use and which
|
|
|
|
// command will run.
|
|
|
|
let is_one_search = self.is_one_search(&paths);
|
|
|
|
let threads = if is_one_search { 1 } else { self.threads()? };
|
|
|
|
if threads == 1 {
|
|
|
|
log::debug!("running in single threaded mode");
|
|
|
|
} else {
|
|
|
|
log::debug!("running with {threads} threads for parallelism");
|
|
|
|
}
|
|
|
|
let command = if self.is_present("pcre2-version") {
|
|
|
|
Command::PCRE2Version
|
|
|
|
} else if self.is_present("type-list") {
|
|
|
|
Command::Types
|
|
|
|
} else if self.is_present("files") {
|
|
|
|
if threads == 1 {
|
|
|
|
Command::Files
|
|
|
|
} else {
|
|
|
|
Command::FilesParallel
|
|
|
|
}
|
|
|
|
} else if self.can_never_match(&patterns) {
|
|
|
|
Command::SearchNever
|
|
|
|
} else if threads == 1 {
|
|
|
|
Command::Search
|
|
|
|
} else {
|
|
|
|
Command::SearchParallel
|
|
|
|
};
|
2018-08-03 23:26:22 +02:00
|
|
|
Ok(Args(Arc::new(ArgsImp {
|
|
|
|
matches: self,
|
2023-01-05 15:15:09 +02:00
|
|
|
command,
|
|
|
|
threads,
|
2020-03-15 15:04:39 +02:00
|
|
|
matcher,
|
|
|
|
paths,
|
|
|
|
using_default_path,
|
2018-08-03 23:26:22 +02:00
|
|
|
})))
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
2016-09-05 06:52:23 +02:00
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// High level routines for converting command line arguments into various
|
|
|
|
/// data structures used by ripgrep.
|
|
|
|
///
|
|
|
|
/// Methods are sorted alphabetically.
|
|
|
|
impl ArgMatches {
|
|
|
|
/// Return the matcher that should be used for searching.
|
|
|
|
///
|
|
|
|
/// If there was a problem building the matcher (e.g., a syntax error),
|
|
|
|
/// then this returns an error.
|
|
|
|
fn matcher(&self, patterns: &[String]) -> Result<PatternMatcher> {
|
|
|
|
if self.is_present("pcre2") {
|
2020-02-27 17:47:34 +02:00
|
|
|
self.matcher_engine("pcre2", patterns)
|
2019-04-14 23:39:37 +02:00
|
|
|
} else if self.is_present("auto-hybrid-regex") {
|
2020-02-27 17:47:34 +02:00
|
|
|
self.matcher_engine("auto", patterns)
|
2018-08-03 23:26:22 +02:00
|
|
|
} else {
|
2020-02-27 17:58:56 +02:00
|
|
|
let engine = self.value_of_lossy("engine").unwrap();
|
|
|
|
self.matcher_engine(&engine, patterns)
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 17:47:34 +02:00
|
|
|
/// Return the matcher that should be used for searching using engine
|
|
|
|
/// as the engine for the patterns.
|
2016-09-05 06:52:23 +02:00
|
|
|
///
|
2018-08-03 23:26:22 +02:00
|
|
|
/// If there was a problem building the matcher (e.g., a syntax error),
|
|
|
|
/// then this returns an error.
|
2020-02-27 17:47:34 +02:00
|
|
|
fn matcher_engine(
|
|
|
|
&self,
|
|
|
|
engine: &str,
|
|
|
|
patterns: &[String],
|
|
|
|
) -> Result<PatternMatcher> {
|
|
|
|
match engine {
|
|
|
|
"default" => {
|
|
|
|
let matcher = match self.matcher_rust(patterns) {
|
|
|
|
Ok(matcher) => matcher,
|
|
|
|
Err(err) => {
|
|
|
|
return Err(From::from(suggest(err.to_string())));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(PatternMatcher::RustRegex(matcher))
|
|
|
|
}
|
|
|
|
#[cfg(feature = "pcre2")]
|
|
|
|
"pcre2" => {
|
|
|
|
let matcher = self.matcher_pcre2(patterns)?;
|
|
|
|
Ok(PatternMatcher::PCRE2(matcher))
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "pcre2"))]
|
|
|
|
"pcre2" => Err(From::from(
|
2018-08-03 23:26:22 +02:00
|
|
|
"PCRE2 is not available in this build of ripgrep",
|
2020-02-27 17:47:34 +02:00
|
|
|
)),
|
|
|
|
"auto" => {
|
|
|
|
let rust_err = match self.matcher_rust(patterns) {
|
|
|
|
Ok(matcher) => {
|
|
|
|
return Ok(PatternMatcher::RustRegex(matcher));
|
|
|
|
}
|
|
|
|
Err(err) => err,
|
|
|
|
};
|
|
|
|
log::debug!(
|
|
|
|
"error building Rust regex in hybrid mode:\n{}",
|
|
|
|
rust_err,
|
|
|
|
);
|
|
|
|
|
|
|
|
let pcre_err = match self.matcher_engine("pcre2", patterns) {
|
|
|
|
Ok(matcher) => return Ok(matcher),
|
|
|
|
Err(err) => err,
|
|
|
|
};
|
|
|
|
Err(From::from(format!(
|
|
|
|
"regex could not be compiled with either the default \
|
|
|
|
regex engine or with PCRE2.\n\n\
|
|
|
|
default regex engine error:\n{}\n{}\n{}\n\n\
|
|
|
|
PCRE2 regex engine error:\n{}",
|
|
|
|
"~".repeat(79),
|
|
|
|
rust_err,
|
|
|
|
"~".repeat(79),
|
|
|
|
pcre_err,
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
_ => Err(From::from(format!(
|
|
|
|
"unrecognized regex engine '{}'",
|
|
|
|
engine
|
|
|
|
))),
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Build a matcher using Rust's regex engine.
|
|
|
|
///
|
|
|
|
/// If there was a problem building the matcher (such as a regex syntax
|
|
|
|
/// error), then an error is returned.
|
|
|
|
fn matcher_rust(&self, patterns: &[String]) -> Result<RustRegexMatcher> {
|
|
|
|
let mut builder = RustRegexMatcherBuilder::new();
|
|
|
|
builder
|
|
|
|
.case_smart(self.case_smart())
|
|
|
|
.case_insensitive(self.case_insensitive())
|
|
|
|
.multi_line(true)
|
2020-02-17 22:34:59 +02:00
|
|
|
.unicode(self.unicode())
|
2018-08-03 23:26:22 +02:00
|
|
|
.octal(false)
|
2023-06-20 02:47:07 +02:00
|
|
|
.fixed_strings(self.is_present("fixed-strings"))
|
|
|
|
.whole_line(self.is_present("line-regexp"))
|
2018-08-03 23:26:22 +02:00
|
|
|
.word(self.is_present("word-regexp"));
|
|
|
|
if self.is_present("multiline") {
|
|
|
|
builder.dot_matches_new_line(self.is_present("multiline-dotall"));
|
|
|
|
if self.is_present("crlf") {
|
2020-02-18 01:08:47 +02:00
|
|
|
builder.crlf(true).line_terminator(None);
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-02-18 01:08:47 +02:00
|
|
|
builder.line_terminator(Some(b'\n')).dot_matches_new_line(false);
|
2018-08-03 23:26:22 +02:00
|
|
|
if self.is_present("crlf") {
|
|
|
|
builder.crlf(true);
|
|
|
|
}
|
|
|
|
// We don't need to set this in multiline mode since mulitline
|
|
|
|
// matchers don't use optimizations related to line terminators.
|
|
|
|
// Moreover, a mulitline regex used with --null-data should
|
|
|
|
// be allowed to match NUL bytes explicitly, which this would
|
|
|
|
// otherwise forbid.
|
|
|
|
if self.is_present("null-data") {
|
|
|
|
builder.line_terminator(Some(b'\x00'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(limit) = self.regex_size_limit()? {
|
|
|
|
builder.size_limit(limit);
|
|
|
|
}
|
|
|
|
if let Some(limit) = self.dfa_size_limit()? {
|
|
|
|
builder.dfa_size_limit(limit);
|
|
|
|
}
|
2023-06-20 02:47:07 +02:00
|
|
|
match builder.build_many(patterns) {
|
2018-09-25 02:58:17 +02:00
|
|
|
Ok(m) => Ok(m),
|
|
|
|
Err(err) => Err(From::from(suggest_multiline(err.to_string()))),
|
|
|
|
}
|
2016-09-29 02:50:50 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Build a matcher using PCRE2.
|
2016-12-24 19:53:09 +02:00
|
|
|
///
|
2018-08-03 23:26:22 +02:00
|
|
|
/// If there was a problem building the matcher (such as a regex syntax
|
|
|
|
/// error), then an error is returned.
|
|
|
|
#[cfg(feature = "pcre2")]
|
|
|
|
fn matcher_pcre2(&self, patterns: &[String]) -> Result<PCRE2RegexMatcher> {
|
|
|
|
let mut builder = PCRE2RegexMatcherBuilder::new();
|
|
|
|
builder
|
|
|
|
.case_smart(self.case_smart())
|
|
|
|
.caseless(self.case_insensitive())
|
|
|
|
.multi_line(true)
|
2023-06-20 02:47:07 +02:00
|
|
|
.fixed_strings(self.is_present("fixed-strings"))
|
|
|
|
.whole_line(self.is_present("line-regexp"))
|
2018-08-03 23:26:22 +02:00
|
|
|
.word(self.is_present("word-regexp"));
|
2018-08-30 02:53:52 +02:00
|
|
|
// For whatever reason, the JIT craps out during regex compilation with
|
|
|
|
// a "no more memory" error on 32 bit systems. So don't use it there.
|
2019-04-14 22:46:02 +02:00
|
|
|
if cfg!(target_pointer_width = "64") {
|
2019-04-14 22:46:59 +02:00
|
|
|
builder
|
|
|
|
.jit_if_available(true)
|
|
|
|
// The PCRE2 docs say that 32KB is the default, and that 1MB
|
|
|
|
// should be big enough for anything. But let's crank it to
|
|
|
|
// 10MB.
|
2020-02-18 01:08:47 +02:00
|
|
|
.max_jit_stack_size(Some(10 * (1 << 20)));
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
2020-02-17 22:34:59 +02:00
|
|
|
if self.unicode() {
|
2018-08-03 23:26:22 +02:00
|
|
|
builder.utf(true).ucp(true);
|
|
|
|
}
|
|
|
|
if self.is_present("multiline") {
|
|
|
|
builder.dotall(self.is_present("multiline-dotall"));
|
|
|
|
}
|
|
|
|
if self.is_present("crlf") {
|
|
|
|
builder.crlf(true);
|
|
|
|
}
|
2023-06-20 02:47:07 +02:00
|
|
|
Ok(builder.build_many(patterns)?)
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Build a JSON printer that writes results to the given writer.
|
|
|
|
fn printer_json<W: io::Write>(&self, wtr: W) -> Result<JSON<W>> {
|
|
|
|
let mut builder = JSONBuilder::new();
|
|
|
|
builder
|
|
|
|
.pretty(false)
|
|
|
|
.max_matches(self.max_count()?)
|
|
|
|
.always_begin_end(false);
|
|
|
|
Ok(builder.build(wtr))
|
2016-11-06 20:09:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Build a Standard printer that writes results to the given writer.
|
|
|
|
///
|
|
|
|
/// The given paths are used to configure aspects of the printer.
|
|
|
|
///
|
|
|
|
/// If `separator_search` is true, then the returned printer will assume
|
|
|
|
/// the responsibility of printing a separator between each set of
|
|
|
|
/// search results, when appropriate (e.g., when contexts are enabled).
|
|
|
|
/// When it's set to false, the caller is responsible for handling
|
|
|
|
/// separators.
|
|
|
|
///
|
|
|
|
/// In practice, we want the printer to handle it in the single threaded
|
|
|
|
/// case but not in the multi-threaded case.
|
|
|
|
fn printer_standard<W: WriteColor>(
|
|
|
|
&self,
|
|
|
|
paths: &[PathBuf],
|
|
|
|
wtr: W,
|
|
|
|
separator_search: bool,
|
|
|
|
) -> Result<Standard<W>> {
|
|
|
|
let mut builder = StandardBuilder::new();
|
|
|
|
builder
|
|
|
|
.color_specs(self.color_specs()?)
|
2023-07-08 00:56:50 +02:00
|
|
|
.hyperlink_pattern(self.hyperlink_pattern()?)
|
2018-08-03 23:26:22 +02:00
|
|
|
.stats(self.stats())
|
|
|
|
.heading(self.heading())
|
|
|
|
.path(self.with_filename(paths))
|
|
|
|
.only_matching(self.is_present("only-matching"))
|
|
|
|
.per_match(self.is_present("vimgrep"))
|
2021-05-31 03:36:35 +02:00
|
|
|
.per_match_one_line(true)
|
2018-08-03 23:26:22 +02:00
|
|
|
.replacement(self.replacement())
|
|
|
|
.max_columns(self.max_columns()?)
|
2019-04-15 12:51:51 +02:00
|
|
|
.max_columns_preview(self.max_columns_preview())
|
2018-08-03 23:26:22 +02:00
|
|
|
.max_matches(self.max_count()?)
|
|
|
|
.column(self.column())
|
|
|
|
.byte_offset(self.is_present("byte-offset"))
|
|
|
|
.trim_ascii(self.is_present("trim"))
|
|
|
|
.separator_search(None)
|
2019-09-26 13:50:40 +02:00
|
|
|
.separator_context(self.context_separator())
|
2021-05-26 03:41:11 +02:00
|
|
|
.separator_field_match(self.field_match_separator())
|
|
|
|
.separator_field_context(self.field_context_separator())
|
2018-08-03 23:26:22 +02:00
|
|
|
.separator_path(self.path_separator()?)
|
|
|
|
.path_terminator(self.path_terminator());
|
|
|
|
if separator_search {
|
|
|
|
builder.separator_search(self.file_separator()?);
|
|
|
|
}
|
|
|
|
Ok(builder.build(wtr))
|
2018-02-12 19:17:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Build a Summary printer that writes results to the given writer.
|
|
|
|
///
|
|
|
|
/// The given paths are used to configure aspects of the printer.
|
|
|
|
///
|
|
|
|
/// This panics if the output format is not `OutputKind::Summary`.
|
|
|
|
fn printer_summary<W: WriteColor>(
|
|
|
|
&self,
|
|
|
|
paths: &[PathBuf],
|
|
|
|
wtr: W,
|
|
|
|
) -> Result<Summary<W>> {
|
|
|
|
let mut builder = SummaryBuilder::new();
|
|
|
|
builder
|
|
|
|
.kind(self.summary_kind().expect("summary format"))
|
|
|
|
.color_specs(self.color_specs()?)
|
2023-07-08 00:56:50 +02:00
|
|
|
.hyperlink_pattern(self.hyperlink_pattern()?)
|
2018-08-03 23:26:22 +02:00
|
|
|
.stats(self.stats())
|
|
|
|
.path(self.with_filename(paths))
|
|
|
|
.max_matches(self.max_count()?)
|
2019-10-17 04:03:00 +02:00
|
|
|
.exclude_zero(!self.is_present("include-zero"))
|
2018-08-03 23:26:22 +02:00
|
|
|
.separator_field(b":".to_vec())
|
|
|
|
.separator_path(self.path_separator()?)
|
|
|
|
.path_terminator(self.path_terminator());
|
|
|
|
Ok(builder.build(wtr))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build a searcher from the command line parameters.
|
|
|
|
fn searcher(&self, paths: &[PathBuf]) -> Result<Searcher> {
|
|
|
|
let (ctx_before, ctx_after) = self.contexts()?;
|
2020-02-18 01:08:47 +02:00
|
|
|
let line_term = if self.is_present("crlf") {
|
|
|
|
LineTerminator::crlf()
|
|
|
|
} else if self.is_present("null-data") {
|
|
|
|
LineTerminator::byte(b'\x00')
|
|
|
|
} else {
|
|
|
|
LineTerminator::byte(b'\n')
|
|
|
|
};
|
2018-08-03 23:26:22 +02:00
|
|
|
let mut builder = SearcherBuilder::new();
|
|
|
|
builder
|
|
|
|
.line_terminator(line_term)
|
|
|
|
.invert_match(self.is_present("invert-match"))
|
|
|
|
.line_number(self.line_number(paths))
|
|
|
|
.multi_line(self.is_present("multiline"))
|
|
|
|
.before_context(ctx_before)
|
|
|
|
.after_context(ctx_after)
|
|
|
|
.passthru(self.is_present("passthru"))
|
2021-07-07 18:50:23 +02:00
|
|
|
.memory_map(self.mmap_choice(paths))
|
|
|
|
.stop_on_nonmatch(self.is_present("stop-on-nonmatch"));
|
2019-03-04 18:18:45 +02:00
|
|
|
match self.encoding()? {
|
|
|
|
EncodingMode::Some(enc) => {
|
|
|
|
builder.encoding(Some(enc));
|
|
|
|
}
|
|
|
|
EncodingMode::Auto => {} // default for the searcher
|
|
|
|
EncodingMode::Disabled => {
|
|
|
|
builder.bom_sniffing(false);
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
Ok(builder.build())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a builder for recursively traversing a directory while
|
|
|
|
/// respecting ignore rules.
|
|
|
|
///
|
|
|
|
/// If there was a problem parsing the CLI arguments necessary for
|
|
|
|
/// constructing the builder, then this returns an error.
|
2023-01-05 15:15:09 +02:00
|
|
|
fn walker_builder(
|
|
|
|
&self,
|
|
|
|
paths: &[PathBuf],
|
|
|
|
threads: usize,
|
|
|
|
) -> Result<WalkBuilder> {
|
2018-08-03 23:26:22 +02:00
|
|
|
let mut builder = WalkBuilder::new(&paths[0]);
|
|
|
|
for path in &paths[1..] {
|
|
|
|
builder.add(path);
|
|
|
|
}
|
2020-03-15 17:04:47 +02:00
|
|
|
if !self.no_ignore_files() {
|
|
|
|
for path in self.ignore_paths() {
|
|
|
|
if let Some(err) = builder.add_ignore(path) {
|
|
|
|
ignore_message!("{}", err);
|
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
builder
|
|
|
|
.max_depth(self.usize_of("max-depth")?)
|
|
|
|
.follow_links(self.is_present("follow"))
|
|
|
|
.max_filesize(self.max_file_size()?)
|
2023-01-05 15:15:09 +02:00
|
|
|
.threads(threads)
|
2018-08-26 03:08:42 +02:00
|
|
|
.same_file_system(self.is_present("one-file-system"))
|
2019-01-24 03:04:44 +02:00
|
|
|
.skip_stdout(!self.is_present("files"))
|
2018-08-03 23:26:22 +02:00
|
|
|
.overrides(self.overrides()?)
|
|
|
|
.types(self.types()?)
|
|
|
|
.hidden(!self.hidden())
|
|
|
|
.parents(!self.no_ignore_parent())
|
2019-01-26 20:40:12 +02:00
|
|
|
.ignore(!self.no_ignore_dot())
|
|
|
|
.git_global(!self.no_ignore_vcs() && !self.no_ignore_global())
|
|
|
|
.git_ignore(!self.no_ignore_vcs())
|
2019-11-07 02:23:57 +02:00
|
|
|
.git_exclude(!self.no_ignore_vcs() && !self.no_ignore_exclude())
|
2020-02-17 21:43:22 +02:00
|
|
|
.require_git(!self.is_present("no-require-git"))
|
2019-01-21 03:32:34 +02:00
|
|
|
.ignore_case_insensitive(self.ignore_file_case_insensitive());
|
2022-05-08 15:48:56 +02:00
|
|
|
if !self.no_ignore() && !self.no_ignore_dot() {
|
2018-08-03 23:26:22 +02:00
|
|
|
builder.add_custom_ignore_filename(".rgignore");
|
|
|
|
}
|
2022-11-28 06:36:15 +02:00
|
|
|
self.sort_by()?.configure_builder_sort(&mut builder);
|
2018-08-03 23:26:22 +02:00
|
|
|
Ok(builder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mid level routines for converting command line arguments into various types
|
|
|
|
/// of data structures.
|
|
|
|
///
|
|
|
|
/// Methods are sorted alphabetically.
|
|
|
|
impl ArgMatches {
|
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
2019-04-09 01:28:38 +02:00
|
|
|
/// Returns the form of binary detection to perform on files that are
|
|
|
|
/// implicitly searched via recursive directory traversal.
|
|
|
|
fn binary_detection_implicit(&self) -> BinaryDetection {
|
2020-02-18 01:08:47 +02:00
|
|
|
let none = self.is_present("text") || self.is_present("null-data");
|
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
2019-04-09 01:28:38 +02:00
|
|
|
let convert =
|
2020-02-18 01:08:47 +02:00
|
|
|
self.is_present("binary") || self.unrestricted_count() >= 3;
|
2018-08-03 23:26:22 +02:00
|
|
|
if none {
|
|
|
|
BinaryDetection::none()
|
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
2019-04-09 01:28:38 +02:00
|
|
|
} else if convert {
|
|
|
|
BinaryDetection::convert(b'\x00')
|
2018-06-24 02:17:29 +02:00
|
|
|
} else {
|
2018-08-03 23:26:22 +02:00
|
|
|
BinaryDetection::quit(b'\x00')
|
2018-06-24 02:17:29 +02:00
|
|
|
}
|
2016-09-14 03:11:46 +02:00
|
|
|
}
|
|
|
|
|
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
2019-04-09 01:28:38 +02:00
|
|
|
/// Returns the form of binary detection to perform on files that are
|
|
|
|
/// explicitly searched via the user invoking ripgrep on a particular
|
|
|
|
/// file or files or stdin.
|
|
|
|
///
|
|
|
|
/// In general, this should never be BinaryDetection::quit, since that acts
|
|
|
|
/// as a filter (but quitting immediately once a NUL byte is seen), and we
|
|
|
|
/// should never filter out files that the user wants to explicitly search.
|
|
|
|
fn binary_detection_explicit(&self) -> BinaryDetection {
|
2020-02-18 01:08:47 +02:00
|
|
|
let none = self.is_present("text") || self.is_present("null-data");
|
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
2019-04-09 01:28:38 +02:00
|
|
|
if none {
|
|
|
|
BinaryDetection::none()
|
|
|
|
} else {
|
|
|
|
BinaryDetection::convert(b'\x00')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns true if the command line configuration implies that a match
|
|
|
|
/// can never be shown.
|
|
|
|
fn can_never_match(&self, patterns: &[String]) -> bool {
|
|
|
|
patterns.is_empty() || self.max_count().ok() == Some(Some(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if and only if case should be ignore.
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 17:27:30 +02:00
|
|
|
///
|
2018-08-03 23:26:22 +02:00
|
|
|
/// If --case-sensitive is present, then case is never ignored, even if
|
|
|
|
/// --ignore-case is present.
|
|
|
|
fn case_insensitive(&self) -> bool {
|
|
|
|
self.is_present("ignore-case") && !self.is_present("case-sensitive")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if and only if smart case has been enabled.
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 17:27:30 +02:00
|
|
|
///
|
2018-08-03 23:26:22 +02:00
|
|
|
/// If either --ignore-case of --case-sensitive are present, then smart
|
|
|
|
/// case is disabled.
|
|
|
|
fn case_smart(&self) -> bool {
|
|
|
|
self.is_present("smart-case")
|
2020-02-18 01:08:47 +02:00
|
|
|
&& !self.is_present("ignore-case")
|
|
|
|
&& !self.is_present("case-sensitive")
|
Don't search stdout redirected file.
When running ripgrep like this:
rg foo > output
we must be careful not to search `output` since ripgrep is actively writing
to it. Searching it can cause massive blowups where the file grows without
bound.
While this is conceptually easy to fix (check the inode of the redirection
and the inode of the file you're about to search), there are a few problems
with it.
First, inodes are a Unix thing, so we need a Windows specific solution to
this as well. To resolve this concern, I created a new crate, `same-file`,
which provides a cross platform abstraction.
Second, stat'ing every file is costly. This is not avoidable on Windows,
but on Unix, we can get the inode number directly from directory traversal.
However, this information wasn't exposed, but now it is (through both the
ignore and walkdir crates).
Fixes #286
2017-01-08 17:27:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns the user's color choice based on command line parameters and
|
|
|
|
/// environment.
|
|
|
|
fn color_choice(&self) -> ColorChoice {
|
|
|
|
let preference = match self.value_of_lossy("color") {
|
|
|
|
None => "auto".to_string(),
|
|
|
|
Some(v) => v,
|
|
|
|
};
|
|
|
|
if preference == "always" {
|
|
|
|
ColorChoice::Always
|
|
|
|
} else if preference == "ansi" {
|
|
|
|
ColorChoice::AlwaysAnsi
|
|
|
|
} else if preference == "auto" {
|
2023-09-20 20:42:03 +02:00
|
|
|
if std::io::stdout().is_terminal() || self.is_present("pretty") {
|
2018-08-03 23:26:22 +02:00
|
|
|
ColorChoice::Auto
|
|
|
|
} else {
|
|
|
|
ColorChoice::Never
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ColorChoice::Never
|
|
|
|
}
|
2016-09-09 03:46:14 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns the color specifications given by the user on the CLI.
|
|
|
|
///
|
|
|
|
/// If the was a problem parsing any of the provided specs, then an error
|
|
|
|
/// is returned.
|
|
|
|
fn color_specs(&self) -> Result<ColorSpecs> {
|
|
|
|
// Start with a default set of color specs.
|
2018-08-30 02:53:52 +02:00
|
|
|
let mut specs = default_color_specs();
|
2018-08-03 23:26:22 +02:00
|
|
|
for spec_str in self.values_of_lossy_vec("colors") {
|
|
|
|
specs.push(spec_str.parse()?);
|
|
|
|
}
|
|
|
|
Ok(ColorSpecs::new(&specs))
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns true if and only if column numbers should be shown.
|
|
|
|
fn column(&self) -> bool {
|
|
|
|
if self.is_present("no-column") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
self.is_present("column") || self.is_present("vimgrep")
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns the before and after contexts from the command line.
|
|
|
|
///
|
|
|
|
/// If a context setting was absent, then `0` is returned.
|
|
|
|
///
|
|
|
|
/// If there was a problem parsing the values from the user as an integer,
|
|
|
|
/// then an error is returned.
|
|
|
|
fn contexts(&self) -> Result<(usize, usize)> {
|
|
|
|
let both = self.usize_of("context")?.unwrap_or(0);
|
2023-07-07 16:51:51 +02:00
|
|
|
let after = self.usize_of("after-context")?.unwrap_or(both);
|
|
|
|
let before = self.usize_of("before-context")?.unwrap_or(both);
|
|
|
|
Ok((before, after))
|
2016-09-07 03:47:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns the unescaped context separator in UTF-8 bytes.
|
|
|
|
///
|
|
|
|
/// If one was not provided, the default `--` is returned.
|
2019-09-26 13:50:40 +02:00
|
|
|
/// If --no-context-separator is passed, None is returned.
|
|
|
|
fn context_separator(&self) -> Option<Vec<u8>> {
|
|
|
|
let nosep = self.is_present("no-context-separator");
|
|
|
|
let sep = self.value_of_os("context-separator");
|
|
|
|
match (nosep, sep) {
|
|
|
|
(true, _) => None,
|
|
|
|
(false, None) => Some(b"--".to_vec()),
|
|
|
|
(false, Some(sep)) => Some(cli::unescape_os(&sep)),
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns whether the -c/--count or the --count-matches flags were
|
|
|
|
/// passed from the command line.
|
|
|
|
///
|
|
|
|
/// If --count-matches and --invert-match were passed in, behave
|
|
|
|
/// as if --count and --invert-match were passed in (i.e. rg will
|
|
|
|
/// count inverted matches as per existing behavior).
|
|
|
|
fn counts(&self) -> (bool, bool) {
|
|
|
|
let count = self.is_present("count");
|
|
|
|
let count_matches = self.is_present("count-matches");
|
|
|
|
let invert_matches = self.is_present("invert-match");
|
|
|
|
let only_matching = self.is_present("only-matching");
|
|
|
|
if count_matches && invert_matches {
|
|
|
|
// Treat `-v --count-matches` as `-v -c`.
|
|
|
|
(true, false)
|
|
|
|
} else if count && only_matching {
|
|
|
|
// Treat `-c --only-matching` as `--count-matches`.
|
|
|
|
(false, true)
|
|
|
|
} else {
|
|
|
|
(count, count_matches)
|
|
|
|
}
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Parse the dfa-size-limit argument option into a byte count.
|
|
|
|
fn dfa_size_limit(&self) -> Result<Option<usize>> {
|
|
|
|
let r = self.parse_human_readable_size("dfa-size-limit")?;
|
|
|
|
u64_to_usize("dfa-size-limit", r)
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 18:18:45 +02:00
|
|
|
/// Returns the encoding mode to use.
|
2018-08-03 23:26:22 +02:00
|
|
|
///
|
2019-03-04 18:18:45 +02:00
|
|
|
/// This only returns an encoding if one is explicitly specified. Otherwise
|
|
|
|
/// if set to automatic, the Searcher will do BOM sniffing for UTF-16
|
|
|
|
/// and transcode seamlessly. If disabled, no BOM sniffing nor transcoding
|
|
|
|
/// will occur.
|
|
|
|
fn encoding(&self) -> Result<EncodingMode> {
|
2018-08-03 23:26:22 +02:00
|
|
|
if self.is_present("no-encoding") {
|
2019-03-04 18:18:45 +02:00
|
|
|
return Ok(EncodingMode::Auto);
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
2019-03-04 18:18:45 +02:00
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
let label = match self.value_of_lossy("encoding") {
|
2019-03-04 18:18:45 +02:00
|
|
|
None => return Ok(EncodingMode::Auto),
|
2018-08-03 23:26:22 +02:00
|
|
|
Some(label) => label,
|
|
|
|
};
|
2019-03-04 18:18:45 +02:00
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
if label == "auto" {
|
2019-03-04 18:18:45 +02:00
|
|
|
return Ok(EncodingMode::Auto);
|
|
|
|
} else if label == "none" {
|
|
|
|
return Ok(EncodingMode::Disabled);
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
2019-03-04 18:18:45 +02:00
|
|
|
|
|
|
|
Ok(EncodingMode::Some(Encoding::new(&label)?))
|
2016-11-06 21:36:08 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Return the file separator to use based on the CLI configuration.
|
|
|
|
fn file_separator(&self) -> Result<Option<Vec<u8>>> {
|
|
|
|
// File separators are only used for the standard grep-line format.
|
|
|
|
if self.output_kind() != OutputKind::Standard {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (ctx_before, ctx_after) = self.contexts()?;
|
|
|
|
Ok(if self.heading() {
|
|
|
|
Some(b"".to_vec())
|
|
|
|
} else if ctx_before > 0 || ctx_after > 0 {
|
2019-09-26 13:50:40 +02:00
|
|
|
self.context_separator()
|
2018-08-03 23:26:22 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
2018-04-24 00:18:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns true if and only if matches should be grouped with file name
|
|
|
|
/// headings.
|
|
|
|
fn heading(&self) -> bool {
|
|
|
|
if self.is_present("no-heading") || self.is_present("vimgrep") {
|
|
|
|
false
|
|
|
|
} else {
|
2023-09-20 20:42:03 +02:00
|
|
|
std::io::stdout().is_terminal()
|
2020-02-18 01:08:47 +02:00
|
|
|
|| self.is_present("heading")
|
|
|
|
|| self.is_present("pretty")
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
2016-11-06 03:44:15 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns true if and only if hidden files/directories should be
|
|
|
|
/// searched.
|
|
|
|
fn hidden(&self) -> bool {
|
|
|
|
self.is_present("hidden") || self.unrestricted_count() >= 2
|
2016-11-06 03:44:15 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 00:56:50 +02:00
|
|
|
/// Returns the hyperlink pattern to use. A default pattern suitable
|
|
|
|
/// for the current system is used if the value is not set.
|
|
|
|
///
|
|
|
|
/// If an invalid pattern is provided, then an error is returned.
|
|
|
|
fn hyperlink_pattern(&self) -> Result<HyperlinkPattern> {
|
|
|
|
Ok(match self.value_of_lossy("hyperlink-format") {
|
|
|
|
Some(pattern) => HyperlinkPattern::from_str(&pattern)?,
|
|
|
|
None => HyperlinkPattern::default_file_scheme(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-21 03:32:34 +02:00
|
|
|
/// Returns true if ignore files should be processed case insensitively.
|
|
|
|
fn ignore_file_case_insensitive(&self) -> bool {
|
|
|
|
self.is_present("ignore-file-case-insensitive")
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Return all of the ignore file paths given on the command line.
|
|
|
|
fn ignore_paths(&self) -> Vec<PathBuf> {
|
|
|
|
let paths = match self.values_of_os("ignore-file") {
|
|
|
|
None => return vec![],
|
|
|
|
Some(paths) => paths,
|
|
|
|
};
|
|
|
|
paths.map(|p| Path::new(p).to_path_buf()).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if and only if ripgrep is invoked in a way where it knows
|
|
|
|
/// it search exactly one thing.
|
|
|
|
fn is_one_search(&self, paths: &[PathBuf]) -> bool {
|
|
|
|
if paths.len() != 1 {
|
|
|
|
return false;
|
2016-09-27 05:56:15 +02:00
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
self.is_only_stdin(paths) || paths[0].is_file()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if and only if we're only searching a single thing and
|
|
|
|
/// that thing is stdin.
|
|
|
|
fn is_only_stdin(&self, paths: &[PathBuf]) -> bool {
|
|
|
|
paths == [Path::new("-")]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if and only if we should show line numbers.
|
|
|
|
fn line_number(&self, paths: &[PathBuf]) -> bool {
|
|
|
|
if self.output_kind() == OutputKind::Summary {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if self.is_present("no-line-number") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if self.output_kind() == OutputKind::JSON {
|
|
|
|
return true;
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
2016-10-12 01:57:09 +02:00
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
// A few things can imply counting line numbers. In particular, we
|
|
|
|
// generally want to show line numbers by default when printing to a
|
|
|
|
// tty for human consumption, except for one interesting case: when
|
|
|
|
// we're only searching stdin. This makes pipelines work as expected.
|
2023-09-20 20:42:03 +02:00
|
|
|
(std::io::stdout().is_terminal() && !self.is_only_stdin(paths))
|
2020-02-18 01:08:47 +02:00
|
|
|
|| self.is_present("line-number")
|
|
|
|
|| self.is_present("column")
|
|
|
|
|| self.is_present("pretty")
|
|
|
|
|| self.is_present("vimgrep")
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// The maximum number of columns allowed on each line.
|
|
|
|
///
|
|
|
|
/// If `0` is provided, then this returns `None`.
|
|
|
|
fn max_columns(&self) -> Result<Option<u64>> {
|
|
|
|
Ok(self.usize_of_nonzero("max-columns")?.map(|n| n as u64))
|
|
|
|
}
|
|
|
|
|
2019-04-14 00:35:24 +02:00
|
|
|
/// Returns true if and only if a preview should be shown for lines that
|
|
|
|
/// exceed the maximum column limit.
|
2019-04-15 12:51:51 +02:00
|
|
|
fn max_columns_preview(&self) -> bool {
|
|
|
|
self.is_present("max-columns-preview")
|
2019-04-14 00:35:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// The maximum number of matches permitted.
|
|
|
|
fn max_count(&self) -> Result<Option<u64>> {
|
|
|
|
Ok(self.usize_of("max-count")?.map(|n| n as u64))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses the max-filesize argument option into a byte count.
|
|
|
|
fn max_file_size(&self) -> Result<Option<u64>> {
|
|
|
|
self.parse_human_readable_size("max-filesize")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether we should attempt to use memory maps or not.
|
|
|
|
fn mmap_choice(&self, paths: &[PathBuf]) -> MmapChoice {
|
|
|
|
// SAFETY: Memory maps are difficult to impossible to encapsulate
|
|
|
|
// safely in a portable way that doesn't simultaneously negate some of
|
|
|
|
// the benfits of using memory maps. For ripgrep's use, we never mutate
|
|
|
|
// a memory map and generally never store the contents of memory map
|
|
|
|
// in a data structure that depends on immutability. Generally
|
|
|
|
// speaking, the worst thing that can happen is a SIGBUS (if the
|
|
|
|
// underlying file is truncated while reading it), which will cause
|
|
|
|
// ripgrep to abort. This reasoning should be treated as suspect.
|
|
|
|
let maybe = unsafe { MmapChoice::auto() };
|
|
|
|
let never = MmapChoice::never();
|
|
|
|
if self.is_present("no-mmap") {
|
|
|
|
never
|
|
|
|
} else if self.is_present("mmap") {
|
|
|
|
maybe
|
|
|
|
} else if paths.len() <= 10 && paths.iter().all(|p| p.is_file()) {
|
|
|
|
// If we're only searching a few paths and all of them are
|
|
|
|
// files, then memory maps are probably faster.
|
|
|
|
maybe
|
|
|
|
} else {
|
|
|
|
never
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if ignore files should be ignored.
|
|
|
|
fn no_ignore(&self) -> bool {
|
|
|
|
self.is_present("no-ignore") || self.unrestricted_count() >= 1
|
|
|
|
}
|
|
|
|
|
2019-01-26 20:40:12 +02:00
|
|
|
/// Returns true if .ignore files should be ignored.
|
|
|
|
fn no_ignore_dot(&self) -> bool {
|
|
|
|
self.is_present("no-ignore-dot") || self.no_ignore()
|
|
|
|
}
|
|
|
|
|
2019-11-07 02:23:57 +02:00
|
|
|
/// Returns true if local exclude (ignore) files should be ignored.
|
|
|
|
fn no_ignore_exclude(&self) -> bool {
|
|
|
|
self.is_present("no-ignore-exclude") || self.no_ignore()
|
|
|
|
}
|
|
|
|
|
2020-03-15 17:04:47 +02:00
|
|
|
/// Returns true if explicitly given ignore files should be ignored.
|
|
|
|
fn no_ignore_files(&self) -> bool {
|
|
|
|
// We don't look at no-ignore here because --no-ignore is explicitly
|
|
|
|
// documented to not override --ignore-file. We could change this, but
|
|
|
|
// it would be a fairly severe breaking change.
|
|
|
|
self.is_present("no-ignore-files")
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns true if global ignore files should be ignored.
|
|
|
|
fn no_ignore_global(&self) -> bool {
|
|
|
|
self.is_present("no-ignore-global") || self.no_ignore()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if parent ignore files should be ignored.
|
|
|
|
fn no_ignore_parent(&self) -> bool {
|
|
|
|
self.is_present("no-ignore-parent") || self.no_ignore()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if VCS ignore files should be ignored.
|
|
|
|
fn no_ignore_vcs(&self) -> bool {
|
|
|
|
self.is_present("no-ignore-vcs") || self.no_ignore()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determine the type of output we should produce.
|
|
|
|
fn output_kind(&self) -> OutputKind {
|
|
|
|
if self.is_present("quiet") {
|
|
|
|
// While we don't technically print results (or aggregate results)
|
|
|
|
// in quiet mode, we still support the --stats flag, and those
|
|
|
|
// stats are computed by the Summary printer for now.
|
|
|
|
return OutputKind::Summary;
|
|
|
|
} else if self.is_present("json") {
|
|
|
|
return OutputKind::JSON;
|
|
|
|
}
|
|
|
|
|
2018-02-20 17:33:07 +02:00
|
|
|
let (count, count_matches) = self.counts();
|
2020-02-18 01:08:47 +02:00
|
|
|
let summary = count
|
2018-08-03 23:26:22 +02:00
|
|
|
|| count_matches
|
|
|
|
|| self.is_present("files-with-matches")
|
|
|
|
|| self.is_present("files-without-match");
|
|
|
|
if summary {
|
|
|
|
OutputKind::Summary
|
|
|
|
} else {
|
|
|
|
OutputKind::Standard
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the set of glob overrides from the command line flags.
|
|
|
|
fn overrides(&self) -> Result<Override> {
|
2020-02-17 04:03:07 +02:00
|
|
|
let globs = self.values_of_lossy_vec("glob");
|
|
|
|
let iglobs = self.values_of_lossy_vec("iglob");
|
|
|
|
if globs.is_empty() && iglobs.is_empty() {
|
|
|
|
return Ok(Override::empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut builder = OverrideBuilder::new(current_dir()?);
|
2019-08-01 23:08:58 +02:00
|
|
|
// Make all globs case insensitive with --glob-case-insensitive.
|
|
|
|
if self.is_present("glob-case-insensitive") {
|
|
|
|
builder.case_insensitive(true).unwrap();
|
|
|
|
}
|
2020-02-17 04:03:07 +02:00
|
|
|
for glob in globs {
|
2018-08-03 23:26:22 +02:00
|
|
|
builder.add(&glob)?;
|
|
|
|
}
|
|
|
|
// This only enables case insensitivity for subsequent globs.
|
2019-01-21 03:32:34 +02:00
|
|
|
builder.case_insensitive(true).unwrap();
|
2020-02-17 04:03:07 +02:00
|
|
|
for glob in iglobs {
|
2018-08-03 23:26:22 +02:00
|
|
|
builder.add(&glob)?;
|
|
|
|
}
|
|
|
|
Ok(builder.build()?)
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return all file paths that ripgrep should search.
|
2018-08-03 23:26:22 +02:00
|
|
|
///
|
|
|
|
/// If no paths were given, then this returns an empty list.
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
fn paths(&self) -> Vec<PathBuf> {
|
2018-02-03 21:31:40 +02:00
|
|
|
let mut paths: Vec<PathBuf> = match self.values_of_os("path") {
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
None => vec![],
|
2018-08-03 23:26:22 +02:00
|
|
|
Some(paths) => paths.map(|p| Path::new(p).to_path_buf()).collect(),
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
};
|
|
|
|
// If --file, --files or --regexp is given, then the first path is
|
|
|
|
// always in `pattern`.
|
|
|
|
if self.is_present("file")
|
|
|
|
|| self.is_present("files")
|
2018-08-03 23:26:22 +02:00
|
|
|
|| self.is_present("regexp")
|
|
|
|
{
|
2018-02-03 21:31:40 +02:00
|
|
|
if let Some(path) = self.value_of_os("pattern") {
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
paths.insert(0, Path::new(path).to_path_buf());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paths
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Return the default path that ripgrep should search. This should only
|
|
|
|
/// be used when ripgrep is not otherwise given at least one file path
|
|
|
|
/// as a positional argument.
|
|
|
|
fn path_default(&self) -> PathBuf {
|
2020-02-18 01:08:47 +02:00
|
|
|
let file_is_stdin = self
|
|
|
|
.values_of_os("file")
|
2018-08-03 23:26:22 +02:00
|
|
|
.map_or(false, |mut files| files.any(|f| f == "-"));
|
2020-02-18 01:08:47 +02:00
|
|
|
let search_cwd = !cli::is_readable_stdin()
|
2016-11-18 03:48:11 +02:00
|
|
|
|| (self.is_present("file") && file_is_stdin)
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
|| self.is_present("files")
|
2019-04-14 22:46:02 +02:00
|
|
|
|| self.is_present("type-list")
|
|
|
|
|| self.is_present("pcre2-version");
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
if search_cwd {
|
|
|
|
Path::new("./").to_path_buf()
|
|
|
|
} else {
|
|
|
|
Path::new("-").to_path_buf()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns the unescaped path separator as a single byte, if one exists.
|
|
|
|
///
|
|
|
|
/// If the provided path separator is more than a single byte, then an
|
|
|
|
/// error is returned.
|
|
|
|
fn path_separator(&self) -> Result<Option<u8>> {
|
2018-08-30 02:53:52 +02:00
|
|
|
let sep = match self.value_of_os("path-separator") {
|
2018-08-03 23:26:22 +02:00
|
|
|
None => return Ok(None),
|
2018-08-30 02:53:52 +02:00
|
|
|
Some(sep) => cli::unescape_os(&sep),
|
2018-08-03 23:26:22 +02:00
|
|
|
};
|
|
|
|
if sep.is_empty() {
|
|
|
|
Ok(None)
|
|
|
|
} else if sep.len() > 1 {
|
|
|
|
Err(From::from(format!(
|
|
|
|
"A path separator must be exactly one byte, but \
|
|
|
|
the given separator is {} bytes: {}\n\
|
|
|
|
In some shells on Windows '/' is automatically \
|
|
|
|
expanded. Use '//' instead.",
|
2020-02-18 01:08:47 +02:00
|
|
|
sep.len(),
|
|
|
|
cli::escape(&sep),
|
2018-08-03 23:26:22 +02:00
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(Some(sep[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the byte that should be used to terminate paths.
|
|
|
|
///
|
|
|
|
/// Typically, this is only set to `\x00` when the --null flag is provided,
|
|
|
|
/// and `None` otherwise.
|
|
|
|
fn path_terminator(&self) -> Option<u8> {
|
|
|
|
if self.is_present("null") {
|
|
|
|
Some(b'\x00')
|
|
|
|
} else {
|
|
|
|
None
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 03:41:11 +02:00
|
|
|
/// Returns the unescaped field context separator. If one wasn't specified,
|
|
|
|
/// then '-' is used as the default.
|
|
|
|
fn field_context_separator(&self) -> Vec<u8> {
|
|
|
|
match self.value_of_os("field-context-separator") {
|
|
|
|
None => b"-".to_vec(),
|
|
|
|
Some(sep) => cli::unescape_os(&sep),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the unescaped field match separator. If one wasn't specified,
|
|
|
|
/// then ':' is used as the default.
|
|
|
|
fn field_match_separator(&self) -> Vec<u8> {
|
|
|
|
match self.value_of_os("field-match-separator") {
|
|
|
|
None => b":".to_vec(),
|
|
|
|
Some(sep) => cli::unescape_os(&sep),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
/// Get a sequence of all available patterns from the command line.
|
|
|
|
/// This includes reading the -e/--regexp and -f/--file flags.
|
|
|
|
///
|
|
|
|
/// If any pattern is invalid UTF-8, then an error is returned.
|
|
|
|
fn patterns(&self) -> Result<Vec<String>> {
|
2017-02-18 22:34:54 +02:00
|
|
|
if self.is_present("files") || self.is_present("type-list") {
|
2018-08-03 23:26:22 +02:00
|
|
|
return Ok(vec![]);
|
2017-02-18 22:34:54 +02:00
|
|
|
}
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
let mut pats = vec![];
|
|
|
|
match self.values_of_os("regexp") {
|
|
|
|
None => {
|
|
|
|
if self.values_of_os("file").is_none() {
|
2018-02-03 21:31:40 +02:00
|
|
|
if let Some(os_pat) = self.value_of_os("pattern") {
|
2018-08-03 23:26:22 +02:00
|
|
|
pats.push(self.pattern_from_os_str(os_pat)?);
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
Some(os_pats) => {
|
|
|
|
for os_pat in os_pats {
|
2018-08-03 23:26:22 +02:00
|
|
|
pats.push(self.pattern_from_os_str(os_pat)?);
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
2018-08-30 02:53:52 +02:00
|
|
|
if let Some(paths) = self.values_of_os("file") {
|
|
|
|
for path in paths {
|
|
|
|
if path == "-" {
|
2020-02-18 01:08:47 +02:00
|
|
|
pats.extend(
|
|
|
|
cli::patterns_from_stdin()?
|
|
|
|
.into_iter()
|
|
|
|
.map(|p| self.pattern_from_string(p)),
|
2019-01-26 23:00:43 +02:00
|
|
|
);
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
} else {
|
2020-02-18 01:08:47 +02:00
|
|
|
pats.extend(
|
|
|
|
cli::patterns_from_path(path)?
|
|
|
|
.into_iter()
|
|
|
|
.map(|p| self.pattern_from_string(p)),
|
2019-01-26 23:00:43 +02:00
|
|
|
);
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
Ok(pats)
|
2016-09-05 06:52:23 +02:00
|
|
|
}
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Converts an OsStr pattern to a String pattern. The pattern is escaped
|
|
|
|
/// if -F/--fixed-strings is set.
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
///
|
|
|
|
/// If the pattern is not valid UTF-8, then an error is returned.
|
2018-08-03 23:26:22 +02:00
|
|
|
fn pattern_from_os_str(&self, pat: &OsStr) -> Result<String> {
|
2018-08-30 02:53:52 +02:00
|
|
|
let s = cli::pattern_from_os(pat)?;
|
2018-08-03 23:26:22 +02:00
|
|
|
Ok(self.pattern_from_str(s))
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Converts a &str pattern to a String pattern. The pattern is escaped
|
|
|
|
/// if -F/--fixed-strings is set.
|
|
|
|
fn pattern_from_str(&self, pat: &str) -> String {
|
2019-01-26 23:00:43 +02:00
|
|
|
self.pattern_from_string(pat.to_string())
|
|
|
|
}
|
2017-08-09 12:53:35 +02:00
|
|
|
|
2019-01-26 23:00:43 +02:00
|
|
|
/// Applies additional processing on the given pattern if necessary
|
|
|
|
/// (such as escaping meta characters or turning it into a line regex).
|
|
|
|
fn pattern_from_string(&self, pat: String) -> String {
|
|
|
|
if pat.is_empty() {
|
2023-06-20 02:47:07 +02:00
|
|
|
// This would normally just be an empty string, which works on its
|
|
|
|
// own, but if the patterns are joined in a set of alternations,
|
|
|
|
// then you wind up with `foo|`, which is currently invalid in
|
|
|
|
// Rust's regex engine.
|
|
|
|
"(?:)".to_string()
|
2018-03-10 17:34:35 +02:00
|
|
|
} else {
|
2018-08-03 23:26:22 +02:00
|
|
|
pat
|
2018-02-20 17:33:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns the preprocessor command if one was specified.
|
|
|
|
fn preprocessor(&self) -> Option<PathBuf> {
|
|
|
|
let path = match self.value_of_os("pre") {
|
|
|
|
None => return None,
|
|
|
|
Some(path) => path,
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 18:14:52 +02:00
|
|
|
};
|
2018-08-03 23:26:22 +02:00
|
|
|
if path.is_empty() {
|
|
|
|
return None;
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 18:14:52 +02:00
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
Some(Path::new(path).to_path_buf())
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 18:14:52 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 04:45:24 +02:00
|
|
|
/// Builds the set of globs for filtering files to apply to the --pre
|
|
|
|
/// flag. If no --pre-globs are available, then this always returns an
|
|
|
|
/// empty set of globs.
|
|
|
|
fn preprocessor_globs(&self) -> Result<Override> {
|
2020-02-17 04:03:07 +02:00
|
|
|
let globs = self.values_of_lossy_vec("pre-glob");
|
|
|
|
if globs.is_empty() {
|
|
|
|
return Ok(Override::empty());
|
|
|
|
}
|
|
|
|
let mut builder = OverrideBuilder::new(current_dir()?);
|
|
|
|
for glob in globs {
|
2018-09-05 04:45:24 +02:00
|
|
|
builder.add(&glob)?;
|
|
|
|
}
|
|
|
|
Ok(builder.build()?)
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Parse the regex-size-limit argument option into a byte count.
|
|
|
|
fn regex_size_limit(&self) -> Result<Option<usize>> {
|
|
|
|
let r = self.parse_human_readable_size("regex-size-limit")?;
|
|
|
|
u64_to_usize("regex-size-limit", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the replacement string as UTF-8 bytes if it exists.
|
|
|
|
fn replacement(&self) -> Option<Vec<u8>> {
|
|
|
|
self.value_of_lossy("replace").map(|s| s.into_bytes())
|
|
|
|
}
|
|
|
|
|
2018-08-26 04:53:27 +02:00
|
|
|
/// Returns the sorting criteria based on command line parameters.
|
|
|
|
fn sort_by(&self) -> Result<SortBy> {
|
|
|
|
// For backcompat, continue supporting deprecated --sort-files flag.
|
|
|
|
if self.is_present("sort-files") {
|
|
|
|
return Ok(SortBy::asc(SortByKind::Path));
|
|
|
|
}
|
|
|
|
let sortby = match self.value_of_lossy("sort") {
|
|
|
|
None => match self.value_of_lossy("sortr") {
|
|
|
|
None => return Ok(SortBy::none()),
|
|
|
|
Some(choice) => SortBy::desc(SortByKind::new(&choice)),
|
2020-02-18 01:08:47 +02:00
|
|
|
},
|
2018-08-26 04:53:27 +02:00
|
|
|
Some(choice) => SortBy::asc(SortByKind::new(&choice)),
|
|
|
|
};
|
|
|
|
Ok(sortby)
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns true if and only if aggregate statistics for a search should
|
|
|
|
/// be tracked.
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 18:14:52 +02:00
|
|
|
///
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Generally, this is only enabled when explicitly requested by in the
|
|
|
|
/// command line arguments via the --stats flag, but this can also be
|
2020-06-04 15:06:09 +02:00
|
|
|
/// enabled implicitly via the output format, e.g., for JSON Lines.
|
2018-08-03 23:26:22 +02:00
|
|
|
fn stats(&self) -> bool {
|
|
|
|
self.output_kind() == OutputKind::JSON || self.is_present("stats")
|
Completely re-work colored output and tty handling.
This commit completely guts all of the color handling code and replaces
most of it with two new crates: wincolor and termcolor. wincolor
provides a simple API to coloring using the Windows console and
termcolor provides a platform independent coloring API tuned for
multithreaded command line programs. This required a lot more
flexibility than what the `term` crate provided, so it was dropped.
We instead switch to writing ANSI escape sequences directly and ignore
the TERMINFO database.
In addition to fixing several bugs, this commit also permits end users
to customize colors to a certain extent. For example, this command will
set the match color to magenta and the line number background to yellow:
rg --colors 'match:fg:magenta' --colors 'line:bg:yellow' foo
For tty handling, we've adopted a hack from `git` to do tty detection in
MSYS/mintty terminals. As a result, ripgrep should get both color
detection and piping correct on Windows regardless of which terminal you
use.
Finally, switch to line buffering. Performance doesn't seem to be
impacted and it's an otherwise more user friendly option.
Fixes #37, Fixes #51, Fixes #94, Fixes #117, Fixes #182, Fixes #231
2016-11-20 18:14:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// When the output format is `Summary`, this returns the type of summary
|
|
|
|
/// output to show.
|
|
|
|
///
|
|
|
|
/// This returns `None` if the output format is not `Summary`.
|
|
|
|
fn summary_kind(&self) -> Option<SummaryKind> {
|
|
|
|
let (count, count_matches) = self.counts();
|
|
|
|
if self.is_present("quiet") {
|
|
|
|
Some(SummaryKind::Quiet)
|
|
|
|
} else if count_matches {
|
|
|
|
Some(SummaryKind::CountMatches)
|
|
|
|
} else if count {
|
|
|
|
Some(SummaryKind::Count)
|
|
|
|
} else if self.is_present("files-with-matches") {
|
|
|
|
Some(SummaryKind::PathWithMatch)
|
|
|
|
} else if self.is_present("files-without-match") {
|
|
|
|
Some(SummaryKind::PathWithoutMatch)
|
|
|
|
} else {
|
|
|
|
None
|
2018-02-12 19:17:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Return the number of threads that should be used for parallelism.
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
fn threads(&self) -> Result<usize> {
|
2018-08-26 04:53:27 +02:00
|
|
|
if self.sort_by()?.kind != SortByKind::None {
|
2017-01-07 05:43:59 +02:00
|
|
|
return Ok(1);
|
|
|
|
}
|
2018-01-01 16:22:35 +02:00
|
|
|
let threads = self.usize_of("threads")?.unwrap_or(0);
|
2023-01-05 15:15:09 +02:00
|
|
|
let available =
|
|
|
|
std::thread::available_parallelism().map_or(1, |n| n.get());
|
|
|
|
Ok(if threads == 0 { cmp::min(12, available) } else { threads })
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a file type matcher from the command line flags.
|
|
|
|
fn types(&self) -> Result<Types> {
|
2018-08-03 23:26:22 +02:00
|
|
|
let mut builder = TypesBuilder::new();
|
|
|
|
builder.add_defaults();
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
for ty in self.values_of_lossy_vec("type-clear") {
|
2018-08-03 23:26:22 +02:00
|
|
|
builder.clear(&ty);
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
|
|
|
for def in self.values_of_lossy_vec("type-add") {
|
2018-08-03 23:26:22 +02:00
|
|
|
builder.add_def(&def)?;
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
|
|
|
for ty in self.values_of_lossy_vec("type") {
|
2018-08-03 23:26:22 +02:00
|
|
|
builder.select(&ty);
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
|
|
|
for ty in self.values_of_lossy_vec("type-not") {
|
2018-08-03 23:26:22 +02:00
|
|
|
builder.negate(&ty);
|
|
|
|
}
|
|
|
|
builder.build().map_err(From::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of times the `unrestricted` flag is provided.
|
|
|
|
fn unrestricted_count(&self) -> u64 {
|
|
|
|
self.occurrences_of("unrestricted")
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:34:59 +02:00
|
|
|
/// Returns true if and only if Unicode mode should be enabled.
|
|
|
|
fn unicode(&self) -> bool {
|
|
|
|
// Unicode mode is enabled by default, so only disable it when
|
|
|
|
// --no-unicode is given explicitly.
|
|
|
|
!(self.is_present("no-unicode") || self.is_present("no-pcre2-unicode"))
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Returns true if and only if file names containing each match should
|
|
|
|
/// be emitted.
|
|
|
|
fn with_filename(&self, paths: &[PathBuf]) -> bool {
|
|
|
|
if self.is_present("no-filename") {
|
|
|
|
false
|
|
|
|
} else {
|
ripgrep: fix bug when CWD has directory named `-`
Specifically, when searching stdin, if the current directory has a
directory named `-`, then the `--with-filename` flag would automatically
be turned on. This is because `--with-filename` is automatically enabled
when ripgrep is given a single path that is a directory. When ripgrep is
given empty arguments, and if it is searching stdin, then its default
path list is just simple `["-"]`. The `is_dir` check passes, and
`--with-filename` gets enabled.
This commit fixes the problem by checking whether the path is `-` first.
If so, then we assume it isn't a directory. This is fine, since if it is
a directory and one asks to search it explicitly, then ripgrep will
interpret `-` as stdin anyway (which is arguably a bug on its own, but
probably not one worth fixing).
Fixes #1223, Closes #1292
2019-06-05 17:33:52 +02:00
|
|
|
let path_stdin = Path::new("-");
|
2018-08-03 23:26:22 +02:00
|
|
|
self.is_present("with-filename")
|
2020-02-18 01:08:47 +02:00
|
|
|
|| self.is_present("vimgrep")
|
|
|
|
|| paths.len() > 1
|
|
|
|
|| paths
|
|
|
|
.get(0)
|
|
|
|
.map_or(false, |p| p != path_stdin && p.is_dir())
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Lower level generic helper methods for teasing values out of clap.
|
|
|
|
impl ArgMatches {
|
|
|
|
/// Like values_of_lossy, but returns an empty vec if the flag is not
|
|
|
|
/// present.
|
|
|
|
fn values_of_lossy_vec(&self, name: &str) -> Vec<String> {
|
|
|
|
self.values_of_lossy(name).unwrap_or_else(Vec::new)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Safely reads an arg value with the given name, and if it's present,
|
|
|
|
/// tries to parse it as a usize value.
|
|
|
|
///
|
|
|
|
/// If the number is zero, then it is considered absent and `None` is
|
|
|
|
/// returned.
|
|
|
|
fn usize_of_nonzero(&self, name: &str) -> Result<Option<usize>> {
|
|
|
|
let n = match self.usize_of(name)? {
|
|
|
|
None => return Ok(None),
|
|
|
|
Some(n) => n,
|
|
|
|
};
|
2020-02-18 01:08:47 +02:00
|
|
|
Ok(if n == 0 { None } else { Some(n) })
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Safely reads an arg value with the given name, and if it's present,
|
|
|
|
/// tries to parse it as a usize value.
|
|
|
|
fn usize_of(&self, name: &str) -> Result<Option<usize>> {
|
|
|
|
match self.value_of_lossy(name) {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(v) => v.parse().map(Some).map_err(From::from),
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 23:55:58 +02:00
|
|
|
/// Parses an argument of the form `[0-9]+(KMG)?`.
|
|
|
|
///
|
2018-08-03 23:26:22 +02:00
|
|
|
/// If the aforementioned format is not recognized, then this returns an
|
|
|
|
/// error.
|
|
|
|
fn parse_human_readable_size(
|
2017-04-01 23:55:58 +02:00
|
|
|
&self,
|
|
|
|
arg_name: &str,
|
|
|
|
) -> Result<Option<u64>> {
|
2018-08-30 02:53:52 +02:00
|
|
|
let size = match self.value_of_lossy(arg_name) {
|
|
|
|
None => return Ok(None),
|
|
|
|
Some(size) => size,
|
2017-04-01 23:55:58 +02:00
|
|
|
};
|
2018-08-30 02:53:52 +02:00
|
|
|
Ok(Some(cli::parse_human_readable_size(&size)?))
|
2017-02-28 06:53:52 +02:00
|
|
|
}
|
2018-08-03 23:26:22 +02:00
|
|
|
}
|
2017-02-28 06:53:52 +02:00
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// The following methods mostly dispatch to the underlying clap methods
|
|
|
|
/// directly. Methods that would otherwise get a single value will fetch all
|
|
|
|
/// values and return the last one. (Clap returns the first one.) We only
|
|
|
|
/// define the ones we need.
|
|
|
|
impl ArgMatches {
|
2018-02-03 21:31:40 +02:00
|
|
|
fn is_present(&self, name: &str) -> bool {
|
|
|
|
self.0.is_present(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn occurrences_of(&self, name: &str) -> u64 {
|
|
|
|
self.0.occurrences_of(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn value_of_lossy(&self, name: &str) -> Option<String> {
|
2018-02-06 02:22:44 +02:00
|
|
|
self.0.value_of_lossy(name).map(|s| s.into_owned())
|
2018-02-03 21:31:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn values_of_lossy(&self, name: &str) -> Option<Vec<String>> {
|
|
|
|
self.0.values_of_lossy(name)
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
fn value_of_os(&self, name: &str) -> Option<&OsStr> {
|
2018-02-06 02:22:44 +02:00
|
|
|
self.0.value_of_os(name)
|
2018-02-03 21:31:40 +02:00
|
|
|
}
|
|
|
|
|
2021-06-02 01:47:46 +02:00
|
|
|
fn values_of_os(&self, name: &str) -> Option<clap::OsValues<'_>> {
|
2018-02-03 21:31:40 +02:00
|
|
|
self.0.values_of_os(name)
|
|
|
|
}
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 17:47:34 +02:00
|
|
|
/// Inspect an error resulting from building a Rust regex matcher, and if it's
|
|
|
|
/// believed to correspond to a syntax error that another engine could handle,
|
|
|
|
/// then add a message to suggest the use of the engine flag.
|
|
|
|
fn suggest(msg: String) -> String {
|
|
|
|
if let Some(pcre_msg) = suggest_pcre2(&msg) {
|
|
|
|
return pcre_msg;
|
|
|
|
}
|
|
|
|
msg
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Inspect an error resulting from building a Rust regex matcher, and if it's
|
|
|
|
/// believed to correspond to a syntax error that PCRE2 could handle, then
|
|
|
|
/// add a message to suggest the use of -P/--pcre2.
|
2020-02-27 17:47:34 +02:00
|
|
|
fn suggest_pcre2(msg: &str) -> Option<String> {
|
|
|
|
#[cfg(feature = "pcre2")]
|
|
|
|
fn suggest(msg: &str) -> Option<String> {
|
|
|
|
if !msg.contains("backreferences") && !msg.contains("look-around") {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(format!(
|
|
|
|
"{}
|
2016-12-24 19:53:09 +02:00
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
Consider enabling PCRE2 with the --pcre2 flag, which can handle backreferences
|
2020-02-18 01:08:47 +02:00
|
|
|
and look-around.",
|
2020-02-27 17:47:34 +02:00
|
|
|
msg
|
|
|
|
))
|
|
|
|
}
|
2016-12-24 19:53:09 +02:00
|
|
|
}
|
2020-02-27 17:47:34 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "pcre2"))]
|
|
|
|
fn suggest(_: &str) -> Option<String> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
suggest(msg)
|
2016-12-24 19:53:09 +02:00
|
|
|
}
|
2017-01-15 23:32:30 +02:00
|
|
|
|
2018-09-25 02:58:17 +02:00
|
|
|
fn suggest_multiline(msg: String) -> String {
|
|
|
|
if msg.contains("the literal") && msg.contains("not allowed") {
|
2020-02-18 01:08:47 +02:00
|
|
|
format!(
|
|
|
|
"{}
|
2018-09-25 02:58:17 +02:00
|
|
|
|
|
|
|
Consider enabling multiline mode with the --multiline flag (or -U for short).
|
2020-02-18 01:08:47 +02:00
|
|
|
When multiline mode is enabled, new line characters can be matched.",
|
|
|
|
msg
|
|
|
|
)
|
2018-09-25 02:58:17 +02:00
|
|
|
} else {
|
|
|
|
msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
/// Convert the result of parsing a human readable file size to a `usize`,
|
|
|
|
/// failing if the type does not fit.
|
2020-02-18 01:08:47 +02:00
|
|
|
fn u64_to_usize(arg_name: &str, value: Option<u64>) -> Result<Option<usize>> {
|
2017-04-01 23:55:58 +02:00
|
|
|
use std::usize;
|
|
|
|
|
2018-08-03 23:26:22 +02:00
|
|
|
let value = match value {
|
|
|
|
None => return Ok(None),
|
|
|
|
Some(value) => value,
|
|
|
|
};
|
|
|
|
if value <= usize::MAX as u64 {
|
|
|
|
Ok(Some(value as usize))
|
|
|
|
} else {
|
|
|
|
Err(From::from(format!("number too large for {}", arg_name)))
|
2017-04-01 23:55:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 06:36:15 +02:00
|
|
|
/// Sorts by an optional parameter.
|
|
|
|
//
|
|
|
|
/// If parameter is found to be `None`, both entries compare equal.
|
|
|
|
fn sort_by_option<T: Ord>(
|
|
|
|
p1: &Option<T>,
|
|
|
|
p2: &Option<T>,
|
2018-08-26 04:53:27 +02:00
|
|
|
reverse: bool,
|
2022-11-28 06:36:15 +02:00
|
|
|
) -> cmp::Ordering {
|
|
|
|
match (p1, p2, reverse) {
|
|
|
|
(Some(p1), Some(p2), true) => p1.cmp(&p2).reverse(),
|
|
|
|
(Some(p1), Some(p2), false) => p1.cmp(&p2),
|
|
|
|
_ => cmp::Ordering::Equal,
|
2018-08-26 04:53:27 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-26 21:36:34 +02:00
|
|
|
|
|
|
|
/// Returns a clap matches object if the given arguments parse successfully.
|
|
|
|
///
|
|
|
|
/// Otherwise, if an error occurred, then it is returned unless the error
|
|
|
|
/// corresponds to a `--help` or `--version` request. In which case, the
|
|
|
|
/// corresponding output is printed and the current process is exited
|
|
|
|
/// successfully.
|
2020-02-18 01:08:47 +02:00
|
|
|
fn clap_matches<I, T>(args: I) -> Result<clap::ArgMatches<'static>>
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = T>,
|
|
|
|
T: Into<OsString> + Clone,
|
2019-01-26 21:36:34 +02:00
|
|
|
{
|
|
|
|
let err = match app::app().get_matches_from_safe(args) {
|
|
|
|
Ok(matches) => return Ok(matches),
|
|
|
|
Err(err) => err,
|
|
|
|
};
|
|
|
|
if err.use_stderr() {
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
2019-04-06 13:59:36 +02:00
|
|
|
// Explicitly ignore any error returned by write!. The most likely error
|
2019-01-26 21:36:34 +02:00
|
|
|
// at this point is a broken pipe error, in which case, we want to ignore
|
|
|
|
// it and exit quietly.
|
|
|
|
//
|
|
|
|
// (This is the point of this helper function. clap's functionality for
|
|
|
|
// doing this will panic on a broken pipe error.)
|
2019-04-06 13:59:36 +02:00
|
|
|
let _ = write!(io::stdout(), "{}", err);
|
2019-01-26 21:36:34 +02:00
|
|
|
process::exit(0);
|
|
|
|
}
|
2020-02-17 04:03:07 +02:00
|
|
|
|
|
|
|
/// Attempts to discover the current working directory. This mostly just defers
|
|
|
|
/// to the standard library, however, such things will fail if ripgrep is in
|
|
|
|
/// a directory that no longer exists. We attempt some fallback mechanisms,
|
|
|
|
/// such as querying the PWD environment variable, but otherwise return an
|
|
|
|
/// error.
|
|
|
|
fn current_dir() -> Result<PathBuf> {
|
|
|
|
let err = match env::current_dir() {
|
|
|
|
Err(err) => err,
|
|
|
|
Ok(cwd) => return Ok(cwd),
|
|
|
|
};
|
|
|
|
if let Some(cwd) = env::var_os("PWD") {
|
|
|
|
if !cwd.is_empty() {
|
|
|
|
return Ok(PathBuf::from(cwd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(format!(
|
|
|
|
"failed to get current working directory: {} \
|
|
|
|
--- did your CWD get deleted?",
|
|
|
|
err,
|
2020-02-18 01:08:47 +02:00
|
|
|
)
|
|
|
|
.into())
|
2020-02-17 04:03:07 +02:00
|
|
|
}
|
2022-11-28 06:36:15 +02:00
|
|
|
|
|
|
|
/// Tries to assign a timestamp to every `Subject` in the vector to help with
|
|
|
|
/// sorting Subjects by time.
|
|
|
|
fn load_timestamps<G>(
|
|
|
|
subjects: impl Iterator<Item = Subject>,
|
|
|
|
get_time: G,
|
|
|
|
) -> Vec<(Option<SystemTime>, Subject)>
|
|
|
|
where
|
|
|
|
G: Fn(&fs::Metadata) -> io::Result<SystemTime>,
|
|
|
|
{
|
|
|
|
subjects
|
|
|
|
.map(|s| (s.path().metadata().and_then(|m| get_time(&m)).ok(), s))
|
|
|
|
.collect()
|
|
|
|
}
|