Skip to content

Commit

Permalink
bring back the no-repo commitgraph for stress-tests to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jun 12, 2023
1 parent 750b07a commit ff8d42a
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
2 changes: 2 additions & 0 deletions gitoxide-core/src/commitgraph/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod verify;
pub use verify::function::verify;
79 changes: 79 additions & 0 deletions gitoxide-core/src/commitgraph/verify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use crate::OutputFormat;

/// A general purpose context for many operations provided here
pub struct Context<W1: std::io::Write, W2: std::io::Write> {
/// A stream to which to output errors
pub err: W2,
/// A stream to which to output operation results
pub out: W1,
pub output_statistics: Option<OutputFormat>,
}

impl Default for Context<Vec<u8>, Vec<u8>> {
fn default() -> Self {
Context {
err: Vec::new(),
out: Vec::new(),
output_statistics: None,
}
}
}

pub(crate) mod function {
use crate::OutputFormat;
use anyhow::{Context as AnyhowContext, Result};
use gix::commitgraph::{verify::Outcome, Graph};
use std::{io, path::Path};

pub fn verify<W1, W2>(
path: impl AsRef<Path>,
super::Context {
err: _err,
mut out,
output_statistics,
}: super::Context<W1, W2>,
) -> Result<gix::commitgraph::verify::Outcome>
where
W1: io::Write,
W2: io::Write,
{
let g = Graph::at(path).with_context(|| "Could not open commit graph")?;

#[allow(clippy::unnecessary_wraps, unknown_lints)]
fn noop_processor(_commit: &gix::commitgraph::file::Commit<'_>) -> std::result::Result<(), std::fmt::Error> {
Ok(())
}
let stats = g
.verify_integrity(noop_processor)
.with_context(|| "Verification failure")?;

#[cfg_attr(not(feature = "serde"), allow(clippy::single_match))]
match output_statistics {
Some(OutputFormat::Human) => drop(print_human_output(&mut out, &stats)),
#[cfg(feature = "serde")]
Some(OutputFormat::Json) => serde_json::to_writer_pretty(out, &stats)?,
_ => {}
}

Ok(stats)
}

fn print_human_output(out: &mut impl io::Write, stats: &Outcome) -> io::Result<()> {
writeln!(out, "number of commits with the given number of parents")?;
let mut parent_counts: Vec<_> = stats.parent_counts.iter().map(|(a, b)| (*a, *b)).collect();
parent_counts.sort_by_key(|e| e.0);
for (parent_count, commit_count) in parent_counts.into_iter() {
writeln!(out, "\t{parent_count:>2}: {commit_count}")?;
}
writeln!(out, "\t->: {}", stats.num_commits)?;

write!(out, "\nlongest path length between two commits: ")?;
if let Some(n) = stats.longest_path_length {
writeln!(out, "{n}")?;
} else {
writeln!(out, "unknown")?;
}

Ok(())
}
}
1 change: 1 addition & 0 deletions gitoxide-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl FromStr for OutputFormat {
}
}

pub mod commitgraph;
pub mod net;

#[cfg(feature = "estimate-hours")]
Expand Down
26 changes: 23 additions & 3 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ use gitoxide_core as core;
use gitoxide_core::pack::verify;
use gix::bstr::io::BufReadExt;

use crate::plumbing::options::commitgraph;
use crate::{
plumbing::{
options::{
attributes, commit, config, credential, exclude, free, index, mailmap, odb, revision, tree, Args,
Subcommands,
attributes, commit, commitgraph, config, credential, exclude, free, index, mailmap, odb, revision, tree,
Args, Subcommands,
},
show_progress,
},
Expand Down Expand Up @@ -303,6 +302,27 @@ pub fn main() -> Result<()> {
)
.map(|_| ()),
Subcommands::Free(subcommands) => match subcommands {
free::Subcommands::CommitGraph(cmd) => match cmd {
free::commitgraph::Subcommands::Verify { path, statistics } => prepare_and_run(
"commitgraph-verify",
auto_verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| {
let output_statistics = if statistics { Some(format) } else { None };
core::commitgraph::verify(
path,
core::commitgraph::verify::Context {
err,
out,
output_statistics,
},
)
},
)
.map(|_| ()),
},
free::Subcommands::Index(free::index::Platform {
object_hash,
index_path,
Expand Down
20 changes: 20 additions & 0 deletions src/plumbing/options/free.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[derive(Debug, clap::Subcommand)]
#[clap(visible_alias = "no-repo")]
pub enum Subcommands {
/// Subcommands for interacting with commit-graphs
#[clap(subcommand)]
CommitGraph(commitgraph::Subcommands),
/// Subcommands for interacting with mailmaps
Mailmap {
#[clap(flatten)]
Expand All @@ -13,6 +16,23 @@ pub enum Subcommands {
Index(index::Platform),
}

///
pub mod commitgraph {
use std::path::PathBuf;

#[derive(Debug, clap::Subcommand)]
pub enum Subcommands {
/// Verify the integrity of a commit graph
Verify {
/// The path to '.git/objects/info/', '.git/objects/info/commit-graphs/', or '.git/objects/info/commit-graph' to validate.
path: PathBuf,
/// output statistical information about the pack
#[clap(long, short = 's')]
statistics: bool,
},
}
}

pub mod index {
use std::path::PathBuf;

Expand Down

0 comments on commit ff8d42a

Please sign in to comment.