Skip to content

Adding tree walk #343

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

Merged
merged 6 commits into from
Aug 21, 2018
Merged

Adding tree walk #343

merged 6 commits into from
Aug 21, 2018

Conversation

mzhang28
Copy link
Contributor

Just wanted to get some code down and start some discussion. I haven't worked much with bindings before so I'd appreciate some tips.

#267

@alexcrichton
Copy link
Member

Thanks for the PR! Looks like tests on MSVC may be failing though? Could a bitflags type be used instead perhaps?

Other than that with a small test this should be good to go!

@mzhang28
Copy link
Contributor Author

What should the callback function return? I actually only put bool cuz the filter function I referenced did but doesn't really make sense in this context.

@alexcrichton
Copy link
Member

According to libgit2:

If the callback returns a positive value, the passed entry will be skipped on the traversal (in pre mode). A negative value stops the walk.

Could errors translate to -1 and the boolean be used to skip/not skip?

@neithernut
Copy link

Libgit2 exposes a specific error number for such cases (stopping an iteration):
https://github.com/libgit2/libgit2/blob/9913e2df077fd9ed4c0fd5f81075ca5a153cd34b/include/git2/errors.h#L34

@mzhang28
Copy link
Contributor Author

mzhang28 commented Aug 14, 2018

How about using a custom enum type that's kind of like Option but also includes an option to stop? Seems like there's three kinds of actions that could take place here:

enum TreeWalkResult {
    Ok,
    Skip,
    Stop,
}

Not sure about the exact naming but then we could implement either From<TreeWalkResult> for i32 or Into<i32> for TreeWalkResult and convert it to an integer for git_tree_walk.

Thoughts?

@mzhang28
Copy link
Contributor Author

@alexcrichton what are your thoughts on this implementation?

src/tree.rs Outdated

extern fn treewalk_cb(root: *const c_char, entry: *const raw::git_tree_entry, payload: *mut c_void) -> c_int {
match panic::wrap(|| unsafe {
if panic::panicked() {
Copy link
Member

Choose a reason for hiding this comment

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

I think this shouldn't be necessary in that panic::wrap already checks it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok 👍

@alexcrichton
Copy link
Member

Looks good to me! Could some documentation be added to the function to mention the enum types as well?

@alexcrichton alexcrichton merged commit 27deee9 into rust-lang:master Aug 21, 2018
@alexcrichton
Copy link
Member

Thanks!

@asayers
Copy link
Contributor

asayers commented Sep 10, 2018

I'm seeing some strange behaviour with this feature. I have a callback which returns TreeWalkResult::Ok unconditionally, and I expect every node in the tree to be visited. Instead I see a subset of the top-level nodes only, and this subset is not deterministic across runs! Looks like something nasty is happening. Returning 0 from the callback instead gives the expected behaviour.

@mzhang28
Copy link
Contributor Author

mzhang28 commented Sep 10, 2018 via email

@asayers
Copy link
Contributor

asayers commented Sep 12, 2018

Also, reading libgit2/libgit2#4703 makes it seem like we should be passing GIT_EUSER (-7) rather than -1 to abort the traversal. (Unrelated to previous comment.)

EDIT: Just noticed that @neithernut pointed out the same thing.

@asayers asayers mentioned this pull request Sep 12, 2018
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.

4 participants