From 41be35e83dacb800b6cf8ad26c5f2662f02c16f7 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Mon, 25 Jan 2021 02:51:42 -0300 Subject: [PATCH 1/8] feat: add range and date at commit log action related to #7 --- cmd/git-sv/handlers.go | 50 ++++++++++++++++++----- cmd/git-sv/main.go | 7 +++- sv/git.go | 90 +++++++++++++++++++++++++++++++++--------- 3 files changed, 119 insertions(+), 28 deletions(-) diff --git a/cmd/git-sv/handlers.go b/cmd/git-sv/handlers.go index a9f5319..aa7f99c 100644 --- a/cmd/git-sv/handlers.go +++ b/cmd/git-sv/handlers.go @@ -36,7 +36,7 @@ func nextVersionHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) f return fmt.Errorf("error parsing version: %s from describe, message: %v", describe, err) } - commits, err := git.Log(describe, "") + commits, err := git.Log(sv.NewLogRange(sv.TagRange, describe, "")) if err != nil { return fmt.Errorf("error getting git log, message: %v", err) } @@ -51,11 +51,23 @@ func commitLogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) fun return func(c *cli.Context) error { var commits []sv.GitCommitLog var err error + tagFlag := c.String("t") + rangeFlag := c.String("r") + startFlag := c.String("s") + endFlag := c.String("e") + if tagFlag != "" && (rangeFlag != "" || startFlag != "" || endFlag != "") { + return fmt.Errorf("cannot define tag flag with range, start or end flags") + } - if tag := c.String("t"); tag != "" { - commits, err = getTagCommits(git, tag) + if tagFlag != "" { + commits, err = getTagCommits(git, tagFlag) } else { - commits, err = git.Log(git.Describe(), "") + // commits, err = git.Log(sv.NewLogRange(sv.TagRange, git.Describe(), "")) + r, rerr := logRange(git, rangeFlag, startFlag, endFlag) + if rerr != nil { + return rerr + } + commits, err = git.Log(r) } if err != nil { return fmt.Errorf("error getting git log, message: %v", err) @@ -77,7 +89,20 @@ func getTagCommits(git sv.Git, tag string) ([]sv.GitCommitLog, error) { if err != nil { return nil, err } - return git.Log(prev, tag) + return git.Log(sv.NewLogRange(sv.TagRange, prev, tag)) +} + +func logRange(git sv.Git, rangeFlag, startFlag, endFlag string) (sv.LogRange, error) { + switch rangeFlag { + case "", string(sv.TagRange): + return sv.NewLogRange(sv.TagRange, str(startFlag, git.Describe()), endFlag), nil + case string(sv.DateRange): + return sv.NewLogRange(sv.DateRange, startFlag, endFlag), nil + case string(sv.HashRange): + return sv.NewLogRange(sv.HashRange, startFlag, endFlag), nil + default: + return sv.LogRange{}, fmt.Errorf("invalid range: %s, expected: %s, %s or %s", rangeFlag, sv.TagRange, sv.DateRange, sv.HashRange) + } } func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor, outputFormatter sv.OutputFormatter) func(c *cli.Context) error { @@ -114,7 +139,7 @@ func getTagVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, ta return semver.Version{}, time.Time{}, nil, fmt.Errorf("error listing tags, message: %v", err) } - commits, err := git.Log(previousTag, tag) + commits, err := git.Log(sv.NewLogRange(sv.TagRange, previousTag, tag)) if err != nil { return semver.Version{}, time.Time{}, nil, fmt.Errorf("error getting git log from tag: %s, message: %v", tag, err) } @@ -157,7 +182,7 @@ func getNextVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) ( return semver.Version{}, time.Time{}, nil, fmt.Errorf("error parsing version: %s from describe, message: %v", describe, err) } - commits, err := git.Log(describe, "") + commits, err := git.Log(sv.NewLogRange(sv.TagRange, describe, "")) if err != nil { return semver.Version{}, time.Time{}, nil, fmt.Errorf("error getting git log, message: %v", err) } @@ -174,7 +199,7 @@ func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) func(c *c return fmt.Errorf("error parsing version: %s from describe, message: %v", describe, err) } - commits, err := git.Log(describe, "") + commits, err := git.Log(sv.NewLogRange(sv.TagRange, describe, "")) if err != nil { return fmt.Errorf("error getting git log, message: %v", err) } @@ -276,7 +301,7 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP previousTag = tags[i+1].Name } - commits, err := git.Log(previousTag, tag.Name) + commits, err := git.Log(sv.NewLogRange(sv.TagRange, previousTag, tag.Name)) if err != nil { return fmt.Errorf("error getting git log from tag: %s, message: %v", tag.Name, err) } @@ -348,3 +373,10 @@ func appendOnFile(message, filepath string) error { _, err = f.WriteString(message) return err } + +func str(value, defaultValue string) string { + if value != "" { + return value + } + return defaultValue +} diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index bb10f57..0cf575a 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -44,7 +44,12 @@ func main() { Aliases: []string{"cl"}, Usage: "list all commit logs since last version as jsons", Action: commitLogHandler(git, semverProcessor), - Flags: []cli.Flag{&cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from tag"}}, + Flags: []cli.Flag{ + &cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from a specific tag"}, + &cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash"}, + &cli.StringFlag{Name: "s", Aliases: []string{"start"}, Usage: "start range"}, + &cli.StringFlag{Name: "e", Aliases: []string{"end"}, Usage: "end range"}, + }, }, { Name: "release-notes", diff --git a/sv/git.go b/sv/git.go index c5c8cbe..e4bec74 100644 --- a/sv/git.go +++ b/sv/git.go @@ -26,7 +26,7 @@ const ( // Git commands type Git interface { Describe() string - Log(initialTag, endTag string) ([]GitCommitLog, error) + Log(lr LogRange) ([]GitCommitLog, error) Commit(header, body, footer string) error Tag(version semver.Version) error Tags() ([]GitTag, error) @@ -35,6 +35,7 @@ type Git interface { // GitCommitLog description of a single commit log type GitCommitLog struct { + Date string `json:"date,omitempty"` Hash string `json:"hash,omitempty"` Type string `json:"type,omitempty"` Scope string `json:"scope,omitempty"` @@ -49,6 +50,28 @@ type GitTag struct { Date time.Time } +// LogRangeType type of log range +type LogRangeType string + +// constants for log range type +const ( + TagRange LogRangeType = "tag" + DateRange = "date" + HashRange = "hash" +) + +// LogRange git log range +type LogRange struct { + rangeType LogRangeType + start string + end string +} + +// NewLogRange LogRange constructor +func NewLogRange(t LogRangeType, start, end string) LogRange { + return LogRange{rangeType: t, start: start, end: end} +} + // GitImpl git command implementation type GitImpl struct { messageMetadata map[string][]string @@ -74,22 +97,27 @@ func (GitImpl) Describe() string { } // Log return git log -func (g GitImpl) Log(initialTag, endTag string) ([]GitCommitLog, error) { - format := "--pretty=format:\"%h" + logSeparator + "%s" + logSeparator + "%b" + endLine + "\"" - var cmd *exec.Cmd - if initialTag == "" && endTag == "" { - cmd = exec.Command("git", "log", format) - } else if endTag == "" { - cmd = exec.Command("git", "log", initialTag+"..HEAD", format) - } else if initialTag == "" { - cmd = exec.Command("git", "log", endTag, format) - } else { - cmd = exec.Command("git", "log", initialTag+".."+endTag, format) +func (g GitImpl) Log(lr LogRange) ([]GitCommitLog, error) { + format := "--pretty=format:\"%ad" + logSeparator + "%h" + logSeparator + "%s" + logSeparator + "%b" + endLine + "\"" + params := []string{"log", "--date=short", format} + + if lr.start != "" || lr.end != "" { + switch lr.rangeType { + case DateRange: + params = append(params, "--since", lr.start, "--until", addDay(lr.end)) + default: + if lr.start == "" { + params = append(params, lr.end) + } else { + params = append(params, lr.start+".."+str(lr.end, "HEAD")) + } + } } + cmd := exec.Command("git", params...) out, err := cmd.CombinedOutput() if err != nil { - return nil, err + return nil, combinedOutputErr(err, out) } return parseLogOutput(g.messageMetadata, string(out)), nil } @@ -121,7 +149,7 @@ func (g GitImpl) Tags() ([]GitTag, error) { cmd := exec.Command("git", "tag", "-l", "--format", "%(taggerdate:iso8601)#%(refname:short)") out, err := cmd.CombinedOutput() if err != nil { - return nil, err + return nil, combinedOutputErr(err, out) } return parseTagsOutput(string(out)) } @@ -163,12 +191,12 @@ func parseLogOutput(messageMetadata map[string][]string, log string) []GitCommit func parseCommitLog(messageMetadata map[string][]string, commit string) GitCommitLog { content := strings.Split(strings.Trim(commit, "\""), logSeparator) - commitType, scope, subject := parseCommitLogMessage(content[1]) + commitType, scope, subject := parseCommitLogMessage(content[2]) metadata := make(map[string]string) for key, prefixes := range messageMetadata { for _, prefix := range prefixes { - if tagValue := extractTag(prefix, content[2]); tagValue != "" { + if tagValue := extractTag(prefix, content[3]); tagValue != "" { metadata[key] = tagValue break } @@ -176,11 +204,12 @@ func parseCommitLog(messageMetadata map[string][]string, commit string) GitCommi } return GitCommitLog{ - Hash: content[0], + Date: content[0], + Hash: content[1], Type: commitType, Scope: scope, Subject: subject, - Body: content[2], + Body: content[3], Metadata: metadata, } } @@ -222,3 +251,28 @@ func splitAt(b []byte) func(data []byte, atEOF bool) (advance int, token []byte, return 0, nil, nil } } + +func addDay(value string) string { + if value == "" { + return value + } + + t, err := time.Parse("2006-01-02", value) + if err != nil { // keep original value if is not date format + return value + } + + return t.AddDate(0, 0, 1).Format("2006-01-02") +} + +func str(value, defaultValue string) string { + if value != "" { + return value + } + return defaultValue +} + +func combinedOutputErr(err error, out []byte) error { + msg := strings.Split(string(out), "\n") + return fmt.Errorf("%v - %s", err, msg[0]) +} From 8b57bee97be225d6b698d40dcba97baacde5adc6 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Mon, 25 Jan 2021 12:43:40 -0300 Subject: [PATCH 2/8] refactor: better description on commit-log --- cmd/git-sv/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index 0cf575a..c23bcb0 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -42,7 +42,7 @@ func main() { { Name: "commit-log", Aliases: []string{"cl"}, - Usage: "list all commit logs since last version as jsons", + Usage: "list all commit logs since last version or according with range as jsons", Action: commitLogHandler(git, semverProcessor), Flags: []cli.Flag{ &cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from a specific tag"}, From a6259a910c85a45af0a17a98ed87b97be75cb6bc Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Mon, 25 Jan 2021 17:16:56 -0300 Subject: [PATCH 3/8] feat: add commit-notes action issue: #7 --- cmd/git-sv/handlers.go | 34 +++++++++++++++++++++++++++++----- cmd/git-sv/main.go | 23 ++++++++++++++++++----- sv/formatter.go | 9 +++++++-- sv/formatter_test.go | 13 ++++++++++--- sv/helpers_test.go | 2 +- sv/releasenotes.go | 6 +++--- sv/releasenotes_test.go | 14 +++++++------- 7 files changed, 75 insertions(+), 26 deletions(-) diff --git a/cmd/git-sv/handlers.go b/cmd/git-sv/handlers.go index aa7f99c..05cb293 100644 --- a/cmd/git-sv/handlers.go +++ b/cmd/git-sv/handlers.go @@ -55,14 +55,13 @@ func commitLogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) fun rangeFlag := c.String("r") startFlag := c.String("s") endFlag := c.String("e") - if tagFlag != "" && (rangeFlag != "" || startFlag != "" || endFlag != "") { + if tagFlag != "" && (rangeFlag != string(sv.TagRange) || startFlag != "" || endFlag != "") { return fmt.Errorf("cannot define tag flag with range, start or end flags") } if tagFlag != "" { commits, err = getTagCommits(git, tagFlag) } else { - // commits, err = git.Log(sv.NewLogRange(sv.TagRange, git.Describe(), "")) r, rerr := logRange(git, rangeFlag, startFlag, endFlag) if rerr != nil { return rerr @@ -94,7 +93,7 @@ func getTagCommits(git sv.Git, tag string) ([]sv.GitCommitLog, error) { func logRange(git sv.Git, rangeFlag, startFlag, endFlag string) (sv.LogRange, error) { switch rangeFlag { - case "", string(sv.TagRange): + case string(sv.TagRange): return sv.NewLogRange(sv.TagRange, str(startFlag, git.Describe()), endFlag), nil case string(sv.DateRange): return sv.NewLogRange(sv.DateRange, startFlag, endFlag), nil @@ -105,6 +104,31 @@ func logRange(git sv.Git, rangeFlag, startFlag, endFlag string) (sv.LogRange, er } } +func commitNotesHandler(git sv.Git, rnProcessor sv.ReleaseNoteProcessor, outputFormatter sv.OutputFormatter) func(c *cli.Context) error { + return func(c *cli.Context) error { + var date time.Time + + rangeFlag := c.String("r") + lr, err := logRange(git, rangeFlag, c.String("s"), c.String("e")) + if err != nil { + return err + } + + commits, err := git.Log(lr) + if err != nil { + return fmt.Errorf("error getting git log from range: %s, message: %v", rangeFlag, err) + } + + if len(commits) > 0 { + date, _ = time.Parse("2006-01-02", commits[0].Date) + } + + releasenote := rnProcessor.Create(nil, date, commits) + fmt.Println(outputFormatter.FormatReleaseNote(releasenote)) + return nil + } +} + func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor, outputFormatter sv.OutputFormatter) func(c *cli.Context) error { return func(c *cli.Context) error { var commits []sv.GitCommitLog @@ -122,7 +146,7 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, return err } - releasenote := rnProcessor.Create(rnVersion, date, commits) + releasenote := rnProcessor.Create(&rnVersion, date, commits) fmt.Println(outputFormatter.FormatReleaseNote(releasenote)) return nil } @@ -310,7 +334,7 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP if err != nil { return fmt.Errorf("error parsing version: %s from describe, message: %v", tag.Name, err) } - releaseNotes = append(releaseNotes, rnProcessor.Create(currentVer, tag.Date, commits)) + releaseNotes = append(releaseNotes, rnProcessor.Create(¤tVer, tag.Date, commits)) } fmt.Println(formatter.FormatChangelog(releaseNotes)) diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index c23bcb0..11de0e1 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -40,13 +40,26 @@ func main() { Action: nextVersionHandler(git, semverProcessor), }, { - Name: "commit-log", - Aliases: []string{"cl"}, - Usage: "list all commit logs since last version or according with range as jsons", - Action: commitLogHandler(git, semverProcessor), + Name: "commit-log", + Aliases: []string{"cl"}, + Usage: "list all commit logs according to range as jsons", + Description: "when flag range is \"tag\" and start is empty, last tag created will be used instead", + Action: commitLogHandler(git, semverProcessor), Flags: []cli.Flag{ &cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from a specific tag"}, - &cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash"}, + &cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash", Value: string(sv.TagRange)}, + &cli.StringFlag{Name: "s", Aliases: []string{"start"}, Usage: "start range"}, + &cli.StringFlag{Name: "e", Aliases: []string{"end"}, Usage: "end range"}, + }, + }, + { + Name: "commit-notes", + Aliases: []string{"cn"}, + Usage: "create a release notes according to range", + Description: "when flag range is \"tag\" and start is empty, last tag created will be used instead", + Action: commitNotesHandler(git, releasenotesProcessor, outputFormatter), + Flags: []cli.Flag{ + &cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash", Required: true}, &cli.StringFlag{Name: "s", Aliases: []string{"start"}, Usage: "start range"}, &cli.StringFlag{Name: "e", Aliases: []string{"end"}, Usage: "end range"}, }, diff --git a/sv/formatter.go b/sv/formatter.go index 8e83d76..c0d7e05 100644 --- a/sv/formatter.go +++ b/sv/formatter.go @@ -40,7 +40,7 @@ const ( {{- end}} {{- end}}` - rnTemplate = `## v{{.Version}}{{if .Date}} ({{.Date}}){{end}} + rnTemplate = `## {{if .Version}}v{{.Version}}{{end}}{{if and .Date .Version}} ({{end}}{{.Date}}{{if and .Version .Date}}){{end}} {{- template "rnSection" .Sections.feat}} {{- template "rnSection" .Sections.fix}} {{- template "rnSectionBreakingChanges" .BreakingChanges}} @@ -93,8 +93,13 @@ func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables if !releasenote.Date.IsZero() { date = releasenote.Date.Format("2006-01-02") } + + var version = "" + if releasenote.Version != nil { + version = fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch()) + } return releaseNoteTemplateVariables{ - Version: fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch()), + Version: version, Date: date, Sections: releasenote.Sections, BreakingChanges: releasenote.BreakingChanges, diff --git a/sv/formatter_test.go b/sv/formatter_test.go index d60c7f9..ddc21dd 100644 --- a/sv/formatter_test.go +++ b/sv/formatter_test.go @@ -11,6 +11,8 @@ var dateChangelog = `## v1.0.0 (2020-05-01) ` var emptyDateChangelog = `## v1.0.0 ` +var emptyVersionChangelog = `## 2020-05-01 +` func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) { date, _ := time.Parse("2006-01-02", "2020-05-01") @@ -20,8 +22,9 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) { input ReleaseNote want string }{ - {"", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog}, - {"", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog}, + {"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog}, + {"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog}, + {"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -33,8 +36,12 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) { } func emptyReleaseNote(version string, date time.Time) ReleaseNote { + var v *semver.Version + if version != "" { + v = semver.MustParse(version) + } return ReleaseNote{ - Version: *semver.MustParse(version), + Version: v, Date: date, } } diff --git a/sv/helpers_test.go b/sv/helpers_test.go index 69b6656..c35642e 100644 --- a/sv/helpers_test.go +++ b/sv/helpers_test.go @@ -19,7 +19,7 @@ func commitlog(t string, metadata map[string]string) GitCommitLog { } } -func releaseNote(version semver.Version, date time.Time, sections map[string]ReleaseNoteSection, breakingChanges []string) ReleaseNote { +func releaseNote(version *semver.Version, date time.Time, sections map[string]ReleaseNoteSection, breakingChanges []string) ReleaseNote { return ReleaseNote{ Version: version, Date: date.Truncate(time.Minute), diff --git a/sv/releasenotes.go b/sv/releasenotes.go index 9a30f41..823e113 100644 --- a/sv/releasenotes.go +++ b/sv/releasenotes.go @@ -8,7 +8,7 @@ import ( // ReleaseNoteProcessor release note processor interface. type ReleaseNoteProcessor interface { - Create(version semver.Version, date time.Time, commits []GitCommitLog) ReleaseNote + Create(version *semver.Version, date time.Time, commits []GitCommitLog) ReleaseNote } // ReleaseNoteProcessorImpl release note based on commit log. @@ -22,7 +22,7 @@ func NewReleaseNoteProcessor(tags map[string]string) *ReleaseNoteProcessorImpl { } // Create create a release note based on commits. -func (p ReleaseNoteProcessorImpl) Create(version semver.Version, date time.Time, commits []GitCommitLog) ReleaseNote { +func (p ReleaseNoteProcessorImpl) Create(version *semver.Version, date time.Time, commits []GitCommitLog) ReleaseNote { sections := make(map[string]ReleaseNoteSection) var breakingChanges []string for _, commit := range commits { @@ -44,7 +44,7 @@ func (p ReleaseNoteProcessorImpl) Create(version semver.Version, date time.Time, // ReleaseNote release note. type ReleaseNote struct { - Version semver.Version + Version *semver.Version Date time.Time Sections map[string]ReleaseNoteSection BreakingChanges []string diff --git a/sv/releasenotes_test.go b/sv/releasenotes_test.go index 4ca20b6..8c8f032 100644 --- a/sv/releasenotes_test.go +++ b/sv/releasenotes_test.go @@ -13,31 +13,31 @@ func TestReleaseNoteProcessorImpl_Create(t *testing.T) { tests := []struct { name string - version semver.Version + version *semver.Version date time.Time commits []GitCommitLog want ReleaseNote }{ { name: "mapped tag", - version: *semver.MustParse("1.0.0"), + version: semver.MustParse("1.0.0"), date: date, commits: []GitCommitLog{commitlog("t1", map[string]string{})}, - want: releaseNote(*semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), + want: releaseNote(semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), }, { name: "unmapped tag", - version: *semver.MustParse("1.0.0"), + version: semver.MustParse("1.0.0"), date: date, commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{})}, - want: releaseNote(*semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), + want: releaseNote(semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, nil), }, { name: "breaking changes tag", - version: *semver.MustParse("1.0.0"), + version: semver.MustParse("1.0.0"), date: date, commits: []GitCommitLog{commitlog("t1", map[string]string{}), commitlog("unmapped", map[string]string{"breakingchange": "breaks"})}, - want: releaseNote(*semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, []string{"breaks"}), + want: releaseNote(semver.MustParse("1.0.0"), date, map[string]ReleaseNoteSection{"t1": newReleaseNoteSection("Tag 1", []GitCommitLog{commitlog("t1", map[string]string{})})}, []string{"breaks"}), }, } for _, tt := range tests { From 997c604a863202f6678d631a238097008918fb0d Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Mon, 25 Jan 2021 17:22:20 -0300 Subject: [PATCH 4/8] refactor: change commit-notes description --- cmd/git-sv/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index 11de0e1..c9da92a 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -55,7 +55,7 @@ func main() { { Name: "commit-notes", Aliases: []string{"cn"}, - Usage: "create a release notes according to range", + Usage: "generate a commit notes according to range", Description: "when flag range is \"tag\" and start is empty, last tag created will be used instead", Action: commitNotesHandler(git, releasenotesProcessor, outputFormatter), Flags: []cli.Flag{ From 1d0a7777b873433093e86eb7c3e3e58f8c05eb39 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Mon, 25 Jan 2021 17:23:10 -0300 Subject: [PATCH 5/8] docs: add commit-notes on readme issue: #7 --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8b3883a..abf4737 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,8 @@ git-sv rn -h | ---------------------------- | ------------------------------------------------------------- | :----------------: | | current-version, cv | get last released version from git | :x: | | next-version, nv | generate the next version based on git commit messages | :x: | -| commit-log, cl | list all commit logs since last version as jsons | :heavy_check_mark: | +| commit-log, cl | list all commit logs according to range as jsons | :heavy_check_mark: | +| commit-notes, cl | generate a commit notes according to range | :heavy_check_mark: | | release-notes, rn | generate release notes | :heavy_check_mark: | | changelog, cgl | generate changelog | :heavy_check_mark: | | tag, tg | generate tag with version based on git commit messages | :x: | From 3652c524780baa8653b36806d8ed49c1342d99b8 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Mon, 25 Jan 2021 17:25:56 -0300 Subject: [PATCH 6/8] docs: fix commit-notes alias on readme issue: #7 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index abf4737..ad811e2 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ git-sv rn -h | current-version, cv | get last released version from git | :x: | | next-version, nv | generate the next version based on git commit messages | :x: | | commit-log, cl | list all commit logs according to range as jsons | :heavy_check_mark: | -| commit-notes, cl | generate a commit notes according to range | :heavy_check_mark: | +| commit-notes, cn | generate a commit notes according to range | :heavy_check_mark: | | release-notes, rn | generate release notes | :heavy_check_mark: | | changelog, cgl | generate changelog | :heavy_check_mark: | | tag, tg | generate tag with version based on git commit messages | :x: | From 741962bd23305ebc177f1b1cf48e63667534d425 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Mon, 25 Jan 2021 18:01:35 -0300 Subject: [PATCH 7/8] refactor: change start and end descriptions issue: #7 --- cmd/git-sv/main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index c9da92a..43894ee 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -43,25 +43,25 @@ func main() { Name: "commit-log", Aliases: []string{"cl"}, Usage: "list all commit logs according to range as jsons", - Description: "when flag range is \"tag\" and start is empty, last tag created will be used instead", + 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: commitLogHandler(git, semverProcessor), Flags: []cli.Flag{ &cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from a specific tag"}, &cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash", Value: string(sv.TagRange)}, - &cli.StringFlag{Name: "s", Aliases: []string{"start"}, Usage: "start range"}, - &cli.StringFlag{Name: "e", Aliases: []string{"end"}, Usage: "end range"}, + &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"}, + &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: "commit-notes", Aliases: []string{"cn"}, Usage: "generate a commit notes according to range", - Description: "when flag range is \"tag\" and start is empty, last tag created will be used instead", + 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: commitNotesHandler(git, releasenotesProcessor, outputFormatter), Flags: []cli.Flag{ &cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash", Required: true}, - &cli.StringFlag{Name: "s", Aliases: []string{"start"}, Usage: "start range"}, - &cli.StringFlag{Name: "e", Aliases: []string{"end"}, Usage: "end range"}, + &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"}, + &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"}, }, }, { From a6df0327937a3a2bad25a1aec5c0bff024c266a3 Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Mon, 25 Jan 2021 18:24:25 -0300 Subject: [PATCH 8/8] docs: add range section issue: #7 --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index ad811e2..7f11866 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,26 @@ git-sv rn -h | validate-commit-message, vcm | use as prepare-commit-message hook to validate commit message | :heavy_check_mark: | | help, h | shows a list of commands or help for one command | :x: | +##### Use range + +Commands like `commit-log` and `commit-notes` has a range option. Supported range types are: `tag`, `date` and `hash`. + +By default, it's used [--date=short](https://git-scm.com/docs/git-log#Documentation/git-log.txt---dateltformatgt) at `git log`, all dates returned from it will be in `YYYY-MM-DD` format. + +Range `tag` will use `git describe` to get the last tag available if `start` is empty, the others types won't use the existing tags, it's recommended to always use a start limit in a old repository with a lot of commits. This behavior was maintained to not break the retrocompatibility. + +Range `date` use git log `--since` and `--until`, it's possible to use all supported formats from [git log](https://git-scm.com/docs/git-log#Documentation/git-log.txt---sinceltdategt), if `end` is in `YYYY-MM-DD` format, `sv` will add a day on git log command to make the end date inclusive. + +Range `tag` and `hash` are used on git log [revision range](https://git-scm.com/docs/git-log#Documentation/git-log.txt-ltrevisionrangegt). If `end` is empty, `HEAD` will be used instead. + +```bash +# get commit log as json using a inclusive range +git-sv commit-log --range hash --start 7ea9306~1 --end c444318 + +# return all commits after last tag +git-sv commit-log --range tag +``` + ##### Use validate-commit-message as prepare-commit-msg hook Configure your .git/hooks/prepare-commit-msg