authors.go 6.9 KB

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