authors.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 https://mozilla.org/MPL/2.0/.
  6. //go:build ignore
  7. // +build ignore
  8. // Generates the list of contributors in gui/index.html based on contents of
  9. // AUTHORS.
  10. package main
  11. import (
  12. "bytes"
  13. "fmt"
  14. "io"
  15. "log"
  16. "math"
  17. "os"
  18. "os/exec"
  19. "regexp"
  20. "sort"
  21. "strings"
  22. )
  23. const htmlFile = "gui/default/syncthing/core/aboutModalView.html"
  24. var (
  25. nicknameRe = regexp.MustCompile(`\(([^\s]*)\)`)
  26. emailRe = regexp.MustCompile(`<([^\s]*)>`)
  27. authorBotsRegexps = []string{
  28. `\[bot\]`,
  29. `Syncthing.*Automation`,
  30. }
  31. )
  32. var authorBotsRe = regexp.MustCompile(strings.Join(authorBotsRegexps, "|"))
  33. const authorsHeader = `# This is the official list of Syncthing authors for copyright purposes.
  34. #
  35. # THIS FILE IS MOSTLY AUTO GENERATED. IF YOU'VE MADE A COMMIT TO THE
  36. # REPOSITORY YOU WILL BE ADDED HERE AUTOMATICALLY WITHOUT THE NEED FOR
  37. # ANY MANUAL ACTION.
  38. #
  39. # That said, you are welcome to correct your name or add a nickname / GitHub
  40. # user name as appropriate. The format is:
  41. #
  42. # Name Name Name (nickname) <[email protected]> <[email protected]>
  43. #
  44. # The in-GUI authors list is periodically automatically updated from the
  45. # contents of this file.
  46. #
  47. `
  48. type author struct {
  49. name string
  50. nickname string
  51. emails []string
  52. commits int
  53. log10commits int
  54. }
  55. func main() {
  56. // Read authors from the AUTHORS file
  57. authors := getAuthors()
  58. // Grab the set of thus known email addresses
  59. listed := make(stringSet)
  60. names := make(map[string]int)
  61. for i, a := range authors {
  62. names[a.name] = i
  63. for _, e := range a.emails {
  64. listed.add(e)
  65. }
  66. }
  67. // Grab the set of all known authors based on the git log, and add any
  68. // missing ones to the authors list.
  69. all := allAuthors()
  70. for email, name := range all {
  71. if listed.has(email) {
  72. continue
  73. }
  74. if _, ok := names[name]; ok && name != "" {
  75. // We found a match on name
  76. authors[names[name]].emails = append(authors[names[name]].emails, email)
  77. listed.add(email)
  78. continue
  79. }
  80. authors = append(authors, author{
  81. name: name,
  82. emails: []string{email},
  83. })
  84. names[name] = len(authors) - 1
  85. listed.add(email)
  86. }
  87. // Write author names in GUI about modal
  88. getContributions(authors)
  89. sort.Sort(byContributions(authors))
  90. var lines []string
  91. for _, author := range authors {
  92. if authorBotsRe.MatchString(author.name) {
  93. // Only humans are eligible, pending future legislation to the
  94. // contrary.
  95. continue
  96. }
  97. lines = append(lines, author.name)
  98. }
  99. replacement := strings.Join(lines, ", ")
  100. authorsRe := regexp.MustCompile(`(?s)id="contributor-list">.*?</div>`)
  101. bs := readAll(htmlFile)
  102. bs = authorsRe.ReplaceAll(bs, []byte("id=\"contributor-list\">\n"+replacement+"\n </div>"))
  103. if err := os.WriteFile(htmlFile, bs, 0644); err != nil {
  104. log.Fatal(err)
  105. }
  106. // Write AUTHORS file
  107. sort.Sort(byName(authors))
  108. out, err := os.Create("AUTHORS")
  109. if err != nil {
  110. log.Fatal(err)
  111. }
  112. fmt.Fprintf(out, "%s\n", authorsHeader)
  113. for _, author := range authors {
  114. fmt.Fprintf(out, "%s", author.name)
  115. if author.nickname != "" {
  116. fmt.Fprintf(out, " (%s)", author.nickname)
  117. }
  118. for _, email := range author.emails {
  119. fmt.Fprintf(out, " <%s>", email)
  120. }
  121. fmt.Fprintf(out, "\n")
  122. }
  123. out.Close()
  124. }
  125. func getAuthors() []author {
  126. bs := readAll("AUTHORS")
  127. lines := strings.Split(string(bs), "\n")
  128. var authors []author
  129. for _, line := range lines {
  130. if len(line) == 0 || line[0] == '#' {
  131. continue
  132. }
  133. fields := strings.Fields(line)
  134. var author author
  135. for _, field := range fields {
  136. if m := nicknameRe.FindStringSubmatch(field); len(m) > 1 {
  137. author.nickname = m[1]
  138. } else if m := emailRe.FindStringSubmatch(field); len(m) > 1 {
  139. author.emails = append(author.emails, m[1])
  140. } else {
  141. if author.name == "" {
  142. author.name = field
  143. } else {
  144. author.name = author.name + " " + field
  145. }
  146. }
  147. }
  148. authors = append(authors, author)
  149. }
  150. return authors
  151. }
  152. func readAll(path string) []byte {
  153. fd, err := os.Open(path)
  154. if err != nil {
  155. log.Fatal(err)
  156. }
  157. defer fd.Close()
  158. bs, err := io.ReadAll(fd)
  159. if err != nil {
  160. log.Fatal(err)
  161. }
  162. return bs
  163. }
  164. // Add number of commits per author to the author list.
  165. func getContributions(authors []author) {
  166. buf := new(bytes.Buffer)
  167. cmd := exec.Command("git", "log", "--pretty=format:%ae")
  168. cmd.Stdout = buf
  169. err := cmd.Run()
  170. if err != nil {
  171. log.Fatal(err)
  172. }
  173. next:
  174. for _, line := range strings.Split(buf.String(), "\n") {
  175. for i := range authors {
  176. for _, email := range authors[i].emails {
  177. if email == line {
  178. authors[i].commits++
  179. continue next
  180. }
  181. }
  182. }
  183. }
  184. for i := range authors {
  185. authors[i].log10commits = int(math.Log10(float64(authors[i].commits + 1)))
  186. }
  187. }
  188. // list of commits that we don't include in our author file; because they
  189. // are legacy things that don't affect code, are committed with incorrect
  190. // address, or for other reasons.
  191. var excludeCommits = stringSetFromStrings([]string{
  192. "a9339d0627fff439879d157c75077f02c9fac61b",
  193. "254c63763a3ad42fd82259f1767db526cff94a14",
  194. "32a76901a91ff0f663db6f0830e0aedec946e4d0",
  195. "bc7639b0ffcea52b2197efb1c0bb68b338d1c915",
  196. "9bdcadf6345aba3a939e9e58d85b89dbe9d44bc9",
  197. "b933e9666abdfcd22919dd458c930d944e1e1b7f",
  198. "b84d960a81c1282a79e2b9477558de4f1af6faae",
  199. })
  200. // allAuthors returns the set of authors in the git commit log, except those
  201. // in excluded commits.
  202. func allAuthors() map[string]string {
  203. args := append([]string{"log", "--format=%H %ae %an"})
  204. cmd := exec.Command("git", args...)
  205. bs, err := cmd.Output()
  206. if err != nil {
  207. log.Fatal("git:", err)
  208. }
  209. names := make(map[string]string)
  210. for _, line := range bytes.Split(bs, []byte{'\n'}) {
  211. fields := strings.SplitN(string(line), " ", 3)
  212. if len(fields) != 3 {
  213. continue
  214. }
  215. hash, email, name := fields[0], fields[1], fields[2]
  216. if excludeCommits.has(hash) {
  217. continue
  218. }
  219. if names[email] == "" {
  220. names[email] = name
  221. }
  222. }
  223. return names
  224. }
  225. type byContributions []author
  226. func (l byContributions) Len() int { return len(l) }
  227. // Sort first by log10(commits), then by name. This means that we first get
  228. // an alphabetic list of people with >= 1000 commits, then a list of people
  229. // with >= 100 commits, and so on.
  230. func (l byContributions) Less(a, b int) bool {
  231. if l[a].log10commits != l[b].log10commits {
  232. return l[a].log10commits > l[b].log10commits
  233. }
  234. return l[a].name < l[b].name
  235. }
  236. func (l byContributions) Swap(a, b int) { l[a], l[b] = l[b], l[a] }
  237. type byName []author
  238. func (l byName) Len() int { return len(l) }
  239. func (l byName) Less(a, b int) bool {
  240. aname := strings.ToLower(l[a].name)
  241. bname := strings.ToLower(l[b].name)
  242. return aname < bname
  243. }
  244. func (l byName) Swap(a, b int) { l[a], l[b] = l[b], l[a] }
  245. // A simple string set type
  246. type stringSet map[string]struct{}
  247. func stringSetFromStrings(ss []string) stringSet {
  248. s := make(stringSet)
  249. for _, e := range ss {
  250. s.add(e)
  251. }
  252. return s
  253. }
  254. func (s stringSet) add(e string) {
  255. s[e] = struct{}{}
  256. }
  257. func (s stringSet) has(e string) bool {
  258. _, ok := s[e]
  259. return ok
  260. }
  261. func (s stringSet) except(other stringSet) stringSet {
  262. diff := make(stringSet)
  263. for e := range s {
  264. if !other.has(e) {
  265. diff.add(e)
  266. }
  267. }
  268. return diff
  269. }