You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.4 KiB
57 lines
1.4 KiB
use crate::HashId;
|
|
use git2::Repository;
|
|
use std::sync::Arc;
|
|
|
|
/// Represent a commit on a repository
|
|
///
|
|
/// When creating a commit object, it is guaranteed that it exists in
|
|
/// the repository.
|
|
#[derive(Clone)]
|
|
pub struct Commit {
|
|
pub id: HashId,
|
|
repo: Arc<Repository>,
|
|
}
|
|
|
|
impl Commit {
|
|
/// Create a commit object and check if it exists in the repo
|
|
pub fn new(r: &Arc<Repository>, id: HashId) -> Option<Self> {
|
|
r.find_commit(id.to_oid()).ok().map(|_| Self {
|
|
id,
|
|
repo: Arc::clone(r),
|
|
})
|
|
}
|
|
|
|
pub fn id_str(&self) -> String {
|
|
self.id.to_string()
|
|
}
|
|
|
|
pub fn summary(&self) -> String {
|
|
self.find().summary().unwrap().into()
|
|
}
|
|
|
|
pub fn parent_count(&self) -> usize {
|
|
self.repo
|
|
.find_commit(self.id.to_oid())
|
|
.unwrap()
|
|
.parent_count()
|
|
}
|
|
|
|
/// Return the first parent, if it exists
|
|
pub fn first_parent(&self) -> Option<Self> {
|
|
self.find()
|
|
.parent(0)
|
|
.ok()
|
|
.and_then(|c| Self::new(&self.repo, c.id().into()))
|
|
}
|
|
|
|
pub fn parent(&self, num: usize) -> Option<Self> {
|
|
self.find()
|
|
.parent(num)
|
|
.ok()
|
|
.and_then(|c| Self::new(&self.repo, c.id().into()))
|
|
}
|
|
|
|
fn find(&self) -> git2::Commit {
|
|
self.repo.find_commit(self.id.to_oid()).unwrap()
|
|
}
|
|
}
|
|
|