From be1b5924706570b321cba909ada4ffd22689a393 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Tue, 17 Oct 2023 09:52:28 +0200 Subject: [PATCH] feat: add special value next for tag filters --- app/commands/changelog.go | 21 +++++++------- app/commands/commitlog.go | 54 ++++++++++++++++++++---------------- app/commands/commitnotes.go | 12 ++++---- app/commands/releasenotes.go | 16 +++++++---- app/config.go | 8 ++++++ cmd/git-sv/main.go | 10 +++---- 6 files changed, 69 insertions(+), 52 deletions(-) diff --git a/app/commands/changelog.go b/app/commands/changelog.go index b21cbb0..4233e15 100644 --- a/app/commands/changelog.go +++ b/app/commands/changelog.go @@ -13,10 +13,11 @@ import ( func ChangelogFlags(settings *app.ChangelogSettings) []cli.Flag { return []cli.Flag{ &cli.IntFlag{ - Name: "size", - Value: 10, //nolint:gomnd - Aliases: []string{"n"}, - Usage: "get changelog from last 'n' tags", + Name: "size", + Value: 10, //nolint:gomnd + Aliases: []string{"n"}, + Destination: &settings.Size, + Usage: "get changelog from last 'n' tags", }, &cli.BoolFlag{ Name: "all", @@ -43,7 +44,7 @@ func ChangelogFlags(settings *app.ChangelogSettings) []cli.Flag { } //nolint:gocognit -func ChangelogHandler(g app.GitSV) cli.ActionFunc { +func ChangelogHandler(g app.GitSV, settings *app.ChangelogSettings) cli.ActionFunc { return func(c *cli.Context) error { tags, err := g.Tags() if err != nil { @@ -56,7 +57,7 @@ func ChangelogHandler(g app.GitSV) cli.ActionFunc { var releaseNotes []sv.ReleaseNote - if g.Settings.ChangelogSettings.AddNext { + if settings.AddNext { rnVersion, updated, date, commits, uerr := getNextVersionInfo(g, g.CommitProcessor) if uerr != nil { return uerr @@ -68,7 +69,7 @@ func ChangelogHandler(g app.GitSV) cli.ActionFunc { } for i, tag := range tags { - if !g.Settings.ChangelogSettings.All && i >= g.Settings.ChangelogSettings.Size { + if !settings.All && i >= settings.Size { break } @@ -77,7 +78,7 @@ func ChangelogHandler(g app.GitSV) cli.ActionFunc { previousTag = tags[i+1].Name } - if g.Settings.ChangelogSettings.Strict && !sv.IsValidVersion(tag.Name) { + if settings.Strict && !sv.IsValidVersion(tag.Name) { continue } @@ -95,13 +96,13 @@ func ChangelogHandler(g app.GitSV) cli.ActionFunc { return fmt.Errorf("could not format changelog: %w", err) } - if g.Settings.ChangelogSettings.Out == "" { + if settings.Out == "" { os.Stdout.WriteString(fmt.Sprintf("%s\n", output)) return nil } - w, err := os.Create(g.Settings.ChangelogSettings.Out) + w, err := os.Create(settings.Out) if err != nil { return fmt.Errorf("could not write changelog: %w", err) } diff --git a/app/commands/commitlog.go b/app/commands/commitlog.go index aa2c190..a48135f 100644 --- a/app/commands/commitlog.go +++ b/app/commands/commitlog.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" "github.com/thegeeklab/git-sv/app" "github.com/thegeeklab/git-sv/sv" @@ -16,56 +17,61 @@ var ( errUnknownTag = errors.New("unknown tag") ) -func CommitLogFlags() []cli.Flag { +func CommitLogFlags(settings *app.CommitLogSettings) []cli.Flag { return []cli.Flag{ &cli.StringFlag{ - Name: "t", - Aliases: []string{"tag"}, - Usage: "get commit log from a specific tag", + Name: "t", + Aliases: []string{"tag"}, + Usage: "get commit log from a specific tag", + Destination: &settings.Tag, + Value: "next", }, &cli.StringFlag{ - Name: "r", - Aliases: []string{"range"}, - Usage: "type of range of commits, use: tag, date or hash", - Value: string(app.TagRange), + Name: "r", + Aliases: []string{"range"}, + Usage: "type of range of commits, use: tag, date or hash", + Destination: &settings.Range, + Value: string(app.TagRange), }, &cli.StringFlag{ - Name: "s", - Aliases: []string{"start"}, - Usage: "start range of git log revision range, if date, the value is used on since flag instead", + Name: "s", + Aliases: []string{"start"}, + Usage: "start range of git log revision range, if date, the value is used on since flag instead", + Destination: &settings.Start, }, &cli.StringFlag{ - Name: "e", - Aliases: []string{"end"}, - Usage: "end range of git log revision range, if date, the value is used on until flag instead", + Name: "e", + Aliases: []string{"end"}, + Usage: "end range of git log revision range, if date, the value is used on until flag instead", + Destination: &settings.End, }, } } -func CommitLogHandler(g app.GitSV) cli.ActionFunc { +func CommitLogHandler(g app.GitSV, settings *app.CommitLogSettings) cli.ActionFunc { return func(c *cli.Context) error { var ( commits []sv.CommitLog err error ) - tagFlag := c.String("t") - rangeFlag := c.String("r") - startFlag := c.String("s") - endFlag := c.String("e") + tagDefault := "next" + tagFlag := strings.TrimSpace(strings.ToLower(settings.Tag)) - if tagFlag != "" && (rangeFlag != string(app.TagRange) || startFlag != "" || endFlag != "") { + if tagFlag != tagDefault && + (settings.Range != string(app.TagRange) || settings.Start != "" || settings.End != "") { return errCanNotCreateTagFlag } - if tagFlag != "" { - commits, err = getTagCommits(g, tagFlag) - } else { - r, rerr := logRange(g, rangeFlag, startFlag, endFlag) + if tagFlag == tagDefault { + r, rerr := logRange(g, settings.Range, settings.Start, settings.End) if rerr != nil { return rerr } + commits, err = g.Log(r) + } else { + commits, err = getTagCommits(g, tagFlag) } if err != nil { diff --git a/app/commands/commitnotes.go b/app/commands/commitnotes.go index a9edb6e..91477f9 100644 --- a/app/commands/commitnotes.go +++ b/app/commands/commitnotes.go @@ -38,20 +38,18 @@ func CommitNotesFlags(settings *app.CommitNotesSettings) []cli.Flag { } } -func CommitNotesHandler(g app.GitSV) cli.ActionFunc { +func CommitNotesHandler(g app.GitSV, settings *app.CommitNotesSettings) cli.ActionFunc { return func(c *cli.Context) error { var date time.Time - rangeFlag := g.Settings.CommitNotesSettings.Range - - lr, err := logRange(g, rangeFlag, g.Settings.CommitNotesSettings.Start, g.Settings.CommitNotesSettings.End) + lr, err := logRange(g, settings.Range, settings.Start, settings.End) if err != nil { return err } commits, err := g.Log(lr) if err != nil { - return fmt.Errorf("error getting git log from range: %s: %w", rangeFlag, err) + return fmt.Errorf("error getting git log from range: %s: %w", settings.Range, err) } if len(commits) > 0 { @@ -63,13 +61,13 @@ func CommitNotesHandler(g app.GitSV) cli.ActionFunc { return fmt.Errorf("could not format commit notes: %w", err) } - if g.Settings.CommitNotesSettings.Out == "" { + if settings.End == "" { os.Stdout.WriteString(fmt.Sprintf("%s\n", output)) return nil } - w, err := os.Create(g.Settings.CommitNotesSettings.Out) + w, err := os.Create(settings.End) if err != nil { return fmt.Errorf("could not write commit notes: %w", err) } diff --git a/app/commands/releasenotes.go b/app/commands/releasenotes.go index 3c58930..0a93336 100644 --- a/app/commands/releasenotes.go +++ b/app/commands/releasenotes.go @@ -3,6 +3,7 @@ package commands import ( "fmt" "os" + "strings" "time" "github.com/Masterminds/semver/v3" @@ -18,6 +19,7 @@ func ReleaseNotesFlags(settings *app.ReleaseNotesSettings) []cli.Flag { Aliases: []string{"tag"}, Usage: "get release note from tag", Destination: &settings.Tag, + Value: "next", }, &cli.StringFlag{ Name: "o", @@ -28,7 +30,7 @@ func ReleaseNotesFlags(settings *app.ReleaseNotesSettings) []cli.Flag { } } -func ReleaseNotesHandler(g app.GitSV) cli.ActionFunc { +func ReleaseNotesHandler(g app.GitSV, settings *app.ReleaseNotesSettings) cli.ActionFunc { return func(c *cli.Context) error { var ( commits []sv.CommitLog @@ -38,11 +40,13 @@ func ReleaseNotesHandler(g app.GitSV) cli.ActionFunc { err error ) - if tag = g.Settings.ReleaseNotesSettings.Tag; tag != "" { - rnVersion, date, commits, err = getTagVersionInfo(g, tag) - } else { + tagFlag := strings.TrimSpace(strings.ToLower(settings.Tag)) + + if tagFlag == "next" { // TODO: should generate release notes if version was not updated? rnVersion, _, date, commits, err = getNextVersionInfo(g, g.CommitProcessor) + } else { + rnVersion, date, commits, err = getTagVersionInfo(g, tag) } if err != nil { @@ -56,13 +60,13 @@ func ReleaseNotesHandler(g app.GitSV) cli.ActionFunc { return fmt.Errorf("could not format release notes: %w", err) } - if g.Settings.ReleaseNotesSettings.Out == "" { + if settings.Out == "" { os.Stdout.WriteString(fmt.Sprintf("%s\n", output)) return nil } - w, err := os.Create(g.Settings.ReleaseNotesSettings.Out) + w, err := os.Create(settings.Out) if err != nil { return fmt.Errorf("could not write release notes: %w", err) } diff --git a/app/config.go b/app/config.go index 6ec53b7..8327a78 100644 --- a/app/config.go +++ b/app/config.go @@ -18,6 +18,7 @@ type Settings struct { ChangelogSettings ChangelogSettings ReleaseNotesSettings ReleaseNotesSettings CommitNotesSettings CommitNotesSettings + CommitLogSettings CommitLogSettings } type ChangelogSettings struct { @@ -40,6 +41,13 @@ type CommitNotesSettings struct { Out string } +type CommitLogSettings struct { + Tag string + Range string + Start string + End string +} + // Config cli yaml config. type Config struct { Version string `yaml:"version"` diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index c400aaf..5c4a35f 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -86,8 +86,8 @@ func main() { Description: `The range filter is used based on git log filters, check https://git-scm.com/docs/git-log for more info. When flag range is "tag" and start is empty, last tag created will be used instead. When flag range is "date", if "end" is YYYY-MM-DD the range will be inclusive.`, - Action: commands.CommitLogHandler(gsv), - Flags: commands.CommitLogFlags(), + Action: commands.CommitLogHandler(gsv, &gsv.Settings.CommitLogSettings), + Flags: commands.CommitLogFlags(&gsv.Settings.CommitLogSettings), }, { Name: "commit-notes", @@ -96,21 +96,21 @@ When flag range is "date", if "end" is YYYY-MM-DD the range will be inclusive.`, Description: `The range filter is used based on git log filters, check https://git-scm.com/docs/git-log for more info. When flag range is "tag" and start is empty, last tag created will be used instead. When flag range is "date", if "end" is YYYY-MM-DD the range will be inclusive.`, - Action: commands.CommitNotesHandler(gsv), + Action: commands.CommitNotesHandler(gsv, &gsv.Settings.CommitNotesSettings), Flags: commands.CommitNotesFlags(&gsv.Settings.CommitNotesSettings), }, { Name: "release-notes", Aliases: []string{"rn"}, Usage: "generate release notes", - Action: commands.ReleaseNotesHandler(gsv), + Action: commands.ReleaseNotesHandler(gsv, &gsv.Settings.ReleaseNotesSettings), Flags: commands.ReleaseNotesFlags(&gsv.Settings.ReleaseNotesSettings), }, { Name: "changelog", Aliases: []string{"cgl"}, Usage: "generate changelog", - Action: commands.ChangelogHandler(gsv), + Action: commands.ChangelogHandler(gsv, &gsv.Settings.ChangelogSettings), Flags: commands.ChangelogFlags(&gsv.Settings.ChangelogSettings), }, {