1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use anyhow::bail;
use gix::bstr::{BString, ByteSlice};
use std::io;

#[cfg(feature = "serde")]
use gix::mailmap::Entry;

use crate::OutputFormat;

#[cfg(feature = "serde")]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
struct JsonEntry {
    new_name: Option<String>,
    new_email: Option<String>,
    old_name: Option<String>,
    old_email: String,
}

#[cfg(feature = "serde")]
impl<'a> From<Entry<'a>> for JsonEntry {
    fn from(v: Entry<'a>) -> Self {
        use gix::bstr::ByteSlice;
        JsonEntry {
            new_name: v.new_name().map(|s| s.to_str_lossy().into_owned()),
            new_email: v.new_email().map(|s| s.to_str_lossy().into_owned()),
            old_name: v.old_name().map(|s| s.to_str_lossy().into_owned()),
            old_email: v.old_email().to_str_lossy().into_owned(),
        }
    }
}

pub fn entries(
    repo: gix::Repository,
    format: OutputFormat,
    #[cfg_attr(not(feature = "serde"), allow(unused_variables))] out: impl io::Write,
    mut err: impl io::Write,
) -> anyhow::Result<()> {
    if format == OutputFormat::Human {
        writeln!(err, "Defaulting to JSON as human format isn't implemented").ok();
    }

    let mut mailmap = gix::mailmap::Snapshot::default();
    if let Err(e) = repo.open_mailmap_into(&mut mailmap) {
        writeln!(err, "Error while loading mailmap, the first error is: {e}").ok();
    }

    #[cfg(feature = "serde")]
    serde_json::to_writer_pretty(out, &mailmap.iter().map(JsonEntry::from).collect::<Vec<_>>())?;

    Ok(())
}

pub fn check(
    repo: gix::Repository,
    format: OutputFormat,
    contacts: Vec<BString>,
    mut out: impl io::Write,
    mut err: impl io::Write,
) -> anyhow::Result<()> {
    if format != OutputFormat::Human {
        bail!("Only human output is supported right now");
    }
    if contacts.is_empty() {
        bail!("specify at least one contact to run through the mailmap")
    }

    let mut mailmap = gix::mailmap::Snapshot::default();
    if let Err(err) = repo.open_mailmap_into(&mut mailmap) {
        bail!(err);
    }

    let mut buf = Vec::new();
    for contact in contacts {
        let actor = match gix::actor::IdentityRef::from_bytes::<()>(&contact) {
            Ok(a) => a,
            Err(_) => {
                let Some(email) = contact
                    .trim_start()
                    .strip_prefix(b"<")
                    .and_then(|rest| rest.trim_end().strip_suffix(b">"))
                else {
                    writeln!(err, "Failed to parse contact '{contact}' - skipping")?;
                    continue;
                };
                gix::actor::IdentityRef {
                    name: "".into(),
                    email: email.into(),
                }
            }
        };
        let resolved = mailmap.resolve_cow(gix::actor::SignatureRef {
            name: actor.name,
            email: actor.email,
            time: Default::default(),
        });
        let resolved = gix::actor::IdentityRef {
            name: resolved.name.as_ref(),
            email: resolved.email.as_ref(),
        };
        buf.clear();
        resolved.write_to(&mut buf)?;

        out.write_all(&buf)?;
        out.write_all(b"\n")?;
    }
    Ok(())
}