mirror of
https://github.com/git-chglog/git-chglog.git
synced 2026-01-23 02:15:12 +00:00
feat: First implement
This commit is contained in:
parent
a44743ef3f
commit
6caf676beb
105 changed files with 20966 additions and 0 deletions
180
commit_extractor.go
Normal file
180
commit_extractor.go
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
package chglog
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type commitExtractor struct {
|
||||
opts *Options
|
||||
}
|
||||
|
||||
func newCommitExtractor(opts *Options) *commitExtractor {
|
||||
return &commitExtractor{
|
||||
opts: opts,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *commitExtractor) Extract(commits []*Commit) ([]*CommitGroup, []*NoteGroup) {
|
||||
filteredCommits := commitFilter(commits, e.opts.CommitFilters)
|
||||
commitGroups := []*CommitGroup{}
|
||||
noteGroups := []*NoteGroup{}
|
||||
|
||||
for _, commit := range filteredCommits {
|
||||
e.processCommitGroups(&commitGroups, commit)
|
||||
e.processNoteGroups(¬eGroups, commit)
|
||||
}
|
||||
|
||||
e.sortCommitGroups(commitGroups)
|
||||
e.sortNoteGroups(noteGroups)
|
||||
|
||||
return commitGroups, noteGroups
|
||||
}
|
||||
|
||||
func (e *commitExtractor) processCommitGroups(groups *[]*CommitGroup, commit *Commit) {
|
||||
var group *CommitGroup
|
||||
|
||||
// commit group
|
||||
raw, ttl := e.commitGroupTitle(commit)
|
||||
|
||||
for _, g := range *groups {
|
||||
if g.RawTitle == raw {
|
||||
group = g
|
||||
}
|
||||
}
|
||||
|
||||
if group != nil {
|
||||
group.Commits = append(group.Commits, commit)
|
||||
} else if raw != "" {
|
||||
*groups = append(*groups, &CommitGroup{
|
||||
RawTitle: raw,
|
||||
Title: ttl,
|
||||
Commits: []*Commit{commit},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (e *commitExtractor) processNoteGroups(groups *[]*NoteGroup, commit *Commit) {
|
||||
if len(commit.Notes) != 0 {
|
||||
// notes
|
||||
for _, note := range commit.Notes {
|
||||
e.appendNoteToNoteGroups(groups, note)
|
||||
}
|
||||
} else if commit.Merge != nil && e.opts.MergeNoteTitle != "" {
|
||||
// merges
|
||||
e.appendNoteToNoteGroups(groups, &Note{
|
||||
Title: e.opts.MergeNoteTitle,
|
||||
Body: commit.Header,
|
||||
})
|
||||
} else if commit.Revert != nil && e.opts.RevertNoteTitle != "" {
|
||||
// reverts
|
||||
e.appendNoteToNoteGroups(groups, &Note{
|
||||
Title: e.opts.RevertNoteTitle,
|
||||
Body: commit.Header,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (e *commitExtractor) appendNoteToNoteGroups(groups *[]*NoteGroup, note *Note) {
|
||||
exist := false
|
||||
|
||||
for _, g := range *groups {
|
||||
if g.Title == note.Title {
|
||||
exist = true
|
||||
g.Notes = append(g.Notes, note)
|
||||
}
|
||||
}
|
||||
|
||||
if !exist {
|
||||
*groups = append(*groups, &NoteGroup{
|
||||
Title: note.Title,
|
||||
Notes: []*Note{note},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (e *commitExtractor) commitGroupTitle(commit *Commit) (string, string) {
|
||||
var (
|
||||
raw string
|
||||
ttl string
|
||||
)
|
||||
|
||||
if title, ok := dotGet(commit, e.opts.CommitGroupBy); ok {
|
||||
if v, ok := title.(string); ok {
|
||||
raw = v
|
||||
if t, ok := e.opts.CommitGroupTitleMaps[v]; ok {
|
||||
ttl = t
|
||||
} else {
|
||||
ttl = strings.Title(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return raw, ttl
|
||||
}
|
||||
|
||||
func (e *commitExtractor) sortCommitGroups(groups []*CommitGroup) {
|
||||
// groups
|
||||
sort.Slice(groups, func(i, j int) bool {
|
||||
var (
|
||||
a, b interface{}
|
||||
ok bool
|
||||
)
|
||||
|
||||
a, ok = dotGet(groups[i], e.opts.CommitGroupSortBy)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
b, ok = dotGet(groups[j], e.opts.CommitGroupSortBy)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
res, err := compare(a, "<", b)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return res
|
||||
})
|
||||
|
||||
// commits
|
||||
for _, group := range groups {
|
||||
sort.Slice(group.Commits, func(i, j int) bool {
|
||||
var (
|
||||
a, b interface{}
|
||||
ok bool
|
||||
)
|
||||
|
||||
a, ok = dotGet(group.Commits[i], e.opts.CommitSortBy)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
b, ok = dotGet(group.Commits[j], e.opts.CommitSortBy)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
res, err := compare(a, "<", b)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (e *commitExtractor) sortNoteGroups(groups []*NoteGroup) {
|
||||
// groups
|
||||
sort.Slice(groups, func(i, j int) bool {
|
||||
return strings.ToLower(groups[i].Title) < strings.ToLower(groups[j].Title)
|
||||
})
|
||||
|
||||
// notes
|
||||
for _, group := range groups {
|
||||
sort.Slice(group.Notes, func(i, j int) bool {
|
||||
return strings.ToLower(group.Notes[i].Title) < strings.ToLower(group.Notes[j].Title)
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue