1
0
Fork 0
mirror of https://github.com/Luzifer/git-changerelease.git synced 2024-12-20 11:01:16 +00:00

add the initial version

This commit is contained in:
Knut Ahlers 2016-07-14 12:55:04 +02:00
commit 84b8bcf88e
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
5 changed files with 596 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
git-changerelease

260
assets.go Normal file
View file

@ -0,0 +1,260 @@
// Code generated by go-bindata.
// sources:
// assets/git_changerelease.yaml
// assets/log_template.md
// DO NOT EDIT!
package main
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
func bindataRead(data []byte, name string) ([]byte, error) {
gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, gz)
clErr := gz.Close()
if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err)
}
if clErr != nil {
return nil, err
}
return buf.Bytes(), nil
}
type asset struct {
bytes []byte
info os.FileInfo
}
type bindataFileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
}
func (fi bindataFileInfo) Name() string {
return fi.name
}
func (fi bindataFileInfo) Size() int64 {
return fi.size
}
func (fi bindataFileInfo) Mode() os.FileMode {
return fi.mode
}
func (fi bindataFileInfo) ModTime() time.Time {
return fi.modTime
}
func (fi bindataFileInfo) IsDir() bool {
return false
}
func (fi bindataFileInfo) Sys() interface{} {
return nil
}
var _assetsGit_changereleaseYaml = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xd2\xd5\xd5\xe5\xca\x4d\x2c\x49\xce\x88\x2f\x00\x91\x56\x5c\x0a\x0a\xba\x0a\x4a\x71\xd1\x69\x6e\xb1\x99\x15\x4a\x5c\x50\xb9\xdc\xc4\xac\xfc\x22\xb8\x9c\x53\x52\x6c\x51\x6a\x62\x76\x66\x5e\xba\x12\x17\x20\x00\x00\xff\xff\x2b\x53\xa7\xa0\x40\x00\x00\x00")
func assetsGit_changereleaseYamlBytes() ([]byte, error) {
return bindataRead(
_assetsGit_changereleaseYaml,
"assets/git_changerelease.yaml",
)
}
func assetsGit_changereleaseYaml() (*asset, error) {
bytes, err := assetsGit_changereleaseYamlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "assets/git_changerelease.yaml", size: 64, mode: os.FileMode(436), modTime: time.Unix(1468492027, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
var _assetsLog_templateMd = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\x52\x56\xa8\xae\x56\xd0\xf3\x4b\xad\x28\x09\x4b\x2d\x2a\xce\xcc\xcf\x53\xa8\xad\x55\xd0\x87\x08\xe6\x97\xeb\xb9\xe5\x17\xe5\x26\x96\x28\x28\x19\x19\x18\x98\xe9\x1a\x18\xea\x1a\x18\x29\x01\x15\x70\x01\xa5\x8b\x12\xf3\xd2\x53\x15\x54\x72\x32\xf3\x52\x15\xac\x6c\x15\xf4\x7c\xf2\xd3\x7d\x80\xec\x62\x90\xb4\x82\x82\x16\xc8\x04\xb0\xa4\x5e\x70\x69\x52\x56\x6a\x72\x09\x44\x9b\xae\x42\x6a\x5e\x0a\x88\x09\x32\x42\xcf\x3f\x27\x05\xa8\x0d\xc4\x05\x04\x00\x00\xff\xff\x04\x83\xa4\x75\x87\x00\x00\x00")
func assetsLog_templateMdBytes() ([]byte, error) {
return bindataRead(
_assetsLog_templateMd,
"assets/log_template.md",
)
}
func assetsLog_templateMd() (*asset, error) {
bytes, err := assetsLog_templateMdBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "assets/log_template.md", size: 135, mode: os.FileMode(436), modTime: time.Unix(1468493297, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
// Asset loads and returns the asset for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func Asset(name string) ([]byte, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
}
return a.bytes, nil
}
return nil, fmt.Errorf("Asset %s not found", name)
}
// MustAsset is like Asset but panics when Asset would return an error.
// It simplifies safe initialization of global variables.
func MustAsset(name string) []byte {
a, err := Asset(name)
if err != nil {
panic("asset: Asset(" + name + "): " + err.Error())
}
return a
}
// AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func AssetInfo(name string) (os.FileInfo, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
}
return a.info, nil
}
return nil, fmt.Errorf("AssetInfo %s not found", name)
}
// AssetNames returns the names of the assets.
func AssetNames() []string {
names := make([]string, 0, len(_bindata))
for name := range _bindata {
names = append(names, name)
}
return names
}
// _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() (*asset, error){
"assets/git_changerelease.yaml": assetsGit_changereleaseYaml,
"assets/log_template.md": assetsLog_templateMd,
}
// AssetDir returns the file names below a certain
// directory embedded in the file by go-bindata.
// For example if you run go-bindata on data/... and data contains the
// following hierarchy:
// data/
// foo.txt
// img/
// a.png
// b.png
// then AssetDir("data") would return []string{"foo.txt", "img"}
// AssetDir("data/img") would return []string{"a.png", "b.png"}
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
// AssetDir("") will return []string{"data"}.
func AssetDir(name string) ([]string, error) {
node := _bintree
if len(name) != 0 {
cannonicalName := strings.Replace(name, "\\", "/", -1)
pathList := strings.Split(cannonicalName, "/")
for _, p := range pathList {
node = node.Children[p]
if node == nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
}
}
if node.Func != nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
rv := make([]string, 0, len(node.Children))
for childName := range node.Children {
rv = append(rv, childName)
}
return rv, nil
}
type bintree struct {
Func func() (*asset, error)
Children map[string]*bintree
}
var _bintree = &bintree{nil, map[string]*bintree{
"assets": &bintree{nil, map[string]*bintree{
"git_changerelease.yaml": &bintree{assetsGit_changereleaseYaml, map[string]*bintree{}},
"log_template.md": &bintree{assetsLog_templateMd, map[string]*bintree{}},
}},
}}
// RestoreAsset restores an asset under the given directory
func RestoreAsset(dir, name string) error {
data, err := Asset(name)
if err != nil {
return err
}
info, err := AssetInfo(name)
if err != nil {
return err
}
err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
if err != nil {
return err
}
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil {
return err
}
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
if err != nil {
return err
}
return nil
}
// RestoreAssets restores an asset under the given directory recursively
func RestoreAssets(dir, name string) error {
children, err := AssetDir(name)
// File
if err != nil {
return RestoreAsset(dir, name)
}
// Dir
for _, child := range children {
err = RestoreAssets(dir, filepath.Join(name, child))
if err != nil {
return err
}
}
return nil
}
func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
}

View file

@ -0,0 +1,6 @@
---
match_patch:
- "^[fF]ix"
match_major:
- "^[Bb]reaking"

6
assets/log_template.md Normal file
View file

@ -0,0 +1,6 @@
# {{ .NextVersion }} / {{ .Now.Format "2006-01-02" }}
{{ range $line := .LogLines }}
* {{ $line.Subject }}
{{- end }}
{{ .OldLog }}

323
main.go Normal file
View file

@ -0,0 +1,323 @@
package main
//go:generate go-bindata -o assets.go assets/
import (
"bytes"
"errors"
"fmt"
"html/template"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"gopkg.in/yaml.v2"
"github.com/Luzifer/rconfig"
"github.com/mitchellh/go-homedir"
)
type semVerBump uint
const (
semVerBumpMinor semVerBump = iota
semVerBumpPatch
semVerBumpMajor
)
type commit struct {
ShortHash string
Subject string
AuthorName string
AuthorEmail string
}
func parseCommit(line string) (*commit, error) {
t := strings.Split(line, "\t")
if len(t) != 4 {
return nil, errors.New("Unexpected line format")
}
return &commit{
ShortHash: t[0],
Subject: t[1],
AuthorName: t[2],
AuthorEmail: t[3],
}, nil
}
type configFile struct {
MatchPatch []string `yaml:"match_patch"`
MatchMajor []string `yaml:"match_major"`
}
var (
cfg = struct {
ChangelogFile string `flag:"changelog" default:"History.md" description:"File to write the changelog to"`
ConfigFile string `flag:"config" default:"~/.git_changerelease.yaml" description:"Location of the configuration file"`
MkConfig bool `flag:"create-config" default:"false" description:"Copy an example configuration file to the location of --config"`
NoEdit bool `flag:"no-edit" default:"false" description:"Do not open the $EDITOR to modify the changelog"`
PreRelease string `flag:"pre-release" default:"" description:"Pre-Release information to append to the version (e.g. 'beta' or 'alpha.1')"`
ReleaseMeta string `flag:"release-meta" default:"" description:"Release metadata to append to the version (e.g. 'exp.sha.5114f85' or '20130313144700')"`
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
}{}
config configFile
version = "dev"
)
func init() {
if err := rconfig.Parse(&cfg); err != nil {
log.Fatalf("Unable to parse commandline options: %s", err)
}
if ecfg, err := homedir.Expand(cfg.ConfigFile); err != nil {
log.Fatalf("Unable to parse config path: %s", err)
} else {
cfg.ConfigFile = ecfg
}
if cfg.VersionAndExit {
fmt.Printf("git-changerelease %s\n", version)
os.Exit(0)
}
if cfg.MkConfig {
data, _ := Asset("assets/git_changerelease.yaml")
ioutil.WriteFile(cfg.ConfigFile, data, 0600)
log.Printf("Wrote an example configuration to %s", cfg.ConfigFile)
os.Exit(0)
}
if !cfg.NoEdit && os.Getenv("EDITOR") == "" {
log.Fatalf("You chose to open the changelog in the editor but there is no $EDITOR in your env")
}
if err := loadConfig(); err != nil {
log.Fatalf("Unable to load config file: %s", err)
}
}
func loadConfig() error {
if _, err := os.Stat(cfg.ConfigFile); err != nil {
return errors.New("Config file does not exist, use --create-config to create one")
}
data, err := ioutil.ReadFile(cfg.ConfigFile)
if err != nil {
return err
}
return yaml.Unmarshal(data, &config)
}
func git(stderrEnabled bool, args ...string) (string, error) {
buf := bytes.NewBuffer([]byte{})
cmd := exec.Command("git", args...)
cmd.Stdout = buf
if stderrEnabled {
cmd.Stderr = os.Stderr
}
err := cmd.Run()
return strings.TrimSpace(buf.String()), err
}
func gitErr(args ...string) (string, error) {
return git(true, args...)
}
func gitSilent(args ...string) (string, error) {
return git(false, args...)
}
func doSemVerBump(previousVersion string, bumpType semVerBump) (string, error) {
previousVersion = strings.Split(previousVersion, "-")[0] // Don't care about pre-release / meta-data
previousVersion = strings.TrimLeft(previousVersion, "v")
elements := strings.Split(previousVersion, ".")
major, err := strconv.Atoi(elements[0])
if err != nil {
return "", err
}
minor, err := strconv.Atoi(elements[1])
if err != nil {
return "", err
}
patch, err := strconv.Atoi(elements[2])
if err != nil {
return "", err
}
switch bumpType {
case semVerBumpPatch:
patch += 1
case semVerBumpMinor:
patch = 0
minor += 1
case semVerBumpMajor:
patch = 0
minor = 0
major += 1
}
nextVer := []string{strings.Join([]string{
strconv.Itoa(major),
strconv.Itoa(minor),
strconv.Itoa(patch),
}, ".")}
if cfg.PreRelease != "" {
nextVer = append(nextVer, "-"+cfg.PreRelease)
}
if cfg.ReleaseMeta != "" {
nextVer = append(nextVer, "+"+cfg.ReleaseMeta)
}
return strings.Join(nextVer, ""), nil
}
func selectBumpType(logs []commit) (semVerBump, error) {
bump := semVerBumpMinor
matchers := map[*regexp.Regexp]semVerBump{}
for _, m := range config.MatchPatch {
r, err := regexp.Compile(m)
if err != nil {
return 0, err
}
matchers[r] = semVerBumpPatch
}
for _, m := range config.MatchMajor {
r, err := regexp.Compile(m)
if err != nil {
return 0, err
}
matchers[r] = semVerBumpMajor
}
for _, l := range logs {
for m, t := range matchers {
if m.MatchString(l.Subject) && t > bump {
bump = t
}
}
}
return bump, nil
}
func readChangelog() string {
var changelog string
if _, err := os.Stat(cfg.ChangelogFile); err == nil {
d, err := ioutil.ReadFile(cfg.ChangelogFile)
if err != nil {
log.Fatalf("Unable to read old changelog: %s", err)
}
changelog = string(d)
}
return changelog
}
func main() {
// Get last tag
lastTag, err := gitSilent("describe", "--tags", "--abbrev=0")
// Fetch logs since last tag / since repo start
logArgs := []string{"log", `--format=%h%x09%s%x09%an%x09%ae`, "--abbrev-commit"}
if err == nil {
logArgs = append(logArgs, fmt.Sprintf("%s..HEAD", lastTag))
} else {
lastTag = "0.0.0"
}
rawLogs, err := gitErr(logArgs...)
if err != nil {
log.Fatalf("Unable to read git log entries: %s", err)
}
logs := []commit{}
for _, l := range strings.Split(rawLogs, "\n") {
if l == "" {
continue
}
pl, err := parseCommit(l)
if err != nil {
log.Fatalf("Git used an unexpected log format.")
}
logs = append(logs, *pl)
}
if len(logs) == 0 {
log.Printf("Found no changes since last tag, stopping now.")
return
}
// Tetermine increase type
semVerBumpType, err := selectBumpType(logs)
if err != nil {
log.Fatalf("Could not determine how to increase the version: %s", err)
}
// Generate new version
newVersion, err := doSemVerBump(lastTag, semVerBumpType)
if err != nil {
log.Fatalf("Was unable to increase the version: %s", err)
}
// Render log
rawTpl, _ := Asset("assets/log_template.md")
tpl, err := template.New("log_template").Parse(string(rawTpl))
if err != nil {
log.Fatalf("Unable to parse log template: %s", err)
}
buf := bytes.NewBuffer([]byte{})
tpl.Execute(buf, map[string]interface{}{
"NextVersion": newVersion,
"Now": time.Now(),
"LogLines": logs,
"OldLog": readChangelog(),
})
if err := ioutil.WriteFile(cfg.ChangelogFile, buf.Bytes(), 0644); err != nil {
log.Fatalf("Unable to write new changelog: %s", err)
}
// Spawning editor
if !cfg.NoEdit {
editor := exec.Command(os.Getenv("EDITOR"), cfg.ChangelogFile)
editor.Stdin = os.Stdin
editor.Stdout = os.Stdout
editor.Stderr = os.Stderr
if err := editor.Run(); err != nil {
log.Fatalf("Editor ended with non-zero status, stopping here.")
}
}
// Read back version from changelog file
changelog := strings.Split(readChangelog(), "\n")
if len(changelog) < 1 {
log.Fatalf("Changelog is empty, no way to read back the version.")
}
newVersion = strings.Split(changelog[0], " ")[1]
// Write the tag
if _, err := gitErr("add", cfg.ChangelogFile); err != nil {
log.Fatalf("Unable to add changelog file: %s", err)
}
if _, err := gitErr("commit", "-m", "Prepared release v"+newVersion); err != nil {
log.Fatalf("Unable to commit changelog: %s", err)
}
if _, err := gitErr("tag", "-s", "-m", "v"+newVersion, "v"+newVersion); err != nil {
log.Fatalf("Unable to tag release: %s", err)
}
}