gitoxide_core/repository/commitgraph/
verify.rs1use crate::OutputFormat;
2
3pub struct Context<W1: std::io::Write, W2: std::io::Write> {
5 pub err: W2,
7 pub out: W1,
9 pub output_statistics: Option<OutputFormat>,
10}
11
12pub(crate) mod function {
13 use std::io;
14
15 use anyhow::{Context as AnyhowContext, Result};
16
17 use crate::{repository::commitgraph::verify::Context, OutputFormat};
18
19 pub fn verify<W1, W2>(
20 repo: gix::Repository,
21 Context {
22 err: _err,
23 mut out,
24 output_statistics,
25 }: Context<W1, W2>,
26 ) -> Result<gix::commitgraph::verify::Outcome>
27 where
28 W1: io::Write,
29 W2: io::Write,
30 {
31 let g = repo.commit_graph()?;
32
33 #[allow(clippy::unnecessary_wraps, unknown_lints)]
34 fn noop_processor(_commit: &gix::commitgraph::file::Commit<'_>) -> std::result::Result<(), std::fmt::Error> {
35 Ok(())
36 }
37 let stats = g
38 .verify_integrity(noop_processor)
39 .with_context(|| "Verification failure")?;
40
41 #[cfg_attr(not(feature = "serde"), allow(clippy::single_match))]
42 match output_statistics {
43 Some(OutputFormat::Human) => drop(print_human_output(&mut out, &stats)),
44 #[cfg(feature = "serde")]
45 Some(OutputFormat::Json) => serde_json::to_writer_pretty(out, &stats)?,
46 _ => {}
47 }
48
49 Ok(stats)
50 }
51
52 fn print_human_output(out: &mut impl io::Write, stats: &gix::commitgraph::verify::Outcome) -> io::Result<()> {
53 writeln!(out, "number of commits with the given number of parents")?;
54 let mut parent_counts: Vec<_> = stats.parent_counts.iter().map(|(a, b)| (*a, *b)).collect();
55 parent_counts.sort_by_key(|e| e.0);
56 for (parent_count, commit_count) in parent_counts.into_iter() {
57 writeln!(out, "\t{parent_count:>2}: {commit_count}")?;
58 }
59 writeln!(out, "\t->: {}", stats.num_commits)?;
60
61 write!(out, "\nlongest path length between two commits: ")?;
62 if let Some(n) = stats.longest_path_length {
63 writeln!(out, "{n}")?;
64 } else {
65 writeln!(out, "unknown")?;
66 }
67
68 Ok(())
69 }
70}