Skip to content

Commit

Permalink
Propagate errors on Tree::walk
Browse files Browse the repository at this point in the history
  • Loading branch information
edrevo committed Nov 19, 2024
1 parent a2e05da commit 1cd7a94
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
3 changes: 1 addition & 2 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,7 @@ git_enum! {
}
}

pub type git_treewalk_cb =
Option<extern "C" fn(*const c_char, *const git_tree_entry, *mut c_void) -> c_int>;
pub type git_treewalk_cb = *const 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
11 changes: 11 additions & 0 deletions src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ mod impls {
}
}

impl Convert<raw::git_treewalk_mode> for crate::TreeWalkMode {
#[cfg(target_env = "msvc")]
fn convert(&self) -> raw::git_treewalk_mode {
*self as i32
}
#[cfg(not(target_env = "msvc"))]
fn convert(&self) -> raw::git_treewalk_mode {
*self as u32
}
}

impl Convert<raw::git_direction> for Direction {
fn convert(&self) -> raw::git_direction {
match *self {
Expand Down
36 changes: 20 additions & 16 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 All @@ -60,17 +61,6 @@ impl Into<i32> for TreeWalkResult {
}
}

impl Into<raw::git_treewalk_mode> for TreeWalkMode {
#[cfg(target_env = "msvc")]
fn into(self) -> raw::git_treewalk_mode {
self as i32
}
#[cfg(not(target_env = "msvc"))]
fn into(self) -> raw::git_treewalk_mode {
self as u32
}
}

impl<'repo> Tree<'repo> {
/// Get the id (SHA1) of a repository object
pub fn id(&self) -> Oid {
Expand Down Expand Up @@ -126,12 +116,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,
treewalk_cb::<T> as raw::git_treewalk_cb,
&mut data as *mut _ as *mut c_void
));
Ok(())
}
}
Expand Down Expand Up @@ -599,4 +589,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();

assert!(tree.walk(TreeWalkMode::PreOrder, |_, _| { -1 }).is_err());
}
}

0 comments on commit 1cd7a94

Please sign in to comment.