Skip to content

Commit

Permalink
feat: Add commits to Unreleased when no tags.
Browse files Browse the repository at this point in the history
When there are commits but no tags, then insert those commits under
the unreleased section of the change log output.

This requires get an updated CHANGELOG.md.tpl file.

resolves git-chglog#76

BREAKING CHANGE

* No longer throw the error "no tags" when:
   * No tag query entered
   * There are commits to tag
* Requires an update to the changelog template
  • Loading branch information
b01 committed Jun 25, 2023
1 parent 03f0a44 commit 5bb5d0f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
5 changes: 3 additions & 2 deletions chglog.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (gen *Generator) Generate(w io.Writer, query string) error {
return err
}

if len(versions) == 0 {
if len(versions) == 0 && query != "" {
return fmt.Errorf("commits corresponding to \"%s\" was not found", query)
}

Expand Down Expand Up @@ -284,7 +284,8 @@ func (gen *Generator) getTags(query string) ([]*Tag, string, error) {
}, tags...)
}

if len(tags) == 0 {
// Error only when you explicitly set a query
if len(tags) == 0 && query != "" {
return nil, "", errors.New("git-tag does not exist")
}

Expand Down
9 changes: 4 additions & 5 deletions chglog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func cleanup() {

func TestGeneratorNotFoundTags(t *testing.T) {
assert := assert.New(t)
testName := "not_found"
testName := "no_tags"

setup(testName, func(commit commitFunc, _ tagFunc, _ gitcmd.Client) {
commit("2018-01-01 00:00:00", "feat(*): New feature", "")
Expand All @@ -89,11 +89,10 @@ func TestGeneratorNotFoundTags(t *testing.T) {

buf := &bytes.Buffer{}
err := gen.Generate(buf, "")
expected := strings.TrimSpace(buf.String())
actual := strings.TrimSpace(buf.String())

assert.Error(err)
assert.Contains(err.Error(), "git-tag does not exist")
assert.Equal("", expected)
assert.Equal(nil, err)
assert.Contains(actual, "<a name=\"unreleased\"></a>\n## [Unreleased]")
}

func TestGeneratorNotFoundCommits(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions cmd/git-chglog/kac_template_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (t *kacTemplateBuilderImpl) unreleased(style, format string) string {
title = fmt.Sprintf("[%s]", title)
}

return fmt.Sprintf(`{{ if .Versions -}}
return fmt.Sprintf(`{{ if .Unreleased -}}
%s## %s
{{ if .Unreleased.CommitGroups -}}
Expand Down Expand Up @@ -156,24 +156,24 @@ func (*kacTemplateBuilderImpl) footer(style string) string {
case styleGitHub, styleGitLab:
return `
{{- if .Versions }}
{{- if .Unreleased }}
[Unreleased]: {{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD
{{ end -}}
{{ range .Versions -}}
{{ if .Tag.Previous -}}
[{{ .Tag.Name }}]: {{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}
{{ end -}}
{{ end -}}
{{ end -}}`
case styleBitbucket:
return `
{{- if .Versions }}
{{- if .Unreleased }}
[Unreleased]: {{ .Info.RepositoryURL }}/compare/HEAD..{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}
{{ end -}}
{{ range .Versions -}}
{{ if .Tag.Previous -}}
[{{ .Tag.Name }}]: {{ $.Info.RepositoryURL }}/compare/{{ .Tag.Name }}..{{ .Tag.Previous.Name }}
{{ end -}}
{{ end -}}
{{ end -}}`
default:
return ""
Expand Down
10 changes: 5 additions & 5 deletions cmd/git-chglog/kac_template_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestKACTemplateBuilderDefault(t *testing.T) {
})

assert.Nil(err)
assert.Equal(`{{ if .Versions -}}
assert.Equal(`{{ if .Unreleased -}}
<a name="unreleased"></a>
## [Unreleased]
Expand Down Expand Up @@ -67,13 +67,13 @@ func TestKACTemplateBuilderDefault(t *testing.T) {
{{ end -}}
{{ end -}}
{{- if .Versions }}
{{- if .Unreleased }}
[Unreleased]: {{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD
{{ end -}}
{{ range .Versions -}}
{{ if .Tag.Previous -}}
[{{ .Tag.Name }}]: {{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}
{{ end -}}
{{ end -}}
{{ end -}}`, out)
}

Expand All @@ -90,7 +90,7 @@ func TestKACTemplateBuilderNone(t *testing.T) {
})

assert.Nil(err)
assert.Equal(`{{ if .Versions -}}
assert.Equal(`{{ if .Unreleased -}}
## Unreleased
{{ if .Unreleased.CommitGroups -}}
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestKACTemplateBuilderSubject(t *testing.T) {
})

assert.Nil(err)
assert.Equal(`{{ if .Versions -}}
assert.Equal(`{{ if .Unreleased -}}
## Unreleased
{{ if .Unreleased.CommitGroups -}}
Expand Down
49 changes: 49 additions & 0 deletions testdata/no_tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{{ if .Unreleased -}}
<a name="unreleased"></a>
## [Unreleased]

{{ if .Unreleased.CommitGroups -}}
{{ range .Unreleased.CommitGroups -}}
### {{ .Title }}
{{ range .Commits -}}
- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}

{{ range .Versions }}
<a name="{{ .Tag.Name }}"></a>
## {{ if .Tag.Previous }}[{{ .Tag.Name }}]{{ else }}{{ .Tag.Name }}{{ end }} - {{ datetime "2006-01-02" .Tag.Date }}
{{ range .CommitGroups -}}
### {{ .Title }}
{{ range .Commits -}}
- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}
{{ end }}
{{ end -}}

{{- if .RevertCommits -}}
### Reverts
{{ range .RevertCommits -}}
- {{ .Revert.Header }}
{{ end }}
{{ end -}}

{{- if .NoteGroups -}}
{{ range .NoteGroups -}}
### {{ .Title }}
{{ range .Notes }}
{{ .Body }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}

{{- if .Versions }}
[Unreleased]: {{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD
{{ range .Versions -}}
{{ if .Tag.Previous -}}
[{{ .Tag.Name }}]: {{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}
{{ end -}}
{{ end -}}
{{ end -}}

0 comments on commit 5bb5d0f

Please sign in to comment.