Add --prune flag

closes #535
This commit is contained in:
Matthias Reitinger 2020-10-25 08:16:01 +01:00 committed by GitHub
parent 06eb231fbd
commit ec4cc981fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 57 additions and 0 deletions

View File

@ -3,6 +3,7 @@
## Features
- Improved the usability of the time-based options, see #624 and #645 (@gorogoroumaru)
- Add new `--prune` flag, see #535 (@reima)
## Bugfixes

View File

@ -102,6 +102,9 @@ _fd() {
'(--exact-depth --min-depth)--min-depth=[set directory depth to descend before start searching]:depth'
'(--exact-depth -d --max-depth --maxdepth --min-depth)--exact-depth=[only search at the exact given directory depth]:depth'
+ prune # pruning
"--prune[don't traverse into matching directories]"
+ filter-misc # filter search
'*'{-t+,--type=}"[filter search by type]:type:(($fd_types))"
'*'{-e+,--extension=}'[filter search by file extension]:extension'

3
doc/fd.1 vendored
View File

@ -115,6 +115,9 @@ Only show search results starting at the given depth. See also: '--max-depth' an
.BI "\-\-exact\-depth " d
Only show search results at the exact given depth. This is an alias for '--min-depth <depth> --max-depth <depth>'.
.TP
.B \-\-prune
Do not traverse into matching directories.
.TP
.BI "\-t, \-\-type " filetype
Filter search by type:
.RS

View File

@ -215,6 +215,13 @@ pub fn build_app() -> App<'static, 'static> {
'--min-depth <depth> --max-depth <depth>'.",
),
)
.arg(
Arg::with_name("prune")
.long("prune")
.conflicts_with_all(&["size", "exact-depth"])
.hidden_short_help(true)
.long_help("Do not traverse into matching directories.")
)
.arg(
Arg::with_name("file-type")
.long("type")

View File

@ -323,6 +323,7 @@ fn run() -> Result<ExitCode> {
.value_of("min-depth")
.or_else(|| matches.value_of("exact-depth"))
.and_then(|n| usize::from_str_radix(n, 10).ok()),
prune: matches.is_present("prune"),
threads: std::cmp::max(
matches
.value_of("threads")

View File

@ -48,6 +48,9 @@ pub struct Options {
/// The minimum depth for reported entries, or `None`.
pub min_depth: Option<usize>,
/// Whether to stop traversing into matching directories.
pub prune: bool,
/// The number of threads to use.
pub threads: usize,

View File

@ -494,6 +494,11 @@ fn spawn_senders(
return ignore::WalkState::Quit;
}
// Apply pruning.
if config.prune {
return ignore::WalkState::Skip;
}
ignore::WalkState::Continue
})
});

View File

@ -758,6 +758,40 @@ fn test_exact_depth() {
);
}
/// Pruning (--prune)
#[test]
fn test_prune() {
let dirs = &["foo/bar", "bar/foo", "baz"];
let files = &[
"foo/foo.file",
"foo/bar/foo.file",
"bar/foo.file",
"bar/foo/foo.file",
"baz/foo.file",
];
let te = TestEnv::new(dirs, files);
te.assert_output(
&["foo"],
"foo
foo/foo.file
foo/bar/foo.file
bar/foo.file
bar/foo
bar/foo/foo.file
baz/foo.file",
);
te.assert_output(
&["--prune", "foo"],
"foo
bar/foo
bar/foo.file
baz/foo.file",
);
}
/// Absolute paths (--absolute-path)
#[test]
fn test_absolute_path() {