Skip to content

Commit

Permalink
feat: Add Unreleased field to RenderData
Browse files Browse the repository at this point in the history
  • Loading branch information
wadackel committed May 3, 2018
1 parent f65b08d commit 5ce1760
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
61 changes: 46 additions & 15 deletions chglog.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ type Info struct {

// RenderData is the data passed to the template
type RenderData struct {
Info *Info
Versions []*Version
Info *Info
Unreleased *Unreleased
Versions []*Version
}

// Config for generating CHANGELOG
Expand Down Expand Up @@ -126,21 +127,29 @@ func (gen *Generator) Generate(w io.Writer, query string) error {
}
defer back()

versions, err := gen.readVersions(query)
tags, first, err := gen.getTags(query)
if err != nil {
return err
}

return gen.render(w, versions)
}

func (gen *Generator) readVersions(query string) ([]*Version, error) {
tags, first, err := gen.getTags(query)
unreleased, err := gen.readUnreleased(tags)
if err != nil {
return err
}

versions, err := gen.readVersions(tags, first)
if err != nil {
return nil, err
return err
}

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

return gen.render(w, unreleased, versions)
}

func (gen *Generator) readVersions(tags []*Tag, first string) ([]*Version, error) {
versions := []*Version{}

for i, tag := range tags {
Expand Down Expand Up @@ -173,11 +182,32 @@ func (gen *Generator) readVersions(query string) ([]*Version, error) {
})
}

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

func (gen *Generator) readUnreleased(tags []*Tag) (*Unreleased, error) {
rev := "HEAD"

if len(tags) > 0 {
rev = tags[0].Name + "..HEAD"
}

return versions, nil
commits, err := gen.commitParser.Parse(rev)
if err != nil {
return nil, err
}

commitGroups, mergeCommits, revertCommits, noteGroups := gen.commitExtractor.Extract(commits)

unreleased := &Unreleased{
CommitGroups: commitGroups,
Commits: commits,
MergeCommits: mergeCommits,
RevertCommits: revertCommits,
NoteGroups: noteGroups,
}

return unreleased, nil
}

func (gen *Generator) getTags(query string) ([]*Tag, string, error) {
Expand Down Expand Up @@ -217,7 +247,7 @@ func (gen *Generator) workdir() (func() error, error) {
}, nil
}

func (gen *Generator) render(w io.Writer, versions []*Version) error {
func (gen *Generator) render(w io.Writer, unreleased *Unreleased, versions []*Version) error {
if _, err := os.Stat(gen.config.Template); err != nil {
return err
}
Expand All @@ -233,7 +263,8 @@ func (gen *Generator) render(w io.Writer, versions []*Version) error {
t := template.Must(template.New(fname).Funcs(fmap).ParseFiles(gen.config.Template))

return t.Execute(w, &RenderData{
Info: gen.config.Info,
Versions: versions,
Info: gen.config.Info,
Unreleased: unreleased,
Versions: versions,
})
}
10 changes: 10 additions & 0 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,13 @@ type Version struct {
RevertCommits []*Commit
NoteGroups []*NoteGroup
}

// Unreleased is unreleased commit dataset
type Unreleased struct {
Tag *Tag
CommitGroups []*CommitGroup
Commits []*Commit
MergeCommits []*Commit
RevertCommits []*Commit
NoteGroups []*NoteGroup
}

0 comments on commit 5ce1760

Please sign in to comment.