use crate::error::Result;
use crate::logic::{aggregate, filter, render, sort};
use crate::model::Options;
use crate::repo::{self, Repo, WalkedCommit};
pub fn run(repo: &Repo, opts: &Options) -> Result<String> {
let walked = repo.walk(&opts.range, opts.reviews)?;
let mut output = String::new();
output.push_str(&stats_section(repo, opts, &walked)?);
if opts.reviews {
let reviews = aggregate::aggregate_reviews(walked.iter().map(|w| &w.meta), opts.email);
if !reviews.is_empty() {
output.push('\n');
output.push_str(&render::render_reviews(&reviews));
output.push('\n');
}
}
Ok(output)
}
fn stats_section(repo: &Repo, opts: &Options, walked: &[WalkedCommit]) -> Result<String> {
let authors = filter::compile_authors(&opts.authors)?;
let since = repo::parse_date(opts.since.as_deref())?;
let until = repo::parse_date(opts.until.as_deref())?;
let kept_idx = filter::keep_indices(walked.iter().map(|w| &w.meta), &authors, since, until);
let kept: Vec<&WalkedCommit> = kept_idx.iter().map(|&i| &walked[i]).collect();
let diffs = repo.numstats(&kept)?;
let rows: Vec<aggregate::CommitStat> = kept
.iter()
.zip(diffs)
.map(|(w, diff)| aggregate::CommitStat {
author_key: aggregate::author_key(&w.meta.author, opts.email),
diff,
})
.collect();
let mut stats = aggregate::aggregate(&rows);
if stats.is_empty() {
return Ok(String::new());
}
sort::sort_stats(&mut stats, opts.sort, opts.reverse);
stats.push(aggregate::compute_totals(&stats));
let mut section = render::render_stats(&stats);
section.push('\n');
Ok(section)
}