From 47974b64795996dfe0adc06dadfe791efca25416 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Wed, 15 Apr 2020 15:47:17 +0200 Subject: [PATCH] --glob: '*' does not match on path separators --- CHANGELOG.md | 4 ++++ src/main.rs | 12 +++++++++--- tests/tests.rs | 10 ++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffd4327..9722acf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,10 @@ ## Changes +- When using `--glob` in combination with `--full-path`, a `*` character does not match a path + separation character (`/` or `\\`) anymore. You can use `**` for that. This allows things like + `fd -p -g '/some/base/path/*/*/*.txt'` which would previously match to arbitrary depths (instead + of exactly two folders below `/some/base/path`. See #404. ## Other diff --git a/src/main.rs b/src/main.rs index 5872778..69653e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ use std::time; use anyhow::{anyhow, Context, Result}; use atty::Stream; -use globset::Glob; +use globset::GlobBuilder; use lscolors::LsColors; use regex::bytes::{RegexBuilder, RegexSetBuilder}; @@ -123,7 +123,7 @@ fn run() -> Result { } let pattern_regex = if matches.is_present("glob") { - let glob = Glob::new(pattern)?; + let glob = GlobBuilder::new(pattern).literal_separator(true).build()?; glob.regex().to_owned() } else if matches.is_present("fixed-strings") { // Treat pattern as literal string if '--fixed-strings' is used @@ -296,7 +296,13 @@ fn run() -> Result { .value_of("max-results") .and_then(|n| usize::from_str_radix(n, 10).ok()) .filter(|&n| n != 0) - .or_else(|| if matches.is_present("max-one-result") { Some(1) } else { None }), + .or_else(|| { + if matches.is_present("max-one-result") { + Some(1) + } else { + None + } + }), }; let re = RegexBuilder::new(&pattern_regex) diff --git a/tests/tests.rs b/tests/tests.rs index 1b8a244..ce7df82 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -272,6 +272,16 @@ fn test_full_path_glob_searches() { one/two/c.foo one/two/three/d.foo", ); + + te.assert_output( + &["--glob", "--full-path", "**/one/*/*.foo"], + " one/two/c.foo", + ); + + te.assert_output( + &["--glob", "--full-path", "**/one/*/*/*.foo"], + " one/two/three/d.foo", + ); } #[test]