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