Skip to content

Commit

Permalink
Merge pull request #1098 from edrevo/edrevo/fix-tree-walk
Browse files Browse the repository at this point in the history
Propagate errors on Tree::walk
  • Loading branch information
ehuss authored Jan 4, 2025
2 parents ba07a6f + e0d7dae commit 3b8dc04
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,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);
}
}

0 comments on commit 3b8dc04

Please sign in to comment.