authors.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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) <email1@example.com> <email2@example.com>
  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. // Format is hash, email, name, newline, body. The body is indented with
  204. // one space, to differentiate from the hash lines.
  205. args := append([]string{"log", "--format=%H %ae %an%n%w(,1,1)%b"})
  206. cmd := exec.Command("git", args...)
  207. bs, err := cmd.Output()
  208. if err != nil {
  209. log.Fatal("git:", err)
  210. }
  211. coAuthoredPrefix := "Co-authored-by: "
  212. names := make(map[string]string)
  213. skipCommit := false
  214. for _, line := range bytes.Split(bs, []byte{'\n'}) {
  215. if len(line) == 0 {
  216. continue
  217. }
  218. switch line[0] {
  219. case ' ':
  220. // Look for Co-authored-by: lines in the commit body.
  221. if skipCommit {
  222. continue
  223. }
  224. line = line[1:]
  225. if bytes.HasPrefix(line, []byte(coAuthoredPrefix)) {
  226. // Co-authored-by: Name Name <email@example.com>
  227. line = line[len(coAuthoredPrefix):]
  228. if name, email, ok := strings.Cut(string(line), "<"); ok {
  229. name = strings.TrimSpace(name)
  230. email = strings.Trim(strings.TrimSpace(email), "<>")
  231. if email == "@" {
  232. // GitHub special for users who hide their email.
  233. continue
  234. }
  235. if names[email] == "" {
  236. names[email] = name
  237. }
  238. }
  239. }
  240. default: // hash email name
  241. fields := strings.SplitN(string(line), " ", 3)
  242. if len(fields) != 3 {
  243. continue
  244. }
  245. hash, email, name := fields[0], fields[1], fields[2]
  246. if excludeCommits.has(hash) {
  247. skipCommit = true
  248. continue
  249. }
  250. skipCommit = false
  251. if names[email] == "" {
  252. names[email] = name
  253. }
  254. }
  255. }
  256. return names
  257. }
  258. type byContributions []author
  259. func (l byContributions) Len() int { return len(l) }
  260. // Sort first by log10(commits), then by name. This means that we first get
  261. // an alphabetic list of people with >= 1000 commits, then a list of people
  262. // with >= 100 commits, and so on.
  263. func (l byContributions) Less(a, b int) bool {
  264. if l[a].log10commits != l[b].log10commits {
  265. return l[a].log10commits > l[b].log10commits
  266. }
  267. return l[a].name < l[b].name
  268. }
  269. func (l byContributions) Swap(a, b int) { l[a], l[b] = l[b], l[a] }
  270. type byName []author
  271. func (l byName) Len() int { return len(l) }
  272. func (l byName) Less(a, b int) bool {
  273. aname := strings.ToLower(l[a].name)
  274. bname := strings.ToLower(l[b].name)
  275. return aname < bname
  276. }
  277. func (l byName) Swap(a, b int) { l[a], l[b] = l[b], l[a] }
  278. // A simple string set type
  279. type stringSet map[string]struct{}
  280. func stringSetFromStrings(ss []string) stringSet {
  281. s := make(stringSet)
  282. for _, e := range ss {
  283. s.add(e)
  284. }
  285. return s
  286. }
  287. func (s stringSet) add(e string) {
  288. s[e] = struct{}{}
  289. }
  290. func (s stringSet) has(e string) bool {
  291. _, ok := s[e]
  292. return ok
  293. }
  294. func (s stringSet) except(other stringSet) stringSet {
  295. diff := make(stringSet)
  296. for e := range s {
  297. if !other.has(e) {
  298. diff.add(e)
  299. }
  300. }
  301. return diff
  302. }