Skip to content
Myr

Use absolute path to perform search, fixes #81

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

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.

authored by sharkdp · committed by David Peter · parent 97e3110 · Browse files · 2 files+50 added−15 removed
This commit

State

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

Diff stats

2 files+50 added−15 removed

  • tests:+27 added−2 removed
  • other:+23 added−13 removed

2 files+50 added−15 removed

modified:src/walk.rs+23 added-13 removed
1use internal::{error, FdOptions};2use fshelper;3use output;45use std::path::Path;5use std::path::{Path, PathBuf};6use std::sync::Arc;7use std::sync::mpsc::channel;8use std::thread;9use std::time;
90 unmodified lines
100 let base = base.to_owned();101 let config = Arc::clone(&config);102 let pattern = Arc::clone(&pattern);103 let tx_thread = tx.clone();104 let root = root.to_owned();105106 Box::new(move |entry_o| {107 let entry = match entry_o {108 Ok(e) => e,109 Err(_) => return ignore::WalkState::Continue,110 };111112 let entry_path = entry.path();113114 if entry_path == root {115 return ignore::WalkState::Continue;116 }117118 // Filter out unwanted file types.119 match config.file_type {120 FileType::Any => (),121 FileType::RegularFile => {
14 unmodified lines
136 }137138 // Filter out unwanted extensions.139 if let Some(ref filter_ext) = config.extension {133 let entry_ext = entry.path().extension().map(|e| {134 e.to_string_lossy().to_lowercase()135 });140 let entry_ext = entry_path.extension().map(141 |e| e.to_string_lossy().to_lowercase(),142 );143 if entry_ext.map_or(true, |ext| ext != *filter_ext) {144 return ignore::WalkState::Continue;145 }146 }147141 let path_rel_buf = match fshelper::path_relative_from(entry.path(), &*base) {142 Some(p) => p,143 None => error("Error: could not get relative path for directory entry."),144 };145 let path_rel = path_rel_buf.as_path();146148 let search_str_o = if config.search_full_path {148 Some(path_rel.to_string_lossy())149 Some(entry_path.to_string_lossy())150 } else {150 path_rel.file_name().map(|f| f.to_string_lossy())151 entry_path.file_name().map(|f| f.to_string_lossy())152 };153154 if let Some(search_str) = search_str_o {154 // TODO: take care of the unwrap call155 pattern.find(&*search_str).map(|_| {156 let mut path_rel_buf = match fshelper::path_relative_from(entry_path, &*base) {157 Some(p) => p,158 None => error("Error: could not get relative path for directory entry."),159 };160161 if path_rel_buf == PathBuf::new() {162 path_rel_buf.push(".");163 }164165 // TODO: take care of the unwrap call166 tx_thread.send(path_rel_buf.to_owned()).unwrap()167 });168 }169
11 unmodified lines
modified:tests/tests.rs+27 added-2 removed
66 unmodified lines
67 C.Foo268 three/d.foo69 three/directory_foo",70 );7172 te.assert_output_subdirectory(73 "one/two/three",74 &["", ".."],75 ".76 ../c.foo77 ../C.Foo278 d.foo79 directory_foo",80 );81}8283/// Regex searches84#[test]
74 unmodified lines
159 &["--full-path", "three.*foo"],160 "one/two/three/d.foo161 one/two/three/directory_foo",162 );153154 te.assert_output(&["--full-path", "^a\\.foo"], "a.foo");163}164165/// Hidden files (--hidden)166#[test]
149 unmodified lines
316 #[cfg(windows)]317 let abs_path = abs_path.trim_left_matches(r"\\?\");318319 te.assert_output(320 &["--absolute-path"],321 &format!(322 "{abs_path}/a.foo323 {abs_path}/one324 {abs_path}/one/b.foo325 {abs_path}/one/two326 {abs_path}/one/two/c.foo327 {abs_path}/one/two/C.Foo2328 {abs_path}/one/two/three329 {abs_path}/one/two/three/d.foo330 {abs_path}/one/two/three/directory_foo331 {abs_path}/symlink",332 abs_path = abs_path333 ),334 );335336 te.assert_output(337 &["--absolute-path", "foo"],338 &format!(339 "{abs_path}/a.foo340 {abs_path}/one/b.foo
70 unmodified lines