mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-07-28 10:41:58 -07:00
Compare commits
57 Commits
ag/snafu
...
ignore-0.4
Author | SHA1 | Date | |
---|---|---|---|
|
00bfcd14a6 | ||
|
bf0ddc4675 | ||
|
0fb3f6a159 | ||
|
837fb5e21f | ||
|
2e1815606e | ||
|
cb2f6ddc61 | ||
|
bd7a42602f | ||
|
528ce56e1b | ||
|
8892bf648c | ||
|
8cb7271b64 | ||
|
4858267f3b | ||
|
5011dba2fd | ||
|
e14f9195e5 | ||
|
ef0e7af56a | ||
|
b266818aa5 | ||
|
81415ae52d | ||
|
5c4584aa7c | ||
|
0972c6e7c7 | ||
|
0a372bf2e4 | ||
|
345124a7fa | ||
|
31807f805a | ||
|
4de227fd9a | ||
|
d7ce274722 | ||
|
5b10328f41 | ||
|
813c676eca | ||
|
f625d72b6f | ||
|
3de31f7527 | ||
|
e402d6c260 | ||
|
48b5bdc441 | ||
|
709ca91f50 | ||
|
9c220f9a9b | ||
|
9085bed139 | ||
|
931ab35f76 | ||
|
b5e5979ff1 | ||
|
052c857da0 | ||
|
5e84e784c8 | ||
|
01e8e11621 | ||
|
9268ff8e8d | ||
|
c2cb0a4de4 | ||
|
adb9332f52 | ||
|
bc37c32717 | ||
|
08ae4da2b7 | ||
|
7ac95c1f50 | ||
|
7a6903bd4e | ||
|
9801fae29f | ||
|
abdf7140d7 | ||
|
b83e7968ef | ||
|
8ebc113847 | ||
|
785c1f1766 | ||
|
8b734cb490 | ||
|
b93762ea7a | ||
|
34677d2622 | ||
|
d1389db2e3 | ||
|
50bcb7409e | ||
|
7b9972c308 | ||
|
9f000c2910 | ||
|
392682d352 |
108
.github/workflows/ci.yml
vendored
Normal file
108
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
name: ci
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
schedule:
|
||||
- cron: '00 01 * * *'
|
||||
jobs:
|
||||
test:
|
||||
name: test
|
||||
runs-on: ubuntu-18.04
|
||||
steps:
|
||||
- name: no-op
|
||||
run: echo hello
|
||||
|
||||
# test:
|
||||
# name: test
|
||||
# runs-on: ${{ matrix.os }}
|
||||
# strategy:
|
||||
# matrix:
|
||||
# # The docs seem to suggest that we can have a matrix with just an
|
||||
# # include directive, but it result in a "matrix must define at least
|
||||
# # one vector" error in the CI system.
|
||||
# build:
|
||||
# # - pinned-glibc
|
||||
# - pinned-musl
|
||||
# - stable
|
||||
# # - beta
|
||||
# # We test musl with nightly because every once in a while, this will
|
||||
# # catch an upstream regression.
|
||||
# # - nightly-glibc
|
||||
# # - nightly-musl
|
||||
# # - macos
|
||||
# # - win-msvc-32
|
||||
# # - win-msvc-64
|
||||
# # - win-gnu-32
|
||||
# # - win-gnu-64
|
||||
# include:
|
||||
# # - build: pinned-glibc
|
||||
# # os: ubuntu-18.04
|
||||
# # rust: 1.34.0
|
||||
# # target: x86_64-unknown-linux-gnu
|
||||
# - build: pinned-musl
|
||||
# os: ubuntu-18.04
|
||||
# rust: 1.34.0
|
||||
# target: x86_64-unknown-linux-musl
|
||||
# - build: stable
|
||||
# os: ubuntu-18.04
|
||||
# rust: stable
|
||||
# target: x86_64-unknown-linux-gnu
|
||||
# # - build: beta
|
||||
# # os: ubuntu-18.04
|
||||
# # rust: beta
|
||||
# # target: x86_64-unknown-linux-gnu
|
||||
# # - build: nightly-glibc
|
||||
# # os: ubuntu-18.04
|
||||
# # rust: nightly
|
||||
# # target: x86_64-unknown-linux-gnu
|
||||
# # - build: nightly-musl
|
||||
# # os: ubuntu-18.04
|
||||
# # rust: nightly
|
||||
# # target: x86_64-unknown-linux-musl
|
||||
# # - build: macos
|
||||
# # os: macOS-10.14
|
||||
# # rust: stable
|
||||
# # target: x86_64-apple-darwin
|
||||
# # - build: win-msvc-32
|
||||
# # os: windows-2019
|
||||
# # rust: stable
|
||||
# # target: i686-pc-windows-msvc
|
||||
# # - build: win-msvc-64
|
||||
# # os: windows-2019
|
||||
# # rust: stable
|
||||
# # target: x86_64-pc-windows-msvc
|
||||
# # - build: win-gnu-32
|
||||
# # os: windows-2019
|
||||
# # rust: stable-i686-gnu
|
||||
# # target: i686-pc-windows-gnu
|
||||
# # - build: win-gnu-64
|
||||
# # os: windows-2019
|
||||
# # rust: stable-x86_64-gnu
|
||||
# # target: x86_64-pc-windows-gnu
|
||||
# steps:
|
||||
# - name: Checkout repository
|
||||
# uses: actions/checkout@v1
|
||||
# with:
|
||||
# fetch-depth: 1
|
||||
# - name: Install Rust
|
||||
# uses: hecrj/setup-rust-action@v1
|
||||
# with:
|
||||
# rust-version: ${{ matrix.rust }}
|
||||
# - name: Install Rust Target
|
||||
# run: rustup target add ${{ matrix.target }}
|
||||
# - name: Install musl-gcc
|
||||
# if: contains(matrix.target, 'musl')
|
||||
# run: |
|
||||
# sudo apt-get install musl-tools
|
||||
# - name: Build everything
|
||||
# run: cargo build --verbose --target ${{ matrix.target }} --all --features pcre2
|
||||
# - name: Install zsh
|
||||
# if: matrix.build == 'stable'
|
||||
# run: sudo apt-get install zsh
|
||||
# - name: Test zsh auto-completions
|
||||
# if: matrix.build == 'stable'
|
||||
# run: ./ci/test_complete.sh
|
||||
# - name: Run tests
|
||||
# run: cargo test --verbose --target ${{ matrix.target }} --all --features pcre2
|
33
CHANGELOG.md
33
CHANGELOG.md
@@ -1,11 +1,42 @@
|
||||
TBD
|
||||
===
|
||||
TODO.
|
||||
TODO
|
||||
|
||||
Bug fixes:
|
||||
|
||||
* [BUG #1335](https://github.com/BurntSushi/ripgrep/issues/1335):
|
||||
Fixes a performance bug when searching plain text files with very long lines.
|
||||
|
||||
|
||||
11.0.2 (2019-08-01)
|
||||
===================
|
||||
ripgrep 11.0.2 is a new patch release that fixes a few bugs, including a
|
||||
performance regression and a matching bug when using the `-F/--fixed-strings`
|
||||
flag.
|
||||
|
||||
Feature enhancements:
|
||||
|
||||
* [FEATURE #1293](https://github.com/BurntSushi/ripgrep/issues/1293):
|
||||
Added `--glob-case-insensitive` flag that makes `--glob` behave as `--iglob`.
|
||||
|
||||
Bug fixes:
|
||||
|
||||
* [BUG #1246](https://github.com/BurntSushi/ripgrep/issues/1246):
|
||||
Add translations to README, starting with an unofficial Chinese translation.
|
||||
* [BUG #1259](https://github.com/BurntSushi/ripgrep/issues/1259):
|
||||
Fix bug where the last byte of a `-f file` was stripped if it wasn't a `\n`.
|
||||
* [BUG #1261](https://github.com/BurntSushi/ripgrep/issues/1261):
|
||||
Document that no error is reported when searching for `\n` with `-P/--pcre2`.
|
||||
* [BUG #1284](https://github.com/BurntSushi/ripgrep/issues/1284):
|
||||
Mention `.ignore` and `.rgignore` more prominently in the README.
|
||||
* [BUG #1292](https://github.com/BurntSushi/ripgrep/issues/1292):
|
||||
Fix bug where `--with-filename` was sometimes enabled incorrectly.
|
||||
* [BUG #1268](https://github.com/BurntSushi/ripgrep/issues/1268):
|
||||
Fix major performance regression in GitHub `x86_64-linux` binary release.
|
||||
* [BUG #1302](https://github.com/BurntSushi/ripgrep/issues/1302):
|
||||
Show better error messages when a non-existent preprocessor command is given.
|
||||
* [BUG #1334](https://github.com/BurntSushi/ripgrep/issues/1334):
|
||||
Fix match regression with `-F` flag when patterns contain meta characters.
|
||||
|
||||
|
||||
11.0.1 (2019-04-16)
|
||||
|
690
Cargo.lock
generated
690
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,11 +1,11 @@
|
||||
[package]
|
||||
name = "ripgrep"
|
||||
version = "11.0.1" #:version
|
||||
version = "11.0.2" #:version
|
||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
||||
description = """
|
||||
ripgrep is a line-oriented search tool that recursively searches your current
|
||||
directory for a regex pattern while respecting your gitignore rules. ripgrep
|
||||
has first class support on Windows, macOS and Linux
|
||||
has first class support on Windows, macOS and Linux.
|
||||
"""
|
||||
documentation = "https://github.com/BurntSushi/ripgrep"
|
||||
homepage = "https://github.com/BurntSushi/ripgrep"
|
||||
@@ -46,15 +46,14 @@ members = [
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
bstr = "0.1.2"
|
||||
bstr = "0.2.0"
|
||||
grep = { version = "0.2.4", path = "grep" }
|
||||
ignore = { version = "0.4.7", path = "ignore" }
|
||||
lazy_static = "1.1.0"
|
||||
log = { version = "0.4.5", features = ["std"] }
|
||||
log = "0.4.5"
|
||||
num_cpus = "1.8.0"
|
||||
regex = "1.0.5"
|
||||
serde_json = "1.0.23"
|
||||
snafu = "0.2.3"
|
||||
termcolor = "1.0.3"
|
||||
|
||||
[dependencies.clap]
|
||||
|
2
Cross.toml
Normal file
2
Cross.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[target.x86_64-unknown-linux-musl]
|
||||
image = "burntsushi/cross:x86_64-unknown-linux-musl"
|
2
FAQ.md
2
FAQ.md
@@ -934,7 +934,7 @@ Here are some cases where you might *not* want to use ripgrep. The same caveats
|
||||
for the previous section apply.
|
||||
|
||||
* Are you writing portable shell scripts intended to work in a variety of
|
||||
environments? Great, probably not a good idea to use ripgrep! ripgrep is has
|
||||
environments? Great, probably not a good idea to use ripgrep! ripgrep has
|
||||
nowhere near the ubiquity of grep, so if you do use ripgrep, you might need
|
||||
to futz with the installation process more than you would with grep.
|
||||
* Do you care about POSIX compatibility? If so, then you can't use ripgrep
|
||||
|
2
GUIDE.md
2
GUIDE.md
@@ -110,7 +110,7 @@ colors, you'll notice that `faster` will be highlighted instead of just the
|
||||
|
||||
It is beyond the scope of this guide to provide a full tutorial on regular
|
||||
expressions, but ripgrep's specific syntax is documented here:
|
||||
https://docs.rs/regex/0.2.5/regex/#syntax
|
||||
https://docs.rs/regex/*/regex/#syntax
|
||||
|
||||
|
||||
### Recursive search
|
||||
|
27
README.md
27
README.md
@@ -29,6 +29,7 @@ Please see the [CHANGELOG](CHANGELOG.md) for a release history.
|
||||
* [Configuration files](GUIDE.md#configuration-file)
|
||||
* [Shell completions](FAQ.md#complete)
|
||||
* [Building](#building)
|
||||
* [Translations](#translations)
|
||||
|
||||
|
||||
### Screenshot of search results
|
||||
@@ -92,11 +93,11 @@ increases the times to `2.640s` for ripgrep and `10.277s` for GNU grep.
|
||||
[the FAQ](FAQ.md#posix4ever) for more details on whether ripgrep can truly
|
||||
replace grep.)
|
||||
* Like other tools specialized to code search, ripgrep defaults to recursive
|
||||
directory search and won't search files ignored by your `.gitignore` files.
|
||||
It also ignores hidden and binary files by default. ripgrep also implements
|
||||
full support for `.gitignore`, whereas there are many bugs related to that
|
||||
functionality in other code search tools claiming to provide the same
|
||||
functionality.
|
||||
directory search and won't search files ignored by your
|
||||
`.gitignore`/`.ignore`/`.rgignore` files. It also ignores hidden and binary
|
||||
files by default. ripgrep also implements full support for `.gitignore`,
|
||||
whereas there are many bugs related to that functionality in other code
|
||||
search tools claiming to provide the same functionality.
|
||||
* ripgrep can search specific types of files. For example, `rg -tpy foo`
|
||||
limits your search to Python files and `rg -Tjs foo` excludes Javascript
|
||||
files from your search. ripgrep can be taught about new file types with
|
||||
@@ -224,7 +225,7 @@ $ choco install ripgrep
|
||||
```
|
||||
|
||||
If you're a **Windows Scoop** user, then you can install ripgrep from the
|
||||
[official bucket](https://github.com/lukesampson/scoop/blob/master/bucket/ripgrep.json):
|
||||
[official bucket](https://github.com/ScoopInstaller/Main/blob/master/bucket/ripgrep.json):
|
||||
|
||||
```
|
||||
$ scoop install ripgrep
|
||||
@@ -287,11 +288,11 @@ then ripgrep can be installed using a binary `.deb` file provided in each
|
||||
[ripgrep release](https://github.com/BurntSushi/ripgrep/releases).
|
||||
|
||||
```
|
||||
$ curl -LO https://github.com/BurntSushi/ripgrep/releases/download/11.0.1/ripgrep_11.0.1_amd64.deb
|
||||
$ sudo dpkg -i ripgrep_11.0.1_amd64.deb
|
||||
$ curl -LO https://github.com/BurntSushi/ripgrep/releases/download/11.0.2/ripgrep_11.0.2_amd64.deb
|
||||
$ sudo dpkg -i ripgrep_11.0.2_amd64.deb
|
||||
```
|
||||
|
||||
If you run Debian Buster (currently Debian testing) or Debian sid, ripgrep is
|
||||
If you run Debian Buster (currently Debian stable) or Debian sid, ripgrep is
|
||||
[officially maintained by Debian](https://tracker.debian.org/pkg/rust-ripgrep).
|
||||
```
|
||||
$ sudo apt-get install ripgrep
|
||||
@@ -418,3 +419,11 @@ $ cargo test --all
|
||||
```
|
||||
|
||||
from the repository root.
|
||||
|
||||
|
||||
### Translations
|
||||
|
||||
The following is a list of known translations of ripgrep's documentation. These
|
||||
are unofficially maintained and may not be up to date.
|
||||
|
||||
* [Chinese](https://github.com/chinanf-boy/ripgrep-zh#%E6%9B%B4%E6%96%B0-)
|
||||
|
5
ci/docker/x86_64-unknown-linux-musl/Dockerfile
Normal file
5
ci/docker/x86_64-unknown-linux-musl/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM rustembedded/cross:x86_64-unknown-linux-musl
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
libxslt1-dev asciidoc docbook-xsl xsltproc libxml2-utils
|
@@ -99,7 +99,9 @@ is_osx() {
|
||||
|
||||
builder() {
|
||||
if is_musl && is_x86_64; then
|
||||
cargo install cross
|
||||
# cargo install cross
|
||||
# To work around https://github.com/rust-embedded/cross/issues/357
|
||||
cargo install --git https://github.com/rust-embedded/cross --force
|
||||
echo "cross"
|
||||
else
|
||||
echo "cargo"
|
||||
|
@@ -104,6 +104,10 @@ _rg() {
|
||||
'*'{-g+,--glob=}'[include/exclude files matching specified glob]:glob'
|
||||
'*--iglob=[include/exclude files matching specified case-insensitive glob]:glob'
|
||||
|
||||
+ '(glob-case-insensitive)' # File-glob case sensitivity options
|
||||
'--glob-case-insensitive[treat -g/--glob patterns case insensitively]'
|
||||
$no'--no-glob-case-insensitive[treat -g/--glob patterns case sensitively]'
|
||||
|
||||
+ '(heading)' # Heading options
|
||||
'(pretty-vimgrep)--heading[show matches grouped by file name]'
|
||||
"(pretty-vimgrep)--no-heading[don't show matches grouped by file name]"
|
||||
|
@@ -143,16 +143,16 @@ would behave identically to the following command
|
||||
|
||||
same with using globs
|
||||
|
||||
--glob=!git/*
|
||||
--glob=!.git
|
||||
|
||||
or
|
||||
|
||||
--glob
|
||||
!git/*
|
||||
!.git
|
||||
|
||||
would behave identically to the following command
|
||||
|
||||
rg --glob '!git/*' foo
|
||||
rg --glob '!.git' foo
|
||||
|
||||
ripgrep also provides a flag, *--no-config*, that when present will suppress
|
||||
any and all support for configuration. This includes any future support
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "globset"
|
||||
version = "0.4.3" #:version
|
||||
version = "0.4.4" #:version
|
||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
||||
description = """
|
||||
Cross platform single glob and glob set matching. Glob set matching is the
|
||||
@@ -20,7 +20,7 @@ bench = false
|
||||
|
||||
[dependencies]
|
||||
aho-corasick = "0.7.3"
|
||||
bstr = { version = "0.1.2", default-features = false, features = ["std"] }
|
||||
bstr = { version = "0.2.0", default-features = false, features = ["std"] }
|
||||
fnv = "1.0.6"
|
||||
log = "0.4.5"
|
||||
regex = "1.1.5"
|
||||
|
@@ -120,7 +120,7 @@ impl GlobMatcher {
|
||||
|
||||
/// Tests whether the given path matches this pattern or not.
|
||||
pub fn is_match_candidate(&self, path: &Candidate) -> bool {
|
||||
self.re.is_match(path.path.as_bytes())
|
||||
self.re.is_match(&path.path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ impl GlobStrategic {
|
||||
|
||||
/// Tests whether the given path matches this pattern or not.
|
||||
fn is_match_candidate(&self, candidate: &Candidate) -> bool {
|
||||
let byte_path = candidate.path.as_bytes();
|
||||
let byte_path = &*candidate.path;
|
||||
|
||||
match self.strategy {
|
||||
MatchStrategy::Literal(ref lit) => lit.as_bytes() == byte_path,
|
||||
|
@@ -119,7 +119,7 @@ use std::path::Path;
|
||||
use std::str;
|
||||
|
||||
use aho_corasick::AhoCorasick;
|
||||
use bstr::{B, BStr, BString};
|
||||
use bstr::{B, ByteSlice, ByteVec};
|
||||
use regex::bytes::{Regex, RegexBuilder, RegexSet};
|
||||
|
||||
use pathutil::{file_name, file_name_ext, normalize_path};
|
||||
@@ -490,15 +490,15 @@ impl GlobSetBuilder {
|
||||
/// path against multiple globs or sets of globs.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Candidate<'a> {
|
||||
path: Cow<'a, BStr>,
|
||||
basename: Cow<'a, BStr>,
|
||||
ext: Cow<'a, BStr>,
|
||||
path: Cow<'a, [u8]>,
|
||||
basename: Cow<'a, [u8]>,
|
||||
ext: Cow<'a, [u8]>,
|
||||
}
|
||||
|
||||
impl<'a> Candidate<'a> {
|
||||
/// Create a new candidate for matching from the given path.
|
||||
pub fn new<P: AsRef<Path> + ?Sized>(path: &'a P) -> Candidate<'a> {
|
||||
let path = normalize_path(BString::from_path_lossy(path.as_ref()));
|
||||
let path = normalize_path(Vec::from_path_lossy(path.as_ref()));
|
||||
let basename = file_name(&path).unwrap_or(Cow::Borrowed(B("")));
|
||||
let ext = file_name_ext(&basename).unwrap_or(Cow::Borrowed(B("")));
|
||||
Candidate {
|
||||
@@ -508,7 +508,7 @@ impl<'a> Candidate<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn path_prefix(&self, max: usize) -> &BStr {
|
||||
fn path_prefix(&self, max: usize) -> &[u8] {
|
||||
if self.path.len() <= max {
|
||||
&*self.path
|
||||
} else {
|
||||
@@ -516,7 +516,7 @@ impl<'a> Candidate<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn path_suffix(&self, max: usize) -> &BStr {
|
||||
fn path_suffix(&self, max: usize) -> &[u8] {
|
||||
if self.path.len() <= max {
|
||||
&*self.path
|
||||
} else {
|
||||
|
@@ -1,15 +1,15 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use bstr::BStr;
|
||||
use bstr::{ByteSlice, ByteVec};
|
||||
|
||||
/// The final component of the path, if it is a normal file.
|
||||
///
|
||||
/// If the path terminates in ., .., or consists solely of a root of prefix,
|
||||
/// file_name will return None.
|
||||
pub fn file_name<'a>(path: &Cow<'a, BStr>) -> Option<Cow<'a, BStr>> {
|
||||
pub fn file_name<'a>(path: &Cow<'a, [u8]>) -> Option<Cow<'a, [u8]>> {
|
||||
if path.is_empty() {
|
||||
return None;
|
||||
} else if path.last() == Some(b'.') {
|
||||
} else if path.last_byte() == Some(b'.') {
|
||||
return None;
|
||||
}
|
||||
let last_slash = path.rfind_byte(b'/').map(|i| i + 1).unwrap_or(0);
|
||||
@@ -39,7 +39,7 @@ pub fn file_name<'a>(path: &Cow<'a, BStr>) -> Option<Cow<'a, BStr>> {
|
||||
/// a pattern like `*.rs` is obviously trying to match files with a `rs`
|
||||
/// extension, but it also matches files like `.rs`, which doesn't have an
|
||||
/// extension according to std::path::Path::extension.
|
||||
pub fn file_name_ext<'a>(name: &Cow<'a, BStr>) -> Option<Cow<'a, BStr>> {
|
||||
pub fn file_name_ext<'a>(name: &Cow<'a, [u8]>) -> Option<Cow<'a, [u8]>> {
|
||||
if name.is_empty() {
|
||||
return None;
|
||||
}
|
||||
@@ -60,7 +60,7 @@ pub fn file_name_ext<'a>(name: &Cow<'a, BStr>) -> Option<Cow<'a, BStr>> {
|
||||
/// Normalizes a path to use `/` as a separator everywhere, even on platforms
|
||||
/// that recognize other characters as separators.
|
||||
#[cfg(unix)]
|
||||
pub fn normalize_path(path: Cow<BStr>) -> Cow<BStr> {
|
||||
pub fn normalize_path(path: Cow<[u8]>) -> Cow<[u8]> {
|
||||
// UNIX only uses /, so we're good.
|
||||
path
|
||||
}
|
||||
@@ -68,7 +68,7 @@ pub fn normalize_path(path: Cow<BStr>) -> Cow<BStr> {
|
||||
/// Normalizes a path to use `/` as a separator everywhere, even on platforms
|
||||
/// that recognize other characters as separators.
|
||||
#[cfg(not(unix))]
|
||||
pub fn normalize_path(mut path: Cow<BStr>) -> Cow<BStr> {
|
||||
pub fn normalize_path(mut path: Cow<[u8]>) -> Cow<[u8]> {
|
||||
use std::path::is_separator;
|
||||
|
||||
for i in 0..path.len() {
|
||||
@@ -84,7 +84,7 @@ pub fn normalize_path(mut path: Cow<BStr>) -> Cow<BStr> {
|
||||
mod tests {
|
||||
use std::borrow::Cow;
|
||||
|
||||
use bstr::{B, BString};
|
||||
use bstr::{B, ByteVec};
|
||||
|
||||
use super::{file_name_ext, normalize_path};
|
||||
|
||||
@@ -92,7 +92,7 @@ mod tests {
|
||||
($name:ident, $file_name:expr, $ext:expr) => {
|
||||
#[test]
|
||||
fn $name() {
|
||||
let bs = BString::from($file_name);
|
||||
let bs = Vec::from($file_name);
|
||||
let got = file_name_ext(&Cow::Owned(bs));
|
||||
assert_eq!($ext.map(|s| Cow::Borrowed(B(s))), got);
|
||||
}
|
||||
@@ -109,7 +109,7 @@ mod tests {
|
||||
($name:ident, $path:expr, $expected:expr) => {
|
||||
#[test]
|
||||
fn $name() {
|
||||
let bs = BString::from_slice($path);
|
||||
let bs = Vec::from_slice($path);
|
||||
let got = normalize_path(Cow::Owned(bs));
|
||||
assert_eq!($expected.to_vec(), got.into_owned());
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "grep-cli"
|
||||
version = "0.1.2" #:version
|
||||
version = "0.1.3" #:version
|
||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
||||
description = """
|
||||
Utilities for search oriented command line applications.
|
||||
@@ -14,7 +14,7 @@ license = "Unlicense/MIT"
|
||||
|
||||
[dependencies]
|
||||
atty = "0.2.11"
|
||||
bstr = "0.1.2"
|
||||
bstr = "0.2.0"
|
||||
globset = { version = "0.4.3", path = "../globset" }
|
||||
lazy_static = "1.1.0"
|
||||
log = "0.4.5"
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use std::ffi::OsStr;
|
||||
use std::str;
|
||||
|
||||
use bstr::{BStr, BString};
|
||||
use bstr::{ByteSlice, ByteVec};
|
||||
|
||||
/// A single state in the state machine used by `unescape`.
|
||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||
@@ -38,7 +38,6 @@ enum State {
|
||||
/// assert_eq!(r"foo\nbar\xFFbaz", escape(b"foo\nbar\xFFbaz"));
|
||||
/// ```
|
||||
pub fn escape(bytes: &[u8]) -> String {
|
||||
let bytes = BStr::new(bytes);
|
||||
let mut escaped = String::new();
|
||||
for (s, e, ch) in bytes.char_indices() {
|
||||
if ch == '\u{FFFD}' {
|
||||
@@ -56,7 +55,7 @@ pub fn escape(bytes: &[u8]) -> String {
|
||||
///
|
||||
/// This is like [`escape`](fn.escape.html), but accepts an OS string.
|
||||
pub fn escape_os(string: &OsStr) -> String {
|
||||
escape(BString::from_os_str_lossy(string).as_bytes())
|
||||
escape(Vec::from_os_str_lossy(string).as_bytes())
|
||||
}
|
||||
|
||||
/// Unescapes a string.
|
||||
@@ -111,7 +110,7 @@ pub fn unescape(s: &str) -> Vec<u8> {
|
||||
}
|
||||
HexFirst => {
|
||||
match c {
|
||||
'0'...'9' | 'A'...'F' | 'a'...'f' => {
|
||||
'0'..='9' | 'A'..='F' | 'a'..='f' => {
|
||||
state = HexSecond(c);
|
||||
}
|
||||
c => {
|
||||
@@ -122,7 +121,7 @@ pub fn unescape(s: &str) -> Vec<u8> {
|
||||
}
|
||||
HexSecond(first) => {
|
||||
match c {
|
||||
'0'...'9' | 'A'...'F' | 'a'...'f' => {
|
||||
'0'..='9' | 'A'..='F' | 'a'..='f' => {
|
||||
let ordinal = format!("{}{}", first, c);
|
||||
let byte = u8::from_str_radix(&ordinal, 16).unwrap();
|
||||
bytes.push(byte);
|
||||
@@ -174,7 +173,7 @@ fn escape_char(cp: char, into: &mut String) {
|
||||
/// Adds the given byte to the given string, escaping it if necessary.
|
||||
fn escape_byte(byte: u8, into: &mut String) {
|
||||
match byte {
|
||||
0x21...0x5B | 0x5D...0x7D => into.push(byte as char),
|
||||
0x21..=0x5B | 0x5D..=0x7D => into.push(byte as char),
|
||||
b'\n' => into.push_str(r"\n"),
|
||||
b'\r' => into.push_str(r"\r"),
|
||||
b'\t' => into.push_str(r"\t"),
|
||||
|
@@ -161,7 +161,7 @@ pub fn patterns_from_reader<R: io::Read>(rdr: R) -> io::Result<Vec<String>> {
|
||||
let mut line_number = 0;
|
||||
io::BufReader::new(rdr).for_byte_line(|line| {
|
||||
line_number += 1;
|
||||
match pattern_from_bytes(line.as_bytes()) {
|
||||
match pattern_from_bytes(line) {
|
||||
Ok(pattern) => {
|
||||
patterns.push(pattern.to_string());
|
||||
Ok(true)
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "grep-matcher"
|
||||
version = "0.1.2" #:version
|
||||
version = "0.1.3" #:version
|
||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
||||
description = """
|
||||
A trait for regular expressions, with a focus on line oriented search.
|
||||
|
@@ -134,7 +134,7 @@ fn find_cap_ref(replacement: &[u8]) -> Option<CaptureRef> {
|
||||
/// Returns true if and only if the given byte is allowed in a capture name.
|
||||
fn is_valid_cap_letter(b: &u8) -> bool {
|
||||
match *b {
|
||||
b'0' ... b'9' | b'a' ... b'z' | b'A' ... b'Z' | b'_' => true,
|
||||
b'0' ..= b'9' | b'a' ..= b'z' | b'A' ..= b'Z' | b'_' => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "grep-printer"
|
||||
version = "0.1.2" #:version
|
||||
version = "0.1.3" #:version
|
||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
||||
description = """
|
||||
An implementation of the grep crate's Sink trait that provides standard
|
||||
@@ -18,8 +18,8 @@ default = ["serde1"]
|
||||
serde1 = ["base64", "serde", "serde_derive", "serde_json"]
|
||||
|
||||
[dependencies]
|
||||
base64 = { version = "0.10.0", optional = true }
|
||||
bstr = "0.1.2"
|
||||
base64 = { version = "0.11.0", optional = true }
|
||||
bstr = "0.2.0"
|
||||
grep-matcher = { version = "0.1.2", path = "../grep-matcher" }
|
||||
grep-searcher = { version = "0.1.4", path = "../grep-searcher" }
|
||||
termcolor = "1.0.4"
|
||||
|
@@ -5,7 +5,7 @@ use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use bstr::BStr;
|
||||
use bstr::ByteSlice;
|
||||
use grep_matcher::{Match, Matcher};
|
||||
use grep_searcher::{
|
||||
LineStep, Searcher,
|
||||
@@ -1274,7 +1274,7 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> {
|
||||
) -> io::Result<()> {
|
||||
if self.config().max_columns_preview {
|
||||
let original = line;
|
||||
let end = BStr::new(&bytes[line])
|
||||
let end = bytes[line]
|
||||
.grapheme_indices()
|
||||
.map(|(_, end, _)| end)
|
||||
.take(self.config().max_columns.unwrap_or(0) as usize)
|
||||
@@ -1396,7 +1396,7 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> {
|
||||
}
|
||||
let remainder = format!(
|
||||
"after match (found {:?} byte around offset {})\n",
|
||||
BStr::new(&[byte]), offset,
|
||||
[byte].as_bstr(), offset,
|
||||
);
|
||||
self.write(remainder.as_bytes())?;
|
||||
} else if let Some(byte) = bin.convert_byte() {
|
||||
@@ -1407,7 +1407,7 @@ impl<'a, M: Matcher, W: WriteColor> StandardImpl<'a, M, W> {
|
||||
}
|
||||
let remainder = format!(
|
||||
"matches (found {:?} byte around offset {})\n",
|
||||
BStr::new(&[byte]), offset,
|
||||
[byte].as_bstr(), offset,
|
||||
);
|
||||
self.write(remainder.as_bytes())?;
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ use std::io;
|
||||
use std::path::Path;
|
||||
use std::time;
|
||||
|
||||
use bstr::{BStr, BString};
|
||||
use bstr::{ByteSlice, ByteVec};
|
||||
use grep_matcher::{Captures, LineTerminator, Match, Matcher};
|
||||
use grep_searcher::{
|
||||
LineIter,
|
||||
@@ -263,12 +263,12 @@ impl<'a> Sunk<'a> {
|
||||
/// portability with a small cost: on Windows, paths that are not valid UTF-16
|
||||
/// will not roundtrip correctly.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PrinterPath<'a>(Cow<'a, BStr>);
|
||||
pub struct PrinterPath<'a>(Cow<'a, [u8]>);
|
||||
|
||||
impl<'a> PrinterPath<'a> {
|
||||
/// Create a new path suitable for printing.
|
||||
pub fn new(path: &'a Path) -> PrinterPath<'a> {
|
||||
PrinterPath(BString::from_path_lossy(path))
|
||||
PrinterPath(Vec::from_path_lossy(path))
|
||||
}
|
||||
|
||||
/// Create a new printer path from the given path which can be efficiently
|
||||
@@ -289,7 +289,7 @@ impl<'a> PrinterPath<'a> {
|
||||
/// path separators that are both replaced by `new_sep`. In all other
|
||||
/// environments, only `/` is treated as a path separator.
|
||||
fn replace_separator(&mut self, new_sep: u8) {
|
||||
let transformed_path: BString = self.0.bytes().map(|b| {
|
||||
let transformed_path: Vec<u8> = self.0.bytes().map(|b| {
|
||||
if b == b'/' || (cfg!(windows) && b == b'\\') {
|
||||
new_sep
|
||||
} else {
|
||||
@@ -301,7 +301,7 @@ impl<'a> PrinterPath<'a> {
|
||||
|
||||
/// Return the raw bytes for this path.
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
self.0.as_bytes()
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "grep-regex"
|
||||
version = "0.1.3" #:version
|
||||
version = "0.1.5" #:version
|
||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
||||
description = """
|
||||
Use Rust's regex library with the 'grep' crate.
|
||||
@@ -18,5 +18,4 @@ grep-matcher = { version = "0.1.2", path = "../grep-matcher" }
|
||||
log = "0.4.5"
|
||||
regex = "1.1"
|
||||
regex-syntax = "0.6.5"
|
||||
thread_local = "0.3.6"
|
||||
utf8-ranges = "1.0.1"
|
||||
thread_local = "1"
|
||||
|
@@ -11,7 +11,6 @@ extern crate log;
|
||||
extern crate regex;
|
||||
extern crate regex_syntax;
|
||||
extern crate thread_local;
|
||||
extern crate utf8_ranges;
|
||||
|
||||
pub use error::{Error, ErrorKind};
|
||||
pub use matcher::{RegexCaptures, RegexMatcher, RegexMatcherBuilder};
|
||||
|
@@ -71,10 +71,31 @@ impl RegexMatcherBuilder {
|
||||
&self,
|
||||
literals: &[B],
|
||||
) -> Result<RegexMatcher, Error> {
|
||||
let slices: Vec<_> = literals.iter().map(|s| s.as_ref()).collect();
|
||||
if !self.config.can_plain_aho_corasick() || literals.len() < 40 {
|
||||
let mut has_escape = false;
|
||||
let mut slices = vec![];
|
||||
for lit in literals {
|
||||
slices.push(lit.as_ref());
|
||||
has_escape = has_escape || lit.as_ref().contains('\\');
|
||||
}
|
||||
// Even when we have a fixed set of literals, we might still want to
|
||||
// use the regex engine. Specifically, if any string has an escape
|
||||
// in it, then we probably can't feed it to Aho-Corasick without
|
||||
// removing the escape. Additionally, if there are any particular
|
||||
// special match semantics we need to honor, that Aho-Corasick isn't
|
||||
// enough. Finally, the regex engine can do really well with a small
|
||||
// number of literals (at time of writing, this is changing soon), so
|
||||
// we use it when there's a small set.
|
||||
//
|
||||
// Yes, this is one giant hack. Ideally, this entirely separate literal
|
||||
// matcher that uses Aho-Corasick would be pushed down into the regex
|
||||
// engine.
|
||||
if has_escape
|
||||
|| !self.config.can_plain_aho_corasick()
|
||||
|| literals.len() < 40
|
||||
{
|
||||
return self.build(&slices.join("|"));
|
||||
}
|
||||
|
||||
let matcher = MultiLiteralMatcher::new(&slices)?;
|
||||
let imp = RegexMatcherImpl::MultiLiteral(matcher);
|
||||
Ok(RegexMatcher {
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use grep_matcher::ByteSet;
|
||||
use regex_syntax::hir::{self, Hir, HirKind};
|
||||
use utf8_ranges::Utf8Sequences;
|
||||
use regex_syntax::utf8::Utf8Sequences;
|
||||
|
||||
/// Return a confirmed set of non-matching bytes from the given expression.
|
||||
pub fn non_matching_bytes(expr: &Hir) -> ByteSet {
|
||||
|
@@ -78,7 +78,7 @@ impl Matcher for WordMatcher {
|
||||
// if it's worth it.
|
||||
|
||||
let cell = self.locs.get_or(|| {
|
||||
Box::new(RefCell::new(self.regex.capture_locations()))
|
||||
RefCell::new(self.regex.capture_locations())
|
||||
});
|
||||
let mut caps = cell.borrow_mut();
|
||||
self.regex.captures_read_at(&mut caps, haystack, at);
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "grep-searcher"
|
||||
version = "0.1.4" #:version
|
||||
version = "0.1.6" #:version
|
||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
||||
description = """
|
||||
Fast line oriented regex searching as a library.
|
||||
@@ -13,8 +13,8 @@ keywords = ["regex", "grep", "egrep", "search", "pattern"]
|
||||
license = "Unlicense/MIT"
|
||||
|
||||
[dependencies]
|
||||
bstr = { version = "0.1.2", default-features = false, features = ["std"] }
|
||||
bytecount = "0.5"
|
||||
bstr = { version = "0.2.0", default-features = false, features = ["std"] }
|
||||
bytecount = "0.6"
|
||||
encoding_rs = "0.8.14"
|
||||
encoding_rs_io = "0.1.6"
|
||||
grep-matcher = { version = "0.1.2", path = "../grep-matcher" }
|
||||
|
@@ -17,7 +17,7 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
fn example() -> Result<(), Box<Error>> {
|
||||
fn example() -> Result<(), Box<dyn Error>> {
|
||||
let pattern = match env::args().nth(1) {
|
||||
Some(pattern) => pattern,
|
||||
None => return Err(From::from(format!(
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use std::cmp;
|
||||
use std::io;
|
||||
|
||||
use bstr::{BStr, BString};
|
||||
use bstr::ByteSlice;
|
||||
|
||||
/// The default buffer capacity that we use for the line buffer.
|
||||
pub(crate) const DEFAULT_BUFFER_CAPACITY: usize = 8 * (1<<10); // 8 KB
|
||||
@@ -122,7 +122,7 @@ impl LineBufferBuilder {
|
||||
pub fn build(&self) -> LineBuffer {
|
||||
LineBuffer {
|
||||
config: self.config,
|
||||
buf: BString::from(vec![0; self.config.capacity]),
|
||||
buf: vec![0; self.config.capacity],
|
||||
pos: 0,
|
||||
last_lineterm: 0,
|
||||
end: 0,
|
||||
@@ -254,13 +254,14 @@ impl<'b, R: io::Read> LineBufferReader<'b, R> {
|
||||
|
||||
/// Return the contents of this buffer.
|
||||
pub fn buffer(&self) -> &[u8] {
|
||||
self.line_buffer.buffer().as_bytes()
|
||||
self.line_buffer.buffer()
|
||||
}
|
||||
|
||||
/// Return the underlying buffer as a byte string. Used for tests only.
|
||||
/// Return the buffer as a BStr, used for convenient equality checking
|
||||
/// in tests only.
|
||||
#[cfg(test)]
|
||||
fn bstr(&self) -> &BStr {
|
||||
self.line_buffer.buffer()
|
||||
fn bstr(&self) -> &::bstr::BStr {
|
||||
self.buffer().as_bstr()
|
||||
}
|
||||
|
||||
/// Consume the number of bytes provided. This must be less than or equal
|
||||
@@ -289,7 +290,7 @@ pub struct LineBuffer {
|
||||
/// The configuration of this buffer.
|
||||
config: Config,
|
||||
/// The primary buffer with which to hold data.
|
||||
buf: BString,
|
||||
buf: Vec<u8>,
|
||||
/// The current position of this buffer. This is always a valid sliceable
|
||||
/// index into `buf`, and its maximum value is the length of `buf`.
|
||||
pos: usize,
|
||||
@@ -352,13 +353,13 @@ impl LineBuffer {
|
||||
}
|
||||
|
||||
/// Return the contents of this buffer.
|
||||
fn buffer(&self) -> &BStr {
|
||||
fn buffer(&self) -> &[u8] {
|
||||
&self.buf[self.pos..self.last_lineterm]
|
||||
}
|
||||
|
||||
/// Return the contents of the free space beyond the end of the buffer as
|
||||
/// a mutable slice.
|
||||
fn free_buffer(&mut self) -> &mut BStr {
|
||||
fn free_buffer(&mut self) -> &mut [u8] {
|
||||
&mut self.buf[self.end..]
|
||||
}
|
||||
|
||||
@@ -481,7 +482,7 @@ impl LineBuffer {
|
||||
}
|
||||
|
||||
let roll_len = self.end - self.pos;
|
||||
self.buf.copy_within(self.pos.., 0);
|
||||
self.buf.copy_within_str(self.pos..self.end, 0);
|
||||
self.pos = 0;
|
||||
self.last_lineterm = roll_len;
|
||||
self.end = roll_len;
|
||||
@@ -519,7 +520,7 @@ impl LineBuffer {
|
||||
|
||||
/// Replaces `src` with `replacement` in bytes, and return the offset of the
|
||||
/// first replacement, if one exists.
|
||||
fn replace_bytes(bytes: &mut BStr, src: u8, replacement: u8) -> Option<usize> {
|
||||
fn replace_bytes(bytes: &mut [u8], src: u8, replacement: u8) -> Option<usize> {
|
||||
if src == replacement {
|
||||
return None;
|
||||
}
|
||||
@@ -542,7 +543,7 @@ fn replace_bytes(bytes: &mut BStr, src: u8, replacement: u8) -> Option<usize> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::str;
|
||||
use bstr::BString;
|
||||
use bstr::{ByteSlice, ByteVec};
|
||||
use super::*;
|
||||
|
||||
const SHERLOCK: &'static str = "\
|
||||
@@ -563,7 +564,7 @@ and exhibited clearly, with a label attached.\
|
||||
src: u8,
|
||||
replacement: u8,
|
||||
) -> (String, Option<usize>) {
|
||||
let mut dst = BString::from(slice);
|
||||
let mut dst = Vec::from(slice);
|
||||
let result = replace_bytes(&mut dst, src, replacement);
|
||||
(dst.into_string().unwrap(), result)
|
||||
}
|
||||
@@ -677,12 +678,12 @@ and exhibited clearly, with a label attached.\
|
||||
let mut linebuf = LineBufferBuilder::new().capacity(1).build();
|
||||
let mut rdr = LineBufferReader::new(bytes.as_bytes(), &mut linebuf);
|
||||
|
||||
let mut got = BString::new();
|
||||
let mut got = vec![];
|
||||
while rdr.fill().unwrap() {
|
||||
got.push(rdr.buffer());
|
||||
got.push_str(rdr.buffer());
|
||||
rdr.consume_all();
|
||||
}
|
||||
assert_eq!(bytes, got);
|
||||
assert_eq!(bytes, got.as_bstr());
|
||||
assert_eq!(rdr.absolute_byte_offset(), bytes.len() as u64);
|
||||
assert_eq!(rdr.binary_byte_offset(), None);
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
A collection of routines for performing operations on lines.
|
||||
*/
|
||||
|
||||
use bstr::B;
|
||||
use bstr::ByteSlice;
|
||||
use bytecount;
|
||||
use grep_matcher::{LineTerminator, Match};
|
||||
|
||||
@@ -85,7 +85,7 @@ impl LineStep {
|
||||
#[inline(always)]
|
||||
fn next_impl(&mut self, mut bytes: &[u8]) -> Option<(usize, usize)> {
|
||||
bytes = &bytes[..self.end];
|
||||
match B(&bytes[self.pos..]).find_byte(self.line_term) {
|
||||
match bytes[self.pos..].find_byte(self.line_term) {
|
||||
None => {
|
||||
if self.pos < bytes.len() {
|
||||
let m = (self.pos, bytes.len());
|
||||
@@ -135,14 +135,14 @@ pub fn locate(
|
||||
line_term: u8,
|
||||
range: Match,
|
||||
) -> Match {
|
||||
let line_start = B(&bytes[..range.start()])
|
||||
let line_start = bytes[..range.start()]
|
||||
.rfind_byte(line_term)
|
||||
.map_or(0, |i| i + 1);
|
||||
let line_end =
|
||||
if range.end() > line_start && bytes[range.end() - 1] == line_term {
|
||||
range.end()
|
||||
} else {
|
||||
B(&bytes[range.end()..])
|
||||
bytes[range.end()..]
|
||||
.find_byte(line_term)
|
||||
.map_or(bytes.len(), |i| range.end() + i + 1)
|
||||
};
|
||||
@@ -182,7 +182,7 @@ fn preceding_by_pos(
|
||||
pos -= 1;
|
||||
}
|
||||
loop {
|
||||
match B(&bytes[..pos]).rfind_byte(line_term) {
|
||||
match bytes[..pos].rfind_byte(line_term) {
|
||||
None => {
|
||||
return 0;
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use std::cmp;
|
||||
|
||||
use bstr::B;
|
||||
use bstr::ByteSlice;
|
||||
|
||||
use grep_matcher::{LineMatchKind, Matcher};
|
||||
use lines::{self, LineStep};
|
||||
@@ -161,7 +161,7 @@ impl<'s, M: Matcher, S: Sink> Core<'s, M, S> {
|
||||
BinaryDetection::Convert(b) => b,
|
||||
_ => return Ok(false),
|
||||
};
|
||||
if let Some(i) = B(&buf[*range]).find_byte(binary_byte) {
|
||||
if let Some(i) = buf[*range].find_byte(binary_byte) {
|
||||
let offset = range.start() + i;
|
||||
self.binary_byte_offset = Some(offset);
|
||||
if !self.binary_data(offset as u64)? {
|
||||
|
@@ -1,3 +1,4 @@
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
|
||||
@@ -49,9 +50,9 @@ impl SinkError for io::Error {
|
||||
|
||||
/// A `Box<std::error::Error>` can be used as an error for `Sink`
|
||||
/// implementations out of the box.
|
||||
impl SinkError for Box<::std::error::Error> {
|
||||
fn error_message<T: fmt::Display>(message: T) -> Box<::std::error::Error> {
|
||||
Box::<::std::error::Error>::from(message.to_string())
|
||||
impl SinkError for Box<dyn error::Error> {
|
||||
fn error_message<T: fmt::Display>(message: T) -> Box<dyn error::Error> {
|
||||
Box::<dyn error::Error>::from(message.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use std::io::{self, Write};
|
||||
use std::str;
|
||||
|
||||
use bstr::B;
|
||||
use bstr::ByteSlice;
|
||||
use grep_matcher::{
|
||||
LineMatchKind, LineTerminator, Match, Matcher, NoCaptures, NoError,
|
||||
};
|
||||
@@ -94,7 +94,7 @@ impl Matcher for RegexMatcher {
|
||||
}
|
||||
// Make it interesting and return the last byte in the current
|
||||
// line.
|
||||
let i = B(haystack)
|
||||
let i = haystack
|
||||
.find_byte(self.line_term.unwrap().as_byte())
|
||||
.map(|i| i)
|
||||
.unwrap_or(haystack.len() - 1);
|
||||
|
@@ -21,7 +21,7 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
fn try_main() -> Result<(), Box<Error>> {
|
||||
fn try_main() -> Result<(), Box<dyn Error>> {
|
||||
let mut args: Vec<OsString> = env::args_os().collect();
|
||||
if args.len() < 2 {
|
||||
return Err("Usage: simplegrep <pattern> [<path> ...]".into());
|
||||
@@ -32,7 +32,7 @@ fn try_main() -> Result<(), Box<Error>> {
|
||||
search(cli::pattern_from_os(&args[1])?, &args[2..])
|
||||
}
|
||||
|
||||
fn search(pattern: &str, paths: &[OsString]) -> Result<(), Box<Error>> {
|
||||
fn search(pattern: &str, paths: &[OsString]) -> Result<(), Box<dyn Error>> {
|
||||
let matcher = RegexMatcher::new_line_matcher(&pattern)?;
|
||||
let mut searcher = SearcherBuilder::new()
|
||||
.binary_detection(BinaryDetection::quit(b'\x00'))
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ignore"
|
||||
version = "0.4.7" #:version
|
||||
version = "0.4.11" #:version
|
||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
||||
description = """
|
||||
A fast library for efficiently matching ignore files such as `.gitignore`
|
||||
@@ -18,21 +18,18 @@ name = "ignore"
|
||||
bench = false
|
||||
|
||||
[dependencies]
|
||||
crossbeam-channel = "0.3.6"
|
||||
crossbeam-channel = "0.4.0"
|
||||
globset = { version = "0.4.3", path = "../globset" }
|
||||
lazy_static = "1.1"
|
||||
log = "0.4.5"
|
||||
memchr = "2.1"
|
||||
regex = "1.1"
|
||||
same-file = "1.0.4"
|
||||
thread_local = "0.3.6"
|
||||
thread_local = "1"
|
||||
walkdir = "2.2.7"
|
||||
|
||||
[target.'cfg(windows)'.dependencies.winapi-util]
|
||||
version = "0.1.2"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.0.5"
|
||||
|
||||
[features]
|
||||
simd-accel = ["globset/simd-accel"]
|
||||
|
@@ -14,13 +14,13 @@
|
||||
// well.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::{OsString, OsStr};
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use gitignore::{self, Gitignore, GitignoreBuilder};
|
||||
use pathutil::{is_hidden, strip_prefix};
|
||||
use overrides::{self, Override};
|
||||
use pathutil::{is_hidden, strip_prefix};
|
||||
use types::{self, Types};
|
||||
use walk::DirEntry;
|
||||
use {Error, Match, PartialErrorBuilder};
|
||||
@@ -152,10 +152,7 @@ impl Ignore {
|
||||
///
|
||||
/// Note that this can only be called on an `Ignore` matcher with no
|
||||
/// parents (i.e., `is_root` returns `true`). This will panic otherwise.
|
||||
pub fn add_parents<P: AsRef<Path>>(
|
||||
&self,
|
||||
path: P,
|
||||
) -> (Ignore, Option<Error>) {
|
||||
pub fn add_parents<P: AsRef<Path>>(&self, path: P) -> (Ignore, Option<Error>) {
|
||||
if !self.0.opts.parents
|
||||
&& !self.0.opts.git_ignore
|
||||
&& !self.0.opts.git_exclude
|
||||
@@ -197,12 +194,11 @@ impl Ignore {
|
||||
errs.maybe_push(err);
|
||||
igtmp.is_absolute_parent = true;
|
||||
igtmp.absolute_base = Some(absolute_base.clone());
|
||||
igtmp.has_git =
|
||||
if self.0.opts.git_ignore {
|
||||
parent.join(".git").exists()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
igtmp.has_git = if self.0.opts.git_ignore {
|
||||
parent.join(".git").exists()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
ig = Ignore(Arc::new(igtmp));
|
||||
compiled.insert(parent.as_os_str().to_os_string(), ig.clone());
|
||||
}
|
||||
@@ -217,10 +213,7 @@ impl Ignore {
|
||||
/// returned if it exists.
|
||||
///
|
||||
/// Note that all I/O errors are completely ignored.
|
||||
pub fn add_child<P: AsRef<Path>>(
|
||||
&self,
|
||||
dir: P,
|
||||
) -> (Ignore, Option<Error>) {
|
||||
pub fn add_child<P: AsRef<Path>>(&self, dir: P) -> (Ignore, Option<Error>) {
|
||||
let (ig, err) = self.add_child_path(dir.as_ref());
|
||||
(Ignore(Arc::new(ig)), err)
|
||||
}
|
||||
@@ -228,64 +221,49 @@ impl Ignore {
|
||||
/// Like add_child, but takes a full path and returns an IgnoreInner.
|
||||
fn add_child_path(&self, dir: &Path) -> (IgnoreInner, Option<Error>) {
|
||||
let mut errs = PartialErrorBuilder::default();
|
||||
let custom_ig_matcher =
|
||||
if self.0.custom_ignore_filenames.is_empty() {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let (m, err) =
|
||||
create_gitignore(
|
||||
&dir,
|
||||
&self.0.custom_ignore_filenames,
|
||||
self.0.opts.ignore_case_insensitive,
|
||||
);
|
||||
errs.maybe_push(err);
|
||||
m
|
||||
};
|
||||
let ig_matcher =
|
||||
if !self.0.opts.ignore {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let (m, err) =
|
||||
create_gitignore(
|
||||
&dir,
|
||||
&[".ignore"],
|
||||
self.0.opts.ignore_case_insensitive,
|
||||
);
|
||||
errs.maybe_push(err);
|
||||
m
|
||||
};
|
||||
let gi_matcher =
|
||||
if !self.0.opts.git_ignore {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let (m, err) =
|
||||
create_gitignore(
|
||||
&dir,
|
||||
&[".gitignore"],
|
||||
self.0.opts.ignore_case_insensitive,
|
||||
);
|
||||
errs.maybe_push(err);
|
||||
m
|
||||
};
|
||||
let gi_exclude_matcher =
|
||||
if !self.0.opts.git_exclude {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let (m, err) =
|
||||
create_gitignore(
|
||||
&dir,
|
||||
&[".git/info/exclude"],
|
||||
self.0.opts.ignore_case_insensitive,
|
||||
);
|
||||
errs.maybe_push(err);
|
||||
m
|
||||
};
|
||||
let has_git =
|
||||
if self.0.opts.git_ignore {
|
||||
dir.join(".git").exists()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let custom_ig_matcher = if self.0.custom_ignore_filenames.is_empty() {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let (m, err) = create_gitignore(
|
||||
&dir,
|
||||
&self.0.custom_ignore_filenames,
|
||||
self.0.opts.ignore_case_insensitive,
|
||||
);
|
||||
errs.maybe_push(err);
|
||||
m
|
||||
};
|
||||
let ig_matcher = if !self.0.opts.ignore {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let (m, err) =
|
||||
create_gitignore(&dir, &[".ignore"], self.0.opts.ignore_case_insensitive);
|
||||
errs.maybe_push(err);
|
||||
m
|
||||
};
|
||||
let gi_matcher = if !self.0.opts.git_ignore {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let (m, err) =
|
||||
create_gitignore(&dir, &[".gitignore"], self.0.opts.ignore_case_insensitive);
|
||||
errs.maybe_push(err);
|
||||
m
|
||||
};
|
||||
let gi_exclude_matcher = if !self.0.opts.git_exclude {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let (m, err) = create_gitignore(
|
||||
&dir,
|
||||
&[".git/info/exclude"],
|
||||
self.0.opts.ignore_case_insensitive,
|
||||
);
|
||||
errs.maybe_push(err);
|
||||
m
|
||||
};
|
||||
let has_git = if self.0.opts.git_ignore {
|
||||
dir.join(".git").exists()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let ig = IgnoreInner {
|
||||
compiled: self.0.compiled.clone(),
|
||||
dir: dir.to_path_buf(),
|
||||
@@ -313,16 +291,16 @@ impl Ignore {
|
||||
let has_custom_ignore_files = !self.0.custom_ignore_filenames.is_empty();
|
||||
let has_explicit_ignores = !self.0.explicit_ignores.is_empty();
|
||||
|
||||
opts.ignore || opts.git_global || opts.git_ignore
|
||||
|| opts.git_exclude || has_custom_ignore_files
|
||||
|| has_explicit_ignores
|
||||
opts.ignore
|
||||
|| opts.git_global
|
||||
|| opts.git_ignore
|
||||
|| opts.git_exclude
|
||||
|| has_custom_ignore_files
|
||||
|| has_explicit_ignores
|
||||
}
|
||||
|
||||
/// Like `matched`, but works with a directory entry instead.
|
||||
pub fn matched_dir_entry<'a>(
|
||||
&'a self,
|
||||
dent: &DirEntry,
|
||||
) -> Match<IgnoreMatch<'a>> {
|
||||
pub fn matched_dir_entry<'a>(&'a self, dent: &DirEntry) -> Match<IgnoreMatch<'a>> {
|
||||
let m = self.matched(dent.path(), dent.is_dir());
|
||||
if m.is_none() && self.0.opts.hidden && is_hidden(dent) {
|
||||
return Match::Ignore(IgnoreMatch::hidden());
|
||||
@@ -334,11 +312,7 @@ impl Ignore {
|
||||
/// ignored or not.
|
||||
///
|
||||
/// The match contains information about its origin.
|
||||
fn matched<'a, P: AsRef<Path>>(
|
||||
&'a self,
|
||||
path: P,
|
||||
is_dir: bool,
|
||||
) -> Match<IgnoreMatch<'a>> {
|
||||
fn matched<'a, P: AsRef<Path>>(&'a self, path: P, is_dir: bool) -> Match<IgnoreMatch<'a>> {
|
||||
// We need to be careful with our path. If it has a leading ./, then
|
||||
// strip it because it causes nothing but trouble.
|
||||
let mut path = path.as_ref();
|
||||
@@ -350,9 +324,11 @@ impl Ignore {
|
||||
// return that result immediately. Overrides have the highest
|
||||
// precedence.
|
||||
if !self.0.overrides.is_empty() {
|
||||
let mat =
|
||||
self.0.overrides.matched(path, is_dir)
|
||||
.map(IgnoreMatch::overrides);
|
||||
let mat = self
|
||||
.0
|
||||
.overrides
|
||||
.matched(path, is_dir)
|
||||
.map(IgnoreMatch::overrides);
|
||||
if !mat.is_none() {
|
||||
return mat;
|
||||
}
|
||||
@@ -367,8 +343,7 @@ impl Ignore {
|
||||
}
|
||||
}
|
||||
if !self.0.types.is_empty() {
|
||||
let mat =
|
||||
self.0.types.matched(path, is_dir).map(IgnoreMatch::types);
|
||||
let mat = self.0.types.matched(path, is_dir).map(IgnoreMatch::types);
|
||||
if mat.is_ignore() {
|
||||
return mat;
|
||||
} else if mat.is_whitelist() {
|
||||
@@ -380,61 +355,70 @@ impl Ignore {
|
||||
|
||||
/// Performs matching only on the ignore files for this directory and
|
||||
/// all parent directories.
|
||||
fn matched_ignore<'a>(
|
||||
&'a self,
|
||||
path: &Path,
|
||||
is_dir: bool,
|
||||
) -> Match<IgnoreMatch<'a>> {
|
||||
let (mut m_custom_ignore, mut m_ignore, mut m_gi, mut m_gi_exclude, mut m_explicit) =
|
||||
(Match::None, Match::None, Match::None, Match::None, Match::None);
|
||||
fn matched_ignore<'a>(&'a self, path: &Path, is_dir: bool) -> Match<IgnoreMatch<'a>> {
|
||||
let (mut m_custom_ignore, mut m_ignore, mut m_gi, mut m_gi_exclude, mut m_explicit) = (
|
||||
Match::None,
|
||||
Match::None,
|
||||
Match::None,
|
||||
Match::None,
|
||||
Match::None,
|
||||
);
|
||||
let any_git = self.parents().any(|ig| ig.0.has_git);
|
||||
let mut saw_git = false;
|
||||
for ig in self.parents().take_while(|ig| !ig.0.is_absolute_parent) {
|
||||
if m_custom_ignore.is_none() {
|
||||
m_custom_ignore =
|
||||
ig.0.custom_ignore_matcher.matched(path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
ig.0.custom_ignore_matcher
|
||||
.matched(path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
}
|
||||
if m_ignore.is_none() {
|
||||
m_ignore =
|
||||
ig.0.ignore_matcher.matched(path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
ig.0.ignore_matcher
|
||||
.matched(path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
}
|
||||
if any_git && !saw_git && m_gi.is_none() {
|
||||
m_gi =
|
||||
ig.0.git_ignore_matcher.matched(path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
ig.0.git_ignore_matcher
|
||||
.matched(path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
}
|
||||
if any_git && !saw_git && m_gi_exclude.is_none() {
|
||||
m_gi_exclude =
|
||||
ig.0.git_exclude_matcher.matched(path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
ig.0.git_exclude_matcher
|
||||
.matched(path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
}
|
||||
saw_git = saw_git || ig.0.has_git;
|
||||
}
|
||||
if self.0.opts.parents {
|
||||
if let Some(abs_parent_path) = self.absolute_base() {
|
||||
let path = abs_parent_path.join(path);
|
||||
for ig in self.parents().skip_while(|ig|!ig.0.is_absolute_parent) {
|
||||
for ig in self.parents().skip_while(|ig| !ig.0.is_absolute_parent) {
|
||||
if m_custom_ignore.is_none() {
|
||||
m_custom_ignore =
|
||||
ig.0.custom_ignore_matcher.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
ig.0.custom_ignore_matcher
|
||||
.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
}
|
||||
if m_ignore.is_none() {
|
||||
m_ignore =
|
||||
ig.0.ignore_matcher.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
ig.0.ignore_matcher
|
||||
.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
}
|
||||
if any_git && !saw_git && m_gi.is_none() {
|
||||
m_gi =
|
||||
ig.0.git_ignore_matcher.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
ig.0.git_ignore_matcher
|
||||
.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
}
|
||||
if any_git && !saw_git && m_gi_exclude.is_none() {
|
||||
m_gi_exclude =
|
||||
ig.0.git_exclude_matcher.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
ig.0.git_exclude_matcher
|
||||
.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore);
|
||||
}
|
||||
saw_git = saw_git || ig.0.has_git;
|
||||
}
|
||||
@@ -446,16 +430,21 @@ impl Ignore {
|
||||
}
|
||||
m_explicit = gi.matched(&path, is_dir).map(IgnoreMatch::gitignore);
|
||||
}
|
||||
let m_global =
|
||||
if any_git {
|
||||
self.0.git_global_matcher
|
||||
.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore)
|
||||
} else {
|
||||
Match::None
|
||||
};
|
||||
let m_global = if any_git {
|
||||
self.0
|
||||
.git_global_matcher
|
||||
.matched(&path, is_dir)
|
||||
.map(IgnoreMatch::gitignore)
|
||||
} else {
|
||||
Match::None
|
||||
};
|
||||
|
||||
m_custom_ignore.or(m_ignore).or(m_gi).or(m_gi_exclude).or(m_global).or(m_explicit)
|
||||
m_custom_ignore
|
||||
.or(m_ignore)
|
||||
.or(m_gi)
|
||||
.or(m_gi_exclude)
|
||||
.or(m_global)
|
||||
.or(m_explicit)
|
||||
}
|
||||
|
||||
/// Returns an iterator over parent ignore matchers, including this one.
|
||||
@@ -535,20 +524,19 @@ impl IgnoreBuilder {
|
||||
/// The matcher returned won't match anything until ignore rules from
|
||||
/// directories are added to it.
|
||||
pub fn build(&self) -> Ignore {
|
||||
let git_global_matcher =
|
||||
if !self.opts.git_global {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let mut builder = GitignoreBuilder::new("");
|
||||
builder
|
||||
.case_insensitive(self.opts.ignore_case_insensitive)
|
||||
.unwrap();
|
||||
let (gi, err) = builder.build_global();
|
||||
if let Some(err) = err {
|
||||
debug!("{}", err);
|
||||
}
|
||||
gi
|
||||
};
|
||||
let git_global_matcher = if !self.opts.git_global {
|
||||
Gitignore::empty()
|
||||
} else {
|
||||
let mut builder = GitignoreBuilder::new("");
|
||||
builder
|
||||
.case_insensitive(self.opts.ignore_case_insensitive)
|
||||
.unwrap();
|
||||
let (gi, err) = builder.build_global();
|
||||
if let Some(err) = err {
|
||||
debug!("{}", err);
|
||||
}
|
||||
gi
|
||||
};
|
||||
|
||||
Ignore(Arc::new(IgnoreInner {
|
||||
compiled: Arc::new(RwLock::new(HashMap::new())),
|
||||
@@ -604,9 +592,10 @@ impl IgnoreBuilder {
|
||||
/// later names.
|
||||
pub fn add_custom_ignore_filename<S: AsRef<OsStr>>(
|
||||
&mut self,
|
||||
file_name: S
|
||||
file_name: S,
|
||||
) -> &mut IgnoreBuilder {
|
||||
self.custom_ignore_filenames.push(file_name.as_ref().to_os_string());
|
||||
self.custom_ignore_filenames
|
||||
.push(file_name.as_ref().to_os_string());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -678,10 +667,7 @@ impl IgnoreBuilder {
|
||||
/// Process ignore files case insensitively
|
||||
///
|
||||
/// This is disabled by default.
|
||||
pub fn ignore_case_insensitive(
|
||||
&mut self,
|
||||
yes: bool,
|
||||
) -> &mut IgnoreBuilder {
|
||||
pub fn ignore_case_insensitive(&mut self, yes: bool) -> &mut IgnoreBuilder {
|
||||
self.opts.ignore_case_insensitive = yes;
|
||||
self
|
||||
}
|
||||
@@ -721,10 +707,9 @@ mod tests {
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
use tempfile::{self, TempDir};
|
||||
|
||||
use dir::IgnoreBuilder;
|
||||
use gitignore::Gitignore;
|
||||
use tests::TempDir;
|
||||
use Error;
|
||||
|
||||
fn wfile<P: AsRef<Path>>(path: P, contents: &str) {
|
||||
@@ -743,19 +728,21 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn tmpdir(prefix: &str) -> TempDir {
|
||||
tempfile::Builder::new().prefix(prefix).tempdir().unwrap()
|
||||
fn tmpdir() -> TempDir {
|
||||
TempDir::new().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_ignore() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
wfile(td.path().join("not-an-ignore"), "foo\n!bar");
|
||||
|
||||
let (gi, err) = Gitignore::new(td.path().join("not-an-ignore"));
|
||||
assert!(err.is_none());
|
||||
let (ig, err) = IgnoreBuilder::new()
|
||||
.add_ignore(gi).build().add_child(td.path());
|
||||
.add_ignore(gi)
|
||||
.build()
|
||||
.add_child(td.path());
|
||||
assert!(err.is_none());
|
||||
assert!(ig.matched("foo", false).is_ignore());
|
||||
assert!(ig.matched("bar", false).is_whitelist());
|
||||
@@ -764,7 +751,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn git_exclude() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join(".git/info"));
|
||||
wfile(td.path().join(".git/info/exclude"), "foo\n!bar");
|
||||
|
||||
@@ -777,7 +764,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn gitignore() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join(".git"));
|
||||
wfile(td.path().join(".gitignore"), "foo\n!bar");
|
||||
|
||||
@@ -790,7 +777,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn gitignore_no_git() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
wfile(td.path().join(".gitignore"), "foo\n!bar");
|
||||
|
||||
let (ig, err) = IgnoreBuilder::new().build().add_child(td.path());
|
||||
@@ -802,7 +789,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn ignore() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
wfile(td.path().join(".ignore"), "foo\n!bar");
|
||||
|
||||
let (ig, err) = IgnoreBuilder::new().build().add_child(td.path());
|
||||
@@ -814,13 +801,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn custom_ignore() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
let custom_ignore = ".customignore";
|
||||
wfile(td.path().join(custom_ignore), "foo\n!bar");
|
||||
|
||||
let (ig, err) = IgnoreBuilder::new()
|
||||
.add_custom_ignore_filename(custom_ignore)
|
||||
.build().add_child(td.path());
|
||||
.build()
|
||||
.add_child(td.path());
|
||||
assert!(err.is_none());
|
||||
assert!(ig.matched("foo", false).is_ignore());
|
||||
assert!(ig.matched("bar", false).is_whitelist());
|
||||
@@ -830,14 +818,15 @@ mod tests {
|
||||
// Tests that a custom ignore file will override an .ignore.
|
||||
#[test]
|
||||
fn custom_ignore_over_ignore() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
let custom_ignore = ".customignore";
|
||||
wfile(td.path().join(".ignore"), "foo");
|
||||
wfile(td.path().join(custom_ignore), "!foo");
|
||||
|
||||
let (ig, err) = IgnoreBuilder::new()
|
||||
.add_custom_ignore_filename(custom_ignore)
|
||||
.build().add_child(td.path());
|
||||
.build()
|
||||
.add_child(td.path());
|
||||
assert!(err.is_none());
|
||||
assert!(ig.matched("foo", false).is_whitelist());
|
||||
}
|
||||
@@ -845,7 +834,7 @@ mod tests {
|
||||
// Tests that earlier custom ignore files have lower precedence than later.
|
||||
#[test]
|
||||
fn custom_ignore_precedence() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
let custom_ignore1 = ".customignore1";
|
||||
let custom_ignore2 = ".customignore2";
|
||||
wfile(td.path().join(custom_ignore1), "foo");
|
||||
@@ -854,7 +843,8 @@ mod tests {
|
||||
let (ig, err) = IgnoreBuilder::new()
|
||||
.add_custom_ignore_filename(custom_ignore1)
|
||||
.add_custom_ignore_filename(custom_ignore2)
|
||||
.build().add_child(td.path());
|
||||
.build()
|
||||
.add_child(td.path());
|
||||
assert!(err.is_none());
|
||||
assert!(ig.matched("foo", false).is_whitelist());
|
||||
}
|
||||
@@ -862,7 +852,7 @@ mod tests {
|
||||
// Tests that an .ignore will override a .gitignore.
|
||||
#[test]
|
||||
fn ignore_over_gitignore() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
wfile(td.path().join(".gitignore"), "foo");
|
||||
wfile(td.path().join(".ignore"), "!foo");
|
||||
|
||||
@@ -874,7 +864,7 @@ mod tests {
|
||||
// Tests that exclude has lower precedent than both .ignore and .gitignore.
|
||||
#[test]
|
||||
fn exclude_lowest() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
wfile(td.path().join(".gitignore"), "!foo");
|
||||
wfile(td.path().join(".ignore"), "!bar");
|
||||
mkdirp(td.path().join(".git/info"));
|
||||
@@ -889,7 +879,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn errored() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
wfile(td.path().join(".gitignore"), "{foo");
|
||||
|
||||
let (_, err) = IgnoreBuilder::new().build().add_child(td.path());
|
||||
@@ -898,7 +888,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn errored_both() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
wfile(td.path().join(".gitignore"), "{foo");
|
||||
wfile(td.path().join(".ignore"), "{bar");
|
||||
|
||||
@@ -908,7 +898,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn errored_partial() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join(".git"));
|
||||
wfile(td.path().join(".gitignore"), "{foo\nbar");
|
||||
|
||||
@@ -919,7 +909,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn errored_partial_and_ignore() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
wfile(td.path().join(".gitignore"), "{foo\nbar");
|
||||
wfile(td.path().join(".ignore"), "!bar");
|
||||
|
||||
@@ -930,7 +920,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn not_present_empty() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
|
||||
let (_, err) = IgnoreBuilder::new().build().add_child(td.path());
|
||||
assert!(err.is_none());
|
||||
@@ -940,7 +930,7 @@ mod tests {
|
||||
fn stops_at_git_dir() {
|
||||
// This tests that .gitignore files beyond a .git barrier aren't
|
||||
// matched, but .ignore files are.
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join(".git"));
|
||||
mkdirp(td.path().join("foo/.git"));
|
||||
wfile(td.path().join(".gitignore"), "foo");
|
||||
@@ -961,7 +951,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn absolute_parent() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join(".git"));
|
||||
mkdirp(td.path().join("foo"));
|
||||
wfile(td.path().join(".gitignore"), "bar");
|
||||
@@ -984,7 +974,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn absolute_parent_anchored() {
|
||||
let td = tmpdir("ignore-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join(".git"));
|
||||
mkdirp(td.path().join("src/llvm"));
|
||||
wfile(td.path().join(".gitignore"), "/llvm/\nfoo");
|
||||
|
@@ -249,7 +249,7 @@ impl Gitignore {
|
||||
return Match::None;
|
||||
}
|
||||
let path = path.as_ref();
|
||||
let _matches = self.matches.as_ref().unwrap().get_default();
|
||||
let _matches = self.matches.as_ref().unwrap().get_or_default();
|
||||
let mut matches = _matches.borrow_mut();
|
||||
let candidate = Candidate::new(path);
|
||||
self.set.matches_candidate_into(&candidate, &mut *matches);
|
||||
@@ -537,7 +537,7 @@ impl GitignoreBuilder {
|
||||
///
|
||||
/// Note that the file path returned may not exist.
|
||||
fn gitconfig_excludes_path() -> Option<PathBuf> {
|
||||
// git supports $HOME/.gitconfig and $XDG_CONFIG_DIR/git/config. Notably,
|
||||
// git supports $HOME/.gitconfig and $XDG_CONFIG_HOME/git/config. Notably,
|
||||
// both can be active at the same time, where $HOME/.gitconfig takes
|
||||
// precedent. So if $HOME/.gitconfig defines a `core.excludesFile`, then
|
||||
// we're done.
|
||||
@@ -568,7 +568,7 @@ fn gitconfig_home_contents() -> Option<Vec<u8>> {
|
||||
}
|
||||
|
||||
/// Returns the file contents of git's global config file, if one exists, in
|
||||
/// the user's XDG_CONFIG_DIR directory.
|
||||
/// the user's XDG_CONFIG_HOME directory.
|
||||
fn gitconfig_xdg_contents() -> Option<Vec<u8>> {
|
||||
let path = env::var_os("XDG_CONFIG_HOME")
|
||||
.and_then(|x| if x.is_empty() { None } else { Some(PathBuf::from(x)) })
|
||||
|
@@ -55,8 +55,6 @@ extern crate log;
|
||||
extern crate memchr;
|
||||
extern crate regex;
|
||||
extern crate same_file;
|
||||
#[cfg(test)]
|
||||
extern crate tempfile;
|
||||
extern crate thread_local;
|
||||
extern crate walkdir;
|
||||
#[cfg(windows)]
|
||||
@@ -442,3 +440,66 @@ impl<T> Match<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::env;
|
||||
use std::error;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::result;
|
||||
|
||||
/// A convenient result type alias.
|
||||
pub type Result<T> =
|
||||
result::Result<T, Box<dyn error::Error + Send + Sync>>;
|
||||
|
||||
macro_rules! err {
|
||||
($($tt:tt)*) => {
|
||||
Box::<dyn error::Error + Send + Sync>::from(format!($($tt)*))
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple wrapper for creating a temporary directory that is
|
||||
/// automatically deleted when it's dropped.
|
||||
///
|
||||
/// We use this in lieu of tempfile because tempfile brings in too many
|
||||
/// dependencies.
|
||||
#[derive(Debug)]
|
||||
pub struct TempDir(PathBuf);
|
||||
|
||||
impl Drop for TempDir {
|
||||
fn drop(&mut self) {
|
||||
fs::remove_dir_all(&self.0).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl TempDir {
|
||||
/// Create a new empty temporary directory under the system's configured
|
||||
/// temporary directory.
|
||||
pub fn new() -> Result<TempDir> {
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
static TRIES: usize = 100;
|
||||
static COUNTER: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
let tmpdir = env::temp_dir();
|
||||
for _ in 0..TRIES {
|
||||
let count = COUNTER.fetch_add(1, Ordering::SeqCst);
|
||||
let path = tmpdir.join("rust-ignore").join(count.to_string());
|
||||
if path.is_dir() {
|
||||
continue;
|
||||
}
|
||||
fs::create_dir_all(&path).map_err(|e| {
|
||||
err!("failed to create {}: {}", path.display(), e)
|
||||
})?;
|
||||
return Ok(TempDir(path));
|
||||
}
|
||||
Err(err!("failed to create temp dir after {} tries", TRIES))
|
||||
}
|
||||
|
||||
/// Return the underlying path to this temporary directory.
|
||||
pub fn path(&self) -> &Path {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -135,6 +135,7 @@ const DEFAULT_TYPES: &'static [(&'static str, &'static [&'static str])] = &[
|
||||
("d", &["*.d"]),
|
||||
("dhall", &["*.dhall"]),
|
||||
("docker", &["*Dockerfile*"]),
|
||||
("edn", &["*.edn"]),
|
||||
("elisp", &["*.el"]),
|
||||
("elixir", &["*.ex", "*.eex", "*.exs"]),
|
||||
("elm", &["*.elm"]),
|
||||
@@ -219,7 +220,7 @@ const DEFAULT_TYPES: &'static [(&'static str, &'static [&'static str])] = &[
|
||||
("msbuild", &[
|
||||
"*.csproj", "*.fsproj", "*.vcxproj", "*.proj", "*.props", "*.targets"
|
||||
]),
|
||||
("nim", &["*.nim"]),
|
||||
("nim", &["*.nim", "*.nimf", "*.nimble", "*.nims"]),
|
||||
("nix", &["*.nix"]),
|
||||
("objc", &["*.h", "*.m"]),
|
||||
("objcpp", &["*.h", "*.mm"]),
|
||||
@@ -241,6 +242,7 @@ const DEFAULT_TYPES: &'static [(&'static str, &'static [&'static str])] = &[
|
||||
("readme", &["README*", "*README"]),
|
||||
("r", &["*.R", "*.r", "*.Rmd", "*.Rnw"]),
|
||||
("rdoc", &["*.rdoc"]),
|
||||
("robot", &["*.robot"]),
|
||||
("rst", &["*.rst"]),
|
||||
("ruby", &["Gemfile", "*.gemspec", ".irbrc", "Rakefile", "*.rb"]),
|
||||
("rust", &["*.rs"]),
|
||||
@@ -513,7 +515,7 @@ impl Types {
|
||||
return Match::None;
|
||||
}
|
||||
};
|
||||
let mut matches = self.matches.get_default().borrow_mut();
|
||||
let mut matches = self.matches.get_or_default().borrow_mut();
|
||||
self.set.matches_into(name, &mut *matches);
|
||||
// The highest precedent match is the last one.
|
||||
if let Some(&i) = matches.last() {
|
||||
|
@@ -4,8 +4,8 @@ use std::fmt;
|
||||
use std::fs::{self, FileType, Metadata};
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::vec;
|
||||
@@ -182,14 +182,14 @@ impl DirEntryInner {
|
||||
match *self {
|
||||
Stdin => {
|
||||
let err = Error::Io(io::Error::new(
|
||||
io::ErrorKind::Other, "<stdin> has no metadata"));
|
||||
io::ErrorKind::Other,
|
||||
"<stdin> has no metadata",
|
||||
));
|
||||
Err(err.with_path("<stdin>"))
|
||||
}
|
||||
Walkdir(ref x) => {
|
||||
x.metadata().map_err(|err| {
|
||||
Error::Io(io::Error::from(err)).with_path(x.path())
|
||||
})
|
||||
}
|
||||
Walkdir(ref x) => x
|
||||
.metadata()
|
||||
.map_err(|err| Error::Io(io::Error::from(err)).with_path(x.path())),
|
||||
Raw(ref x) => x.metadata(),
|
||||
}
|
||||
}
|
||||
@@ -223,8 +223,8 @@ impl DirEntryInner {
|
||||
|
||||
#[cfg(unix)]
|
||||
fn ino(&self) -> Option<u64> {
|
||||
use walkdir::DirEntryExt;
|
||||
use self::DirEntryInner::*;
|
||||
use walkdir::DirEntryExt;
|
||||
match *self {
|
||||
Stdin => None,
|
||||
Walkdir(ref x) => Some(x.ino()),
|
||||
@@ -297,7 +297,8 @@ impl DirEntryRaw {
|
||||
fs::metadata(&self.path)
|
||||
} else {
|
||||
Ok(self.metadata.clone())
|
||||
}.map_err(|err| Error::Io(io::Error::from(err)).with_path(&self.path))
|
||||
}
|
||||
.map_err(|err| Error::Io(io::Error::from(err)).with_path(&self.path))
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
@@ -306,7 +307,8 @@ impl DirEntryRaw {
|
||||
fs::metadata(&self.path)
|
||||
} else {
|
||||
fs::symlink_metadata(&self.path)
|
||||
}.map_err(|err| Error::Io(io::Error::from(err)).with_path(&self.path))
|
||||
}
|
||||
.map_err(|err| Error::Io(io::Error::from(err)).with_path(&self.path))
|
||||
}
|
||||
|
||||
fn file_type(&self) -> FileType {
|
||||
@@ -314,7 +316,9 @@ impl DirEntryRaw {
|
||||
}
|
||||
|
||||
fn file_name(&self) -> &OsStr {
|
||||
self.path.file_name().unwrap_or_else(|| self.path.as_os_str())
|
||||
self.path
|
||||
.file_name()
|
||||
.unwrap_or_else(|| self.path.as_os_str())
|
||||
}
|
||||
|
||||
fn depth(&self) -> usize {
|
||||
@@ -326,10 +330,7 @@ impl DirEntryRaw {
|
||||
self.ino
|
||||
}
|
||||
|
||||
fn from_entry(
|
||||
depth: usize,
|
||||
ent: &fs::DirEntry,
|
||||
) -> Result<DirEntryRaw, Error> {
|
||||
fn from_entry(depth: usize, ent: &fs::DirEntry) -> Result<DirEntryRaw, Error> {
|
||||
let ty = ent.file_type().map_err(|err| {
|
||||
let err = Error::Io(io::Error::from(err)).with_path(ent.path());
|
||||
Error::WithDepth {
|
||||
@@ -379,15 +380,22 @@ impl DirEntryRaw {
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn from_path(
|
||||
// Placeholder implementation to allow compiling on non-standard platforms (e.g. wasm32).
|
||||
#[cfg(not(any(windows, unix)))]
|
||||
fn from_entry_os(
|
||||
depth: usize,
|
||||
pb: PathBuf,
|
||||
link: bool,
|
||||
ent: &fs::DirEntry,
|
||||
ty: fs::FileType,
|
||||
) -> Result<DirEntryRaw, Error> {
|
||||
let md = fs::metadata(&pb).map_err(|err| {
|
||||
Error::Io(err).with_path(&pb)
|
||||
})?;
|
||||
Err(Error::Io(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"unsupported platform",
|
||||
)))
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn from_path(depth: usize, pb: PathBuf, link: bool) -> Result<DirEntryRaw, Error> {
|
||||
let md = fs::metadata(&pb).map_err(|err| Error::Io(err).with_path(&pb))?;
|
||||
Ok(DirEntryRaw {
|
||||
path: pb,
|
||||
ty: md.file_type(),
|
||||
@@ -398,16 +406,10 @@ impl DirEntryRaw {
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn from_path(
|
||||
depth: usize,
|
||||
pb: PathBuf,
|
||||
link: bool,
|
||||
) -> Result<DirEntryRaw, Error> {
|
||||
fn from_path(depth: usize, pb: PathBuf, link: bool) -> Result<DirEntryRaw, Error> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
|
||||
let md = fs::metadata(&pb).map_err(|err| {
|
||||
Error::Io(err).with_path(&pb)
|
||||
})?;
|
||||
let md = fs::metadata(&pb).map_err(|err| Error::Io(err).with_path(&pb))?;
|
||||
Ok(DirEntryRaw {
|
||||
path: pb,
|
||||
ty: md.file_type(),
|
||||
@@ -416,6 +418,15 @@ impl DirEntryRaw {
|
||||
ino: md.ino(),
|
||||
})
|
||||
}
|
||||
|
||||
// Placeholder implementation to allow compiling on non-standard platforms (e.g. wasm32).
|
||||
#[cfg(not(any(windows, unix)))]
|
||||
fn from_path(depth: usize, pb: PathBuf, link: bool) -> Result<DirEntryRaw, Error> {
|
||||
Err(Error::Io(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"unsupported platform",
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// WalkBuilder builds a recursive directory iterator.
|
||||
@@ -481,8 +492,8 @@ pub struct WalkBuilder {
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Sorter {
|
||||
ByName(Arc<Fn(&OsStr, &OsStr) -> cmp::Ordering + Send + Sync + 'static>),
|
||||
ByPath(Arc<Fn(&Path, &Path) -> cmp::Ordering + Send + Sync + 'static>),
|
||||
ByName(Arc<dyn Fn(&OsStr, &OsStr) -> cmp::Ordering + Send + Sync + 'static>),
|
||||
ByPath(Arc<dyn Fn(&Path, &Path) -> cmp::Ordering + Send + Sync + 'static>),
|
||||
}
|
||||
|
||||
impl fmt::Debug for WalkBuilder {
|
||||
@@ -525,33 +536,34 @@ impl WalkBuilder {
|
||||
let follow_links = self.follow_links;
|
||||
let max_depth = self.max_depth;
|
||||
let sorter = self.sorter.clone();
|
||||
let its = self.paths.iter().map(move |p| {
|
||||
if p == Path::new("-") {
|
||||
(p.to_path_buf(), None)
|
||||
} else {
|
||||
let mut wd = WalkDir::new(p);
|
||||
wd = wd.follow_links(follow_links || p.is_file());
|
||||
wd = wd.same_file_system(self.same_file_system);
|
||||
if let Some(max_depth) = max_depth {
|
||||
wd = wd.max_depth(max_depth);
|
||||
}
|
||||
if let Some(ref sorter) = sorter {
|
||||
match sorter.clone() {
|
||||
Sorter::ByName(cmp) => {
|
||||
wd = wd.sort_by(move |a, b| {
|
||||
cmp(a.file_name(), b.file_name())
|
||||
});
|
||||
}
|
||||
Sorter::ByPath(cmp) => {
|
||||
wd = wd.sort_by(move |a, b| {
|
||||
cmp(a.path(), b.path())
|
||||
});
|
||||
let its = self
|
||||
.paths
|
||||
.iter()
|
||||
.map(move |p| {
|
||||
if p == Path::new("-") {
|
||||
(p.to_path_buf(), None)
|
||||
} else {
|
||||
let mut wd = WalkDir::new(p);
|
||||
wd = wd.follow_links(follow_links || p.is_file());
|
||||
wd = wd.same_file_system(self.same_file_system);
|
||||
if let Some(max_depth) = max_depth {
|
||||
wd = wd.max_depth(max_depth);
|
||||
}
|
||||
if let Some(ref sorter) = sorter {
|
||||
match sorter.clone() {
|
||||
Sorter::ByName(cmp) => {
|
||||
wd = wd.sort_by(move |a, b| cmp(a.file_name(), b.file_name()));
|
||||
}
|
||||
Sorter::ByPath(cmp) => {
|
||||
wd = wd.sort_by(move |a, b| cmp(a.path(), b.path()));
|
||||
}
|
||||
}
|
||||
}
|
||||
(p.to_path_buf(), Some(WalkEventIter::from(wd)))
|
||||
}
|
||||
(p.to_path_buf(), Some(WalkEventIter::from(wd)))
|
||||
}
|
||||
}).collect::<Vec<_>>().into_iter();
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter();
|
||||
let ig_root = self.ig_builder.build();
|
||||
Walk {
|
||||
its: its,
|
||||
@@ -635,8 +647,12 @@ impl WalkBuilder {
|
||||
let mut errs = PartialErrorBuilder::default();
|
||||
errs.maybe_push(builder.add(path));
|
||||
match builder.build() {
|
||||
Ok(gi) => { self.ig_builder.add_ignore(gi); }
|
||||
Err(err) => { errs.push(err); }
|
||||
Ok(gi) => {
|
||||
self.ig_builder.add_ignore(gi);
|
||||
}
|
||||
Err(err) => {
|
||||
errs.push(err);
|
||||
}
|
||||
}
|
||||
errs.into_error_option()
|
||||
}
|
||||
@@ -649,7 +665,7 @@ impl WalkBuilder {
|
||||
/// later names.
|
||||
pub fn add_custom_ignore_filename<S: AsRef<OsStr>>(
|
||||
&mut self,
|
||||
file_name: S
|
||||
file_name: S,
|
||||
) -> &mut WalkBuilder {
|
||||
self.ig_builder.add_custom_ignore_filename(file_name);
|
||||
self
|
||||
@@ -786,11 +802,9 @@ impl WalkBuilder {
|
||||
/// by `sort_by_file_name`.
|
||||
///
|
||||
/// Note that this is not used in the parallel iterator.
|
||||
pub fn sort_by_file_path<F>(
|
||||
&mut self,
|
||||
cmp: F,
|
||||
) -> &mut WalkBuilder
|
||||
where F: Fn(&Path, &Path) -> cmp::Ordering + Send + Sync + 'static
|
||||
pub fn sort_by_file_path<F>(&mut self, cmp: F) -> &mut WalkBuilder
|
||||
where
|
||||
F: Fn(&Path, &Path) -> cmp::Ordering + Send + Sync + 'static,
|
||||
{
|
||||
self.sorter = Some(Sorter::ByPath(Arc::new(cmp)));
|
||||
self
|
||||
@@ -808,7 +822,8 @@ impl WalkBuilder {
|
||||
///
|
||||
/// Note that this is not used in the parallel iterator.
|
||||
pub fn sort_by_file_name<F>(&mut self, cmp: F) -> &mut WalkBuilder
|
||||
where F: Fn(&OsStr, &OsStr) -> cmp::Ordering + Send + Sync + 'static
|
||||
where
|
||||
F: Fn(&OsStr, &OsStr) -> cmp::Ordering + Send + Sync + 'static,
|
||||
{
|
||||
self.sorter = Some(Sorter::ByName(Arc::new(cmp)));
|
||||
self
|
||||
@@ -989,7 +1004,11 @@ enum WalkEvent {
|
||||
|
||||
impl From<WalkDir> for WalkEventIter {
|
||||
fn from(it: WalkDir) -> WalkEventIter {
|
||||
WalkEventIter { depth: 0, it: it.into_iter(), next: None }
|
||||
WalkEventIter {
|
||||
depth: 0,
|
||||
it: it.into_iter(),
|
||||
next: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1072,10 +1091,10 @@ impl WalkParallel {
|
||||
/// Execute the parallel recursive directory iterator. `mkf` is called
|
||||
/// for each thread used for iteration. The function produced by `mkf`
|
||||
/// is then in turn called for each visited file path.
|
||||
pub fn run<F>(
|
||||
self,
|
||||
mut mkf: F,
|
||||
) where F: FnMut() -> Box<FnMut(Result<DirEntry, Error>) -> WalkState + Send + 'static> {
|
||||
pub fn run<F>(self, mut mkf: F)
|
||||
where
|
||||
F: FnMut() -> Box<dyn FnMut(Result<DirEntry, Error>) -> WalkState + Send + 'static>,
|
||||
{
|
||||
let mut f = mkf();
|
||||
let threads = self.threads();
|
||||
// TODO: Figure out how to use a bounded channel here. With an
|
||||
@@ -1092,30 +1111,16 @@ impl WalkParallel {
|
||||
// Note that we only send directories. For files, we send to them the
|
||||
// callback directly.
|
||||
for path in self.paths {
|
||||
let (dent, root_device) =
|
||||
if path == Path::new("-") {
|
||||
(DirEntry::new_stdin(), None)
|
||||
let (dent, root_device) = if path == Path::new("-") {
|
||||
(DirEntry::new_stdin(), None)
|
||||
} else {
|
||||
let root_device = if !self.same_file_system {
|
||||
None
|
||||
} else {
|
||||
let root_device =
|
||||
if !self.same_file_system {
|
||||
None
|
||||
} else {
|
||||
match device_num(&path) {
|
||||
Ok(root_device) => Some(root_device),
|
||||
Err(err) => {
|
||||
let err = Error::Io(err).with_path(path);
|
||||
if f(Err(err)).is_quit() {
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
match DirEntryRaw::from_path(0, path, false) {
|
||||
Ok(dent) => {
|
||||
(DirEntry::new_raw(dent, None), root_device)
|
||||
}
|
||||
match device_num(&path) {
|
||||
Ok(root_device) => Some(root_device),
|
||||
Err(err) => {
|
||||
let err = Error::Io(err).with_path(path);
|
||||
if f(Err(err)).is_quit() {
|
||||
return;
|
||||
}
|
||||
@@ -1123,11 +1128,22 @@ impl WalkParallel {
|
||||
}
|
||||
}
|
||||
};
|
||||
match DirEntryRaw::from_path(0, path, false) {
|
||||
Ok(dent) => (DirEntry::new_raw(dent, None), root_device),
|
||||
Err(err) => {
|
||||
if f(Err(err)).is_quit() {
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
tx.send(Message::Work(Work {
|
||||
dent: dent,
|
||||
ignore: self.ig_root.clone(),
|
||||
root_device: root_device,
|
||||
})).unwrap();
|
||||
}))
|
||||
.unwrap();
|
||||
any_work = true;
|
||||
}
|
||||
// ... but there's no need to start workers if we don't need them.
|
||||
@@ -1253,7 +1269,7 @@ impl Work {
|
||||
/// Note that a worker is *both* a producer and a consumer.
|
||||
struct Worker {
|
||||
/// The caller's callback.
|
||||
f: Box<FnMut(Result<DirEntry, Error>) -> WalkState + Send + 'static>,
|
||||
f: Box<dyn FnMut(Result<DirEntry, Error>) -> WalkState + Send + 'static>,
|
||||
/// The push side of our mpmc queue.
|
||||
tx: channel::Sender<Message>,
|
||||
/// The receive side of our mpmc queue.
|
||||
@@ -1319,22 +1335,21 @@ impl Worker {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let descend =
|
||||
if let Some(root_device) = work.root_device {
|
||||
match is_same_file_system(root_device, work.dent.path()) {
|
||||
Ok(true) => true,
|
||||
Ok(false) => false,
|
||||
Err(err) => {
|
||||
if (self.f)(Err(err)).is_quit() {
|
||||
self.quit_now();
|
||||
return;
|
||||
}
|
||||
false
|
||||
let descend = if let Some(root_device) = work.root_device {
|
||||
match is_same_file_system(root_device, work.dent.path()) {
|
||||
Ok(true) => true,
|
||||
Ok(false) => false,
|
||||
Err(err) => {
|
||||
if (self.f)(Err(err)).is_quit() {
|
||||
self.quit_now();
|
||||
return;
|
||||
}
|
||||
false
|
||||
}
|
||||
} else {
|
||||
true
|
||||
};
|
||||
}
|
||||
} else {
|
||||
true
|
||||
};
|
||||
|
||||
let depth = work.dent.depth();
|
||||
match (self.f)(Ok(work.dent)) {
|
||||
@@ -1352,12 +1367,7 @@ impl Worker {
|
||||
continue;
|
||||
}
|
||||
for result in readdir {
|
||||
let state = self.run_one(
|
||||
&work.ignore,
|
||||
depth + 1,
|
||||
work.root_device,
|
||||
result,
|
||||
);
|
||||
let state = self.run_one(&work.ignore, depth + 1, work.root_device, result);
|
||||
if state.is_quit() {
|
||||
self.quit_now();
|
||||
return;
|
||||
@@ -1422,23 +1432,24 @@ impl Worker {
|
||||
}
|
||||
}
|
||||
let should_skip_path = should_skip_entry(ig, &dent);
|
||||
let should_skip_filesize =
|
||||
if self.max_filesize.is_some() && !dent.is_dir() {
|
||||
skip_filesize(
|
||||
self.max_filesize.unwrap(),
|
||||
dent.path(),
|
||||
&dent.metadata().ok(),
|
||||
)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let should_skip_filesize = if self.max_filesize.is_some() && !dent.is_dir() {
|
||||
skip_filesize(
|
||||
self.max_filesize.unwrap(),
|
||||
dent.path(),
|
||||
&dent.metadata().ok(),
|
||||
)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if !should_skip_path && !should_skip_filesize {
|
||||
self.tx.send(Message::Work(Work {
|
||||
dent: dent,
|
||||
ignore: ig.clone(),
|
||||
root_device: root_device,
|
||||
})).unwrap();
|
||||
self.tx
|
||||
.send(Message::Work(Work {
|
||||
dent: dent,
|
||||
ignore: ig.clone(),
|
||||
root_device: root_device,
|
||||
}))
|
||||
.unwrap();
|
||||
}
|
||||
WalkState::Continue
|
||||
}
|
||||
@@ -1568,17 +1579,25 @@ fn check_symlink_loop(
|
||||
child_depth: usize,
|
||||
) -> Result<(), Error> {
|
||||
let hchild = Handle::from_path(child_path).map_err(|err| {
|
||||
Error::from(err).with_path(child_path).with_depth(child_depth)
|
||||
Error::from(err)
|
||||
.with_path(child_path)
|
||||
.with_depth(child_depth)
|
||||
})?;
|
||||
for ig in ig_parent.parents().take_while(|ig| !ig.is_absolute_parent()) {
|
||||
for ig in ig_parent
|
||||
.parents()
|
||||
.take_while(|ig| !ig.is_absolute_parent())
|
||||
{
|
||||
let h = Handle::from_path(ig.path()).map_err(|err| {
|
||||
Error::from(err).with_path(child_path).with_depth(child_depth)
|
||||
Error::from(err)
|
||||
.with_path(child_path)
|
||||
.with_depth(child_depth)
|
||||
})?;
|
||||
if hchild == h {
|
||||
return Err(Error::Loop {
|
||||
ancestor: ig.path().to_path_buf(),
|
||||
child: child_path.to_path_buf(),
|
||||
}.with_depth(child_depth));
|
||||
}
|
||||
.with_depth(child_depth));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -1586,14 +1605,10 @@ fn check_symlink_loop(
|
||||
|
||||
// Before calling this function, make sure that you ensure that is really
|
||||
// necessary as the arguments imply a file stat.
|
||||
fn skip_filesize(
|
||||
max_filesize: u64,
|
||||
path: &Path,
|
||||
ent: &Option<Metadata>
|
||||
) -> bool {
|
||||
fn skip_filesize(max_filesize: u64, path: &Path, ent: &Option<Metadata>) -> bool {
|
||||
let filesize = match *ent {
|
||||
Some(ref md) => Some(md.len()),
|
||||
None => None
|
||||
None => None,
|
||||
};
|
||||
|
||||
if let Some(fs) = filesize {
|
||||
@@ -1608,10 +1623,7 @@ fn skip_filesize(
|
||||
}
|
||||
}
|
||||
|
||||
fn should_skip_entry(
|
||||
ig: &Ignore,
|
||||
dent: &DirEntry,
|
||||
) -> bool {
|
||||
fn should_skip_entry(ig: &Ignore, dent: &DirEntry) -> bool {
|
||||
let m = ig.matched_dir_entry(dent);
|
||||
if m.is_ignore() {
|
||||
debug!("ignoring {}: {:?}", dent.path().display(), m);
|
||||
@@ -1673,28 +1685,27 @@ fn path_equals(dent: &DirEntry, handle: &Handle) -> Result<bool, Error> {
|
||||
/// Returns true if and only if the given path is on the same device as the
|
||||
/// given root device.
|
||||
fn is_same_file_system(root_device: u64, path: &Path) -> Result<bool, Error> {
|
||||
let dent_device = device_num(path)
|
||||
.map_err(|err| Error::Io(err).with_path(path))?;
|
||||
let dent_device = device_num(path).map_err(|err| Error::Io(err).with_path(path))?;
|
||||
Ok(root_device == dent_device)
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn device_num<P: AsRef<Path>>(path: P)-> io::Result<u64> {
|
||||
fn device_num<P: AsRef<Path>>(path: P) -> io::Result<u64> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
|
||||
path.as_ref().metadata().map(|md| md.dev())
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg(windows)]
|
||||
fn device_num<P: AsRef<Path>>(path: P) -> io::Result<u64> {
|
||||
use winapi_util::{Handle, file};
|
||||
use winapi_util::{file, Handle};
|
||||
|
||||
let h = Handle::from_path_any(path)?;
|
||||
file::information(h).map(|info| info.volume_serial_number())
|
||||
}
|
||||
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
fn device_num<P: AsRef<Path>>(_: P)-> io::Result<u64> {
|
||||
fn device_num<P: AsRef<Path>>(_: P) -> io::Result<u64> {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"walkdir: same_file_system option not supported on this platform",
|
||||
@@ -1708,9 +1719,8 @@ mod tests {
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use tempfile::{self, TempDir};
|
||||
|
||||
use super::{DirEntry, WalkBuilder, WalkState};
|
||||
use tests::TempDir;
|
||||
|
||||
fn wfile<P: AsRef<Path>>(path: P, contents: &str) {
|
||||
let mut file = File::create(path).unwrap();
|
||||
@@ -1757,10 +1767,7 @@ mod tests {
|
||||
paths
|
||||
}
|
||||
|
||||
fn walk_collect_parallel(
|
||||
prefix: &Path,
|
||||
builder: &WalkBuilder,
|
||||
) -> Vec<String> {
|
||||
fn walk_collect_parallel(prefix: &Path, builder: &WalkBuilder) -> Vec<String> {
|
||||
let mut paths = vec![];
|
||||
for dent in walk_collect_entries_parallel(builder) {
|
||||
let path = dent.path().strip_prefix(prefix).unwrap();
|
||||
@@ -1795,15 +1802,11 @@ mod tests {
|
||||
paths
|
||||
}
|
||||
|
||||
fn tmpdir(prefix: &str) -> TempDir {
|
||||
tempfile::Builder::new().prefix(prefix).tempdir().unwrap()
|
||||
fn tmpdir() -> TempDir {
|
||||
TempDir::new().unwrap()
|
||||
}
|
||||
|
||||
fn assert_paths(
|
||||
prefix: &Path,
|
||||
builder: &WalkBuilder,
|
||||
expected: &[&str],
|
||||
) {
|
||||
fn assert_paths(prefix: &Path, builder: &WalkBuilder, expected: &[&str]) {
|
||||
let got = walk_collect(prefix, builder);
|
||||
assert_eq!(got, mkpaths(expected), "single threaded");
|
||||
let got = walk_collect_parallel(prefix, builder);
|
||||
@@ -1812,20 +1815,22 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn no_ignores() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join("a/b/c"));
|
||||
mkdirp(td.path().join("x/y"));
|
||||
wfile(td.path().join("a/b/foo"), "");
|
||||
wfile(td.path().join("x/y/foo"), "");
|
||||
|
||||
assert_paths(td.path(), &WalkBuilder::new(td.path()), &[
|
||||
"x", "x/y", "x/y/foo", "a", "a/b", "a/b/foo", "a/b/c",
|
||||
]);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
&WalkBuilder::new(td.path()),
|
||||
&["x", "x/y", "x/y/foo", "a", "a/b", "a/b/foo", "a/b/c"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_ignore() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
let custom_ignore = ".customignore";
|
||||
mkdirp(td.path().join("a"));
|
||||
wfile(td.path().join(custom_ignore), "foo");
|
||||
@@ -1841,7 +1846,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn custom_ignore_exclusive_use() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
let custom_ignore = ".customignore";
|
||||
mkdirp(td.path().join("a"));
|
||||
wfile(td.path().join(custom_ignore), "foo");
|
||||
@@ -1861,7 +1866,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn gitignore() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join(".git"));
|
||||
mkdirp(td.path().join("a"));
|
||||
wfile(td.path().join(".gitignore"), "foo");
|
||||
@@ -1870,14 +1875,16 @@ mod tests {
|
||||
wfile(td.path().join("bar"), "");
|
||||
wfile(td.path().join("a/bar"), "");
|
||||
|
||||
assert_paths(td.path(), &WalkBuilder::new(td.path()), &[
|
||||
"bar", "a", "a/bar",
|
||||
]);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
&WalkBuilder::new(td.path()),
|
||||
&["bar", "a", "a/bar"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_ignore() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
let igpath = td.path().join(".not-an-ignore");
|
||||
mkdirp(td.path().join("a"));
|
||||
wfile(&igpath, "foo");
|
||||
@@ -1893,7 +1900,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn explicit_ignore_exclusive_use() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
let igpath = td.path().join(".not-an-ignore");
|
||||
mkdirp(td.path().join("a"));
|
||||
wfile(&igpath, "foo");
|
||||
@@ -1905,13 +1912,16 @@ mod tests {
|
||||
let mut builder = WalkBuilder::new(td.path());
|
||||
builder.standard_filters(false);
|
||||
assert!(builder.add_ignore(&igpath).is_none());
|
||||
assert_paths(td.path(), &builder,
|
||||
&[".not-an-ignore", "bar", "a", "a/bar"]);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
&builder,
|
||||
&[".not-an-ignore", "bar", "a", "a/bar"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gitignore_parent() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join(".git"));
|
||||
mkdirp(td.path().join("a"));
|
||||
wfile(td.path().join(".gitignore"), "foo");
|
||||
@@ -1924,7 +1934,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn max_depth() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join("a/b/c"));
|
||||
wfile(td.path().join("foo"), "");
|
||||
wfile(td.path().join("a/foo"), "");
|
||||
@@ -1932,19 +1942,23 @@ mod tests {
|
||||
wfile(td.path().join("a/b/c/foo"), "");
|
||||
|
||||
let mut builder = WalkBuilder::new(td.path());
|
||||
assert_paths(td.path(), &builder, &[
|
||||
"a", "a/b", "a/b/c", "foo", "a/foo", "a/b/foo", "a/b/c/foo",
|
||||
]);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
&builder,
|
||||
&["a", "a/b", "a/b/c", "foo", "a/foo", "a/b/foo", "a/b/c/foo"],
|
||||
);
|
||||
assert_paths(td.path(), builder.max_depth(Some(0)), &[]);
|
||||
assert_paths(td.path(), builder.max_depth(Some(1)), &["a", "foo"]);
|
||||
assert_paths(td.path(), builder.max_depth(Some(2)), &[
|
||||
"a", "a/b", "foo", "a/foo",
|
||||
]);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
builder.max_depth(Some(2)),
|
||||
&["a", "a/b", "foo", "a/foo"],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn max_filesize() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join("a/b"));
|
||||
wfile_size(td.path().join("foo"), 0);
|
||||
wfile_size(td.path().join("bar"), 400);
|
||||
@@ -1954,41 +1968,49 @@ mod tests {
|
||||
wfile_size(td.path().join("a/baz"), 200);
|
||||
|
||||
let mut builder = WalkBuilder::new(td.path());
|
||||
assert_paths(td.path(), &builder, &[
|
||||
"a", "a/b", "foo", "bar", "baz", "a/foo", "a/bar", "a/baz",
|
||||
]);
|
||||
assert_paths(td.path(), builder.max_filesize(Some(0)), &[
|
||||
"a", "a/b", "foo"
|
||||
]);
|
||||
assert_paths(td.path(), builder.max_filesize(Some(500)), &[
|
||||
"a", "a/b", "foo", "bar", "a/bar", "a/baz"
|
||||
]);
|
||||
assert_paths(td.path(), builder.max_filesize(Some(50000)), &[
|
||||
"a", "a/b", "foo", "bar", "baz", "a/foo", "a/bar", "a/baz",
|
||||
]);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
&builder,
|
||||
&["a", "a/b", "foo", "bar", "baz", "a/foo", "a/bar", "a/baz"],
|
||||
);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
builder.max_filesize(Some(0)),
|
||||
&["a", "a/b", "foo"],
|
||||
);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
builder.max_filesize(Some(500)),
|
||||
&["a", "a/b", "foo", "bar", "a/bar", "a/baz"],
|
||||
);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
builder.max_filesize(Some(50000)),
|
||||
&["a", "a/b", "foo", "bar", "baz", "a/foo", "a/bar", "a/baz"],
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)] // because symlinks on windows are weird
|
||||
#[test]
|
||||
fn symlinks() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join("a/b"));
|
||||
symlink(td.path().join("a/b"), td.path().join("z"));
|
||||
wfile(td.path().join("a/b/foo"), "");
|
||||
|
||||
let mut builder = WalkBuilder::new(td.path());
|
||||
assert_paths(td.path(), &builder, &[
|
||||
"a", "a/b", "a/b/foo", "z",
|
||||
]);
|
||||
assert_paths(td.path(), &builder.follow_links(true), &[
|
||||
"a", "a/b", "a/b/foo", "z", "z/foo",
|
||||
]);
|
||||
assert_paths(td.path(), &builder, &["a", "a/b", "a/b/foo", "z"]);
|
||||
assert_paths(
|
||||
td.path(),
|
||||
&builder.follow_links(true),
|
||||
&["a", "a/b", "a/b/foo", "z", "z/foo"],
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)] // because symlinks on windows are weird
|
||||
#[test]
|
||||
fn first_path_not_symlink() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join("foo"));
|
||||
|
||||
let dents = WalkBuilder::new(td.path().join("foo"))
|
||||
@@ -1999,9 +2021,7 @@ mod tests {
|
||||
assert_eq!(1, dents.len());
|
||||
assert!(!dents[0].path_is_symlink());
|
||||
|
||||
let dents = walk_collect_entries_parallel(
|
||||
&WalkBuilder::new(td.path().join("foo")),
|
||||
);
|
||||
let dents = walk_collect_entries_parallel(&WalkBuilder::new(td.path().join("foo")));
|
||||
assert_eq!(1, dents.len());
|
||||
assert!(!dents[0].path_is_symlink());
|
||||
}
|
||||
@@ -2009,17 +2029,13 @@ mod tests {
|
||||
#[cfg(unix)] // because symlinks on windows are weird
|
||||
#[test]
|
||||
fn symlink_loop() {
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
mkdirp(td.path().join("a/b"));
|
||||
symlink(td.path().join("a"), td.path().join("a/b/c"));
|
||||
|
||||
let mut builder = WalkBuilder::new(td.path());
|
||||
assert_paths(td.path(), &builder, &[
|
||||
"a", "a/b", "a/b/c",
|
||||
]);
|
||||
assert_paths(td.path(), &builder.follow_links(true), &[
|
||||
"a", "a/b",
|
||||
]);
|
||||
assert_paths(td.path(), &builder, &["a", "a/b", "a/b/c"]);
|
||||
assert_paths(td.path(), &builder.follow_links(true), &["a", "a/b"]);
|
||||
}
|
||||
|
||||
// It's a little tricky to test the 'same_file_system' option since
|
||||
@@ -2039,7 +2055,7 @@ mod tests {
|
||||
|
||||
// If our test directory actually isn't a different volume from /sys,
|
||||
// then this test is meaningless and we shouldn't run it.
|
||||
let td = tmpdir("walk-test-");
|
||||
let td = tmpdir();
|
||||
if device_num(td.path()).unwrap() == device_num("/sys").unwrap() {
|
||||
return;
|
||||
}
|
||||
@@ -2053,8 +2069,6 @@ mod tests {
|
||||
// completely.
|
||||
let mut builder = WalkBuilder::new(td.path());
|
||||
builder.follow_links(true).same_file_system(true);
|
||||
assert_paths(td.path(), &builder, &[
|
||||
"same_file", "same_file/alink",
|
||||
]);
|
||||
assert_paths(td.path(), &builder, &["same_file", "same_file/alink"]);
|
||||
}
|
||||
}
|
||||
|
@@ -1,14 +1,14 @@
|
||||
class RipgrepBin < Formula
|
||||
version '11.0.1'
|
||||
version '11.0.2'
|
||||
desc "Recursively search directories for a regex pattern."
|
||||
homepage "https://github.com/BurntSushi/ripgrep"
|
||||
|
||||
if OS.mac?
|
||||
url "https://github.com/BurntSushi/ripgrep/releases/download/#{version}/ripgrep-#{version}-x86_64-apple-darwin.tar.gz"
|
||||
sha256 "92446c6b28b6c726f91ad66a03bcd533fc6e1a28ef4b44c27bfe2d49a0f88531"
|
||||
sha256 "0ba26423691deedf2649b12b1abe3d2be294ee1cb17c40b68fe85efe194f4f57"
|
||||
elsif OS.linux?
|
||||
url "https://github.com/BurntSushi/ripgrep/releases/download/#{version}/ripgrep-#{version}-x86_64-unknown-linux-musl.tar.gz"
|
||||
sha256 "ce74cabac9b39b1ad55837ec01e2c670fa7e965772ac2647b209e31ead19008c"
|
||||
sha256 "2e7978e346553fbc45c0940d9fa11e12f9afbae8213b261aad19b698150e169a"
|
||||
end
|
||||
|
||||
conflicts_with "ripgrep"
|
||||
|
32
src/app.rs
32
src/app.rs
@@ -571,6 +571,7 @@ pub fn all_args_and_flags() -> Vec<RGArg> {
|
||||
flag_fixed_strings(&mut args);
|
||||
flag_follow(&mut args);
|
||||
flag_glob(&mut args);
|
||||
flag_glob_case_insensitive(&mut args);
|
||||
flag_heading(&mut args);
|
||||
flag_hidden(&mut args);
|
||||
flag_iglob(&mut args);
|
||||
@@ -1218,6 +1219,25 @@ it.
|
||||
args.push(arg);
|
||||
}
|
||||
|
||||
fn flag_glob_case_insensitive(args: &mut Vec<RGArg>) {
|
||||
const SHORT: &str = "Process all glob patterns case insensitively.";
|
||||
const LONG: &str = long!("\
|
||||
Process glob patterns given with the -g/--glob flag case insensitively. This
|
||||
effectively treats --glob as --iglob.
|
||||
|
||||
This flag can be disabled with the --no-glob-case-insensitive flag.
|
||||
");
|
||||
let arg = RGArg::switch("glob-case-insensitive")
|
||||
.help(SHORT).long_help(LONG)
|
||||
.overrides("no-glob-case-insensitive");
|
||||
args.push(arg);
|
||||
|
||||
let arg = RGArg::switch("no-glob-case-insensitive")
|
||||
.hidden()
|
||||
.overrides("glob-case-insensitive");
|
||||
args.push(arg);
|
||||
}
|
||||
|
||||
fn flag_heading(args: &mut Vec<RGArg>) {
|
||||
const SHORT: &str = "Print matches grouped by each file.";
|
||||
const LONG: &str = long!("\
|
||||
@@ -1975,7 +1995,12 @@ or backreferences.
|
||||
|
||||
Note that PCRE2 is an optional ripgrep feature. If PCRE2 wasn't included in
|
||||
your build of ripgrep, then using this flag will result in ripgrep printing
|
||||
an error message and exiting.
|
||||
an error message and exiting. PCRE2 may also have worse user experience in
|
||||
some cases, since it has fewer introspection APIs than ripgrep's default regex
|
||||
engine. For example, if you use a '\n' in a PCRE2 regex without the
|
||||
'-U/--multiline' flag, then ripgrep will silently fail to match anything
|
||||
instead of reporting an error immediately (like it does with the default
|
||||
regex engine).
|
||||
|
||||
Related flags: --no-pcre2-unicode
|
||||
|
||||
@@ -2171,7 +2196,10 @@ Replace every match with the text given when printing results. Neither this
|
||||
flag nor any other ripgrep flag will modify your files.
|
||||
|
||||
Capture group indices (e.g., $5) and names (e.g., $foo) are supported in the
|
||||
replacement string.
|
||||
replacement string. In shells such as Bash and zsh, you should wrap the
|
||||
pattern in single quotes instead of double quotes. Otherwise, capture group
|
||||
indices will be replaced by expanded shell variables which will most likely
|
||||
be empty.
|
||||
|
||||
Note that the replacement by default replaces each match, and NOT the entire
|
||||
line. To replace the entire line, you should match the entire line.
|
||||
|
189
src/args.rs
189
src/args.rs
@@ -36,7 +36,6 @@ use ignore::{Walk, WalkBuilder, WalkParallel};
|
||||
use log;
|
||||
use num_cpus;
|
||||
use regex;
|
||||
use snafu::{self, ResultExt};
|
||||
use termcolor::{
|
||||
WriteColor,
|
||||
BufferWriter, ColorChoice,
|
||||
@@ -44,7 +43,6 @@ use termcolor::{
|
||||
|
||||
use crate::app;
|
||||
use crate::config;
|
||||
use crate::err::{self, Result};
|
||||
use crate::logger::Logger;
|
||||
use crate::messages::{set_messages, set_ignore_messages};
|
||||
use crate::path_printer::{PathPrinter, PathPrinterBuilder};
|
||||
@@ -52,6 +50,7 @@ use crate::search::{
|
||||
PatternMatcher, Printer, SearchWorker, SearchWorkerBuilder,
|
||||
};
|
||||
use crate::subject::SubjectBuilder;
|
||||
use crate::Result;
|
||||
|
||||
/// The command that ripgrep should execute based on the command line
|
||||
/// configuration.
|
||||
@@ -142,7 +141,9 @@ impl Args {
|
||||
set_messages(!early_matches.is_present("no-messages"));
|
||||
set_ignore_messages(!early_matches.is_present("no-ignore-messages"));
|
||||
|
||||
Logger::init()?;
|
||||
if let Err(err) = Logger::init() {
|
||||
return Err(format!("failed to initialize logger: {}", err).into());
|
||||
}
|
||||
if early_matches.is_present("trace") {
|
||||
log::set_max_level(log::LevelFilter::Trace);
|
||||
} else if early_matches.is_present("debug") {
|
||||
@@ -421,22 +422,13 @@ impl SortBy {
|
||||
match self.kind {
|
||||
SortByKind::None | SortByKind::Path => {}
|
||||
SortByKind::LastModified => {
|
||||
env::current_exe()
|
||||
.and_then(|x| x.metadata())
|
||||
.and_then(|x| x.modified())
|
||||
.context(err::UnsupportedSortModified)?;
|
||||
env::current_exe()?.metadata()?.modified()?;
|
||||
}
|
||||
SortByKind::LastAccessed => {
|
||||
env::current_exe()
|
||||
.and_then(|x| x.metadata())
|
||||
.and_then(|x| x.accessed())
|
||||
.context(err::UnsupportedSortAccess)?;
|
||||
env::current_exe()?.metadata()?.accessed()?;
|
||||
}
|
||||
SortByKind::Created => {
|
||||
env::current_exe()
|
||||
.and_then(|x| x.metadata())
|
||||
.and_then(|x| x.created())
|
||||
.context(err::UnsupportedSortCreation)?;
|
||||
env::current_exe()?.metadata()?.created()?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -619,12 +611,21 @@ impl ArgMatches {
|
||||
Ok(matcher) => return Ok(PatternMatcher::PCRE2(matcher)),
|
||||
Err(err) => err,
|
||||
};
|
||||
err::Hybrid {
|
||||
rust_err: Box::new(rust_err),
|
||||
pcre_err: Box::new(pcre_err),
|
||||
}.fail()
|
||||
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,
|
||||
)))
|
||||
} else {
|
||||
Ok(PatternMatcher::RustRegex(self.matcher_rust(patterns)?))
|
||||
let matcher = match self.matcher_rust(patterns) {
|
||||
Ok(matcher) => matcher,
|
||||
Err(err) => {
|
||||
return Err(From::from(suggest_pcre2(err.to_string())));
|
||||
}
|
||||
};
|
||||
Ok(PatternMatcher::RustRegex(matcher))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -634,7 +635,11 @@ impl ArgMatches {
|
||||
/// then this returns an error.
|
||||
#[cfg(not(feature = "pcre2"))]
|
||||
fn matcher(&self, patterns: &[String]) -> Result<PatternMatcher> {
|
||||
snafu::ensure!(!self.is_present("pcre2"), err::PCRE2Unavailable);
|
||||
if self.is_present("pcre2") {
|
||||
return Err(From::from(
|
||||
"PCRE2 is not available in this build of ripgrep",
|
||||
));
|
||||
}
|
||||
let matcher = self.matcher_rust(patterns)?;
|
||||
Ok(PatternMatcher::RustRegex(matcher))
|
||||
}
|
||||
@@ -687,7 +692,10 @@ impl ArgMatches {
|
||||
} else {
|
||||
builder.build(&patterns.join("|"))
|
||||
};
|
||||
res.eager_context(err::RustRegex)
|
||||
match res {
|
||||
Ok(m) => Ok(m),
|
||||
Err(err) => Err(From::from(suggest_multiline(err.to_string()))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a matcher using PCRE2.
|
||||
@@ -729,7 +737,7 @@ impl ArgMatches {
|
||||
if self.is_present("crlf") {
|
||||
builder.crlf(true);
|
||||
}
|
||||
Ok(builder.build(&patterns.join("|")).context(err::PCRE2Regex)?)
|
||||
Ok(builder.build(&patterns.join("|"))?)
|
||||
}
|
||||
|
||||
/// Build a JSON printer that writes results to the given writer.
|
||||
@@ -979,7 +987,7 @@ impl ArgMatches {
|
||||
// Start with a default set of color specs.
|
||||
let mut specs = default_color_specs();
|
||||
for spec_str in self.values_of_lossy_vec("colors") {
|
||||
specs.push(spec_str.parse().context(err::InvalidColorSpec)?);
|
||||
specs.push(spec_str.parse()?);
|
||||
}
|
||||
Ok(ColorSpecs::new(&specs))
|
||||
}
|
||||
@@ -1070,8 +1078,7 @@ impl ArgMatches {
|
||||
return Ok(EncodingMode::Disabled);
|
||||
}
|
||||
|
||||
let enc = Encoding::new(&label).context(err::SearchConfig)?;
|
||||
Ok(EncodingMode::Some(enc))
|
||||
Ok(EncodingMode::Some(Encoding::new(&label)?))
|
||||
}
|
||||
|
||||
/// Return the file separator to use based on the CLI configuration.
|
||||
@@ -1260,17 +1267,20 @@ impl ArgMatches {
|
||||
|
||||
/// Builds the set of glob overrides from the command line flags.
|
||||
fn overrides(&self) -> Result<Override> {
|
||||
let cwd = env::current_dir().context(err::CurrentDir)?;
|
||||
let mut builder = OverrideBuilder::new(cwd);
|
||||
let mut builder = OverrideBuilder::new(env::current_dir()?);
|
||||
// Make all globs case insensitive with --glob-case-insensitive.
|
||||
if self.is_present("glob-case-insensitive") {
|
||||
builder.case_insensitive(true).unwrap();
|
||||
}
|
||||
for glob in self.values_of_lossy_vec("glob") {
|
||||
builder.add(&glob).context(err::InvalidGlobFlag)?;
|
||||
builder.add(&glob)?;
|
||||
}
|
||||
// This only enables case insensitivity for subsequent globs.
|
||||
builder.case_insensitive(true).unwrap();
|
||||
for glob in self.values_of_lossy_vec("iglob") {
|
||||
builder.add(&glob).context(err::InvalidIGlobFlag)?;
|
||||
builder.add(&glob)?;
|
||||
}
|
||||
Ok(builder.build().context(err::GlobBuild)?)
|
||||
Ok(builder.build()?)
|
||||
}
|
||||
|
||||
/// Return all file paths that ripgrep should search.
|
||||
@@ -1324,10 +1334,16 @@ impl ArgMatches {
|
||||
};
|
||||
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.",
|
||||
sep.len(),
|
||||
cli::escape(&sep),
|
||||
)))
|
||||
} else {
|
||||
snafu::ensure!(sep.len() == 1, err::InvalidPathSeparator {
|
||||
separator: sep.clone(),
|
||||
});
|
||||
Ok(Some(sep[0]))
|
||||
}
|
||||
}
|
||||
@@ -1374,17 +1390,17 @@ impl ArgMatches {
|
||||
}
|
||||
if let Some(paths) = self.values_of_os("file") {
|
||||
for path in paths {
|
||||
let some_pats =
|
||||
if path == "-" {
|
||||
cli::patterns_from_stdin()
|
||||
.context(err::ReadManyPatterns)?
|
||||
} else {
|
||||
cli::patterns_from_path(path)
|
||||
.context(err::ReadManyPatterns)?
|
||||
};
|
||||
pats.extend(
|
||||
some_pats.into_iter().map(|p| self.pattern_from_string(p))
|
||||
);
|
||||
if path == "-" {
|
||||
pats.extend(cli::patterns_from_stdin()?
|
||||
.into_iter()
|
||||
.map(|p| self.pattern_from_string(p))
|
||||
);
|
||||
} else {
|
||||
pats.extend(cli::patterns_from_path(path)?
|
||||
.into_iter()
|
||||
.map(|p| self.pattern_from_string(p))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(pats)
|
||||
@@ -1405,7 +1421,7 @@ impl ArgMatches {
|
||||
///
|
||||
/// If the pattern is not valid UTF-8, then an error is returned.
|
||||
fn pattern_from_os_str(&self, pat: &OsStr) -> Result<String> {
|
||||
let s = cli::pattern_from_os(pat).context(err::ReadOSPattern)?;
|
||||
let s = cli::pattern_from_os(pat)?;
|
||||
Ok(self.pattern_from_str(s))
|
||||
}
|
||||
|
||||
@@ -1463,12 +1479,11 @@ impl ArgMatches {
|
||||
/// flag. If no --pre-globs are available, then this always returns an
|
||||
/// empty set of globs.
|
||||
fn preprocessor_globs(&self) -> Result<Override> {
|
||||
let cwd = env::current_dir().context(err::CurrentDir)?;
|
||||
let mut builder = OverrideBuilder::new(cwd);
|
||||
let mut builder = OverrideBuilder::new(env::current_dir()?);
|
||||
for glob in self.values_of_lossy_vec("pre-glob") {
|
||||
builder.add(&glob).context(err::InvalidPreGlob)?;
|
||||
builder.add(&glob)?;
|
||||
}
|
||||
Ok(builder.build().context(err::PreGlobBuild)?)
|
||||
Ok(builder.build()?)
|
||||
}
|
||||
|
||||
/// Parse the regex-size-limit argument option into a byte count.
|
||||
@@ -1550,7 +1565,7 @@ impl ArgMatches {
|
||||
builder.clear(&ty);
|
||||
}
|
||||
for def in self.values_of_lossy_vec("type-add") {
|
||||
builder.add_def(&def).context(err::InvalidTypeDefinition)?;
|
||||
builder.add_def(&def)?;
|
||||
}
|
||||
for ty in self.values_of_lossy_vec("type") {
|
||||
builder.select(&ty);
|
||||
@@ -1558,7 +1573,7 @@ impl ArgMatches {
|
||||
for ty in self.values_of_lossy_vec("type-not") {
|
||||
builder.negate(&ty);
|
||||
}
|
||||
builder.build().eager_context(err::TypeDefinitionBuild)
|
||||
builder.build().map_err(From::from)
|
||||
}
|
||||
|
||||
/// Returns the number of times the `unrestricted` flag is provided.
|
||||
@@ -1579,10 +1594,11 @@ impl ArgMatches {
|
||||
if self.is_present("no-filename") {
|
||||
false
|
||||
} else {
|
||||
let path_stdin = Path::new("-");
|
||||
self.is_present("with-filename")
|
||||
|| self.is_present("vimgrep")
|
||||
|| paths.len() > 1
|
||||
|| paths.get(0).map_or(false, |p| p.is_dir())
|
||||
|| paths.get(0).map_or(false, |p| p != path_stdin && p.is_dir())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1617,11 +1633,7 @@ impl ArgMatches {
|
||||
fn usize_of(&self, name: &str) -> Result<Option<usize>> {
|
||||
match self.value_of_lossy(name) {
|
||||
None => Ok(None),
|
||||
Some(v) => {
|
||||
v.parse()
|
||||
.map(Some)
|
||||
.eager_context(err::InvalidNumber { flag: name.to_string() })
|
||||
}
|
||||
Some(v) => v.parse().map(Some).map_err(From::from),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1629,17 +1641,15 @@ impl ArgMatches {
|
||||
///
|
||||
/// If the aforementioned format is not recognized, then this returns an
|
||||
/// error.
|
||||
fn parse_human_readable_size(&self, name: &str) -> Result<Option<u64>> {
|
||||
match self.value_of_lossy(name) {
|
||||
None => Ok(None),
|
||||
Some(size) => {
|
||||
cli::parse_human_readable_size(&size)
|
||||
.map(Some)
|
||||
.eager_context(err::InvalidHumanSize {
|
||||
flag: name.to_string()
|
||||
})
|
||||
}
|
||||
}
|
||||
fn parse_human_readable_size(
|
||||
&self,
|
||||
arg_name: &str,
|
||||
) -> Result<Option<u64>> {
|
||||
let size = match self.value_of_lossy(arg_name) {
|
||||
None => return Ok(None),
|
||||
Some(size) => size,
|
||||
};
|
||||
Ok(Some(cli::parse_human_readable_size(&size)?))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1673,6 +1683,32 @@ impl ArgMatches {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
#[cfg(feature = "pcre2")]
|
||||
fn suggest_pcre2(msg: String) -> String {
|
||||
if !msg.contains("backreferences") && !msg.contains("look-around") {
|
||||
msg
|
||||
} else {
|
||||
format!("{}
|
||||
|
||||
Consider enabling PCRE2 with the --pcre2 flag, which can handle backreferences
|
||||
and look-around.", msg)
|
||||
}
|
||||
}
|
||||
|
||||
fn suggest_multiline(msg: String) -> String {
|
||||
if msg.contains("the literal") && msg.contains("not allowed") {
|
||||
format!("{}
|
||||
|
||||
Consider enabling multiline mode with the --multiline flag (or -U for short).
|
||||
When multiline mode is enabled, new line characters can be matched.", msg)
|
||||
} else {
|
||||
msg
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert the result of parsing a human readable file size to a `usize`,
|
||||
/// failing if the type does not fit.
|
||||
fn u64_to_usize(
|
||||
@@ -1685,14 +1721,11 @@ fn u64_to_usize(
|
||||
None => return Ok(None),
|
||||
Some(value) => value,
|
||||
};
|
||||
snafu::ensure!(
|
||||
value <= usize::MAX as u64,
|
||||
err::NumberTooBig {
|
||||
flag: arg_name.to_string(),
|
||||
limit: usize::MAX as u64,
|
||||
}
|
||||
);
|
||||
Ok(Some(value as usize))
|
||||
if value <= usize::MAX as u64 {
|
||||
Ok(Some(value as usize))
|
||||
} else {
|
||||
Err(From::from(format!("number too large for {}", arg_name)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds a comparator for sorting two files according to a system time
|
||||
@@ -1740,7 +1773,7 @@ where I: IntoIterator<Item=T>,
|
||||
Err(err) => err,
|
||||
};
|
||||
if err.use_stderr() {
|
||||
return Err(snafu::Context { error: err, context: err::Clap }.into());
|
||||
return Err(err.into());
|
||||
}
|
||||
// Explicitly ignore any error returned by write!. The most likely error
|
||||
// at this point is a broken pipe error, in which case, we want to ignore
|
||||
|
@@ -3,16 +3,16 @@
|
||||
// argument corresponds precisely to one shell argument.
|
||||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::ffi::OsString;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use bstr::io::BufReadExt;
|
||||
use bstr::{io::BufReadExt, ByteSlice};
|
||||
use log;
|
||||
use snafu::{self, ResultExt};
|
||||
|
||||
use crate::err::{self, Error, Result};
|
||||
use crate::Result;
|
||||
|
||||
/// Return a sequence of arguments derived from ripgrep rc configuration files.
|
||||
pub fn args() -> Vec<OsString> {
|
||||
@@ -55,11 +55,12 @@ 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<Error>)> {
|
||||
) -> Result<(Vec<OsString>, Vec<Box<dyn Error>>)> {
|
||||
let path = path.as_ref();
|
||||
let file = File::open(&path).context(err::ConfigIO { path })?;
|
||||
let res = parse_reader(file).context(err::ConfigIO { path })?;
|
||||
Ok(res)
|
||||
match File::open(&path) {
|
||||
Ok(file) => parse_reader(file),
|
||||
Err(err) => Err(From::from(format!("{}: {}", path.display(), err))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a single ripgrep rc file from the given reader.
|
||||
@@ -75,10 +76,10 @@ fn parse<P: AsRef<Path>>(
|
||||
/// in addition to successfully parsed arguments.
|
||||
fn parse_reader<R: io::Read>(
|
||||
rdr: R,
|
||||
) -> io::Result<(Vec<OsString>, Vec<Error>)> {
|
||||
) -> Result<(Vec<OsString>, Vec<Box<dyn Error>>)> {
|
||||
let bufrdr = io::BufReader::new(rdr);
|
||||
let (mut args, mut errs) = (vec![], vec![]);
|
||||
let mut line_number = 0u64;
|
||||
let mut line_number = 0;
|
||||
bufrdr.for_byte_line_with_terminator(|line| {
|
||||
line_number += 1;
|
||||
|
||||
@@ -91,11 +92,7 @@ fn parse_reader<R: io::Read>(
|
||||
args.push(osstr.to_os_string());
|
||||
}
|
||||
Err(err) => {
|
||||
let ctx = snafu::Context {
|
||||
error: err,
|
||||
context: err::ConfigInvalidUTF8 { line_number },
|
||||
};
|
||||
errs.push(ctx.into());
|
||||
errs.push(format!("{}: {}", line_number, err).into());
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
|
250
src/err.rs
250
src/err.rs
@@ -1,250 +0,0 @@
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::result;
|
||||
|
||||
use snafu::{Backtrace, Context, Snafu};
|
||||
|
||||
pub type Result<T> = result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
#[snafu(visibility(pub(crate)))]
|
||||
pub enum Error {
|
||||
#[snafu(display("I/O error: {}", source))]
|
||||
IO {
|
||||
backtrace: Backtrace,
|
||||
source: io::Error,
|
||||
},
|
||||
#[snafu(display(
|
||||
"failed to retrieve current working directory: {}", source,
|
||||
))]
|
||||
CurrentDir {
|
||||
backtrace: Backtrace,
|
||||
source: io::Error,
|
||||
},
|
||||
#[snafu(display("sorting by modified time is unsupported: {}", source))]
|
||||
UnsupportedSortModified {
|
||||
backtrace: Backtrace,
|
||||
source: io::Error,
|
||||
},
|
||||
#[snafu(display("sorting by access time is unsupported: {}", source))]
|
||||
UnsupportedSortAccess {
|
||||
backtrace: Backtrace,
|
||||
source: io::Error,
|
||||
},
|
||||
#[snafu(display("sorting by creation time is unsupported: {}", source))]
|
||||
UnsupportedSortCreation {
|
||||
backtrace: Backtrace,
|
||||
source: io::Error,
|
||||
},
|
||||
#[snafu(display(
|
||||
"I/O error parsing config in {}: {}",
|
||||
path.display(),
|
||||
source,
|
||||
))]
|
||||
ConfigIO {
|
||||
backtrace: Backtrace,
|
||||
source: io::Error,
|
||||
path: PathBuf,
|
||||
},
|
||||
#[snafu(display(
|
||||
"config parse error on line {}: {}",
|
||||
line_number,
|
||||
source,
|
||||
))]
|
||||
ConfigInvalidUTF8 {
|
||||
backtrace: Backtrace,
|
||||
source: bstr::Utf8Error,
|
||||
line_number: u64,
|
||||
},
|
||||
#[snafu(display("failed to initialize logger: {}", source))]
|
||||
LoggerInit {
|
||||
backtrace: Backtrace,
|
||||
source: log::SetLoggerError,
|
||||
},
|
||||
#[snafu(display("{}", source))]
|
||||
Clap {
|
||||
backtrace: Backtrace,
|
||||
source: clap::Error,
|
||||
},
|
||||
#[snafu(display("{}", source))]
|
||||
ReadManyPatterns {
|
||||
backtrace: Backtrace,
|
||||
source: io::Error,
|
||||
},
|
||||
#[snafu(display("{}", source))]
|
||||
ReadOSPattern {
|
||||
backtrace: Backtrace,
|
||||
source: grep::cli::InvalidPatternError,
|
||||
},
|
||||
#[snafu(display(
|
||||
"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.",
|
||||
separator.len(), grep::cli::escape(&separator),
|
||||
))]
|
||||
InvalidPathSeparator {
|
||||
backtrace: Backtrace,
|
||||
separator: Vec<u8>,
|
||||
},
|
||||
#[snafu(display("error parsing -g/--glob: {}", source))]
|
||||
InvalidGlobFlag {
|
||||
backtrace: Backtrace,
|
||||
source: ignore::Error,
|
||||
},
|
||||
#[snafu(display("error parsing --iglob: {}", source))]
|
||||
InvalidIGlobFlag {
|
||||
backtrace: Backtrace,
|
||||
source: ignore::Error,
|
||||
},
|
||||
#[snafu(display("error building --glob matcher: {}", source))]
|
||||
GlobBuild {
|
||||
backtrace: Backtrace,
|
||||
source: ignore::Error,
|
||||
},
|
||||
#[snafu(display("error parsing --pre-glob: {}", source))]
|
||||
InvalidPreGlob {
|
||||
backtrace: Backtrace,
|
||||
source: ignore::Error,
|
||||
},
|
||||
#[snafu(display("error building --pre-glob matcher: {}", source))]
|
||||
PreGlobBuild {
|
||||
backtrace: Backtrace,
|
||||
source: ignore::Error,
|
||||
},
|
||||
#[snafu(display("error parsing --type-add value: {}", source))]
|
||||
InvalidTypeDefinition {
|
||||
backtrace: Backtrace,
|
||||
source: ignore::Error,
|
||||
},
|
||||
#[snafu(display("error building type matcher: {}", source))]
|
||||
TypeDefinitionBuild {
|
||||
backtrace: Backtrace,
|
||||
source: ignore::Error,
|
||||
},
|
||||
#[snafu(display("failed to parse {} value as a number: {}", flag, source))]
|
||||
InvalidNumber {
|
||||
backtrace: Backtrace,
|
||||
source: std::num::ParseIntError,
|
||||
flag: String,
|
||||
},
|
||||
#[snafu(display(
|
||||
"failed to parse {} value as a file size: {}", flag, source,
|
||||
))]
|
||||
InvalidHumanSize {
|
||||
backtrace: Backtrace,
|
||||
source: grep::cli::ParseSizeError,
|
||||
flag: String,
|
||||
},
|
||||
#[snafu(display(
|
||||
"number given to {} is too large (limit is {})",
|
||||
flag, limit,
|
||||
))]
|
||||
NumberTooBig {
|
||||
backtrace: Backtrace,
|
||||
flag: String,
|
||||
limit: u64,
|
||||
},
|
||||
#[snafu(display("{}", source))]
|
||||
SearchConfig {
|
||||
backtrace: Backtrace,
|
||||
source: grep::searcher::ConfigError,
|
||||
},
|
||||
#[snafu(display("invalid --colors spec: {}", source))]
|
||||
InvalidColorSpec {
|
||||
backtrace: Backtrace,
|
||||
source: grep::printer::ColorError,
|
||||
},
|
||||
#[snafu(display("{}", suggest(source)))]
|
||||
RustRegex {
|
||||
backtrace: Backtrace,
|
||||
source: grep::regex::Error,
|
||||
},
|
||||
#[snafu(display(
|
||||
"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,
|
||||
))]
|
||||
Hybrid {
|
||||
backtrace: Backtrace,
|
||||
rust_err: Box<Error>,
|
||||
pcre_err: Box<Error>,
|
||||
},
|
||||
#[snafu(display("failed to write type definitions to stdout: {}", source))]
|
||||
WriteTypes {
|
||||
backtrace: Backtrace,
|
||||
source: io::Error,
|
||||
},
|
||||
#[cfg(feature = "pcre2")]
|
||||
#[snafu(display("{}", suggest(source)))]
|
||||
PCRE2Regex {
|
||||
backtrace: Backtrace,
|
||||
source: grep::pcre2::Error,
|
||||
},
|
||||
#[snafu(display("PCRE2 is not available in this build of ripgrep"))]
|
||||
PCRE2Unavailable {
|
||||
backtrace: Backtrace,
|
||||
},
|
||||
#[snafu(display("failed to write PCRE2 version to stdout: {}", source))]
|
||||
PCRE2Version {
|
||||
backtrace: Backtrace,
|
||||
source: io::Error,
|
||||
},
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Return true if and only if this corresponds to an I/O error generated
|
||||
/// by a broken pipe.
|
||||
pub fn is_broken_pipe(&self) -> bool {
|
||||
self.io_err().map_or(false, |e| e.kind() == io::ErrorKind::BrokenPipe)
|
||||
}
|
||||
|
||||
/// Return a reference to this error's underlying I/O error, if one exists.
|
||||
pub fn io_err(&self) -> Option<&io::Error> {
|
||||
match *self {
|
||||
Error::IO { ref source, .. } => Some(source),
|
||||
Error::CurrentDir { ref source, .. } => Some(source),
|
||||
Error::UnsupportedSortModified { ref source, .. } => Some(source),
|
||||
Error::UnsupportedSortAccess { ref source, .. } => Some(source),
|
||||
Error::UnsupportedSortCreation { ref source, .. } => Some(source),
|
||||
Error::ConfigIO { ref source, .. } => Some(source),
|
||||
Error::ReadManyPatterns { ref source, .. } => Some(source),
|
||||
Error::WriteTypes { ref source, .. } => Some(source),
|
||||
Error::PCRE2Version { ref source, .. } => Some(source),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Inspect an error's display string and look for potentially suggestions
|
||||
/// to give to an end user.
|
||||
///
|
||||
/// These include:
|
||||
///
|
||||
/// 1. If the error results from the use of a new line literal, then return a
|
||||
/// new message suggesting the use of the -U/--multiline flag.
|
||||
/// 2. If the error correspond to a syntax error that PCRE2 could handle, then
|
||||
/// add a message to suggest the use of -P/--pcre2.
|
||||
fn suggest<E: std::error::Error>(err: &E) -> String {
|
||||
let msg = err.to_string();
|
||||
if msg.contains("the literal") && msg.contains("not allowed") {
|
||||
format!("{}
|
||||
|
||||
Consider enabling multiline mode with the --multiline flag (or -U for short).
|
||||
When multiline mode is enabled, new line characters can be matched.", msg)
|
||||
} else if cfg!(feature = "pcre2") &&
|
||||
(msg.contains("backreferences") || msg.contains("look-around"))
|
||||
{
|
||||
format!("{}
|
||||
|
||||
Consider enabling PCRE2 with the --pcre2 flag, which can handle backreferences
|
||||
and look-around.", msg)
|
||||
} else {
|
||||
msg
|
||||
}
|
||||
}
|
@@ -4,9 +4,6 @@
|
||||
// for this functionality.
|
||||
|
||||
use log::{self, Log};
|
||||
use snafu::ResultExt;
|
||||
|
||||
use crate::err::{self, Result};
|
||||
|
||||
/// The simplest possible logger that logs to stderr.
|
||||
///
|
||||
@@ -21,8 +18,8 @@ impl Logger {
|
||||
/// Create a new logger that logs to stderr and initialize it as the
|
||||
/// global logger. If there was a problem setting the logger, then an
|
||||
/// error is returned.
|
||||
pub fn init() -> Result<()> {
|
||||
log::set_logger(LOGGER).eager_context(err::LoggerInit)
|
||||
pub fn init() -> Result<(), log::SetLoggerError> {
|
||||
log::set_logger(LOGGER)
|
||||
}
|
||||
}
|
||||
|
||||
|
46
src/main.rs
46
src/main.rs
@@ -1,15 +1,12 @@
|
||||
#![allow(warnings)]
|
||||
|
||||
use std::error;
|
||||
use std::io::{self, Write};
|
||||
use std::process;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
|
||||
use ignore::WalkState;
|
||||
use snafu::ResultExt;
|
||||
|
||||
use args::Args;
|
||||
use err::Result;
|
||||
use subject::Subject;
|
||||
|
||||
#[macro_use]
|
||||
@@ -18,7 +15,6 @@ mod messages;
|
||||
mod app;
|
||||
mod args;
|
||||
mod config;
|
||||
mod err;
|
||||
mod logger;
|
||||
mod path_printer;
|
||||
mod search;
|
||||
@@ -47,8 +43,7 @@ mod subject;
|
||||
#[global_allocator]
|
||||
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
||||
|
||||
// type Result<T> = ::std::result::Result<T, Box<::std::error::Error>>;
|
||||
// type Result<T> =
|
||||
type Result<T> = ::std::result::Result<T, Box<dyn error::Error>>;
|
||||
|
||||
fn main() {
|
||||
if let Err(err) = Args::parse().and_then(try_main) {
|
||||
@@ -219,12 +214,12 @@ fn files(args: &Args) -> Result<bool> {
|
||||
}
|
||||
if let Err(err) = path_printer.write_path(subject.path()) {
|
||||
// A broken pipe means graceful termination.
|
||||
if err.is_broken_pipe() {
|
||||
if err.kind() == io::ErrorKind::BrokenPipe {
|
||||
break;
|
||||
}
|
||||
// Otherwise, we have some other error that's preventing us from
|
||||
// writing to stdout, so we should bubble it up.
|
||||
return Err(err);
|
||||
return Err(err.into());
|
||||
}
|
||||
}
|
||||
Ok(matched)
|
||||
@@ -245,7 +240,7 @@ fn files_parallel(args: &Args) -> Result<bool> {
|
||||
let matched = Arc::new(AtomicBool::new(false));
|
||||
let (tx, rx) = mpsc::channel::<Subject>();
|
||||
|
||||
let print_thread = thread::spawn(move || -> Result<()> {
|
||||
let print_thread = thread::spawn(move || -> io::Result<()> {
|
||||
for subject in rx.iter() {
|
||||
path_printer.write_path(subject.path())?;
|
||||
}
|
||||
@@ -277,8 +272,8 @@ fn files_parallel(args: &Args) -> Result<bool> {
|
||||
// A broken pipe means graceful termination, so fall through.
|
||||
// Otherwise, something bad happened while writing to stdout, so bubble
|
||||
// it up.
|
||||
if !err.is_broken_pipe() {
|
||||
return Err(err);
|
||||
if err.kind() != io::ErrorKind::BrokenPipe {
|
||||
return Err(err.into());
|
||||
}
|
||||
}
|
||||
Ok(matched.load(SeqCst))
|
||||
@@ -286,29 +281,22 @@ fn files_parallel(args: &Args) -> Result<bool> {
|
||||
|
||||
/// The top-level entry point for --type-list.
|
||||
fn types(args: &Args) -> Result<bool> {
|
||||
write_types(args.stdout(), &args.type_defs()?)
|
||||
.eager_context(err::WriteTypes)
|
||||
}
|
||||
|
||||
fn write_types<W: io::Write>(
|
||||
mut wtr: W,
|
||||
defs: &[ignore::types::FileTypeDef],
|
||||
) -> io::Result<bool> {
|
||||
let mut count = 0;
|
||||
for def in defs {
|
||||
let mut stdout = args.stdout();
|
||||
for def in args.type_defs()? {
|
||||
count += 1;
|
||||
wtr.write_all(def.name().as_bytes())?;
|
||||
wtr.write_all(b": ")?;
|
||||
stdout.write_all(def.name().as_bytes())?;
|
||||
stdout.write_all(b": ")?;
|
||||
|
||||
let mut first = true;
|
||||
for glob in def.globs() {
|
||||
if !first {
|
||||
wtr.write_all(b", ")?;
|
||||
stdout.write_all(b", ")?;
|
||||
}
|
||||
wtr.write_all(glob.as_bytes())?;
|
||||
stdout.write_all(glob.as_bytes())?;
|
||||
first = false;
|
||||
}
|
||||
wtr.write_all(b"\n")?;
|
||||
stdout.write_all(b"\n")?;
|
||||
}
|
||||
Ok(count > 0)
|
||||
}
|
||||
@@ -316,7 +304,7 @@ fn write_types<W: io::Write>(
|
||||
/// The top-level entry point for --pcre2-version.
|
||||
fn pcre2_version(args: &Args) -> Result<bool> {
|
||||
#[cfg(feature = "pcre2")]
|
||||
fn imp(args: &Args) -> io::Result<bool> {
|
||||
fn imp(args: &Args) -> Result<bool> {
|
||||
use grep::pcre2;
|
||||
|
||||
let mut stdout = args.stdout();
|
||||
@@ -331,11 +319,11 @@ fn pcre2_version(args: &Args) -> Result<bool> {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "pcre2"))]
|
||||
fn imp(args: &Args) -> io::Result<bool> {
|
||||
fn imp(args: &Args) -> Result<bool> {
|
||||
let mut stdout = args.stdout();
|
||||
writeln!(stdout, "PCRE2 is not available in this build of ripgrep.")?;
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
imp(args).eager_context(err::PCRE2Version)
|
||||
imp(args)
|
||||
}
|
||||
|
@@ -2,11 +2,8 @@ use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
use grep::printer::{ColorSpecs, PrinterPath};
|
||||
use snafu::ResultExt;
|
||||
use termcolor::WriteColor;
|
||||
|
||||
use crate::err::{self, Result};
|
||||
|
||||
/// A configuration for describing how paths should be written.
|
||||
#[derive(Clone, Debug)]
|
||||
struct Config {
|
||||
@@ -90,11 +87,7 @@ pub struct PathPrinter<W> {
|
||||
|
||||
impl<W: WriteColor> PathPrinter<W> {
|
||||
/// Write the given path to the underlying writer.
|
||||
pub fn write_path(&mut self, path: &Path) -> Result<()> {
|
||||
self.write(path).eager_context(err::IO)
|
||||
}
|
||||
|
||||
fn write(&mut self, path: &Path) -> io::Result<()> {
|
||||
pub fn write_path(&mut self, path: &Path) -> io::Result<()> {
|
||||
let ppath = PrinterPath::with_separator(path, self.config.separator);
|
||||
if !self.wtr.supports_color() {
|
||||
self.wtr.write_all(ppath.as_bytes())?;
|
||||
|
@@ -315,7 +315,24 @@ pub struct SearchWorker<W> {
|
||||
impl<W: WriteColor> SearchWorker<W> {
|
||||
/// Execute a search over the given subject.
|
||||
pub fn search(&mut self, subject: &Subject) -> io::Result<SearchResult> {
|
||||
self.search_impl(subject)
|
||||
let bin =
|
||||
if subject.is_explicit() {
|
||||
self.config.binary_explicit.clone()
|
||||
} else {
|
||||
self.config.binary_implicit.clone()
|
||||
};
|
||||
self.searcher.set_binary_detection(bin);
|
||||
|
||||
let path = subject.path();
|
||||
if subject.is_stdin() {
|
||||
self.search_reader(path, io::stdin().lock())
|
||||
} else if self.should_preprocess(path) {
|
||||
self.search_preprocessor(path)
|
||||
} else if self.should_decompress(path) {
|
||||
self.search_decompress(path)
|
||||
} else {
|
||||
self.search_path(path)
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the underlying printer.
|
||||
@@ -341,30 +358,6 @@ impl<W: WriteColor> SearchWorker<W> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Search the given subject using the appropriate strategy.
|
||||
fn search_impl(&mut self, subject: &Subject) -> io::Result<SearchResult> {
|
||||
let bin =
|
||||
if subject.is_explicit() {
|
||||
self.config.binary_explicit.clone()
|
||||
} else {
|
||||
self.config.binary_implicit.clone()
|
||||
};
|
||||
self.searcher.set_binary_detection(bin);
|
||||
|
||||
let path = subject.path();
|
||||
if subject.is_stdin() {
|
||||
let stdin = io::stdin();
|
||||
// A `return` here appeases the borrow checker. NLL will fix this.
|
||||
return self.search_reader(path, stdin.lock());
|
||||
} else if self.should_preprocess(path) {
|
||||
self.search_preprocessor(path)
|
||||
} else if self.should_decompress(path) {
|
||||
self.search_decompress(path)
|
||||
} else {
|
||||
self.search_path(path)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if and only if the given file path should be
|
||||
/// decompressed before searching.
|
||||
fn should_decompress(&self, path: &Path) -> bool {
|
||||
@@ -392,11 +385,23 @@ impl<W: WriteColor> SearchWorker<W> {
|
||||
&mut self,
|
||||
path: &Path,
|
||||
) -> io::Result<SearchResult> {
|
||||
let bin = self.config.preprocessor.clone().unwrap();
|
||||
let mut cmd = Command::new(&bin);
|
||||
let bin = self.config.preprocessor.as_ref().unwrap();
|
||||
let mut cmd = Command::new(bin);
|
||||
cmd.arg(path).stdin(Stdio::from(File::open(path)?));
|
||||
|
||||
let rdr = self.command_builder.build(&mut cmd)?;
|
||||
let rdr = self
|
||||
.command_builder
|
||||
.build(&mut cmd)
|
||||
.map_err(|err| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!(
|
||||
"preprocessor command could not start: '{:?}': {}",
|
||||
cmd,
|
||||
err,
|
||||
),
|
||||
)
|
||||
})?;
|
||||
self.search_reader(path, rdr).map_err(|err| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
|
@@ -1,2 +0,0 @@
|
||||
termcolor has moved to its own repository:
|
||||
https://github.com/BurntSushi/termcolor
|
@@ -341,6 +341,14 @@ rgtest!(glob_case_sensitive, |dir: Dir, mut cmd: TestCommand| {
|
||||
eqnice!("file2.html:Sherlock\n", cmd.stdout());
|
||||
});
|
||||
|
||||
rgtest!(glob_always_case_insensitive, |dir: Dir, mut cmd: TestCommand| {
|
||||
dir.create("sherlock", SHERLOCK);
|
||||
dir.create("file.HTML", "Sherlock");
|
||||
cmd.args(&["--glob-case-insensitive", "--glob", "*.html", "Sherlock"]);
|
||||
|
||||
eqnice!("file.HTML:Sherlock\n", cmd.stdout());
|
||||
});
|
||||
|
||||
rgtest!(byte_offset_only_matching, |dir: Dir, mut cmd: TestCommand| {
|
||||
dir.create("sherlock", SHERLOCK);
|
||||
cmd.arg("-b").arg("-o").arg("Sherlock");
|
||||
|
@@ -392,8 +392,8 @@ rgtest!(r428_unrecognized_style, |_: Dir, mut cmd: TestCommand| {
|
||||
let output = cmd.cmd().output().unwrap();
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
let expected = "\
|
||||
invalid --colors spec: unrecognized style attribute ''. \
|
||||
Choose from: nobold, bold, nointense, intense, nounderline, underline.
|
||||
unrecognized style attribute ''. Choose from: nobold, bold, nointense, \
|
||||
intense, nounderline, underline.
|
||||
";
|
||||
eqnice!(expected, stderr);
|
||||
});
|
||||
@@ -706,6 +706,18 @@ rgtest!(r1203_reverse_suffix_literal, |dir: Dir, _: TestCommand| {
|
||||
eqnice!("153.230000\n", cmd.arg(r"\d\d\d000").arg("test").stdout());
|
||||
});
|
||||
|
||||
// See: https://github.com/BurntSushi/ripgrep/issues/1223
|
||||
rgtest!(r1223_no_dir_check_for_default_path, |dir: Dir, mut cmd: TestCommand| {
|
||||
dir.create_dir("-");
|
||||
dir.create("a.json", "{}");
|
||||
dir.create("a.txt", "some text");
|
||||
|
||||
eqnice!(
|
||||
"a.json\na.txt\n",
|
||||
sort_lines(&cmd.arg("a").pipe(b"a.json\na.txt"))
|
||||
);
|
||||
});
|
||||
|
||||
// See: https://github.com/BurntSushi/ripgrep/issues/1259
|
||||
rgtest!(r1259_drop_last_byte_nonl, |dir: Dir, mut cmd: TestCommand| {
|
||||
dir.create("patterns-nonl", "[foo]");
|
||||
@@ -716,3 +728,13 @@ rgtest!(r1259_drop_last_byte_nonl, |dir: Dir, mut cmd: TestCommand| {
|
||||
cmd = dir.command();
|
||||
eqnice!("fz\n", cmd.arg("-f").arg("patterns-nl").arg("test").stdout());
|
||||
});
|
||||
|
||||
// See: https://github.com/BurntSushi/ripgrep/issues/1334
|
||||
rgtest!(r1334_crazy_literals, |dir: Dir, mut cmd: TestCommand| {
|
||||
dir.create("patterns", &"1.208.0.0/12\n".repeat(40));
|
||||
dir.create("corpus", "1.208.0.0/12\n");
|
||||
eqnice!(
|
||||
"1.208.0.0/12\n",
|
||||
cmd.arg("-Ff").arg("patterns").arg("corpus").stdout()
|
||||
);
|
||||
});
|
||||
|
@@ -1,2 +0,0 @@
|
||||
wincolor has moved to the termcolor repository:
|
||||
https://github.com/BurntSushi/termcolor
|
Reference in New Issue
Block a user