commit-msg.go 1011 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
  6. //go:build ignore
  7. // +build ignore
  8. package main
  9. import (
  10. "bytes"
  11. "fmt"
  12. "io/ioutil"
  13. "os"
  14. "path/filepath"
  15. "regexp"
  16. )
  17. const (
  18. exitSuccess = 0
  19. exitError = 1
  20. )
  21. var subject = regexp.MustCompile(`^[\w/,\. ]+: \w`)
  22. func main() {
  23. if len(os.Args) != 2 {
  24. fmt.Printf("Usage: %s <file>\n", filepath.Base(os.Args[0]))
  25. os.Exit(exitError)
  26. }
  27. bs, err := ioutil.ReadFile(os.Args[1])
  28. if err != nil {
  29. fmt.Println("Reading input:", err)
  30. os.Exit(exitError)
  31. }
  32. lines := bytes.Split(bs, []byte{'\n'})
  33. if !subject.Match(lines[0]) {
  34. fmt.Printf(`Commit message subject:
  35. %s
  36. doesn't look like "tag: One sentence description". Specifically, it doesn't
  37. match this pattern:
  38. %s
  39. `, lines[0], subject)
  40. os.Exit(exitError)
  41. }
  42. os.Exit(exitSuccess)
  43. }