check-authors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. })
  33. func init() {
  34. log.SetOutput(os.Stdout)
  35. log.SetFlags(0)
  36. }
  37. func main() {
  38. actual := actualAuthorEmails()
  39. listed := listedAuthorEmails()
  40. missing := actual.except(listed)
  41. if len(missing) > 0 {
  42. log.Println("Missing authors:")
  43. for author := range missing {
  44. log.Println(" ", author)
  45. }
  46. os.Exit(1)
  47. }
  48. }
  49. // actualAuthorEmails returns the set of author emails found in the actual git
  50. // commit log, except those in excluded commits.
  51. func actualAuthorEmails() stringSet {
  52. cmd := exec.Command("git", "log", "--format=%H %ae")
  53. bs, err := cmd.Output()
  54. if err != nil {
  55. log.Fatal("authorEmails:", err)
  56. }
  57. authors := newStringSet()
  58. for _, line := range bytes.Split(bs, []byte{'\n'}) {
  59. fields := strings.Fields(string(line))
  60. if len(fields) != 2 {
  61. continue
  62. }
  63. hash, author := fields[0], fields[1]
  64. if excludeCommits.has(hash) {
  65. continue
  66. }
  67. authors.add(author)
  68. }
  69. return authors
  70. }
  71. // listedAuthorEmails returns the set of author emails mentioned in AUTHORS
  72. func listedAuthorEmails() stringSet {
  73. bs, err := ioutil.ReadFile("AUTHORS")
  74. if err != nil {
  75. log.Fatal("listedAuthorEmails:", err)
  76. }
  77. emailRe := regexp.MustCompile(`<([^>]+)>`)
  78. matches := emailRe.FindAllStringSubmatch(string(bs), -1)
  79. authors := newStringSet()
  80. for _, match := range matches {
  81. authors.add(match[1])
  82. }
  83. return authors
  84. }
  85. // A simple string set type
  86. type stringSet map[string]struct{}
  87. func newStringSet() stringSet {
  88. return make(stringSet)
  89. }
  90. func stringSetFromStrings(ss []string) stringSet {
  91. s := newStringSet()
  92. for _, e := range ss {
  93. s.add(e)
  94. }
  95. return s
  96. }
  97. func (s stringSet) add(e string) {
  98. s[e] = struct{}{}
  99. }
  100. func (s stringSet) has(e string) bool {
  101. _, ok := s[e]
  102. return ok
  103. }
  104. func (s stringSet) except(other stringSet) stringSet {
  105. diff := newStringSet()
  106. for e := range s {
  107. if !other.has(e) {
  108. diff.add(e)
  109. }
  110. }
  111. return diff
  112. }