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