mailmap.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package identity
  2. import (
  3. "strings"
  4. "gopkg.in/src-d/go-git.v4/plumbing/object"
  5. )
  6. // ParseMailmap parses the contents of .mailmap and returns the mapping
  7. // between signature parts. It does *not* follow the full signature
  8. // matching convention, that is, developers are identified by email
  9. // and by name independently.
  10. func ParseMailmap(contents string) map[string]object.Signature {
  11. mm := map[string]object.Signature{}
  12. lines := strings.Split(contents, "\n")
  13. for _, line := range lines {
  14. line = strings.TrimSpace(line)
  15. if line == "" {
  16. continue
  17. }
  18. if strings.HasPrefix(line, "#") {
  19. continue
  20. }
  21. if strings.LastIndex(line, ">") != len(line)-1 {
  22. continue
  23. }
  24. ltp := strings.LastIndex(line, "<")
  25. fromEmail := line[ltp+1 : len(line)-1]
  26. line = strings.TrimSpace(line[:ltp])
  27. gtp := strings.LastIndex(line, ">")
  28. fromName := ""
  29. if gtp != len(line)-1 {
  30. fromName = strings.TrimSpace(line[gtp+1:])
  31. }
  32. toEmail := ""
  33. if gtp > 0 {
  34. line = line[:gtp]
  35. ltp = strings.LastIndex(line, "<")
  36. toEmail = line[ltp+1:]
  37. line = strings.TrimSpace(line[:ltp])
  38. }
  39. toName := line
  40. if fromEmail != "" {
  41. mm[fromEmail] = object.Signature{Name: toName, Email: toEmail}
  42. }
  43. if fromName != "" {
  44. mm[fromName] = object.Signature{Name: toName, Email: toEmail}
  45. }
  46. }
  47. return mm
  48. }