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

Propagate errors on Tree::walk #1098

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ git_enum! {
}

pub type git_treewalk_cb =
Option<extern "C" fn(*const c_char, *const git_tree_entry, *mut c_void) -> c_int>;
extern "C" fn(*const c_char, *const git_tree_entry, *mut c_void) -> c_int;
pub type git_treebuilder_filter_cb =
Option<extern "C" fn(*const git_tree_entry, *mut c_void) -> c_int>;

Expand Down
25 changes: 20 additions & 5 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct TreeIter<'tree> {

/// A binary indicator of whether a tree walk should be performed in pre-order
/// or post-order.
#[derive(Clone, Copy)]
pub enum TreeWalkMode {
/// Runs the traversal in pre-order.
PreOrder = 0,
Expand Down Expand Up @@ -126,12 +127,12 @@ impl<'repo> Tree<'repo> {
let mut data = TreeWalkCbData {
callback: &mut callback,
};
raw::git_tree_walk(
try_call!(raw::git_tree_walk(
self.raw(),
mode.into(),
Some(treewalk_cb::<T>),
&mut data as *mut _ as *mut c_void,
);
mode as raw::git_treewalk_mode,
treewalk_cb::<T>,
&mut data as *mut _ as *mut c_void
));
Ok(())
}
}
Expand Down Expand Up @@ -599,4 +600,18 @@ mod tests {
.unwrap();
assert_eq!(ct, 8);
}

#[test]
fn tree_walk_error() {
let (td, repo) = crate::test::repo_init();

setup_repo(&td, &repo);

let head = repo.head().unwrap();
let target = head.target().unwrap();
let commit = repo.find_commit(target).unwrap();
let tree = repo.find_tree(commit.tree_id()).unwrap();
let e = tree.walk(TreeWalkMode::PreOrder, |_, _| -1).unwrap_err();
assert_eq!(e.class(), crate::ErrorClass::Callback);
}
}
Loading