Merge pull request #1983 from DeflateAwning/feat-exact-i1963
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Thayne McCombs · committed by GitHub · parents febc1b6, 9f12a30 · Browse files · 5 files·+76 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- feat: Add --exact argument to match exact filename
Diff stats
5 files+76 added−7 removed
- tests:+45 added−2 removed
- other:+31 added−5 removed
Provenance
- Commit
42b2ab8- Tree
5392f27- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
febc1b6- Parent 2
9f12a30
5 files changed+76 added−7 removed
modified:doc/fd.1+8 added-1 removed
112 unmodified lines113 Perform a regular-expression based search (default). This can be used to override --glob.114 .TP115 .B \-F, \-\-fixed\-strings116 Treat the pattern as a literal string instead of a regular expression. Note that this also117-performs substring comparison. If you want to match on an exact filename, consider using '\-\-glob'.117+performs substring comparison. If you want to match on an exact filename, consider using '\-\-glob'118+or '\-\-exact'.119 .TP120+.B \-\-exact121+Perform an exact match. This is equivalent to '\-\-fixed\-strings' but requires the pattern to122+match the entire filename (or the full path if '\-\-full\-path' is used), rather than a substring.123+Special regex characters in the pattern are treated as literal characters.124+Cannot be used together with '\-\-glob' or '\-\-fixed\-strings'.125+.TP126 .BI "\-\-and " pattern127 Add additional required search patterns, all of which must be matched. Multiple additional128 patterns can be specified. The patterns are regular expressions, unless '\-\-glob'129 or '\-\-fixed\-strings' is used.456 unmodified linesmodified:src/cli.rs+15 added-2 removed
171 unmodified lines172 pub regex: bool,173 174 /// Treat the pattern as a literal string instead of a regular expression. Note175 /// that this also performs substring comparison. If you want to match on an176- /// exact filename, consider using '--glob'.176+ /// exact filename, consider using '--glob' or '--exact' instead.177 #[arg(178 long,179 short = 'F',180 alias = "literal",181 hide_short_help = true,182- help = "Treat pattern as literal string stead of regex",182+ help = "Treat pattern as literal string instead of regex",183 long_help184 )]185 pub fixed_strings: bool,186 187+ /// Perform an exact match. This is equivalent to '--fixed-strings' but requires188+ /// the pattern to match the entire filename (or path if '--full-path' is used),189+ /// rather than a substring. Special regex characters in the pattern are treated190+ /// as literal characters.191+ #[arg(192+ long,193+ conflicts_with_all(["glob", "fixed_strings"]),194+ hide_short_help = true,195+ help = "Match the entire filename exactly (literal, non-substring)",196+ long_help197+ )]198+ pub exact: bool,199+200 /// Add additional required search patterns, all of which must be matched. Multiple201 /// additional patterns can be specified. The patterns are regular202 /// expressions, unless '--glob' or '--fixed-strings' is used.203 #[arg(767 unmodified linesmodified:src/main.rs+7 added-2 removed
203 unmodified lines204 fn build_pattern_regex(pattern: &str, opts: &Opts) -> Result<String> {205 Ok(if opts.glob && !pattern.is_empty() {206 let glob = GlobBuilder::new(pattern).literal_separator(true).build()?;207 glob.regex().to_owned()208+ } else if opts.exact {209+ // Anchor the escaped pattern so the full filename (or path) must match exactly.210+ // Literal. No substring matching.211+ format!("^{}$", regex::escape(pattern))212 } else if opts.fixed_strings {213 // Treat pattern as literal string if '--fixed-strings' is used214 regex::escape(pattern)215 } else {314 unmodified lines530 .dot_matches_new_line(true)531 .build()532 .map_err(|e| {533 anyhow!(530- "{}\n\nNote: You can use the '--fixed-strings' option to search for a \531- literal string instead of a regular expression. Alternatively, you can \534+ "{}\n\nNote: You can search for literal substrings with '--fixed-strings' \535+ or literal strings with '--exact' options (instead of a regular expression). \536+ Alternatively, you can \537 also use the '--glob' option to match on a glob pattern.",538 e539 )540 })1 unmodified linemodified:tests/tests.rs+45 added-2 removed
2183 unmodified lines2184 2185 // Regex search, parens are treated as group2186 te.assert_output(&["download (1)"], "");2187 2188- // Literal search, parens are treated as characters2188+ // Literal search, parens are treated as characters. Case-insensitive by default.2189 te.assert_output(2190 &["--fixed-strings", "download (1)"],2191 "test2/Download (1).tar.gz",2192 );2193 2194- // Combine with --case-sensitive2194+ // Combine with --case-sensitive (unmatched).2195 te.assert_output(&["--fixed-strings", "--case-sensitive", "download (1)"], "");2196+2197+ // Combine with --case-sensitive (matched).2198+ te.assert_output(2199+ &["--fixed-strings", "--case-sensitive", "Download (1)"],2200+ "test2/Download (1).tar.gz",2201+ );2202 }2203 2204+/// Literal search, non-substring (--exact)2205+#[test]2206+fn test_exact_literal_nonsubstring() {2207+ let dirs = &["test1", "test2"];2208+ let files = &[2209+ "test1/a.foo",2210+ "test1/aa.foo",2211+ "test1/a.food",2212+ "test1/ca.food",2213+ "test1/a_foo",2214+ "test2/Download (1).tar.gz",2215+ ];2216+ let te = TestEnv::new(dirs, files);2217+2218+ // Literal search, dot is treated as character. Should match only the exact name,2219+ // not "aa.foo", "a.food", or "ca.food".2220+ te.assert_output(&["--exact", "a.foo"], "test1/a.foo");2221+2222+ // Literal search, parens are treated as characters. Substring should not match.2223+ te.assert_output(&["--exact", "download (1)"], "");2224+2225+ // Literal search, parens are treated as characters. Case-insensitive by default.2226+ te.assert_output(2227+ &["--exact", "download (1).tar.gz"],2228+ "test2/Download (1).tar.gz",2229+ );2230+2231+ // Combine with --case-sensitive, should not match due to case mismatch.2232+ te.assert_output(&["--exact", "--case-sensitive", "download (1).tar.gz"], "");2233+2234+ // Combine with --case-sensitive, exact match should match.2235+ te.assert_output(2236+ &["--exact", "--case-sensitive", "Download (1).tar.gz"],2237+ "test2/Download (1).tar.gz",2238+ );2239+}2240+2241 /// Filenames with invalid UTF-8 sequences2242 #[cfg(target_os = "linux")]2243 #[test]2244 fn test_invalid_utf8() {609 unmodified linesmodified:CHANGELOG.md+1 added
1 # Unreleased2 3 ## Features4 - Add `--ignore-parent` option to override `--no-ignore-parent`, see #1958 (@tmchow)5+- Add `--exact` option to match the entire filename exactly (literal, non-substring).6 7 ## Bugfixes8 - Handle invalid working directories gracefully when using `--full-path`, see #1900 (@Xavrir).9 - Fire the "search pattern contains a path separator" diagnostic for any pattern containing `/`, not just patterns that happen to name an existing directory. Preserves the legacy Windows behaviour that also flags native `\` separators when the pattern resolves to a real directory. See #1873.853 unmodified lines