From babe80d498675b1cb8e649a96cf73f6cc694792d Mon Sep 17 00:00:00 2001 From: Garrett Squire Date: Mon, 26 Sep 2016 20:56:15 -0700 Subject: [PATCH] add a max-depth option for directory traversal CR and add integration test --- doc/rg.1.md | 4 ++++ src/args.rs | 12 +++++++++++- tests/tests.rs | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/doc/rg.1.md b/doc/rg.1.md index c996d8321..f7379fc53 100644 --- a/doc/rg.1.md +++ b/doc/rg.1.md @@ -136,6 +136,10 @@ the raw speed of grep. -L, --follow : Follow symlinks. +--maxdepth *NUM* +: Descend at most NUM directories below the command line arguments. + A value of zero searches only the starting-points themselves. + --mmap : Search using memory maps when possible. This is enabled by default when ripgrep thinks it will be faster. (Note that mmap searching diff --git a/src/args.rs b/src/args.rs index 2a825e295..0f49e53e4 100644 --- a/src/args.rs +++ b/src/args.rs @@ -136,6 +136,10 @@ Less common options: -L, --follow Follow symlinks. + --maxdepth NUM + Descend at most NUM directories below the command line arguments. + A value of zero only searches the starting-points themselves. + --mmap Search using memory maps when possible. This is enabled by default when ripgrep thinks it will be faster. (Note that mmap searching @@ -222,6 +226,7 @@ pub struct RawArgs { flag_invert_match: bool, flag_line_number: bool, flag_fixed_strings: bool, + flag_maxdepth: Option, flag_mmap: bool, flag_no_heading: bool, flag_no_ignore: bool, @@ -272,6 +277,7 @@ pub struct Args { invert_match: bool, line_number: bool, line_per_match: bool, + maxdepth: Option, mmap: bool, no_ignore: bool, no_ignore_parent: bool, @@ -399,6 +405,7 @@ impl RawArgs { invert_match: self.flag_invert_match, line_number: !self.flag_no_line_number && self.flag_line_number, line_per_match: self.flag_vimgrep, + maxdepth: self.flag_maxdepth, mmap: mmap, no_ignore: no_ignore, no_ignore_parent: @@ -681,7 +688,10 @@ impl Args { /// Create a new recursive directory iterator at the path given. pub fn walker(&self, path: &Path) -> Result { - let wd = WalkDir::new(path).follow_links(self.follow); + let mut wd = WalkDir::new(path).follow_links(self.follow); + if let Some(maxdepth) = self.maxdepth { + wd = wd.max_depth(maxdepth); + } let mut ig = Ignore::new(); // Only register ignore rules if this is a directory. If it's a file, // then it was explicitly given by the end user, so we always search diff --git a/tests/tests.rs b/tests/tests.rs index 11513aa24..660b3523e 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -831,6 +831,21 @@ sherlock\x00can extract a clew from a wisp of straw or a flake of cigar ash; assert_eq!(lines, expected); }); +// See: https://github.com/BurntSushi/ripgrep/issues/109 +clean!(max_depth, "far", ".", |wd: WorkDir, mut cmd: Command| { + wd.create_dir("one"); + wd.create("one/pass", "far"); + wd.create_dir("one/too"); + wd.create("one/too/many", "far"); + + cmd.arg("--maxdepth").arg("2"); + + let lines: String = wd.stdout(&mut cmd); + let expected = path("one/pass:far\n"); + + assert_eq!(lines, expected); +}); + #[test] fn binary_nosearch() { let wd = WorkDir::new("binary_nosearch");