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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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>;
*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());
}
}