mailmap.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package hercules
  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. ltp = strings.LastIndex(line, "<")
  35. toEmail = line[ltp+1 : gtp]
  36. line = strings.TrimSpace(line[:ltp])
  37. }
  38. toName := line
  39. if fromEmail != "" {
  40. mm[fromEmail] = object.Signature{Name: toName, Email: toEmail}
  41. }
  42. if fromName != "" {
  43. mm[fromName] = object.Signature{Name: toName, Email: toEmail}
  44. }
  45. }
  46. return mm
  47. }