|
| 1 | +// Copyright 2017 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +// implied. See the License for the specific language governing |
| 13 | +// permissions and limitations under the License. |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "fmt" |
| 19 | + "go/build" |
| 20 | + "io/ioutil" |
| 21 | + "log" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + "unicode" |
| 27 | + "unicode/utf8" |
| 28 | + |
| 29 | + "github.com/ghemawat/stream" |
| 30 | + "github.com/pkg/errors" |
| 31 | +) |
| 32 | + |
| 33 | +// validScopes is a set of all valid scopes for a commit message of the form |
| 34 | +// scope[,scope]: message. It is populated by init with every directory in crdb. |
| 35 | +var validScopes = map[string]struct{}{ |
| 36 | + // "*" is special and indicates "all scopes." |
| 37 | + "*": {}, |
| 38 | + |
| 39 | + // Following are pseudo-scopes that don't map neatly to a directory. |
| 40 | + "mvcc": {}, |
| 41 | +} |
| 42 | + |
| 43 | +// impliedScopePrefixes lists scope prefixes that may be omitted from the |
| 44 | +// commit message. For example, including "pkg/sql/" as an implied scope prefix |
| 45 | +// allows "pkg/sql/distsqlrun" to be shortened to "distsqlrun". |
| 46 | +var impliedScopePrefixes = []string{ |
| 47 | + "build/", |
| 48 | + "c-deps/", |
| 49 | + "docs/", |
| 50 | + "pkg/", |
| 51 | + "pkg/ccl/", |
| 52 | + "pkg/cmd/", |
| 53 | + "pkg/internal/", |
| 54 | + "pkg/sql/", |
| 55 | + "pkg/storage/", |
| 56 | + "pkg/testutils/", |
| 57 | + "pkg/util/", |
| 58 | +} |
| 59 | + |
| 60 | +// allowFileScope lists directories in which files and their basenames can be |
| 61 | +// used directly as scopes. For example, if the "scripts" directory allows file |
| 62 | +// scope and contains a file named "azworker.sh", both "azworker" and |
| 63 | +// "azworker.sh" become valid scopes. |
| 64 | +var allowFileScope = []string{ |
| 65 | + ".", ".github", "scripts", |
| 66 | +} |
| 67 | + |
| 68 | +func fileScopeAllowed(dir string) bool { |
| 69 | + for _, d := range allowFileScope { |
| 70 | + if d == dir { |
| 71 | + return true |
| 72 | + } |
| 73 | + } |
| 74 | + return false |
| 75 | +} |
| 76 | + |
| 77 | +func exit(args ...interface{}) { |
| 78 | + fmt.Fprint(os.Stderr, args...) |
| 79 | + os.Exit(1) |
| 80 | +} |
| 81 | + |
| 82 | +func exitf(format string, args ...interface{}) { |
| 83 | + fmt.Fprintf(os.Stderr, format, args...) |
| 84 | + os.Exit(1) |
| 85 | +} |
| 86 | + |
| 87 | +func init() { |
| 88 | + rootPkg, err := build.Import("github.com/cockroachdb/cockroach", "", build.FindOnly) |
| 89 | + if err != nil { |
| 90 | + exit(err) |
| 91 | + } |
| 92 | + |
| 93 | + lsFiles := exec.Command("git", "ls-files") |
| 94 | + lsFiles.Dir = rootPkg.Dir |
| 95 | + stdout, err := lsFiles.StdoutPipe() |
| 96 | + if err != nil { |
| 97 | + exit(err) |
| 98 | + } |
| 99 | + stderr := new(bytes.Buffer) |
| 100 | + lsFiles.Stderr = stderr |
| 101 | + filter := stream.ReadLines(stdout) |
| 102 | + |
| 103 | + if err := lsFiles.Start(); err != nil { |
| 104 | + log.Fatal(err) |
| 105 | + } |
| 106 | + |
| 107 | + if err := stream.ForEach(filter, func(s string) { |
| 108 | + dir := filepath.Dir(s) |
| 109 | + if dir != "." { |
| 110 | + for _, prefix := range impliedScopePrefixes { |
| 111 | + validScopes[strings.TrimPrefix(dir, prefix)] = struct{}{} |
| 112 | + } |
| 113 | + } |
| 114 | + if fileScopeAllowed(dir) { |
| 115 | + base := filepath.Base(s) |
| 116 | + validScopes[base] = struct{}{} |
| 117 | + if baseNoExt := strings.TrimSuffix(base, filepath.Ext(base)); baseNoExt != "" { |
| 118 | + validScopes[baseNoExt] = struct{}{} |
| 119 | + } |
| 120 | + } |
| 121 | + }); err != nil { |
| 122 | + exit(err) |
| 123 | + } |
| 124 | + |
| 125 | + if err := lsFiles.Wait(); err != nil { |
| 126 | + exitf("%s failed: %s\n%s", strings.Join(lsFiles.Args, " "), err, stderr.String()) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// Everything after scissorsLine in a Git commit message is ignored. This |
| 131 | +// behavior is built into Git itself [0]; when using `git commit --verbose`, |
| 132 | +// for example, the commit message template includes a scissorsLine. |
| 133 | +// |
| 134 | +// [0]: https://git-scm.com/docs/git-commit#git-commit-scissors |
| 135 | +const scissorsLine = "# ------------------------ >8 ------------------------" |
| 136 | + |
| 137 | +func checkCommitMessage(message string) error { |
| 138 | + if strings.HasPrefix(message, `Revert "`) { |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + lines := strings.Split(message, "\n") |
| 143 | + for i, line := range lines { |
| 144 | + if line == scissorsLine { |
| 145 | + break |
| 146 | + } else if max := 72; len(line) > max && line[0] != '#' { |
| 147 | + return errors.Errorf("line %d exceeds maximum line length of %d characters", i+1, max) |
| 148 | + } |
| 149 | + } |
| 150 | + if len(lines) > 1 && lines[1] != "" { |
| 151 | + return errors.New("missing blank line after summary") |
| 152 | + } |
| 153 | + summary := lines[0] |
| 154 | + |
| 155 | + if summary == "" { |
| 156 | + return errors.New("missing summary") |
| 157 | + } |
| 158 | + if summary[len(summary)-1] == '.' { |
| 159 | + return errors.New("summary should not end in period") |
| 160 | + } |
| 161 | + |
| 162 | + splits := strings.SplitN(summary, ":", 2) |
| 163 | + if len(splits) != 2 { |
| 164 | + return errors.New("summary does not match format `scope[,scope]: message`") |
| 165 | + } |
| 166 | + scopes, description := strings.Split(splits[0], ","), strings.TrimSpace(splits[1]) |
| 167 | + |
| 168 | + for _, scope := range scopes { |
| 169 | + if trimmedScope := strings.TrimSpace(scope); trimmedScope != scope { |
| 170 | + return errors.Errorf("scope %q has extraneous whitespace", trimmedScope) |
| 171 | + } |
| 172 | + if _, ok := validScopes[scope]; !ok { |
| 173 | + return errors.Errorf("unknown scope %q", scope) |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if description == "" { |
| 178 | + return errors.New("summary missing text after colon") |
| 179 | + } |
| 180 | + if ch, _ := utf8.DecodeRuneInString(description); unicode.IsUpper(ch) { |
| 181 | + return errors.New("text after colon in summary should not begin with capital letter") |
| 182 | + } |
| 183 | + |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +const noLintEnvVar = "COCKROACH_NO_LINT_COMMITS" |
| 188 | + |
| 189 | +func main() { |
| 190 | + if len(os.Args) != 2 { |
| 191 | + exitf("usage: %s COMMIT-MSG-FILE\n", os.Args[0]) |
| 192 | + } |
| 193 | + |
| 194 | + if _, noLint := os.LookupEnv(noLintEnvVar); noLint { |
| 195 | + os.Exit(0) |
| 196 | + } |
| 197 | + |
| 198 | + message, err := ioutil.ReadFile(os.Args[1]) |
| 199 | + if err != nil { |
| 200 | + exitf("failed to read commit message file: %s\n", err) |
| 201 | + } |
| 202 | + |
| 203 | + if err := checkCommitMessage(string(message)); err != nil { |
| 204 | + fmt.Printf(`commit message warning: %s |
| 205 | +
|
| 206 | +A good commit message has the following form: |
| 207 | +
|
| 208 | + scope[,scope]: short summary in imperative tense |
| 209 | +
|
| 210 | + A detailed description follows the summary here, after a blank line, and |
| 211 | + explains the rationale for the change. It should be wrapped at 72 |
| 212 | + characters, with blank lines between paragraphs. |
| 213 | + |
| 214 | + The scope at the beginning of the summary indicates the package (e.g., |
| 215 | + "storage") primarily impacted by the change. If multiple packages are |
| 216 | + affected, separate them with commas and no whitespace. Commits which affect |
| 217 | + no package in particular can use the special scope "*". Note that certain |
| 218 | + obvious prefixes ("pkg/", "pkg/sql/", "scripts/", et al.) can be omitted |
| 219 | + from the scope. |
| 220 | +
|
| 221 | + See CONTRIBUTING.md for details. |
| 222 | +
|
| 223 | +If you believe this warning is in error, please update this linter. If you wish |
| 224 | +to suppress this linter, set %s=1 in your environment.`, err, noLintEnvVar) |
| 225 | + fmt.Println() |
| 226 | + } |
| 227 | +} |
0 commit comments