Use absolute path to perform search, fixes #81

Previously, we were using the relative path to extract the search
string. For the current directory, the returned relative path was equal
to `""`. This is why the current directory did not show in the search
results (see #81).

This commit also changes the way that `--full-path` works, which was
previously working on relative paths. It seems more useful to search the
absolute path, though. Otherwise, search results could change just by
calling fd (with a given search path) from a different directory.
This commit is contained in:
sharkdp 2017-10-11 23:08:41 +02:00 committed by David Peter
parent 97e3110333
commit 8fc3a83d92
2 changed files with 50 additions and 15 deletions

View File

@ -2,7 +2,7 @@ use internal::{error, FdOptions};
use fshelper;
use output;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::mpsc::channel;
use std::thread;
@ -101,6 +101,7 @@ pub fn scan(root: &Path, pattern: Arc<Regex>, base: &Path, config: Arc<FdOptions
let config = Arc::clone(&config);
let pattern = Arc::clone(&pattern);
let tx_thread = tx.clone();
let root = root.to_owned();
Box::new(move |entry_o| {
let entry = match entry_o {
@ -108,6 +109,12 @@ pub fn scan(root: &Path, pattern: Arc<Regex>, base: &Path, config: Arc<FdOptions
Err(_) => return ignore::WalkState::Continue,
};
let entry_path = entry.path();
if entry_path == root {
return ignore::WalkState::Continue;
}
// Filter out unwanted file types.
match config.file_type {
FileType::Any => (),
@ -130,29 +137,32 @@ pub fn scan(root: &Path, pattern: Arc<Regex>, base: &Path, config: Arc<FdOptions
// Filter out unwanted extensions.
if let Some(ref filter_ext) = config.extension {
let entry_ext = entry.path().extension().map(|e| {
e.to_string_lossy().to_lowercase()
});
let entry_ext = entry_path.extension().map(
|e| e.to_string_lossy().to_lowercase(),
);
if entry_ext.map_or(true, |ext| ext != *filter_ext) {
return ignore::WalkState::Continue;
}
}
let path_rel_buf = match fshelper::path_relative_from(entry.path(), &*base) {
Some(p) => p,
None => error("Error: could not get relative path for directory entry."),
};
let path_rel = path_rel_buf.as_path();
let search_str_o = if config.search_full_path {
Some(path_rel.to_string_lossy())
Some(entry_path.to_string_lossy())
} else {
path_rel.file_name().map(|f| f.to_string_lossy())
entry_path.file_name().map(|f| f.to_string_lossy())
};
if let Some(search_str) = search_str_o {
// TODO: take care of the unwrap call
pattern.find(&*search_str).map(|_| {
let mut path_rel_buf = match fshelper::path_relative_from(entry_path, &*base) {
Some(p) => p,
None => error("Error: could not get relative path for directory entry."),
};
if path_rel_buf == PathBuf::new() {
path_rel_buf.push(".");
}
// TODO: take care of the unwrap call
tx_thread.send(path_rel_buf.to_owned()).unwrap()
});
}

View File

@ -68,6 +68,16 @@ fn test_explicit_root_path() {
three/d.foo
three/directory_foo",
);
te.assert_output_subdirectory(
"one/two/three",
&["", ".."],
".
../c.foo
../C.Foo2
d.foo
directory_foo",
);
}
/// Regex searches
@ -150,8 +160,6 @@ fn test_full_path() {
"one/two/three/d.foo
one/two/three/directory_foo",
);
te.assert_output(&["--full-path", "^a\\.foo"], "a.foo");
}
/// Hidden files (--hidden)
@ -308,6 +316,23 @@ fn test_absolute_path() {
#[cfg(windows)]
let abs_path = abs_path.trim_left_matches(r"\\?\");
te.assert_output(
&["--absolute-path"],
&format!(
"{abs_path}/a.foo
{abs_path}/one
{abs_path}/one/b.foo
{abs_path}/one/two
{abs_path}/one/two/c.foo
{abs_path}/one/two/C.Foo2
{abs_path}/one/two/three
{abs_path}/one/two/three/d.foo
{abs_path}/one/two/three/directory_foo
{abs_path}/symlink",
abs_path = abs_path
),
);
te.assert_output(
&["--absolute-path", "foo"],
&format!(