Skip to content

Commit d6b44b8

Browse files
authored
Merge pull request #1928 from tmccombs/full-path-fixes
Full path fixes
2 parents 9d137d3 + dc9c503 commit d6b44b8

4 files changed

Lines changed: 54 additions & 55 deletions

File tree

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Config {
1717

1818
/// Cached current working directory for absolute path construction.
1919
/// Populated when `--full-path` is set; `None` means search by filename only.
20-
pub cwd: Option<PathBuf>,
20+
pub full_path_base: Option<PathBuf>,
2121

2222
/// Whether to ignore hidden files and directories (or not).
2323
pub ignore_hidden: bool,

src/filesystem.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {
2020
env::current_dir().map(|path_buf| path_buf.join(path))
2121
}
2222

23-
/// Construct an absolute path from a potentially relative path and a
24-
/// pre-resolved working directory. Unlike `path_absolute_form`, this
25-
/// 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-
3423
pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {
3524
let path_buf = path_absolute_form(path)?;
3625

@@ -164,40 +153,4 @@ mod tests {
164153
Path::new("foo/bar/baz")
165154
);
166155
}
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-
}
203156
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
245245
let command = extract_command(&mut opts, colored_output)?;
246246
let has_command = command.is_some();
247247

248-
let cwd = if opts.full_path {
248+
let full_path_base = if opts.full_path {
249249
Some(env::current_dir().context(
250250
"Could not determine current directory. \
251251
This is required for --full-path.",
@@ -256,7 +256,7 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
256256

257257
Ok(Config {
258258
case_sensitive,
259-
cwd,
259+
full_path_base,
260260
ignore_hidden: !(opts.hidden || opts.rg_alias_ignore()),
261261
read_fdignore: !(opts.no_ignore || opts.rg_alias_ignore()),
262262
read_vcsignore: !(opts.no_ignore || opts.rg_alias_ignore() || opts.no_ignore_vcs),

src/walk.rs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl WorkerState {
524524
// Check the name first, since it doesn't require metadata
525525
let entry_path = entry.path();
526526

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());
528528

529529
if !patterns
530530
.iter()
@@ -665,11 +665,16 @@ impl WorkerState {
665665

666666
fn search_str_for_entry<'a>(
667667
entry_path: &'a std::path::Path,
668-
cwd: Option<&std::path::Path>,
668+
full_path_base: Option<&std::path::Path>,
669669
) -> 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 return
672+
// the absolute path
673+
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())
673678
} else {
674679
match entry_path.file_name() {
675680
Some(filename) => Cow::Borrowed(filename),
@@ -690,3 +695,44 @@ fn search_str_for_entry<'a>(
690695
pub fn scan(paths: &[PathBuf], patterns: Vec<Regex>, config: Config) -> Result<ExitCode> {
691696
WorkerState::new(patterns, config).scan(paths)
692697
}
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+
}

0 commit comments

Comments
 (0)