Skip to content

Commit ef9c8ed

Browse files
kornelskialexcrichton
authored andcommitted
Don't panic in shallow clones (rust-lang#346)
1 parent 9e54c37 commit ef9c8ed

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/commit.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ pub struct Commit<'repo> {
1717
}
1818

1919
/// An iterator over the parent commits of a commit.
20+
///
21+
/// Aborts iteration when a commit cannot be found
2022
pub struct Parents<'commit, 'repo: 'commit> {
2123
range: Range<usize>,
2224
commit: &'commit Commit<'repo>,
2325
}
2426

2527
/// An iterator over the parent commits' ids of a commit.
28+
///
29+
/// Aborts iteration when a commit cannot be found
2630
pub struct ParentIds<'commit> {
2731
range: Range<usize>,
2832
commit: &'commit Commit<'commit>,
@@ -81,7 +85,7 @@ impl<'repo> Commit<'repo> {
8185
let bytes = unsafe {
8286
::opt_bytes(self, raw::git_commit_message(&*self.raw))
8387
};
84-
bytes.map(|b| str::from_utf8(b).unwrap())
88+
bytes.and_then(|b| str::from_utf8(b).ok())
8589
}
8690

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

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

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

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

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

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

0 commit comments

Comments
 (0)