gitoxide_core/
mailmap.rs

1use std::{collections::HashSet, io::Write, path::Path};
2
3use anyhow::{bail, Context};
4
5use crate::OutputFormat;
6
7pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=2;
8
9pub fn verify(path: impl AsRef<Path>, format: OutputFormat, mut out: impl Write) -> anyhow::Result<()> {
10    if format != OutputFormat::Human {
11        bail!("Only 'human' format is currently supported");
12    }
13    let path = path.as_ref();
14    let buf = std::fs::read(path).with_context(|| format!("Failed to read mailmap file at '{}'", path.display()))?;
15    let mut err_count = 0;
16    for err in gix::mailmap::parse(&buf).filter_map(Result::err) {
17        err_count += 1;
18        writeln!(out, "{err}")?;
19    }
20
21    let mut seen = HashSet::<(_, _)>::default();
22    for entry in gix::mailmap::parse(&buf).filter_map(Result::ok) {
23        if !seen.insert((entry.old_email(), entry.old_name())) {
24            writeln!(
25                out,
26                "NOTE: entry ({:?}, {:?}) -> ({:?}, {:?}) is being overwritten",
27                entry.old_email(),
28                entry.old_name(),
29                entry.new_email(),
30                entry.new_name()
31            )?;
32        }
33    }
34
35    if err_count == 0 {
36        writeln!(out, "{} lines OK", gix::mailmap::parse(&buf).count())?;
37        Ok(())
38    } else {
39        bail!("{} lines in '{}' could not be parsed", err_count, path.display());
40    }
41}