Merge pull request #1928 from tmccombs/full-path-fixes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Full path fixes
authored by Tavian Barnes · committed by GitHub · parents 9d137d3, dc9c503 · Browse files · 4 files·+54 added−55 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+54 added−55 removed
- other:+54 added−55 removed
Provenance
- Commit
d6b44b8- Tree
33e2a85- Author
Tavian Barnes <tavianator@tavianator.com>- Committer
GitHub <noreply@github.com>- Parent 1
9d137d3- Parent 2
dc9c503
4 files changed+54 added−55 removed
modified:src/config.rs+1 added-1 removed
15 unmodified lines16 pub case_sensitive: bool,17 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>,20+ pub full_path_base: 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 removed
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-23 pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {24 let path_buf = path_absolute_form(path)?;25 26 #[cfg(windows)]125 unmodified lines152 strip_current_dir(Path::new("foo/bar/baz")),153 Path::new("foo/bar/baz")154 );155 }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- }156 }modified:src/main.rs+2 added-2 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 {248+ let full_path_base = if opts.full_path {249 Some(env::current_dir().context(250 "Could not determine current directory. \251 This is required for --full-path.",252 )?)2 unmodified lines255 };256 257 Ok(Config {258 case_sensitive,259- cwd,259+ full_path_base,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+51 added-5 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 = search_str_for_entry(entry_path, config.cwd.as_deref());527+ let search_str = search_str_for_entry(entry_path, config.full_path_base.as_deref());528 529 if !patterns530 .iter()531 .all(|pat| pat.is_match(&filesystem::osstr_to_bytes(search_str.as_ref())))132 unmodified lines664 }665 666 fn search_str_for_entry<'a>(667 entry_path: &'a std::path::Path,668- cwd: Option<&std::path::Path>,668+ full_path_base: 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())670+ if let Some(cwd) = full_path_base {671+ // If full_path_base is some, that means that we need to return672+ // the absolute path673+ if entry_path.is_absolute() {674+ return Cow::Borrowed(entry_path.as_os_str());675+ }676+ let path = entry_path.strip_prefix(".").unwrap_or(entry_path);677+ Cow::Owned(cwd.join(path).into())678 } else {679 match entry_path.file_name() {680 Some(filename) => Cow::Borrowed(filename),681 None => unreachable!(12 unmodified lines694 /// path will simply be written to standard output.695 pub fn scan(paths: &[PathBuf], patterns: Vec<Regex>, config: Config) -> Result<ExitCode> {696 WorkerState::new(patterns, config).scan(paths)697 }698+699+#[cfg(test)]700+mod tests {701+ use super::search_str_for_entry;702+ use std::path::{Path, PathBuf};703+704+ #[test]705+ fn search_str_for_entry_with_relative_path() {706+ let full_path_base = Some(Path::new("/home/user"));707+ assert_eq!(708+ search_str_for_entry(Path::new("foo/bar"), full_path_base),709+ PathBuf::from("/home/user/foo/bar")710+ );711+ }712+713+ #[test]714+ fn search_str_for_entry_strips_dot_prefix() {715+ let full_path_base = Some(Path::new("/home/user"));716+ assert_eq!(717+ search_str_for_entry(Path::new("./foo/bar"), full_path_base),718+ PathBuf::from("/home/user/foo/bar")719+ );720+ }721+722+ #[test]723+ fn search_str_for_entry_with_absolute_path() {724+ let full_path_base = Some(Path::new("/home/user"));725+ assert_eq!(726+ search_str_for_entry(Path::new("/absolute/path"), full_path_base),727+ PathBuf::from("/absolute/path")728+ );729+ }730+731+ #[test]732+ fn search_str_no_base_dir() {733+ assert_eq!(734+ search_str_for_entry(Path::new("./foo/bar"), None),735+ PathBuf::from("bar")736+ );737+ }738+}