mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-05-19 01:30:21 -07:00
core: polish the core of ripgrep
This I believe finishes are quest to do mechanical updates to ripgrep's style, bringing it in line with my current practice (loosely speaking).
This commit is contained in:
parent
90b849912f
commit
392bb0944a
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -53,7 +53,7 @@ jobs:
|
||||
include:
|
||||
- build: pinned
|
||||
os: ubuntu-latest
|
||||
rust: 1.70.0
|
||||
rust: 1.72.1
|
||||
- build: stable
|
||||
os: ubuntu-latest
|
||||
rust: stable
|
||||
|
@ -23,8 +23,8 @@ exclude = [
|
||||
]
|
||||
build = "build.rs"
|
||||
autotests = false
|
||||
edition = "2018"
|
||||
rust-version = "1.70"
|
||||
edition = "2021"
|
||||
rust-version = "1.72"
|
||||
|
||||
[[bin]]
|
||||
bench = false
|
||||
|
@ -1,49 +1,52 @@
|
||||
use std::cmp;
|
||||
use std::env;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs;
|
||||
use std::io::{self, IsTerminal, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::time::SystemTime;
|
||||
use std::{
|
||||
env,
|
||||
ffi::{OsStr, OsString},
|
||||
io::{self, IsTerminal, Write},
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use {
|
||||
clap,
|
||||
grep::{
|
||||
cli,
|
||||
matcher::LineTerminator,
|
||||
printer::{
|
||||
default_color_specs, ColorSpecs, HyperlinkConfig,
|
||||
HyperlinkEnvironment, HyperlinkFormat, JSONBuilder, PathPrinter,
|
||||
PathPrinterBuilder, Standard, StandardBuilder, Stats, Summary,
|
||||
SummaryBuilder, SummaryKind, JSON,
|
||||
},
|
||||
regex::{
|
||||
RegexMatcher as RustRegexMatcher,
|
||||
RegexMatcherBuilder as RustRegexMatcherBuilder,
|
||||
},
|
||||
searcher::{
|
||||
BinaryDetection, Encoding, MmapChoice, Searcher, SearcherBuilder,
|
||||
},
|
||||
},
|
||||
ignore::{
|
||||
overrides::{Override, OverrideBuilder},
|
||||
types::{FileTypeDef, Types, TypesBuilder},
|
||||
{Walk, WalkBuilder, WalkParallel},
|
||||
},
|
||||
termcolor::{BufferWriter, ColorChoice, WriteColor},
|
||||
};
|
||||
|
||||
use clap;
|
||||
use grep::cli;
|
||||
use grep::matcher::LineTerminator;
|
||||
#[cfg(feature = "pcre2")]
|
||||
use grep::pcre2::{
|
||||
RegexMatcher as PCRE2RegexMatcher,
|
||||
RegexMatcherBuilder as PCRE2RegexMatcherBuilder,
|
||||
};
|
||||
use grep::printer::{
|
||||
default_color_specs, ColorSpecs, HyperlinkConfig, HyperlinkEnvironment,
|
||||
HyperlinkFormat, JSONBuilder, PathPrinter, PathPrinterBuilder, Standard,
|
||||
StandardBuilder, Stats, Summary, SummaryBuilder, SummaryKind, JSON,
|
||||
};
|
||||
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};
|
||||
use log;
|
||||
use termcolor::{BufferWriter, ColorChoice, WriteColor};
|
||||
|
||||
use crate::app;
|
||||
use crate::config;
|
||||
use crate::logger::Logger;
|
||||
use crate::messages::{set_ignore_messages, set_messages};
|
||||
use crate::search::{
|
||||
PatternMatcher, Printer, SearchWorker, SearchWorkerBuilder,
|
||||
use crate::{
|
||||
app, config,
|
||||
logger::Logger,
|
||||
messages::{set_ignore_messages, set_messages},
|
||||
search::{PatternMatcher, Printer, SearchWorker, SearchWorkerBuilder},
|
||||
subject::{Subject, SubjectBuilder},
|
||||
Result,
|
||||
};
|
||||
use crate::subject::{Subject, SubjectBuilder};
|
||||
use crate::Result;
|
||||
|
||||
/// The command that ripgrep should execute based on the command line
|
||||
/// configuration.
|
||||
@ -1130,9 +1133,10 @@ impl ArgMatches {
|
||||
let mut env = HyperlinkEnvironment::new();
|
||||
env.host(hostname(self.value_of_os("hostname-bin")))
|
||||
.wsl_prefix(wsl_prefix());
|
||||
let fmt = match self.value_of_lossy("hyperlink-format") {
|
||||
None => HyperlinkFormat::from_str("default").unwrap(),
|
||||
Some(format) => match HyperlinkFormat::from_str(&format) {
|
||||
let fmt: HyperlinkFormat =
|
||||
match self.value_of_lossy("hyperlink-format") {
|
||||
None => "default".parse().unwrap(),
|
||||
Some(format) => match format.parse() {
|
||||
Ok(format) => format,
|
||||
Err(err) => {
|
||||
let msg = format!("invalid hyperlink format: {err}");
|
||||
@ -1589,7 +1593,7 @@ impl ArgMatches {
|
||||
let threads = self.usize_of("threads")?.unwrap_or(0);
|
||||
let available =
|
||||
std::thread::available_parallelism().map_or(1, |n| n.get());
|
||||
Ok(if threads == 0 { cmp::min(12, available) } else { threads })
|
||||
Ok(if threads == 0 { std::cmp::min(12, available) } else { threads })
|
||||
}
|
||||
|
||||
/// Builds a file type matcher from the command line flags.
|
||||
@ -1790,11 +1794,11 @@ fn sort_by_option<T: Ord>(
|
||||
p1: &Option<T>,
|
||||
p2: &Option<T>,
|
||||
reverse: bool,
|
||||
) -> cmp::Ordering {
|
||||
) -> std::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,
|
||||
_ => std::cmp::Ordering::Equal,
|
||||
}
|
||||
}
|
||||
|
||||
@ -1823,7 +1827,7 @@ where
|
||||
// (This is the point of this helper function. clap's functionality for
|
||||
// doing this will panic on a broken pipe error.)
|
||||
let _ = write!(io::stdout(), "{}", err);
|
||||
process::exit(0);
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
/// Attempts to discover the current working directory. This mostly just defers
|
||||
@ -1871,8 +1875,8 @@ fn hostname(bin: Option<&OsStr>) -> Option<String> {
|
||||
return platform_hostname();
|
||||
}
|
||||
};
|
||||
let mut cmd = process::Command::new(&bin);
|
||||
cmd.stdin(process::Stdio::null());
|
||||
let mut cmd = std::process::Command::new(&bin);
|
||||
cmd.stdin(std::process::Stdio::null());
|
||||
let rdr = match grep::cli::CommandReader::new(&mut cmd) {
|
||||
Ok(rdr) => rdr,
|
||||
Err(err) => {
|
||||
@ -1955,9 +1959,9 @@ fn wsl_prefix() -> Option<String> {
|
||||
fn load_timestamps<G>(
|
||||
subjects: impl Iterator<Item = Subject>,
|
||||
get_time: G,
|
||||
) -> Vec<(Option<SystemTime>, Subject)>
|
||||
) -> Vec<(Option<std::time::SystemTime>, Subject)>
|
||||
where
|
||||
G: Fn(&fs::Metadata) -> io::Result<SystemTime>,
|
||||
G: Fn(&std::fs::Metadata) -> io::Result<std::time::SystemTime>,
|
||||
{
|
||||
subjects
|
||||
.map(|s| (s.path().metadata().and_then(|m| get_time(&m)).ok(), s))
|
||||
|
@ -2,21 +2,16 @@
|
||||
// primary output of these routines is a sequence of arguments, where each
|
||||
// argument corresponds precisely to one shell argument.
|
||||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::ffi::OsString;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{
|
||||
ffi::OsString,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use bstr::{io::BufReadExt, ByteSlice};
|
||||
use log;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
/// Return a sequence of arguments derived from ripgrep rc configuration files.
|
||||
pub fn args() -> Vec<OsString> {
|
||||
let config_path = match env::var_os("RIPGREP_CONFIG_PATH") {
|
||||
let config_path = match std::env::var_os("RIPGREP_CONFIG_PATH") {
|
||||
None => return vec![],
|
||||
Some(config_path) => {
|
||||
if config_path.is_empty() {
|
||||
@ -58,9 +53,9 @@ pub fn args() -> Vec<OsString> {
|
||||
/// for each line in addition to successfully parsed arguments.
|
||||
fn parse<P: AsRef<Path>>(
|
||||
path: P,
|
||||
) -> Result<(Vec<OsString>, Vec<Box<dyn Error>>)> {
|
||||
) -> crate::Result<(Vec<OsString>, Vec<Box<dyn std::error::Error>>)> {
|
||||
let path = path.as_ref();
|
||||
match File::open(&path) {
|
||||
match std::fs::File::open(&path) {
|
||||
Ok(file) => parse_reader(file),
|
||||
Err(err) => Err(From::from(format!("{}: {}", path.display(), err))),
|
||||
}
|
||||
@ -77,10 +72,10 @@ fn parse<P: AsRef<Path>>(
|
||||
/// If the reader could not be read, then an error is returned. If there was a
|
||||
/// problem parsing one or more lines, then errors are returned for each line
|
||||
/// in addition to successfully parsed arguments.
|
||||
fn parse_reader<R: io::Read>(
|
||||
fn parse_reader<R: std::io::Read>(
|
||||
rdr: R,
|
||||
) -> Result<(Vec<OsString>, Vec<Box<dyn Error>>)> {
|
||||
let mut bufrdr = io::BufReader::new(rdr);
|
||||
) -> crate::Result<(Vec<OsString>, Vec<Box<dyn std::error::Error>>)> {
|
||||
let mut bufrdr = std::io::BufReader::new(rdr);
|
||||
let (mut args, mut errs) = (vec![], vec![]);
|
||||
let mut line_number = 0;
|
||||
bufrdr.for_byte_line_with_terminator(|line| {
|
||||
|
@ -1,13 +1,11 @@
|
||||
use std::error;
|
||||
use std::io::{self, Write};
|
||||
use std::process;
|
||||
use std::sync::Mutex;
|
||||
use std::time::Instant;
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
use ignore::WalkState;
|
||||
|
||||
use args::Args;
|
||||
use subject::Subject;
|
||||
use crate::{args::Args, subject::Subject};
|
||||
|
||||
#[macro_use]
|
||||
mod messages;
|
||||
@ -42,12 +40,12 @@ mod subject;
|
||||
#[global_allocator]
|
||||
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
||||
|
||||
type Result<T> = ::std::result::Result<T, Box<dyn error::Error>>;
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
fn main() {
|
||||
if let Err(err) = Args::parse().and_then(try_main) {
|
||||
eprintln_locked!("{}", err);
|
||||
process::exit(2);
|
||||
std::process::exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,11 +62,11 @@ fn try_main(args: Args) -> Result<()> {
|
||||
PCRE2Version => pcre2_version(&args),
|
||||
}?;
|
||||
if matched && (args.quiet() || !messages::errored()) {
|
||||
process::exit(0)
|
||||
std::process::exit(0)
|
||||
} else if messages::errored() {
|
||||
process::exit(2)
|
||||
std::process::exit(2)
|
||||
} else {
|
||||
process::exit(1)
|
||||
std::process::exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,7 +146,7 @@ fn search_parallel(args: &Args) -> Result<bool> {
|
||||
let started_at = Instant::now();
|
||||
let subject_builder = args.subject_builder();
|
||||
let bufwtr = args.buffer_writer()?;
|
||||
let stats = args.stats()?.map(Mutex::new);
|
||||
let stats = args.stats()?.map(std::sync::Mutex::new);
|
||||
let matched = AtomicBool::new(false);
|
||||
let searched = AtomicBool::new(false);
|
||||
let mut searcher_err = None;
|
||||
|
@ -1,20 +1,24 @@
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::time::Duration;
|
||||
use std::{
|
||||
io,
|
||||
path::{Path, PathBuf},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use {
|
||||
grep::{
|
||||
cli,
|
||||
matcher::Matcher,
|
||||
printer::{Standard, Stats, Summary, JSON},
|
||||
regex::RegexMatcher as RustRegexMatcher,
|
||||
searcher::{BinaryDetection, Searcher},
|
||||
},
|
||||
ignore::overrides::Override,
|
||||
serde_json::{self as json, json},
|
||||
termcolor::WriteColor,
|
||||
};
|
||||
|
||||
use grep::cli;
|
||||
use grep::matcher::Matcher;
|
||||
#[cfg(feature = "pcre2")]
|
||||
use grep::pcre2::RegexMatcher as PCRE2RegexMatcher;
|
||||
use grep::printer::{Standard, Stats, Summary, JSON};
|
||||
use grep::regex::RegexMatcher as RustRegexMatcher;
|
||||
use grep::searcher::{BinaryDetection, Searcher};
|
||||
use ignore::overrides::Override;
|
||||
use serde_json as json;
|
||||
use serde_json::json;
|
||||
use termcolor::WriteColor;
|
||||
|
||||
use crate::subject::Subject;
|
||||
|
||||
@ -396,8 +400,9 @@ impl<W: WriteColor> SearchWorker<W> {
|
||||
path: &Path,
|
||||
) -> io::Result<SearchResult> {
|
||||
let bin = self.config.preprocessor.as_ref().unwrap();
|
||||
let mut cmd = Command::new(bin);
|
||||
cmd.arg(path).stdin(Stdio::from(File::open(path)?));
|
||||
let mut cmd = std::process::Command::new(bin);
|
||||
cmd.arg(path)
|
||||
.stdin(std::process::Stdio::from(std::fs::File::open(path)?));
|
||||
|
||||
let mut rdr = self.command_builder.build(&mut cmd).map_err(|err| {
|
||||
io::Error::new(
|
||||
|
@ -1,8 +1,5 @@
|
||||
use std::path::Path;
|
||||
|
||||
use ignore::{self, DirEntry};
|
||||
use log;
|
||||
|
||||
/// A configuration for describing how subjects should be built.
|
||||
#[derive(Clone, Debug)]
|
||||
struct Config {
|
||||
@ -34,7 +31,7 @@ impl SubjectBuilder {
|
||||
/// deemed searchable, then it is returned.
|
||||
pub fn build_from_result(
|
||||
&self,
|
||||
result: Result<DirEntry, ignore::Error>,
|
||||
result: Result<ignore::DirEntry, ignore::Error>,
|
||||
) -> Option<Subject> {
|
||||
match result {
|
||||
Ok(dent) => self.build(dent),
|
||||
@ -49,7 +46,7 @@ impl SubjectBuilder {
|
||||
///
|
||||
/// If a subject could not be created or should otherwise not be searched,
|
||||
/// then this returns `None` after emitting any relevant log messages.
|
||||
pub fn build(&self, dent: DirEntry) -> Option<Subject> {
|
||||
pub fn build(&self, dent: ignore::DirEntry) -> Option<Subject> {
|
||||
let subj =
|
||||
Subject { dent, strip_dot_prefix: self.config.strip_dot_prefix };
|
||||
if let Some(ignore_err) = subj.dent.error() {
|
||||
@ -96,7 +93,7 @@ impl SubjectBuilder {
|
||||
/// file or stdin.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Subject {
|
||||
dent: DirEntry,
|
||||
dent: ignore::DirEntry,
|
||||
strip_dot_prefix: bool,
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user