diff --git a/README.md b/README.md index 8b3883a..7f11866 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, 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: | @@ -72,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 diff --git a/cmd/git-sv/handlers.go b/cmd/git-sv/handlers.go index a9f5319..05cb293 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,22 @@ 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 != string(sv.TagRange) || 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(), "") + 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 +88,45 @@ 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 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 { @@ -97,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 } @@ -114,7 +163,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 +206,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 +223,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 +325,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) } @@ -285,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)) @@ -348,3 +397,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..43894ee 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -40,11 +40,29 @@ func main() { Action: nextVersionHandler(git, semverProcessor), }, { - Name: "commit-log", - 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"}}, + Name: "commit-log", + Aliases: []string{"cl"}, + Usage: "list all commit logs according to range as jsons", + 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 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: "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 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: "release-notes", 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/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]) +} 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 {