Merge pull request #1917 from Xavrir/issue-1900
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 db7d448, 807aa11 · Browse files · 5 files·+85 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- fix: handle invalid working directories gracefully with --full-path
Diff stats
5 files+85 added−18 removed
- other:+85 added−18 removed
Provenance
- Commit
6d77799- Tree
1e4b78c- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
db7d448- Parent 2
807aa11
5 files changed+85 added−18 removed
modified:src/config.rs+3 added-3 removed
13 unmodified lines14 pub struct Config {15 /// Whether the search is case-sensitive or case-insensitive.16 pub case_sensitive: bool,17 18- /// Whether to search within the full file path or just the base name (filename or directory19- /// name).20- pub search_full_path: bool,18+ /// Cached current working directory for absolute path construction.19+ /// Populated when `--full-path` is set; `None` means search by filename only.20+ pub cwd: Option<PathBuf>,21 22 /// Whether to ignore hidden files and directories (or not).23 pub ignore_hidden: bool,24 119 unmodified linesmodified:src/filesystem.rs+47 added
18 unmodified lines19 let path = path.strip_prefix(".").unwrap_or(path);20 env::current_dir().map(|path_buf| path_buf.join(path))21 }22 23+/// Construct an absolute path from a potentially relative path and a24+/// pre-resolved working directory. Unlike `path_absolute_form`, this25+/// does not call `env::current_dir()` and cannot fail.26+pub fn make_absolute(path: &Path, cwd: &Path) -> PathBuf {27+ if path.is_absolute() {28+ return path.to_path_buf();29+ }30+ let path = path.strip_prefix(".").unwrap_or(path);31+ cwd.join(path)32+}33+34 pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {35 let path_buf = path_absolute_form(path)?;36 37 #[cfg(windows)]125 unmodified lines163 strip_current_dir(Path::new("foo/bar/baz")),164 Path::new("foo/bar/baz")165 );166 }167+168+ #[test]169+ fn make_absolute_with_relative_path() {170+ use super::make_absolute;171+ use std::path::PathBuf;172+173+ let cwd = Path::new("/home/user");174+ assert_eq!(175+ make_absolute(Path::new("foo/bar"), cwd),176+ PathBuf::from("/home/user/foo/bar")177+ );178+ }179+180+ #[test]181+ fn make_absolute_strips_dot_prefix() {182+ use super::make_absolute;183+ use std::path::PathBuf;184+185+ let cwd = Path::new("/home/user");186+ assert_eq!(187+ make_absolute(Path::new("./foo/bar"), cwd),188+ PathBuf::from("/home/user/foo/bar")189+ );190+ }191+192+ #[test]193+ fn make_absolute_with_absolute_path() {194+ use super::make_absolute;195+ use std::path::PathBuf;196+197+ let cwd = Path::new("/home/user");198+ assert_eq!(199+ make_absolute(Path::new("/absolute/path"), cwd),200+ PathBuf::from("/absolute/path")201+ );202+ }203 }modified:src/main.rs+10 added-1 removed
243 unmodified lines244 };245 let command = extract_command(&mut opts, colored_output)?;246 let has_command = command.is_some();247 248+ let cwd = if opts.full_path {249+ Some(env::current_dir().context(250+ "Could not determine current directory. \251+ This is required for --full-path.",252+ )?)253+ } else {254+ None255+ };256+257 Ok(Config {258 case_sensitive,250- search_full_path: opts.full_path,259+ cwd,260 ignore_hidden: !(opts.hidden || opts.rg_alias_ignore()),261 read_fdignore: !(opts.no_ignore || opts.rg_alias_ignore()),262 read_vcsignore: !(opts.no_ignore || opts.rg_alias_ignore() || opts.no_ignore_vcs),263 require_git_to_read_vcsignore: !opts.no_require_git,238 unmodified linesmodified:src/walk.rs+20 added-14 removed
522 unmodified lines523 524 // Check the name first, since it doesn't require metadata525 let entry_path = entry.path();526 527- let search_str: Cow<OsStr> = if config.search_full_path {528- let path_abs_buf = filesystem::path_absolute_form(entry_path)529- .expect("Retrieving absolute path succeeds");530- Cow::Owned(path_abs_buf.as_os_str().to_os_string())531- } else {532- match entry_path.file_name() {533- Some(filename) => Cow::Borrowed(filename),534- None => unreachable!(535- "Encountered file system entry without a file name. This should only \536- happen for paths like 'foo/bar/..' or '/' which are not supposed to \537- appear in a file system traversal."538- ),539- }540- };527+ let search_str = search_str_for_entry(entry_path, config.cwd.as_deref());528 529 if !patterns530 .iter()531 .all(|pat| pat.is_match(&filesystem::osstr_to_bytes(search_str.as_ref())))130 unmodified lines662 }663 }664 }665 666+fn search_str_for_entry<'a>(667+ entry_path: &'a std::path::Path,668+ cwd: Option<&std::path::Path>,669+) -> Cow<'a, OsStr> {670+ if let Some(cwd) = cwd {671+ let abs_path = filesystem::make_absolute(entry_path, cwd);672+ Cow::Owned(abs_path.into_os_string())673+ } else {674+ match entry_path.file_name() {675+ Some(filename) => Cow::Borrowed(filename),676+ None => unreachable!(677+ "Encountered file system entry without a file name. This should only \678+ happen for paths like 'foo/bar/..' or '/' which are not supposed to \679+ appear in a file system traversal."680+ ),681+ }682+ }683+}684+685 /// Recursively scan the given search path for files / pathnames matching the patterns.686 ///687 /// If the `--exec` argument was supplied, this will create a thread pool for executing688 /// jobs in parallel from a given command line and the discovered paths. Otherwise, each4 unmodified linesmodified:CHANGELOG.md+5 added
1+# Unreleased2+3+## Bugfixes4+- Handle invalid working directories gracefully when using `--full-path`, see #1900 (@Xavrir).5+6 # 10.4.27 8 ## Bugfixes9 - Fixed performance regression due to `--ignore-contain`; see #1913 and #1914845 unmodified lines