Skip to content

Don't panic in shallow clones #346

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

Merged
merged 1 commit into from
Aug 14, 2018
Merged
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
18 changes: 13 additions & 5 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ pub struct Commit<'repo> {
}

/// An iterator over the parent commits of a commit.
///
/// Aborts iteration when a commit cannot be found
pub struct Parents<'commit, 'repo: 'commit> {
range: Range<usize>,
commit: &'commit Commit<'repo>,
}

/// An iterator over the parent commits' ids of a commit.
///
/// Aborts iteration when a commit cannot be found
pub struct ParentIds<'commit> {
range: Range<usize>,
commit: &'commit Commit<'commit>,
Expand Down Expand Up @@ -81,7 +85,7 @@ impl<'repo> Commit<'repo> {
let bytes = unsafe {
::opt_bytes(self, raw::git_commit_message(&*self.raw))
};
bytes.map(|b| str::from_utf8(b).unwrap())
bytes.and_then(|b| str::from_utf8(b).ok())
}

/// Get the full raw message of a commit.
Expand Down Expand Up @@ -273,33 +277,37 @@ impl<'repo> ::std::fmt::Debug for Commit<'repo> {
}
}

/// Aborts iteration when a commit cannot be found
impl<'repo, 'commit> Iterator for Parents<'commit, 'repo> {
type Item = Commit<'repo>;
fn next(&mut self) -> Option<Commit<'repo>> {
self.range.next().map(|i| self.commit.parent(i).unwrap())
self.range.next().and_then(|i| self.commit.parent(i).ok())
}
fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
}

/// Aborts iteration when a commit cannot be found
impl<'repo, 'commit> DoubleEndedIterator for Parents<'commit, 'repo> {
fn next_back(&mut self) -> Option<Commit<'repo>> {
self.range.next_back().map(|i| self.commit.parent(i).unwrap())
self.range.next_back().and_then(|i| self.commit.parent(i).ok())
}
}

impl<'repo, 'commit> ExactSizeIterator for Parents<'commit, 'repo> {}

/// Aborts iteration when a commit cannot be found
impl<'commit> Iterator for ParentIds<'commit> {
type Item = Oid;
fn next(&mut self) -> Option<Oid> {
self.range.next().map(|i| self.commit.parent_id(i).unwrap())
self.range.next().and_then(|i| self.commit.parent_id(i).ok())
}
fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
}

/// Aborts iteration when a commit cannot be found
impl<'commit> DoubleEndedIterator for ParentIds<'commit> {
fn next_back(&mut self) -> Option<Oid> {
self.range.next_back().map(|i| self.commit.parent_id(i).unwrap())
self.range.next_back().and_then(|i| self.commit.parent_id(i).ok())
}
}

Expand Down