Browse Source

Rewrite changelog.sh in Go

Jakob Borg 10 years ago
parent
commit
6186a746e0
2 changed files with 64 additions and 20 deletions
  1. 64 0
      changelog.go
  2. 0 20
      changelog.sh

+ 64 - 0
changelog.go

@@ -0,0 +1,64 @@
+package main
+
+import (
+	"bytes"
+	"flag"
+	"fmt"
+	"log"
+	"os/exec"
+	"regexp"
+)
+
+var (
+	subjectIssues = regexp.MustCompile(`^([^(]+)\s+\((?:fixes|ref) ([^)]+)\)$`)
+	issueNumbers  = regexp.MustCompile(`(#\d+)`)
+)
+
+func main() {
+	flag.Parse()
+
+	// Display changelog since the version given on the command line, or
+	// figure out the last release if there were no arguments.
+	var prevRel string
+	if flag.NArg() > 0 {
+		prevRel = flag.Arg(0)
+	} else {
+		bs, err := runError("git", "describe", "--abbrev=0", "HEAD^")
+		if err != nil {
+			log.Fatal(err)
+		}
+		prevRel = string(bs)
+	}
+
+	// Get the git log with subject and author nickname
+	bs, err := runError("git", "log", "--reverse", "--pretty=format:%s|%aN", prevRel+"..")
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	// Split into lines
+	for _, line := range bytes.Split(bs, []byte{'\n'}) {
+		// Split into subject and author
+		fields := bytes.Split(line, []byte{'|'})
+		subj := fields[0]
+		author := fields[1]
+
+		// Check if subject contains a "(fixes ...)" or "(ref ...)""
+		if m := subjectIssues.FindSubmatch(subj); len(m) > 0 {
+			// Find all issue numbers
+			issues := issueNumbers.FindAll(m[2], -1)
+
+			// Format a changelog entry
+			fmt.Printf("* %s (%s, @%s)\n", m[1], bytes.Join(issues, []byte(", ")), author)
+		}
+	}
+}
+
+func runError(cmd string, args ...string) ([]byte, error) {
+	ecmd := exec.Command(cmd, args...)
+	bs, err := ecmd.CombinedOutput()
+	if err != nil {
+		return nil, err
+	}
+	return bytes.TrimSpace(bs), nil
+}

+ 0 - 20
changelog.sh

@@ -1,20 +0,0 @@
-#!/bin/bash
-
-since="$1"
-if [[ -z $since ]] ; then
-	since="$(git describe --abbrev=0 HEAD^).."
-fi
-
-case $(uname) in
-	Darwin)
-		grep="egrep"
-		;;
-	*)
-		grep="grep -P"
-		;;
-esac
-
-git log --reverse --pretty=format:'* %s, @%aN)' "$since" | $grep 'fixes #\d|ref #\d' | sed 's/)[,. ]*,/,/' | sed 's/fixes #/#/g' | sed 's/ref #/#/g'
-
-git diff "$since" -- AUTHORS
-