check-authors.go 2.8 KB

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