check-authors.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. if strings.Contains(strings.ToLower(body(hash)), "skip-check: authors") {
  70. continue
  71. }
  72. authors.add(author)
  73. }
  74. return authors
  75. }
  76. // listedAuthorEmails returns the set of author emails mentioned in AUTHORS
  77. func listedAuthorEmails() stringSet {
  78. bs, err := ioutil.ReadFile("AUTHORS")
  79. if err != nil {
  80. log.Fatal("listedAuthorEmails:", err)
  81. }
  82. emailRe := regexp.MustCompile(`<([^>]+)>`)
  83. matches := emailRe.FindAllStringSubmatch(string(bs), -1)
  84. authors := newStringSet()
  85. for _, match := range matches {
  86. authors.add(match[1])
  87. }
  88. return authors
  89. }
  90. func body(hash string) string {
  91. cmd := exec.Command("git", "show", "--pretty=format:%b", "-s", hash)
  92. bs, err := cmd.Output()
  93. if err != nil {
  94. log.Fatal("body:", err)
  95. }
  96. return string(bs)
  97. }
  98. // A simple string set type
  99. type stringSet map[string]struct{}
  100. func newStringSet() stringSet {
  101. return make(stringSet)
  102. }
  103. func stringSetFromStrings(ss []string) stringSet {
  104. s := newStringSet()
  105. for _, e := range ss {
  106. s.add(e)
  107. }
  108. return s
  109. }
  110. func (s stringSet) add(e string) {
  111. s[e] = struct{}{}
  112. }
  113. func (s stringSet) has(e string) bool {
  114. _, ok := s[e]
  115. return ok
  116. }
  117. func (s stringSet) except(other stringSet) stringSet {
  118. diff := newStringSet()
  119. for e := range s {
  120. if !other.has(e) {
  121. diff.add(e)
  122. }
  123. }
  124. return diff
  125. }