Skip to content

Commit

Permalink
Change revwalk to iterate over Result<Oid>
Browse files Browse the repository at this point in the history
Turns out iteration can fail! Probably shouldn't be swallowing errors...

Closes rust-lang#112
  • Loading branch information
alexcrichton committed Feb 24, 2016
1 parent 2f45528 commit c9d1ed0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ fn run(args: &Args) -> Result<(), Error> {
($e:expr) => (match $e { Ok(t) => t, Err(e) => return Some(Err(e)) })
}
let revwalk = revwalk.filter_map(|id| {
let id = filter_try!(id);
let commit = filter_try!(repo.find_commit(id));
let parents = commit.parents().len();
if parents < args.min_parents() { return None }
Expand Down
1 change: 1 addition & 0 deletions examples/rev-list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn run(args: &Args) -> Result<(), git2::Error> {
}

for id in revwalk {
let id = try!(id);
println!("{}", id);
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn try(ret: libc::c_int) -> Result<libc::c_int, Error> {
}
}

fn last_error(code: libc::c_int) -> Error {
pub fn last_error(code: libc::c_int) -> Error {
// Apparently libgit2 isn't necessarily guaranteed to set the last error
// whenever a function returns a negative value!
Error::last_error(code).unwrap_or_else(|| {
Expand Down
15 changes: 7 additions & 8 deletions src/revwalk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,23 @@ impl<'repo> Drop for Revwalk<'repo> {
}

impl<'repo> Iterator for Revwalk<'repo> {
type Item = Oid;
fn next(&mut self) -> Option<Oid> {
type Item = Result<Oid, Error>;
fn next(&mut self) -> Option<Result<Oid, Error>> {
let mut out: raw::git_oid = raw::git_oid{ id: [0; raw::GIT_OID_RAWSZ] };
unsafe {
match raw::git_revwalk_next(&mut out, self.raw()) {
0 => (),
_ => return None,
raw::GIT_ITEROVER => return None,
err => return Some(Err(::call::last_error(err))),
}

Some(Binding::from_raw(&out as *const _))
Some(Ok(Binding::from_raw(&out as *const _)))
}
}
}

#[cfg(test)]
mod tests {
extern crate std;
use std::vec::{Vec};

#[test]
fn smoke() {
let (_td, repo) = ::test::repo_init();
Expand All @@ -192,7 +190,8 @@ mod tests {
let mut walk = repo.revwalk().unwrap();
walk.push(target).unwrap();

let oids: Vec<::Oid> = walk.by_ref().collect();
let oids: Vec<::Oid> = walk.by_ref().collect::<Result<Vec<_>, _>>()
.unwrap();

assert_eq!(oids.len(), 1);
assert_eq!(oids[0], target);
Expand Down

0 comments on commit c9d1ed0

Please sign in to comment.