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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::fmt;
use std::pin::Pin;
use std::ptr::NonNull;

#[cfg(test)]
mod test;

/// Loads a mailmap from the string passed in.
///
/// The format is the same as used by `git`; specifically:
///
/// * `Canonical Name <canonical email> Current Name <current email>`
///   * This changes authors matching both name and email to the canonical forms.
/// * `Canonical Name <current email>`
///   * This changes all entries with this email to new name, regardless of their current name.
/// * `Canonical Name <canonical email> <current email>`
///   * This changes all entries with the current email to the canonical name and email.
/// * `<canonical email> <current email>`
///   * This changes all entries with the current email to the canonical email.
pub struct Mailmap {
    buffer: Pin<Box<str>>,
    entries: Vec<RawMapEntry>,
}

impl fmt::Debug for Mailmap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut list = f.debug_list();
        for entry in &self.entries {
            // these entries were created from this buffer
            let entry = unsafe { entry.to_entry(&self.buffer) };
            list.entry(&entry);
        }
        list.finish()
    }
}

#[derive(Copy, Clone)]
struct RawMapEntry {
    canonical_name: Option<NonNull<str>>,
    canonical_email: Option<NonNull<str>>,
    current_name: Option<NonNull<str>>,
    current_email: Option<NonNull<str>>,
}

impl RawMapEntry {
    unsafe fn to_entry<'a>(self, _: &'a str) -> MapEntry<'a> {
        MapEntry {
            canonical_name: self.canonical_name.map(|v| &*v.as_ptr()),
            canonical_email: self.canonical_email.map(|v| &*v.as_ptr()),
            current_name: self.current_name.map(|v| &*v.as_ptr()),
            current_email: self.current_email.map(|v| &*v.as_ptr()),
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
struct MapEntry<'a> {
    canonical_name: Option<&'a str>,
    canonical_email: Option<&'a str>,
    current_name: Option<&'a str>,
    current_email: Option<&'a str>,
}

impl<'a> MapEntry<'a> {
    fn to_raw_entry(self) -> RawMapEntry {
        RawMapEntry {
            canonical_name: self.canonical_name.map(|v| v.into()),
            canonical_email: self.canonical_email.map(|v| v.into()),
            current_name: self.current_name.map(|v| v.into()),
            current_email: self.current_email.map(|v| v.into()),
        }
    }
}

#[derive(Clone, PartialEq, PartialOrd, Ord, Eq, Hash)]
pub struct Author {
    pub name: String,
    pub email: String,
}

impl fmt::Debug for Author {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} <{}>", self.name, self.email)
    }
}

impl Mailmap {
    pub fn from_string(file: String) -> Result<Mailmap, Box<dyn std::error::Error>> {
        let file = Pin::new(file.into_boxed_str());
        let mut entries = Vec::with_capacity(file.lines().count());
        for (idx, line) in file.lines().enumerate() {
            if let Some(entry) = parse_line(&line, idx + 1) {
                entries.push(entry.to_raw_entry());
            }
        }
        Ok(Mailmap {
            buffer: file,
            entries,
        })
    }

    pub fn canonicalize(&self, author: &Author) -> Author {
        for entry in &self.entries {
            // these entries were created from this buffer
            let entry = unsafe { entry.to_entry(&self.buffer) };
            if let Some(email) = entry.current_email {
                if let Some(name) = entry.current_name {
                    if author.name == name && author.email == email {
                        return Author {
                            name: entry.canonical_name.unwrap_or(&author.name).to_owned(),
                            email: entry.canonical_email.expect("canonical email").to_owned(),
                        };
                    }
                } else {
                    if author.email == email {
                        return Author {
                            name: entry.canonical_name.unwrap_or(&author.name).to_owned(),
                            email: entry.canonical_email.expect("canonical email").to_owned(),
                        };
                    }
                }
            }
        }

        author.clone()
    }
}

fn read_email<'a>(line: &mut &'a str) -> Option<&'a str> {
    if !line.starts_with('<') {
        return None;
    }

    let end = line
        .find('>')
        .unwrap_or_else(|| panic!("could not find email end in {:?}", line));
    let ret = &line[1..end];
    *line = &line[end + 1..];
    Some(ret)
}

fn read_name<'a>(line: &mut &'a str) -> Option<&'a str> {
    let end = if let Some(end) = line.find('<') {
        end
    } else {
        return None;
    };
    let ret = &line[..end].trim();
    *line = &line[end..];
    if ret.is_empty() {
        None
    } else {
        Some(ret)
    }
}

fn read_comment(line: &mut &str) -> bool {
    if line.trim().starts_with('#') {
        *line = "";
        true
    } else {
        false
    }
}

fn parse_line(mut line: &str, idx: usize) -> Option<MapEntry<'_>> {
    let mut entry = MapEntry {
        canonical_name: None,
        canonical_email: None,
        current_name: None,
        current_email: None,
    };
    loop {
        line = line.trim_start();
        if read_comment(&mut line) || line.trim().is_empty() {
            break;
        }

        if let Some(email) = read_email(&mut line) {
            if entry.canonical_email.is_none() {
                entry.canonical_email = Some(email);
            } else {
                if entry.current_email.is_some() {
                    eprintln!("malformed mailmap on line {}: too many emails", idx);
                } else {
                    entry.current_email = Some(email);
                }
            }
        } else if let Some(name) = read_name(&mut line) {
            if entry.canonical_name.is_none() {
                entry.canonical_name = Some(name);
            } else {
                if entry.current_name.is_some() {
                    eprintln!("malformed mailmap on line {}: too many names", idx);
                } else {
                    entry.current_name = Some(name);
                }
            }
        } else {
            break;
        }
    }

    if entry.canonical_email.is_some() && entry.current_email.is_none() {
        entry.current_email = entry.canonical_email;
    }

    if entry.canonical_name.is_some()
        || entry.canonical_email.is_some()
        || entry.current_name.is_some()
        || entry.current_email.is_some()
    {
        Some(entry)
    } else {
        None
    }
}