authors.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // +build ignore
  7. // Generates the list of contributors in gui/index.html based on contents of
  8. // AUTHORS.
  9. package main
  10. import (
  11. "bytes"
  12. "io/ioutil"
  13. "log"
  14. "math"
  15. "os"
  16. "os/exec"
  17. "regexp"
  18. "sort"
  19. "strings"
  20. )
  21. const htmlFile = "gui/default/syncthing/core/aboutModalView.html"
  22. type author struct {
  23. name string
  24. emails []string
  25. commits int
  26. log10commits int
  27. }
  28. func main() {
  29. authors := getAuthors()
  30. getContributions(authors)
  31. sort.Sort(byContributions(authors))
  32. var lines []string
  33. for _, author := range authors {
  34. lines = append(lines, author.name)
  35. }
  36. replacement := strings.Join(lines, ", ")
  37. authorsRe := regexp.MustCompile(`(?s)id="contributor-list">.*?</div>`)
  38. bs := readAll(htmlFile)
  39. bs = authorsRe.ReplaceAll(bs, []byte("id=\"contributor-list\">\n"+replacement+"\n </div>"))
  40. if err := ioutil.WriteFile(htmlFile, bs, 0644); err != nil {
  41. log.Fatal(err)
  42. }
  43. }
  44. func getAuthors() []author {
  45. bs := readAll("AUTHORS")
  46. lines := strings.Split(string(bs), "\n")
  47. var authors []author
  48. nameRe := regexp.MustCompile(`(.+?)\s+<`)
  49. authorRe := regexp.MustCompile(`<([^>]+)>`)
  50. for _, line := range lines {
  51. m := nameRe.FindStringSubmatch(line)
  52. if len(m) < 2 {
  53. continue
  54. }
  55. name := m[1]
  56. ms := authorRe.FindAllStringSubmatch(line, -1)
  57. if len(ms) == 0 {
  58. continue
  59. }
  60. var emails []string
  61. for i := range ms {
  62. emails = append(emails, ms[i][1])
  63. }
  64. a := author{
  65. name: name,
  66. emails: emails,
  67. }
  68. authors = append(authors, a)
  69. }
  70. return authors
  71. }
  72. func readAll(path string) []byte {
  73. fd, err := os.Open(path)
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. defer fd.Close()
  78. bs, err := ioutil.ReadAll(fd)
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. return bs
  83. }
  84. // Add number of commits per author to the author list.
  85. func getContributions(authors []author) {
  86. buf := new(bytes.Buffer)
  87. cmd := exec.Command("git", "log", "--pretty=format:%ae")
  88. cmd.Stdout = buf
  89. err := cmd.Run()
  90. if err != nil {
  91. log.Fatal(err)
  92. }
  93. next:
  94. for _, line := range strings.Split(buf.String(), "\n") {
  95. for i := range authors {
  96. for _, email := range authors[i].emails {
  97. if email == line {
  98. authors[i].commits++
  99. continue next
  100. }
  101. }
  102. }
  103. }
  104. for i := range authors {
  105. authors[i].log10commits = int(math.Log10(float64(authors[i].commits + 1)))
  106. }
  107. }
  108. type byContributions []author
  109. func (l byContributions) Len() int { return len(l) }
  110. // Sort first by log10(commits), then by name. This means that we first get
  111. // an alphabetic list of people with >= 1000 commits, then a list of people
  112. // with >= 100 commits, and so on.
  113. func (l byContributions) Less(a, b int) bool {
  114. if l[a].log10commits != l[b].log10commits {
  115. return l[a].log10commits > l[b].log10commits
  116. }
  117. return l[a].name < l[b].name
  118. }
  119. func (l byContributions) Swap(a, b int) { l[a], l[b] = l[b], l[a] }