2023-10-15 19:29:29 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-15 22:29:02 +00:00
|
|
|
"os"
|
2023-10-15 19:29:29 +00:00
|
|
|
"sort"
|
|
|
|
|
2023-10-16 19:41:33 +00:00
|
|
|
"github.com/thegeeklab/git-sv/app"
|
|
|
|
"github.com/thegeeklab/git-sv/sv"
|
2023-10-15 19:29:29 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2023-10-15 22:29:02 +00:00
|
|
|
func ChangelogFlags(settings *app.ChangelogSettings) []cli.Flag {
|
2023-10-15 19:29:29 +00:00
|
|
|
return []cli.Flag{
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "size",
|
|
|
|
Value: 10, //nolint:gomnd
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Usage: "get changelog from last 'n' tags",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2023-10-15 22:29:02 +00:00
|
|
|
Name: "all",
|
|
|
|
Usage: "ignore size parameter, get changelog for every tag",
|
|
|
|
Destination: &settings.All,
|
2023-10-15 19:29:29 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2023-10-15 22:29:02 +00:00
|
|
|
Name: "add-next",
|
|
|
|
Usage: "add next version on change log (commits since last tag, only if there is a new release)",
|
|
|
|
Destination: &settings.AddNext,
|
2023-10-15 19:29:29 +00:00
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
2023-10-15 22:29:02 +00:00
|
|
|
Name: "strict",
|
|
|
|
Usage: "only show tags 'SemVer-ish'",
|
|
|
|
Destination: &settings.Strict,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "o",
|
|
|
|
Aliases: []string{"output"},
|
|
|
|
Usage: "output file name. Omit to use standard output.",
|
|
|
|
Destination: &settings.Out,
|
2023-10-15 19:29:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 22:29:02 +00:00
|
|
|
//nolint:gocognit
|
|
|
|
func ChangelogHandler(g app.GitSV) cli.ActionFunc {
|
2023-10-15 19:29:29 +00:00
|
|
|
return func(c *cli.Context) error {
|
|
|
|
tags, err := g.Tags()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(tags, func(i, j int) bool {
|
|
|
|
return tags[i].Date.After(tags[j].Date)
|
|
|
|
})
|
|
|
|
|
|
|
|
var releaseNotes []sv.ReleaseNote
|
|
|
|
|
2023-10-15 22:29:02 +00:00
|
|
|
if g.Settings.ChangelogSettings.AddNext {
|
2023-10-15 19:29:29 +00:00
|
|
|
rnVersion, updated, date, commits, uerr := getNextVersionInfo(g, g.CommitProcessor)
|
|
|
|
if uerr != nil {
|
|
|
|
return uerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated {
|
|
|
|
releaseNotes = append(releaseNotes, g.ReleasenotesProcessor.Create(rnVersion, "", date, commits))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tag := range tags {
|
2023-10-15 22:29:02 +00:00
|
|
|
if !g.Settings.ChangelogSettings.All && i >= g.Settings.ChangelogSettings.Size {
|
2023-10-15 19:29:29 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
previousTag := ""
|
|
|
|
if i+1 < len(tags) {
|
|
|
|
previousTag = tags[i+1].Name
|
|
|
|
}
|
|
|
|
|
2023-10-15 22:29:02 +00:00
|
|
|
if g.Settings.ChangelogSettings.Strict && !sv.IsValidVersion(tag.Name) {
|
2023-10-15 19:29:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
commits, err := g.Log(app.NewLogRange(app.TagRange, previousTag, tag.Name))
|
|
|
|
if err != nil {
|
2023-10-15 22:29:02 +00:00
|
|
|
return fmt.Errorf("error getting git log from tag: %s: %w", tag.Name, err)
|
2023-10-15 19:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
currentVer, _ := sv.ToVersion(tag.Name)
|
|
|
|
releaseNotes = append(releaseNotes, g.ReleasenotesProcessor.Create(currentVer, tag.Name, tag.Date, commits))
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := g.OutputFormatter.FormatChangelog(releaseNotes)
|
|
|
|
if err != nil {
|
2023-10-15 22:29:02 +00:00
|
|
|
return fmt.Errorf("could not format changelog: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.Settings.ChangelogSettings.Out == "" {
|
|
|
|
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := os.Create(g.Settings.ChangelogSettings.Out)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not write changelog: %w", err)
|
2023-10-15 19:29:29 +00:00
|
|
|
}
|
2023-10-15 22:29:02 +00:00
|
|
|
defer w.Close()
|
2023-10-15 19:29:29 +00:00
|
|
|
|
2023-10-15 22:29:02 +00:00
|
|
|
if _, err := w.Write(output); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-15 19:29:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|