2017-10-16 00:21:35 +02:00
|
|
|
// Package changelog provides the release changelog to goreleaser.
|
|
|
|
package changelog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-18 03:45:19 +02:00
|
|
|
"regexp"
|
2017-10-16 00:40:53 +02:00
|
|
|
"strings"
|
2017-10-16 00:21:35 +02:00
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
|
|
|
"github.com/goreleaser/goreleaser/pipeline"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Pipe for checksums
|
|
|
|
type Pipe struct{}
|
|
|
|
|
|
|
|
// Description of the pipe
|
|
|
|
func (Pipe) Description() string {
|
|
|
|
return "Generating changelog"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the pipe
|
2017-10-16 00:33:39 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2017-10-16 00:21:35 +02:00
|
|
|
if ctx.ReleaseNotes != "" {
|
|
|
|
return pipeline.Skip("release notes already provided via --release-notes")
|
|
|
|
}
|
2017-10-16 00:33:39 +02:00
|
|
|
if ctx.Snapshot {
|
|
|
|
return pipeline.Skip("not available for snapshots")
|
2017-10-16 00:21:35 +02:00
|
|
|
}
|
2017-10-16 00:33:39 +02:00
|
|
|
log, err := getChangelog(ctx.Git.CurrentTag)
|
2017-10-16 00:21:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-16 00:40:53 +02:00
|
|
|
var entries = strings.Split(log, "\n")
|
|
|
|
for _, filter := range ctx.Config.Changelog.Filters.Exclude {
|
2017-10-18 03:45:19 +02:00
|
|
|
r, err := regexp.Compile(filter)
|
|
|
|
if err != nil {
|
2017-10-18 04:37:03 +02:00
|
|
|
return err
|
2017-10-18 03:45:19 +02:00
|
|
|
}
|
|
|
|
entries = remove(r, entries)
|
2017-10-16 00:40:53 +02:00
|
|
|
}
|
|
|
|
ctx.ReleaseNotes = fmt.Sprintf("## Changelog\n\n%v", strings.Join(entries, "\n"))
|
2017-10-16 00:21:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-18 03:45:19 +02:00
|
|
|
func remove(filter *regexp.Regexp, entries []string) (result []string) {
|
2017-10-16 00:40:53 +02:00
|
|
|
for _, entry := range entries {
|
2017-10-18 03:45:19 +02:00
|
|
|
if !match(filter, entry) {
|
2017-10-16 00:40:53 +02:00
|
|
|
result = append(result, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-10-18 03:45:19 +02:00
|
|
|
func match(filter *regexp.Regexp, line string) bool {
|
|
|
|
s := strings.Join(strings.SplitAfter(line, " ")[1:], "")
|
|
|
|
return filter.MatchString(s)
|
|
|
|
}
|
|
|
|
|
2017-10-16 00:21:35 +02:00
|
|
|
func getChangelog(tag string) (string, error) {
|
|
|
|
prev, err := previous(tag)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !prev.Tag {
|
|
|
|
return gitLog(prev.SHA, tag)
|
|
|
|
}
|
|
|
|
return gitLog(fmt.Sprintf("%v..%v", prev.SHA, tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func gitLog(refs ...string) (string, error) {
|
|
|
|
var args = []string{"log", "--pretty=oneline", "--abbrev-commit"}
|
|
|
|
args = append(args, refs...)
|
|
|
|
return git.Run(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func previous(tag string) (result ref, err error) {
|
|
|
|
result.Tag = true
|
|
|
|
result.SHA, err = git.Clean(git.Run("describe", "--tags", "--abbrev=0", tag+"^"))
|
|
|
|
if err != nil {
|
|
|
|
result.Tag = false
|
|
|
|
result.SHA, err = git.Clean(git.Run("rev-list", "--max-parents=0", "HEAD"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type ref struct {
|
|
|
|
Tag bool
|
|
|
|
SHA string
|
|
|
|
}
|