Skip to content
Myr

globset: fix bug where trailing `.` in file name was incorrectly handled

Before adoption: this commit came in with the history the project adopted, so no landing records it.

I'm not sure why I did this, but I think I was trying to imitate the contract of [`std::path::Path::file_name`]: > Returns None if the path terminates in `..`. But the status quo clearly did not implement this. And as a result, if you have a glob that ends in a `.`, it was instead treated as the empty string (which only matches the empty string). We fix this by implementing the semantic from the standard library correctly. Fixes #2990 [`std::path::Path::file_name`]: https://doc.rust-lang.org/std/path/struct.Path.html#method.file_name

authored by Andrew Gallant · committed by Andrew Gallant · parent ba23ced · Browse files · 3 files+27 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+27 added−5 removed

  • tests:+16 added−0 removed
  • other:+11 added−5 removed

3 files+27 added−5 removed

modified:crates/globset/src/pathutil.rs+9 added-5 removed
2 unmodified lines
3use bstr::{ByteSlice, ByteVec};45/// The final component of the path, if it is a normal file.6///7/// If the path terminates in `.`, `..`, or consists solely of a root of8/// prefix, file_name will return None.7/// If the path terminates in `..`, or consists solely of a root of prefix,8/// file_name will return `None`.9pub(crate) fn file_name<'a>(path: &Cow<'a, [u8]>) -> Option<Cow<'a, [u8]>> {10 if path.last_byte().map_or(true, |b| b == b'.') {10 if path.is_empty() {11 return None;12 }13 let last_slash = path.rfind_byte(b'/').map(|i| i + 1).unwrap_or(0);14 Some(match *path {14 let got = match *path {15 Cow::Borrowed(path) => Cow::Borrowed(&path[last_slash..]),16 Cow::Owned(ref path) => {17 let mut path = path.clone();18 path.drain_bytes(..last_slash);19 Cow::Owned(path)20 }21 })21 };22 if got == &b".."[..] {23 return None;24 }25 Some(got)26}2728/// Return a file extension given a path's file name.29///
104 unmodified lines
modified:tests/regression.rs+16 added
1459 unmodified lines
1460 dir.create("haystack", "foo1\nfoo2\nfoo3\nfoo4\nfoo5\n");1461 let got = cmd.args(&["--stats", "-m2", "foo", "."]).stdout();1462 assert!(got.contains("10 bytes searched\n"));1463});14641465// See: https://github.com/BurntSushi/ripgrep/issues/29901466#[cfg(unix)]1467rgtest!(r2990_trip_over_trailing_dot, |dir: Dir, _cmd: TestCommand| {1468 dir.create_dir("asdf");1469 dir.create_dir("asdf.");1470 dir.create("asdf/foo", "");1471 dir.create("asdf./foo", "");14721473 let got = dir.command().args(&["--files", "-g", "!asdf/"]).stdout();1474 eqnice!("asdf./foo\n", got);14751476 // This used to ignore the glob given and included `asdf./foo` in output.1477 let got = dir.command().args(&["--files", "-g", "!asdf./"]).stdout();1478 eqnice!("asdf/foo\n", got);1479});
modified:CHANGELOG.md+2 added
21 unmodified lines
22* [BUG #2177](https://github.com/BurntSushi/ripgrep/issues/2177):23 Ignore a UTF-8 BOM marker at the start of `.gitignore` (and similar files).24* [BUG #2944](https://github.com/BurntSushi/ripgrep/pull/2944):25 Fix a bug where the "bytes searched" in `--stats` output could be incorrect.26* [BUG #2990](https://github.com/BurntSushi/ripgrep/issues/2990):27 Fix a bug where ripgrep would mishandle globs that ended with a `.`.2829Feature enhancements:3031* Many enhancements to the default set of file types available for filtering.
1727 unmodified lines