Skip to content

Commit

Permalink
feat: gix mailmap verify command (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Mar 29, 2022
1 parent e3bc1b4 commit 384ed66
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions gitoxide-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub mod commitgraph;
#[cfg(feature = "estimate-hours")]
pub mod hours;
pub mod index;
pub mod mailmap;
#[cfg(feature = "organize")]
pub mod organize;
pub mod pack;
Expand Down
26 changes: 26 additions & 0 deletions gitoxide-core/src/mailmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::OutputFormat;
use anyhow::{bail, Context};
use git_repository as git;
use std::io::Write;
use std::path::Path;

pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=2;

pub fn verify(path: impl AsRef<Path>, format: OutputFormat, mut out: impl Write) -> anyhow::Result<()> {
if format != OutputFormat::Human {
bail!("Only 'human' format is currently supported");
}
let path = path.as_ref();
let buf = std::fs::read(path).with_context(|| format!("Failed to read mailmap file at '{}'", path.display()))?;
let mut err_count = 0;
for err in git::mailmap::parse(&buf).filter_map(Result::err) {
err_count += 1;
writeln!(out, "{}", err)?;
}
if err_count == 0 {
writeln!(out, "{} lines OK", git::mailmap::parse(&buf).count())?;
Ok(())
} else {
bail!("{} lines in '{}' could not be parsed", err_count, path.display());
}
}
11 changes: 11 additions & 0 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use clap::Parser;
use gitoxide_core as core;
use gitoxide_core::pack::verify;

use crate::plumbing::options::mailmap;
use crate::plumbing::options::pack::multi_index;
#[cfg(any(feature = "gitoxide-core-async-client", feature = "gitoxide-core-blocking-client"))]
use crate::plumbing::options::remote;
Expand Down Expand Up @@ -74,6 +75,16 @@ pub fn main() -> Result<()> {
})?;

match cmd {
Subcommands::Mailmap(mailmap::Platform { path, cmd }) => match cmd {
mailmap::Subcommands::Verify => prepare_and_run(
"mailmap-verify",
verbose,
progress,
progress_keep_open,
core::mailmap::PROGRESS_RANGE,
move |_progress, out, _err| core::mailmap::verify(path, format, out),
),
},
Subcommands::Index(index::Platform {
object_hash,
index_path,
Expand Down
24 changes: 24 additions & 0 deletions src/plumbing/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub enum Subcommands {
Index(index::Platform),
/// Subcommands for interacting with entire git repositories
Repository(repo::Platform),
/// Subcommands for interacting with mailmaps
Mailmap(mailmap::Platform),
}

///
Expand Down Expand Up @@ -448,6 +450,28 @@ pub mod index {
}
}

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

#[derive(Debug, clap::Parser)]
pub struct Platform {
/// The path to the mailmap file.
#[clap(short = 'p', long, default_value = ".mailmap")]
pub path: PathBuf,

/// Subcommands
#[clap(subcommand)]
pub cmd: Subcommands,
}

#[derive(Debug, clap::Subcommand)]
pub enum Subcommands {
/// Parse all entries in the mailmap and report malformed lines.
Verify,
}
}

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

0 comments on commit 384ed66

Please sign in to comment.