changelog.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2014 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. package main
  8. import (
  9. "bytes"
  10. "flag"
  11. "fmt"
  12. "log"
  13. "os/exec"
  14. "regexp"
  15. )
  16. var (
  17. subjectIssues = regexp.MustCompile(`^([^(]+)\s+\((?:fixes|ref) ([^)]+)\)(?:[^\w])?$`)
  18. issueNumbers = regexp.MustCompile(`(#\d+)`)
  19. )
  20. func main() {
  21. flag.Parse()
  22. // Display changelog since the version given on the command line, or
  23. // figure out the last release if there were no arguments.
  24. var prevRel string
  25. if flag.NArg() > 0 {
  26. prevRel = flag.Arg(0)
  27. } else {
  28. bs, err := runError("git", "describe", "--abbrev=0", "HEAD^")
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. prevRel = string(bs)
  33. }
  34. // Get the git log with subject and author nickname
  35. bs, err := runError("git", "log", "--reverse", "--pretty=format:%s|%aN", prevRel+"..")
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. // Split into lines
  40. for _, line := range bytes.Split(bs, []byte{'\n'}) {
  41. // Split into subject and author
  42. fields := bytes.Split(line, []byte{'|'})
  43. subj := fields[0]
  44. author := fields[1]
  45. // Check if subject contains a "(fixes ...)" or "(ref ...)""
  46. if m := subjectIssues.FindSubmatch(subj); len(m) > 0 {
  47. // Find all issue numbers
  48. issues := issueNumbers.FindAll(m[2], -1)
  49. // Format a changelog entry
  50. fmt.Printf("* %s (%s, @%s)\n", m[1], bytes.Join(issues, []byte(", ")), author)
  51. }
  52. }
  53. }
  54. func runError(cmd string, args ...string) ([]byte, error) {
  55. ecmd := exec.Command(cmd, args...)
  56. bs, err := ecmd.CombinedOutput()
  57. if err != nil {
  58. return nil, err
  59. }
  60. return bytes.TrimSpace(bs), nil
  61. }