check-authors.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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("cmd/", "lib/", "gui/", "test/", "script/")
  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(paths ...string) stringSet {
  53. args := append([]string{"log", "--format=%H %ae"}, paths...)
  54. cmd := exec.Command("git", args...)
  55. bs, err := cmd.Output()
  56. if err != nil {
  57. log.Fatal("authorEmails:", err)
  58. }
  59. authors := newStringSet()
  60. for _, line := range bytes.Split(bs, []byte{'\n'}) {
  61. fields := strings.Fields(string(line))
  62. if len(fields) != 2 {
  63. continue
  64. }
  65. hash, author := fields[0], fields[1]
  66. if excludeCommits.has(hash) {
  67. continue
  68. }
  69. authors.add(author)
  70. }
  71. return authors
  72. }
  73. // listedAuthorEmails returns the set of author emails mentioned in AUTHORS
  74. func listedAuthorEmails() stringSet {
  75. bs, err := ioutil.ReadFile("AUTHORS")
  76. if err != nil {
  77. log.Fatal("listedAuthorEmails:", err)
  78. }
  79. emailRe := regexp.MustCompile(`<([^>]+)>`)
  80. matches := emailRe.FindAllStringSubmatch(string(bs), -1)
  81. authors := newStringSet()
  82. for _, match := range matches {
  83. authors.add(match[1])
  84. }
  85. return authors
  86. }
  87. // A simple string set type
  88. type stringSet map[string]struct{}
  89. func newStringSet() stringSet {
  90. return make(stringSet)
  91. }
  92. func stringSetFromStrings(ss []string) stringSet {
  93. s := newStringSet()
  94. for _, e := range ss {
  95. s.add(e)
  96. }
  97. return s
  98. }
  99. func (s stringSet) add(e string) {
  100. s[e] = struct{}{}
  101. }
  102. func (s stringSet) has(e string) bool {
  103. _, ok := s[e]
  104. return ok
  105. }
  106. func (s stringSet) except(other stringSet) stringSet {
  107. diff := newStringSet()
  108. for e := range s {
  109. if !other.has(e) {
  110. diff.add(e)
  111. }
  112. }
  113. return diff
  114. }