check-authors.go 3.4 KB

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