-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmailmap.go
More file actions
55 lines (47 loc) · 1.36 KB
/
mailmap.go
File metadata and controls
55 lines (47 loc) · 1.36 KB
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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"strings"
)
// ReadMailMap reads a .mailmap file and returns the relation mapped amongst the emails.
func ReadMailMap(reader io.Reader) (map[string]*MailMapEntry, error) {
if reader == nil {
return nil, nil
}
mailmap := make(map[string]*MailMapEntry)
scanner := bufio.NewScanner(reader)
utf8bom := []byte{0xEF, 0xBB, 0xBF} //UTF8 Byte order mark
// Regular Expression to extract the fields required. Only uses proper email and commit email
pattern := regexp.MustCompile(`^(?P<pname>[^<]+)?(?:\\s+)?(?:<(?P<pmail>.*?)>)?(?:\\s+)?(?P<cname>[^<]+)?(?:\\s+)?(?:<(?P<cmail>.*?)>)?$`)
currentLine := 0
for scanner.Scan() {
scannedBytes := scanner.Bytes()
if currentLine == 0 {
scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
}
currentLine++
row := string(scannedBytes)
if strings.HasPrefix(row, "#") {
continue
}
if row := strings.TrimSpace(row); row == "" {
continue
}
groups := pattern.FindStringSubmatch(row)
entry := MailMapEntry{groups[2], groups[4]}
if _, hasKey := mailmap[entry.CommitEmail]; !hasKey {
mailmap[entry.CommitEmail] = &entry
}
if _, hasKey := mailmap[entry.ProperEmail]; !hasKey {
mailmap[entry.ProperEmail] = &entry
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("Error reading .mailmap: %v", err)
}
return mailmap, nil
}