use crate::error::Result;
use crate::repo::{CheckIn, Repository};
pub struct HistoryBuilder<'a> {
repo: &'a Repository,
}
impl<'a> HistoryBuilder<'a> {
pub(crate) fn new(repo: &'a Repository) -> Self {
Self { repo }
}
pub fn trunk_tip(&self) -> Result<CheckIn> {
self.repo.branch_tip_internal("trunk")
}
pub fn branch_tip(&self, branch: &str) -> Result<CheckIn> {
self.repo.branch_tip_internal(branch)
}
pub fn recent(&self, limit: usize) -> Result<Vec<CheckIn>> {
self.repo.recent_checkins_internal(limit)
}
pub fn get(&self, hash: &str) -> Result<CheckIn> {
self.repo.get_checkin_internal(hash)
}
pub fn on_branch(&self, branch: &str) -> HistoryQuery<'a> {
HistoryQuery {
repo: self.repo,
branch: Some(branch.to_string()),
limit: None,
}
}
pub fn on_trunk(&self) -> HistoryQuery<'a> {
HistoryQuery {
repo: self.repo,
branch: Some("trunk".to_string()),
limit: None,
}
}
}
pub struct HistoryQuery<'a> {
repo: &'a Repository,
branch: Option<String>,
limit: Option<usize>,
}
impl<'a> HistoryQuery<'a> {
pub fn limit(mut self, n: usize) -> Self {
self.limit = Some(n);
self
}
pub fn list(&self) -> Result<Vec<CheckIn>> {
let limit = self.limit.unwrap_or(100);
let all = self.repo.recent_checkins_internal(limit * 2)?;
if let Some(_branch) = &self.branch {
Ok(all.into_iter().take(limit).collect())
} else {
Ok(all.into_iter().take(limit).collect())
}
}
}