Skip to content

Commit 1cd7a94

Browse files
committed
Propagate errors on Tree::walk
1 parent a2e05da commit 1cd7a94

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

libgit2-sys/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,7 @@ git_enum! {
741741
}
742742
}
743743

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

src/call.rs

+11
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ mod impls {
116116
}
117117
}
118118

119+
impl Convert<raw::git_treewalk_mode> for crate::TreeWalkMode {
120+
#[cfg(target_env = "msvc")]
121+
fn convert(&self) -> raw::git_treewalk_mode {
122+
*self as i32
123+
}
124+
#[cfg(not(target_env = "msvc"))]
125+
fn convert(&self) -> raw::git_treewalk_mode {
126+
*self as u32
127+
}
128+
}
129+
119130
impl Convert<raw::git_direction> for Direction {
120131
fn convert(&self) -> raw::git_direction {
121132
match *self {

src/tree.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct TreeIter<'tree> {
3636

3737
/// A binary indicator of whether a tree walk should be performed in pre-order
3838
/// or post-order.
39+
#[derive(Clone, Copy)]
3940
pub enum TreeWalkMode {
4041
/// Runs the traversal in pre-order.
4142
PreOrder = 0,
@@ -60,17 +61,6 @@ impl Into<i32> for TreeWalkResult {
6061
}
6162
}
6263

63-
impl Into<raw::git_treewalk_mode> for TreeWalkMode {
64-
#[cfg(target_env = "msvc")]
65-
fn into(self) -> raw::git_treewalk_mode {
66-
self as i32
67-
}
68-
#[cfg(not(target_env = "msvc"))]
69-
fn into(self) -> raw::git_treewalk_mode {
70-
self as u32
71-
}
72-
}
73-
7464
impl<'repo> Tree<'repo> {
7565
/// Get the id (SHA1) of a repository object
7666
pub fn id(&self) -> Oid {
@@ -126,12 +116,12 @@ impl<'repo> Tree<'repo> {
126116
let mut data = TreeWalkCbData {
127117
callback: &mut callback,
128118
};
129-
raw::git_tree_walk(
119+
try_call!(raw::git_tree_walk(
130120
self.raw(),
131-
mode.into(),
132-
Some(treewalk_cb::<T>),
133-
&mut data as *mut _ as *mut c_void,
134-
);
121+
mode,
122+
treewalk_cb::<T> as raw::git_treewalk_cb,
123+
&mut data as *mut _ as *mut c_void
124+
));
135125
Ok(())
136126
}
137127
}
@@ -599,4 +589,18 @@ mod tests {
599589
.unwrap();
600590
assert_eq!(ct, 8);
601591
}
592+
593+
#[test]
594+
fn tree_walk_error() {
595+
let (td, repo) = crate::test::repo_init();
596+
597+
setup_repo(&td, &repo);
598+
599+
let head = repo.head().unwrap();
600+
let target = head.target().unwrap();
601+
let commit = repo.find_commit(target).unwrap();
602+
let tree = repo.find_tree(commit.tree_id()).unwrap();
603+
604+
assert!(tree.walk(TreeWalkMode::PreOrder, |_, _| { -1 }).is_err());
605+
}
602606
}

0 commit comments

Comments
 (0)