2017-10-15 20:21:35 -02:00
|
|
|
// Package changelog provides the release changelog to goreleaser.
|
|
|
|
package changelog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-15 20:40:53 -02:00
|
|
|
"strings"
|
2017-10-15 20: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-15 20:33:39 -02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2017-10-15 20:21:35 -02:00
|
|
|
if ctx.ReleaseNotes != "" {
|
|
|
|
return pipeline.Skip("release notes already provided via --release-notes")
|
|
|
|
}
|
2017-10-15 20:33:39 -02:00
|
|
|
if ctx.Snapshot {
|
|
|
|
return pipeline.Skip("not available for snapshots")
|
2017-10-15 20:21:35 -02:00
|
|
|
}
|
2017-10-15 20:33:39 -02:00
|
|
|
log, err := getChangelog(ctx.Git.CurrentTag)
|
2017-10-15 20:21:35 -02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-15 20:40:53 -02:00
|
|
|
var entries = strings.Split(log, "\n")
|
|
|
|
for _, filter := range ctx.Config.Changelog.Filters.Exclude {
|
|
|
|
entries = filterLog(filter, entries)
|
|
|
|
}
|
|
|
|
ctx.ReleaseNotes = fmt.Sprintf("## Changelog\n\n%v", strings.Join(entries, "\n"))
|
2017-10-15 20:21:35 -02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-15 20:40:53 -02:00
|
|
|
func filterLog(filter string, entries []string) (result []string) {
|
|
|
|
for _, entry := range entries {
|
|
|
|
if !strings.Contains(entry, filter) {
|
|
|
|
result = append(result, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-10-15 20: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
|
|
|
|
}
|