Skip to content

Commit

Permalink
Merge pull request #8 from bvieira/commit-notes
Browse files Browse the repository at this point in the history
Feature: commit notes command
  • Loading branch information
bvieira authored Jan 25, 2021
2 parents 02fce48 + a6df032 commit ba56427
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 51 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,35 @@ 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: |
| commit, cmt | execute git commit with convetional commit message helper | :x: |
| 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
Expand Down
78 changes: 67 additions & 11 deletions cmd/git-sv/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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(&currentVer, tag.Date, commits))
}

fmt.Println(formatter.FormatChangelog(releaseNotes))
Expand Down Expand Up @@ -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
}
28 changes: 23 additions & 5 deletions cmd/git-sv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions sv/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 10 additions & 3 deletions sv/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand All @@ -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,
}
}
Loading

0 comments on commit ba56427

Please sign in to comment.