Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max depth option #111

Merged
merged 1 commit into from
Sep 27, 2016
Merged

Max depth option #111

merged 1 commit into from
Sep 27, 2016

Conversation

gsquire
Copy link
Contributor

@gsquire gsquire commented Sep 27, 2016

Closes #109

@gsquire
Copy link
Contributor Author

gsquire commented Sep 27, 2016

I'm not sure of how to write a unit test because I think I would just be testing walkdir if I did :)

Let me know if you are more clever than me in this regard.

@@ -136,6 +136,9 @@ Less common options:
-L, --follow
Follow symlinks.

--maxdepth NUM
Descend at most NUM directories below the command line arguments.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add this flag to doc/rg.1.md and run the convert script to create a new doc/rg.1 file?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe clarify that a value of 0 searches the current directory only.

WalkDir::new(path).follow_links(self.follow).max_depth(self.maxdepth)
} else {
WalkDir::new(path).follow_links(self.follow)
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like this better, stylistically:

let mut wd = WalkDir::new(path).follow_links(self.follow);
if let Some(maxdepth) = self.maxdepth {
    wd = wd.max_depth(maxdepth);
}

@@ -681,7 +687,12 @@ impl Args {

/// Create a new recursive directory iterator at the path given.
pub fn walker(&self, path: &Path) -> Result<walk::Iter> {
let wd = WalkDir::new(path).follow_links(self.follow);
let wd =
if self.maxdepth > 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to allow a value of 0 to mean "search current directory." In this case, maxdepth should be an Option<usize>, which docopt will handle correctly.

@BurntSushi
Copy link
Owner

@gsquire Could you write an integration test please? It would be good to follow the pattern I've been using whenever I introduce new command line flags: https://github.com/BurntSushi/ripgrep/blob/master/tests/tests.rs#L781-L820

Other than that and a few nits, this looks good, thanks!

@gsquire gsquire force-pushed the max-depth branch 2 times, most recently from 1b8404b to b4ed9e5 Compare September 27, 2016 17:13
@gsquire
Copy link
Contributor Author

gsquire commented Sep 27, 2016

I rebased onto master and squashed. Is that the style of integration test you expected?


cmd.arg("--maxdepth").arg("1");

wd.assert_err(&mut cmd);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a file one/foo with far in it, and assert that you get results? That seems like a slightly better test since it makes sure search still works, but that it also respects the depth setting.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so I think with one/foo, you do indeed need to set maxdepth to 2. The argument, ., corresponds to depth 0. one is depth 1 and one/foo is depth 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I thought the count applied to directories and not their contents. I'll update it :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The count applies to whether to descend into a directory or not. At depth 0, you don't descend ever, you just yield whatever was given. So a walkdir of ./ at depth 0 yields ./. At depth 1, you descend once, so that a walkdir of ./ at depth 1 yields one, but doesn't descend into one. Finally, at depth 2, a walkdir of ./ yields ./one and then ./one/too and also ./one/foo, but doesn't descend into ./one/too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, thanks for a detailed explanation. I suppose it's really a "tree" after all.

@BurntSushi
Copy link
Owner

@gsquire That's perfect! I had one last suggestion and then this is ready to go. Thanks so much!

@gsquire
Copy link
Contributor Author

gsquire commented Sep 27, 2016

@BurntSushi Doing this causes the test to fail:

wd.create_dir("one");
wd.create("one/foo", "far");
wd.create_dir("one/too");
wd.create("one/too/many", "far");

cmd.arg("--maxdepth").arg("1");

let lines: String = wd.stdout(&mut cmd);
let expected = path("one/pass:far\n");

assert_eq!(lines, expected);

But changing the depth to 2 passes...am I misunderstanding the depth used in walkdir?

@BurntSushi
Copy link
Owner

I goofed. Looks like your initial implementation is correct. I was off by
one. Could you just make sure we are consistent with GNU find? The docs
will need to be updated too. Sorry for short response. On mobile.

On Sep 27, 2016 2:40 PM, "Garrett Squire" [email protected] wrote:

@BurntSushi https://github.com/BurntSushi Doing this causes the test to
fail:

wd.create_dir("one");
wd.create("one/foo", "far");
wd.create_dir("one/too");
wd.create("one/too/many", "far");

cmd.arg("--maxdepth").arg("1");
let lines: String = wd.stdout(&mut cmd);let expected = path("one/pass:far\n");

assert_eq!(lines, expected);

But changing the depth to 2 passes...am I misunderstanding the depth used
in walkdir?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#111 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAb34tN10dEsKkF0JR2Cd86vipvZiHLBks5quWMCgaJpZM4KHO4u
.

@gsquire
Copy link
Contributor Author

gsquire commented Sep 27, 2016

It's okay, just wanted to verify.

Quoting from the GNU find man page:

Descend at most levels (a non-negative integer) levels of directories below the starting-points.
-maxdepth 0 means only apply the tests and actions to the starting-points themselves.

So no need to update anything? I have to say, I thought having max depth of one and having a file in a directory below . would be found. This is why I asked you to confirm. No rush on this, I want to make sure I understand it correctly is all.

@BurntSushi
Copy link
Owner

@gsquire Ah OK. Looks good. I think just updating the test to make sure it both ignores a deeper directory and gets search results from a shallower directory should be sufficient. Also, I think the docs need to be updated. Probably saying "current directory" is wrong and we should say "starting points" like find does. Thanks for sticking with me!

@gsquire
Copy link
Contributor Author

gsquire commented Sep 27, 2016

Sweet, I know the phrasing is important. I updated that and the test. Hope it's good to go!

@gsquire
Copy link
Contributor Author

gsquire commented Sep 27, 2016

Looks like the build failure happened on master too...strange.

@BurntSushi BurntSushi merged commit 3550f2e into BurntSushi:master Sep 27, 2016
@BurntSushi
Copy link
Owner

Yup this looks great. The failure looks unrelated. Thanks so much for doing this and putting up with me! :-)

@gsquire gsquire deleted the max-depth branch September 27, 2016 23:46
@gsquire
Copy link
Contributor Author

gsquire commented Sep 27, 2016

I'm happy to be able to contribute, you're a Rust machine :)

Congrats on this project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants