check-authors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Checks for authors that are not mentioned in AUTHORS
  8. package main
  9. import (
  10. "bytes"
  11. "io/ioutil"
  12. "log"
  13. "os"
  14. "os/exec"
  15. "regexp"
  16. "strings"
  17. )
  18. // list of commits that we don't include in our checks; because they are
  19. // legacy things that don't check code, are committed with incorrect address,
  20. // or for other reasons.
  21. var excludeCommits = stringSetFromStrings([]string{
  22. "63bd0136fb40a91efaa279cb4b4159d82e8e6904",
  23. "4e2feb6fbc791bb8a2daf0ab8efb10775d66343e",
  24. "f2459ef3319b2f060dbcdacd0c35a1788a94b8bd",
  25. "b61f418bf2d1f7d5a9d7088a20a2a448e5e66801",
  26. "a9339d0627fff439879d157c75077f02c9fac61b",
  27. "254c63763a3ad42fd82259f1767db526cff94a14",
  28. "4b76ec40c07078beaa2c5e250ed7d9bd6276a718",
  29. "32a76901a91ff0f663db6f0830e0aedec946e4d0",
  30. "3626003f680bad3e63677982576d3a05421e88e9",
  31. "342036408e65bd25bb6afbcc705e2e2c013bb01f",
  32. "e37cefdbee1c1cd95ad095b5da6d1252723f103b",
  33. })
  34. func init() {
  35. log.SetOutput(os.Stdout)
  36. log.SetFlags(0)
  37. }
  38. func main() {
  39. actual := actualAuthorEmails()
  40. listed := listedAuthorEmails()
  41. missing := actual.except(listed)
  42. if len(missing) > 0 {
  43. log.Println("Missing authors:")
  44. for author := range missing {
  45. log.Println(" ", author)
  46. }
  47. os.Exit(1)
  48. }
  49. }
  50. // actualAuthorEmails returns the set of author emails found in the actual git
  51. // commit log, except those in excluded commits.
  52. func actualAuthorEmails() stringSet {
  53. cmd := exec.Command("git", "log", "--format=%H %ae")
  54. bs, err := cmd.Output()
  55. if err != nil {
  56. log.Fatal("authorEmails:", err)
  57. }
  58. authors := newStringSet()
  59. for _, line := range bytes.Split(bs, []byte{'\n'}) {
  60. fields := strings.Fields(string(line))
  61. if len(fields) != 2 {
  62. continue
  63. }
  64. hash, author := fields[0], fields[1]
  65. if excludeCommits.has(hash) {
  66. continue
  67. }
  68. authors.add(author)
  69. }
  70. return authors
  71. }
  72. // listedAuthorEmails returns the set of author emails mentioned in AUTHORS
  73. func listedAuthorEmails() stringSet {
  74. bs, err := ioutil.ReadFile("AUTHORS")
  75. if err != nil {
  76. log.Fatal("listedAuthorEmails:", err)
  77. }
  78. emailRe := regexp.MustCompile(`<([^>]+)>`)
  79. matches := emailRe.FindAllStringSubmatch(string(bs), -1)
  80. authors := newStringSet()
  81. for _, match := range matches {
  82. authors.add(match[1])
  83. }
  84. return authors
  85. }
  86. // A simple string set type
  87. type stringSet map[string]struct{}
  88. func newStringSet() stringSet {
  89. return make(stringSet)
  90. }
  91. func stringSetFromStrings(ss []string) stringSet {
  92. s := newStringSet()
  93. for _, e := range ss {
  94. s.add(e)
  95. }
  96. return s
  97. }
  98. func (s stringSet) add(e string) {
  99. s[e] = struct{}{}
  100. }
  101. func (s stringSet) has(e string) bool {
  102. _, ok := s[e]
  103. return ok
  104. }
  105. func (s stringSet) except(other stringSet) stringSet {
  106. diff := newStringSet()
  107. for e := range s {
  108. if !other.has(e) {
  109. diff.add(e)
  110. }
  111. }
  112. return diff
  113. }