gitoxide_core/repository/commitgraph/
verify.rs

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